Comparing version 0.11.0 to 0.11.1
@@ -8,2 +8,10 @@ # Nodecaf Changelog | ||
## [v0.11.1] - 2020-11-24 | ||
### Added | ||
- `fork()` to run functions as middlewares from outside of the chain | ||
### Changed | ||
- error responses to include proper automatic `Content-Type` | ||
## [v0.11.0] - 2020-11-20 | ||
@@ -436,1 +444,3 @@ | ||
[v0.10.1]: https://gitlab.com/GCSBOSS/nodecaf/-/tags/v0.10.1 | ||
[v0.11.0]: https://gitlab.com/GCSBOSS/nodecaf/-/tags/v0.11.0 | ||
[v0.11.1]: https://gitlab.com/GCSBOSS/nodecaf/-/tags/v0.11.1 |
@@ -1,1 +0,1 @@ | ||
[{"categories":["Complexity"],"check_name":"method_complexity","content":{"body":"# Cognitive Complexity\nCognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.\n\n### A method's cognitive complexity is based on a few simple rules:\n* Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one\n* Code is considered more complex for each \"break in the linear flow of the code\"\n* Code is considered more complex when \"flow breaking structures are nested\"\n\n### Further reading\n* [Cognitive Complexity docs](https://docs.codeclimate.com/v1.0/docs/cognitive-complexity)\n* [Cognitive Complexity: A new way of measuring understandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf)\n"},"description":"Function `matchRoute` has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.","fingerprint":"f1ec05382472876a629e10fabf52d32b","location":{"path":"lib/api.js","lines":{"begin":32,"end":48}},"other_locations":[],"remediation_points":350000,"severity":"minor","type":"issue","engine_name":"structure"},{"categories":["Complexity"],"check_name":"method_complexity","content":{"body":"# Cognitive Complexity\nCognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.\n\n### A method's cognitive complexity is based on a few simple rules:\n* Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one\n* Code is considered more complex for each \"break in the linear flow of the code\"\n* Code is considered more complex when \"flow breaking structures are nested\"\n\n### Further reading\n* [Cognitive Complexity docs](https://docs.codeclimate.com/v1.0/docs/cognitive-complexity)\n* [Cognitive Complexity: A new way of measuring understandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf)\n"},"description":"Function `parseSignedCookies` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.","fingerprint":"1a232bcf8abb180d7d962d700b545e32","location":{"path":"lib/api.js","lines":{"begin":89,"end":99}},"other_locations":[],"remediation_points":250000,"severity":"minor","type":"issue","engine_name":"structure"},{"categories":["Complexity"],"check_name":"method_complexity","content":{"body":"# Cognitive Complexity\nCognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.\n\n### A method's cognitive complexity is based on a few simple rules:\n* Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one\n* Code is considered more complex for each \"break in the linear flow of the code\"\n* Code is considered more complex when \"flow breaking structures are nested\"\n\n### Further reading\n* [Cognitive Complexity docs](https://docs.codeclimate.com/v1.0/docs/cognitive-complexity)\n* [Cognitive Complexity: A new way of measuring understandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf)\n"},"description":"Function `parseBody` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.","fingerprint":"0a4d135aa1342b4a3c74ca38b609ff04","location":{"path":"lib/parser.js","lines":{"begin":26,"end":52}},"other_locations":[],"remediation_points":250000,"severity":"minor","type":"issue","engine_name":"structure"},{"categories":["Complexity"],"check_name":"return_statements","content":{"body":""},"description":"Avoid too many `return` statements within this function.","location":{"path":"lib/parser.js","lines":{"begin":47,"end":47}},"other_locations":[],"remediation_points":300000,"severity":"major","type":"issue","engine_name":"structure","fingerprint":"3e5e98cdcba8196f5b12dfdad720bf15"}] | ||
[{"categories":["Complexity"],"check_name":"method_complexity","content":{"body":"# Cognitive Complexity\nCognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.\n\n### A method's cognitive complexity is based on a few simple rules:\n* Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one\n* Code is considered more complex for each \"break in the linear flow of the code\"\n* Code is considered more complex when \"flow breaking structures are nested\"\n\n### Further reading\n* [Cognitive Complexity docs](https://docs.codeclimate.com/v1.0/docs/cognitive-complexity)\n* [Cognitive Complexity: A new way of measuring understandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf)\n"},"description":"Function `matchRoute` has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.","fingerprint":"f1ec05382472876a629e10fabf52d32b","location":{"path":"lib/api.js","lines":{"begin":32,"end":48}},"other_locations":[],"remediation_points":350000,"severity":"minor","type":"issue","engine_name":"structure"},{"categories":["Complexity"],"check_name":"method_complexity","content":{"body":"# Cognitive Complexity\nCognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.\n\n### A method's cognitive complexity is based on a few simple rules:\n* Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one\n* Code is considered more complex for each \"break in the linear flow of the code\"\n* Code is considered more complex when \"flow breaking structures are nested\"\n\n### Further reading\n* [Cognitive Complexity docs](https://docs.codeclimate.com/v1.0/docs/cognitive-complexity)\n* [Cognitive Complexity: A new way of measuring understandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf)\n"},"description":"Function `parseSignedCookies` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.","fingerprint":"1a232bcf8abb180d7d962d700b545e32","location":{"path":"lib/api.js","lines":{"begin":89,"end":99}},"other_locations":[],"remediation_points":250000,"severity":"minor","type":"issue","engine_name":"structure"},{"categories":["Complexity"],"check_name":"method_complexity","content":{"body":"# Cognitive Complexity\nCognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.\n\n### A method's cognitive complexity is based on a few simple rules:\n* Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one\n* Code is considered more complex for each \"break in the linear flow of the code\"\n* Code is considered more complex when \"flow breaking structures are nested\"\n\n### Further reading\n* [Cognitive Complexity docs](https://docs.codeclimate.com/v1.0/docs/cognitive-complexity)\n* [Cognitive Complexity: A new way of measuring understandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf)\n"},"description":"Function `parseBody` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.","fingerprint":"0a4d135aa1342b4a3c74ca38b609ff04","location":{"path":"lib/parser.js","lines":{"begin":26,"end":52}},"other_locations":[],"remediation_points":250000,"severity":"minor","type":"issue","engine_name":"structure"},{"categories":["Complexity"],"check_name":"return_statements","content":{"body":""},"description":"Avoid too many `return` statements within this function.","location":{"path":"lib/parser.js","lines":{"begin":47,"end":47}},"other_locations":[],"remediation_points":300000,"severity":"major","type":"issue","engine_name":"structure","fingerprint":"3e5e98cdcba8196f5b12dfdad720bf15"},{"categories":["Complexity"],"check_name":"return_statements","content":{"body":""},"description":"Avoid too many `return` statements within this function.","location":{"path":"lib/error.js","lines":{"begin":20,"end":20}},"other_locations":[],"remediation_points":300000,"severity":"major","type":"issue","engine_name":"structure","fingerprint":"4817974afd7e0907cec853ce7a62130c"}] |
@@ -101,2 +101,10 @@ const assert = require('assert'); | ||
function fork(input, func){ | ||
// this => app | ||
return new Promise((resolve, reject) => { | ||
func = normalizeHandler(func.bind(this)); | ||
func({ ...input, next: resolve }).catch(reject); | ||
}); | ||
} | ||
module.exports = class API { | ||
@@ -166,2 +174,4 @@ | ||
input.fork = fork.bind(app, input); | ||
Object.assign(input.res, resMethods); | ||
@@ -168,0 +178,0 @@ |
class HTTPError extends Error { | ||
constructor(status, message) { | ||
constructor(status, message, type) { | ||
super(message); | ||
this.status = status; | ||
this.name = 'HTTPError'; | ||
this.type = type; | ||
} | ||
@@ -14,6 +15,8 @@ } | ||
if(thing instanceof Error) | ||
return new HTTPError(500, thing.message); | ||
return new HTTPError(500, thing.message, 'text'); | ||
if(thing instanceof Buffer) | ||
return new HTTPError(500, thing, 'binary'); | ||
if(typeof thing == 'object') | ||
return new HTTPError(500, JSON.stringify(thing)); | ||
return new HTTPError(500, String(thing)); | ||
return new HTTPError(500, JSON.stringify(thing), 'json'); | ||
return new HTTPError(500, String(thing), 'text'); | ||
} | ||
@@ -39,3 +42,3 @@ | ||
if(!res.finished) | ||
res.status(err.status).end( | ||
res.status(err.status).type(err.type).end( | ||
/*istanbul ignore next */ | ||
@@ -42,0 +45,0 @@ process.env.NODE_ENV !== 'production' ? err.message : ''); |
@@ -8,3 +8,4 @@ const { sign } = require('cookie-signature'); | ||
'text': 'text/plain', | ||
'json': 'application/json' | ||
'json': 'application/json', | ||
'binary': 'application/octet-stream' | ||
}; | ||
@@ -68,2 +69,3 @@ | ||
// Not necessarily an APP error, but a client Error | ||
error(status, message, ...args){ | ||
@@ -75,15 +77,20 @@ | ||
this.status(status); | ||
let type = 'text'; | ||
if(typeof message == 'string') | ||
message = format(message, ...args); | ||
else if(typeof message != 'undefined' && message !== null){ | ||
else if(message instanceof Buffer) | ||
type = 'binary'; | ||
else if(message && typeof message == 'object'){ | ||
type = 'json'; | ||
message = JSON.stringify(message); | ||
this.type('json'); | ||
} | ||
else | ||
message = String(message); | ||
this.status(status).end(message); | ||
this.type(type).end(message); | ||
// MAYBE we should do stackAborted on all sorts of errors | ||
this.stackAborted = true; | ||
return new HTTPError(status, message); | ||
return new HTTPError(status, message, type); | ||
}, | ||
@@ -90,0 +97,0 @@ |
{ | ||
"name": "nodecaf", | ||
"version": "0.11.0", | ||
"version": "0.11.1", | ||
"description": "Nodecaf is a light framework for developing RESTful Apps in a quick and convenient manner.", | ||
@@ -5,0 +5,0 @@ "main": "lib/main.js", |
@@ -495,2 +495,28 @@ const assert = require('assert'); | ||
it('Should run a given function as if it was regularly in the pipeline', async function(){ | ||
function middle({ res, next }){ | ||
res.write('K'); | ||
next(); | ||
} | ||
let app = new Nodecaf({ | ||
conf: { port: 80 }, | ||
api({ post }){ | ||
post('/foobar', async function before({ res, fork, next }){ | ||
res.type('text'); | ||
res.write('O'); | ||
await fork(middle); | ||
next(); | ||
}, function after({ res }){ | ||
res.end('!'); | ||
}); | ||
} | ||
}); | ||
await app.start(); | ||
let { assert } = await base.post('foobar'); | ||
assert.status.is(200); | ||
assert.body.exactly('OK!'); | ||
await app.stop(); | ||
}); | ||
}); | ||
@@ -633,3 +659,3 @@ | ||
get('/foo', function({ res }){ | ||
assert.throws( () => res.badRequest(true) ); | ||
assert.throws( () => res.badRequest(true, Buffer.from('abc')) ); | ||
assert.throws( () => res.unauthorized(true) ); | ||
@@ -743,4 +769,4 @@ assert.throws( () => res.forbidden(true) ); | ||
}); | ||
post('/unknown/object', () => { | ||
throw 'resterr'; | ||
post('/unknown/object', ({ res }) => { | ||
res.error(Buffer.from('abc')); | ||
}); | ||
@@ -747,0 +773,0 @@ } |
93678
1407