@mostra/runtime 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +39 -2
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +236 -0
- package/dist/bin.js.map +1 -0
- package/dist/builder/child/main.d.ts +1 -0
- package/dist/builder/child/main.js +1408 -0
- package/dist/builder/child/main.js.map +1 -0
- package/dist/child-xrHmytks.js +49 -0
- package/dist/child-xrHmytks.js.map +1 -0
- package/dist/deps/child/install.d.ts +1 -0
- package/dist/deps/child/install.js +33 -0
- package/dist/deps/child/install.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/instance-D7_UVdoN.js +15752 -0
- package/dist/instance-D7_UVdoN.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -3
- package/src/actions/index.ts +622 -0
- package/src/addressing/index.ts +281 -0
- package/src/bin.ts +240 -0
- package/src/builder/child/dev.ts +383 -0
- package/src/builder/child/fields.ts +363 -0
- package/src/builder/child/main.ts +47 -0
- package/src/builder/child/plugins.ts +195 -0
- package/src/builder/child/scan.ts +459 -0
- package/src/builder/child/spa.ts +348 -0
- package/src/builder/coverage.ts +72 -0
- package/src/builder/index.ts +406 -0
- package/src/builder/types.ts +180 -0
- package/src/deps/child/install.ts +30 -0
- package/src/deps/index.ts +918 -0
- package/src/deps/installer.ts +321 -0
- package/src/deps/lock.ts +82 -0
- package/src/deps/scan.ts +266 -0
- package/src/deps/specifier.ts +64 -0
- package/src/deps/warm.ts +57 -0
- package/src/dev/index.ts +518 -0
- package/src/faults.ts +65 -0
- package/src/gate.ts +55 -0
- package/src/gateway/actions.ts +268 -0
- package/src/gateway/browser.ts +650 -0
- package/src/gateway/capabilities.ts +739 -0
- package/src/gateway/controls.ts +91 -0
- package/src/gateway/dev.ts +119 -0
- package/src/gateway/http.ts +229 -0
- package/src/gateway/index.ts +1209 -0
- package/src/gateway/integrity.ts +117 -0
- package/src/gateway/listener.ts +217 -0
- package/src/gateway/routes.ts +197 -0
- package/src/gateway/serve.ts +138 -0
- package/src/gc/index.ts +451 -0
- package/src/home/acquire.ts +276 -0
- package/src/home/index.ts +16 -0
- package/src/home/paths.ts +192 -0
- package/src/home/platform.ts +34 -0
- package/src/home/record.ts +76 -0
- package/src/home/settings.ts +113 -0
- package/src/index.ts +30 -0
- package/src/instance/index.ts +942 -0
- package/src/interaction/ephemeral.ts +300 -0
- package/src/interaction/events.ts +355 -0
- package/src/interaction/fields.ts +392 -0
- package/src/interaction/index.ts +1009 -0
- package/src/interaction/outbox.ts +493 -0
- package/src/interaction/policy.ts +26 -0
- package/src/interaction/results.ts +341 -0
- package/src/interaction/runs.ts +313 -0
- package/src/interaction/signals.ts +120 -0
- package/src/interaction/submissions.ts +585 -0
- package/src/interaction/subscriptions.ts +258 -0
- package/src/interaction/viewers.ts +173 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +583 -0
- package/src/ledger/requests.ts +105 -0
- package/src/ledger/types.ts +28 -0
- package/src/live/index.ts +573 -0
- package/src/log.ts +316 -0
- package/src/processes/child.ts +67 -0
- package/src/processes/environment.ts +64 -0
- package/src/processes/identity.ts +191 -0
- package/src/processes/index.ts +19 -0
- package/src/processes/manager.ts +543 -0
- package/src/protocol-server/client.ts +94 -0
- package/src/protocol-server/index.ts +91 -0
- package/src/protocol-server/server.ts +509 -0
- package/src/protocol-server/streams.ts +81 -0
- package/src/publish/checks.ts +92 -0
- package/src/publish/digest.ts +87 -0
- package/src/publish/index.ts +23 -0
- package/src/publish/manifest.ts +409 -0
- package/src/publish/methods.ts +96 -0
- package/src/publish/publisher.ts +913 -0
- package/src/publish/source.ts +272 -0
- package/src/publish/tar.ts +110 -0
- package/src/registry/index.ts +481 -0
- package/src/registry/store.ts +136 -0
- package/src/secrets/index.ts +223 -0
- package/src/service/idle.ts +64 -0
- package/src/service/index.ts +3 -0
- package/src/service/unit.ts +250 -0
- package/src/verify/fields.ts +125 -0
- package/src/verify/index.ts +913 -0
- package/src/verify/regression.ts +157 -0
- package/src/verify/runner.ts +230 -0
- package/src/verify/sandbox.ts +407 -0
- package/src/verify/typecheck.ts +371 -0
- package/src/workers/index.ts +17 -0
- package/src/workers/launcher.ts +260 -0
- package/src/workers/pool.ts +708 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Artifact sources (INTERFACES §2.1, DATA_MODEL §2): finding an artifact's entry, listing its files
|
|
3
|
+
* with Mostra's ignore rules, and copying them into a staging snapshot whose digest is computed from
|
|
4
|
+
* the copy, so what is built and recorded is exactly what was snapshotted, whatever the author
|
|
5
|
+
* edits meanwhile.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { createReadStream, constants as fsConstants } from "node:fs";
|
|
10
|
+
import {
|
|
11
|
+
access,
|
|
12
|
+
chmod,
|
|
13
|
+
copyFile,
|
|
14
|
+
mkdir,
|
|
15
|
+
readdir,
|
|
16
|
+
readFile,
|
|
17
|
+
realpath,
|
|
18
|
+
stat,
|
|
19
|
+
} from "node:fs/promises";
|
|
20
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
21
|
+
import { createDiagnostic, RpcError } from "@mostra/protocol";
|
|
22
|
+
import ignore from "ignore";
|
|
23
|
+
import type { ListedFile } from "./digest.ts";
|
|
24
|
+
|
|
25
|
+
/** The entry files a directory artifact may have, in INTERFACES §2.1's order. */
|
|
26
|
+
const ENTRY_NAMES = ["index.tsx", "index.jsx", "index.mdx"] as const;
|
|
27
|
+
/** The extensions a single-file artifact may have. */
|
|
28
|
+
const SINGLE_FILE = new Set([".tsx", ".jsx", ".mdx"]);
|
|
29
|
+
/** Directories never part of a source tree (DATA_MODEL §2). */
|
|
30
|
+
const IGNORED_DIRS = new Set(["node_modules", ".git", ".mostra"]);
|
|
31
|
+
/** Editor swap and backup files, and Finder's metadata (DATA_MODEL §2). */
|
|
32
|
+
const IGNORED_FILE = /^(?:\.DS_Store|\.#.*|#.*#|.*~|.*\.sw[a-px])$/;
|
|
33
|
+
|
|
34
|
+
/** An artifact's source, resolved on disk. */
|
|
35
|
+
export interface ResolvedSource {
|
|
36
|
+
/** A single file, or a directory with an entry. */
|
|
37
|
+
readonly kind: "file" | "dir";
|
|
38
|
+
/** The canonical path an artifact without a manifest `id` is bound to (D51): the real path. */
|
|
39
|
+
readonly bindingPath: string;
|
|
40
|
+
/** The directory holding the files (the file's directory for a single file). */
|
|
41
|
+
readonly root: string;
|
|
42
|
+
/** The entry, relative to the snapshot root. */
|
|
43
|
+
readonly entry: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** One file of a source tree. */
|
|
47
|
+
export interface SourceFile {
|
|
48
|
+
/** Relative, `/`-separated, NFC path inside the snapshot. */
|
|
49
|
+
readonly path: string;
|
|
50
|
+
/** Where it is read from. */
|
|
51
|
+
readonly absolute: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function artifactError(
|
|
55
|
+
code:
|
|
56
|
+
| "MOSTRA_ARTIFACT_SOURCE_MISSING"
|
|
57
|
+
| "MOSTRA_ARTIFACT_NO_ENTRY"
|
|
58
|
+
| "MOSTRA_ARTIFACT_ENTRY_AMBIGUOUS"
|
|
59
|
+
| "MOSTRA_ARTIFACT_SOURCE_UNREADABLE",
|
|
60
|
+
message: string,
|
|
61
|
+
): RpcError {
|
|
62
|
+
return new RpcError(createDiagnostic(code, { message }));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const errno = (error: unknown): string | undefined => (error as NodeJS.ErrnoException).code;
|
|
66
|
+
|
|
67
|
+
/** Finds an artifact's entry at `path` (INTERFACES §2.1). */
|
|
68
|
+
export async function resolveSource(path: string): Promise<ResolvedSource> {
|
|
69
|
+
let real: string;
|
|
70
|
+
try {
|
|
71
|
+
real = await realpath(path);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (errno(error) === "ENOENT" || errno(error) === "ENOTDIR") {
|
|
74
|
+
throw artifactError("MOSTRA_ARTIFACT_SOURCE_MISSING", `The source ${path} doesn't exist.`);
|
|
75
|
+
}
|
|
76
|
+
throw artifactError(
|
|
77
|
+
"MOSTRA_ARTIFACT_SOURCE_UNREADABLE",
|
|
78
|
+
`The source ${path} can't be read (${errno(error) ?? "error"}).`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
const info = await stat(real);
|
|
82
|
+
if (info.isFile()) {
|
|
83
|
+
if (!SINGLE_FILE.has(extname(real))) {
|
|
84
|
+
throw artifactError(
|
|
85
|
+
"MOSTRA_ARTIFACT_NO_ENTRY",
|
|
86
|
+
`${path} isn't an artifact: a single-file artifact is a .tsx, .jsx or .mdx file.`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
kind: "file",
|
|
91
|
+
bindingPath: real,
|
|
92
|
+
root: dirname(real),
|
|
93
|
+
entry: basename(real).normalize("NFC"),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (!info.isDirectory()) {
|
|
97
|
+
throw artifactError("MOSTRA_ARTIFACT_NO_ENTRY", `${path} is neither a file nor a directory.`);
|
|
98
|
+
}
|
|
99
|
+
const names = new Set(await readdir(real));
|
|
100
|
+
const entries = ENTRY_NAMES.filter((name) => names.has(name));
|
|
101
|
+
if (entries.length === 0) {
|
|
102
|
+
throw artifactError(
|
|
103
|
+
"MOSTRA_ARTIFACT_NO_ENTRY",
|
|
104
|
+
`The directory ${path} has no entry: add index.tsx, index.jsx or index.mdx.`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
if (entries.length > 1) {
|
|
108
|
+
throw artifactError(
|
|
109
|
+
"MOSTRA_ARTIFACT_ENTRY_AMBIGUOUS",
|
|
110
|
+
`The directory ${path} has ${entries.join(" and ")}; an artifact has one entry.`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
return { kind: "dir", bindingPath: real, root: real, entry: entries[0] as string };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Lists a source's files: the single file, or every file under the directory except the ignored
|
|
118
|
+
* ones (DATA_MODEL §2) and those `.mostraignore` matches. Symbolic links are followed; a link
|
|
119
|
+
* that loops back into its own ancestry is skipped.
|
|
120
|
+
*/
|
|
121
|
+
export async function listSource(source: ResolvedSource): Promise<SourceFile[]> {
|
|
122
|
+
if (source.kind === "file") {
|
|
123
|
+
return [{ path: source.entry, absolute: source.bindingPath }];
|
|
124
|
+
}
|
|
125
|
+
const rules = ignore();
|
|
126
|
+
try {
|
|
127
|
+
rules.add(await readFile(join(source.root, ".mostraignore"), "utf8"));
|
|
128
|
+
} catch (error) {
|
|
129
|
+
if (errno(error) !== "ENOENT") throw unreadable(join(source.root, ".mostraignore"), error);
|
|
130
|
+
}
|
|
131
|
+
const files: SourceFile[] = [];
|
|
132
|
+
const seen = new Map<string, string>();
|
|
133
|
+
const walk = async (
|
|
134
|
+
dir: string,
|
|
135
|
+
relative: string,
|
|
136
|
+
ancestors: ReadonlySet<string>,
|
|
137
|
+
): Promise<void> => {
|
|
138
|
+
let entries: import("node:fs").Dirent[];
|
|
139
|
+
try {
|
|
140
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
141
|
+
} catch (error) {
|
|
142
|
+
throw unreadable(dir, error);
|
|
143
|
+
}
|
|
144
|
+
for (const entry of entries.sort((a, b) => (a.name < b.name ? -1 : 1))) {
|
|
145
|
+
const absolute = join(dir, entry.name);
|
|
146
|
+
const path = (relative ? `${relative}/${entry.name}` : entry.name).normalize("NFC");
|
|
147
|
+
let kind: "file" | "dir" | "other" = entry.isFile()
|
|
148
|
+
? "file"
|
|
149
|
+
: entry.isDirectory()
|
|
150
|
+
? "dir"
|
|
151
|
+
: "other";
|
|
152
|
+
if (entry.isSymbolicLink()) {
|
|
153
|
+
try {
|
|
154
|
+
const target = await stat(absolute);
|
|
155
|
+
kind = target.isFile() ? "file" : target.isDirectory() ? "dir" : "other";
|
|
156
|
+
} catch {
|
|
157
|
+
continue; // A dangling link has no content to snapshot.
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (kind === "dir") {
|
|
161
|
+
if (IGNORED_DIRS.has(entry.name) || rules.ignores(`${path}/`)) continue;
|
|
162
|
+
let real: string;
|
|
163
|
+
try {
|
|
164
|
+
real = await realpath(absolute);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
throw unreadable(absolute, error);
|
|
167
|
+
}
|
|
168
|
+
if (ancestors.has(real)) continue;
|
|
169
|
+
await walk(absolute, path, new Set([...ancestors, real]));
|
|
170
|
+
} else if (kind === "file") {
|
|
171
|
+
if (IGNORED_FILE.test(entry.name) || rules.ignores(path)) continue;
|
|
172
|
+
const clash = seen.get(path);
|
|
173
|
+
if (clash !== undefined) {
|
|
174
|
+
throw artifactError(
|
|
175
|
+
"MOSTRA_ARTIFACT_SOURCE_UNREADABLE",
|
|
176
|
+
`${clash} and ${absolute} have the same name after Unicode normalization (NFC).`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
seen.set(path, absolute);
|
|
180
|
+
files.push({ path, absolute });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
await walk(source.root, "", new Set([source.root]));
|
|
185
|
+
if (!files.some((file) => file.path === source.entry)) {
|
|
186
|
+
throw artifactError(
|
|
187
|
+
"MOSTRA_ARTIFACT_NO_ENTRY",
|
|
188
|
+
`${source.entry} is excluded by ${join(source.root, ".mostraignore")}; an artifact's entry can't be ignored.`,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
return files;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function unreadable(path: string, error: unknown): RpcError {
|
|
195
|
+
return artifactError(
|
|
196
|
+
"MOSTRA_ARTIFACT_SOURCE_UNREADABLE",
|
|
197
|
+
`${path} can't be read (${errno(error) ?? (error as Error).message}).`,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function hashFile(path: string): Promise<string> {
|
|
202
|
+
const hash = createHash("sha256");
|
|
203
|
+
for await (const chunk of createReadStream(path)) hash.update(chunk as Buffer);
|
|
204
|
+
return hash.digest("hex");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Copies `files` into `target` (the staging snapshot) and lists the copies with their digests.
|
|
209
|
+
* Hashing the copy, not the original, means the listing describes exactly the bytes that are
|
|
210
|
+
* built and archived.
|
|
211
|
+
*/
|
|
212
|
+
export async function copySnapshot(
|
|
213
|
+
files: readonly SourceFile[],
|
|
214
|
+
target: string,
|
|
215
|
+
): Promise<ListedFile[]> {
|
|
216
|
+
const listed: ListedFile[] = [];
|
|
217
|
+
for (const file of files) {
|
|
218
|
+
const destination = join(target, ...file.path.split("/"));
|
|
219
|
+
let info: Awaited<ReturnType<typeof stat>>;
|
|
220
|
+
try {
|
|
221
|
+
info = await stat(file.absolute);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
throw unreadable(file.absolute, error);
|
|
224
|
+
}
|
|
225
|
+
try {
|
|
226
|
+
await mkdir(dirname(destination), { recursive: true });
|
|
227
|
+
await copyFile(file.absolute, destination, fsConstants.COPYFILE_FICLONE);
|
|
228
|
+
const executable = (info.mode & 0o111) !== 0;
|
|
229
|
+
await chmod(destination, executable ? 0o755 : 0o644);
|
|
230
|
+
listed.push({ path: file.path, executable, sha256: await hashFile(destination) });
|
|
231
|
+
} catch (error) {
|
|
232
|
+
throw await snapshotError(error, file.absolute);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return listed;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Says which side of a snapshot copy failed (D80 Claude review finding 6): a full disk is the
|
|
240
|
+
* home's (`MOSTRA_HOME_DISK_FULL`, FAILURE_MODEL §3); a source the author can no longer read is
|
|
241
|
+
* the author's; anything else happened in the home's staging and stays an internal failure,
|
|
242
|
+
* logged with its cause, rather than being blamed on the author's file.
|
|
243
|
+
*/
|
|
244
|
+
export async function snapshotError(error: unknown, source: string): Promise<unknown> {
|
|
245
|
+
const code = errno(error);
|
|
246
|
+
if (code === "ENOSPC" || code === "EDQUOT") {
|
|
247
|
+
return new RpcError(
|
|
248
|
+
createDiagnostic("MOSTRA_HOME_DISK_FULL", {
|
|
249
|
+
message: `The disk holding the home is full; ${source} couldn't be snapshotted, and nothing was published.`,
|
|
250
|
+
}),
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
await access(source, fsConstants.R_OK);
|
|
255
|
+
} catch (readError) {
|
|
256
|
+
return unreadable(source, readError);
|
|
257
|
+
}
|
|
258
|
+
return error;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Reads an artifact's entry from disk, with the same diagnostics as the rest of its source. */
|
|
262
|
+
export async function readEntry(source: ResolvedSource): Promise<string> {
|
|
263
|
+
const path = source.kind === "file" ? source.bindingPath : join(source.root, source.entry);
|
|
264
|
+
try {
|
|
265
|
+
return await readFile(path, "utf8");
|
|
266
|
+
} catch (error) {
|
|
267
|
+
if (errno(error) === "ENOENT") {
|
|
268
|
+
throw artifactError("MOSTRA_ARTIFACT_SOURCE_MISSING", `The entry ${path} disappeared.`);
|
|
269
|
+
}
|
|
270
|
+
throw unreadable(path, error);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal, deterministic POSIX tar (pax interchange format) for source snapshots (DATA_MODEL §1):
|
|
3
|
+
* regular files only, in the order given, with mode 0644 or 0755, owner 0, modification time 0 and
|
|
4
|
+
* a pax `path` record for any path longer than 100 bytes or outside ASCII. The same tree always
|
|
5
|
+
* gives the same bytes. {@link readTar} reads what {@link tarFile} writes.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const BLOCK = 512;
|
|
9
|
+
|
|
10
|
+
/** One file in an archive. */
|
|
11
|
+
export interface TarFile {
|
|
12
|
+
/** Relative, `/`-separated path. */
|
|
13
|
+
readonly path: string;
|
|
14
|
+
readonly executable: boolean;
|
|
15
|
+
readonly data: Uint8Array;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function octal(value: number, width: number): string {
|
|
19
|
+
return `${value.toString(8).padStart(width - 1, "0")}\0`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function header(name: string, size: number, type: "0" | "x", mode: number): Buffer {
|
|
23
|
+
const block = Buffer.alloc(BLOCK, 0);
|
|
24
|
+
block.write(name, 0, 100, "utf8");
|
|
25
|
+
block.write(octal(mode, 8), 100, 8, "ascii");
|
|
26
|
+
block.write(octal(0, 8), 108, 8, "ascii");
|
|
27
|
+
block.write(octal(0, 8), 116, 8, "ascii");
|
|
28
|
+
block.write(octal(size, 12), 124, 12, "ascii");
|
|
29
|
+
block.write(octal(0, 12), 136, 12, "ascii");
|
|
30
|
+
block.write(" ", 148, 8, "ascii");
|
|
31
|
+
block.write(type, 156, 1, "ascii");
|
|
32
|
+
block.write("ustar\0", 257, 6, "ascii");
|
|
33
|
+
block.write("00", 263, 2, "ascii");
|
|
34
|
+
let sum = 0;
|
|
35
|
+
for (const byte of block) sum += byte;
|
|
36
|
+
block.write(`${sum.toString(8).padStart(6, "0")}\0 `, 148, 8, "ascii");
|
|
37
|
+
return block;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function padding(size: number): Buffer {
|
|
41
|
+
const rest = size % BLOCK;
|
|
42
|
+
return Buffer.alloc(rest === 0 ? 0 : BLOCK - rest, 0);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A pax record `"<length> path=<value>\n"`, where the length counts the whole record. */
|
|
46
|
+
function paxRecord(key: string, value: string): Buffer {
|
|
47
|
+
const body = ` ${key}=${value}\n`;
|
|
48
|
+
const bodyLength = Buffer.byteLength(body, "utf8");
|
|
49
|
+
let length = bodyLength + 1;
|
|
50
|
+
while (String(length).length + bodyLength !== length) length = String(length).length + bodyLength;
|
|
51
|
+
return Buffer.from(`${length}${body}`, "utf8");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The archive blocks of one file: an optional pax header, the header, the data and padding. */
|
|
55
|
+
export function tarFile(file: TarFile): Buffer[] {
|
|
56
|
+
const mode = file.executable ? 0o755 : 0o644;
|
|
57
|
+
const blocks: Buffer[] = [];
|
|
58
|
+
const plain = /^[\x20-\x7e]*$/.test(file.path) && Buffer.byteLength(file.path) <= 100;
|
|
59
|
+
if (!plain) {
|
|
60
|
+
const record = paxRecord("path", file.path);
|
|
61
|
+
blocks.push(header("PaxHeader", record.length, "x", 0o644), record, padding(record.length));
|
|
62
|
+
}
|
|
63
|
+
// Readers that ignore pax still get a printable stand-in; the pax record carries the real path.
|
|
64
|
+
const name = plain ? file.path : file.path.replace(/[^\x20-\x7e]/g, "_").slice(-100);
|
|
65
|
+
blocks.push(
|
|
66
|
+
header(name, file.data.length, "0", mode),
|
|
67
|
+
Buffer.from(file.data),
|
|
68
|
+
padding(file.data.length),
|
|
69
|
+
);
|
|
70
|
+
return blocks;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The end of an archive: two zero blocks. */
|
|
74
|
+
export const TAR_END: Buffer = Buffer.alloc(BLOCK * 2, 0);
|
|
75
|
+
|
|
76
|
+
/** Reads an archive written by {@link tarFile}. Throws on anything malformed. */
|
|
77
|
+
export function readTar(archive: Uint8Array): TarFile[] {
|
|
78
|
+
const buffer = Buffer.from(archive.buffer, archive.byteOffset, archive.byteLength);
|
|
79
|
+
const files: TarFile[] = [];
|
|
80
|
+
let offset = 0;
|
|
81
|
+
let paxPath: string | undefined;
|
|
82
|
+
while (offset + BLOCK <= buffer.length) {
|
|
83
|
+
const block = buffer.subarray(offset, offset + BLOCK);
|
|
84
|
+
if (block.every((byte) => byte === 0)) break;
|
|
85
|
+
const field = (start: number, length: number): string =>
|
|
86
|
+
block
|
|
87
|
+
.subarray(start, start + length)
|
|
88
|
+
.toString("latin1")
|
|
89
|
+
.replace(/\0.*$/s, "");
|
|
90
|
+
const size = Number.parseInt(field(124, 12), 8);
|
|
91
|
+
const type = field(156, 1);
|
|
92
|
+
if (!Number.isSafeInteger(size) || size < 0)
|
|
93
|
+
throw new Error(`Malformed tar header at ${offset}.`);
|
|
94
|
+
const dataStart = offset + BLOCK;
|
|
95
|
+
const data = buffer.subarray(dataStart, dataStart + size);
|
|
96
|
+
if (data.length !== size) throw new Error("Truncated tar archive.");
|
|
97
|
+
offset = dataStart + size + padding(size).length;
|
|
98
|
+
if (type === "x") {
|
|
99
|
+
const match = /(?:^|\n)\d+ path=([^\n]*)\n/.exec(data.toString("utf8"));
|
|
100
|
+
paxPath = match?.[1];
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (type !== "0" && type !== "") throw new Error(`Unexpected tar entry type "${type}".`);
|
|
104
|
+
const path = paxPath ?? field(0, 100);
|
|
105
|
+
paxPath = undefined;
|
|
106
|
+
const mode = Number.parseInt(field(100, 8), 8);
|
|
107
|
+
files.push({ path, executable: (mode & 0o111) !== 0, data: new Uint8Array(data) });
|
|
108
|
+
}
|
|
109
|
+
return files;
|
|
110
|
+
}
|