Skip to content

Commit

Permalink
WfP Assets support
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Dec 5, 2024
1 parent bc30823 commit 6caebe4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-plants-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

feat: Allow Workers for Platforms scripts (scripts deployed with `--dispatch-namespace`) to bring along `assets`
87 changes: 69 additions & 18 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5140,6 +5140,36 @@ addEventListener('fetch', event => {});`
});
await runWrangler("deploy");
});

it("should be able to upload to a WfP script", async () => {
const assets = [
{ filePath: "file-1.txt", content: "Content of file-1" },
{ filePath: "boop/file-2.txt", content: "Content of file-2" },
];
writeAssets(assets);
writeWorkerSource({ format: "js" });
writeWranglerConfig({
compatibility_date: "2024-09-27",
compatibility_flags: ["nodejs_compat"],
assets: {
directory: "assets",
html_handling: "none",
},
});
await mockAUSRequest(undefined, undefined, undefined, "my-namespace");
mockSubDomainRequest();
mockUploadWorkerRequest({
expectedAssets: {
jwt: "<<aus-completion-token>>",
config: { html_handling: "none" },
},
expectedCompatibilityDate: "2024-09-27",
expectedCompatibilityFlags: ["nodejs_compat"],
expectedMainModule: undefined,
expectedDispatchNamespace: "my-namespace",
});
await runWrangler("deploy --dispatch-namespace my-namespace");
});
});

describe("workers_dev setting", () => {
Expand Down Expand Up @@ -12542,25 +12572,46 @@ function mockPostQueueHTTPConsumer(
const mockAUSRequest = async (
bodies?: AssetManifest[],
buckets: string[][] = [[]],
jwt: string = "<<aus-completion-token>>"
jwt: string = "<<aus-completion-token>>",
dispatchNamespace?: string
) => {
msw.use(
http.post<never, AssetManifest>(
`*/accounts/some-account-id/workers/scripts/test-name/assets-upload-session`,
async ({ request }) => {
bodies?.push(await request.json());
return HttpResponse.json(
{
success: true,
errors: [],
messages: [],
result: { jwt, buckets },
},
{ status: 201 }
);
}
)
);
if (dispatchNamespace) {
msw.use(
http.post<never, AssetManifest>(
`*/accounts/some-account-id/workers/dispatch/namespaces/my-namespace/scripts/test-name/assets-upload-session`,
async ({ request }) => {
bodies?.push(await request.json());
return HttpResponse.json(
{
success: true,
errors: [],
messages: [],
result: { jwt, buckets },
},
{ status: 201 }
);
}
)
);
} else {
msw.use(
http.post<never, AssetManifest>(
`*/accounts/some-account-id/workers/scripts/test-name/assets-upload-session`,
async ({ request }) => {
bodies?.push(await request.json());
return HttpResponse.json(
{
success: true,
errors: [],
messages: [],
result: { jwt, buckets },
},
{ status: 201 }
);
}
)
);
}
};

const mockAssetUploadRequest = async (
Expand Down
9 changes: 7 additions & 2 deletions packages/wrangler/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,24 @@ const MAX_UPLOAD_GATEWAY_ERRORS = 5;

export const syncAssets = async (
accountId: string | undefined,
assetDirectory: string,
scriptName: string,
assetDirectory: string
dispatchNamespace?: string
): Promise<string> => {
assert(accountId, "Missing accountId");

// 1. generate asset manifest
logger.info("🌀 Building list of assets...");
const manifest = await buildAssetManifest(assetDirectory);

const url = dispatchNamespace
? `/accounts/${accountId}/workers/dispatch/namespaces/${dispatchNamespace}/scripts/${scriptName}/assets-upload-session`
: `/accounts/${accountId}/workers/scripts/${scriptName}/assets-upload-session`;

// 2. fetch buckets w/ hashes
logger.info("🌀 Starting asset upload...");
const initializeAssetsResponse = await fetchResult<InitializeAssetsResponse>(
`/accounts/${accountId}/workers/scripts/${scriptName}/assets-upload-session`,
url,
{
headers: { "Content-Type": "application/json" },
method: "POST",
Expand Down
7 changes: 6 additions & 1 deletion packages/wrangler/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,12 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
// Upload assets if assets is being used
const assetsJwt =
props.assetsOptions && !props.dryRun
? await syncAssets(accountId, scriptName, props.assetsOptions.directory)
? await syncAssets(
accountId,
props.assetsOptions.directory,
scriptName,
props.dispatchNamespace
)
: undefined;

const legacyAssets = await syncLegacyAssets(
Expand Down
7 changes: 6 additions & 1 deletion packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,12 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
// Upload assets if assets is being used
const assetsJwt =
props.assetsOptions && !props.dryRun
? await syncAssets(accountId, scriptName, props.assetsOptions.directory)
? await syncAssets(
accountId,
props.assetsOptions.directory,
scriptName,
undefined
)
: undefined;

const bindings = getBindings({
Expand Down

0 comments on commit 6caebe4

Please sign in to comment.