Skip to content

Commit 780a21c

Browse files
authored
fix: disable compression for worker version requests (#1297)
1 parent 127942d commit 780a21c

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

.changeset/quiet-pianos-smile.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: disable response compression for skew protection API requests
6+
7+
Avoid truncated compressed Cloudflare API responses causing worker version lookups to fail during deployment.

packages/cloudflare/src/cli/commands/skew-protection.spec.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
1-
import { describe, expect, test, vi } from "vitest";
1+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
22

33
import { CURRENT_VERSION_ID } from "../templates/skew-protection.js";
4-
import { listWorkerVersions, updateDeploymentMapping } from "./skew-protection.js";
4+
import { getDeploymentMapping, listWorkerVersions, updateDeploymentMapping } from "./skew-protection.js";
5+
6+
const { MockCloudflare } = vi.hoisted(() => {
7+
class MockCloudflare {
8+
workers = {
9+
scripts: {
10+
versions: {
11+
list: vi.fn(() => []),
12+
},
13+
},
14+
};
15+
}
16+
17+
return { MockCloudflare: vi.fn(MockCloudflare) };
18+
});
19+
20+
vi.mock("@opennextjs/aws/adapters/config/util.js", () => ({
21+
loadConfig: vi.fn(() => ({ deploymentId: "deployment-id" })),
22+
}));
23+
24+
vi.mock("cloudflare", async (importOriginal) => {
25+
const original = await importOriginal<typeof import("cloudflare")>();
26+
return { ...original, Cloudflare: MockCloudflare };
27+
});
528

629
describe("skew protection", () => {
30+
describe("getDeploymentMapping", () => {
31+
beforeEach(() => {
32+
vi.stubEnv("CF_WORKER_NAME", "worker-name");
33+
vi.stubEnv("CF_PREVIEW_DOMAIN", "example.workers.dev");
34+
vi.stubEnv("CF_WORKERS_SCRIPTS_API_TOKEN", "test-token");
35+
vi.stubEnv("CF_ACCOUNT_ID", "test-account-id");
36+
});
37+
38+
afterEach(() => {
39+
vi.unstubAllEnvs();
40+
});
41+
42+
test("disables response compression for worker version requests", async () => {
43+
await expect(
44+
getDeploymentMapping(
45+
{ appBuildOutputPath: "/tmp/app" } as never,
46+
{ cloudflare: { skewProtection: { enabled: true } } },
47+
{}
48+
)
49+
).resolves.toEqual({ "deployment-id": CURRENT_VERSION_ID });
50+
51+
expect(MockCloudflare).toHaveBeenCalledWith({
52+
apiToken: "test-token",
53+
defaultHeaders: { "Accept-Encoding": "identity" },
54+
});
55+
});
56+
});
57+
758
describe("listWorkerVersions", () => {
859
test("listWorkerVersions return versions ordered by time DESC", async () => {
960
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/cloudflare/src/cli/commands/skew-protection.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ export async function getDeploymentMapping(
9696
const apiToken = envVars.CF_WORKERS_SCRIPTS_API_TOKEN!;
9797
const accountId = envVars.CF_ACCOUNT_ID!;
9898

99-
const client = new Cloudflare({ apiToken });
99+
const client = new Cloudflare({
100+
apiToken,
101+
defaultHeaders: { "Accept-Encoding": "identity" },
102+
});
100103
const scriptName = envVars.CF_WORKER_NAME!;
101104

102105
const deployedVersions = await listWorkerVersions(scriptName, {

0 commit comments

Comments
 (0)