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

feat(workers-shared): Add Asset Server Worker behaviour #6539

Merged
merged 6 commits into from
Aug 23, 2024

Conversation

CarmenPopoviciu
Copy link
Contributor

@CarmenPopoviciu CarmenPopoviciu commented Aug 20, 2024

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

  • Tests
    • TODO (before merge)
    • Included
    • Not necessary because:
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required / Maybe required
    • Not required because: no e2e tests capture ASW2
  • Changeset (Changeset guidelines)
    • TODO (before merge)
    • Included
    • Not necessary because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Not necessary because: we're not there yet

@CarmenPopoviciu CarmenPopoviciu requested a review from a team as a code owner August 20, 2024 20:03
Copy link

changeset-bot bot commented Aug 20, 2024

🦋 Changeset detected

Latest commit: c0bcf8d

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

This PR includes changesets to release 3 packages
Name Type
@cloudflare/workers-shared Minor
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

Copy link
Contributor

github-actions bot commented Aug 20, 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/10523785777/npm-package-wrangler-6539

You 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-6539

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-wrangler-6539 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-create-cloudflare-6539 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-kv-asset-handler-6539
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-miniflare-6539
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-pages-shared-6539
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-vitest-pool-workers-6539
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-workers-editor-shared-6539
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/10523785777/npm-package-cloudflare-workers-shared-6539

Note that these links will no longer work once the GitHub Actions artifact expires.


[email protected] includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240821.0
workerd 1.20240821.1 1.20240821.1
workerd --version 1.20240821.1 2024-08-21

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

@CarmenPopoviciu CarmenPopoviciu force-pushed the carmen/asw2 branch 2 times, most recently from e2dd4eb to ad3a948 Compare August 20, 2024 20:30
@CarmenPopoviciu CarmenPopoviciu changed the title feat(workers-shared): Add Assets Server Worker behaviour feat(workers-shared): Add Asset Server Worker behaviour Aug 20, 2024
Copy link
Member

@GregBrimble GregBrimble left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with additional small pieces here: #6542

@CarmenPopoviciu
Copy link
Contributor Author

@GregBrimble thanks for reviewing this. I've incorporated the changes you suggested in this commit

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.

Looking good. I left a bunch of comments.
I'd really really like to see some tests of this worker.

fixtures/workers-with-assets/tests/index.test.ts Outdated Show resolved Hide resolved
packages/wrangler/src/dev/miniflare.ts Show resolved Hide resolved
@@ -23,7 +23,7 @@
],
"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:*",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

packages/workers-shared/asset-server-worker/src/index.ts Outdated Show resolved Hide resolved
packages/workers-shared/asset-server-worker/src/index.ts Outdated Show resolved Hide resolved
packages/workers-shared/asset-server-worker/src/index.ts Outdated Show resolved Hide resolved
assetEntry
);
console.log(assetResponse);
if (!assetResponse || !assetResponse.value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like assetResponse.value being falsy is a programming error on our part?

Copy link
Contributor Author

@CarmenPopoviciu CarmenPopoviciu Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@CarmenPopoviciu CarmenPopoviciu force-pushed the carmen/asw2 branch 2 times, most recently from 827b4fc to e11c3ef Compare August 22, 2024 19:02
@CarmenPopoviciu CarmenPopoviciu force-pushed the carmen/asw2 branch 2 times, most recently from f395487 to 25c0ce4 Compare August 22, 2024 19:26
@CarmenPopoviciu CarmenPopoviciu removed the e2e Run e2e tests on a PR label Aug 22, 2024
@CarmenPopoviciu CarmenPopoviciu force-pushed the carmen/asw2 branch 4 times, most recently from cb8fcab to eed70a0 Compare August 23, 2024 01:23
const newResponse = new Response(response.body, response);
// ensure the runtime will return the metadata we need
newResponse.headers.append(
"cf-kv-metadata",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,12 @@
import { defineProject, mergeConfig } from "vitest/config";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no vite expert so I would appreciate a sanity check here on this config 🙏

@@ -4,6 +4,11 @@ const CONTENT_HASH_SIZE = 16;
const TAIL_SIZE = 8;
const ENTRY_SIZE = PATH_HASH_SIZE + CONTENT_HASH_SIZE + TAIL_SIZE;

export type AssetEntry = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this ends up being used anywhere

Copy link
Contributor Author

@CarmenPopoviciu CarmenPopoviciu Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

@andyjessop andyjessop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with this - very nice work!

@CarmenPopoviciu CarmenPopoviciu added the e2e Run e2e tests on a PR label Aug 23, 2024
@CarmenPopoviciu CarmenPopoviciu merged commit 6c057d1 into main Aug 23, 2024
25 checks passed
@CarmenPopoviciu CarmenPopoviciu deleted the carmen/asw2 branch August 23, 2024 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e Run e2e tests on a PR
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

6 participants