Comparing version 2.4.0 to 2.5.0
@@ -5,23 +5,31 @@ const Trouter = require('trouter') | ||
const parse = require('regexparam') | ||
const queryparams = require('../utils/queryparams') | ||
module.exports = (config = {}) => { | ||
config.defaultRoute || (config.defaultRoute = (req, res) => { | ||
res.statusCode = 404 | ||
res.end() | ||
}) | ||
config.errorHandler = config.errorHandler || ((err, req, res) => { | ||
res.statusCode = 500 | ||
res.end(err.message) | ||
}) | ||
if (config.defaultRoute === undefined) { | ||
config.defaultRoute = (req, res) => { | ||
res.statusCode = 404 | ||
res.end() | ||
} | ||
} | ||
if (config.errorHandler === undefined) { | ||
config.errorHandler = (err, req, res) => { | ||
res.statusCode = 500 | ||
res.end(err.message) | ||
} | ||
} | ||
if (config.cacheSize === undefined) { | ||
config.cacheSize = 1000 | ||
} | ||
config.id = config.id || (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase() | ||
if (config.id === undefined) { | ||
config.id = (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase() | ||
} | ||
const routers = {} | ||
const cache = new LRU(config.cacheSize) | ||
const hasCache = config.cacheSize > 0 | ||
const cache = hasCache ? new LRU(config.cacheSize) : null | ||
const router = new Trouter() | ||
router.id = config.id | ||
router._use = router.use | ||
const _use = router.use | ||
@@ -33,3 +41,3 @@ router.use = (prefix, ...middlewares) => { | ||
} | ||
router._use(prefix, middlewares) | ||
_use.call(router, prefix, middlewares) | ||
@@ -52,12 +60,8 @@ if (middlewares[0].id) { | ||
} | ||
const qsIndex = req.url.indexOf('?') | ||
if (qsIndex > 0) { | ||
req.path = req.url.slice(0, qsIndex) | ||
} else { | ||
req.path = req.url | ||
} | ||
queryparams(req, req.url) | ||
let match | ||
if (config.cacheSize > 0) { | ||
const reqCacheKey = `${req.method + req.path}` | ||
if (hasCache) { | ||
const reqCacheKey = req.method + req.path | ||
match = cache.get(reqCacheKey) | ||
@@ -72,5 +76,5 @@ if (!match) { | ||
if (match.handlers.length) { | ||
if (match.handlers.length !== 0) { | ||
const middlewares = [...match.handlers] | ||
if (step) { | ||
if (step !== undefined) { | ||
// router is being used as a nested router | ||
@@ -91,3 +95,3 @@ middlewares.push((req, res, next) => { | ||
} | ||
req.params = Object.assign(req.params, match.params) | ||
Object.assign(req.params, match.params) | ||
@@ -94,0 +98,0 @@ return next(middlewares, req, res, 0, routers, config.defaultRoute, config.errorHandler) |
const uWS = require('uWebSockets.js') | ||
const { Writable } = require('stream') | ||
const { toString, toLowerCase } = require('../utils/string') | ||
const { forEach } = require('../utils/object') | ||
const REQUEST_EVENT = 'request' | ||
@@ -81,2 +83,4 @@ | ||
this.method = uRequest.getMethod().toUpperCase() | ||
this.statusCode = null | ||
this.statusMessage = null | ||
this.body = null | ||
@@ -90,2 +94,10 @@ this.headers = {} | ||
getRawHeaders () { | ||
const raw = [] | ||
forEach(this.headers, (header, value) => { | ||
raw.push(header, value) | ||
}) | ||
return raw | ||
} | ||
getRaw () { | ||
@@ -96,2 +108,12 @@ return this.req | ||
function writeAllHeaders () { | ||
this.res.writeHeader('Date', this.server._date) | ||
forEach(this.__headers, ([name, value]) => { | ||
this.res.writeHeader(name, value) | ||
}) | ||
this.headersSent = true | ||
} | ||
class HttpResponse extends Writable { | ||
@@ -107,3 +129,3 @@ constructor (uResponse, uServer) { | ||
this.headers = {} | ||
this.__headers = {} | ||
this.headersSent = false | ||
@@ -113,3 +135,3 @@ | ||
this.__isWritable = true | ||
this.__writeAllHeaders() | ||
writeAllHeaders.call(this) | ||
}) | ||
@@ -119,19 +141,23 @@ } | ||
setHeader (name, value) { | ||
this.headers[name] = String(value) | ||
this.__headers[toLowerCase(name)] = [name, toString(value)] | ||
} | ||
getHeaderNames () { | ||
return Object.keys(this.headers) | ||
return Object.keys(this.__headers) | ||
} | ||
getHeaders () { | ||
return Object.freeze(this.headers) | ||
const headers = {} | ||
forEach(this.__headers, ([, value], name) => { | ||
headers[name] = value | ||
}) | ||
return headers | ||
} | ||
getHeader (name) { | ||
return this.headers[name] | ||
return this.__headers[toLowerCase(name)] | ||
} | ||
removeHeader (name) { | ||
delete this.headers[name] | ||
delete this.__headers[toLowerCase(name)] | ||
} | ||
@@ -143,13 +169,16 @@ | ||
__writeAllHeaders () { | ||
this.res.writeHeader('Date', this.server._date) | ||
const headerKeys = Object.keys(this.headers) | ||
const length = headerKeys.length | ||
for (let index = 0; index < length; index++) { | ||
const key = headerKeys[index] | ||
this.res.writeHeader(key, this.headers[key]) | ||
writeHead (statusCode) { | ||
this.statusCode = statusCode | ||
let headers | ||
if (arguments.length === 2) { | ||
headers = arguments[1] | ||
} else if (arguments.length === 3) { | ||
this.statusMessage = arguments[1] | ||
headers = arguments[2] | ||
} else { | ||
headers = {} | ||
} | ||
this.headersSent = true | ||
forEach(headers, (value, name) => { | ||
this.setHeader(name, value) | ||
}) | ||
} | ||
@@ -161,3 +190,3 @@ | ||
if (!this.__isWritable) { | ||
this.__writeAllHeaders() | ||
writeAllHeaders.call(this) | ||
} | ||
@@ -164,0 +193,0 @@ |
{ | ||
"name": "0http", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "Cero friction HTTP request router. The need for speed!", | ||
@@ -32,4 +32,4 @@ "main": "index.js", | ||
"chai": "^4.2.0", | ||
"find-my-way": "^2.2.3", | ||
"mocha": "^6.2.3", | ||
"find-my-way": "^3.0.1", | ||
"mocha": "^7.2.0", | ||
"nyc": "^15.0.1", | ||
@@ -36,0 +36,0 @@ "standard": "^14.3.4", |
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
16707
10
328