-
Notifications
You must be signed in to change notification settings - Fork 548
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
Check if middleware is an array #5703
Check if middleware is an array #5703
Conversation
🦋 Changeset detectedLatest commit: 1b189a1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this means that it won't crash for itty-router anymore.
But do we actually want to ignore middleware that has been proxied such as is happening there?
I believe we do, yes. As far as I know we've never documented this middleware behaviour, and I think we implemented it for testing purposes. |
I don't think it is just for testing... we add these middlewares to handle specific If the itty-router is just wrapping the array containing these middlewares in a proxy that is then ignored by our bundler then these middlewares are no longer applied and so this functionality would be unavailable to them. |
Shouldn't the fix be for the itty-router to pass through this middleware property untouched by the Proxy that they create? |
Sorry, that wasn't super clear—we do definitely use middleware, but I think this way of defining middleware (a user specified array in the exported handler object) was only for testing? |
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9212466448/npm-package-wrangler-5703 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/5703/npm-package-wrangler-5703 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9212466448/npm-package-wrangler-5703 dev path/to/script.js Additional artifacts:npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9212466448/npm-package-create-cloudflare-5703 --no-auto-update npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9212466448/npm-package-cloudflare-kv-asset-handler-5703 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9212466448/npm-package-miniflare-5703 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9212466448/npm-package-cloudflare-pages-shared-5703 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9212466448/npm-package-cloudflare-vitest-pool-workers-5703 Note that these links will no longer work once the GitHub Actions artifact expires.
Please ensure constraints are pinned, and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed offline. I wonder if we should avoid reading the middleware
property from the default export at all? Perhaps move it to a non-default export?
I believe we could do this if this is only an internal API...
We don't actually include/expose a middleware property at all. Our router is an object with a Proxy prototype. Any attributes on the object are accessed normally, and unknown attributes are intercepted by the proxy, interpreting them as route handlers. So a accessing router.middleware('/path', () => 'this will match the MIDDLEWARE HTTP verb...') As a result, something like this (below) will throw a similar error to what we're seeing here, as it DOES find router.middleware, but it's a function... not an iterable array: for (const handler of router.middleware) {
// do something
} |
Any update on this? We'd love to remove our disclaimer for using itty with the very environment it was designed for! :) |
f070ae6
to
bbefc7b
Compare
This should be ready for a re-review now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it!
|
||
export const __INTERNAL_WRANGLER_MIDDLEWARE__ = [ | ||
...(OTHER_EXPORTS.__INJECT_FOR_TESTING_WRANGLER_MIDDLEWARE__ ?? []), | ||
${middlewareFns} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: I would prefer to put the join(",")
here rather than in the variable declaration above.
What this PR solves / how to test
Use the named export
__INTERNAL_WRANGLER_MIDDLEWARE__
for listing the middleware to be injected in module-format workers. To inject middleware for testing, use__INJECT_FOR_TESTING_WRANGLER_MIDDLEWARE__
.This removes support for adding a
middleware
array to your exported handler.Fixes #5420.
Author has addressed the following