feat(workers-shared): Add Asset Server Worker behaviour#6539
feat(workers-shared): Add Asset Server Worker behaviour#6539CarmenPopoviciu merged 6 commits intomainfrom
Conversation
🦋 Changeset detectedLatest commit: c0bcf8d The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 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 |
|
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/10523785777/npm-package-wrangler-6539You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/6539/npm-package-wrangler-6539Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-wrangler-6539 dev path/to/script.jsAdditional artifacts:npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-create-cloudflare-6539 --no-auto-updatenpm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-kv-asset-handler-6539npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-miniflare-6539npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-pages-shared-6539npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-vitest-pool-workers-6539npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-workers-editor-shared-6539npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-workers-shared-6539Note that these links will no longer work once the GitHub Actions artifact expires.
Please ensure constraints are pinned, and |
e2dd4eb to
ad3a948
Compare
GregBrimble
left a comment
There was a problem hiding this comment.
Approved with additional small pieces here: #6542
|
@GregBrimble thanks for reviewing this. I've incorporated the changes you suggested in this commit |
db338a2 to
5b7156f
Compare
petebacondarwin
left a comment
There was a problem hiding this comment.
Looking good. I left a bunch of comments.
I'd really really like to see some tests of this worker.
packages/workers-shared/package.json
Outdated
| "scripts": { | ||
| "build": "pnpm run clean && pnpm run bundle:asset-server:prod", | ||
| "bundle:asset-server": "esbuild asset-server-worker/src/index.ts --format=esm --bundle --outfile=dist/asset-server-worker.mjs --sourcemap=external", | ||
| "bundle:asset-server": "esbuild asset-server-worker/src/index.ts --format=esm --bundle --outfile=dist/asset-server-worker.mjs --sourcemap=external --external:cloudflare:*", |
There was a problem hiding this comment.
I wonder if we could use wrangler deploy --dry-run --outdir=dist/asset-worker.mjs to do this so that we don't have to keep the esbuild settings in sync?
| assetEntry | ||
| ); | ||
| console.log(assetResponse); | ||
| if (!assetResponse || !assetResponse.value) { |
There was a problem hiding this comment.
It feels like assetResponse.value being falsy is a programming error on our part?
There was a problem hiding this comment.
technically this should be just a sanity check, but felt like we should check nonetheless. I'll ask the EWC folks if we want to throw a different error here. IMO a 500 sounds like the right thing to return
827b4fc to
e11c3ef
Compare
f395487 to
25c0ce4
Compare
cb8fcab to
eed70a0
Compare
eed70a0 to
786c1a7
Compare
| const newResponse = new Response(response.body, response); | ||
| // ensure the runtime will return the metadata we need | ||
| newResponse.headers.append( | ||
| "cf-kv-metadata", |
There was a problem hiding this comment.
| @@ -0,0 +1,12 @@ | |||
| import { defineProject, mergeConfig } from "vitest/config"; | |||
There was a problem hiding this comment.
I'm no vite expert so I would appreciate a sanity check here on this config 🙏
1154d63 to
c0bcf8d
Compare
| const TAIL_SIZE = 8; | ||
| const ENTRY_SIZE = PATH_HASH_SIZE + CONTENT_HASH_SIZE + TAIL_SIZE; | ||
|
|
||
| export type AssetEntry = { |
There was a problem hiding this comment.
I don't think this ends up being used anywhere
There was a problem hiding this comment.
oh right. must be some leftover from all the iterations 😅 . will remove as part of WC-2597
| let { pathname } = url; | ||
|
|
||
| const assetsManifest = new AssetsManifest(this.env.ASSETS_MANIFEST); | ||
| pathname = globalThis.decodeURIComponent(pathname); |
There was a problem hiding this comment.
not sure if this should be decoded because the assetManifest uses encoded pathnames?
Might be worth adding a test for url encoding with a funky pathname. Not high priority though.
There was a problem hiding this comment.
ok, so funky path names def don't work, with or without this decoding. Let's have a look together, and I'll add the proper fix/tests in a follow-up PR. Tracking in WC-2597
andyjessop
left a comment
There was a problem hiding this comment.
Happy with this - very nice work!
What this PR solves / how to test
[Workers + Assets]
This PR adds a proper-ish implementation of a very un-opinionated Asset Server Worker. Proper-ish because it handles the essential things needed for ASW to work, but I expect some other details we'll be needed as Workers + Assets work progresses further.
Fixes WC-2434
Author has addressed the following