-
-
Notifications
You must be signed in to change notification settings - Fork 19
Fix context loss when async storage is instantiated early in the request lifecycle #12
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2a9df9c
Add test for POST request with body, using async storage in prevalida…
kibertoad 4e8b091
Fix linting
kibertoad 3da0d3b
Add end-to-end test
kibertoad 7bce481
Fix linting
kibertoad 075a526
Implement hook configuration support
kibertoad d405af2
Fix typo
kibertoad d97cb01
Remove unneeded timeout config
kibertoad 48c23e7
Address code review comments
kibertoad c37fe5c
Set default to preValidation which actually works, update types, add …
kibertoad 965f256
Remove support for multiple hooks, clarify documentation
kibertoad 5e19b08
Reduce duplication
kibertoad 0be0b99
Add test case for not losing context after body parsing
kibertoad b7384f0
Monkey-patch event emitter to support all lifecycle phases
kibertoad d859cfa
Fix Node 10 support
kibertoad 4cedd50
Implement alternate solution without monkey-patching
kibertoad ecaea79
Extend test to also cover preSerialization hook
kibertoad 457f7ac
Use symbol for asyncResource
kibertoad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const fastify = require('fastify') | ||
const { fastifyRequestContextPlugin } = require('../../lib/requestContextPlugin') | ||
|
||
function initAppGet(endpoint) { | ||
const app = fastify({ logger: true }) | ||
app.register(fastifyRequestContextPlugin) | ||
|
||
app.get('/', endpoint) | ||
return app | ||
} | ||
|
||
function initAppPost(endpoint) { | ||
const app = fastify({ logger: true }) | ||
app.register(fastifyRequestContextPlugin) | ||
|
||
app.post('/', endpoint) | ||
|
||
return app | ||
} | ||
|
||
function initAppPostWithPrevalidation(endpoint) { | ||
const app = fastify({ logger: true }) | ||
app.register(fastifyRequestContextPlugin, { hook: 'preValidation' }) | ||
|
||
const preValidationFn = (req, reply, done) => { | ||
const requestId = Number.parseInt(req.body.requestId) | ||
req.requestContext.set('testKey', `testValue${requestId}`) | ||
done() | ||
} | ||
|
||
app.route({ | ||
url: '/', | ||
method: ['GET', 'POST'], | ||
preValidation: preValidationFn, | ||
handler: endpoint, | ||
}) | ||
|
||
return app | ||
} | ||
|
||
function initAppPostWithAllPlugins(endpoint, requestHook) { | ||
const app = fastify({ logger: true }) | ||
app.register(fastifyRequestContextPlugin, { hook: requestHook }) | ||
|
||
app.addHook('onRequest', (req, reply, done) => { | ||
req.requestContext.set('onRequest', 'dummy') | ||
done() | ||
}) | ||
|
||
app.addHook('preParsing', (req, reply, payload, done) => { | ||
req.requestContext.set('preParsing', 'dummy') | ||
done(null, payload) | ||
}) | ||
|
||
app.addHook('preValidation', (req, reply, done) => { | ||
const requestId = Number.parseInt(req.body.requestId) | ||
req.requestContext.set('preValidation', requestId) | ||
req.requestContext.set('testKey', `testValue${requestId}`) | ||
done() | ||
}) | ||
|
||
app.addHook('preHandler', (req, reply, done) => { | ||
const requestId = Number.parseInt(req.body.requestId) | ||
req.requestContext.set('preHandler', requestId) | ||
done() | ||
}) | ||
|
||
app.addHook('preSerialization', (req, reply, payload, done) => { | ||
const onRequestValue = req.requestContext.get('onRequest') | ||
const preValidationValue = req.requestContext.get('preValidation') | ||
done(null, { | ||
...payload, | ||
preSerialization1: onRequestValue, | ||
preSerialization2: preValidationValue, | ||
}) | ||
}) | ||
app.route({ | ||
url: '/', | ||
method: ['GET', 'POST'], | ||
handler: endpoint, | ||
}) | ||
|
||
return app | ||
} | ||
|
||
module.exports = { | ||
initAppPostWithAllPlugins, | ||
initAppPostWithPrevalidation, | ||
initAppPost, | ||
initAppGet, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.