-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.ts
30 lines (29 loc) · 944 Bytes
/
load.ts
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
import { readBig, readSmall } from "./utils.ts";
import { getBytes } from "./getBytes.ts";
import type { bundle, log } from "./types.ts";
import { human } from "./size.ts";
export const load = async (
input: Deno.Reader,
log: log = () => {},
): Promise<bundle> => {
const isFile = (await readSmall(input)) === 0;
if (isFile) {
const length = Number(await readBig(input));
log("[load] file with size", human(length));
const data = await getBytes(length, input);
return data;
} else {
const entries = Number(await readBig(input));
log("[load] dir with", entries, "entries");
const res: { [k: string]: bundle } = {};
for (let i = 0; i < entries; i++) {
const nameLength = await readSmall(input);
const name = new TextDecoder().decode(
await getBytes(nameLength, input),
);
log("[load] entry:", name);
res[name] = await load(input, log);
}
return res;
}
};