Skip to content

Commit 26b91d4

Browse files
fix: check actual asset file size, not base64 encoded size
Previously we were checking whether the base64 encoded size of an asset was too large (>25MiB). But base64 takes up more space than a normal file, so this was too aggressive.
1 parent a8ce57d commit 26b91d4

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

.changeset/silly-students-live.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: check actual asset file size, not base64 encoded size
6+
7+
Previously we were checking whether the base64 encoded size of an asset was too large (>25MiB).
8+
But base64 takes up more space than a normal file, so this was too aggressive.

packages/wrangler/src/__tests__/publish.test.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,24 @@ describe("publish", () => {
373373
});
374374

375375
it("should error if the asset is over 25Mb", async () => {
376-
const veryLargeAsset = {
377-
filePath: "large-file.txt",
378-
content: "X".repeat(25 * 1024 * 1024 + 1),
379-
};
376+
const assets = [
377+
{
378+
filePath: "large-file.txt",
379+
// This file is greater than 25MiB when base64 encoded but small enough to be uploaded.
380+
content: "X".repeat(25 * 1024 * 1024 * 0.8 + 1),
381+
},
382+
{
383+
filePath: "too-large-file.txt",
384+
content: "X".repeat(25 * 1024 * 1024 + 1),
385+
},
386+
];
380387
const kvNamespace = {
381388
title: "__test-name_sites_assets",
382389
id: "__test-name_sites_assets-id",
383390
};
384391
writeWranglerToml("./index.js", "./assets", undefined, ["file-1.txt"]);
385392
writeEsmWorkerSource();
386-
writeAssets("./assets", [veryLargeAsset]);
393+
writeAssets("./assets", assets);
387394
mockUploadWorkerRequest();
388395
mockSubDomainRequest();
389396
mockListKVNamespacesRequest(kvNamespace);
@@ -394,11 +401,14 @@ describe("publish", () => {
394401
expect(stdout).toMatchInlineSnapshot(
395402
`"uploading assets/large-file.txt..."`
396403
);
397-
expect(stderr).toMatchInlineSnapshot(
398-
`"File assets/large-file.txt is too big, it should be under 25 mb."`
399-
);
404+
expect(stderr).toMatchInlineSnapshot(`
405+
"File assets/too-large-file.txt is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits
406+
407+
%s
408+
If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
409+
`);
400410
expect(error).toMatchInlineSnapshot(
401-
`[Error: File assets/large-file.txt is too big, it should be under 25 mb.]`
411+
`[Error: File assets/too-large-file.txt is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits]`
402412
);
403413
});
404414
});

packages/wrangler/src/sites.tsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import crypto from "node:crypto";
22
import { createReadStream } from "node:fs";
33
import * as path from "node:path";
4-
import { readdir, readFile } from "node:fs/promises";
4+
import { readdir, readFile, stat } from "node:fs/promises";
55
import ignore from "ignore";
66
import { fetchResult } from "./cfetch";
77
import type { Config } from "./config";
@@ -127,14 +127,14 @@ export async function syncAssets(
127127
if (exclude(relativePath)) {
128128
continue;
129129
}
130+
131+
await validateAssetSize(file);
132+
130133
const { assetKey } = await hashAsset(file);
131134
// now put each of the files into kv
132135
if (!keys.has(assetKey)) {
133136
console.log(`uploading ${file}...`);
134137
const content = await readFile(file, "base64");
135-
if (content.length > 25 * 1024 * 1024) {
136-
throw new Error(`File ${file} is too big, it should be under 25 mb.`);
137-
}
138138
upload.push({
139139
key: assetKey,
140140
value: content,
@@ -159,6 +159,15 @@ function createPatternMatcher(
159159
}
160160
}
161161

162+
async function validateAssetSize(filePath: string) {
163+
const { size } = await stat(filePath);
164+
if (size > 25 * 1024 * 1024) {
165+
throw new Error(
166+
`File ${filePath} is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits`
167+
);
168+
}
169+
}
170+
162171
/**
163172
* Information about the assets that should be uploaded
164173
*/

0 commit comments

Comments
 (0)