@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 56 lines 2.04 KB
Raw
1 import { Buffer } from "node:buffer";
2 import * as Effect from "effect/Effect";
3 import type { Zippable } from "fflate";
4
5 export interface ZipFile {
6 path: string;
7 content: string | Uint8Array<ArrayBufferLike>;
8 /**
9 * Unix file mode to record in the archive entry (e.g. `0o755` to keep an
10 * executable bit). Omit to use the archiver's default.
11 */
12 mode?: number;
13 }
14
15 // ZIP timestamps use local calendar fields; local midnight keeps the encoded
16 // date identical in every timezone, including those west of UTC.
17 const archiveDate = new Date(1980, 0, 1);
18
19 export const zipCode = Effect.fn(function* (
20 content: string | Uint8Array<ArrayBufferLike>,
21 files?: ReadonlyArray<ZipFile>,
22 ) {
23 return yield* zipFiles([{ path: "index.mjs", content }, ...(files ?? [])]);
24 });
25
26 /**
27 * Package `files` into a deterministic zip archive: entries are sorted by
28 * path and stamped with a fixed timestamp so identical inputs always produce
29 * identical bytes.
30 */
31 export const zipFiles = Effect.fn(function* (files: ReadonlyArray<ZipFile>) {
32 const { zipSync, strToU8 } = yield* Effect.promise(() => import("fflate"));
33 const entries: Zippable = Object.create(null);
34 for (const file of [...files].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0))) {
35 entries[file.path] = [
36 typeof file.content === "string" ? strToU8(file.content) : file.content,
37 { attrs: (file.mode ?? 0o100644) << 16 },
38 ];
39 }
40 return Buffer.from(zipSync(entries, { mtime: archiveDate, os: 3 }));
41 });
42
43 /**
44 * Expand a zip archive into its file entries. Directory entries are dropped,
45 * so every returned path addresses real content.
46 */
47 export const unzipFiles = Effect.fn(function* (archive: Uint8Array<ArrayBufferLike>) {
48 const { unzipSync } = yield* Effect.promise(() => import("fflate"));
49 const entries = yield* Effect.try(() => unzipSync(archive));
50 const files: Record<string, Uint8Array<ArrayBufferLike>> = Object.create(null);
51 for (const [path, content] of Object.entries(entries)) {
52 if (path.endsWith("/")) continue;
53 files[path] = content;
54 }
55 return files;
56 });