express-promise
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -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 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 10 instances in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 3 instances in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
3162248
320
50384
118
24
21