-
-
Notifications
You must be signed in to change notification settings - Fork 576
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
Convert hooks system to a middleware system #2071
Merged
Conversation
This file contains 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
…particularly useful there
🦋 Changeset detectedLatest commit: 16aff7f The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…and `requestContext` directly; passing these through additional arguments is now deprecated. `plugin.grafast.hooks.args` is now `plugin.grafast.middleware.prepareArgs`, and the signature has changed - you must be sure to call the `next()` function and ctx/resolvedPreset can be extracted directly from `args`: ```diff const plugin = { grafast: { - hooks: { + middleware: { - args({ args, ctx, resolvedPreset }) { + prepareArgs(next, { args }) { + const { requestContext: ctx, resolvedPreset } = args; // ... + return next(); } } } } ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
In an earlier version of v5 I introduced the
hooks
system (e.g.plugin.grafserv.hooks.ruruHTMLParts
). The intention of this system was to give people a chance to quickly customize certain things, e.g. tweaking the configuration object for Ruru.But it turns out that sometimes we want to wrap a code block in hooks. At first I attempted to do this by having hooks return a callback function to be called once the block completes, this was inspired by the pattern used by the Envelop plugin system. However, the issue with this is that it doesn't integrate well with things like Node's
async_hooks
system unless the wrapped code is synchronous. To solve this, I've switched to more of a middleware system inspired by Koa. This allows you to do things like timing execution and reporting on the number of errors:And also hooking into
async_hooks
to track where errors/promises are generated, etc.It's important to note that some of the middlewares are synchronous and some asynchronous (and that we try not to create promises unless they're necessary) so although it's possible to write many middlewares as
async
/await
functions, we recommend against it in plugin code because it forces promises, causing performance issues and also preventing synchronous paths which may be used, for exampleexecuteSync()
. This is why we have thenext.callback()
method above rather than callingnext()
directly.Middleware are experimental, and could change in minor revisions.
In many cases, middleware replace hooks (since every hook can theoretically be implemented via middleware).
Performance impact
Marginal if middlewares are not in use.
Security impact
Increases surface area, and middleware can be written in a way that compromises the system (but that's the responsibility of the person writing the middleware).
Checklist
yarn lint:fix
passes.yarn test
passes.RELEASE_NOTES.md
file (if one exists).