Skip to content

Commit

Permalink
Fix WASM modules in versions secrets (#6365)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Jul 30, 2024
1 parent a9021aa commit 13549c3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-zebras-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: WASM modules meant that `wrangler versions secret ...` could not properly update the version. This has now been fixed.
78 changes: 78 additions & 0 deletions packages/wrangler/src/__tests__/versions/secrets/put.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { http, HttpResponse } from "msw";
import { File, FormData } from "undici";
import { describe, expect, test } from "vitest";
import { mockAccountId, mockApiToken } from "../../helpers/mock-account-id";
import { mockConsoleMethods } from "../../helpers/mock-console";
import { clearDialogs, mockPrompt } from "../../helpers/mock-dialogs";
import { useMockIsTTY } from "../../helpers/mock-istty";
import { useMockStdin } from "../../helpers/mock-stdin";
import { msw } from "../../helpers/msw";
import { runInTempDir } from "../../helpers/run-in-tmp";
import { runWrangler } from "../../helpers/run-wrangler";
import { writeWranglerToml } from "../../helpers/write-wrangler-toml";
Expand Down Expand Up @@ -225,4 +228,79 @@ describe("versions secret put", () => {
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

test("can add secret on wasm worker", async () => {
setIsTTY(true);

mockSetupApiCalls();
// Mock content call to have wasm
msw.use(
http.get(
`*/accounts/:accountId/workers/scripts/:scriptName/content/v2?version=ce15c78b-cc43-4f60-b5a9-15ce4f298c2a`,
async ({ params }) => {
expect(params.accountId).toEqual("some-account-id");
expect(params.scriptName).toEqual("script-name");

const formData = new FormData();
formData.set(
"index.js",
new File(["export default {}"], "index.js", {
type: "application/javascript+module",
}),
"index.js"
);
formData.set(
"module.wasm",
new File(
[Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])],
"module.wasm",
{
type: "application/wasm",
}
),
"module.wasm"
);

return HttpResponse.formData(formData, {
headers: { "cf-entrypoint": "index.js" },
});
},
{ once: true }
)
);

mockPrompt({
text: "Enter a secret value:",
options: { isSecret: true },
result: "the-secret",
});

mockPostVersion((metadata, formData) => {
expect(formData.get("module.wasm")).not.toBeNull();
expect((formData.get("module.wasm") as File).size).equal(10);

expect(metadata.bindings).toStrictEqual([
{ type: "secret_text", name: "SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
"secret_key",
"secret_text",
]);

expect(metadata.annotations).not.toBeUndefined();
expect(
(metadata.annotations as Record<string, string>)["workers/message"]
).toBe("Deploy a new secret");
});
await runWrangler(
"versions secret put SECRET --name script-name --message 'Deploy a new secret' --x-versions"
);

expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating the secret for the Worker \\"script-name\\"
✨ Success! Created version id with secret SECRET.
➡️ To deploy this version with secret SECRET to production traffic use the command \\"wrangler versions deploy --x-versions\\"."
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
});
6 changes: 4 additions & 2 deletions packages/wrangler/src/__tests__/versions/secrets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ export function mockGetWorkerSettings() {
);
}

export function mockPostVersion(validate?: (metadata: WorkerMetadata) => void) {
export function mockPostVersion(
validate?: (metadata: WorkerMetadata, formData: FormData) => void
) {
msw.use(
http.post(
`*/accounts/:accountId/workers/scripts/:scriptName/versions`,
Expand All @@ -138,7 +140,7 @@ export function mockPostVersion(validate?: (metadata: WorkerMetadata) => void) {
formData.get("metadata") as string
) as WorkerMetadata;

validate && validate(metadata);
validate && validate(metadata, formData);

return HttpResponse.json(
createFetchResult({
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/versions/secrets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ async function parseModules(
const mainModule: CfModule = {
name: entrypointPart.name,
filePath: "",
content: await entrypointPart.text(),
content: Buffer.from(await entrypointPart.arrayBuffer()),
type: fromMimeType(entrypointPart.type),
};

Expand All @@ -288,7 +288,7 @@ async function parseModules(
({
name,
filePath: "",
content: await file.text(),
content: Buffer.from(await file.arrayBuffer()),
type: fromMimeType(file.type),
}) as CfModule
)
Expand Down

0 comments on commit 13549c3

Please sign in to comment.