Skip to content

Commit bc0b3df

Browse files
committed
Readme and jsdoc tweaks
1 parent 3a46ee6 commit bc0b3df

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## v2.1.0 2018-09-14
3+
## v2.1.0 2018-09-17
44

55
* Close file if its file name is already changed even if there is no new data
66
written: this check is made by interval timer.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
This module creates
88
[stream.Writable](https://nodejs.org/api/stream.html#stream_class_stream_writable)
9-
to a file which is automatically rotated based on current time.
9+
to a file which is automatically rotated based on current time and uses
10+
[strftime](https://www.npmjs.com/package/strftime) template for file names.
1011

1112
## Requirements
1213

@@ -111,7 +112,7 @@ Readonly public properties based on contructor's options:
111112
Protected properties for custom subclass:
112113

113114
* `currentFilename` contains last opened filename
114-
* `stream` contains
115+
* `stream` contains current
115116
[fs.WriteStream](https://nodejs.org/api/fs.html#fs_class_fs_writestream)
116117
object
117118

src/file-timestamp-stream.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import strftime from 'ultra-strftime'
99
const finished = require('stream.finished') as (stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream, callback?: (err: NodeJS.ErrnoException) => void) => () => void // TODO: wait for new typings for node
1010

1111
export interface FileTimestampStreamOptions extends WritableOptions {
12+
/** a string with [flags](https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback) for opened stream (default: `'a'`) */
1213
flags?: string | null
14+
/** a custom [fs](https://nodejs.org/api/fs.html) module (optional) */
1315
fs?: typeof fs
16+
/** a template for new filenames (default: `'out.log'`) */
1417
path?: string
1518
}
1619

@@ -21,15 +24,17 @@ export class FileTimestampStream extends Writable {
2124
readonly fs: typeof fs
2225
readonly path: string
2326

27+
/** contains last opened filename */
2428
protected currentFilename?: string
29+
/** contains current [fs.WriteStream](https://nodejs.org/api/fs.html#fs_class_fs_writestream) object */
2530
protected stream?: WriteStream
2631

2732
private destroyed = false
2833
private streams: Map<string, WriteStream> = new Map()
2934
private streamCancelFinishers: Map<string, () => void> = new Map()
3035
private streamErrorHandlers: Map<string, (err: Error) => void> = new Map()
31-
private timer?: Interval
32-
private timers: Map<string, Interval> = new Map()
36+
private closer?: Interval
37+
private closers: Map<string, Interval> = new Map()
3338

3439
constructor (options: FileTimestampStreamOptions = {}) {
3540
super(options)
@@ -109,32 +114,37 @@ export class FileTimestampStream extends Writable {
109114
}
110115
this.streams.clear()
111116
}
112-
if (this.timers.size > 0) {
113-
for (const timer of this.timers.values()) {
117+
if (this.closers.size > 0) {
118+
for (const timer of this.closers.values()) {
114119
timer.remove()
115120
}
116121
this.streams.clear()
117122
}
118123

119124
this.destroyed = true
120125
this.stream = undefined
121-
this.timer = undefined
126+
this.closer = undefined
122127

123128
callback(error)
124129
}
125130

126-
/** Override this */
131+
/**
132+
* This method can be overriden in subclass
133+
*
134+
* The method generates a filename for new files. By default it returns new
135+
* filename based on path and current time.
136+
*/
127137
protected newFilename (): string {
128138
return strftime(this.path, new Date())
129139
}
130140

131141
private rotate (): void {
132142
const newFilename = this.newFilename()
133-
const { currentFilename, stream, timer } = this
143+
const { currentFilename, stream, closer } = this
134144

135145
if (newFilename !== currentFilename) {
136-
if (currentFilename && stream && timer) {
137-
timer.remove()
146+
if (currentFilename && stream && closer) {
147+
closer.remove()
138148
stream.end()
139149

140150
const streamErrorHandler = this.streamErrorHandlers.get(currentFilename)
@@ -160,17 +170,17 @@ export class FileTimestampStream extends Writable {
160170
const newTimer = interval(FileTimestampStream.CLOSE_UNUSED_FILE_AFTER, () => {
161171
if (newFilename !== this.newFilename()) {
162172
newTimer.remove()
163-
this.timers.delete(newFilename)
173+
this.closers.delete(newFilename)
164174

165175
newStream.end()
166176
}
167177
})
168-
this.timer = timer
169-
this.timers.set(newFilename, newTimer)
178+
this.closer = closer
179+
this.closers.set(newFilename, newTimer)
170180

171181
const newStreamCancelFinisher = finished(newStream, () => {
172182
newTimer.remove()
173-
this.timers.delete(newFilename)
183+
this.closers.delete(newFilename)
174184

175185
// tslint:disable-next-line:strict-type-predicates
176186
if (typeof newStream.destroy === 'function') {

0 commit comments

Comments
 (0)