Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

zipArchive.ts

115 lines3.6 KB

1import * as Effect from "effect/Effect";
2import type * as NodeStream from "node:stream";
3import * as Yauzl from "yauzl";
4​
5/**
6 * Opens a local, already verified ZIP for lazy, one-entry-at-a-time reading.
7 * `makeError` turns each failure into the caller's own error type. Close the
8 * scope to close the archive.
9 */
10export const openZipArchive = Effect.fn("zipArchive.open")(function* <E>(
11 archivePath: string,
12 makeError: (detail: string, cause: unknown) => E,
13) {
14 const opened = yield* Effect.acquireRelease(
15 Effect.callback<
16 {
17 readonly zip: Yauzl.ZipFile;
18 readonly error: () => E | undefined;
19 readonly close: Effect.Effect<void>;
20 },
21 E
22 >((resume) => {
23 Yauzl.open(
24 archivePath,
25 { lazyEntries: true, autoClose: false, validateEntrySizes: true, strictFileNames: true },
26 (error, zip) => {
27 if (error || !zip) {
28 resume(Effect.fail(makeError("Could not open the verified archive.", error)));
29 return;
30 }
31 let closed = false;
32 let archiveError: E | undefined;
33 zip.on("close", () => {
34 closed = true;
35 });
36 zip.on("error", (cause: unknown) => {
37 archiveError = makeError("The archive could not be read.", cause);
38 });
39 resume(
40 Effect.succeed({
41 zip,
42 error: () => archiveError,
43 close: Effect.callback<void>((finish) => {
44 if (closed) {
45 finish(Effect.void);
46 return;
47 }
48 const onClose = () => {
49 zip.removeListener("error", onError);
50 finish(Effect.void);
51 };
52 const onError = (cause: unknown) => {
53 zip.removeListener("close", onClose);
54 finish(Effect.die(makeError("Could not close the archive.", cause)));
55 };
56 zip.once("close", onClose);
57 zip.once("error", onError);
58 zip.close();
59 }),
60 }),
61 );
62 },
63 );
64 }),
65 (opened) => opened.close,
66 );
67​
68 const next = Effect.callback<Yauzl.Entry | null, E>((resume) => {
69 const existingError = opened.error();
70 if (existingError) {
71 resume(Effect.fail(existingError));
72 return;
73 }
74 const cleanup = () => {
75 opened.zip.removeListener("entry", onEntry);
76 opened.zip.removeListener("end", onEnd);
77 opened.zip.removeListener("error", onError);
78 };
79 const onEntry = (entry: Yauzl.Entry) => {
80 cleanup();
81 resume(Effect.succeed(entry));
82 };
83 const onEnd = () => {
84 cleanup();
85 resume(Effect.succeed(null));
86 };
87 const onError = (cause: unknown) => {
88 cleanup();
89 resume(Effect.fail(makeError("The archive could not be read.", cause)));
90 };
91 opened.zip.once("entry", onEntry);
92 opened.zip.once("end", onEnd);
93 opened.zip.once("error", onError);
94 opened.zip.readEntry();
95 return Effect.sync(cleanup);
96 });
97​
98 const streamEntry = (entry: Yauzl.Entry) =>
99 Effect.acquireRelease(
100 Effect.callback<NodeStream.Readable, E>((resume) => {
101 opened.zip.openReadStream(entry, (cause, readable) => {
102 resume(
103 cause || !readable
104 ? Effect.fail(makeError("Could not read an archive member.", cause))
105 : Effect.succeed(readable),
106 );
107 });
108 }),
109 (readable) =>
110 Effect.sync(() => {
111 readable.destroy();
112 }),
113 );
114 return { entryCount: opened.zip.entryCount, next, streamEntry };
115});