Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'];
Comment on lines -32 to -39
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just don't do this anywhere else, so consistency

_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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can revert this, I figured now that its our only "gridfs" no need to name it after its implementation detail

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
16 changes: 6 additions & 10 deletions test/functional/gridfs_stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ describe('GridFS Stream', function () {
expect(error).to.not.exist;
expect(docs.length).to.equal(1);

const hash = crypto.createHash('md5');
hash.update(license);
expect(docs[0].md5).to.equal(hash.digest('hex'));
expect(docs[0]).to.not.have.property('md5');

// make sure we created indexes
filesColl.listIndexes().toArray(function (error, indexes) {
Expand Down Expand Up @@ -179,9 +177,7 @@ describe('GridFS Stream', function () {
expect(error).to.not.exist;
expect(docs.length).to.equal(1);

const hash = crypto.createHash('md5');
hash.update(license);
expect(docs[0].md5).to.equal(hash.digest('hex'));
expect(docs[0]).to.not.have.property('md5');

// make sure we created indexes
filesColl.listIndexes().toArray(function (error, indexes) {
Expand Down Expand Up @@ -257,9 +253,7 @@ describe('GridFS Stream', function () {
expect(error).to.not.exist;
expect(docs.length).to.equal(1);

const hash = crypto.createHash('md5');
hash.update(license);
expect(docs[0].md5).to.equal(hash.digest('hex'));
expect(docs[0]).to.not.have.property('md5');
client.close(done);
});
});
Expand Down Expand Up @@ -1232,7 +1226,9 @@ describe('GridFS Stream', function () {
});

function testResultDoc(specDoc, resDoc, result) {
const specKeys = Object.keys(specDoc).sort();
const specKeys = Object.keys(specDoc)
.filter(key => key !== 'md5')
.sort();
Comment on lines +1067 to +1069
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's new spec tests that omit the md5 property for us, we should use those, but we are also behind on our GridFS sync so I defer that to later work.

const resKeys = Object.keys(resDoc).sort();

expect(specKeys.length === resKeys.length);
Expand Down