-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
41 lines (38 loc) · 1.4 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { ArchiveReader, libarchiveWasm } from 'libarchive-wasm';
import { wrap } from 'minlink/dist/browser';
document.getElementById('upload').addEventListener('change', async (e) => {
const file = e.currentTarget.files[0];
await (async () => {
console.log('Extract (main)');
const data = await file.arrayBuffer();
const mod = await libarchiveWasm({
locateFile() {
return new URL('npm:libarchive-wasm/dist/libarchive.wasm', import.meta.url).toString();
},
});
const reader = new ArchiveReader(mod, new Int8Array(data));
const results = [];
for (const entry of reader.entries()) {
const result = {
pathname: entry.getPathname(),
size: entry.getSize(),
};
if (result.pathname.endsWith('.md')) {
result.data = new TextDecoder().decode(entry.readData());
}
console.log(result);
results.push(result);
}
reader.free();
document.getElementById('result-main').textContent = JSON.stringify(results, null, ' ');
})();
await (async () => {
console.log('Extract (worker)');
const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
const api = wrap(worker);
const results = await api.exec('extractAll', file);
console.log(results);
await api.terminate();
document.getElementById('result-worker').textContent = JSON.stringify(results, null, ' ');
})();
});