-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create very basic Asset Worker and plumb it into wrangler dev
This commit does the ground work needed in order to add Assets support for Workers in `wrangler dev`. Amongst others, it implements the following: - it creates a new package called `workers-shared` that hosts the `Asset Server Worker`, and the `Router Worker`in the future - it scaffolds the `Asset Server Worker` in some very basic form, with basic configuration. Further behaviour implementation will follow in a subsequent PR - it does the ground work of plumbing ASW into Miniflare
- Loading branch information
1 parent
c3e19b7
commit 0ce9eca
Showing
30 changed files
with
611 additions
and
110 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
"@cloudflare/workers-shared": minor | ||
"wrangler": minor | ||
--- | ||
|
||
feat: Create very basic Asset Server Worker and plumb it into `wrangler dev` | ||
|
||
These changes do the ground work needed in order to add Assets support for Workers in `wrangler dev`. They introduce the following components, and the following conceptual "client pipeline": | ||
|
||
<!-- prettier-ignore-start --> | ||
| | ||
wrangler dev | ||
| workerd | ||
+------------------+---------------------+ | ||
| | | | ||
| +-----------v--------------+ | | ||
| | | | | ||
| | NOOP Entry Worker | | | ||
| | | | | ||
| +------------+-------------+ | | ||
| | | | ||
| env.ASSET_SERVER | | ||
| | | | ||
| +------------v-------------+ | | ||
| | | | | ||
| | Asset Server Worker | | | ||
| | | | | ||
| +------------+-------------+ | | ||
| | | | ||
+-------------------+--------------------+ | ||
| | ||
v | ||
"Hello world from Asset Server" | ||
|
||
<!-- prettier-ignore-end --> | ||
|
||
Based on the above architectural diagram, this body of work implements the following: | ||
|
||
- it creates a new package called `workers-shared` that hosts the `Asset Server Worker`, and the `Router Worker`in the future | ||
- it scaffolds the `Asset Server Worker` in some very basic form, with basic configuration. Further behaviour implementation will follow in a subsequent PR | ||
- it does the ground work of plumbing ASW into Miniflare |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# workers-with-assets | ||
|
||
`workers-with-assets` is a test fixture that showcases Workers with Assets. This particular fixture sets up an assets-only Workers project. | ||
|
||
## dev | ||
|
||
To start a dev session you can run | ||
|
||
``` | ||
wrangler dev | ||
``` | ||
|
||
or | ||
|
||
``` | ||
wrangler dev --experimental-assets=./public | ||
``` | ||
|
||
## Run tests | ||
|
||
``` | ||
npm run test | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "assets-worker", | ||
"private": true, | ||
"scripts": { | ||
"check:type": "tsc", | ||
"dev": "npx wrangler dev", | ||
"test:ci": "vitest run", | ||
"test:watch": "vitest", | ||
"type:tests": "tsc -p ./tests/tsconfig.json" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-tsconfig": "workspace:*", | ||
"@cloudflare/workers-types": "^4.20240725.0", | ||
"undici": "^5.28.4", | ||
"wrangler": "workspace:*" | ||
}, | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Hello Workers + Assets World!</h1> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { resolve } from "node:path"; | ||
import { fetch } from "undici"; | ||
import { afterAll, beforeAll, describe, it } from "vitest"; | ||
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; | ||
|
||
describe("[Workers + Assets] `wrangler dev`", () => { | ||
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined; | ||
|
||
beforeAll(async () => { | ||
({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [ | ||
"--port=0", | ||
"--inspector-port=0", | ||
])); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stop?.(); | ||
}); | ||
|
||
it("renders ", async ({ expect }) => { | ||
const response = await fetch(`http://${ip}:${port}/`); | ||
const text = await response.text(); | ||
expect(response.status).toBe(200); | ||
expect(text).toContain(`Hello from Asset Server Worker 🚀`); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "@cloudflare/workers-tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node"] | ||
}, | ||
"include": ["**/*.ts", "../../../node-types.d.ts"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"esModuleInterop": true, | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["node"], | ||
"moduleResolution": "node", | ||
"noEmit": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["tests", "../../node-types.d.ts"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name = "assets-worker" | ||
compatibility_date = "2024-01-01" | ||
|
||
[experimental_assets] | ||
directory = "./public" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# `@cloudflare/workers-shared` | ||
|
||
This is a package that is used at Cloudflare to power some internal features of [Cloudflare Workers](https://developers.cloudflare.com/workers/), as well as their open-source equivalents here in workers-sdk and Wrangler. | ||
|
||
## `asset-server` | ||
|
||
The Asset Server Worker. | ||
|
||
For more details please refer to the dedicated README file. | ||
|
||
> [!NOTE] | ||
> Since code in this package is used by the Workers infrastructure, it is important that PRs are given careful review with regards to how they could cause a failure in production. | ||
> Ideally, there should be comprehensive tests for changes being made to give extra confidence about the behavior. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# `asset-server` | ||
|
||
The Asset Server is a [Cloudflare Worker](https://developers.cloudflare.com/workers/) that is responsible of serving assets for Workers deployed on the edge, that contain static assets as well. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default { | ||
async fetch(request, env) { | ||
return new Response("Hello from Asset Server Worker 🚀"); | ||
}, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## | ||
# Configuration file for the Asset Server Worker | ||
# | ||
# Please note that wrangler has a dependency on this file, and will | ||
# attempt to read it as part of setting up a new Miniflare instance | ||
# in developemnt mode. We should ensure that any configuration changes | ||
# to this file are persisted in wrangler as well, when necessary. | ||
# (see packages/wrangler/src/dev/miniflare.ts -> buildMiniflareOptions()) | ||
## | ||
name = "asset-server" | ||
main = "src/index.ts" | ||
compatibility_date = "2024-07-31" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "@cloudflare/workers-shared", | ||
"version": "0.0.1", | ||
"description": "Package that is used at Cloudflare to power some internal features of Cloudflare Workers.", | ||
"scripts": { | ||
"build": "pnpm run clean && pnpm run bundle:asset-server", | ||
"bundle:asset-server": "esbuild asset-server-worker/src/index.ts --format=esm --bundle --outfile=dist/asset-server-worker.mjs --sourcemap=external", | ||
"clean": "rimraf dist", | ||
"dev": "pnpm run clean && concurrently -n bundle:asset-server -c blue \"pnpm run bundle:asset-server --watch\"" | ||
}, | ||
"devDependencies": { | ||
"concurrently": "^8.2.2", | ||
"esbuild": "0.17.19", | ||
"rimraf": "^6.0.1", | ||
"typescript": "^5.5.4" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "http://turbo.build/schema.json", | ||
"extends": ["//"], | ||
"pipeline": { | ||
"build": { | ||
"outputs": ["dist/**"] | ||
} | ||
} | ||
} |
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
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
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
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
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
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
Oops, something went wrong.