Files
blitzkit/packages/vfs/src/zip.ts
T

54 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-11-29 14:34:06 -06:00
import { HTTPRangeReader, unzip, ZipEntry } from "unzipit";
import { AbstractVFS } from "./abstract";
export class ZipVFS extends AbstractVFS {
2025-11-29 15:16:09 -06:00
entries = new Map<string, ZipEntry>();
2025-11-29 14:34:06 -06:00
2026-04-01 21:34:44 -05:00
constructor(
private url: string,
private root = "app/",
) {
2025-11-29 14:34:06 -06:00
super();
}
2026-08-30 14:36:51 -05:00
async _init() {
2025-11-29 14:34:06 -06:00
const reader = new HTTPRangeReader(this.url);
const { entries } = await unzip(reader);
for (const entry of Object.values(entries)) {
2026-08-30 14:46:14 -05:00
const normalized = this.normalizePath!(
entry.name.slice(this.root.length),
);
2025-11-29 15:16:09 -06:00
this.entries.set(normalized, entry);
2025-11-29 14:34:06 -06:00
}
return this;
}
2025-11-29 15:16:09 -06:00
2026-04-01 21:34:44 -05:00
async has(path: string) {
2025-11-29 15:22:13 -06:00
return this.entries.has(path);
2025-11-29 15:16:09 -06:00
}
async raw(path: string) {
const arrayBuffer = await this.entries.get(path)!.arrayBuffer();
return new Uint8Array(arrayBuffer);
}
2026-08-11 09:45:17 -05:00
async _dir(path: string) {
2026-04-01 21:34:44 -05:00
const parentSegments = path.split("/").length;
const children = new Set<string>();
for (const child of this.entries.keys()) {
if (!child.startsWith(path) || child === path) continue;
const childSegments = child.split("/");
const nextSegment = childSegments[parentSegments];
children.add(nextSegment);
}
return Array.from(children);
2025-11-29 15:16:09 -06:00
}
dispose() {}
2025-11-29 14:34:06 -06:00
}