@@ -9,8 +9,11 @@ import strftime from 'ultra-strftime'
9
9
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
10
10
11
11
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'`) */
12
13
flags ?: string | null
14
+ /** a custom [fs](https://nodejs.org/api/fs.html) module (optional) */
13
15
fs ?: typeof fs
16
+ /** a template for new filenames (default: `'out.log'`) */
14
17
path ?: string
15
18
}
16
19
@@ -21,15 +24,17 @@ export class FileTimestampStream extends Writable {
21
24
readonly fs : typeof fs
22
25
readonly path : string
23
26
27
+ /** contains last opened filename */
24
28
protected currentFilename ?: string
29
+ /** contains current [fs.WriteStream](https://nodejs.org/api/fs.html#fs_class_fs_writestream) object */
25
30
protected stream ?: WriteStream
26
31
27
32
private destroyed = false
28
33
private streams : Map < string , WriteStream > = new Map ( )
29
34
private streamCancelFinishers : Map < string , ( ) => void > = new Map ( )
30
35
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 ( )
33
38
34
39
constructor ( options : FileTimestampStreamOptions = { } ) {
35
40
super ( options )
@@ -109,32 +114,37 @@ export class FileTimestampStream extends Writable {
109
114
}
110
115
this . streams . clear ( )
111
116
}
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 ( ) ) {
114
119
timer . remove ( )
115
120
}
116
121
this . streams . clear ( )
117
122
}
118
123
119
124
this . destroyed = true
120
125
this . stream = undefined
121
- this . timer = undefined
126
+ this . closer = undefined
122
127
123
128
callback ( error )
124
129
}
125
130
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
+ */
127
137
protected newFilename ( ) : string {
128
138
return strftime ( this . path , new Date ( ) )
129
139
}
130
140
131
141
private rotate ( ) : void {
132
142
const newFilename = this . newFilename ( )
133
- const { currentFilename, stream, timer } = this
143
+ const { currentFilename, stream, closer } = this
134
144
135
145
if ( newFilename !== currentFilename ) {
136
- if ( currentFilename && stream && timer ) {
137
- timer . remove ( )
146
+ if ( currentFilename && stream && closer ) {
147
+ closer . remove ( )
138
148
stream . end ( )
139
149
140
150
const streamErrorHandler = this . streamErrorHandlers . get ( currentFilename )
@@ -160,17 +170,17 @@ export class FileTimestampStream extends Writable {
160
170
const newTimer = interval ( FileTimestampStream . CLOSE_UNUSED_FILE_AFTER , ( ) => {
161
171
if ( newFilename !== this . newFilename ( ) ) {
162
172
newTimer . remove ( )
163
- this . timers . delete ( newFilename )
173
+ this . closers . delete ( newFilename )
164
174
165
175
newStream . end ( )
166
176
}
167
177
} )
168
- this . timer = timer
169
- this . timers . set ( newFilename , newTimer )
178
+ this . closer = closer
179
+ this . closers . set ( newFilename , newTimer )
170
180
171
181
const newStreamCancelFinisher = finished ( newStream , ( ) => {
172
182
newTimer . remove ( )
173
- this . timers . delete ( newFilename )
183
+ this . closers . delete ( newFilename )
174
184
175
185
// tslint:disable-next-line:strict-type-predicates
176
186
if ( typeof newStream . destroy === 'function' ) {
0 commit comments