@mostra/sdk 0.5.0 → 0.6.1
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/README.md +1 -1
- package/dist/bundles.json +30 -18
- package/dist/index.d.ts +32 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -4
- package/dist/index.js.map +1 -1
- package/dist/{runtime-CUbnK5Za.js → runtime-NiLgW1ED.js} +532 -88
- package/dist/runtime-NiLgW1ED.js.map +1 -0
- package/dist/setup-main.js +1 -1
- package/package.json +7 -7
- package/src/bundle/cache.ts +106 -26
- package/src/bundle/extract.ts +197 -34
- package/src/bundle/links.ts +187 -0
- package/src/bundle/setup.ts +67 -8
- package/src/bundle/tree.ts +72 -8
- package/src/environment.ts +59 -0
- package/src/index.ts +1 -1
- package/src/mostra.ts +10 -2
- package/src/platform.ts +7 -6
- package/src/runtime.ts +3 -2
- package/dist/runtime-CUbnK5Za.js.map +0 -1
package/src/bundle/extract.ts
CHANGED
|
@@ -1,40 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Unpacking a downloaded archive (RT-01)
|
|
3
|
-
* accepted, all under the archive's one top directory, which is
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
2
|
+
* Unpacking a downloaded archive (RT-01) into a new, empty directory. Only regular files,
|
|
3
|
+
* directories and symbolic links are accepted, all under the archive's one top directory, which is
|
|
4
|
+
* stripped; an entry's path must be relative, with no `..`, `.` or empty part and no backslash
|
|
5
|
+
* (nor, on Windows, a colon; node-tar reads a backslash there as a separator, so its parts are
|
|
6
|
+
* judged as separate ones), and no path may appear twice. Links are made only after every file
|
|
7
|
+
* and directory is written, and after every link's own directory, so no write and no link ever
|
|
8
|
+
* goes through one. Setup then checks the whole tree
|
|
9
|
+
* against the bundle's file list (`checkTree`), which refuses any file or link it doesn't list and
|
|
10
|
+
* any link that leaves the bundle. A pinned archive's SHA-256 is checked before any of this. A
|
|
11
|
+
* Windows archive holds no links at all (D153): one that does is refused, since its junctions are
|
|
12
|
+
* made by setup, from `files.json`.
|
|
13
|
+
*
|
|
14
|
+
* The files are written synchronously as node-tar's parser reads them. node-tar's own `x` checks
|
|
15
|
+
* and clobbers what each path already holds, and on Windows writes one entry at a time; into an
|
|
16
|
+
* empty directory none of that is needed, and a first install wrote its ~50,000 files 4 to 10
|
|
17
|
+
* times slower for it: 6 minutes on Windows arm64 (#88, VERIFIED_FACTS, MT-02).
|
|
8
18
|
*/
|
|
19
|
+
import { closeSync, createReadStream, mkdirSync, openSync, symlinkSync, writeSync } from "node:fs";
|
|
20
|
+
import { dirname, join, resolve } from "node:path";
|
|
9
21
|
import { createDiagnostic, RpcError } from "@mostra/protocol";
|
|
10
|
-
import {
|
|
22
|
+
import { Parser, type ReadEntry } from "tar";
|
|
11
23
|
|
|
12
|
-
const
|
|
24
|
+
const FILES = new Set(["File", "OldFile", "ContiguousFile"]);
|
|
25
|
+
|
|
26
|
+
/** Options of {@link extractBundle}. */
|
|
27
|
+
export interface ExtractOptions {
|
|
28
|
+
/** `keep` extracts symbolic links; `refuse` refuses an archive that has any (Windows'). */
|
|
29
|
+
readonly links?: "keep" | "refuse";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** An entry a bundle's archive never has, or an archive that doesn't read as a tar at all. */
|
|
33
|
+
class ArchiveRefused extends Error {}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Whether `error` is two of the archive's entries meeting where the file system names them as one
|
|
37
|
+
* path: a file where a directory must go, a directory or file already there, names the file system
|
|
38
|
+
* folds alike (`σ` and `ς`, `ß` and `ss`). The directory is new and only this extractor writes in
|
|
39
|
+
* it, so these are never the disk's failure (the Opus review of MT-02).
|
|
40
|
+
*/
|
|
41
|
+
function collides(error: unknown): boolean {
|
|
42
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
43
|
+
return code === "EEXIST" || code === "ENOTDIR" || code === "EISDIR";
|
|
44
|
+
}
|
|
13
45
|
|
|
14
46
|
/** Extracts `archive` (a `.tar.gz` with one top directory `top`) into the empty directory `into`. */
|
|
15
|
-
export async function extractBundle(
|
|
16
|
-
|
|
47
|
+
export async function extractBundle(
|
|
48
|
+
archive: string,
|
|
49
|
+
into: string,
|
|
50
|
+
top: string,
|
|
51
|
+
options: ExtractOptions = {},
|
|
52
|
+
): Promise<void> {
|
|
17
53
|
try {
|
|
18
|
-
await
|
|
19
|
-
file: archive,
|
|
20
|
-
cwd: into,
|
|
21
|
-
strip: 1,
|
|
22
|
-
strict: true,
|
|
23
|
-
preservePaths: false,
|
|
24
|
-
preserveOwner: false,
|
|
25
|
-
// Owner-only directories are tightened later; files keep their 0644/0755 modes.
|
|
26
|
-
noChmod: false,
|
|
27
|
-
filter: (path, entry) => {
|
|
28
|
-
const type = "type" in entry ? String(entry.type) : "File";
|
|
29
|
-
const inside = path === `${top}/` || path === top || path.startsWith(`${top}/`);
|
|
30
|
-
if (!inside || !ACCEPTED.has(type)) {
|
|
31
|
-
refused ??= `${path} (${type})`;
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
return true;
|
|
35
|
-
},
|
|
36
|
-
});
|
|
54
|
+
await unpack(archive, resolve(into), top, options.links === "refuse");
|
|
37
55
|
} catch (error) {
|
|
56
|
+
if (error instanceof ArchiveRefused) {
|
|
57
|
+
throw new RpcError(createDiagnostic("MOSTRA_ENV_BUNDLE_INVALID", { message: error.message }));
|
|
58
|
+
}
|
|
38
59
|
const code = (error as NodeJS.ErrnoException).code;
|
|
39
60
|
// The file system failing (a full disk, a permission) isn't the archive's fault.
|
|
40
61
|
if (typeof code === "string" && /^E[A-Z]+$/.test(code) && code !== "EOF") {
|
|
@@ -50,11 +71,153 @@ export async function extractBundle(archive: string, into: string, top: string):
|
|
|
50
71
|
}),
|
|
51
72
|
);
|
|
52
73
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The path below `top` an entry names, as parts, or why it is refused. */
|
|
77
|
+
export function entryParts(path: string, top: string, windows: boolean): string[] | string {
|
|
78
|
+
const parts = path.split("/");
|
|
79
|
+
if (parts[0] !== top) return "it is outside the bundle's directory";
|
|
80
|
+
// A directory's name ends with a slash.
|
|
81
|
+
if (parts.length > 1 && parts.at(-1) === "") parts.pop();
|
|
82
|
+
const below = parts.slice(1);
|
|
83
|
+
for (const part of below) {
|
|
84
|
+
if (part === "" || part === "." || part === "..") return "its path isn't a plain relative one";
|
|
85
|
+
if (part.includes("\\") || (windows && part.includes(":"))) {
|
|
86
|
+
return "its path holds a character a bundle's paths never have";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return below;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function unpack(archive: string, root: string, top: string, refuseLinks: boolean) {
|
|
93
|
+
const windows = process.platform === "win32";
|
|
94
|
+
const seen = new Set<string>();
|
|
95
|
+
const made = new Set<string>([root]);
|
|
96
|
+
const links: { path: string; target: string }[] = [];
|
|
97
|
+
const ensureDir = (dir: string): void => {
|
|
98
|
+
if (made.has(dir)) return;
|
|
99
|
+
mkdirSync(dir, { recursive: true, mode: 0o755 });
|
|
100
|
+
for (let at = dir; !made.has(at); at = dirname(at)) made.add(at);
|
|
101
|
+
};
|
|
102
|
+
let failure: unknown;
|
|
103
|
+
let open: number | undefined;
|
|
104
|
+
const fail = (error: unknown): void => {
|
|
105
|
+
failure ??= error;
|
|
106
|
+
};
|
|
107
|
+
const refuse = (path: string, type: string, why: string): void =>
|
|
108
|
+
fail(
|
|
109
|
+
new ArchiveRefused(
|
|
110
|
+
`The runtime bundle's archive holds an entry a bundle never has: ${path} (${type}): ${why}.`,
|
|
111
|
+
),
|
|
58
112
|
);
|
|
113
|
+
|
|
114
|
+
const parser = new Parser({ strict: true });
|
|
115
|
+
parser.on("entry", (entry: ReadEntry) => {
|
|
116
|
+
const type = String(entry.type);
|
|
117
|
+
if (failure !== undefined) return entry.resume();
|
|
118
|
+
const parts = entryParts(entry.path, top, windows);
|
|
119
|
+
if (typeof parts === "string") {
|
|
120
|
+
refuse(entry.path, type, parts);
|
|
121
|
+
return entry.resume();
|
|
122
|
+
}
|
|
123
|
+
const path = parts.length === 0 ? root : join(root, ...parts);
|
|
124
|
+
// Windows and macOS keep one file for names that differ only in case or (macOS) in Unicode
|
|
125
|
+
// normalization, so those are the same path there.
|
|
126
|
+
const key =
|
|
127
|
+
windows || process.platform === "darwin" ? path.normalize("NFC").toLowerCase() : path;
|
|
128
|
+
if (parts.length > 0 && seen.has(key)) {
|
|
129
|
+
refuse(entry.path, type, "its path appears twice");
|
|
130
|
+
return entry.resume();
|
|
131
|
+
}
|
|
132
|
+
seen.add(key);
|
|
133
|
+
try {
|
|
134
|
+
if (type === "Directory") {
|
|
135
|
+
entry.resume();
|
|
136
|
+
ensureDir(path);
|
|
137
|
+
} else if (type === "SymbolicLink" && parts.length > 0) {
|
|
138
|
+
entry.resume();
|
|
139
|
+
if (refuseLinks) refuse(entry.path, type, "a Windows bundle's links are made by setup");
|
|
140
|
+
else if (!entry.linkpath) refuse(entry.path, type, "it links to nothing");
|
|
141
|
+
else links.push({ path, target: entry.linkpath });
|
|
142
|
+
} else if (FILES.has(type) && parts.length > 0) {
|
|
143
|
+
ensureDir(dirname(path));
|
|
144
|
+
// A new file only (`wx`): nothing that was there before, a link least of all, is written to.
|
|
145
|
+
const fd = openSync(path, "wx", (entry.mode ?? 0) & 0o111 ? 0o755 : 0o644);
|
|
146
|
+
open = fd;
|
|
147
|
+
entry.on("data", (chunk: Buffer) => {
|
|
148
|
+
if (failure !== undefined) return;
|
|
149
|
+
try {
|
|
150
|
+
let at = 0;
|
|
151
|
+
while (at < chunk.length) at += writeSync(fd, chunk, at);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
fail(error);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
entry.on("end", () => {
|
|
157
|
+
open = undefined;
|
|
158
|
+
try {
|
|
159
|
+
closeSync(fd);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
fail(error);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
} else {
|
|
165
|
+
refuse(entry.path, type, "a bundle holds only files, directories and links");
|
|
166
|
+
entry.resume();
|
|
167
|
+
}
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (collides(error))
|
|
170
|
+
refuse(entry.path, type, "its path meets another entry's, as this file system names them");
|
|
171
|
+
else fail(error);
|
|
172
|
+
entry.resume();
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
// A parser error (a damaged header, a bad gzip stream) is the archive's: it settles the wait
|
|
176
|
+
// rather than rejecting a promise nobody awaits yet.
|
|
177
|
+
const ended = new Promise<void>((done) => {
|
|
178
|
+
parser.on("end", done);
|
|
179
|
+
parser.on("error", (error: Error) => {
|
|
180
|
+
fail(new ArchiveRefused(`The runtime bundle's archive couldn't be read: ${error.message}`));
|
|
181
|
+
done();
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
try {
|
|
185
|
+
for await (const chunk of createReadStream(archive, { highWaterMark: 1024 * 1024 })) {
|
|
186
|
+
parser.write(chunk as Buffer);
|
|
187
|
+
if (failure !== undefined) break;
|
|
188
|
+
}
|
|
189
|
+
if (failure === undefined) {
|
|
190
|
+
parser.end();
|
|
191
|
+
await ended;
|
|
192
|
+
}
|
|
193
|
+
} finally {
|
|
194
|
+
if (open !== undefined) closeSync(open);
|
|
195
|
+
}
|
|
196
|
+
if (failure !== undefined) throw failure;
|
|
197
|
+
// Every link's directory first, while no link exists: a link whose path another entry lies
|
|
198
|
+
// beneath (another link included) then meets a real directory and is refused, and no directory is
|
|
199
|
+
// ever made or reached through a link (the GPT review of MT-02).
|
|
200
|
+
const refused = (path: string, why: string): ArchiveRefused =>
|
|
201
|
+
new ArchiveRefused(
|
|
202
|
+
`The runtime bundle's archive holds an entry a bundle never has: ${path} (SymbolicLink): ${why}.`,
|
|
203
|
+
);
|
|
204
|
+
for (const link of links) {
|
|
205
|
+
try {
|
|
206
|
+
ensureDir(dirname(link.path));
|
|
207
|
+
} catch (error) {
|
|
208
|
+
if (collides(error)) throw refused(link.path, "it lies beneath a file");
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
for (const link of links) {
|
|
213
|
+
try {
|
|
214
|
+
symlinkSync(link.target, link.path);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
// Its path is a directory other entries filled (the link would have been written through), or
|
|
217
|
+
// a name the file system folds with another entry's.
|
|
218
|
+
if (collides(error))
|
|
219
|
+
throw refused(link.path, "other entries are inside it, or meet its path");
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
59
222
|
}
|
|
60
223
|
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Windows bundle's links (D153). An ordinary Windows user can't create symbolic links (that
|
|
3
|
+
* takes an administrator or Developer Mode), so a Windows archive holds none: its `files.json`
|
|
4
|
+
* lists each link, every one to a directory inside the bundle, and setup makes each a junction,
|
|
5
|
+
* which any user can create. A junction names its target by absolute path, so it is made at the
|
|
6
|
+
* bundle's real path, and a bundle moved since is set up again (`readSetup` compares the root
|
|
7
|
+
* its marker names).
|
|
8
|
+
*
|
|
9
|
+
* Nothing outside the bundle is ever touched: a listed link's path and target must stay inside
|
|
10
|
+
* it, every directory above a link must be a real directory (never a link), the target must be a
|
|
11
|
+
* directory, and what already sits at a link's path is replaced only when it is itself a link, or
|
|
12
|
+
* a real directory holding exactly the files and bytes of the link's target. That is what a copy
|
|
13
|
+
* made by a tool that follows junctions leaves (Explorer, `Copy-Item`, `robocopy` without `/SJ`,
|
|
14
|
+
* and a move to another drive, which is a copy), so such a copy is set up again; a directory
|
|
15
|
+
* holding anything else is refused and kept (Opus review of WP-W3, finding 4).
|
|
16
|
+
*
|
|
17
|
+
* Two setups of one bundle may run at once (a moved bundle met by two commands): each step
|
|
18
|
+
* tolerates the other's, and a link counts as made once it names its target, whoever made it
|
|
19
|
+
* (finding 5).
|
|
20
|
+
*/
|
|
21
|
+
import {
|
|
22
|
+
lstat,
|
|
23
|
+
mkdir,
|
|
24
|
+
readdir,
|
|
25
|
+
readlink,
|
|
26
|
+
rm,
|
|
27
|
+
rmdir,
|
|
28
|
+
stat,
|
|
29
|
+
symlink,
|
|
30
|
+
unlink,
|
|
31
|
+
} from "node:fs/promises";
|
|
32
|
+
import { dirname, join, resolve } from "node:path";
|
|
33
|
+
import type { BundleFiles, BundleLinkEntry } from "@mostra/protocol";
|
|
34
|
+
import { hashFile, linkStaysInside, linkTarget, realOrNone, safeRelative } from "./tree.ts";
|
|
35
|
+
|
|
36
|
+
/** Why a link couldn't be made; the caller reports it as `MOSTRA_ENV_BUNDLE_INVALID`. */
|
|
37
|
+
export class LinkRefused extends Error {}
|
|
38
|
+
|
|
39
|
+
/** Whether `path` (an absolute path inside `root`) has only real directories above it. */
|
|
40
|
+
async function realParents(root: string, path: string): Promise<boolean> {
|
|
41
|
+
let dir = dirname(path);
|
|
42
|
+
const chain: string[] = [];
|
|
43
|
+
while (dir.length > root.length) {
|
|
44
|
+
chain.unshift(dir);
|
|
45
|
+
dir = dirname(dir);
|
|
46
|
+
}
|
|
47
|
+
for (const parent of chain) {
|
|
48
|
+
const info = await lstat(parent).catch(() => undefined);
|
|
49
|
+
if (info === undefined) {
|
|
50
|
+
await mkdir(parent);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (!info.isDirectory() || info.isSymbolicLink()) return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Makes every link `manifest` lists as a junction under `root`, which must be the bundle's real
|
|
60
|
+
* path (`realpath.native`). A link that already names its target is kept; one that names another
|
|
61
|
+
* (the bundle moved) is replaced. Throws {@link LinkRefused} for a link the manifest can't have.
|
|
62
|
+
*/
|
|
63
|
+
export async function makeJunctions(root: string, manifest: BundleFiles): Promise<void> {
|
|
64
|
+
// A link's target may lie behind another link (`node_modules/x` through a scope's link), so a
|
|
65
|
+
// link whose target isn't there yet waits for the next round; a round that makes nothing ends.
|
|
66
|
+
const realRoot = realOrNone(root);
|
|
67
|
+
let waiting = [...manifest.links].sort((a, b) => a.path.length - b.path.length);
|
|
68
|
+
while (waiting.length > 0) {
|
|
69
|
+
const later: typeof waiting = [];
|
|
70
|
+
for (const link of waiting) {
|
|
71
|
+
if (!(await makeJunction(root, link, realRoot))) later.push(link);
|
|
72
|
+
}
|
|
73
|
+
if (later.length === waiting.length) {
|
|
74
|
+
const first = later[0] as BundleLinkEntry;
|
|
75
|
+
throw new LinkRefused(`${first.path} links to ${first.target}, which is no directory.`);
|
|
76
|
+
}
|
|
77
|
+
waiting = later;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Makes one link; `false` when its target isn't a directory (yet). */
|
|
82
|
+
async function makeJunction(
|
|
83
|
+
root: string,
|
|
84
|
+
link: BundleLinkEntry,
|
|
85
|
+
realRoot: string | undefined,
|
|
86
|
+
): Promise<boolean> {
|
|
87
|
+
if (!safeRelative(link.path) || !linkStaysInside(root, link.path, link.target)) {
|
|
88
|
+
throw new LinkRefused(`${link.path} links outside the bundle (${link.target}).`);
|
|
89
|
+
}
|
|
90
|
+
const at = join(root, link.path);
|
|
91
|
+
const target = resolve(dirname(at), link.target);
|
|
92
|
+
if (!(await realParents(root, at))) {
|
|
93
|
+
throw new LinkRefused(`${link.path} sits below a link or a file.`);
|
|
94
|
+
}
|
|
95
|
+
const kind = await stat(target).catch(() => undefined);
|
|
96
|
+
if (kind === undefined || !kind.isDirectory()) {
|
|
97
|
+
return names(root, link, at, realRoot);
|
|
98
|
+
}
|
|
99
|
+
// Another setup of this bundle may be making the same links (finding 5). Each attempt reads the
|
|
100
|
+
// state afresh and accepts a link that names its target, whoever made it; a setup removes only
|
|
101
|
+
// what doesn't, and always makes the link right after, while nothing ever makes a stale one, so
|
|
102
|
+
// the link names its target once both stop.
|
|
103
|
+
let last: unknown;
|
|
104
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
105
|
+
if (await names(root, link, at, realRoot)) return true;
|
|
106
|
+
const current = await lstat(at).catch(() => undefined);
|
|
107
|
+
try {
|
|
108
|
+
if (current?.isSymbolicLink()) {
|
|
109
|
+
// A junction is a directory to Windows: `rmdir` removes the link, never what it names.
|
|
110
|
+
await rmdir(at).catch(async (error: NodeJS.ErrnoException) => {
|
|
111
|
+
if (error.code !== "ENOENT") await unlink(at);
|
|
112
|
+
});
|
|
113
|
+
} else if (current !== undefined) {
|
|
114
|
+
if (!current.isDirectory() || !(await sameTree(at, target))) {
|
|
115
|
+
// Read again: another setup may have replaced the copy while it was compared.
|
|
116
|
+
if (await names(root, link, at, realRoot)) return true;
|
|
117
|
+
const now = await lstat(at).catch(() => undefined);
|
|
118
|
+
if (now !== undefined && !now.isSymbolicLink()) {
|
|
119
|
+
throw new LinkRefused(
|
|
120
|
+
`${link.path} is a file or directory where the bundle has a link, and it isn't a copy of ${link.target}.`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
// A copy of the target: no link inside it (`sameTree` refuses one), so nothing outside it.
|
|
126
|
+
await rm(at, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
127
|
+
}
|
|
128
|
+
await symlink(target, at, "junction");
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (error instanceof LinkRefused) throw error;
|
|
131
|
+
last = error; // another setup's step came between: the next attempt reads the state again
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (await names(root, link, at, realRoot)) return true;
|
|
135
|
+
throw new LinkRefused(
|
|
136
|
+
`${link.path} couldn't be made as a junction to ${link.target}${last ? `: ${(last as Error).message}` : ""}.`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Whether the link at `at` exists and names the manifest's target. */
|
|
141
|
+
async function names(
|
|
142
|
+
root: string,
|
|
143
|
+
link: BundleLinkEntry,
|
|
144
|
+
at: string,
|
|
145
|
+
realRoot: string | undefined,
|
|
146
|
+
): Promise<boolean> {
|
|
147
|
+
const info = await lstat(at).catch(() => undefined);
|
|
148
|
+
if (info === undefined || !info.isSymbolicLink()) return false;
|
|
149
|
+
const written = await readlink(at).catch(() => undefined);
|
|
150
|
+
return written !== undefined && linkTarget(root, link.path, written, realRoot) === link.target;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Whether the real directory `copy` holds exactly what `original` holds: the same names, files of
|
|
155
|
+
* the same bytes, and directories alike all the way down. A link anywhere in the copy, or an entry
|
|
156
|
+
* of any other kind, makes it no copy.
|
|
157
|
+
*/
|
|
158
|
+
async function sameTree(copy: string, original: string): Promise<boolean> {
|
|
159
|
+
const [mine, theirs] = await Promise.all([
|
|
160
|
+
readdir(copy, { withFileTypes: true }).catch(() => undefined),
|
|
161
|
+
readdir(original, { withFileTypes: true }).catch(() => undefined),
|
|
162
|
+
]);
|
|
163
|
+
if (mine === undefined || theirs === undefined || mine.length !== theirs.length) return false;
|
|
164
|
+
const byName = new Map(theirs.map((entry) => [entry.name, entry]));
|
|
165
|
+
for (const entry of mine) {
|
|
166
|
+
const other = byName.get(entry.name);
|
|
167
|
+
if (other === undefined || entry.isSymbolicLink()) return false;
|
|
168
|
+
const a = join(copy, entry.name);
|
|
169
|
+
const b = join(original, entry.name);
|
|
170
|
+
if (entry.isDirectory()) {
|
|
171
|
+
// The original may reach a directory through a link (a junction made a moment ago).
|
|
172
|
+
if (!(await stat(b).catch(() => undefined))?.isDirectory()) return false;
|
|
173
|
+
if (!(await sameTree(a, b))) return false;
|
|
174
|
+
} else if (entry.isFile()) {
|
|
175
|
+
if (!(other.isFile() || other.isSymbolicLink()) || !(await sameBytes(a, b))) return false;
|
|
176
|
+
} else return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Whether two files hold the same bytes (streamed, so a large program isn't held in memory). */
|
|
182
|
+
async function sameBytes(a: string, b: string): Promise<boolean> {
|
|
183
|
+
const [sa, sb] = await Promise.all([stat(a), stat(b)]).catch(() => [undefined, undefined]);
|
|
184
|
+
if (sa === undefined || sb === undefined || !sb.isFile() || sa.size !== sb.size) return false;
|
|
185
|
+
const [ha, hb] = await Promise.all([hashFile(a), hashFile(b)]);
|
|
186
|
+
return ha.sha256 === hb.sha256;
|
|
187
|
+
}
|
package/src/bundle/setup.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* hand, and for the Rust crate (WP-12), so there is one implementation of what "ready" means.
|
|
7
7
|
*/
|
|
8
8
|
import { spawn } from "node:child_process";
|
|
9
|
+
import { realpathSync } from "node:fs";
|
|
9
10
|
import { readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
10
11
|
import { join } from "node:path";
|
|
11
12
|
import {
|
|
@@ -14,9 +15,12 @@ import {
|
|
|
14
15
|
BundleInfo,
|
|
15
16
|
BundleSetup,
|
|
16
17
|
createDiagnostic,
|
|
18
|
+
isWindowsBundle,
|
|
17
19
|
RpcError,
|
|
18
20
|
} from "@mostra/protocol";
|
|
21
|
+
import { childEnvironment } from "../environment.ts";
|
|
19
22
|
import { scrubUrls } from "./download.ts";
|
|
23
|
+
import { LinkRefused, makeJunctions } from "./links.ts";
|
|
20
24
|
import { checkTree, hashFile, safeRelative } from "./tree.ts";
|
|
21
25
|
|
|
22
26
|
/** The environment setup reads. */
|
|
@@ -90,14 +94,28 @@ export async function readBundle(root: string): Promise<Bundle> {
|
|
|
90
94
|
return { root, info };
|
|
91
95
|
}
|
|
92
96
|
|
|
93
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* The bundle's real path: links followed, Windows' short names written out, the case as the file
|
|
99
|
+
* system keeps it. A Windows bundle's junctions point into it, and its marker names it.
|
|
100
|
+
*/
|
|
101
|
+
export function realRoot(root: string): string {
|
|
102
|
+
return realpathSync.native(root);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The setup marker, when it describes exactly this bundle. A Windows bundle's marker must also
|
|
107
|
+
* name the root it sits at now: its junctions point into the root setup ran at, so a bundle moved
|
|
108
|
+
* or copied since is set up again, which makes them afresh (D153).
|
|
109
|
+
*/
|
|
94
110
|
export async function readSetup(bundle: Bundle): Promise<BundleSetup | undefined> {
|
|
95
111
|
try {
|
|
96
112
|
const marker = BundleSetup.parse(
|
|
97
113
|
JSON.parse(await readFile(join(bundle.root, BUNDLE_SETUP_MARKER), "utf8")),
|
|
98
114
|
);
|
|
99
115
|
const { info } = bundle;
|
|
100
|
-
|
|
116
|
+
const here = isWindowsBundle(info.platform) ? marker.root === realRoot(bundle.root) : true;
|
|
117
|
+
return here &&
|
|
118
|
+
marker.files === info.files.sha256 &&
|
|
101
119
|
marker.version === info.version &&
|
|
102
120
|
marker.platform === info.platform &&
|
|
103
121
|
marker.browser.revision === info.browser.revision
|
|
@@ -120,8 +138,8 @@ export function setupExtras(path: string): boolean {
|
|
|
120
138
|
);
|
|
121
139
|
}
|
|
122
140
|
|
|
123
|
-
/**
|
|
124
|
-
|
|
141
|
+
/** The bundle's `files.json`, once its SHA-256 is the one `bundle.json` pins. */
|
|
142
|
+
async function readManifest(bundle: Bundle): Promise<BundleFiles> {
|
|
125
143
|
const { root, info } = bundle;
|
|
126
144
|
const listing = join(root, info.files.path);
|
|
127
145
|
const { sha256 } = await hashFile(listing).catch(() => ({ sha256: "" }));
|
|
@@ -130,7 +148,39 @@ export async function checkFiles(bundle: Bundle): Promise<void> {
|
|
|
130
148
|
}
|
|
131
149
|
const manifest = BundleFiles.safeParse(JSON.parse(await readFile(listing, "utf8")));
|
|
132
150
|
if (!manifest.success) throw invalid(`${info.files.path} isn't a bundle's file list.`);
|
|
133
|
-
|
|
151
|
+
return manifest.data;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Makes a Windows bundle's links, each a junction at the bundle's real path (D153); nothing for
|
|
156
|
+
* any other bundle, whose archive carries its links. Throws `MOSTRA_ENV_BUNDLE_INVALID` for a link
|
|
157
|
+
* the bundle can't have.
|
|
158
|
+
*/
|
|
159
|
+
export async function makeLinks(bundle: Bundle, read?: BundleFiles): Promise<void> {
|
|
160
|
+
if (!isWindowsBundle(bundle.info.platform)) return;
|
|
161
|
+
const manifest = read ?? (await readManifest(bundle));
|
|
162
|
+
try {
|
|
163
|
+
await makeJunctions(realRoot(bundle.root), manifest);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
if (error instanceof LinkRefused) throw invalid(`The runtime bundle's links: ${error.message}`);
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Checks the bundle's files; throws `MOSTRA_ENV_BUNDLE_INVALID` naming what differs. With
|
|
172
|
+
* `links: false` the files alone are checked (a Windows bundle before `makeLinks` made its junctions).
|
|
173
|
+
*/
|
|
174
|
+
export async function checkFiles(
|
|
175
|
+
bundle: Bundle,
|
|
176
|
+
options: { readonly links?: boolean; readonly manifest?: BundleFiles } = {},
|
|
177
|
+
): Promise<void> {
|
|
178
|
+
const { root, info } = bundle;
|
|
179
|
+
const manifest = options.manifest ?? (await readManifest(bundle));
|
|
180
|
+
const problems = await checkTree(root, manifest, {
|
|
181
|
+
allowExtra: setupExtras,
|
|
182
|
+
links: options.links ?? true,
|
|
183
|
+
});
|
|
134
184
|
if (problems.length > 0) {
|
|
135
185
|
throw invalid(
|
|
136
186
|
`Mostra ${info.version}'s runtime bundle for ${info.platform} differs from its file list: ${problems.join("; ")}.`,
|
|
@@ -151,13 +201,13 @@ export async function checkBrowser(bundle: Bundle): Promise<readonly string[]> {
|
|
|
151
201
|
|
|
152
202
|
/** The environment the browser install runs in: proxies and Playwright's mirror pass through. */
|
|
153
203
|
function installEnvironment(bundle: Bundle, env: Environment): NodeJS.ProcessEnv {
|
|
154
|
-
const out:
|
|
204
|
+
const out: Record<string, string> = {};
|
|
155
205
|
for (const [name, value] of Object.entries(env)) {
|
|
156
206
|
if (value !== undefined && !name.startsWith("MOSTRA_") && name !== "PLAYWRIGHT_BROWSERS_PATH")
|
|
157
207
|
out[name] = value;
|
|
158
208
|
}
|
|
159
209
|
out["PLAYWRIGHT_BROWSERS_PATH"] = browsersDir(bundle.root);
|
|
160
|
-
return out;
|
|
210
|
+
return childEnvironment(out);
|
|
161
211
|
}
|
|
162
212
|
|
|
163
213
|
/** The variables whose URLs may carry credentials the browser's fetch uses. */
|
|
@@ -202,6 +252,7 @@ async function installBrowser(bundle: Bundle, options: SetupOptions): Promise<vo
|
|
|
202
252
|
cwd: root,
|
|
203
253
|
env: installEnvironment(bundle, options.env ?? process.env),
|
|
204
254
|
stdio: ["ignore", "pipe", "pipe"],
|
|
255
|
+
windowsHide: true,
|
|
205
256
|
...(options.signal ? { signal: options.signal } : {}),
|
|
206
257
|
timeout: 15 * 60_000,
|
|
207
258
|
},
|
|
@@ -239,9 +290,16 @@ async function installBrowser(bundle: Bundle, options: SetupOptions): Promise<vo
|
|
|
239
290
|
export async function setupBundle(root: string, options: SetupOptions = {}): Promise<BundleSetup> {
|
|
240
291
|
const env = options.env ?? process.env;
|
|
241
292
|
const bundle = await readBundle(root);
|
|
293
|
+
// A Windows bundle's junctions are made where it sits now, each read back as it is made; a
|
|
294
|
+
// tree checked a moment ago (`filesChecked`, before the cache's rename) needs nothing more.
|
|
295
|
+
// `files.json` is read and checked against its pin once, for both steps (Opus review of
|
|
296
|
+
// WP-W3, finding 9): it lists every file of the bundle and is large.
|
|
297
|
+
const windows = isWindowsBundle(bundle.info.platform);
|
|
298
|
+
const manifest = windows || !options.filesChecked ? await readManifest(bundle) : undefined;
|
|
299
|
+
await makeLinks(bundle, manifest);
|
|
242
300
|
if (!options.filesChecked) {
|
|
243
301
|
options.onStep?.("files");
|
|
244
|
-
await checkFiles(bundle);
|
|
302
|
+
await checkFiles(bundle, manifest ? { manifest } : {});
|
|
245
303
|
}
|
|
246
304
|
options.onStep?.("browser");
|
|
247
305
|
if ((await checkBrowser(bundle)).length > 0) {
|
|
@@ -270,6 +328,7 @@ export async function setupBundle(root: string, options: SetupOptions = {}): Pro
|
|
|
270
328
|
files: bundle.info.files.sha256,
|
|
271
329
|
browser: { revision: bundle.info.browser.revision },
|
|
272
330
|
at: new Date().toISOString(),
|
|
331
|
+
root: realRoot(root),
|
|
273
332
|
};
|
|
274
333
|
const temporary = join(root, `${BUNDLE_SETUP_MARKER}.tmp-${process.pid}`);
|
|
275
334
|
await writeFile(temporary, `${JSON.stringify(marker, null, 2)}\n`);
|