mixdown-router
Advanced tools
Comparing version 1.0.1 to 1.0.2
249
index.js
@@ -1,2 +0,2 @@ | ||
var _ = require('lodash'); | ||
var _ = require('./lodash.custom.min.js'); | ||
var plRouter = require('pipeline-router'); | ||
@@ -29,155 +29,149 @@ var util = require('util'); | ||
// attach the server side component | ||
_.extend(self, { | ||
self.getHandler = function(key) { | ||
// check object first. | ||
// then check prototype. | ||
return typeof(handlers[key]) === 'function' ? handlers[key] : handlers.constructor.prototype[key]; | ||
}; | ||
getHandler: function(key) { | ||
// check object first. | ||
// then check prototype. | ||
return _.isFunction(handlers[key]) ? handlers[key] : handlers.constructor.prototype[key]; | ||
}, | ||
/** | ||
* Creates a new instance of router. | ||
* | ||
**/ | ||
self.create = function() { | ||
var newRouter = new plRouter(); | ||
/** | ||
* Creates a new instance of router. | ||
* | ||
**/ | ||
create: function() { | ||
var newRouter = new plRouter(); | ||
if (options.timeout) { | ||
newRouter.timeout = options.timeout; | ||
} | ||
var addParam = function (param, key) { | ||
if (options.timeout) { | ||
newRouter.timeout = options.timeout; | ||
} | ||
var addParam = function (param, key) { | ||
if (param && param.regex) { | ||
if (param && param.regex) { | ||
if (param.kind.toLowerCase() === "rest") { | ||
newRouter.param(key, new RegExp(param.regex)); | ||
} | ||
else if (param.kind.toLowerCase() === 'query') { | ||
newRouter.qparam(key, new RegExp(param.regex)); | ||
} | ||
if (param.kind.toLowerCase() === "rest") { | ||
newRouter.param(key, new RegExp(param.regex)); | ||
} | ||
}; | ||
// add routes | ||
_.each(self.routes, function (route, key) { | ||
// add route-level params | ||
if (route.params) { | ||
_.each(route.params, addParam); | ||
else if (param.kind.toLowerCase() === 'query') { | ||
newRouter.qparam(key, new RegExp(param.regex)); | ||
} | ||
} | ||
}; | ||
// check object first. | ||
var handler = self.getHandler(route.handler); | ||
// add routes | ||
_.each(self.routes, function (route, key) { | ||
// if we found the handler on the router, then bind it. | ||
if (_.isFunction(handler)) { | ||
newRouter.use( | ||
route.method, | ||
route.path, | ||
{ timeout: route.timeout }, | ||
handlers.constructor.prototype._baseHandler.bind(app, handler, route) | ||
); | ||
} | ||
// add route-level params | ||
if (route.params) { | ||
_.each(route.params, addParam); | ||
} | ||
}); | ||
// check object first. | ||
var handler = self.getHandler(route.handler); | ||
return newRouter; | ||
}, | ||
// if we found the handler on the router, then bind it. | ||
if (typeof(handler) === 'function') { | ||
newRouter.use( | ||
route.method, | ||
route.path, | ||
{ timeout: route.timeout }, | ||
handlers.constructor.prototype._baseHandler.bind(app, handler, route) | ||
); | ||
} | ||
listen: function(callback) { | ||
}); | ||
/* | ||
The popstate event - A popstate event is dispatched to the window every time the active history entry changes. If the history entry being activated was created | ||
by a call to pushState or affected by a call to replaceState, the popstate event's state property contains a copy of the history entry's state object. | ||
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history | ||
https://developer.mozilla.org/en-US/docs/Web/API/window.onpopstate | ||
*/ | ||
window.onpopstate = function(e) { | ||
// you can access state using history.state | ||
self.navigate(url.format(window.location)); | ||
}; | ||
return newRouter; | ||
}; | ||
self.navigate(url.format(window.location), callback); | ||
}, | ||
self.listen = function(callback) { | ||
// client side url navigation | ||
// 2 overloaded options | ||
// | ||
// 1. function(route, params, callback) | ||
// @param route {String}: named route to generate the url. | ||
// @param params {Object}: hash of params to send to url generation. | ||
// | ||
// 2. function(url, callback) | ||
// @param url {Object|String}: Can be url string or node url object. | ||
navigate: function(route, params, callback) { | ||
var newUrl = null; | ||
/* | ||
The popstate event - A popstate event is dispatched to the window every time the active history entry changes. If the history entry being activated was created | ||
by a call to pushState or affected by a call to replaceState, the popstate event's state property contains a copy of the history entry's state object. | ||
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history | ||
https://developer.mozilla.org/en-US/docs/Web/API/window.onpopstate | ||
*/ | ||
window.onpopstate = function(e) { | ||
// you can access state using history.state | ||
self.navigate(url.format(window.location)); | ||
}; | ||
// handle 2 arg variant function signatures. | ||
if (arguments.length === 2) { | ||
var arg1 = arguments[1]; | ||
self.navigate(url.format(window.location), callback); | ||
}; | ||
if (_.isFunction(arg1)) { | ||
callback = arg1; | ||
params = null; | ||
} | ||
else { | ||
callback = null; | ||
params = arg1; | ||
} | ||
} | ||
// client side url navigation | ||
// 2 overloaded options | ||
// | ||
// 1. function(route, params, callback) | ||
// @param route {String}: named route to generate the url. | ||
// @param params {Object}: hash of params to send to url generation. | ||
// | ||
// 2. function(url, callback) | ||
// @param url {Object|String}: Can be url string or node url object. | ||
self.navigate = function(route, params, callback) { | ||
var newUrl = null; | ||
// if the route is in the route table, then generate the url. if not, then this is a literal url. | ||
if (self.routes[route]) { | ||
newUrl = app.plugins.router.format(route, params); | ||
} | ||
else { | ||
newUrl = route; | ||
} | ||
// handle 2 arg variant function signatures. | ||
if (arguments.length === 2) { | ||
var arg1 = arguments[1]; | ||
if (!history.pushState) { | ||
window.location.href = newUrl; | ||
return; | ||
} | ||
if (typeof(arg1) === 'function') { | ||
callback = arg1; | ||
params = null; | ||
} | ||
else { | ||
callback = null; | ||
params = arg1; | ||
} | ||
} | ||
// keep a single instance around in a browser. | ||
if (!_clientRouter) { | ||
_clientRouter = self.create(); | ||
} | ||
// if the route is in the route table, then generate the url. if not, then this is a literal url. | ||
if (self.routes[route]) { | ||
newUrl = app.plugins.router.format(route, params); | ||
} | ||
else { | ||
newUrl = route; | ||
} | ||
var req = new MockRequest({ url: newUrl }); | ||
var res = new MockResponse(); | ||
if (!history.pushState) { | ||
window.location.href = newUrl; | ||
return; | ||
} | ||
// if the route was matched, then change the url. This will change the url in the address bar before the handler runs. | ||
// This is good for devs for the situation where there is a problem with the controller handler which will cause the pipeline to stop. | ||
_clientRouter.once('match', function(routerData) { | ||
var httpContext = routerData.httpContext; | ||
if (httpContext.url.href !== window.location.href) { | ||
history.pushState({}, httpContext.url.pathname, httpContext.url.href); | ||
} | ||
}); | ||
// keep a single instance around in a browser. | ||
if (!_clientRouter) { | ||
_clientRouter = self.create(); | ||
} | ||
// fire callback once the handler has executed. Note: javascript is async. The handler might not be done when this callback is fired... but you already knew that! | ||
_clientRouter.once('end', function(err, results) { | ||
var req = new MockRequest({ url: newUrl }); | ||
var res = new MockResponse(); | ||
_.isFunction(callback) ? callback(err, { | ||
matched: results[0].matched, | ||
res: res, | ||
req: req | ||
}) : null; | ||
// if the route was matched, then change the url. This will change the url in the address bar before the handler runs. | ||
// This is good for devs for the situation where there is a problem with the controller handler which will cause the pipeline to stop. | ||
_clientRouter.once('match', function(routerData) { | ||
var httpContext = routerData.httpContext; | ||
if (httpContext.url.href !== window.location.href) { | ||
history.pushState({}, httpContext.url.pathname, httpContext.url.href); | ||
} | ||
}); | ||
}); | ||
// fire callback once the handler has executed. Note: javascript is async. The handler might not be done when this callback is fired... but you already knew that! | ||
_clientRouter.once('end', function(err, results) { | ||
typeof(callback) === 'function' ? callback(err, { | ||
matched: results[0].matched, | ||
res: res, | ||
req: req | ||
}) : null; | ||
// fire async so that the req is pass to calling context. | ||
// setTimeout(_clientRouter.dispatch.bind(_clientRouter, req, res), 5); | ||
_clientRouter.dispatch(req, res); | ||
}); | ||
return req; | ||
} | ||
}); | ||
// fire async so that the req is pass to calling context. | ||
// setTimeout(_clientRouter.dispatch.bind(_clientRouter, req, res), 5); | ||
_clientRouter.dispatch(req, res); | ||
return req; | ||
}; | ||
}; | ||
}; | ||
@@ -190,9 +184,8 @@ | ||
var context = _.clone(httpContext); | ||
context.app = this; | ||
context.route = route; | ||
httpContext.app = this; | ||
httpContext.route = route; | ||
handler.call(this, context); | ||
handler.call(this, httpContext); | ||
}; | ||
module.exports = Router; |
@@ -1,2 +0,2 @@ | ||
var _ = require('lodash'); | ||
var _ = require('../lodash.custom.min.js'); | ||
var url = require('url'); | ||
@@ -90,3 +90,4 @@ var querystring = require('querystring'); | ||
// drop first element in array, since a leading slash creates an empty segment | ||
var urlSegments = _.rest(routeObject.path.split('/')); | ||
var urlSegments = routeObject.path.split('/'); | ||
urlSegments = urlSegments.length > 0 ? urlSegments.slice(1) : urlSegments; | ||
@@ -93,0 +94,0 @@ // replace named params with corresponding values and generate uri |
{ | ||
"name": "mixdown-router", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Router for mixdown.js", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape tests/tap/*" | ||
"test": "tape tests/tap/*", | ||
"postinstall": "lodash include=each,cloneDeep,isEmpty exports=node", | ||
"preinstall": "npm install lodash-cli -g" | ||
}, | ||
@@ -21,3 +23,2 @@ "repository": { | ||
"dependencies": { | ||
"lodash": "~2.2.0", | ||
"pipeline-router": "~1.1.1", | ||
@@ -27,2 +28,3 @@ "hammock": "~0.1.1" | ||
"devDependencies": { | ||
"lodash": "~2.2.0", | ||
"tape": "*", | ||
@@ -29,0 +31,0 @@ "mixdown-server": "0.3.x" |
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 2 instances in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
85521
2
15
1932
3
2
1
- Removedlodash@~2.2.0
- Removedlodash@2.2.1(transitive)