Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-promise

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-promise - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

examples/mongoose/app.js

46

lib/express_promise.js

@@ -1,6 +0,15 @@

var resolveAsync = function(object, callback, maxDeep, currentDeep) {
var isPromise = function(v) {
return typeof v === 'object' && typeof v.then === 'function';
};
var isMongooseQuery = function(v) {
return typeof v === 'object' && typeof v.exec === 'function';
};
var resolveAsync = function(object, callback) {
if (!object || typeof object !== 'object') {
return callback(null, object);
}
if (typeof object.then === 'function') {
if (isPromise(object)) {
return object.then(function(result) {

@@ -13,4 +22,4 @@ callback(null, result);

if (!currentDeep) {
currentDeep = 1;
if (typeof object.toJSON === 'function') {
object = object.toJSON();
}

@@ -20,11 +29,9 @@

Object.keys(object).forEach(function(key) {
if (currentDeep < maxDeep &&
typeof object[key] === 'object') {
if (isPromise(object[key]) || isMongooseQuery(object[key])) {
object[key].key = key;
remains.push(object[key]);
} else if (typeof object[key] === 'object') {
remains.push({
key: key,
});
} else if (object[key] &&
typeof object[key].then === 'function') {
object[key].key = key;
remains.push(object[key]);
}

@@ -48,3 +55,3 @@ });

}
if (typeof item.then === 'function') {
if (isPromise(item)) {
item.then(function(result) {

@@ -55,4 +62,8 @@ handleDone(null, result);

});
} else if (isMongooseQuery(item)) {
item.exec(function(err, result) {
handleDone(err, result);
});
} else {
resolveAsync(object[item.key], handleDone, maxDeep, currentDeep + 1);
resolveAsync(object[item.key], handleDone);
}

@@ -64,4 +75,3 @@ });

var defaultOptions = {
methods: ['json', 'render', 'send'],
maxDeep: 5
methods: ['json', 'render', 'send']
};

@@ -104,3 +114,3 @@

}
}, options.maxDeep);
});
};

@@ -125,3 +135,3 @@ }

originalResRender(view, result);
}, options.maxDeep);
});
return;

@@ -134,3 +144,3 @@ }

originalResRender(view, result, fn);
}, options.maxDeep);
});
};

@@ -164,3 +174,3 @@ }

}
}, options.maxDeep);
});
} else {

@@ -167,0 +177,0 @@ if (status) {

{
"name": "express-promise",
"version": "0.1.4",
"version": "0.1.5",
"scripts": {

@@ -5,0 +5,0 @@ "test": "mocha -R spec"

@@ -81,4 +81,18 @@ # express-promise

## Usage
Just `app.use` it!
app.use(require('express-promise')());
This library supports the following methods: `res.send`, `res.json`, `res.render`.
If you want to let express-promise support nodejs-style callbacks, you can use [dotQ](https://github.com/luin/dotQ) to convert the nodejs-style callbacks to Promises. For example:
require('dotq');
app.use(require('express-promise')());
var fs = require('fs');
app.get('/file', function(req, res) {
res.send(fs.readFile.promise(__dirname + '/package.json', 'utf-8'));
});
## License

@@ -85,0 +99,0 @@ The MIT License (MIT)

@@ -5,11 +5,12 @@ require('./spec_helper');

describe('basic', function() {
it('should limit the max deep', function(done) {
it('should use the toJSON method', function(done) {
var res = {
json: function(body) {
body.a.b.should.equal('hi');
body.a.c.d.toString().should.equal('[object Promise]');
body.a.c.should.not.have.property('d');
body.a.c.f.should.equal('hi');
done();
}
};
expressPromise({methods: ['json'], maxDeep: 2})(null, res);
expressPromise({methods: ['json']})(null, res);

@@ -24,3 +25,8 @@ function async(callback) {

c: {
d: async.promise()
d: async.promise(),
toJSON: function() {
return {
f: 'hi'
};
}
}

@@ -27,0 +33,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