@daugasauron/dolly 0.0.0-stage → 0.1.4-rc.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.
Files changed (72) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +89 -2
  3. package/dist/dolly-build-id.mjs +1 -0
  4. package/dist/dolly-dso-0.wasm +0 -0
  5. package/dist/dolly-image-build-id.mjs +1 -0
  6. package/dist/dolly-kernel-plugin-abi.mjs +2 -0
  7. package/dist/dolly-process-0.wasm +0 -0
  8. package/dist/dolly-process-abi.mjs +1 -0
  9. package/dist/dolly-process-dso.mjs +1575 -0
  10. package/dist/dolly-process-gate-0.wasm +0 -0
  11. package/dist/dolly-process-stack.mjs +50 -0
  12. package/dist/dolly-process-worker.mjs +275 -0
  13. package/dist/dolly-threads-0.wasm +0 -0
  14. package/dist/dolly.wasm +0 -0
  15. package/host/abi.mjs +5 -0
  16. package/host/dso/abi.mjs +13 -0
  17. package/host/dso/dso.mjs +26 -0
  18. package/host/dso/module.json +15 -0
  19. package/host/http/abi.mjs +28 -0
  20. package/host/http/broker.mjs +325 -0
  21. package/host/http/http.mjs +74 -0
  22. package/host/http/local-services.mjs +37 -0
  23. package/host/http/module.json +15 -0
  24. package/host/http/policy.mjs +293 -0
  25. package/host/modules.mjs +373 -0
  26. package/host/nena/abi.mjs +19 -0
  27. package/host/nena/module.json +15 -0
  28. package/host/nena/nena.mjs +131 -0
  29. package/host/packages/module.json +13 -0
  30. package/host/packages/packages.mjs +15 -0
  31. package/host/packages/service.mjs +59 -0
  32. package/host/requirements.mjs +62 -0
  33. package/host/runtime/abi.mjs +8 -0
  34. package/host/runtime/boot-files.mjs +45 -0
  35. package/host/runtime/grower.mjs +14 -0
  36. package/host/runtime/lease-bridge.mjs +65 -0
  37. package/host/runtime/module.json +16 -0
  38. package/host/runtime/runtime.mjs +209 -0
  39. package/host/snapshot/abi.mjs +16 -0
  40. package/host/snapshot/module.json +15 -0
  41. package/host/snapshot/snapshot.mjs +138 -0
  42. package/host/snapshot/transport.mjs +132 -0
  43. package/host/snapshot/ui.mjs +207 -0
  44. package/host/stack/abi.mjs +5 -0
  45. package/host/stack/module.json +17 -0
  46. package/host/stack/stack.mjs +19 -0
  47. package/host/threads/abi.mjs +8 -0
  48. package/host/threads/module.json +13 -0
  49. package/host/threads/threads.mjs +36 -0
  50. package/package.json +12 -4
  51. package/src/alphabetical.mjs +4 -0
  52. package/src/browser.mjs +237 -0
  53. package/src/build-log.mjs +30 -0
  54. package/src/image-entry.mjs +29 -0
  55. package/src/image-inputs.mjs +20 -0
  56. package/src/image-names.mjs +11 -0
  57. package/src/images.mjs +10 -0
  58. package/src/kernel-plugin.mjs +77 -0
  59. package/src/packaged-snapshot.mjs +175 -0
  60. package/src/page-chords.mjs +43 -0
  61. package/src/page-indicators.mjs +27 -0
  62. package/src/process-abi.mjs +69 -0
  63. package/src/process-constants.mjs +242 -0
  64. package/src/process-supervisor.mjs +1002 -0
  65. package/src/runtime-worker.mjs +55 -0
  66. package/src/session-store.mjs +198 -0
  67. package/src/snapshot-records.mjs +109 -0
  68. package/src/static-asset.mjs +97 -0
  69. package/src/terminal-text.mjs +72 -0
  70. package/src/version.mjs +3 -0
  71. package/src/wasm-interface.mjs +440 -0
  72. package/src/worker-image.mjs +137 -0
@@ -0,0 +1,55 @@
1
+ // The runtime Worker boot() starts: it restores an image of the image directory
2
+ // from its packs into a new kernel and runs its ENTRY (worker-image.mjs).
3
+ import { DOLLY_IMAGES } from "./images.mjs";
4
+ import { loadPackagedSnapshotMetadata, streamPackagedSystemSnapshot } from "./packaged-snapshot.mjs";
5
+ import { siteReference, hex } from "./static-asset.mjs";
6
+ import { checkedMemoryRange, runImage } from "./worker-image.mjs";
7
+
8
+ await runImage(async ({ image, mode }, host) => {
9
+ const definition = DOLLY_IMAGES.find(candidate => candidate.image === image);
10
+ if (!definition || mode !== "snapshot") throw new Error("invalid Dolly image selection");
11
+ const metadata = await loadPackagedSnapshotMetadata(image);
12
+ const declared = metadata.hostRequirements ?? definition.hostRequirements ?? [];
13
+ host.require(declared);
14
+ host.admit(declared);
15
+ return {
16
+ name: image,
17
+ prepare({ files }) {
18
+ files.write("/etc/dolly/recipe.locator", siteReference(definition.dollyfile));
19
+ // The kernel restores a snapshot against the list of its paths.
20
+ files.write("/etc/dolly/image.manifest", `${metadata.manifest.join("\n")}\n`);
21
+ },
22
+ async restore({ kernel, memory, stage }) {
23
+ stage("loading precompiled userspace snapshot...");
24
+ const capacity = 1024 * 1024;
25
+ const address = kernel.dolly_snapshot_restore_address(BigInt(capacity));
26
+ const staging = () => {
27
+ const range = checkedMemoryRange(memory, address, capacity);
28
+ return new Uint8Array(memory.buffer, range.address, range.size);
29
+ };
30
+ staging().set(Uint8Array.from(metadata.sha256.match(/../g), byte => parseInt(byte, 16)));
31
+ if (kernel.dolly_bootstrap_snapshot_begin(BigInt(metadata.byteLength)) !== 0)
32
+ throw new Error("Dolly snapshot stream initialization failed");
33
+ let loaded = 0, reported = 0;
34
+ await streamPackagedSystemSnapshot(image, metadata, bytes => {
35
+ for (let offset = 0; offset < bytes.length; offset += capacity) {
36
+ const chunk = bytes.subarray(offset, offset + capacity);
37
+ staging().set(chunk);
38
+ if (kernel.dolly_snapshot_stream_write(BigInt(chunk.length), 0) !== 0)
39
+ throw new Error("Dolly rejected a snapshot stream record");
40
+ }
41
+ loaded += bytes.length;
42
+ if (performance.now() - reported >= 1000) {
43
+ const mib = 1024 * 1024;
44
+ stage(`loading userspace: ${Math.min(Math.floor(loaded / mib), Math.ceil(metadata.byteLength / mib))} / ${Math.ceil(metadata.byteLength / mib)} MiB`);
45
+ reported = performance.now();
46
+ }
47
+ }, () => {
48
+ if (kernel.dolly_snapshot_stream_write(0n, 1) !== 0)
49
+ throw new Error("Dolly rejected an incomplete snapshot part");
50
+ return hex(staging().subarray(0, 32));
51
+ });
52
+ return { status: kernel.dolly_bootstrap_snapshot_end() };
53
+ },
54
+ };
55
+ });
@@ -0,0 +1,198 @@
1
+ import { alphabetical } from "./alphabetical.mjs";
2
+ import { validName } from "./image-names.mjs";
3
+ import { imageInputs } from "./image-inputs.mjs";
4
+ import { DOLLY_VERSION } from "./version.mjs";
5
+
6
+ export const DOLLY_SESSION_FORMAT_VERSION = 2;
7
+ export const DOLLY_SESSION_MAX_BYTES = 512 * 1024 * 1024;
8
+ export const DOLLY_SESSION_METADATA_MAX_BYTES = 1024 * 1024;
9
+
10
+ // Saves belong to a version: every version's pages open their own database.
11
+ const databaseName = `dolly-sessions-v${DOLLY_VERSION}`;
12
+ const storeName = "sessions";
13
+
14
+ export function validSessionName(value) {
15
+ return typeof value === "string" && value.length >= 1 && value.length <= 64 &&
16
+ value !== "." && value !== ".." && /^[A-Za-z0-9._-]+$/.test(value);
17
+ }
18
+
19
+ export function sessionImageIdentity(definitions, selectedImage) {
20
+ const definition = definitions.find(({ image }) => image === selectedImage);
21
+ if (!definition) throw new Error("Dolly session names an unknown image");
22
+ return `${definition.image}:${definition.sha256}`;
23
+ }
24
+
25
+ // The policies are what http@0 hands a tab opened from another. An image
26
+ // without a network edge inherits none; http@0 itself refuses a tab whose
27
+ // inheritance lacks them (host/http/http.mjs).
28
+ export function customSessionIdentity(custom) {
29
+ const artifact = custom?.artifact, policies = custom?.policies;
30
+ if (!artifact || typeof artifact.buildId !== "string" || !artifact.buildId.length || artifact.buildId.length > 128 ||
31
+ typeof artifact.recipeSha256 !== "string" || typeof artifact.sha256 !== "string" ||
32
+ !/^[0-9a-f]{64}$/.test(artifact.recipeSha256) || !/^[0-9a-f]{64}$/.test(artifact.sha256) ||
33
+ !Number.isSafeInteger(artifact.byteLength) || artifact.byteLength <= 0 ||
34
+ artifact.byteLength > DOLLY_SESSION_MAX_BYTES ||
35
+ (policies !== undefined && (!Array.isArray(policies) || policies.length === 0 || policies.length > 16 ||
36
+ new TextEncoder().encode(JSON.stringify(policies)).byteLength > 65536))) {
37
+ throw new TypeError("invalid custom session base");
38
+ }
39
+ // The site parses the Dollyfile where it imports a session file or runs the image.
40
+ if (typeof custom.source !== "string") throw new TypeError("invalid custom session base");
41
+ imageInputs(artifact.inputs);
42
+ return `custom:${artifact.recipeSha256}:${artifact.sha256}`;
43
+ }
44
+
45
+ export function sessionCompatible(record, definitions, buildId, imageBuildId) {
46
+ if (record.formatVersion !== DOLLY_SESSION_FORMAT_VERSION || record.buildId !== buildId) return false;
47
+ if (record.image === "custom") {
48
+ try {
49
+ return record.imageIdentity === customSessionIdentity(record.customImage) &&
50
+ record.customImage.artifact.buildId === imageBuildId;
51
+ } catch { return false; }
52
+ }
53
+ return record.customImage === undefined && definitions.some(({ image }) => image === record.image) &&
54
+ record.imageIdentity === sessionImageIdentity(definitions, record.image);
55
+ }
56
+
57
+ export function sessionLoadUrl(name, applicationBase) {
58
+ if (!validSessionName(name)) throw new TypeError("invalid Dolly session name");
59
+ return new URL(`session/?name=${name}`, applicationBase);
60
+ }
61
+
62
+ export async function listStoredSessions() {
63
+ const records = await transaction("readonly", store => store.getAll());
64
+ return records.map(({ bytes, ...metadata }) => ({ ...metadata, byteLength: bytes.size }))
65
+ .sort((left, right) => alphabetical(left.name, right.name));
66
+ }
67
+
68
+ async function collectStream(stream, maximum) {
69
+ const reader = stream.getReader();
70
+ try {
71
+ const buffer = new ArrayBuffer(0, { maxByteLength: maximum });
72
+ const bytes = new Uint8Array(buffer);
73
+ let length = 0;
74
+ for (;;) {
75
+ const { done, value } = await reader.read();
76
+ if (done) break;
77
+ const end = length + value.byteLength;
78
+ if (end > maximum) throw new Error("Dolly session exceeds its size limit");
79
+ if (end > buffer.byteLength) buffer.resize(Math.min(maximum,
80
+ Math.max(end, buffer.byteLength * 2, 65536)));
81
+ bytes.set(value, length);
82
+ length = end;
83
+ }
84
+ buffer.resize(length);
85
+ return buffer;
86
+ } catch (error) {
87
+ await reader.cancel(error).catch(() => {});
88
+ throw error;
89
+ } finally {
90
+ reader.releaseLock();
91
+ }
92
+ }
93
+
94
+ export async function encodeSessionStream(produce) {
95
+ const stream = new CompressionStream("gzip"), writer = stream.writable.getWriter();
96
+ const encoded = collectStream(stream.readable, DOLLY_SESSION_MAX_BYTES);
97
+ // The reader can fail while the producer is awaiting its next mailbox chunk.
98
+ encoded.catch(() => {});
99
+ try {
100
+ await produce(bytes => writer.write(bytes));
101
+ await writer.close();
102
+ // Blob storage needs fixed buffers; decoded buffers transfer directly to Wasm.
103
+ return { encoding: "gzip", bytes: (await encoded).transferToFixedLength() };
104
+ } catch (error) {
105
+ await writer.abort(error).catch(() => {});
106
+ await encoded.catch(() => {});
107
+ throw error;
108
+ }
109
+ }
110
+
111
+ export async function decodeSessionSnapshot(record) {
112
+ if (record === null || typeof record !== "object" ||
113
+ !(record.bytes instanceof ArrayBuffer) || record.bytes.byteLength === 0 ||
114
+ record.bytes.byteLength > DOLLY_SESSION_MAX_BYTES ||
115
+ !["gzip", "identity"].includes(record.encoding)) {
116
+ throw new Error("Stored Dolly session is invalid");
117
+ }
118
+ if (record.encoding === "identity") return record.bytes.slice(0);
119
+ let offset = 0;
120
+ const stream = new ReadableStream({
121
+ pull(controller) {
122
+ if (offset === record.bytes.byteLength) { controller.close(); return; }
123
+ const length = Math.min(65536, record.bytes.byteLength - offset);
124
+ controller.enqueue(new Uint8Array(record.bytes, offset, length));
125
+ offset += length;
126
+ },
127
+ }).pipeThrough(new DecompressionStream("gzip"));
128
+ return collectStream(stream, DOLLY_SESSION_MAX_BYTES);
129
+ }
130
+
131
+ function openDatabase() {
132
+ return new Promise((resolve, reject) => {
133
+ const request = indexedDB.open(databaseName, 1);
134
+ request.addEventListener("upgradeneeded", () => {
135
+ if (!request.result.objectStoreNames.contains(storeName)) {
136
+ request.result.createObjectStore(storeName, { keyPath: "name" });
137
+ }
138
+ });
139
+ request.addEventListener("success", () => resolve(request.result), { once: true });
140
+ request.addEventListener("error", () => reject(request.error), { once: true });
141
+ });
142
+ }
143
+
144
+ async function transaction(mode, operation) {
145
+ const database = await openDatabase();
146
+ try {
147
+ return await new Promise((resolve, reject) => {
148
+ const active = database.transaction(storeName, mode);
149
+ const request = operation(active.objectStore(storeName));
150
+ let result;
151
+ request.addEventListener("success", () => { result = request.result; }, { once: true });
152
+ request.addEventListener("error", () => reject(request.error), { once: true });
153
+ active.addEventListener("complete", () => resolve(result), { once: true });
154
+ active.addEventListener("abort", () => reject(active.error), { once: true });
155
+ });
156
+ } finally {
157
+ database.close();
158
+ }
159
+ }
160
+
161
+ export async function loadStoredSession(name) {
162
+ if (!validSessionName(name)) throw new TypeError("invalid Dolly session name");
163
+ const record = (await transaction("readonly", (store) => store.get(name))) ?? null;
164
+ if (record === null) return null;
165
+ if (!(record.bytes instanceof Blob) || record.bytes.size === 0 || record.bytes.size > DOLLY_SESSION_MAX_BYTES) {
166
+ throw new Error("Stored Dolly session is invalid");
167
+ }
168
+ record.bytes = await record.bytes.arrayBuffer();
169
+ return record;
170
+ }
171
+
172
+ export function validateSessionRecord(record) {
173
+ if (record === null || typeof record !== "object" ||
174
+ !validSessionName(record.name) ||
175
+ record.formatVersion !== DOLLY_SESSION_FORMAT_VERSION ||
176
+ typeof record.buildId !== "string" || record.buildId.length > 128 ||
177
+ typeof record.image !== "string" || !validName(record.image) ||
178
+ typeof record.imageIdentity !== "string" || record.imageIdentity.length > 256 ||
179
+ !Number.isSafeInteger(record.updatedAt) ||
180
+ !(record.bytes instanceof ArrayBuffer) || record.bytes.byteLength === 0 ||
181
+ record.bytes.byteLength > DOLLY_SESSION_MAX_BYTES ||
182
+ !["gzip", "identity"].includes(record.encoding)) {
183
+ throw new TypeError("invalid Dolly session record");
184
+ }
185
+ if (record.image === "custom" ? record.imageIdentity !== customSessionIdentity(record.customImage)
186
+ : record.customImage !== undefined) throw new TypeError("invalid custom session identity");
187
+ }
188
+
189
+ export async function saveStoredSession(record, { overwrite = true } = {}) {
190
+ validateSessionRecord(record);
191
+ const stored = { ...record, bytes: new Blob([record.bytes]) };
192
+ await transaction("readwrite", (store) => overwrite ? store.put(stored) : store.add(stored));
193
+ }
194
+
195
+ export async function deleteStoredSession(name) {
196
+ if (!validSessionName(name)) throw new TypeError("invalid Dolly session name");
197
+ await transaction("readwrite", store => store.delete(name));
198
+ }
@@ -0,0 +1,109 @@
1
+ import { unretainedPath } from "./image-names.mjs";
2
+ const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
3
+ export const MAX_SNAPSHOT_BYTES = 2 * 1024 * 1024 * 1024;
4
+
5
+ function compareBytes(left, right) {
6
+ for (let index = 0; index < Math.min(left.length, right.length); index += 1) {
7
+ if (left[index] !== right[index]) return left[index] - right[index];
8
+ }
9
+ return left.length - right.length;
10
+ }
11
+
12
+ export function decodeSnapshotRecords(input) {
13
+ const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
14
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
15
+ if (bytes.length < 16 || bytes.length > MAX_SNAPSHOT_BYTES ||
16
+ decoder.decode(bytes.subarray(0, 8)) !== "DOLLYSNP" || view.getUint32(8, true) !== 2) {
17
+ throw new Error("invalid Dolly snapshot header");
18
+ }
19
+ const count = view.getUint32(12, true);
20
+ if (count === 0 || count > 100_000) throw new Error("invalid Dolly snapshot record count");
21
+ const records = new Map();
22
+ let offset = 16, previous;
23
+ for (let index = 0; index < count; index += 1) {
24
+ if (offset > bytes.length - 16) throw new Error("truncated snapshot record");
25
+ const kind = view.getUint32(offset, true), pathLength = view.getUint32(offset + 4, true);
26
+ const size = view.getBigUint64(offset + 8, true);
27
+ offset += 16;
28
+ if (kind < 1 || kind > 3 || pathLength < 2 || pathLength >= 4096 ||
29
+ size > BigInt(MAX_SNAPSHOT_BYTES) || (kind === 1 && size !== 0n) ||
30
+ (kind === 3 && (size === 0n || size >= 4096n))) throw new Error("invalid snapshot record size");
31
+ if (offset > bytes.length - pathLength - Number(size)) throw new Error("truncated snapshot record");
32
+ const pathBytes = bytes.subarray(offset, offset + pathLength);
33
+ const path = decoder.decode(pathBytes);
34
+ if (!path.startsWith("/") || /[\0\\\r\n]/.test(path) ||
35
+ path.slice(1).split("/").some(part => !part || part === "." || part === "..") ||
36
+ unretainedPath(path) ||
37
+ previous && compareBytes(previous, pathBytes) >= 0) throw new Error(`invalid snapshot path ${path}`);
38
+ previous = pathBytes;
39
+ offset += pathLength;
40
+ const data = bytes.subarray(offset, offset + Number(size));
41
+ if (kind === 3 && data.includes(0)) throw new Error("invalid snapshot symlink");
42
+ records.set(path, { kind, data });
43
+ offset += Number(size);
44
+ }
45
+ if (offset !== bytes.length) throw new Error("trailing snapshot bytes");
46
+ for (const path of records.keys()) {
47
+ for (let slash = path.indexOf("/", 1); slash !== -1; slash = path.indexOf("/", slash + 1)) {
48
+ const parent = records.get(path.slice(0, slash));
49
+ if (parent && parent.kind !== 1) throw new Error(`snapshot parent is not a directory: ${path}`);
50
+ }
51
+ }
52
+ return records;
53
+ }
54
+
55
+ export function encodeSnapshotRecords(records) {
56
+ const encoder = new TextEncoder();
57
+ const entries = [...records].map(([path, record]) => ({ path: encoder.encode(path), ...record }))
58
+ .sort((left, right) => compareBytes(left.path, right.path));
59
+ const size = entries.reduce((sum, record) => sum + 16 + record.path.length + record.data.length, 16);
60
+ if (size > MAX_SNAPSHOT_BYTES || entries.length === 0 || entries.length > 100_000) throw new Error("snapshot exceeds record limits");
61
+ const bytes = new Uint8Array(size), view = new DataView(bytes.buffer);
62
+ bytes.set(encoder.encode("DOLLYSNP"));
63
+ view.setUint32(8, 2, true);
64
+ view.setUint32(12, entries.length, true);
65
+ let offset = 16;
66
+ for (const record of entries) {
67
+ view.setUint32(offset, record.kind, true);
68
+ view.setUint32(offset + 4, record.path.length, true);
69
+ view.setBigUint64(offset + 8, BigInt(record.data.length), true);
70
+ bytes.set(record.path, offset + 16);
71
+ bytes.set(record.data, offset + 16 + record.path.length);
72
+ offset += 16 + record.path.length + record.data.length;
73
+ }
74
+ return bytes;
75
+ }
76
+
77
+ export function mergeSnapshotRecords(parts) {
78
+ const records = new Map();
79
+ let size = 16;
80
+ for (const part of parts) {
81
+ for (const [path, record] of decodeSnapshotRecords(part)) {
82
+ if (records.has(path)) throw new Error(`duplicate packed snapshot path: ${path}`);
83
+ size += 16 + new TextEncoder().encode(path).length + record.data.length;
84
+ if (size > MAX_SNAPSHOT_BYTES || records.size >= 100_000) throw new Error("packed snapshot exceeds limits");
85
+ records.set(path, record);
86
+ }
87
+ }
88
+ const bytes = encodeSnapshotRecords(records);
89
+ decodeSnapshotRecords(bytes);
90
+ return bytes;
91
+ }
92
+
93
+ export function validateSnapshotPacks(metadata) {
94
+ if (!Array.isArray(metadata.packs) || metadata.packs.length === 0 || metadata.packs.length > 256) throw new Error("invalid snapshot packs");
95
+ const hashes = new Set();
96
+ let size = 16;
97
+ for (const pack of metadata.packs) {
98
+ if (!/^[0-9a-f]{64}$/.test(pack.sha256) || hashes.has(pack.sha256) ||
99
+ !Number.isSafeInteger(pack.byteLength) || pack.byteLength < 16 || pack.byteLength > MAX_SNAPSHOT_BYTES ||
100
+ !Number.isSafeInteger(pack.encodedByteLength) || pack.encodedByteLength <= 0 || pack.encodedByteLength > MAX_SNAPSHOT_BYTES) {
101
+ throw new Error("invalid snapshot pack descriptor");
102
+ }
103
+ hashes.add(pack.sha256);
104
+ size += pack.byteLength - 16;
105
+ if (size > MAX_SNAPSHOT_BYTES) throw new Error("packed snapshot exceeds size limit");
106
+ }
107
+ if (size !== metadata.byteLength) throw new Error("snapshot pack sizes do not match image");
108
+ return metadata.packs;
109
+ }
@@ -0,0 +1,97 @@
1
+ import { MAX_SNAPSHOT_BYTES } from "./snapshot-records.mjs";
2
+ import { DOLLY_VERSION } from "./version.mjs";
3
+
4
+ // Static delivery for an already authorized bootstrap input or snapshot pack.
5
+ // Hosts cap file sizes, so assets travel as verified parts; nothing static is
6
+ // larger than the largest snapshot.
7
+ export const STATIC_PART_BYTES = 20 * 1024 * 1024;
8
+ export const staticMaxParts = () => Math.ceil(MAX_SNAPSHOT_BYTES / STATIC_PART_BYTES);
9
+
10
+ export const hex = bytes =>
11
+ Array.from(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes), byte => byte.toString(16).padStart(2, "0")).join("");
12
+
13
+ export async function sha256(bytes) {
14
+ return hex(await crypto.subtle.digest("SHA-256", bytes));
15
+ }
16
+
17
+ // Recipes name a file this site publishes as a path that starts with the
18
+ // version, /v0.1.0/Dollyfile-system. Every page serves its own version's
19
+ // files wherever it is mounted, so where their bytes come from never changes
20
+ // recipe text, pins or image identity.
21
+ const sitePrefix = `/v${DOLLY_VERSION}`;
22
+ export const siteReference = path => `${sitePrefix}/${path}`;
23
+
24
+ // The public site, for the pages of another site that link to it. A recipe
25
+ // that names it by URL instead of a site path fails the lint.
26
+ export const PUBLIC_ORIGIN = "https://slopshare.dev";
27
+
28
+ // The path ("/Dollyfile-system") a site reference names among this page's
29
+ // files, or null for an absolute URL. Another version's files are not here.
30
+ export function sitePath(reference) {
31
+ if (!reference.startsWith("/")) return null;
32
+ if (!reference.startsWith(`${sitePrefix}/`)) {
33
+ throw new Error(`${reference}: this is Dolly ${DOLLY_VERSION}, which reads only ${sitePrefix}/ paths`);
34
+ }
35
+ return reference.slice(sitePrefix.length);
36
+ }
37
+
38
+ export async function boundedAssetBody(response, limit, signal) {
39
+ if (!response.ok || !response.body) throw new Error(`asset returned HTTP ${response.status}`);
40
+ const reader = response.body.getReader(), chunks = [];
41
+ let size = 0;
42
+ try {
43
+ for (;;) {
44
+ signal?.throwIfAborted();
45
+ const { done, value } = await reader.read();
46
+ if (done) break;
47
+ size += value.length;
48
+ if (size > limit) { await reader.cancel(); throw new Error("asset exceeds declared size"); }
49
+ chunks.push(value);
50
+ }
51
+ } finally { reader.releaseLock(); }
52
+ const bytes = new Uint8Array(size);
53
+ let offset = 0;
54
+ for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.length; }
55
+ return bytes;
56
+ }
57
+
58
+ // Streams verified parts, holding at most one 20 MiB part per request. Each
59
+ // part matches the manifest hash before any of its bytes are delivered.
60
+ export async function decodeStaticAsset(response, target, init, maximumBytes, fetchRequest = globalThis.fetch.bind(globalThis)) {
61
+ if (response.headers.get("x-dolly-parts") !== "1") return response;
62
+ const url = new URL(target);
63
+ if (response.status !== 200 || (init.method ?? "GET") !== "GET" || url.search || url.hash ||
64
+ url.username || url.password || !/^https?:$/.test(url.protocol)) throw new Error("invalid multipart asset request");
65
+ const manifest = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(await boundedAssetBody(response, 65536, init.signal)));
66
+ if (!Number.isSafeInteger(manifest.byteLength) || manifest.byteLength <= 0 ||
67
+ manifest.byteLength > maximumBytes || manifest.byteLength > MAX_SNAPSHOT_BYTES ||
68
+ !Array.isArray(manifest.parts) || manifest.parts.length < 2 || manifest.parts.length > staticMaxParts() ||
69
+ manifest.parts.some(part => !Number.isSafeInteger(part.byteLength) || part.byteLength <= 0 ||
70
+ part.byteLength > STATIC_PART_BYTES || !/^[0-9a-f]{64}$/.test(part.sha256)) ||
71
+ manifest.parts.reduce((size, part) => size + part.byteLength, 0) !== manifest.byteLength) {
72
+ throw new Error("invalid multipart asset manifest");
73
+ }
74
+ let index = 0;
75
+ const body = new ReadableStream({
76
+ async pull(controller) {
77
+ if (index === manifest.parts.length) { controller.close(); return; }
78
+ const part = manifest.parts[index];
79
+ // The manifest supplies no destinations. Parts are fixed siblings, with
80
+ // no guest headers, credentials, query strings or redirect authority.
81
+ const partURL = new URL(url);
82
+ partURL.pathname += `.part-${index++}`;
83
+ const result = await fetchRequest(partURL, { method: "GET", credentials: "omit", redirect: "error",
84
+ referrerPolicy: "no-referrer", cache: "force-cache", signal: init.signal });
85
+ const chunk = await boundedAssetBody(result, part.byteLength, init.signal);
86
+ if (chunk.length !== part.byteLength || await sha256(chunk) !== part.sha256) throw new Error("asset part integrity mismatch");
87
+ controller.enqueue(chunk);
88
+ },
89
+ }, { highWaterMark: 0 });
90
+ const headers = new Headers(response.headers);
91
+ headers.delete("x-dolly-parts");
92
+ headers.delete("content-encoding");
93
+ headers.set("content-length", String(manifest.byteLength));
94
+ const decoded = new Response(body, { status: response.status, statusText: response.statusText, headers });
95
+ Object.defineProperty(decoded, "url", { value: response.url });
96
+ return decoded;
97
+ }
@@ -0,0 +1,72 @@
1
+ // How the page's handle (docs/embedding.md) reads the terminal, over the page APIs of
2
+ // display@0, input@0 and the runtime: the screen's text is the terminal's own
3
+ // selection of every cell, made with pointer records.
4
+ export function terminalText(page) {
5
+ // Resolves to true once predicate holds. When it never does, throws naming
6
+ // description, or resolves to false without one.
7
+ async function waitFor(predicate, description, attempts = 500) {
8
+ for (let attempt = 0; attempt < attempts; attempt++) {
9
+ if (await predicate()) return true;
10
+ await new Promise(resolve => setTimeout(resolve, 10));
11
+ }
12
+ if (description) throw new Error(`timed out waiting for ${description}`);
13
+ return false;
14
+ }
15
+
16
+ // Shift keeps the reader's gestures the terminal's selection while a
17
+ // program has asked for the mouse.
18
+ const shifted = { shiftKey: true };
19
+
20
+ // Selects the whole screen and reads the published selection. The press
21
+ // drops an earlier selection, so the text read after it is this one's. A
22
+ // program that enters or leaves the alternate screen between the press and
23
+ // the drag leaves the gesture without a selection (an editor exiting), so
24
+ // the gesture is made again, each time with a longer wait: a slow frame is
25
+ // not a broken gesture. Output that scrolls between the press and the drag
26
+ // (a program printing, Pi above voice's buttons) carries the selection's
27
+ // start into the history while its end is the screen's last cell: the
28
+ // screen is the selection's last rows.
29
+ async function visibleTerminalText() {
30
+ const { transport, inputTransport } = page;
31
+ for (const attempts of [25, 50, 100, 325]) {
32
+ const geometry = transport.geometry();
33
+ const dimensions = transport.dimensions();
34
+ if (!geometry.cellWidth || !geometry.cellHeight || !dimensions.cols || !dimensions.rows) return "";
35
+ const x = geometry.paddingX + Math.floor(geometry.cellWidth / 4);
36
+ const y = geometry.paddingY + Math.floor(geometry.cellHeight / 2);
37
+ const endX = x + (dimensions.cols - 1) * geometry.cellWidth, endY = y + (dimensions.rows - 1) * geometry.cellHeight;
38
+ let selected;
39
+ await inputTransport.pushPointer(x, y, 1, shifted);
40
+ await waitFor(async () => await transport.copySelection() === null, "terminal selection reset");
41
+ await inputTransport.pushPointer(endX, endY, 2, shifted);
42
+ await inputTransport.pushPointer(endX, endY, 0, shifted);
43
+ if (await waitFor(async () => (selected = await transport.copySelection()) !== null, null, attempts)) {
44
+ return selected.split("\n").slice(-dimensions.rows).join("\n");
45
+ }
46
+ }
47
+ throw new Error("timed out waiting for terminal selection publication");
48
+ }
49
+
50
+ // Resolves to the PID of a new foreground program in raw mode showing text
51
+ // that matches pattern.
52
+ async function waitForInteractiveTerminal(pattern, description, previousPid = 0) {
53
+ const { transport, inputTransport, terminal } = page;
54
+ let pid;
55
+ await waitFor(async () => {
56
+ pid = terminal.foregroundPid();
57
+ if (pid <= 0 || pid === previousPid || terminal.foregroundInterruptible() ||
58
+ transport.graphicsActive() || inputTransport.leased() || !inputTransport.inputIdle()) return false;
59
+ const text = await visibleTerminalText();
60
+ return terminal.foregroundPid() === pid && !terminal.foregroundInterruptible() && pattern.test(text);
61
+ }, description, 6000);
62
+ const geometry = transport.geometry();
63
+ const x = geometry.paddingX + Math.floor(geometry.cellWidth / 2);
64
+ const y = geometry.paddingY + Math.floor(geometry.cellHeight / 2);
65
+ await inputTransport.pushPointer(x, y, 1, shifted);
66
+ await inputTransport.pushPointer(x, y, 0, shifted);
67
+ await waitFor(() => inputTransport.inputIdle(), "terminal selection cleanup");
68
+ return pid;
69
+ }
70
+
71
+ return { visibleTerminalText, waitForInteractiveTerminal };
72
+ }
@@ -0,0 +1,3 @@
1
+ // The version being developed: the release tag vX.Y.Z, the site path /vX.Y.Z/
2
+ // and the prefix of every recipe reference. package.json holds the same value.
3
+ export const DOLLY_VERSION = "0.1.4";