Comparing version 4.6.2 to 4.7.0
42
index.js
@@ -9,3 +9,2 @@ 'use strict' | ||
const methods = require('./libs/methods') | ||
const requestRouter = require('./libs/request-router') | ||
@@ -22,4 +21,2 @@ const exts = { | ||
const routes = new Set() | ||
const router = requestRouter(options) | ||
const server = options.server || require('http').createServer() | ||
@@ -29,19 +26,11 @@ const prp = undefined === options.prioRequestsProcessing ? true : options.prioRequestsProcessing | ||
server.on('request', (req, res) => { | ||
setImmediate(() => app.handle(req, res)) | ||
setImmediate(() => service.handle(req, res)) | ||
}) | ||
} else { | ||
server.on('request', (req, res) => { | ||
app.handle(req, res) | ||
service.handle(req, res) | ||
}) | ||
} | ||
const app = { | ||
routes () { | ||
return [...routes] | ||
}, | ||
getRouter () { | ||
return router | ||
}, | ||
const service = { | ||
errorHandler: options.errorHandler, | ||
@@ -61,8 +50,2 @@ | ||
use: (...args) => { | ||
router.use.apply(router, args) | ||
return app | ||
}, | ||
handle: (req, res) => { | ||
@@ -72,3 +55,3 @@ // request object population | ||
router.lookup(req, res) | ||
service.getRouter().lookup(req, res) | ||
}, | ||
@@ -90,17 +73,10 @@ | ||
}) | ||
} | ||
methods.forEach((method) => { | ||
app[method] = (...args) => { | ||
routes.add(`${method.toUpperCase()}${args[0]}`) | ||
router[method].apply(router, args) | ||
// apply router capabilities | ||
requestRouter(options, service) | ||
return app | ||
} | ||
}) | ||
service.callback = () => service.handle | ||
app.callback = () => app.handle | ||
app.use(async (req, res, next) => { | ||
service.use(async (req, res, next) => { | ||
try { | ||
@@ -113,3 +89,3 @@ await next() | ||
return app | ||
return service | ||
} |
@@ -7,4 +7,7 @@ 'use strict' | ||
const sequential = require('0http/lib/router/sequential') | ||
const methods = require('./methods') | ||
module.exports = (options) => { | ||
module.exports = (options, service = {}) => { | ||
const routes = new Set() | ||
const router = sequential({ | ||
@@ -18,3 +21,49 @@ errorHandler: options.errorHandler, | ||
return router | ||
// attach router id | ||
service.id = router.id | ||
// attach use method | ||
service.use = (...args) => { | ||
router.use.apply(router, args) | ||
return service | ||
} | ||
// attach routes registration shortcuts | ||
methods.forEach((method) => { | ||
service[method] = (...args) => { | ||
if (Array.isArray(args[0])) { | ||
// support multiple paths registration | ||
const argsExceptPath = args.slice(1) | ||
// for arch path | ||
args[0].forEach(urlPath => { | ||
const singleRouteArgs = [...argsExceptPath] | ||
singleRouteArgs.unshift(urlPath) | ||
routes.add(`${method.toUpperCase()}${singleRouteArgs[0]}`) | ||
router[method].apply(router, singleRouteArgs) | ||
}) | ||
} else { | ||
routes.add(`${method.toUpperCase()}${args[0]}`) | ||
router[method].apply(router, args) | ||
} | ||
return service | ||
} | ||
}) | ||
// attach router | ||
service.getRouter = () => router | ||
// attach routes | ||
service.routes = () => [...routes] | ||
// attach lookup and find methods if not main service | ||
if (!service.handle) { | ||
service.lookup = (...args) => router.lookup.apply(router, args) | ||
service.find = (...args) => router.find.apply(router, args) | ||
} | ||
return service | ||
} |
@@ -43,3 +43,3 @@ 'use strict' | ||
module.exports.send = (options, req, res) => { | ||
const send = (data = 200, code = 200, headers = null, cb = NOOP) => { | ||
const send = (data = res.statusCode, code = res.statusCode, headers = null, cb = NOOP) => { | ||
let contentType | ||
@@ -46,0 +46,0 @@ |
{ | ||
"name": "restana", | ||
"version": "4.6.2", | ||
"version": "4.7.0", | ||
"description": "Super fast and minimalist web framework for building REST micro-services.", | ||
@@ -44,3 +44,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@hapi/hapi": "^19.1.1", | ||
"@hapi/hapi": "^19.2.0", | ||
"benchmark": "^2.1.4", | ||
@@ -50,3 +50,3 @@ "chai": "^4.2.0", | ||
"express-jwt": "^5.3.3", | ||
"fastify": "^2.15.1", | ||
"fastify": "^2.15.2", | ||
"http-cache-middleware": "^1.3.5", | ||
@@ -53,0 +53,0 @@ "koa": "^2.13.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
25166
367