Skip to content

Commit

Permalink
NODE-2355: Allow GridFS write stream to destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jan 15, 2021
1 parent 7e89e47 commit 594ef3b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/gridfs-stream/upload.js
Expand Up @@ -33,6 +33,7 @@ module.exports = GridFSBucketWriteStream;

function GridFSBucketWriteStream(bucket, filename, options) {
options = options || {};
stream.Writable.call(this, options);
this.bucket = bucket;
this.chunks = bucket.s._chunksCollection;
this.filename = filename;
Expand Down
33 changes: 33 additions & 0 deletions test/functional/gridfs_stream.test.js
Expand Up @@ -99,6 +99,39 @@ describe('GridFS Stream', function() {
}
});

it('destroy publishes provided error', {
metadata: { requires: { topology: ['single'] } },
test: function(done) {
var configuration = this.configuration;
var GridFSBucket = configuration.require.GridFSBucket;

var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });

client.connect(function(err, client) {
var db = client.db(configuration.db);
db.dropDatabase(function(error) {
test.equal(error, null);

var bucket = new GridFSBucket(db);
var readStream = fs.createReadStream('./LICENSE.md');
var uploadStream = bucket.openUploadStream('test.dat');
var errorMessage = 'error';

uploadStream.once('error', function(e) {
test.equal(e, errorMessage);
client.close(done);
});

uploadStream.once('finish', function() {
uploadStream.destroy(errorMessage);
});

readStream.pipe(uploadStream);
});
});
}
});

/**
* Correctly stream a file from disk into GridFS using openUploadStream
*
Expand Down

0 comments on commit 594ef3b

Please sign in to comment.