New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

restify-enroute

Package Overview
Dependencies
Maintainers
5
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

restify-enroute - npm Package Compare versions

Comparing version 5.0.0 to 6.0.0

4

lib/index.js

@@ -21,2 +21,3 @@ 'use strict';

* validate.
* @param {boolean} [opts.hotReload] Whether to hot reload the routes
* @param {string} opts.server The restify server to install the routes

@@ -43,3 +44,4 @@ * onto.

server: opts.server,
basePath: ctx.basePath
basePath: ctx.basePath,
hotReload: opts.hotReload
}, function (err) {

@@ -46,0 +48,0 @@ return _cb(err);

@@ -9,3 +9,9 @@ 'use strict';

var verror = require('verror');
var compose = require('restify').helpers.compose;
var bunyan = require('bunyan');
// local globals
var LOG;
/**

@@ -16,2 +22,4 @@ * Install the enroute routes into the restify server.

* @param {object} opts.enroute The parsed enroute configuration.
* @param {object} opts.log an optional logger
* @param {boolean} [opts.hotReload] Whether to hot reload the routes
* @param {object} opts.server The restify server.

@@ -24,6 +32,20 @@ * @param {string} opts.basePath The basepath to resolve source files.

assert.object(opts, 'opts');
assert.optionalObject(opts.log, 'opts.log');
assert.object(opts.enroute, 'opts.enroute');
assert.object(opts.server, 'opts.server');
assert.string(opts.basePath, 'opts.basePath');
assert.optionalBool(opts.hotReload, 'opts.hotReload');
var log;
if (opts.log) {
log = opts.log.child({ component : 'enroute' });
} else {
// only create default logger if one wasn't passed in.
if (!LOG) {
LOG = bunyan.createLogger({ name: 'enroute' });
}
log = LOG;
}
vasync.pipeline({arg: {}, funcs: [

@@ -38,2 +60,7 @@ // Read the routes from disk and parse them as functions

if (opts.hotReload) {
log.info({ basePath: opts.basePath },
'hot reloading of routes is enabled for base dir');
}
// go through each of the route names

@@ -49,9 +76,23 @@ _.forEach(opts.enroute.routes, function (methods, routeName) {

try {
var func = (opts.hotReload) ?
// restify middleware wrapper for hot reload
function enrouteHotReloadProxy(req, res, next) {
return reloadProxy({
basePath: opts.basePath,
method: method,
req: req,
res: res,
routeName: routeName,
sourceFile: sourceFile
}, next);
} : require(sourceFile);
route = {
name: routeName,
method: method,
func: require(sourceFile)
func: func
};
} catch (e) {
return cb1(new verror.VError({
name: 'EnrouteRequireError',
cause: e,

@@ -63,3 +104,3 @@ info: {

}
}, 'route function is invalid'));
}, 'failed to require file, possible syntax error'));
}

@@ -98,2 +139,62 @@ // if HTTP method is 'delete', since restify uses 'del'

/**
* using the restify handler composer, create a middleware on the fly that
* re-requires the file on every load executes it.
* @private
* @function reloadProxy
* @param {Object} opts an options object
* @param {String} opts.basePath The basepath to resolve source files.
* @param {String} opts.method http verb
* @param {Object} opts.log the enroute logger
* @param {Object} opts.req the request object
* @param {Object} opts.res the response object
* @param {String} opts.routeName the name of the restify route
* @param {String} opts.sourceFile the response object
* @param {Function} cb callback fn
* @returns {undefined}
*/
function reloadProxy(opts, cb) {
assert.object(opts, 'opts');
assert.string(opts.basePath, 'opts.basePath');
assert.string(opts.method, 'opts.method');
assert.object(opts.req, 'opts.req');
assert.object(opts.res, 'opts.res');
assert.string(opts.routeName, 'opts.routeName');
assert.string(opts.sourceFile, 'opts.sourceFile');
assert.func(cb, 'cb');
// clear require cache for code loaded from a specific base dir
Object.keys(require.cache).forEach(function (cacheKey) {
if (cacheKey.indexOf(opts.basePath) !== -1) {
delete require.cache[cacheKey];
}
});
var handlers;
try {
handlers = require(opts.sourceFile);
} catch (e) {
var err = new verror.VError({
name: 'EnrouteRequireError',
cause: e,
info: {
file: opts.sourceFile,
route: opts.routeName,
method: opts.method
}
}, 'failed to require file, possible syntax error');
// now that the chain has failed, send back the require error.
return cb(err);
}
// if no require error, execute the handler chain. any errors that occur at
// runtime should be a runtime exception.
var handlerChain = compose(handlers);
return handlerChain(opts.req, opts.res, cb);
}
module.exports = install;

@@ -102,2 +102,5 @@ 'use strict'

type: 'number'
},
hotReload: {
type: 'boolean'
}

@@ -104,0 +107,0 @@ },

{
"name": "restify-enroute",
"version": "5.0.0",
"version": "6.0.0",
"main": "./lib/index.js",

@@ -36,7 +36,8 @@ "description": "Config driven restify route creation",

"eslint": "^3.8.1",
"fs-extra": "^6.0.1",
"istanbul": "^0.4.5",
"jscs": "^3.0.7",
"mkdirp": "^0.5.1",
"mocha": "^3.1.2",
"nsp": "^2.6.2",
"restify": "^7.0.0",
"restify-clients": "^1.4.0",

@@ -48,3 +49,5 @@ "uuid": "^2.0.3"

"assert-plus": "^1.0.0",
"bunyan": "^1.8.12",
"lodash": "^4.16.4",
"restify": "^7.2.0",
"vasync": "^1.6.4",

@@ -51,0 +54,0 @@ "verror": "^1.6.0"

@@ -71,5 +71,8 @@ # restify-enroute

* `[opts.config]` The POJO of the enroute config.
* `[opts.basePath]` Used with `[opts.config]`. The POJO requires a
`basePath` to correctly resolve the route source file(s).
* `[opts.basePath]` Used with `[opts.config]`. The POJO requires a
`basePath` to correctly resolve the route source file(s).
* `[opts.configPath]` The path to the enroute config on disk.
* `[opts.hotReload]` Indicate whether you want the server to reload the
route from disk each time a request is served,
defaults to false
* `cb` The callback. Returns `Error` if there's an error installing the routes.

@@ -80,2 +83,6 @@

`opts.hotReload` allows the restify server to reload the route from disk each
time the request is processed. This is extremely slow and should only be used
in non-production instances.
### Example

@@ -82,0 +89,0 @@ ```javascript

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc