Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subrouters middelware dependencies #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ PickerImp = function(filterFunction) {

PickerImp.prototype.middleware = function(callback) {
this.middlewares.push(callback);
for(const subRouter of this.subRouters) {
subRouter.middleware(callback);
}
};

PickerImp.prototype.route = function(path, callback) {
Expand All @@ -23,6 +26,9 @@ PickerImp.prototype.route = function(path, callback) {
PickerImp.prototype.filter = function(callback) {
var subRouter = new PickerImp(callback);
this.subRouters.push(subRouter);
for(const middleware of this.middlewares) {
subRouter.middleware(middleware);
}
return subRouter;
};

Expand All @@ -39,13 +45,12 @@ PickerImp.prototype._dispatch = function(req, res, bypass) {
}
}

processNextMiddleware();
function processNextMiddleware () {
function processNextMiddleware (onDone) {
var middleware = self.middlewares[currentMiddleware++];
if(middleware) {
self._processMiddleware(middleware, req, res, processNextMiddleware);
self._processMiddleware(middleware, req, res, () => { processNextMiddleware(onDone); });
} else {
processNextRoute();
onDone();
}
}

Expand All @@ -54,17 +59,20 @@ PickerImp.prototype._dispatch = function(req, res, bypass) {
if(route) {
var uri = req.url.replace(/\?.*/, '');
var m = uri.match(route);
if(m) {
var params = self._buildParams(route.keys, m);
params.query = urlParse(req.url, true).query;
self._processRoute(route.callback, params, req, res, bypass);
if (m) {
processNextMiddleware(() => {
var params = self._buildParams(route.keys, m);
params.query = urlParse(req.url, true).query;
self._processRoute(route.callback, params, req, res, bypass);
});
} else {
processNextRoute();
}
} else {
processNextSubRouter();
}
}
processNextRoute();

function processNextSubRouter () {
var subRouter = self.subRouters[currentSubRouter++];
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function configurePackage(api) {
api.versionsFrom('[email protected]');
}

api.use(['webapp', 'underscore'], ['server']);
api.use(['webapp', 'underscore', 'ecmascript'], ['server']);
api.addFiles([
'lib/implementation.js',
'lib/instance.js',
Expand Down
34 changes: 34 additions & 0 deletions test/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,40 @@ Tinytest.add('middlewares - with filtered routes', function(test) {
test.equal(res.content, "ok");
});

Tinytest.add('middlewares - with several filtered routes', function(test) {
var path1 = "/" + Random.id() + "/coola";
var path2 = "/" + Random.id() + "/coola";

var routes1 = Picker.filter();
var routes2 = Picker.filter();

const increaseResultBy = (i) => (req, res, next) => {
setTimeout(function() {
req.result = req.result || 0;
req.result += i;
next();
}, 100);
};

routes1.middleware(increaseResultBy(1));
routes2.middleware(increaseResultBy(2));

Picker.middleware(increaseResultBy(10));

routes1.route(path1, function(params, req, res) {
res.end(req.result+'');
});
routes2.route(path2, function(params, req, res) {
res.end(req.result+'');
});

var res = HTTP.get(getPath(path1));
test.equal(res.content, "11");

var res = HTTP.get(getPath(path2));
test.equal(res.content, "12");
});

var urlResolve = Npm.require('url').resolve;
function getPath(path) {
return urlResolve(process.env.ROOT_URL, path);
Expand Down