-
Notifications
You must be signed in to change notification settings - Fork 22
/
zipfs.js
50 lines (44 loc) · 1.18 KB
/
zipfs.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
42
43
44
45
46
47
48
49
50
const { join } = require("path");
const { ZipOpenFS, PosixFS } = require("@yarnpkg/fslib");
const { getLibzipSync } = require("@yarnpkg/libzip");
const zipfs = new PosixFS(
new ZipOpenFS({
libzip: getLibzipSync(),
useCache: true,
maxOpenFiles: 80,
}),
);
async function createReader(filePath) {
const fd = await zipfs.openPromise(filePath, "r");
return {
async read(p) {
const n = await zipfs.readPromise(fd, p, 0);
if (isNaN(n) || n == 0) {
await zipfs.closePromise(fd);
return null;
}
return n;
},
};
}
exports.createReader = createReader;
async function exists(filePath) {
return await zipfs.existsPromise(filePath);
}
exports.exists = exists;
async function readFile(filePath) {
return await zipfs.readFilePromise(filePath);
}
exports.readFile = readFile;
async function readTextFile(filePath) {
return await zipfs.readFilePromise(filePath, "utf8");
}
exports.readTextFile = readTextFile;
async function* walk(dir) {
for await (const d of await zipfs.opendirPromise(dir)) {
const entry = join(dir, d.name);
if (d.isDirectory()) yield* walk(entry);
if (d.isFile()) yield entry;
}
}
exports.walk = walk;