Comparing version 3.3.2 to 3.3.3
@@ -0,1 +1,7 @@ | ||
### 3.3.3 | ||
* Add basic internal type checks - **[@jkrems](https://github.com/jkrems)** [#6](https://github.com/groupon/quinn/pull/6) | ||
- [`538e3cd`](https://github.com/groupon/quinn/commit/538e3cd6e9f3e03347a0dc9e10944ac02ac854da) **style:** Add basic internal type checks | ||
### 3.3.2 | ||
@@ -2,0 +8,0 @@ |
@@ -35,9 +35,24 @@ /* | ||
const quinn = require('./quinn'); | ||
const { respond, runApplication } = require('./quinn'); | ||
const runApplication = quinn.runApplication; | ||
const respond = quinn.respond; | ||
/** | ||
* @typedef {import('http').IncomingMessage} IncomingMessage | ||
* @typedef {import('http').ServerResponse} ServerResponse | ||
* @typedef {() => any} QuinnHandler | ||
*/ | ||
/** | ||
* @param {QuinnHandler} handler | ||
*/ | ||
function createApp(handler) { | ||
return function(req, res, next) { | ||
/** | ||
* | ||
* @param {IncomingMessage} req | ||
* @param {ServerResponse} res | ||
* @param {(error?: Error) => void} next | ||
*/ | ||
function expressHandler(req, res, next) { | ||
/** | ||
* @param {Error} err | ||
*/ | ||
function forwardError(err) { | ||
@@ -49,2 +64,5 @@ setImmediate(() => { | ||
/** | ||
* @param {unknown} result | ||
*/ | ||
function callNext(result) { | ||
@@ -58,9 +76,11 @@ if (result === undefined) setImmediate(next); | ||
.then(null, forwardError); | ||
}; | ||
} | ||
return expressHandler; | ||
} | ||
module.exports = createApp; | ||
createApp['default'] = createApp; | ||
createApp.default = createApp; | ||
createApp.createApp = createApp; | ||
createApp.respond = respond; | ||
createApp.runApplication = runApplication; |
@@ -37,5 +37,14 @@ /* | ||
/** | ||
* @typedef {import('http').IncomingMessage} IncomingMessage | ||
* @typedef {import('http').ServerResponse} ServerResponse | ||
* @typedef {() => any} QuinnHandler | ||
*/ | ||
const NOT_FOUND = Buffer.from('Not Found\n', 'utf8'); | ||
const INTERNAL_ERROR = Buffer.from('Internal Server Error\n', 'utf8'); | ||
/** | ||
* @param {ServerResponse} res | ||
*/ | ||
function sendNotFound(res) { | ||
@@ -46,2 +55,6 @@ res.statusCode = 404; | ||
/** | ||
* @param {ServerResponse} res | ||
* @param {Error} err | ||
*/ | ||
function sendFatalError(res, err) { | ||
@@ -57,2 +70,7 @@ try { | ||
/** | ||
* @param {QuinnHandler} handler | ||
* @param {IncomingMessage} req | ||
* @param {ServerResponse} res | ||
*/ | ||
function runApplication(handler, req, res) { | ||
@@ -70,4 +88,11 @@ return Promise.resolve(req) | ||
/** | ||
* @param {QuinnHandler} handler | ||
*/ | ||
function createApp(handler) { | ||
return function(req, res) { | ||
/** | ||
* @param {IncomingMessage} req | ||
* @param {ServerResponse} res | ||
*/ | ||
function requestListener(req, res) { | ||
return runApplication(handler, req, res) | ||
@@ -81,9 +106,10 @@ .then(result => { | ||
}); | ||
}; | ||
} | ||
return requestListener; | ||
} | ||
module.exports = createApp; | ||
createApp['default'] = createApp; | ||
createApp.default = createApp; | ||
createApp.createApp = createApp; | ||
createApp.respond = respond; | ||
createApp.runApplication = runApplication; |
@@ -38,2 +38,13 @@ /* | ||
/** | ||
* @typedef {import('http').IncomingMessage} IncomingMessage | ||
* @typedef {import('http').ServerResponse} ServerResponse | ||
* @typedef {() => any} QuinnHandler | ||
* @typedef {any} QuinnBody | ||
* @typedef {(() => QuinnBody) | ((req: IncomingMessage, res: ServerResponse) => QuinnBody) | null} QuinnBodyFactory | ||
*/ | ||
/** | ||
* @param {any} value | ||
*/ | ||
function isStream(value) { | ||
@@ -43,2 +54,5 @@ return !!value && typeof value.pipe === 'function'; | ||
/** | ||
* @param {unknown} value | ||
*/ | ||
function isLazy(value) { | ||
@@ -48,2 +62,5 @@ return typeof value === 'function'; | ||
/** | ||
* @type {(value: any) => boolean} | ||
*/ | ||
const isData = | ||
@@ -58,2 +75,5 @@ typeof Uint8Array === 'function' | ||
/** | ||
* @param {any} value | ||
*/ | ||
function isBody(value) { | ||
@@ -68,2 +88,6 @@ return value === null || isData(value) || isStream(value) || isLazy(value); | ||
class VirtualResponse extends PassThrough { | ||
/** | ||
* | ||
* @param {{ statusCode?: number, headers?: object, body?: any }} options | ||
*/ | ||
constructor({ statusCode = 200, headers = {}, body }) { | ||
@@ -73,6 +97,9 @@ super(); | ||
this.statusCode = statusCode; | ||
/** @type {QuinnBodyFactory} */ | ||
this.bodyFactory = null; | ||
/** @type {Error | null} */ | ||
this.cachedError = null; | ||
httpify(this, headers); | ||
if (isBody(body)) { | ||
@@ -85,2 +112,5 @@ this.body(body); | ||
/** | ||
* @param {Error} e | ||
*/ | ||
error(e) { | ||
@@ -92,2 +122,5 @@ // throw error! but maybe make it possible for this to be delayed until | ||
/** | ||
* @param {number} code | ||
*/ | ||
status(code) { | ||
@@ -98,3 +131,8 @@ this.statusCode = code; | ||
/** | ||
* @param {string} name | ||
* @param {string | string[]} value | ||
*/ | ||
header(name, value) { | ||
// @ts-ignore Typescript doesn't understand `httpify(this)` | ||
this.setHeader(name, value); | ||
@@ -104,2 +142,5 @@ return this; | ||
/** | ||
* @param {QuinnBody} body | ||
*/ | ||
body(body) { | ||
@@ -116,8 +157,9 @@ if (typeof body === 'function') { | ||
if (body instanceof Buffer) { | ||
// @ts-ignore | ||
this.body = body; | ||
this.header('Content-Length', body.length); | ||
this.header('Content-Length', `${body.length}`); | ||
this.end(body); | ||
} else if (isStream(body)) { | ||
if (typeof body.on === 'function') { | ||
body.on('error', e => { | ||
body.on('error', (/** @type {Error} */ e) => { | ||
this.error(e); | ||
@@ -133,2 +175,6 @@ }); | ||
/** | ||
* @param {IncomingMessage} req | ||
* @param {ServerResponse} res | ||
*/ | ||
forwardTo(req, res) { | ||
@@ -157,6 +203,15 @@ return new Promise((resolve, reject) => { | ||
/** | ||
* @template {NodeJS.WritableStream} T | ||
* @param {T} res | ||
* @param {{ end?: boolean }} [options] | ||
* @returns {T} | ||
*/ | ||
pipe(res, options) { | ||
// @ts-ignore | ||
res.statusCode = this.statusCode; | ||
// @ts-ignore Typescript doesn't like type narrowing like this | ||
if (typeof res.setHeader === 'function') { | ||
// @ts-ignore Typescript can't understand `httpify(this)` above | ||
const headers = this.headers; | ||
@@ -166,2 +221,3 @@ const headerNames = Object.keys(headers); | ||
const name = headerNames[i]; | ||
// @ts-ignore Typescript doesn't let us cast `res` to ServerResponse | ||
res.setHeader(name, headers[name]); | ||
@@ -182,3 +238,3 @@ } | ||
if (isBody(props)) { | ||
return new VirtualResponse({ body: props }); | ||
return new VirtualResponse({ body: /** @type {QuinnBody} */ (props) }); | ||
} | ||
@@ -189,2 +245,7 @@ | ||
/** | ||
* @param {any} obj | ||
* @param {(key: string, value: any) => any} [visitor] | ||
* @param {string | number} [indent] | ||
*/ | ||
function json(obj, visitor, indent) { | ||
@@ -198,4 +259,4 @@ return respond({ | ||
module.exports = respond; | ||
module.exports['default'] = respond; | ||
module.exports.default = respond; | ||
module.exports.respond = respond; | ||
module.exports.json = json; |
{ | ||
"name": "quinn", | ||
"version": "3.3.2", | ||
"version": "3.3.3", | ||
"description": "A web framework designed for things to come.", | ||
@@ -16,3 +16,3 @@ "license": "BSD-3-Clause", | ||
"scripts": { | ||
"pretest": "eslint lib test", | ||
"pretest": "eslint . && tsc", | ||
"test": "mocha", | ||
@@ -32,2 +32,5 @@ "posttest": "nlm verify" | ||
"devDependencies": { | ||
"@types/caseless": "^0.12.2", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^12.0.4", | ||
"assertive": "^2.1.0", | ||
@@ -46,2 +49,3 @@ "eslint": "^5.1.0", | ||
"response": "^0.18.0", | ||
"typescript": "^3.5.1", | ||
"wegweiser": "^3.2.1" | ||
@@ -48,0 +52,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
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
21115
394
18