@@ -78,35 +78,15 @@ export interface GridFSBucketReadStreamPrivate {
78
78
* Do not instantiate this class directly. Use `openDownloadStream()` instead.
79
79
* @public
80
80
*/
81
- export class GridFSBucketReadStream extends Readable implements NodeJS . ReadableStream {
81
+ export class GridFSBucketReadStream extends Readable {
82
82
/** @internal */
83
83
s : GridFSBucketReadStreamPrivate ;
84
84
85
- /**
86
- * An error occurred
87
- * @event
88
- */
89
- static readonly ERROR = 'error' as const ;
90
85
/**
91
86
* Fires when the stream loaded the file document corresponding to the provided id.
92
87
* @event
93
88
*/
94
89
static readonly FILE = 'file' as const ;
95
- /**
96
- * Emitted when a chunk of data is available to be consumed.
97
- * @event
98
- */
99
- static readonly DATA = 'data' as const ;
100
- /**
101
- * Fired when the stream is exhausted (no more data events).
102
- * @event
103
- */
104
- static readonly END = 'end' as const ;
105
- /**
106
- * Fired when the stream is exhausted and the underlying cursor is killed
107
- * @event
108
- */
109
- static readonly CLOSE = 'close' as const ;
110
90
111
91
/**
112
92
* @param chunks - Handle for chunks collection
@@ -122,7 +102,7 @@ export class GridFSBucketReadStream extends Readable implements NodeJS.ReadableS
122
102
filter : Document ,
123
103
options ?: GridFSBucketReadStreamOptions
124
104
) {
125
- super ( ) ;
105
+ super ( { emitClose : true } ) ;
126
106
this . s = {
127
107
bytesToTrim : 0 ,
128
108
bytesToSkip : 0 ,
@@ -185,20 +165,8 @@ export class GridFSBucketReadStream extends Readable implements NodeJS.ReadableS
185
165
*/
186
166
async abort ( ) : Promise < void > {
187
167
this . push ( null ) ;
188
- this . destroyed = true ;
189
- if ( this . s . cursor ) {
190
- try {
191
- await this . s . cursor . close ( ) ;
192
- } finally {
193
- this . emit ( GridFSBucketReadStream . CLOSE ) ;
194
- }
195
- } else {
196
- if ( ! this . s . init ) {
197
- // If not initialized, fire close event because we will never
198
- // get a cursor
199
- this . emit ( GridFSBucketReadStream . CLOSE ) ;
200
- }
201
- }
168
+ this . destroy ( ) ;
169
+ await this . s . cursor ?. close ( ) ;
202
170
}
203
171
}
204
172
@@ -221,19 +189,15 @@ function doRead(stream: GridFSBucketReadStream): void {
221
189
return ;
222
190
}
223
191
if ( error ) {
224
- stream . emit ( GridFSBucketReadStream . ERROR , error ) ;
192
+ stream . destroy ( error ) ;
225
193
return ;
226
194
}
227
195
if ( ! doc ) {
228
196
stream . push ( null ) ;
229
197
230
198
stream . s . cursor ?. close ( ) . then (
231
- ( ) => {
232
- stream . emit ( GridFSBucketReadStream . CLOSE ) ;
233
- } ,
234
- error => {
235
- stream . emit ( GridFSBucketReadStream . ERROR , error ) ;
236
- }
199
+ ( ) => null ,
200
+ error => stream . destroy ( error )
237
201
) ;
238
202
return ;
239
203
}
@@ -244,17 +208,15 @@ function doRead(stream: GridFSBucketReadStream): void {
244
208
const expectedN = stream . s . expected ++ ;
245
209
const expectedLength = Math . min ( stream . s . file . chunkSize , bytesRemaining ) ;
246
210
if ( doc . n > expectedN ) {
247
- return stream . emit (
248
- GridFSBucketReadStream . ERROR ,
211
+ return stream . destroy (
249
212
new MongoGridFSChunkError (
250
213
`ChunkIsMissing: Got unexpected n: ${ doc . n } , expected: ${ expectedN } `
251
214
)
252
215
) ;
253
216
}
254
217
255
218
if ( doc . n < expectedN ) {
256
- return stream . emit (
257
- GridFSBucketReadStream . ERROR ,
219
+ return stream . destroy (
258
220
new MongoGridFSChunkError ( `ExtraChunk: Got unexpected n: ${ doc . n } , expected: ${ expectedN } ` )
259
221
) ;
260
222
}
@@ -263,16 +225,14 @@ function doRead(stream: GridFSBucketReadStream): void {
263
225
264
226
if ( buf . byteLength !== expectedLength ) {
265
227
if ( bytesRemaining <= 0 ) {
266
- return stream . emit (
267
- GridFSBucketReadStream . ERROR ,
228
+ return stream . destroy (
268
229
new MongoGridFSChunkError (
269
230
`ExtraChunk: Got unexpected n: ${ doc . n } , expected file length ${ stream . s . file . length } bytes but already read ${ stream . s . bytesRead } bytes`
270
231
)
271
232
) ;
272
233
}
273
234
274
- return stream . emit (
275
- GridFSBucketReadStream . ERROR ,
235
+ return stream . destroy (
276
236
new MongoGridFSChunkError (
277
237
`ChunkIsWrongSize: Got unexpected length: ${ buf . byteLength } , expected: ${ expectedLength } `
278
238
)
@@ -332,7 +292,7 @@ function init(stream: GridFSBucketReadStream): void {
332
292
doc
333
293
} : { error : Error ; doc : null } | { error : null ; doc : any } ) => {
334
294
if ( error ) {
335
- return stream . emit ( GridFSBucketReadStream . ERROR , error ) ;
295
+ return stream . destroy ( error ) ;
336
296
}
337
297
338
298
if ( ! doc ) {
@@ -343,7 +303,7 @@ function init(stream: GridFSBucketReadStream): void {
343
303
// TODO(NODE-3483)
344
304
const err = new MongoRuntimeError ( errmsg ) ;
345
305
err . code = 'ENOENT' ; // TODO: NODE-3338 set property as part of constructor
346
- return stream . emit ( GridFSBucketReadStream . ERROR , err ) ;
306
+ return stream . destroy ( err ) ;
347
307
}
348
308
349
309
// If document is empty, kill the stream immediately and don't
@@ -357,14 +317,14 @@ function init(stream: GridFSBucketReadStream): void {
357
317
// If user destroys the stream before we have a cursor, wait
358
318
// until the query is done to say we're 'closed' because we can't
359
319
// cancel a query.
360
- stream . emit ( GridFSBucketReadStream . CLOSE ) ;
320
+ stream . destroy ( ) ;
361
321
return ;
362
322
}
363
323
364
324
try {
365
325
stream . s . bytesToSkip = handleStartOption ( stream , doc , stream . s . options ) ;
366
326
} catch ( error ) {
367
- return stream . emit ( GridFSBucketReadStream . ERROR , error ) ;
327
+ return stream . destroy ( error ) ;
368
328
}
369
329
370
330
const filter : Document = { files_id : doc . _id } ;
@@ -390,7 +350,7 @@ function init(stream: GridFSBucketReadStream): void {
390
350
try {
391
351
stream . s . bytesToTrim = handleEndOption ( stream , doc , stream . s . cursor , stream . s . options ) ;
392
352
} catch ( error ) {
393
- return stream . emit ( GridFSBucketReadStream . ERROR , error ) ;
353
+ return stream . destroy ( error ) ;
394
354
}
395
355
396
356
stream . emit ( GridFSBucketReadStream . FILE , doc ) ;
0 commit comments