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

Check if middleware is an array #5703

Conversation

penalosa
Copy link
Contributor

@penalosa penalosa commented Apr 24, 2024

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

Copy link

changeset-bot bot commented Apr 24, 2024

🦋 Changeset detected

Latest commit: 1b189a1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
wrangler Patch
@cloudflare/vitest-pool-workers Patch

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

@penalosa penalosa requested a review from a team as a code owner April 24, 2024 13:58
Copy link
Contributor

@petebacondarwin petebacondarwin left a 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?

@penalosa
Copy link
Contributor Author

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.

@petebacondarwin
Copy link
Contributor

I don't think it is just for testing... we add these middlewares to handle specific wrangler dev scenarios such as the __scheduled path handling. See https://github.com/cloudflare/workers-sdk/tree/main/packages/wrangler/templates/middleware

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.

@petebacondarwin
Copy link
Contributor

Shouldn't the fix be for the itty-router to pass through this middleware property untouched by the Proxy that they create?

@penalosa
Copy link
Contributor Author

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?

Copy link
Contributor

github-actions bot commented Apr 25, 2024

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 with this latest build directly:

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.


wrangler@3.57.1 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240512.0
workerd 1.20240512.0 1.20240512.0
workerd --version 1.20240512.0 2024-05-12

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

Copy link
Contributor

@petebacondarwin petebacondarwin left a 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...

@kwhitley
Copy link

kwhitley commented Apr 28, 2024

Shouldn't the fix be for the itty-router to pass through this middleware property untouched by the Proxy that they create?

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 returns a function like you might use here:

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
}

@kwhitley
Copy link

Any update on this?

We'd love to remove our disclaimer for using itty with the very environment it was designed for! :)

@petebacondarwin petebacondarwin added the awaiting Cloudflare response Awaiting response from workers-sdk maintainer team label May 21, 2024
@penalosa penalosa force-pushed the 5420-bug-wrangler-dev-throws-if-finding-a-non-iterable-middleware-export branch from f070ae6 to bbefc7b Compare May 23, 2024 17:24
@penalosa
Copy link
Contributor Author

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...

This should be ready for a re-review now

Copy link
Contributor

@petebacondarwin petebacondarwin left a 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}
Copy link
Contributor

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.

@penalosa penalosa merged commit a905f31 into main May 24, 2024
19 checks passed
@penalosa penalosa deleted the 5420-bug-wrangler-dev-throws-if-finding-a-non-iterable-middleware-export branch May 24, 2024 10:49
@CarmenPopoviciu CarmenPopoviciu removed the awaiting Cloudflare response Awaiting response from workers-sdk maintainer team label May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

🐛 BUG: wrangler dev throws if finding a non-iterable middleware export
4 participants