Skip to content

Commit

Permalink
refactor!(NODE-3427): remove md5 hashing from GridFS API (#2899)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken authored Jul 12, 2021
1 parent cb0db49 commit a488d88
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 541 deletions.
18 changes: 9 additions & 9 deletions src/gridfs-stream/download.ts → src/gridfs/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import type { Sort } from '../sort';
import type { Callback } from '../utils';
import type { Collection } from '../collection';
import type { ReadPreference } from '../read_preference';
import type { GridFSBucketWriteStream, GridFSChunk } from './upload';
import type { GridFSChunk } from './upload';
import type { FindCursor } from '../cursor/find_cursor';
import type { ObjectId } from 'bson';

/** @public */
export interface GridFSBucketReadStreamOptions {
Expand All @@ -29,14 +30,13 @@ export interface GridFSBucketReadStreamOptionsWithRevision extends GridFSBucketR

/** @public */
export interface GridFSFile {
_id: GridFSBucketWriteStream['id'];
length: GridFSBucketWriteStream['length'];
chunkSize: GridFSBucketWriteStream['chunkSizeBytes'];
md5?: string;
filename: GridFSBucketWriteStream['filename'];
contentType?: GridFSBucketWriteStream['options']['contentType'];
aliases?: GridFSBucketWriteStream['options']['aliases'];
metadata?: GridFSBucketWriteStream['options']['metadata'];
_id: ObjectId;
length: number;
chunkSize: number;
filename: string;
contentType?: string;
aliases?: string[];
metadata?: Document;
uploadDate: Date;
}

Expand Down
File renamed without changes.
31 changes: 7 additions & 24 deletions src/gridfs-stream/upload.ts → src/gridfs/upload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as crypto from 'crypto';
import { Writable } from 'stream';
import { MongoError, AnyError, MONGODB_ERROR_CODES, MongoDriverError } from '../error';
import { WriteConcern } from './../write_concern';
Expand Down Expand Up @@ -31,8 +30,6 @@ export interface GridFSBucketWriteStreamOptions extends WriteConcernOptions {
contentType?: string;
/** Array of strings to store in the file document's `aliases` field */
aliases?: string[];
/** If true, disables adding an md5 field to file data */
disableMD5?: boolean;
}

/**
Expand All @@ -52,7 +49,6 @@ export class GridFSBucketWriteStream extends Writable {
chunkSizeBytes: number;
bufToStore: Buffer;
length: number;
md5: false | crypto.Hash;
n: number;
pos: number;
state: {
Expand Down Expand Up @@ -96,7 +92,6 @@ export class GridFSBucketWriteStream extends Writable {
this.chunkSizeBytes = options.chunkSizeBytes || this.bucket.s.options.chunkSizeBytes;
this.bufToStore = Buffer.alloc(this.chunkSizeBytes);
this.length = 0;
this.md5 = !options.disableMD5 && crypto.createHash('md5');
this.n = 0;
this.pos = 0;
this.state = {
Expand Down Expand Up @@ -307,7 +302,6 @@ function checkDone(stream: GridFSBucketWriteStream, callback?: Callback): boolea
stream.id,
stream.length,
stream.chunkSizeBytes,
stream.md5 ? stream.md5.digest('hex') : undefined,
stream.filename,
stream.options.contentType,
stream.options.aliases,
Expand Down Expand Up @@ -396,14 +390,13 @@ function checkIndexes(stream: GridFSBucketWriteStream, callback: Callback): void
}

function createFilesDoc(
_id: GridFSFile['_id'],
length: GridFSFile['length'],
chunkSize: GridFSFile['chunkSize'],
md5: GridFSFile['md5'],
filename: GridFSFile['filename'],
contentType: GridFSFile['contentType'],
aliases: GridFSFile['aliases'],
metadata: GridFSFile['metadata']
_id: ObjectId,
length: number,
chunkSize: number,
filename: string,
contentType?: string,
aliases?: string[],
metadata?: Document
): GridFSFile {
const ret: GridFSFile = {
_id,
Expand All @@ -413,10 +406,6 @@ function createFilesDoc(
filename
};

if (md5) {
ret.md5 = md5;
}

if (contentType) {
ret.contentType = contentType;
}
Expand Down Expand Up @@ -472,9 +461,6 @@ function doWrite(
spaceRemaining -= numToCopy;
let doc: GridFSChunk;
if (spaceRemaining === 0) {
if (stream.md5) {
stream.md5.update(stream.bufToStore);
}
doc = createChunkDoc(stream.id, stream.n, Buffer.from(stream.bufToStore));
++stream.state.outstandingRequests;
++outstandingRequests;
Expand Down Expand Up @@ -550,9 +536,6 @@ function writeRemnant(stream: GridFSBucketWriteStream, callback?: Callback): boo
// to be.
const remnant = Buffer.alloc(stream.pos);
stream.bufToStore.copy(remnant, 0, 0, stream.pos);
if (stream.md5) {
stream.md5.update(remnant);
}
const doc = createChunkDoc(stream.id, stream.n, remnant);

// If the stream was aborted, do not write remnant
Expand Down
12 changes: 4 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MongoClient } from './mongo_client';
import { Db } from './db';
import { Collection } from './collection';
import { Logger } from './logger';
import { GridFSBucket } from './gridfs-stream';
import { GridFSBucket } from './gridfs';
import { CancellationToken } from './mongo_types';

export {
Expand Down Expand Up @@ -195,17 +195,13 @@ export type {
GridFSBucketReadStreamOptionsWithRevision,
GridFSBucketReadStreamPrivate,
GridFSFile
} from './gridfs-stream/download';
export type {
GridFSBucketOptions,
GridFSBucketPrivate,
GridFSBucketEvents
} from './gridfs-stream/index';
} from './gridfs/download';
export type { GridFSBucketOptions, GridFSBucketPrivate, GridFSBucketEvents } from './gridfs/index';
export type {
GridFSBucketWriteStreamOptions,
GridFSBucketWriteStream,
GridFSChunk
} from './gridfs-stream/upload';
} from './gridfs/upload';
export type { LoggerOptions, LoggerFunction } from './logger';
export type {
MongoClientEvents,
Expand Down
Loading

0 comments on commit a488d88

Please sign in to comment.