Skip to content

Commit fed1fda

Browse files
authored
chore(workers-shared): Rename Asset Worker RPC methods as unstable (#6670)
The Asset Worker is currently set up as a `WorkerEntrypoint` class so that it is able to accept RPC calls to any of its public methods. There are currently four such public methods defined on this Worker: `canFetch`, `getByETag`, `getByPathname` and `exists`. While we are stabilising the implementation details of these methods, we would like to prevent developers from having their Workers call these methods directly. To that end, we are adopting the `unstable_<method_name>` naming convention for all of the aforementioned methods, to indicate that they are still in flux and that they are not an established API contract.
1 parent 2d080df commit fed1fda

File tree

6 files changed

+75
-24
lines changed

6 files changed

+75
-24
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/workers-shared": patch
3+
---
4+
5+
chore: Rename asset-worker RPC methods as unstable\_\*
6+
7+
The Asset Worker is currently set up as a `WorkerEntrypoint` class so that it is able to accept RPC calls to any of its public methods. There are currently four such public methods defined on this Worker: `canFetch`, `getByETag`, `getByPathname` and `exists`. While we are stabilising the implementation details of these methods, we would like to prevent developers from having their Workers call these methods directly. To that end, we are adopting the `unstable_<method_name>` naming convention for all of the aforementioned methods, to indicate that they are still in flux and that they are not an established API contract.

fixtures/asset-config/html-handling.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;
1010
vi.mock("../../packages/workers-shared/asset-worker/src/utils/kv.ts");
1111
vi.mock("../../packages/workers-shared/asset-worker/src/configuration");
1212
const existsMock = (fileList: Set<string>) => {
13-
vi.spyOn(Worker.prototype, "exists").mockImplementation(
13+
vi.spyOn(Worker.prototype, "unstable_exists").mockImplementation(
1414
async (pathname: string) => {
1515
if (fileList.has(pathname)) {
1616
return pathname;

packages/workers-shared/asset-worker/src/handler.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import type EntrypointType from "./index";
1313
export const handleRequest = async (
1414
request: Request,
1515
configuration: Required<AssetConfig>,
16-
exists: typeof EntrypointType.prototype.exists,
17-
getByETag: typeof EntrypointType.prototype.getByETag
16+
exists: typeof EntrypointType.prototype.unstable_exists,
17+
getByETag: typeof EntrypointType.prototype.unstable_getByETag
1818
) => {
1919
const { pathname, search } = new URL(request.url);
2020

@@ -68,7 +68,7 @@ type Intent =
6868
export const getIntent = async (
6969
pathname: string,
7070
configuration: Required<AssetConfig>,
71-
exists: typeof EntrypointType.prototype.exists,
71+
exists: typeof EntrypointType.prototype.unstable_exists,
7272
skipRedirects = false
7373
): Promise<Intent> => {
7474
switch (configuration.html_handling) {
@@ -105,7 +105,7 @@ export const getIntent = async (
105105
const htmlHandlingAutoTrailingSlash = async (
106106
pathname: string,
107107
configuration: Required<AssetConfig>,
108-
exists: typeof EntrypointType.prototype.exists,
108+
exists: typeof EntrypointType.prototype.unstable_exists,
109109
skipRedirects: boolean
110110
): Promise<Intent> => {
111111
let redirectResult: Intent = null;
@@ -231,7 +231,7 @@ const htmlHandlingAutoTrailingSlash = async (
231231
const htmlHandlingForceTrailingSlash = async (
232232
pathname: string,
233233
configuration: Required<AssetConfig>,
234-
exists: typeof EntrypointType.prototype.exists,
234+
exists: typeof EntrypointType.prototype.unstable_exists,
235235
skipRedirects: boolean
236236
): Promise<Intent> => {
237237
let redirectResult: Intent = null;
@@ -362,7 +362,7 @@ const htmlHandlingForceTrailingSlash = async (
362362
const htmlHandlingDropTrailingSlash = async (
363363
pathname: string,
364364
configuration: Required<AssetConfig>,
365-
exists: typeof EntrypointType.prototype.exists,
365+
exists: typeof EntrypointType.prototype.unstable_exists,
366366
skipRedirects: boolean
367367
): Promise<Intent> => {
368368
let redirectResult: Intent = null;
@@ -521,7 +521,7 @@ const htmlHandlingDropTrailingSlash = async (
521521
const htmlHandlingNone = async (
522522
pathname: string,
523523
configuration: Required<AssetConfig>,
524-
exists: typeof EntrypointType.prototype.exists
524+
exists: typeof EntrypointType.prototype.unstable_exists
525525
): Promise<Intent> => {
526526
const exactETag = await exists(pathname);
527527
if (exactETag) {
@@ -534,7 +534,7 @@ const htmlHandlingNone = async (
534534
const notFound = async (
535535
pathname: string,
536536
configuration: Required<AssetConfig>,
537-
exists: typeof EntrypointType.prototype.exists
537+
exists: typeof EntrypointType.prototype.unstable_exists
538538
): Promise<Intent> => {
539539
switch (configuration.not_found_handling) {
540540
case "single-page-application": {
@@ -566,7 +566,7 @@ const safeRedirect = async (
566566
file: string,
567567
destination: string,
568568
configuration: Required<AssetConfig>,
569-
exists: typeof EntrypointType.prototype.exists,
569+
exists: typeof EntrypointType.prototype.unstable_exists,
570570
skip: boolean
571571
): Promise<Intent> => {
572572
if (skip) {

packages/workers-shared/asset-worker/src/index.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ import { getAssetWithMetadataFromKV } from "./utils/kv";
1111
import type { AssetConfig } from "../../utils/types";
1212

1313
type Env = {
14-
// ASSETS_MANIFEST is a pipeline binding to an ArrayBuffer containing the
15-
// binary-encoded site manifest
14+
/*
15+
* ASSETS_MANIFEST is a pipeline binding to an ArrayBuffer containing the
16+
* binary-encoded site manifest
17+
*/
1618
ASSETS_MANIFEST: ArrayBuffer;
1719

18-
// ASSETS_KV_NAMESPACE is a pipeline binding to the KV namespace that the
19-
// assets are in.
20+
/*
21+
* ASSETS_KV_NAMESPACE is a pipeline binding to the KV namespace that the
22+
* assets are in.
23+
*/
2024
ASSETS_KV_NAMESPACE: KVNamespace;
2125

2226
CONFIG: AssetConfig;
@@ -27,6 +31,17 @@ type Env = {
2731
SENTRY_ACCESS_CLIENT_SECRET: string;
2832
};
2933

34+
/*
35+
* The Asset Worker is currently set up as a `WorkerEntrypoint` class so
36+
* that it is able to accept RPC calls to any of its public methods. There
37+
* are currently four such public methods defined on this Worker:
38+
* `canFetch`, `getByETag`, `getByPathname` and `exists`. While we are
39+
* stabilising the implementation details of these methods, we would like
40+
* to prevent developers from having their Workers call these methods
41+
* directly. To that end, we are adopting the `unstable_<method_name>`
42+
* naming convention for all of the aforementioned methods, to indicate that
43+
* they are still in flux and that they are not an established API contract.
44+
*/
3045
export default class extends WorkerEntrypoint<Env> {
3146
async fetch(request: Request): Promise<Response> {
3247
let sentry: ReturnType<typeof setupSentry> | undefined;
@@ -42,8 +57,8 @@ export default class extends WorkerEntrypoint<Env> {
4257
return handleRequest(
4358
request,
4459
applyConfigurationDefaults(this.env.CONFIG),
45-
this.exists.bind(this),
46-
this.getByETag.bind(this)
60+
this.unstable_exists.bind(this),
61+
this.unstable_getByETag.bind(this)
4762
);
4863
} catch (err) {
4964
const response = new InternalServerErrorResponse(err as Error);
@@ -66,7 +81,7 @@ export default class extends WorkerEntrypoint<Env> {
6681
...applyConfigurationDefaults(this.env.CONFIG),
6782
not_found_handling: "none",
6883
},
69-
this.exists.bind(this)
84+
this.unstable_exists.bind(this)
7085
);
7186
// if asset exists but non GET/HEAD method, 405
7287
if (intent && ["GET", "HEAD"].includes(method)) {
@@ -78,7 +93,7 @@ export default class extends WorkerEntrypoint<Env> {
7893
return true;
7994
}
8095

81-
async getByETag(
96+
async unstable_getByETag(
8297
eTag: string
8398
): Promise<{ readableStream: ReadableStream; contentType: string }> {
8499
const asset = await getAssetWithMetadataFromKV(
@@ -98,18 +113,18 @@ export default class extends WorkerEntrypoint<Env> {
98113
};
99114
}
100115

101-
async getByPathname(
116+
async unstable_getByPathname(
102117
pathname: string
103118
): Promise<{ readableStream: ReadableStream; contentType: string } | null> {
104-
const eTag = await this.exists(pathname);
119+
const eTag = await this.unstable_exists(pathname);
105120
if (!eTag) {
106121
return null;
107122
}
108123

109-
return this.getByETag(eTag);
124+
return this.unstable_getByETag(eTag);
110125
}
111126

112-
async exists(pathname: string): Promise<string | null> {
127+
async unstable_exists(pathname: string): Promise<string | null> {
113128
const assetsManifest = new AssetsManifest(this.env.ASSETS_MANIFEST);
114129
return await assetsManifest.get(pathname);
115130
}

packages/workers-shared/asset-worker/wrangler.toml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@ account_id = "0f1b8aa119a907021f659042f95ea9ba"
1212
workers_dev = false
1313
main = "src/index.ts"
1414
compatibility_date = "2024-07-31"
15-
compatibility_flags = ["nodejs_compat"]
15+
compatibility_flags = ["nodejs_compat"]
16+
17+
[[unsafe.bindings]]
18+
name = "ASSETS_MANIFEST"
19+
type = "param"
20+
param = "assetManifest"
21+
data_ref = true
22+
23+
[[unsafe.bindings]]
24+
name = "ASSETS_KV_NAMESPACE"
25+
type = "internal_assets"
26+
27+
[unsafe.metadata.build_options]
28+
stable_id = "cloudflare/cf_asset_worker"
29+
networks = ["cf","jdc"]

packages/workers-shared/router-worker/wrangler.toml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,20 @@
88
# (see packages/wrangler/src/dev/miniflare.ts -> buildMiniflareOptions())
99
##
1010
name = "router-worker"
11+
account_id = "0f1b8aa119a907021f659042f95ea9ba"
12+
workers_dev = false
1113
main = "src/index.ts"
12-
compatibility_date = "2024-07-31"
14+
compatibility_date = "2024-07-31"
15+
16+
[[unsafe.bindings]]
17+
name = "ASSET_WORKER"
18+
type = "internal_assets"
19+
fetcherApi = "fetcher"
20+
21+
[[unsafe.bindings]]
22+
name = "USER_WORKER"
23+
type = "origin"
24+
25+
[unsafe.metadata.build_options]
26+
stable_id = "cloudflare/cf_router_worker"
27+
networks = ["cf","jdc"]

0 commit comments

Comments
 (0)