Skip to content

update: fix async-app app chaining when mounting sub app#45

Draft
micaelbergeron wants to merge 2 commits into
muralco:masterfrom
micaelbergeron:fix/async-app-mount
Draft

update: fix async-app app chaining when mounting sub app#45
micaelbergeron wants to merge 2 commits into
muralco:masterfrom
micaelbergeron:fix/async-app-mount

Conversation

@micaelbergeron
Copy link
Copy Markdown
Collaborator

When mounting a sub app with app.use(path, subApp), express does a check to see if an app was sent through, or if it was a middleware.

In async-app, we are converting all MiddlewareArguments which confuses express to think that the mounted app isn't an sub application, but a simple middleware.

Thus, express doesn't fire the mount event, which in turns skips a bunch of express routines:

  • sets the mountpath on the subApp
  • copy some settings from the parent app (for instance "trust proxy")

With this change, we can now use req.app.path to get the full route the request was matched for, and req.ip will be properly propagated through.

Comment thread src/async.ts
Comment on lines +117 to +162
if (context.method === 'use') {
return args;
}
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a little bit too eager — one other solution would be to make isMiddleware return false whenever we send a App instance.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine!

When mounting a sub app with `app.use(path, subApp)`, express does a
check to see if an app was sent through, or if it was a middleware.

In async-app, we are converting all MiddlewareArguments which confuses
express to think that the mounted app isn't an sub application, but a
simple middleware.

Thus, express doesn't fire the `mount` event, which in turns skips a
bunch of express routines:

 - sets the `mountpath` on the subApp
 - copy some settings from the parent app (for instance "trust proxy")

With this change, we can now use `req.app.path` to get the full route
the request was matched for, and `req.ip` will be properly propagated
through.
Copy link
Copy Markdown
Collaborator

@nicolapalavecino nicolapalavecino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing finding!

Comment thread features/advanced.feature

Scenario: mounted apps (nest level 1)
Given app setting "trust proxy" is enabled
Given the request header X-Forwarded-For is "1.1.1.1"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NITPICK:

Suggested change
Given the request header X-Forwarded-For is "1.1.1.1"
And the request header X-Forwarded-For is "1.1.1.1"

Comment thread features/advanced.feature

Scenario: mounted apps (nest level 2)
Given app setting "trust proxy" is enabled
Given the request header X-Forwarded-For is "10.0.1.3"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NITPICK:

Suggested change
Given the request header X-Forwarded-For is "10.0.1.3"
And the request header X-Forwarded-For is "10.0.1.3"

Comment thread src/async.ts
Comment on lines +117 to +162
if (context.method === 'use') {
return args;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine!

Comment thread src/test.ts
onTearDown,
}) => {
// === App setup === //
Given('app setting "(.*)" is enabled', (setting) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NITPICK: I would refer this step for enabling a setting for a specific app

Suggested change
Given('app setting "(.*)" is enabled', (setting) => {
Given('the (advanced|basic) app setting "(.*)" is enabled', (setting) => {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants