Skip to content

Commit 5fc0aad

Browse files
kokjowhitequark
andauthored
Chunk resources.tar into 50MB chunks for jsDelivr.
Co-authored-by: Catherine <[email protected]>
1 parent 9090b35 commit 5fc0aad

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

bin/pack-resources.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@ for (const dirent of await readdir(wasmDirectory, { withFileTypes: true })) {
6767
output += `\
6868
});
6969
70-
const filesystem = async (fetch) => ({
70+
const filesystem = async (fetch) => {
71+
var chunks = [];
7172
`;
7273

74+
function* chunks(data, length) {
75+
while (data.length != 0) {
76+
yield data.subarray(0, length);
77+
data = data.subarray(length);
78+
}
79+
}
80+
7381
if (shareDirectory !== undefined) {
7482
const tarEntries = [];
7583
async function archivePath(pathName) {
@@ -89,19 +97,25 @@ if (shareDirectory !== undefined) {
8997
}
9098
await archivePath(shareDirectory);
9199
const tarData = createTar(tarEntries);
92-
const tarFilePath = resourceFilePath.replace(/\.js$/, '.tar');
93-
await writeFile(tarFilePath, tarData);
94-
95-
const tarFileName = tarFilePath.replace(/^.+\//, '');
100+
var i = 0;
101+
for(const chunk of chunks(tarData, 50*1024*1024)) {
102+
const tarFileChunkPath = resourceFilePath.replace(/\.js$/, `.${i}.tar`);
103+
await writeFile(tarFileChunkPath, chunk);
104+
const tarFileChunkName = tarFileChunkPath.replace(/^.+\//, '');
105+
output += `\
106+
chunks.push(await ${await fetchExpr(tarFileChunkPath, `./${tarFileChunkName}`)}.then((resp) => resp.arrayBuffer()));
107+
`
108+
i += 1;
109+
}
96110
output += `\
97-
${JSON.stringify(shareRoot)}: await ${await fetchExpr(tarFilePath, `./${tarFileName}`)}
98-
.then((resp) => resp.arrayBuffer())
99-
.then(unpackTarFilesystem)
111+
return {
112+
${JSON.stringify(shareRoot)}: await new Blob(chunks).arrayBuffer().then(unpackTarFilesystem)
113+
};
100114
`;
101115
}
102116

103117
output += `\
104-
});
118+
};
105119
106120
const totalSize = ${totalSize};
107121

0 commit comments

Comments
 (0)