@waffle-os/runtime 0.0.153 → 0.0.155
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/core/filesystem.ts +18 -0
- package/core/wasi.ts +75 -4
- 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/core/wasi.js +62 -4
- package/dist/fs/backend.js +7 -0
- package/dist/fs/client.js +75 -17
- package/dist/fs/dirty-admission.js +15 -8
- package/dist/fs/guest-watch.js +267 -94
- package/dist/fs/indexeddb-chunks.js +88 -11
- package/dist/fs/indexeddb.js +366 -110
- package/dist/fs/memory.js +106 -20
- package/dist/fs/multitab-backend.js +291 -74
- 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 +62 -9
- package/dist/fs/shared.js +100 -6
- package/dist/fs/storage.js +107 -8
- package/dist/fs/watch-service.js +97 -2
- package/dist/fs/watch-subscriptions.js +121 -8
- package/dist/proc/command-stdio.js +1 -1
- package/dist/proc/intrinsics/core.js +5 -0
- package/dist/proc/intrinsics.js +1 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +17 -0
- package/fs/client.ts +99 -16
- package/fs/dirty-admission.ts +22 -7
- package/fs/guest-watch.ts +299 -103
- package/fs/indexeddb-chunks.ts +103 -9
- package/fs/indexeddb.ts +477 -121
- package/fs/memory.ts +120 -19
- package/fs/multitab-backend.ts +440 -95
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +88 -16
- package/fs/protocol.ts +26 -7
- package/fs/publication-attachment.ts +9 -7
- package/fs/service.ts +62 -7
- package/fs/shared.ts +110 -8
- package/fs/storage.ts +128 -10
- package/fs/watch-service.ts +110 -3
- package/fs/watch-subscriptions.ts +115 -7
- package/package.json +1 -1
- package/proc/command-stdio.ts +1 -1
- package/proc/intrinsics/core.ts +7 -0
- package/proc/intrinsics.ts +1 -0
- package/testing/idle-workspace-cancellation-browser.ts +22 -13
- package/testing/indexeddb-storage-browser.ts +564 -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/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;
|
|
@@ -150,6 +154,16 @@ export interface FileSystem {
|
|
|
150
154
|
readonly watches?: WatchFileSystem;
|
|
151
155
|
}
|
|
152
156
|
|
|
157
|
+
/** One ready subscription returned by a batched owner drain. */
|
|
158
|
+
export type WatchDrainEntry =
|
|
159
|
+
| { readonly id: number; readonly batch: WatchBatch }
|
|
160
|
+
| { readonly id: number; readonly errno: number };
|
|
161
|
+
|
|
162
|
+
export interface WatchDrainResult {
|
|
163
|
+
readonly entries: readonly WatchDrainEntry[];
|
|
164
|
+
readonly more: boolean;
|
|
165
|
+
}
|
|
166
|
+
|
|
153
167
|
/** The opener's filesystem session owns each subscription; session close releases them all. */
|
|
154
168
|
export interface WatchFileSystem {
|
|
155
169
|
/** The root's watch cells. Index 3 counts committed watch records. */
|
|
@@ -157,8 +171,12 @@ export interface WatchFileSystem {
|
|
|
157
171
|
readonly revoked: boolean;
|
|
158
172
|
/** True when watches are served remotely (attach mode); disables local-generation short-circuit. */
|
|
159
173
|
readonly remote?: boolean;
|
|
174
|
+
/** True when `drain()` is currently supported by the active backend role. Defaults to `typeof drain === "function"`. */
|
|
175
|
+
readonly canDrain?: boolean;
|
|
160
176
|
/** Throws FilesystemError. */
|
|
161
177
|
open(handle: FileHandle, recursive: boolean): number;
|
|
162
178
|
read(id: number): WatchBatch;
|
|
179
|
+
/** Optional batched drain of ready watches for this session in one round trip. */
|
|
180
|
+
drain?(): WatchDrainResult;
|
|
163
181
|
close(id: number): void;
|
|
164
182
|
}
|
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,9 +494,11 @@ 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),
|
|
501
|
+
watch_next: this.watchNext.bind(this),
|
|
489
502
|
watch_close: (id: number) => this.watches?.close(id >>> 0) ?? EBADF,
|
|
490
503
|
};
|
|
491
504
|
const out: WebAssembly.Imports = Object.create(null);
|
|
@@ -678,16 +691,37 @@ export class Wasi {
|
|
|
678
691
|
}
|
|
679
692
|
|
|
680
693
|
private watchRead(id: number, buf: number, len: number, out: number): number {
|
|
694
|
+
return this.writeWatch(id >>> 0, buf, len, out, -1);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
private watchNext(outId: number, buf: number, len: number, out: number): number {
|
|
698
|
+
return this.writeWatch(-1, buf, len, out, outId >>> 0);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
private writeWatch(id: number, buf: number, len: number, out: number, outId: number): number {
|
|
681
702
|
buf >>>= 0;
|
|
682
703
|
len >>>= 0;
|
|
683
704
|
out >>>= 0;
|
|
684
705
|
const size = this.bytes().length;
|
|
685
|
-
if (buf + len > size || out + 4 > size) return EFAULT;
|
|
686
|
-
|
|
687
|
-
|
|
706
|
+
if ((outId >= 0 && outId + 4 > size) || buf + len > size || out + 4 > size) return EFAULT;
|
|
707
|
+
if (
|
|
708
|
+
(len > 0 && buf < out + 4 && out < buf + len) ||
|
|
709
|
+
(outId >= 0 &&
|
|
710
|
+
((outId < out + 4 && out < outId + 4) || (len > 0 && buf < outId + 4 && outId < buf + len)))
|
|
711
|
+
)
|
|
712
|
+
return EINVAL;
|
|
713
|
+
if (outId >= 0) this.view().setUint32(outId, 0, true);
|
|
714
|
+
const result = id >= 0 ? this.watches?.read(id, len) : this.watches?.readNext(len);
|
|
715
|
+
if (!result) return id >= 0 ? EBADF : 6;
|
|
688
716
|
if (result.kind === "error") return result.errno;
|
|
717
|
+
const view = this.view();
|
|
718
|
+
if (outId >= 0) view.setUint32(outId, "id" in result ? result.id : 0, true);
|
|
719
|
+
if (result.kind === "watch_error") {
|
|
720
|
+
view.setUint32(out, 0, true);
|
|
721
|
+
return result.errno;
|
|
722
|
+
}
|
|
689
723
|
if (result.kind === "short") {
|
|
690
|
-
|
|
724
|
+
view.setUint32(out, result.needed, true);
|
|
691
725
|
return ENOBUFS;
|
|
692
726
|
}
|
|
693
727
|
// Reacquire after the service round trip; another thread may have grown shared memory.
|
|
@@ -702,6 +736,43 @@ export class Wasi {
|
|
|
702
736
|
return SUCCESS;
|
|
703
737
|
}
|
|
704
738
|
|
|
739
|
+
/** runtime_fs.path_realpath: realpath(3) of `path` from `fd` in one filesystem request. Writes
|
|
740
|
+
* the UTF-8 result's length to `used`; a result longer than `capacity` is ENOBUFS, with
|
|
741
|
+
* nothing written to `buf`. See docs/host-abi.md. */
|
|
742
|
+
private pathRealpath(
|
|
743
|
+
fd: number,
|
|
744
|
+
ptr: number,
|
|
745
|
+
len: number,
|
|
746
|
+
buf: number,
|
|
747
|
+
capacity: number,
|
|
748
|
+
used: number,
|
|
749
|
+
): number {
|
|
750
|
+
ptr >>>= 0;
|
|
751
|
+
len >>>= 0;
|
|
752
|
+
buf >>>= 0;
|
|
753
|
+
capacity >>>= 0;
|
|
754
|
+
used >>>= 0;
|
|
755
|
+
const bytes = this.bytes();
|
|
756
|
+
if (ptr + len > bytes.length || buf + capacity > bytes.length || used + 4 > bytes.length)
|
|
757
|
+
return EFAULT;
|
|
758
|
+
// Either result would clobber the other.
|
|
759
|
+
if (capacity > 0 && buf < used + 4 && used < buf + capacity) return EINVAL;
|
|
760
|
+
const path = guestPathname(bytes, ptr, len);
|
|
761
|
+
if (path === undefined) return EINVAL;
|
|
762
|
+
const entry = this.descriptors.get(fd >>> 0);
|
|
763
|
+
if (!entry) return EBADF;
|
|
764
|
+
if (entry.resource.kind !== "directory") return ENOTDIR;
|
|
765
|
+
if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET)) return ENOTCAPABLE;
|
|
766
|
+
const real = this.opts.fs.realpath(entry.resource.handle, path, this.rooted);
|
|
767
|
+
if (typeof real === "number") return real;
|
|
768
|
+
const encoded = encoder.encode(real);
|
|
769
|
+
// Reacquire after the service round trip; another thread may have grown shared memory.
|
|
770
|
+
this.view().setUint32(used, encoded.length, true);
|
|
771
|
+
if (encoded.length > capacity) return ENOBUFS;
|
|
772
|
+
this.memory.range(buf, encoded.length).set(encoded);
|
|
773
|
+
return SUCCESS;
|
|
774
|
+
}
|
|
775
|
+
|
|
705
776
|
private pathPermissions(
|
|
706
777
|
fd: number,
|
|
707
778
|
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) {
|
package/dist/browser/net/ring.js
CHANGED
|
@@ -34,10 +34,10 @@ export class RingWriter extends RingBase {
|
|
|
34
34
|
headerView = new DataView(this.header.buffer);
|
|
35
35
|
queue = Promise.resolve();
|
|
36
36
|
disposed = false;
|
|
37
|
-
write(handle, kind, payload = new Uint8Array) {
|
|
37
|
+
write(handle, kind, payload = new Uint8Array, dropped) {
|
|
38
38
|
if (payload.length > MAX_RECORD)
|
|
39
39
|
return Promise.reject(new RangeError(`ring record too large: ${payload.length}`));
|
|
40
|
-
this.queue = this.queue.then(() => this.append(handle, kind, payload));
|
|
40
|
+
this.queue = this.queue.then(() => this.append(handle, kind, payload, dropped));
|
|
41
41
|
return this.queue;
|
|
42
42
|
}
|
|
43
43
|
dispose() {
|
|
@@ -45,11 +45,12 @@ export class RingWriter extends RingBase {
|
|
|
45
45
|
Atomics.add(this.cells, SPACE, 1);
|
|
46
46
|
Atomics.notify(this.cells, SPACE);
|
|
47
47
|
}
|
|
48
|
-
async append(handle, kind, payload) {
|
|
48
|
+
async append(handle, kind, payload, dropped) {
|
|
49
49
|
const size = align4(RECORD_HEADER + payload.length);
|
|
50
|
-
|
|
50
|
+
const skip = () => this.disposed || dropped?.() === true;
|
|
51
|
+
while (!skip() && CAPACITY - this.used() < size)
|
|
51
52
|
await this.waitForSpace(size);
|
|
52
|
-
if (
|
|
53
|
+
if (skip())
|
|
53
54
|
return;
|
|
54
55
|
this.headerView.setInt32(0, handle, true);
|
|
55
56
|
this.headerView.setInt32(4, kind, true);
|
|
@@ -97,9 +97,9 @@ function project(helper, assertLive, release) {
|
|
|
97
97
|
live();
|
|
98
98
|
return helper.listenerFor(port);
|
|
99
99
|
},
|
|
100
|
-
request(port, request, sink) {
|
|
100
|
+
request(port, request, sink, options) {
|
|
101
101
|
live();
|
|
102
|
-
return helper.request(port, request, sink);
|
|
102
|
+
return helper.request(port, request, sink, options);
|
|
103
103
|
},
|
|
104
104
|
connect(port, sink) {
|
|
105
105
|
live();
|