Skip to content

Commit d5edfa8

Browse files
committed
Chunk resources.tar into 50MB chunks for jsDelivr
1 parent 9090b35 commit d5edfa8

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

bin/pack-resources.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,20 @@ 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+
var rest = data;
76+
var chunks = [];
77+
while(rest.length != 0) {
78+
chunks.push(rest.subarray(0, length));
79+
rest = rest.subarray(length);
80+
}
81+
return chunks;
82+
}
83+
7384
if (shareDirectory !== undefined) {
7485
const tarEntries = [];
7586
async function archivePath(pathName) {
@@ -89,19 +100,25 @@ if (shareDirectory !== undefined) {
89100
}
90101
await archivePath(shareDirectory);
91102
const tarData = createTar(tarEntries);
92-
const tarFilePath = resourceFilePath.replace(/\.js$/, '.tar');
93-
await writeFile(tarFilePath, tarData);
94-
95-
const tarFileName = tarFilePath.replace(/^.+\//, '');
103+
var i = 0;
104+
for(const chunk of chunks(tarData, 50*1024*1024)) {
105+
const tarFileChunkPath = resourceFilePath.replace(/\.js$/, `.${i}.tar`);
106+
await writeFile(tarFileChunkPath, chunk);
107+
const tarFileChunkName = tarFileChunkPath.replace(/^.+\//, '');
108+
output += `\
109+
chunks.push(await ${await fetchExpr(tarFileChunkPath, `./${tarFileChunkName}`)}.then((resp) => resp.arrayBuffer()));
110+
`
111+
i += 1;
112+
}
96113
output += `\
97-
${JSON.stringify(shareRoot)}: await ${await fetchExpr(tarFilePath, `./${tarFileName}`)}
98-
.then((resp) => resp.arrayBuffer())
99-
.then(unpackTarFilesystem)
114+
return {
115+
${JSON.stringify(shareRoot)}: await new Blob(chunks).arrayBuffer().then(unpackTarFilesystem)
116+
};
100117
`;
101118
}
102119

103120
output += `\
104-
});
121+
};
105122
106123
const totalSize = ${totalSize};
107124

0 commit comments

Comments
 (0)