Skip to content

Commit 4d02710

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 (>25Mb). But base64 takes up more space than a normal file, so this was too aggressive.
1 parent 9260bdc commit 4d02710

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

.changeset/silly-students-live.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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 (>25Mb). But base64 takes up more space than a normal file, so this was too aggressive.

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

+14-7
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 25Mb 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);
@@ -395,10 +402,10 @@ describe("publish", () => {
395402
`"uploading assets/large-file.txt..."`
396403
);
397404
expect(stderr).toMatchInlineSnapshot(
398-
`"File assets/large-file.txt is too big, it should be under 25 mb."`
405+
`"File assets/too-large-file.txt is too big, it should be under 25 mb."`
399406
);
400407
expect(error).toMatchInlineSnapshot(
401-
`[Error: File assets/large-file.txt is too big, it should be under 25 mb.]`
408+
`[Error: File assets/too-large-file.txt is too big, it should be under 25 mb.]`
402409
);
403410
});
404411
});

packages/wrangler/src/sites.tsx

+11-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 { AssetPaths } from "./config";
@@ -124,14 +124,14 @@ export async function syncAssets(
124124
if (exclude(relativePath)) {
125125
continue;
126126
}
127+
128+
await validateAssetSize(file);
129+
127130
const { assetKey } = await hashAsset(file);
128131
// now put each of the files into kv
129132
if (!keys.has(assetKey)) {
130133
console.log(`uploading ${file}...`);
131134
const content = await readFile(file, "base64");
132-
if (content.length > 25 * 1024 * 1024) {
133-
throw new Error(`File ${file} is too big, it should be under 25 mb.`);
134-
}
135135
upload.push({
136136
key: assetKey,
137137
value: content,
@@ -155,3 +155,10 @@ function createPatternMatcher(
155155
return (filePath) => ignorer.test(filePath).ignored;
156156
}
157157
}
158+
159+
async function validateAssetSize(filePath: string) {
160+
const { size } = await stat(filePath);
161+
if (size > 25 * 1024 * 1024) {
162+
throw new Error(`File ${filePath} is too big, it should be under 25 mb.`);
163+
}
164+
}

0 commit comments

Comments
 (0)