Comparing version 0.5.9 to 0.6.0
'use strict'; | ||
const _ = require('lodash'); | ||
const _ = require('./lib/lodash'); | ||
const http = require('http'); | ||
@@ -17,2 +17,4 @@ const compose = require('bay-compose'); | ||
const PATH_SEPARATOR = '/' | ||
class BayApplication { | ||
@@ -31,2 +33,3 @@ constructor(options) { | ||
this.base = options.base; | ||
this.specifiedControllers = options.controllers | ||
@@ -39,9 +42,36 @@ if (!this.base) { | ||
this.getController = resolver(options.controllers || requireDir(path.join(this.base, 'controllers'), { recurse: true }), '/'); | ||
if (this.getVersion) { | ||
this.getVersionTransformer = resolver(options.versions || requireDir(path.join(this.base, 'versions'), { recurse: true }), '/'); | ||
this.getVersionTransformer = resolver(options.versions || requireDir(path.join(this.base, 'versions'), { recurse: true }), PATH_SEPARATOR); | ||
} | ||
} | ||
getController (controllerName) { | ||
if (!this._controllerResolver) { | ||
this._controllerResolver = resolver(this.specifiedControllers, PATH_SEPARATOR) | ||
} | ||
return this._controllerResolver(controllerName); | ||
} | ||
/** | ||
* Load all controllers from disk | ||
* | ||
* @returns | ||
* @private | ||
* @memberof BayApplication | ||
*/ | ||
_requireControllers () { | ||
const ret = {} | ||
this.router.getControllers().forEach((controllerName) => { | ||
const pathParts = controllerName.split(PATH_SEPARATOR); | ||
try { | ||
const subPath = path.join.apply(path, pathParts); | ||
const requiredModule = require(path.join(this.base, 'controllers', subPath)) | ||
_.set(ret, pathParts, requiredModule) | ||
} catch (e) {} | ||
}) | ||
return ret | ||
} | ||
/** | ||
* Use the given middleware `fn`. | ||
@@ -59,2 +89,8 @@ * | ||
listen() { | ||
if (!this.specifiedControllers) { | ||
// Lazy loading all controllers before processing any | ||
// HTTP requests. That introduces an limitation that all | ||
// routes defined after `#listen()` will be ignored. | ||
this.specifiedControllers = this._requireControllers(); | ||
} | ||
const server = http.createServer(this.callback()); | ||
@@ -61,0 +97,0 @@ return server.listen.apply(server, arguments); |
'use strict'; | ||
const pathToRegexp = require('path-to-regexp'); | ||
const _ = require('lodash'); | ||
const _ = require('../lodash'); | ||
@@ -53,2 +53,6 @@ class Route { | ||
} | ||
getControllers() { | ||
return [this.handler.controller]; | ||
} | ||
} | ||
@@ -55,0 +59,0 @@ |
@@ -6,3 +6,3 @@ 'use strict'; | ||
const inflect = require('i')(); | ||
const _ = require('lodash'); | ||
const _ = require('../lodash'); | ||
@@ -115,2 +115,17 @@ class Router { | ||
} | ||
/** | ||
* 获取路由所依赖的所有 Controller 列表 | ||
* | ||
* @memberof Router | ||
*/ | ||
getControllers () { | ||
const controllers = {} | ||
for (let i = 0; i < this.stack.length; i++) { | ||
this.stack[i].getControllers().forEach((controllerName) => { | ||
controllers[controllerName] = true | ||
}) | ||
} | ||
return Object.keys(controllers); | ||
} | ||
} | ||
@@ -117,0 +132,0 @@ |
{ | ||
"name": "bay", | ||
"version": "0.5.9", | ||
"version": "0.6.0", | ||
"description": "The framework", | ||
@@ -10,3 +10,3 @@ "main": "application.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"test": "mocha", | ||
"bench": "make -C benchmarks" | ||
@@ -27,3 +27,3 @@ }, | ||
"filter-match": "0.0.3", | ||
"fresh": "^0.3.0", | ||
"fresh": "^0.5.2", | ||
"http-assert": "^1.1.1", | ||
@@ -33,3 +33,6 @@ "http-errors": "^1.3.1", | ||
"koa-is-json": "^1.0.0", | ||
"lodash": "^3.10.1", | ||
"lodash.clone": "^4.5.0", | ||
"lodash.defaults": "^4.2.0", | ||
"lodash.pick": "^4.4.0", | ||
"lodash.set": "^4.3.2", | ||
"methods": "^1.1.1", | ||
@@ -46,3 +49,7 @@ "mime-types": "^2.1.8", | ||
"vary": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"mocha": "^5.2.0" | ||
} | ||
} |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
48981
17
1633
2
30
2
3
+ Addedlodash.clone@^4.5.0
+ Addedlodash.defaults@^4.2.0
+ Addedlodash.pick@^4.4.0
+ Addedlodash.set@^4.3.2
+ Addedfresh@0.5.2(transitive)
+ Addedlodash.clone@4.5.0(transitive)
+ Addedlodash.defaults@4.2.0(transitive)
+ Addedlodash.pick@4.4.0(transitive)
+ Addedlodash.set@4.3.2(transitive)
- Removedlodash@^3.10.1
- Removedfresh@0.3.0(transitive)
- Removedlodash@3.10.1(transitive)
Updatedfresh@^0.5.2