@waffle-os/runtime 0.0.152 → 0.0.154
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/browser/net/helper.ts +114 -19
- package/browser/net/ring.ts +18 -6
- package/browser/net/runtime.ts +2 -2
- package/browser/notification-relay.ts +105 -18
- package/browser/notification-wait.ts +45 -4
- package/browser/notification-worker.ts +169 -76
- package/browser/preview.ts +15 -2
- package/browser/run-command.ts +14 -8
- package/browser/runtime-terminals.ts +26 -0
- package/browser/terminal-output.ts +3 -1
- package/core/filesystem.ts +4 -0
- package/core/wasi.ts +49 -0
- package/dist/browser/net/helper.js +83 -18
- package/dist/browser/net/ring.js +6 -5
- package/dist/browser/net/runtime.js +2 -2
- package/dist/browser/notification-relay.js +78 -15
- package/dist/browser/notification-wait.js +37 -5
- package/dist/browser/notification-worker.js +146 -67
- package/dist/browser/run-command.js +11 -7
- package/dist/browser/runtime-terminals.js +16 -0
- package/dist/browser/terminal-output.js +2 -2
- package/dist/core/wasi.js +40 -0
- package/dist/fs/backend.js +7 -0
- package/dist/fs/client.js +69 -15
- package/dist/fs/dirty-admission.js +15 -8
- package/dist/fs/indexeddb.js +288 -96
- package/dist/fs/memory.js +106 -20
- package/dist/fs/multitab-backend.js +121 -37
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +70 -16
- package/dist/fs/protocol.js +15 -7
- package/dist/fs/publication-attachment.js +9 -7
- package/dist/fs/service.js +34 -9
- package/dist/fs/shared.js +96 -6
- package/dist/proc/command-stdio.js +1 -1
- package/dist/proc/start-command.js +4 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +11 -0
- package/fs/client.ts +93 -16
- package/fs/dirty-admission.ts +22 -7
- package/fs/guest-watch.ts +1 -1
- package/fs/indexeddb.ts +379 -110
- package/fs/memory.ts +120 -19
- package/fs/multitab-backend.ts +206 -61
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +82 -16
- package/fs/protocol.ts +26 -7
- package/fs/publication-attachment.ts +9 -7
- package/fs/service.ts +35 -7
- package/fs/shared.ts +106 -8
- package/package.json +1 -1
- package/proc/command-stdio.ts +1 -1
- package/proc/start-command.ts +9 -0
- package/testing/idle-workspace-cancellation-browser.ts +22 -13
- package/testing/indexeddb-storage-browser.ts +435 -1
- package/tooling/program/program-transformer.json +1 -1
|
@@ -4,102 +4,195 @@ import {
|
|
|
4
4
|
type NotificationBlockingRequest,
|
|
5
5
|
} from "./notification-blocking-wait.js";
|
|
6
6
|
import type {
|
|
7
|
-
|
|
7
|
+
NotificationRelayControl,
|
|
8
|
+
NotificationRelayNotice,
|
|
8
9
|
NotificationRelayReply,
|
|
9
10
|
NotificationRelayRequest,
|
|
10
11
|
NotificationWord,
|
|
11
12
|
} from "./notification-relay.js";
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
const
|
|
14
|
+
/** A guest sends at most filesystem, network and process words. */
|
|
15
|
+
const MAX_WORDS = 3;
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const pendingWaits: (Promise<string> | undefined)[] = [];
|
|
20
|
-
|
|
21
|
-
function publish(state: Int32Array): void {
|
|
22
|
-
if (Atomics.load(state, 1)) return;
|
|
23
|
-
Atomics.add(state, 0, 1);
|
|
24
|
-
Atomics.notify(state, 0);
|
|
17
|
+
interface Park {
|
|
18
|
+
readonly id: number;
|
|
19
|
+
readonly values: readonly number[];
|
|
25
20
|
}
|
|
26
21
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
/**
|
|
23
|
+
* One guest's relay state. The relay waits on source words only while the guest is parked:
|
|
24
|
+
* a park names the word values the guest last observed, the first difference (or wake on a
|
|
25
|
+
* word) publishes once, and the relay stops until the guest parks again. Source traffic while
|
|
26
|
+
* the guest runs costs the relay nothing.
|
|
27
|
+
*
|
|
28
|
+
* The guest stores the park's id in `state[2]` before its last readiness check and clears it
|
|
29
|
+
* when its blocking call returns. A publication consumes that id with a compare-exchange, so
|
|
30
|
+
* a park the guest retired (a timeout or another wake) never publishes.
|
|
31
|
+
*/
|
|
32
|
+
class RelayConnection {
|
|
33
|
+
private words: readonly NotificationWord[] | undefined;
|
|
34
|
+
private parked: Park | undefined;
|
|
35
|
+
/** The value each outstanding blocking wait was registered with, by word slot. */
|
|
36
|
+
private readonly outstanding: (number | undefined)[] = [];
|
|
37
|
+
/** Pending waits stay reachable from the realm; iOS Safari 26.5 lost unreferenced waits. */
|
|
38
|
+
readonly pendingWaits: (Promise<string> | undefined)[] = [];
|
|
39
|
+
private readonly waiters = createNotificationWaiters(import.meta.url);
|
|
40
|
+
private closed = false;
|
|
41
|
+
private failed = false;
|
|
30
42
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
43
|
+
constructor(
|
|
44
|
+
private readonly id: number,
|
|
45
|
+
private readonly port: MessagePort,
|
|
46
|
+
private readonly state: Int32Array,
|
|
47
|
+
) {
|
|
48
|
+
// The relay is shared by every guest on the page: one guest's malformed message fails only
|
|
49
|
+
// its own connection, never the relay Worker.
|
|
50
|
+
port.onmessage = ({ data }: MessageEvent<NotificationRelayRequest>) => {
|
|
51
|
+
try {
|
|
52
|
+
this.receive(data);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
this.fail(error);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
port.start();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private receive(data: NotificationRelayRequest): void {
|
|
61
|
+
if (this.closed || this.failed) return;
|
|
62
|
+
if (!data || typeof data !== "object") throw new TypeError("malformed notification request");
|
|
63
|
+
if (data.type === "dispose") return this.dispose(true);
|
|
64
|
+
if (Atomics.load(this.state, 1)) return;
|
|
65
|
+
if (data.type === "park") return this.park(data);
|
|
66
|
+
if (data.type !== "arm" || this.words) throw new TypeError("unexpected notification request");
|
|
41
67
|
try {
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
68
|
+
this.arm(data.words);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
this.reply({ type: "error", message: String(error) });
|
|
45
71
|
}
|
|
46
|
-
seen = Atomics.load(word.cells, word.index);
|
|
47
|
-
publish(state);
|
|
48
72
|
}
|
|
49
|
-
}
|
|
50
73
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
74
|
+
private arm(words: readonly NotificationWord[]): void {
|
|
75
|
+
if (words.length > MAX_WORDS) throw new RangeError("too many notification words");
|
|
76
|
+
for (const word of words) Atomics.load(word.cells, word.index);
|
|
77
|
+
this.words = words;
|
|
78
|
+
this.reply({ type: "armed" });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private park({ id, values }: { readonly id: unknown; readonly values: unknown }): void {
|
|
82
|
+
const words = this.words;
|
|
83
|
+
if (!words) throw new TypeError("notification park before arm");
|
|
84
|
+
if (typeof id !== "number" || (id | 0) !== id || id <= 0 || !validValues(values, words.length))
|
|
85
|
+
throw new TypeError("malformed notification park");
|
|
86
|
+
this.parked = { id, values };
|
|
87
|
+
if (this.moved(this.parked)) return this.publish();
|
|
88
|
+
for (let slot = 0; slot < words.length; slot++) this.watch(slot);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private moved(parked: Park): boolean {
|
|
92
|
+
const words = this.words!;
|
|
93
|
+
for (let slot = 0; slot < words.length; slot++)
|
|
94
|
+
if (Atomics.load(words[slot]!.cells, words[slot]!.index) !== parked.values[slot]) return true;
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Registers one blocking wait for the parked value unless one is outstanding. A stale
|
|
99
|
+
* outstanding wait returns soon, because its word already differs, and `settle` rechecks. */
|
|
100
|
+
private watch(slot: number): void {
|
|
101
|
+
const parked = this.parked;
|
|
102
|
+
if (!parked || this.outstanding[slot] !== undefined) return;
|
|
103
|
+
const word = this.words![slot]!;
|
|
104
|
+
const seen = parked.values[slot]!;
|
|
105
|
+
const waiting = this.waiters.wait(word.cells, word.index, seen);
|
|
106
|
+
if (!waiting.async) return this.publish();
|
|
107
|
+
this.outstanding[slot] = seen;
|
|
108
|
+
this.pendingWaits[slot] = waiting.value;
|
|
109
|
+
waiting.value.then(
|
|
110
|
+
() => this.settle(slot, seen),
|
|
111
|
+
(error: unknown) => this.fail(error),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private settle(slot: number, seen: number): void {
|
|
116
|
+
// Disposal cleared these roots; a late reply must not recreate the slots.
|
|
117
|
+
if (this.closed) return;
|
|
118
|
+
this.outstanding[slot] = undefined;
|
|
119
|
+
this.pendingWaits[slot] = undefined;
|
|
120
|
+
const parked = this.parked;
|
|
121
|
+
if (!parked) return;
|
|
122
|
+
const word = this.words![slot]!;
|
|
123
|
+
// A wake on the parked value publishes even without a change, as a notify asks.
|
|
124
|
+
if (
|
|
125
|
+
seen === parked.values[slot] ||
|
|
126
|
+
Atomics.load(word.cells, word.index) !== parked.values[slot]
|
|
127
|
+
)
|
|
128
|
+
return this.publish();
|
|
129
|
+
this.watch(slot);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private publish(): void {
|
|
133
|
+
const parked = this.parked!;
|
|
134
|
+
this.parked = undefined;
|
|
135
|
+
if (Atomics.load(this.state, 1)) return;
|
|
136
|
+
// Every wait attempt has a fresh id that the guest never reactivates. A failed claim means
|
|
137
|
+
// this id is dead: the guest retired it and is running, or a newer park's message carries
|
|
138
|
+
// the current one. Either way that guest is not asleep on this park.
|
|
139
|
+
if (Atomics.compareExchange(this.state, 2, parked.id, 0) !== parked.id) return;
|
|
140
|
+
Atomics.add(this.state, 0, 1);
|
|
141
|
+
Atomics.notify(this.state, 0);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private reply(value: NotificationRelayReply): void {
|
|
145
|
+
this.port.postMessage(value);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Reports the failure to the guest and the page; the page fences and drops the connection. */
|
|
149
|
+
private fail(error: unknown): void {
|
|
150
|
+
if (this.closed || this.failed) return;
|
|
151
|
+
this.failed = true;
|
|
152
|
+
this.parked = undefined;
|
|
153
|
+
this.reply({ type: "error", message: String(error) });
|
|
154
|
+
notice(this.id, "failed");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Stops waiting and publishing for this guest. Disposal by the guest also tells the page. */
|
|
158
|
+
dispose(fromGuest = true): void {
|
|
159
|
+
if (this.closed) return;
|
|
160
|
+
this.closed = true;
|
|
161
|
+
this.parked = undefined;
|
|
162
|
+
this.waiters.dispose();
|
|
163
|
+
this.outstanding.length = 0;
|
|
164
|
+
this.pendingWaits.length = 0;
|
|
165
|
+
this.port.close();
|
|
166
|
+
connections.delete(this.id);
|
|
167
|
+
if (fromGuest) notice(this.id, "disposed");
|
|
64
168
|
}
|
|
65
169
|
}
|
|
66
170
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
171
|
+
/** Checks every expected index; `every()` would skip the holes of a sparse array. */
|
|
172
|
+
function validValues(values: unknown, length: number): values is readonly number[] {
|
|
173
|
+
if (!Array.isArray(values) || values.length !== length) return false;
|
|
174
|
+
for (let index = 0; index < length; index++) {
|
|
175
|
+
const value: unknown = values[index];
|
|
176
|
+
if (typeof value !== "number" || (value | 0) !== value) return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
75
179
|
}
|
|
76
180
|
|
|
77
|
-
function
|
|
78
|
-
|
|
79
|
-
const reply = (value: NotificationRelayReply) => port.postMessage(value);
|
|
80
|
-
port.onmessage = ({ data }: MessageEvent<NotificationRelayRequest>) => {
|
|
81
|
-
if (data.type === "dispose") {
|
|
82
|
-
waiters.dispose();
|
|
83
|
-
observers.length = 0;
|
|
84
|
-
pendingWaits.length = 0;
|
|
85
|
-
port.close();
|
|
86
|
-
globalThis.postMessage("disposed");
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
if (armed || Atomics.load(state, 1)) return;
|
|
90
|
-
armed = true;
|
|
91
|
-
try {
|
|
92
|
-
arm(data.words, state, reply);
|
|
93
|
-
} catch (error) {
|
|
94
|
-
reply({ type: "error", message: String(error) });
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
port.start();
|
|
181
|
+
function notice(id: number, type: NotificationRelayNotice["type"]): void {
|
|
182
|
+
globalThis.postMessage({ id, type } satisfies NotificationRelayNotice);
|
|
98
183
|
}
|
|
99
184
|
|
|
185
|
+
// One relay serves every guest bridge on the page. Connections stay reachable until disposed,
|
|
186
|
+
// together with their pending waits.
|
|
187
|
+
const connections = new Map<number, RelayConnection>();
|
|
188
|
+
|
|
100
189
|
globalThis.onmessage = ({
|
|
101
190
|
data,
|
|
102
|
-
}: MessageEvent<
|
|
103
|
-
if ("type" in data) notificationBlockingWait(data);
|
|
104
|
-
|
|
191
|
+
}: MessageEvent<NotificationRelayControl | NotificationBlockingRequest>) => {
|
|
192
|
+
if ("type" in data) return notificationBlockingWait(data);
|
|
193
|
+
if ("drop" in data) {
|
|
194
|
+
connections.get(data.drop)?.dispose(false);
|
|
195
|
+
return notice(data.drop, "dropped");
|
|
196
|
+
}
|
|
197
|
+
connections.set(data.id, new RelayConnection(data.id, data.port, data.state));
|
|
105
198
|
};
|
package/browser/preview.ts
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
ConnectionSink,
|
|
3
|
+
PreviewConnection,
|
|
4
|
+
PreviewRequestOptions,
|
|
5
|
+
PreviewSink,
|
|
6
|
+
} from "./net/helper.js";
|
|
2
7
|
import type { PreviewRequest } from "./net/http1.js";
|
|
3
8
|
import type { PreviewMessage, PreviewReply } from "./preview-protocol.js";
|
|
4
9
|
import type { PreviewHostOptions, PreviewHostRelay } from "./preview-host.js";
|
|
5
10
|
|
|
11
|
+
export type { PreviewRequestOptions };
|
|
12
|
+
|
|
6
13
|
/** A server transport, independent of the guest engine or worker owning it. */
|
|
7
14
|
export interface PreviewServer {
|
|
8
|
-
|
|
15
|
+
/** Aborting `options.signal` ends this request on the server's side as well as the sink's. */
|
|
16
|
+
request(
|
|
17
|
+
port: number,
|
|
18
|
+
request: PreviewRequest,
|
|
19
|
+
sink: PreviewSink,
|
|
20
|
+
options?: PreviewRequestOptions,
|
|
21
|
+
): boolean;
|
|
9
22
|
connect(port: number, sink: ConnectionSink): PreviewConnection | null;
|
|
10
23
|
}
|
|
11
24
|
|
package/browser/run-command.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { allocateSharedControl } from "../memory/control.js";
|
|
2
2
|
import { commandTransferBytes } from "../proc/command-transfer.js";
|
|
3
|
-
import { DescriptorTable } from "../core/descriptors.js";
|
|
3
|
+
import { DescriptorTable, type Stream } from "../core/descriptors.js";
|
|
4
4
|
import type { Runtime } from "../runtime.js";
|
|
5
5
|
import { commandDisposals, commandOwners } from "./command-owner.js";
|
|
6
6
|
import { controlLoad } from "../proc/control.js";
|
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
type WorkerSpawnTransfer,
|
|
22
22
|
} from "../proc/worker-client.js";
|
|
23
23
|
import type { BrowserProcessBroker } from "./process-broker.js";
|
|
24
|
-
import { createRuntimeTerminalCodec } from "./runtime-terminals.js";
|
|
24
|
+
import { createRuntimeTerminalCodec, hostPipeDestination } from "./runtime-terminals.js";
|
|
25
25
|
import { CommandInput } from "./command-input.js";
|
|
26
26
|
import { yieldTask } from "./yield-task.js";
|
|
27
27
|
|
|
@@ -328,18 +328,24 @@ class Command {
|
|
|
328
328
|
continue;
|
|
329
329
|
this.descriptors.openAt(
|
|
330
330
|
fd,
|
|
331
|
-
|
|
332
|
-
kind: "terminal",
|
|
333
|
-
read: () => 0,
|
|
334
|
-
write: () => 0,
|
|
335
|
-
readiness: () => ({ readable: fd === 0, writable: fd !== 0, hangup: fd === 0 }),
|
|
336
|
-
},
|
|
331
|
+
this.hostStdio(fd),
|
|
337
332
|
(fd === 0 ? READ : WRITE) | POLL | FD_FILESTAT_GET,
|
|
338
333
|
0n,
|
|
339
334
|
);
|
|
340
335
|
}
|
|
341
336
|
}
|
|
342
337
|
|
|
338
|
+
/** Captured stdio is a pipe, as on Unix, unless the caller asked for a terminal. */
|
|
339
|
+
private hostStdio(fd: 0 | 1 | 2): Stream {
|
|
340
|
+
const stream: Stream = {
|
|
341
|
+
kind: this.options.terminal ? "terminal" : "pipe",
|
|
342
|
+
read: () => 0,
|
|
343
|
+
write: () => 0,
|
|
344
|
+
readiness: () => ({ readable: fd === 0, writable: fd !== 0, hangup: fd === 0 }),
|
|
345
|
+
};
|
|
346
|
+
return this.options.terminal ? stream : hostPipeDestination(fd, stream);
|
|
347
|
+
}
|
|
348
|
+
|
|
343
349
|
private deliver(fd: 1 | 2, bytes: Uint8Array): number {
|
|
344
350
|
if (this.outputBatch === 0) {
|
|
345
351
|
this.outputTask ??= yieldTask().then(() => {
|
|
@@ -11,6 +11,8 @@ type TerminalTarget = 0 | 1 | 2;
|
|
|
11
11
|
interface TerminalToken {
|
|
12
12
|
kind: "web-terminal";
|
|
13
13
|
target: TerminalTarget;
|
|
14
|
+
/** The destination is presented to guests as a pipe, as captured output is on Unix. */
|
|
15
|
+
pipe?: true;
|
|
14
16
|
}
|
|
15
17
|
interface NullDeviceToken {
|
|
16
18
|
kind: "null-device";
|
|
@@ -19,7 +21,18 @@ interface NullDeviceToken {
|
|
|
19
21
|
// A remapped fd is not a new destination. Imported terminals retain their provenance across
|
|
20
22
|
// codec instances and later generations; WeakMap entries disappear with their local streams.
|
|
21
23
|
const targets = new WeakMapConstructor<Stream, TerminalTarget>();
|
|
24
|
+
// Host destinations a guest sees as pipes. They keep their target, so their writes still reach
|
|
25
|
+
// the same host output, but fd_kind and fdstat report a pipe and isatty is false.
|
|
26
|
+
const pipeDestinations = new WeakMapConstructor<Stream, true>();
|
|
22
27
|
const NOSYS = 52;
|
|
28
|
+
|
|
29
|
+
/** A pipe-kind stream at fd `target` that a codec exports as that host destination. */
|
|
30
|
+
export function hostPipeDestination(target: TerminalTarget, stream: Stream): Stream {
|
|
31
|
+
if (stream.kind !== "pipe") throw new TypeError("host pipe destination must be a pipe");
|
|
32
|
+
weakMapSet(targets, stream, target);
|
|
33
|
+
weakMapSet(pipeDestinations, stream, true);
|
|
34
|
+
return stream;
|
|
35
|
+
}
|
|
23
36
|
export interface RuntimeTerminalCodecOptions {
|
|
24
37
|
fallback: WorkerDescriptorCodec;
|
|
25
38
|
/** Initial default stdio only. Existing imported provenance wins over the current fd number. */
|
|
@@ -56,6 +69,7 @@ function terminalToken(value: unknown): TerminalToken | undefined {
|
|
|
56
69
|
return;
|
|
57
70
|
if (!("target" in value) || (value.target !== 0 && value.target !== 1 && value.target !== 2))
|
|
58
71
|
throw new TypeError("invalid browser terminal target");
|
|
72
|
+
if ("pipe" in value && value.pipe !== true) throw new TypeError("invalid browser terminal kind");
|
|
59
73
|
return value as TerminalToken;
|
|
60
74
|
}
|
|
61
75
|
function resolveTerminal(
|
|
@@ -95,6 +109,8 @@ export function createRuntimeTerminalCodec(
|
|
|
95
109
|
// transfers. A receiving executor may resolve that destination to an interactive stream.
|
|
96
110
|
if (target === undefined)
|
|
97
111
|
throw new ProcessError(NOSYS, "terminal has no transferable browser destination");
|
|
112
|
+
if (weakMapHas(pipeDestinations, resource))
|
|
113
|
+
return { kind: "web-terminal", target, pipe: true } satisfies TerminalToken;
|
|
98
114
|
return { kind: "web-terminal", target } satisfies TerminalToken;
|
|
99
115
|
},
|
|
100
116
|
import(value, pid) {
|
|
@@ -102,6 +118,16 @@ export function createRuntimeTerminalCodec(
|
|
|
102
118
|
if (nullDevice) return new OpenDescription(createNullDevice(nullDevice.fdflags));
|
|
103
119
|
const token = terminalToken(value);
|
|
104
120
|
if (!token) return options.fallback.import(value, pid);
|
|
121
|
+
// A pipe destination is never an interactive terminal, so it never resolves to one.
|
|
122
|
+
if (token.pipe) {
|
|
123
|
+
const stream = hostTerminalStream(
|
|
124
|
+
token.target,
|
|
125
|
+
options.output,
|
|
126
|
+
options.checkActive,
|
|
127
|
+
"pipe",
|
|
128
|
+
);
|
|
129
|
+
return new OpenDescription(hostPipeDestination(token.target, stream));
|
|
130
|
+
}
|
|
105
131
|
const resolved = resolveTerminal(options, token.target);
|
|
106
132
|
if (resolved !== undefined) {
|
|
107
133
|
const remember = (stream: Stream) => {
|
|
@@ -458,10 +458,12 @@ export function deliveredTerminalOutput(deliver: TerminalOutputDelivery): Runtim
|
|
|
458
458
|
}
|
|
459
459
|
|
|
460
460
|
/** A host terminal description. Without `output`, writes fail with NOSYS. */
|
|
461
|
+
/** A host destination. `kind: "pipe"` presents it to the guest as a pipe rather than a TTY. */
|
|
461
462
|
export function hostTerminalStream(
|
|
462
463
|
target: 0 | 1 | 2,
|
|
463
464
|
output: RuntimeTerminalOutput | undefined,
|
|
464
465
|
checkActive?: () => void,
|
|
466
|
+
kind: "terminal" | "pipe" = "terminal",
|
|
465
467
|
): Stream {
|
|
466
468
|
const destination = target === 2 ? 2 : 1;
|
|
467
469
|
const read = () => {
|
|
@@ -477,7 +479,7 @@ export function hostTerminalStream(
|
|
|
477
479
|
: output.tryWrite(destination, chunks);
|
|
478
480
|
};
|
|
479
481
|
return {
|
|
480
|
-
kind
|
|
482
|
+
kind,
|
|
481
483
|
read,
|
|
482
484
|
tryRead: read,
|
|
483
485
|
write: (chunks) => send(true, chunks),
|
package/core/filesystem.ts
CHANGED
|
@@ -107,6 +107,10 @@ export interface FileSystem {
|
|
|
107
107
|
followFinal?: boolean,
|
|
108
108
|
rooted?: boolean,
|
|
109
109
|
): ResolvedPath | number;
|
|
110
|
+
/** realpath(3) in one lookup: `path` from `directory` with every link followed. Rooted, an
|
|
111
|
+
* absolute path from the root; otherwise relative to `directory` ("." for itself). Positive
|
|
112
|
+
* WASI errno on failure. */
|
|
113
|
+
realpath(directory: FileHandle, path: string, rooted?: boolean): string | number;
|
|
110
114
|
symlink(path: ResolvedPath, target: string): number;
|
|
111
115
|
readlink(handle: FileHandle): string;
|
|
112
116
|
stat(handle: FileHandle): FileStat;
|
package/core/wasi.ts
CHANGED
|
@@ -187,6 +187,17 @@ function lookupFlags(flags: number): FinalLookup {
|
|
|
187
187
|
return flags & 1 ? "follow" : "nofollow";
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
+
/** A guest pathname exactly as its bytes spell it (a leading U+FEFF kept), or undefined for
|
|
191
|
+
* malformed UTF-8 or a NUL. */
|
|
192
|
+
function guestPathname(bytes: Uint8Array, ptr: number, len: number): string | undefined {
|
|
193
|
+
try {
|
|
194
|
+
const path = decodeBytes(bytes, ptr, len, fatalDecoder);
|
|
195
|
+
return path.includes("\0") ? undefined : path;
|
|
196
|
+
} catch {
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
190
201
|
function writeUtf8Prefix(target: string, output: Uint8Array): number {
|
|
191
202
|
if (output.length === 0) return 0;
|
|
192
203
|
// Browser encodeInto rejects resizable backing buffers, including fixed-length views.
|
|
@@ -483,6 +494,7 @@ export class Wasi {
|
|
|
483
494
|
const impl: Record<string, (...args: never[]) => number | void> = this.functions();
|
|
484
495
|
const filesystem: Record<string, (...args: never[]) => number> = {
|
|
485
496
|
path_permissions_get: this.pathPermissions.bind(this),
|
|
497
|
+
path_realpath: this.pathRealpath.bind(this),
|
|
486
498
|
watch_open: this.watchOpen.bind(this),
|
|
487
499
|
watch_ready: () => (this.watches?.ready() ? 1 : 0),
|
|
488
500
|
watch_read: this.watchRead.bind(this),
|
|
@@ -702,6 +714,43 @@ export class Wasi {
|
|
|
702
714
|
return SUCCESS;
|
|
703
715
|
}
|
|
704
716
|
|
|
717
|
+
/** runtime_fs.path_realpath: realpath(3) of `path` from `fd` in one filesystem request. Writes
|
|
718
|
+
* the UTF-8 result's length to `used`; a result longer than `capacity` is ENOBUFS, with
|
|
719
|
+
* nothing written to `buf`. See docs/host-abi.md. */
|
|
720
|
+
private pathRealpath(
|
|
721
|
+
fd: number,
|
|
722
|
+
ptr: number,
|
|
723
|
+
len: number,
|
|
724
|
+
buf: number,
|
|
725
|
+
capacity: number,
|
|
726
|
+
used: number,
|
|
727
|
+
): number {
|
|
728
|
+
ptr >>>= 0;
|
|
729
|
+
len >>>= 0;
|
|
730
|
+
buf >>>= 0;
|
|
731
|
+
capacity >>>= 0;
|
|
732
|
+
used >>>= 0;
|
|
733
|
+
const bytes = this.bytes();
|
|
734
|
+
if (ptr + len > bytes.length || buf + capacity > bytes.length || used + 4 > bytes.length)
|
|
735
|
+
return EFAULT;
|
|
736
|
+
// Either result would clobber the other.
|
|
737
|
+
if (capacity > 0 && buf < used + 4 && used < buf + capacity) return EINVAL;
|
|
738
|
+
const path = guestPathname(bytes, ptr, len);
|
|
739
|
+
if (path === undefined) return EINVAL;
|
|
740
|
+
const entry = this.descriptors.get(fd >>> 0);
|
|
741
|
+
if (!entry) return EBADF;
|
|
742
|
+
if (entry.resource.kind !== "directory") return ENOTDIR;
|
|
743
|
+
if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET)) return ENOTCAPABLE;
|
|
744
|
+
const real = this.opts.fs.realpath(entry.resource.handle, path, this.rooted);
|
|
745
|
+
if (typeof real === "number") return real;
|
|
746
|
+
const encoded = encoder.encode(real);
|
|
747
|
+
// Reacquire after the service round trip; another thread may have grown shared memory.
|
|
748
|
+
this.view().setUint32(used, encoded.length, true);
|
|
749
|
+
if (encoded.length > capacity) return ENOBUFS;
|
|
750
|
+
this.memory.range(buf, encoded.length).set(encoded);
|
|
751
|
+
return SUCCESS;
|
|
752
|
+
}
|
|
753
|
+
|
|
705
754
|
private pathPermissions(
|
|
706
755
|
fd: number,
|
|
707
756
|
ptr: number,
|
|
@@ -177,16 +177,54 @@ export class NetHelper {
|
|
|
177
177
|
return id;
|
|
178
178
|
return;
|
|
179
179
|
}
|
|
180
|
-
request(port, req, sink) {
|
|
181
|
-
|
|
180
|
+
request(port, req, sink, options = {}) {
|
|
181
|
+
const signal = options.signal;
|
|
182
|
+
if (!signal)
|
|
183
|
+
return this.requestConnection(port, req, sink) !== null;
|
|
184
|
+
const captured = snapshotPreviewRequest(req);
|
|
185
|
+
const target = snapshotSink(sink);
|
|
186
|
+
if (signal.aborted)
|
|
187
|
+
return this.live() && this.namespace.has(port);
|
|
188
|
+
let connection = null;
|
|
189
|
+
let abandoned = false;
|
|
190
|
+
const abort = () => {
|
|
191
|
+
abandoned = true;
|
|
192
|
+
connection?.cancel();
|
|
193
|
+
};
|
|
194
|
+
const settled = () => signal.removeEventListener("abort", abort);
|
|
195
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
196
|
+
try {
|
|
197
|
+
connection = this.requestConnection(port, captured, {
|
|
198
|
+
head: (head) => {
|
|
199
|
+
if (!abandoned)
|
|
200
|
+
target.head(head);
|
|
201
|
+
},
|
|
202
|
+
data: (chunk) => abandoned ? undefined : target.data(chunk),
|
|
203
|
+
end: () => {
|
|
204
|
+
settled();
|
|
205
|
+
if (!abandoned)
|
|
206
|
+
target.end();
|
|
207
|
+
},
|
|
208
|
+
error: (message) => {
|
|
209
|
+
settled();
|
|
210
|
+
if (!abandoned)
|
|
211
|
+
target.error(message);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
} catch (error) {
|
|
215
|
+
settled();
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
if (!connection) {
|
|
219
|
+
settled();
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
if (abandoned)
|
|
223
|
+
connection.cancel();
|
|
224
|
+
return true;
|
|
182
225
|
}
|
|
183
226
|
requestConnection(port, req, sink, host) {
|
|
184
|
-
const captured =
|
|
185
|
-
method: req.method,
|
|
186
|
-
target: req.target,
|
|
187
|
-
headers: req.headers,
|
|
188
|
-
body: req.body
|
|
189
|
-
};
|
|
227
|
+
const captured = snapshotPreviewRequest(req);
|
|
190
228
|
const request = serializeRequest(captured, `localhost:${port}`);
|
|
191
229
|
const isHead = captured.method === "HEAD";
|
|
192
230
|
const sinkHead = sink.head.bind(sink), data = sink.data.bind(sink);
|
|
@@ -217,7 +255,7 @@ export class NetHelper {
|
|
|
217
255
|
}
|
|
218
256
|
};
|
|
219
257
|
const parser = new ResponseParser(finish, isHead);
|
|
220
|
-
connection = this.
|
|
258
|
+
connection = this.openConnection(port, {
|
|
221
259
|
data: (bytes) => {
|
|
222
260
|
parser.push(bytes);
|
|
223
261
|
return delivered;
|
|
@@ -263,6 +301,12 @@ export class NetHelper {
|
|
|
263
301
|
await this.ring.write(0, Kind.FileEnd);
|
|
264
302
|
}
|
|
265
303
|
connect(port, sink, host) {
|
|
304
|
+
const connection = this.openConnection(port, sink, host);
|
|
305
|
+
if (!connection)
|
|
306
|
+
return null;
|
|
307
|
+
return { write: (bytes) => connection.write(bytes), close: () => connection.close() };
|
|
308
|
+
}
|
|
309
|
+
openConnection(port, sink, host) {
|
|
266
310
|
const captured = {
|
|
267
311
|
data: sink.data.bind(sink),
|
|
268
312
|
end: sink.end.bind(sink),
|
|
@@ -271,8 +315,10 @@ export class NetHelper {
|
|
|
271
315
|
const listener = this.listenerFor(port);
|
|
272
316
|
if (!this.live())
|
|
273
317
|
return null;
|
|
274
|
-
if (listener === undefined || this.sharedListeners.has(listener))
|
|
275
|
-
|
|
318
|
+
if (listener === undefined || this.sharedListeners.has(listener)) {
|
|
319
|
+
const socket = this.connectNamespace(port, captured, host);
|
|
320
|
+
return socket && { ...socket, cancel: () => socket.close() };
|
|
321
|
+
}
|
|
276
322
|
sink = captured;
|
|
277
323
|
const bind = this.namespace.address(port);
|
|
278
324
|
if (!bind)
|
|
@@ -293,17 +339,24 @@ export class NetHelper {
|
|
|
293
339
|
local
|
|
294
340
|
};
|
|
295
341
|
let sending = this.ring.write(listener, Kind.Conn, json(announce));
|
|
342
|
+
let cancelled = false;
|
|
343
|
+
const unsent = () => !cancelled;
|
|
344
|
+
const close = () => {
|
|
345
|
+
if (!this.connections.delete(conn))
|
|
346
|
+
return;
|
|
347
|
+
sending = sending.then(() => this.ring.write(conn, Kind.End));
|
|
348
|
+
};
|
|
296
349
|
return {
|
|
297
350
|
write: (bytes) => {
|
|
298
351
|
if (!this.connections.has(conn))
|
|
299
352
|
return;
|
|
300
353
|
const owned = bytes.slice();
|
|
301
|
-
sending = sending.then(() => this.writeChunks(conn, owned));
|
|
354
|
+
sending = sending.then(() => this.writeChunks(conn, owned, unsent));
|
|
302
355
|
},
|
|
303
|
-
close
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
356
|
+
close,
|
|
357
|
+
cancel: () => {
|
|
358
|
+
cancelled = true;
|
|
359
|
+
close();
|
|
307
360
|
}
|
|
308
361
|
};
|
|
309
362
|
}
|
|
@@ -913,13 +966,25 @@ export class NetHelper {
|
|
|
913
966
|
}
|
|
914
967
|
}
|
|
915
968
|
async writeChunks(handle, bytes, live) {
|
|
969
|
+
const dropped = live && (() => !live());
|
|
916
970
|
for (let at = 0;at < bytes.length; at += MAX_RECORD) {
|
|
917
|
-
if (
|
|
971
|
+
if (dropped?.())
|
|
918
972
|
return;
|
|
919
|
-
await this.ring.write(handle, Kind.Data, bytes.subarray(at, at + MAX_RECORD));
|
|
973
|
+
await this.ring.write(handle, Kind.Data, bytes.subarray(at, at + MAX_RECORD), dropped);
|
|
920
974
|
}
|
|
921
975
|
}
|
|
922
976
|
}
|
|
977
|
+
function snapshotPreviewRequest(req) {
|
|
978
|
+
return { method: req.method, target: req.target, headers: req.headers, body: req.body };
|
|
979
|
+
}
|
|
980
|
+
function snapshotSink(sink) {
|
|
981
|
+
return {
|
|
982
|
+
head: sink.head.bind(sink),
|
|
983
|
+
data: sink.data.bind(sink),
|
|
984
|
+
end: sink.end.bind(sink),
|
|
985
|
+
error: sink.error.bind(sink)
|
|
986
|
+
};
|
|
987
|
+
}
|
|
923
988
|
function snapshotRequest(req) {
|
|
924
989
|
const { type, id } = req;
|
|
925
990
|
switch (type) {
|