From d5edfa8f892ed571832860478c43fe58b5c46cff Mon Sep 17 00:00:00 2001 From: Jonas Rudloff Date: Wed, 29 Oct 2025 21:30:20 +0100 Subject: [PATCH 1/2] Chunk resources.tar into 50MB chunks for jsDelivr --- bin/pack-resources.js | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/bin/pack-resources.js b/bin/pack-resources.js index 00287c0..8c7c17b 100644 --- a/bin/pack-resources.js +++ b/bin/pack-resources.js @@ -67,9 +67,20 @@ for (const dirent of await readdir(wasmDirectory, { withFileTypes: true })) { output += `\ }); -const filesystem = async (fetch) => ({ +const filesystem = async (fetch) => { + var chunks = []; `; +function chunks(data, length) { + var rest = data; + var chunks = []; + while(rest.length != 0) { + chunks.push(rest.subarray(0, length)); + rest = rest.subarray(length); + } + return chunks; +} + if (shareDirectory !== undefined) { const tarEntries = []; async function archivePath(pathName) { @@ -89,19 +100,25 @@ if (shareDirectory !== undefined) { } await archivePath(shareDirectory); const tarData = createTar(tarEntries); - const tarFilePath = resourceFilePath.replace(/\.js$/, '.tar'); - await writeFile(tarFilePath, tarData); - - const tarFileName = tarFilePath.replace(/^.+\//, ''); + var i = 0; + for(const chunk of chunks(tarData, 50*1024*1024)) { + const tarFileChunkPath = resourceFilePath.replace(/\.js$/, `.${i}.tar`); + await writeFile(tarFileChunkPath, chunk); + const tarFileChunkName = tarFileChunkPath.replace(/^.+\//, ''); + output += `\ + chunks.push(await ${await fetchExpr(tarFileChunkPath, `./${tarFileChunkName}`)}.then((resp) => resp.arrayBuffer())); +` + i += 1; + } output += `\ - ${JSON.stringify(shareRoot)}: await ${await fetchExpr(tarFilePath, `./${tarFileName}`)} - .then((resp) => resp.arrayBuffer()) - .then(unpackTarFilesystem) + return { + ${JSON.stringify(shareRoot)}: await new Blob(chunks).arrayBuffer().then(unpackTarFilesystem) + }; `; } output += `\ -}); +}; const totalSize = ${totalSize}; From db4614e14ec39e50a58b0477fcd0e9efd5e73bfe Mon Sep 17 00:00:00 2001 From: Jonas Rudloff Date: Wed, 29 Oct 2025 21:41:32 +0100 Subject: [PATCH 2/2] Rewrite chunks as a generator Co-authored-by: Catherine --- bin/pack-resources.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/bin/pack-resources.js b/bin/pack-resources.js index 8c7c17b..7284abc 100644 --- a/bin/pack-resources.js +++ b/bin/pack-resources.js @@ -71,14 +71,11 @@ const filesystem = async (fetch) => { var chunks = []; `; -function chunks(data, length) { - var rest = data; - var chunks = []; - while(rest.length != 0) { - chunks.push(rest.subarray(0, length)); - rest = rest.subarray(length); +function* chunks(data, length) { + while (data.length != 0) { + yield data.subarray(0, length); + data = data.subarray(length); } - return chunks; } if (shareDirectory !== undefined) {