From e195af04db7f1e709516efd2c32a92c7d5d139a0 Mon Sep 17 00:00:00 2001 From: Will Huang <1386290+willhuang85@users.noreply.github.com> Date: Mon, 3 Dec 2018 23:06:36 -0800 Subject: [PATCH] Version 1.0.1 (#36) * Correct ls to return array of file path's instead of the whole document object * Correct read and rm to use fd as a string instead of an object * Ability to use callback function for read instead of only a stream * Fix some readme errors --- README.md | 4 +++- index.js | 51 +++++++++++++++++++++++++++++++++------------------ package.json | 5 +++-- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1c2324b..22dec43 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ GridFS adapter for receiving [upstreams](https://github.com/balderdashy/skipper#what-are-upstreams). Particularly useful for handling streaming multipart file uploads from the [Skipper](https://github.com/balderdashy/skipper) body parser. +Currently only supports Node 6 and up + ======================================== @@ -27,7 +29,7 @@ Also make sure you have skipper [installed as your body parser](http://beta.sail req.file('avatar') .upload({ adapter: require('skipper-gridfs'), - uri: 'mongodb://jimmy@j1mtr0n1xx@mongo.jimmy.com:27017/myBucket' + uri: 'mongodb://username:password@myhost.com:27017/myDatabase' }, function whenDone(err, uploadedFiles) { if (err) return res.negotiate(err); else return res.ok({ diff --git a/index.js b/index.js index edc3df5..444a6ee 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,8 @@ const mongodb = require('mongodb'); const path = require('path'); const mime = require('mime'); const _ = require('lodash'); +const concat = require('concat-stream'); + const client = (uri, mongoOptions, fn) => { @@ -35,7 +37,7 @@ module.exports = function SkipperGridFS(globalOptions) { errorHandler(err, client); } - bucket(client.db(), options.bucketOptions).delete(fd._id, errorHandler); + bucket(client.db(), options.bucketOptions).delete(fd, (err) => errorHandler(err, client)); if (cb) cb(); }); } @@ -46,31 +48,38 @@ module.exports = function SkipperGridFS(globalOptions) { if (cb) cb(err); } - const __transform__ = Transform(); + const __transform__ = Transform({ objectMode: true }); __transform__._transform = (chunk, encoding, callback) => { - return callback(null, chunk); + return callback(null, chunk._id ? chunk._id : null); }; + __transform__.once('done', (client) => { + client.close(); + }); + + client(options.uri, options.mongoOptions, (err, client) => { if (err) { errorHandler(err, client); } - const cursor = bucket(client.db(), options.bucketOptions).find({ 'metadata.dirname': dirpath }) - if (cb) { - cursor.toArray((err, documents) => { - if (err) { - errorHandler(err, client); - } - client.close(); - cb(null, documents); - }); - } else { - cursor.pipe(__transform__); - } + const stream = bucket(client.db(), options.bucketOptions).find({ 'metadata.dirname': dirpath }).transformStream(); + stream.once('error', (err) => { + errorHandler(err, client); + }); + + stream.once('end', () => { + __transform__.emit('done', client); + }); + + stream.pipe(__transform__); }); - if (!cb) { + if (cb) { + __transform__.pipe(concat((data) => { + return cb(null, Array.isArray(data) ? data : [data]); + })); + } else { return __transform__; } } @@ -95,7 +104,7 @@ module.exports = function SkipperGridFS(globalOptions) { __transform__.emit('error', error, client); } - const downloadStream = bucket(client.db(), options.bucketOptions).openDownloadStream(fd._id); + const downloadStream = bucket(client.db(), options.bucketOptions).openDownloadStream(fd); downloadStream.once('end', () => { __transform__.emit('done', client); }); @@ -103,7 +112,13 @@ module.exports = function SkipperGridFS(globalOptions) { downloadStream.pipe(__transform__); }); - return __transform__; + if (cb) { + __transform__.pipe(concat((data) => { + return cb(null, data); + })); + } else { + return __transform__; + } } adapter.receive = (opts) => { diff --git a/package.json b/package.json index 7be45d5..05ca3b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "skipper-gridfs", - "version": "1.0.0", + "version": "1.0.1", "description": "A skipper adapter to allow uploading files to MongoDB's GridFS", "main": "index.js", "scripts": { @@ -27,8 +27,9 @@ "skipper-adapter-tests": "github:willhuang85/skipper-adapter-tests#master" }, "dependencies": { + "concat-stream": "^1.6.2", "lodash": "^4.17.11", "mime": "^2.3.1", "mongodb": "^3.1.8" } -} +} \ No newline at end of file