@@ -2,15 +2,8 @@ import { Writable } from 'stream';
2
2
import type { Document } from '../bson' ;
3
3
import { ObjectId } from '../bson' ;
4
4
import type { Collection } from '../collection' ;
5
- import {
6
- AnyError ,
7
- MONGODB_ERROR_CODES ,
8
- MongoDriverError ,
9
- MongoError ,
10
- MongoGridFSStreamError
11
- } from '../error' ;
12
- import { PromiseProvider } from '../promise_provider' ;
13
- import type { Callback } from '../utils' ;
5
+ import { AnyError , MONGODB_ERROR_CODES , MongoError , MongoAPIError } from '../error' ;
6
+ import { Callback , maybePromise } from '../utils' ;
14
7
import type { WriteConcernOptions } from '../write_concern' ;
15
8
import { WriteConcern } from './../write_concern' ;
16
9
import type { GridFSFile } from './download' ;
@@ -149,27 +142,19 @@ export class GridFSBucketWriteStream extends Writable {
149
142
abort ( ) : Promise < void > ;
150
143
abort ( callback : Callback < void > ) : void ;
151
144
abort ( callback ?: Callback < void > ) : Promise < void > | void {
152
- const Promise = PromiseProvider . get ( ) ;
153
- let error : MongoGridFSStreamError ;
154
- if ( this . state . streamEnd ) {
155
- // TODO(NODE-3405): Replace with MongoStreamClosedError
156
- error = new MongoDriverError ( 'Cannot abort a stream that has already completed' ) ;
157
- if ( typeof callback === 'function' ) {
158
- return callback ( error ) ;
145
+ return maybePromise ( callback , callback => {
146
+ if ( this . state . streamEnd ) {
147
+ // TODO(NODE-3485): Replace with MongoGridFSStreamClosed
148
+ return callback ( new MongoAPIError ( 'Cannot abort a stream that has already completed' ) ) ;
159
149
}
160
- return Promise . reject ( error ) ;
161
- }
162
- if ( this . state . aborted ) {
163
- // TODO(NODE-3405): Replace with MongoStreamClosedError
164
- error = new MongoDriverError ( 'Cannot call abort() on a stream twice' ) ;
165
- if ( typeof callback === 'function' ) {
166
- return callback ( error ) ;
150
+
151
+ if ( this . state . aborted ) {
152
+ // TODO(NODE-3485): Replace with MongoGridFSStreamClosed
153
+ return callback ( new MongoAPIError ( 'Cannot call abort() on a stream twice' ) ) ;
167
154
}
168
- return Promise . reject ( error ) ;
169
- }
170
- this . state . aborted = true ;
171
- this . chunks . deleteMany ( { files_id : this . id } , error => {
172
- if ( typeof callback === 'function' ) callback ( error ) ;
155
+
156
+ this . state . aborted = true ;
157
+ this . chunks . deleteMany ( { files_id : this . id } , error => callback ( error ) ) ;
173
158
} ) ;
174
159
}
175
160
@@ -565,8 +550,8 @@ function writeRemnant(stream: GridFSBucketWriteStream, callback?: Callback): boo
565
550
function checkAborted ( stream : GridFSBucketWriteStream , callback ?: Callback < void > ) : boolean {
566
551
if ( stream . state . aborted ) {
567
552
if ( typeof callback === 'function' ) {
568
- // TODO(NODE-3405 ): Replace with MongoStreamClosedError
569
- callback ( new MongoDriverError ( 'this stream has been aborted') ) ;
553
+ // TODO(NODE-3485 ): Replace with MongoGridFSStreamClosedError
554
+ callback ( new MongoAPIError ( 'Stream has been aborted') ) ;
570
555
}
571
556
return true ;
572
557
}
0 commit comments