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

expressjs-router

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expressjs-router - npm Package Compare versions

Comparing version 1.1.5 to 1.2.0

189

lib/expressRouter.js

@@ -8,2 +8,4 @@ "use strict";

var util = require("util");
var _ = require("lodash");
var Promise = require("bluebird");

@@ -15,2 +17,6 @@

//
// ---- Conditional routing ----
//
var conditionals = {};

@@ -34,3 +40,146 @@

function _checkConditionals(route, req, res)
{
var conds = route.conditions || [];
var shouldRunRoute = true;
conds.forEach(function (conditionalName) {
if (!shouldRunRoute) return;
if (!conditionals[conditionalName]) {
throw new Error("Unexisting conditional '" + conditionalName + "'");
}
shouldRunRoute = conditionals[conditionalName](req, res);
});
return shouldRunRoute;
}
//
// ---- Preloading ----
//
var preloaders = {};
function createPreloader(name, func)
{
if (!name || typeof name !== "string") {
throw new Error("name argument invalid or missing");
}
if (!func || typeof func !== "function") {
throw new Error("func argument invalid or missing");
}
if (preloaders[name]) {
throw new Error("Trying to create a duplicate conditional '" + name + "'");
}
preloaders[name] = Promise.promisify(func);
}
var PRELOAD_PARALLEL = true;
/*
* 0 -> Parallel
* 1 -> Sequential
*/
function setPreloadingMode(mode)
{
if (!_.isNumber(mode)) {
throw new Error("mode should be a number");
}
switch (mode) {
case 0:
PRELOAD_PARALLEL = true;
break;
case 1:
PRELOAD_PARALLEL = false;
break;
default:
throw new Error("invalid value for mode");
}
}
function _getPreloaderFuncs(preloaderNames)
{
return preloaderNames.map(function (preloaderName) {
var func = preloaders[preloaderName];
if (!func) {
throw new Error("Unexisting preloader '" + preloaderName + "'");
}
return [preloaderName, func];
});
}
function _preloadParallel(preloaderNames)
{
return Promise.all(_getPreloaderFuncs(preloaderNames).map(function (preloader) {
return Promise.all([preloader[0], preloader[1]()]);
})).reduce(function (preloadedData, result) {
preloadedData[result[0]] = result[1];
return preloadedData;
}, {});
}
function _preloadSequentially(preloaderNames)
{
var funcs = _getPreloaderFuncs(preloaderNames);
var results = {};
var chain = Promise.resolve(results);
funcs.forEach(function (preloader) {
var name = preloader[0];
var func = preloader[1];
if (!chain) {
chain = func();
} else {
chain = chain.then(function () {
return func();
});
}
chain = chain.then(function (value) {
results[name] = value;
return results;
});
});
return chain;
}
function _preloadRoute (route, req, res)
{
var preloaderNames = route.preload || [];
var preload;
if (PRELOAD_PARALLEL) {
preload = _preloadParallel(preloaderNames);
} else {
preload = _preloadSequentially(preloaderNames);
}
return preload.then(function (preloadedData) {
req.preloadedData = preloadedData;
});
}
//
// ---- Core builders ----
//
function validateRequiredParameters (route)

@@ -73,22 +222,2 @@ {

function checkConditionals (route, req, res)
{
var conds = route.conditions || [];
var shouldRunRoute = true;
conds.forEach(function (conditionalName) {
if (!shouldRunRoute) return;
if (!conditionals[conditionalName]) {
throw new Error("Unexisting conditional '" + conditionalName + "'");
}
shouldRunRoute = conditionals[conditionalName](req, res);
});
return shouldRunRoute;
}
function buildHandler (route)

@@ -100,3 +229,3 @@ {

var shouldRun = checkConditionals(route, req, res);
var shouldRun = _checkConditionals(route, req, res);
if (!shouldRun) {

@@ -111,3 +240,3 @@ return next();

if (errors) {
return res.json(400, { errors: errors });
return next({ type: "BadRequest", errors: errors });
}

@@ -119,4 +248,8 @@

// Else execute the route code
route.respond(req, res, next);
// Preload and run
_preloadRoute(route, req, res).then(function () {
route.respond(req, res, next);
}).catch(function (error) {
next({ type: "PreloadError", error: error });
});
};

@@ -201,7 +334,11 @@ }

createConditional : createConditional,
createPreloader : createPreloader,
setPreloadingMode : setPreloadingMode,
enableDebug : enableDebug,
_buildRoute : _buildRoute,
_checkConditionals : checkConditionals,
_checkConditionals : _checkConditionals,
_conditionals : conditionals,
_validateRoute : _validateRoute
_validateRoute : _validateRoute,
_preloaders : preloaders,
_preloadRoute : _preloadRoute
};

6

package.json
{
"name": "expressjs-router",
"version": "1.1.5",
"version": "1.2.0",
"description": "Structured routes for ExpressJS",

@@ -12,5 +12,5 @@ "main": "index.js",

"dependencies": {
"q": "~1.0.0",
"lodash": "~2.4.1",
"express-validator": "~1.0.1"
"express-validator": "~1.0.1",
"bluebird": "~1.2.4"
},

@@ -17,0 +17,0 @@ "devDependencies": {

@@ -80,2 +80,31 @@ "use strict";

});
it("should preload sequentially", function (done) {
expressRouter.setPreloadingMode(1);
expressRouter.createPreloader("test0", function (callback) {
setTimeout(function () {
return callback(null, "data0");
}, 1000);
});
expressRouter.createPreloader("test1", function (callback) {
setTimeout(function () {
return callback(null, "data1");
}, 200);
});
var definition = {
preload: ["test0", "test1"]
};
var req = {};
expressRouter._preloadRoute(definition, req).then(function () {
assert.deepEqual(req.preloadedData, {
test0: "data0",
test1: "data1"
});
done();
}).done();
});
});
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