New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fi-gridfs

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fi-gridfs - npm Package Compare versions

Comparing version 1.0.5 to 1.1.0

.eslintrc.json

335

lib/index.js
'use strict';
var GridFS = require('gridfs-stream');
var Stream = require('stream');
var is = require('is_js');
var fs = require('fs');
const GridFS = require('gridfs-stream');
const Stream = require('stream');
const is = require('fi-is');
const fs = require('fs');
const ERR_INVALID_SELECTOR = 'Invalid query selector! It must be either a valid ObjectID or a filename.';
const ERR_WRONG_SOURCE = 'Source can only be a Readable Stream, Buffer or String!';
const ERR_NOT_FOUND = 'Not Found!';
const ERR_NO_MATCH = 'No match!';
/**
* FiGrid constructor.
*/
function FiGrid() {
this.mongo = null;
this.db = null;
}
class FiGrid {
constructor() {
this.mongo = null;
this.db = null;
}
/**
* FiGrid initialization method.
*
* @param {mongo.Db} db An open mongo.Db instance.
* @param {mongo} mongo The native driver you are using.
* @throws {Error} If any of this arguments is invalid.
*/
FiGrid.prototype.init = function init(db, mongo) {
GridFS.mongo = mongo;
/**
* FiGrid initialization method.
*
* @param {mongo.Db} db An open mongo.Db instance.
* @param {mongo} mongo The native driver you are using.
* @throws {Error} If any of this arguments is invalid.
*/
init(db, mongo) {
GridFS.mongo = mongo;
this.mongo = mongo;
this.db = db;
};
this.mongo = mongo;
this.db = db;
}
/**
* Write a new file to GridFS.
*
* @param {Stream.Readable|Buffer|String} source The input read stream.
* @param {Object} options Any valid gridfs-stream options.
* @param {Function} callback The callback function.
*/
FiGrid.prototype.write = function write() {
/* Last argument should be a callback function */
var callback = arguments[arguments.length - 1];
/* Set empty object as initial options */
var options = {};
/**
* Write a new file to GridFS.
*
* @param {Stream.Readable|Buffer|String} source The input read stream.
* @param {Object} options Any valid gridfs-stream options.
* @param {Function} callback The callback function.
*
* @returns {Promise}
*/
write() {
/* Last argument should be a callback function */
var callback = arguments[arguments.length - 1];
/* Check for callback function */
if (is.not.function(callback)) {
throw new TypeError("Must provide a callback function!");
}
/* Set empty object as initial options */
var options = {};
/* Check if options where passed */
if (is.object(arguments[1])) {
options = arguments[1];
}
/* Check for callback function or behave as promise */
if (is.not.function(callback)) {
callback = function () {};
}
var Grid = new GridFS(this.db);
var source = arguments[0];
/* Check if options where passed */
if (is.object(arguments[1])) {
options = arguments[1];
}
options.mode = options.mode || 'w';
options.filename = options.filename || 'unnamed_file';
var Grid = new GridFS(this.db);
var source = arguments[0];
var ws = Grid.createWriteStream(options);
options.content_type = options.contentType || options.content_type || null;
options.filename = options.filename || 'unnamed_file';
options.mode = options.mode || 'w';
ws.on('close', function (fsfile) {
callback(null, fsfile);
});
return new Promise((resolve, reject) => {
var ws = Grid.createWriteStream(options);
ws.on('error', function (err) {
callback(err);
});
ws.once('close', (fsfile) => {
callback(null, fsfile);
resolve(fsfile);
});
if (source instanceof Stream.Readable) {
source.pipe(ws);
} else if (is.string(source)) {
try {
fs.createReadStream(source).pipe(ws);
} catch (ex) {
callback(ex);
}
} else if (source instanceof Buffer) {
ws.write(source);
ws.end();
} else {
callback(new Error("Source can only be a Readable Stream, Buffer or String!"));
}
};
ws.once('error', (err) => {
callback(err);
reject(err);
});
/**
* Read a file from GridFS.
*
* @param {String} filter Either a valid ObjectID string or a filename.
* @param {Function} callback The callback function.
*/
FiGrid.prototype.read = function read(filter, callback) {
var Grid = new GridFS(this.db);
var query = {};
var _id;
if (source instanceof Stream.Readable) {
source.pipe(ws);
} else if (is.string(source)) {
try {
fs.createReadStream(source).pipe(ws);
} catch (ex) {
callback(ex);
reject(err);
}
} else if (source instanceof Buffer) {
ws.write(source);
ws.end();
} else {
var err = new Error(ERR_WRONG_SOURCE);
/* Check for callback function */
if (is.not.function(callback)) {
throw new TypeError("Must provide a callback function! E.g.: function (err, fsfile, stream) {...}");
callback(err);
reject(err);
}
});
}
/* Check if the filter is an ObjectID, filename or something else */
_id = Grid.tryParseObjectId(filter);
/**
* Read a file from GridFS.
*
* @param {String} filter Either a valid ObjectID string or a filename.
* @param {Function} callback The callback function.
*/
read(selector, callback) {
var Grid = new GridFS(this.db);
var query = {};
var _id;
if (_id) {
query._id = _id;
} else if (is.string(filter)) {
query.filename = filter;
} else {
throw new TypeError("Invalid query filter value! Must be either a valid ObjectID or a filename.");
/* Check for callback function */
if (is.not.function(callback)) {
callback = function () {};
}
return new Promise((resolve, reject) => {
/* Check if the filter is an ObjectID, filename or something else */
_id = Grid.tryParseObjectId(selector);
if (_id) {
query._id = _id;
} else if (is.string(selector)) {
query.filename = selector;
} else {
var err = new TypeError(ERR_INVALID_SELECTOR);
callback(err);
reject(err);
}
/* Fetch the file information if any */
Grid.collection().findOne(query, (err, fsfile) => {
if (err) {
callback(err);
reject(err);
return;
}
if (!fsfile) {
err = new Error(ERR_NO_MATCH);
callback(err);
reject(err);
return;
}
/* Check if the file data exists */
Grid.exist(query, (err, found) => {
if (err) {
callback(err);
reject(err);
return;
}
if (!found) {
err = new Error(ERR_NOT_FOUND);
callback(err);
reject(err);
return;
}
/* Return the file information and the read stream */
fsfile.stream = Grid.createReadStream(query);
callback(null, fsfile, fsfile.stream);
resolve(fsfile);
});
});
});
}
/* Fetch the file information if any */
Grid.collection().findOne(query, function (err, fsfile) {
if (err) {
return callback(err);
}
/**
* Removes a file by passing any options, at least an _id or filename
*
* @param {Object} options
* @param {Function} callback
*/
remove(param, callback) {
var Grid = new GridFS(this.db);
var options = {};
var _id;
if (!fsfile) {
return callback();
/* Check for callback function */
if (is.not.function(callback)) {
callback = function () {};
}
/* Check if the file data exists */
Grid.exist(query, function (err, found) {
if (err) {
return callback(err);
}
return new Promise((resolve, reject) => {
var err;
if (!found) {
return callback();
if (is.not.string(param) && is.not.object(param)) {
err = new Error(ERR_INVALID_SELECTOR);
callback(err);
reject(err);
return;
}
/* Return the file information and the read stream */
callback(null, fsfile, Grid.createReadStream(query));
});
});
};
/* Check if the filter is an ObjectID, filename or something else */
_id = Grid.tryParseObjectId(param);
/**
* Removes a file by passing any options, at least an _id or filename
*
* @param {Object} options
* @param {Function} callback
*/
FiGrid.prototype.remove = function remove(param, callback) {
var Grid = new GridFS(this.db);
var options = {};
var _id;
if (_id) {
options._id = _id;
} else if (is.string(param)) {
options.filename = param;
} else {
err = new Error(ERR_INVALID_SELECTOR);
/* Check for callback function */
if (is.not.function(callback)) {
throw new TypeError("Must provide a callback [Function]!");
}
callback(err);
reject(err);
if (is.not.string(param) && is.not.object(param)) {
throw new Error("The first argument must be a [String] or an [ObjectId]!");
}
return;
}
/* Check if the filter is an ObjectID, filename or something else */
_id = Grid.tryParseObjectId(param);
return Grid.remove(options, (err) => {
if (err) {
callback(err);
reject(err);
if (_id) {
options._id = _id;
} else if (is.string(param)) {
options.filename = param;
} else {
throw new TypeError("Invalid query filter value! Must be either a valid ObjectID or a filename.");
return;
}
callback();
reject();
});
});
}
}
return Grid.remove(options, callback);
};
module.exports = exports = new FiGrid();

6

package.json
{
"name": "fi-gridfs",
"title": "Fi GridFS",
"version": "1.0.5",
"version": "1.1.0",
"description": "Convenience layer for Mongo's GridFS on Node.js applications",

@@ -28,4 +28,4 @@ "main": "index.js",

"dependencies": {
"gridfs-stream": "^1.1.1",
"is_js": "^0.7.4"
"fi-is": "^1.1.1",
"gridfs-stream": "^1.1.1"
},

@@ -32,0 +32,0 @@ "devDependencies": {

@@ -14,3 +14,3 @@ # Fi GridFS

```js
var gridfs = require('fi-seed-component-gridfs');
var gridfs = require('fi-gridfs');
```

@@ -17,0 +17,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc