Skip to content

Commit a488d88

Browse files
authored
refactor!(NODE-3427): remove md5 hashing from GridFS API (#2899)
1 parent cb0db49 commit a488d88

File tree

5 files changed

+347
-541
lines changed

5 files changed

+347
-541
lines changed

src/gridfs-stream/download.ts renamed to src/gridfs/download.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import type { Sort } from '../sort';
66
import type { Callback } from '../utils';
77
import type { Collection } from '../collection';
88
import type { ReadPreference } from '../read_preference';
9-
import type { GridFSBucketWriteStream, GridFSChunk } from './upload';
9+
import type { GridFSChunk } from './upload';
1010
import type { FindCursor } from '../cursor/find_cursor';
11+
import type { ObjectId } from 'bson';
1112

1213
/** @public */
1314
export interface GridFSBucketReadStreamOptions {
@@ -29,14 +30,13 @@ export interface GridFSBucketReadStreamOptionsWithRevision extends GridFSBucketR
2930

3031
/** @public */
3132
export interface GridFSFile {
32-
_id: GridFSBucketWriteStream['id'];
33-
length: GridFSBucketWriteStream['length'];
34-
chunkSize: GridFSBucketWriteStream['chunkSizeBytes'];
35-
md5?: string;
36-
filename: GridFSBucketWriteStream['filename'];
37-
contentType?: GridFSBucketWriteStream['options']['contentType'];
38-
aliases?: GridFSBucketWriteStream['options']['aliases'];
39-
metadata?: GridFSBucketWriteStream['options']['metadata'];
33+
_id: ObjectId;
34+
length: number;
35+
chunkSize: number;
36+
filename: string;
37+
contentType?: string;
38+
aliases?: string[];
39+
metadata?: Document;
4040
uploadDate: Date;
4141
}
4242

File renamed without changes.

src/gridfs-stream/upload.ts renamed to src/gridfs/upload.ts

+7-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as crypto from 'crypto';
21
import { Writable } from 'stream';
32
import { MongoError, AnyError, MONGODB_ERROR_CODES, MongoDriverError } from '../error';
43
import { WriteConcern } from './../write_concern';
@@ -31,8 +30,6 @@ export interface GridFSBucketWriteStreamOptions extends WriteConcernOptions {
3130
contentType?: string;
3231
/** Array of strings to store in the file document's `aliases` field */
3332
aliases?: string[];
34-
/** If true, disables adding an md5 field to file data */
35-
disableMD5?: boolean;
3633
}
3734

3835
/**
@@ -52,7 +49,6 @@ export class GridFSBucketWriteStream extends Writable {
5249
chunkSizeBytes: number;
5350
bufToStore: Buffer;
5451
length: number;
55-
md5: false | crypto.Hash;
5652
n: number;
5753
pos: number;
5854
state: {
@@ -96,7 +92,6 @@ export class GridFSBucketWriteStream extends Writable {
9692
this.chunkSizeBytes = options.chunkSizeBytes || this.bucket.s.options.chunkSizeBytes;
9793
this.bufToStore = Buffer.alloc(this.chunkSizeBytes);
9894
this.length = 0;
99-
this.md5 = !options.disableMD5 && crypto.createHash('md5');
10095
this.n = 0;
10196
this.pos = 0;
10297
this.state = {
@@ -307,7 +302,6 @@ function checkDone(stream: GridFSBucketWriteStream, callback?: Callback): boolea
307302
stream.id,
308303
stream.length,
309304
stream.chunkSizeBytes,
310-
stream.md5 ? stream.md5.digest('hex') : undefined,
311305
stream.filename,
312306
stream.options.contentType,
313307
stream.options.aliases,
@@ -396,14 +390,13 @@ function checkIndexes(stream: GridFSBucketWriteStream, callback: Callback): void
396390
}
397391

398392
function createFilesDoc(
399-
_id: GridFSFile['_id'],
400-
length: GridFSFile['length'],
401-
chunkSize: GridFSFile['chunkSize'],
402-
md5: GridFSFile['md5'],
403-
filename: GridFSFile['filename'],
404-
contentType: GridFSFile['contentType'],
405-
aliases: GridFSFile['aliases'],
406-
metadata: GridFSFile['metadata']
393+
_id: ObjectId,
394+
length: number,
395+
chunkSize: number,
396+
filename: string,
397+
contentType?: string,
398+
aliases?: string[],
399+
metadata?: Document
407400
): GridFSFile {
408401
const ret: GridFSFile = {
409402
_id,
@@ -413,10 +406,6 @@ function createFilesDoc(
413406
filename
414407
};
415408

416-
if (md5) {
417-
ret.md5 = md5;
418-
}
419-
420409
if (contentType) {
421410
ret.contentType = contentType;
422411
}
@@ -472,9 +461,6 @@ function doWrite(
472461
spaceRemaining -= numToCopy;
473462
let doc: GridFSChunk;
474463
if (spaceRemaining === 0) {
475-
if (stream.md5) {
476-
stream.md5.update(stream.bufToStore);
477-
}
478464
doc = createChunkDoc(stream.id, stream.n, Buffer.from(stream.bufToStore));
479465
++stream.state.outstandingRequests;
480466
++outstandingRequests;
@@ -550,9 +536,6 @@ function writeRemnant(stream: GridFSBucketWriteStream, callback?: Callback): boo
550536
// to be.
551537
const remnant = Buffer.alloc(stream.pos);
552538
stream.bufToStore.copy(remnant, 0, 0, stream.pos);
553-
if (stream.md5) {
554-
stream.md5.update(remnant);
555-
}
556539
const doc = createChunkDoc(stream.id, stream.n, remnant);
557540

558541
// If the stream was aborted, do not write remnant

src/index.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { MongoClient } from './mongo_client';
99
import { Db } from './db';
1010
import { Collection } from './collection';
1111
import { Logger } from './logger';
12-
import { GridFSBucket } from './gridfs-stream';
12+
import { GridFSBucket } from './gridfs';
1313
import { CancellationToken } from './mongo_types';
1414

1515
export {
@@ -195,17 +195,13 @@ export type {
195195
GridFSBucketReadStreamOptionsWithRevision,
196196
GridFSBucketReadStreamPrivate,
197197
GridFSFile
198-
} from './gridfs-stream/download';
199-
export type {
200-
GridFSBucketOptions,
201-
GridFSBucketPrivate,
202-
GridFSBucketEvents
203-
} from './gridfs-stream/index';
198+
} from './gridfs/download';
199+
export type { GridFSBucketOptions, GridFSBucketPrivate, GridFSBucketEvents } from './gridfs/index';
204200
export type {
205201
GridFSBucketWriteStreamOptions,
206202
GridFSBucketWriteStream,
207203
GridFSChunk
208-
} from './gridfs-stream/upload';
204+
} from './gridfs/upload';
209205
export type { LoggerOptions, LoggerFunction } from './logger';
210206
export type {
211207
MongoClientEvents,

0 commit comments

Comments
 (0)