Skip to content

Commit 4ff1759

Browse files
committed
feat: remove callbacks from gridfs
1 parent b2594ea commit 4ff1759

File tree

3 files changed

+39
-70
lines changed

3 files changed

+39
-70
lines changed

src/gridfs/download.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -182,30 +182,22 @@ export class GridFSBucketReadStream extends Readable implements NodeJS.ReadableS
182182
* Marks this stream as aborted (will never push another `data` event)
183183
* and kills the underlying cursor. Will emit the 'end' event, and then
184184
* the 'close' event once the cursor is successfully killed.
185-
*
186-
* @param callback - called when the cursor is successfully closed or an error occurred.
187185
*/
188-
abort(callback?: Callback<void>): void {
186+
async abort(): Promise<void> {
189187
this.push(null);
190188
this.destroyed = true;
191189
if (this.s.cursor) {
192-
this.s.cursor.close().then(
193-
() => {
194-
this.emit(GridFSBucketReadStream.CLOSE);
195-
callback?.();
196-
},
197-
error => {
198-
this.emit(GridFSBucketReadStream.CLOSE);
199-
callback?.(error);
200-
}
201-
);
190+
try {
191+
await this.s.cursor.close();
192+
} finally {
193+
this.emit(GridFSBucketReadStream.CLOSE);
194+
}
202195
} else {
203196
if (!this.s.init) {
204197
// If not initialized, fire close event because we will never
205198
// get a cursor
206199
this.emit(GridFSBucketReadStream.CLOSE);
207200
}
208-
callback && callback();
209201
}
210202
}
211203
}

src/gridfs/index.ts

+21-37
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { Logger } from '../logger';
77
import { Filter, TypedEventEmitter } from '../mongo_types';
88
import type { ReadPreference } from '../read_preference';
99
import type { Sort } from '../sort';
10-
import { Callback, maybeCallback } from '../utils';
1110
import { WriteConcern, WriteConcernOptions } from '../write_concern';
1211
import type { FindOptions } from './../operations/find';
1312
import {
@@ -140,22 +139,17 @@ export class GridFSBucket extends TypedEventEmitter<GridFSBucketEvents> {
140139
*
141140
* @param id - The id of the file doc
142141
*/
143-
delete(id: ObjectId): Promise<void>;
144-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
145-
delete(id: ObjectId, callback: Callback<void>): void;
146-
delete(id: ObjectId, callback?: Callback<void>): Promise<void> | void {
147-
return maybeCallback(async () => {
148-
const { deletedCount } = await this.s._filesCollection.deleteOne({ _id: id });
149-
150-
// Delete orphaned chunks before returning FileNotFound
151-
await this.s._chunksCollection.deleteMany({ files_id: id });
152-
153-
if (deletedCount === 0) {
154-
// TODO(NODE-3483): Replace with more appropriate error
155-
// Consider creating new error MongoGridFSFileNotFoundError
156-
throw new MongoRuntimeError(`File not found for id ${id}`);
157-
}
158-
}, callback);
142+
async delete(id: ObjectId): Promise<void> {
143+
const { deletedCount } = await this.s._filesCollection.deleteOne({ _id: id });
144+
145+
// Delete orphaned chunks before returning FileNotFound
146+
await this.s._chunksCollection.deleteMany({ files_id: id });
147+
148+
if (deletedCount === 0) {
149+
// TODO(NODE-3483): Replace with more appropriate error
150+
// Consider creating new error MongoGridFSFileNotFoundError
151+
throw new MongoRuntimeError(`File not found for id ${id}`);
152+
}
159153
}
160154

161155
/** Convenience wrapper around find on the files collection */
@@ -201,29 +195,19 @@ export class GridFSBucket extends TypedEventEmitter<GridFSBucketEvents> {
201195
* @param id - the id of the file to rename
202196
* @param filename - new name for the file
203197
*/
204-
rename(id: ObjectId, filename: string): Promise<void>;
205-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
206-
rename(id: ObjectId, filename: string, callback: Callback<void>): void;
207-
rename(id: ObjectId, filename: string, callback?: Callback<void>): Promise<void> | void {
208-
return maybeCallback(async () => {
209-
const filter = { _id: id };
210-
const update = { $set: { filename } };
211-
const { matchedCount } = await this.s._filesCollection.updateOne(filter, update);
212-
if (matchedCount === 0) {
213-
throw new MongoRuntimeError(`File with id ${id} not found`);
214-
}
215-
}, callback);
198+
async rename(id: ObjectId, filename: string): Promise<void> {
199+
const filter = { _id: id };
200+
const update = { $set: { filename } };
201+
const { matchedCount } = await this.s._filesCollection.updateOne(filter, update);
202+
if (matchedCount === 0) {
203+
throw new MongoRuntimeError(`File with id ${id} not found`);
204+
}
216205
}
217206

218207
/** Removes this bucket's files collection, followed by its chunks collection. */
219-
drop(): Promise<void>;
220-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
221-
drop(callback: Callback<void>): void;
222-
drop(callback?: Callback<void>): Promise<void> | void {
223-
return maybeCallback(async () => {
224-
await this.s._filesCollection.drop();
225-
await this.s._chunksCollection.drop();
226-
}, callback);
208+
async drop(): Promise<void> {
209+
await this.s._filesCollection.drop();
210+
await this.s._chunksCollection.drop();
227211
}
228212

229213
/** Get the Db scoped logger. */

src/gridfs/upload.ts

+12-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Document } from '../bson';
44
import { ObjectId } from '../bson';
55
import type { Collection } from '../collection';
66
import { AnyError, MongoAPIError, MONGODB_ERROR_CODES, MongoError } from '../error';
7-
import { Callback, maybeCallback } from '../utils';
7+
import type { Callback } from '../utils';
88
import type { WriteConcernOptions } from '../write_concern';
99
import { WriteConcern } from './../write_concern';
1010
import type { GridFSFile } from './download';
@@ -144,27 +144,20 @@ export class GridFSBucketWriteStream extends Writable implements NodeJS.Writable
144144
/**
145145
* Places this write stream into an aborted state (all future writes fail)
146146
* and deletes all chunks that have already been written.
147-
*
148-
* @param callback - called when chunks are successfully removed or error occurred
149147
*/
150-
abort(): Promise<void>;
151-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
152-
abort(callback: Callback<void>): void;
153-
abort(callback?: Callback<void>): Promise<void> | void {
154-
return maybeCallback(async () => {
155-
if (this.state.streamEnd) {
156-
// TODO(NODE-3485): Replace with MongoGridFSStreamClosed
157-
throw new MongoAPIError('Cannot abort a stream that has already completed');
158-
}
148+
async abort(): Promise<void> {
149+
if (this.state.streamEnd) {
150+
// TODO(NODE-3485): Replace with MongoGridFSStreamClosed
151+
throw new MongoAPIError('Cannot abort a stream that has already completed');
152+
}
159153

160-
if (this.state.aborted) {
161-
// TODO(NODE-3485): Replace with MongoGridFSStreamClosed
162-
throw new MongoAPIError('Cannot call abort() on a stream twice');
163-
}
154+
if (this.state.aborted) {
155+
// TODO(NODE-3485): Replace with MongoGridFSStreamClosed
156+
throw new MongoAPIError('Cannot call abort() on a stream twice');
157+
}
164158

165-
this.state.aborted = true;
166-
await this.chunks.deleteMany({ files_id: this.id });
167-
}, callback);
159+
this.state.aborted = true;
160+
await this.chunks.deleteMany({ files_id: this.id });
168161
}
169162

170163
/**

0 commit comments

Comments
 (0)