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

Use diagnostics_channel in layers #96

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix layerStack timing issue
  • Loading branch information
Qard committed Sep 22, 2020
commit bae6c122951e7ab3c933150457244e46d5537522
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Router.prototype.handle = function handle(req, res, callback) {
var self = this
var slashAdded = false
var paramcalled = {}
var matchedLayer = false

// middleware and routes
var stack = this.stack
Expand Down Expand Up @@ -286,6 +287,14 @@ Router.prototype.handle = function handle(req, res, callback) {
: layer.params
var layerPath = layer.path

if (!matchedLayer) {
matchedLayer = true
req.layerStack = req.layerStack || []
} else {
req.layerStack.pop()
}
req.layerStack.push(layer)

// this should be done for the layer
self.process_params(layer, paramcalled, req, res, function (err) {
if (err) {
Expand Down
10 changes: 0 additions & 10 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ Layer.prototype.handle_error = function handle_error(error, req, res, next) {
return next(error)
}

req.layerStack = req.layerStack || []
req.layerStack.push(this)

if (onHandleError && onHandleError.shouldPublish()) {
onHandleError.publish({
error: error,
Expand All @@ -92,9 +89,7 @@ Layer.prototype.handle_error = function handle_error(error, req, res, next) {

try {
fn(error, req, res, next)
req.layerStack.pop()
} catch (err) {
req.layerStack.pop()
next(err)
}
}
Expand All @@ -116,9 +111,6 @@ Layer.prototype.handle_request = function handle(req, res, next) {
return next()
}

req.layerStack = req.layerStack || []
req.layerStack.push(this)

if (onHandleRequest && onHandleRequest.shouldPublish()) {
onHandleRequest.publish({
request: req,
Expand All @@ -129,9 +121,7 @@ Layer.prototype.handle_request = function handle(req, res, next) {

try {
fn(req, res, next)
req.layerStack.pop()
} catch (err) {
req.layerStack.pop()
next(err)
}
}
Expand Down
9 changes: 9 additions & 0 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Route.prototype._methods = function _methods() {
*/

Route.prototype.dispatch = function dispatch(req, res, done) {
var matchedLayer = false
var idx = 0
var stack = this.stack
if (stack.length === 0) {
Expand Down Expand Up @@ -138,6 +139,14 @@ Route.prototype.dispatch = function dispatch(req, res, done) {
return done(err)
}

if (!matchedLayer) {
matchedLayer = true
req.layerStack = req.layerStack || []
} else {
req.layerStack.pop()
}
req.layerStack.push(layer)

if (err) {
layer.handle_error(err, req, res, next)
} else {
Expand Down
4 changes: 3 additions & 1 deletion test/diagnostics-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ describe('diagnostics_channel', function () {
res.end();
});

outer.use('/hello', inner);
outer.use('/hello', (req, res, next) => {
next();
}, inner);

function end() {
assert.strictEqual(joinLayerStack(handleRequest.request.layerStack), '/hello/:name/');
Expand Down