@waffle-os/runtime 0.0.153 → 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/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/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/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/testing/idle-workspace-cancellation-browser.ts +22 -13
- package/testing/indexeddb-storage-browser.ts +435 -1
- package/tooling/program/program-transformer.json +1 -1
package/browser/net/helper.ts
CHANGED
|
@@ -95,6 +95,11 @@ export interface PreviewConnection {
|
|
|
95
95
|
close(): void;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
/** A connection whose `cancel` ends it without sending what was written but not sent yet. */
|
|
99
|
+
interface CancellableConnection extends PreviewConnection {
|
|
100
|
+
cancel(): void;
|
|
101
|
+
}
|
|
102
|
+
|
|
98
103
|
export interface ConnectionSink {
|
|
99
104
|
/** Completion releases receive credit; an omitted receipt means synchronous consumption. */
|
|
100
105
|
data(bytes: Uint8Array): void | Promise<void>;
|
|
@@ -127,6 +132,12 @@ export interface NetHelperHooks {
|
|
|
127
132
|
/** Streams a preview response back to whoever asked (the service worker, a test). */
|
|
128
133
|
export type PreviewSink = ResponseSink;
|
|
129
134
|
|
|
135
|
+
/** What the caller of a preview request may add to it. */
|
|
136
|
+
export interface PreviewRequestOptions {
|
|
137
|
+
/** Ends this one request: aborting it closes the guest's connection and silences the sink. */
|
|
138
|
+
readonly signal?: AbortSignal;
|
|
139
|
+
}
|
|
140
|
+
|
|
130
141
|
export class NetHelper {
|
|
131
142
|
private ring: RingWriter;
|
|
132
143
|
private fetches = new Map<number, FetchJob>();
|
|
@@ -254,9 +265,59 @@ export class NetHelper {
|
|
|
254
265
|
/**
|
|
255
266
|
* Delivers `req` to the guest server on `port` as a new connection and streams the parsed response
|
|
256
267
|
* into `sink`. Returns false when nothing listens there.
|
|
268
|
+
*
|
|
269
|
+
* Aborting `options.signal` cancels that connection: request bytes not yet sent are dropped,
|
|
270
|
+
* the guest server sees its client go away, and `sink` hears nothing more. A signal that is
|
|
271
|
+
* already aborted once the caller's request has been read sends nothing; the result then says
|
|
272
|
+
* only whether a server in this runtime listens on `port`.
|
|
257
273
|
*/
|
|
258
|
-
request(
|
|
259
|
-
|
|
274
|
+
request(
|
|
275
|
+
port: number,
|
|
276
|
+
req: PreviewRequest,
|
|
277
|
+
sink: PreviewSink,
|
|
278
|
+
options: PreviewRequestOptions = {},
|
|
279
|
+
): boolean {
|
|
280
|
+
const signal = options.signal;
|
|
281
|
+
if (!signal) return this.requestConnection(port, req, sink) !== null;
|
|
282
|
+
// Caller data is read once, before the abort is checked: a getter that aborts counts.
|
|
283
|
+
const captured = snapshotPreviewRequest(req);
|
|
284
|
+
const target = snapshotSink(sink);
|
|
285
|
+
if (signal.aborted) return this.live() && this.namespace.has(port);
|
|
286
|
+
let connection: CancellableConnection | null = null;
|
|
287
|
+
let abandoned = false;
|
|
288
|
+
const abort = () => {
|
|
289
|
+
abandoned = true;
|
|
290
|
+
connection?.cancel();
|
|
291
|
+
};
|
|
292
|
+
const settled = () => signal.removeEventListener("abort", abort);
|
|
293
|
+
// Armed before dispatch, so an abort during it is not missed.
|
|
294
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
295
|
+
try {
|
|
296
|
+
connection = this.requestConnection(port, captured, {
|
|
297
|
+
head: (head) => {
|
|
298
|
+
if (!abandoned) target.head(head);
|
|
299
|
+
},
|
|
300
|
+
data: (chunk) => (abandoned ? undefined : target.data(chunk)),
|
|
301
|
+
end: () => {
|
|
302
|
+
settled();
|
|
303
|
+
if (!abandoned) target.end();
|
|
304
|
+
},
|
|
305
|
+
error: (message) => {
|
|
306
|
+
settled();
|
|
307
|
+
if (!abandoned) target.error(message);
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
} catch (error) {
|
|
311
|
+
settled();
|
|
312
|
+
throw error;
|
|
313
|
+
}
|
|
314
|
+
if (!connection) {
|
|
315
|
+
settled();
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
// Aborted while the connection was being opened: it is cancelled now that it exists.
|
|
319
|
+
if (abandoned) connection.cancel();
|
|
320
|
+
return true;
|
|
260
321
|
}
|
|
261
322
|
|
|
262
323
|
private requestConnection(
|
|
@@ -264,13 +325,8 @@ export class NetHelper {
|
|
|
264
325
|
req: PreviewRequest,
|
|
265
326
|
sink: PreviewSink,
|
|
266
327
|
host?: string,
|
|
267
|
-
):
|
|
268
|
-
const captured =
|
|
269
|
-
method: req.method,
|
|
270
|
-
target: req.target,
|
|
271
|
-
headers: req.headers,
|
|
272
|
-
body: req.body,
|
|
273
|
-
};
|
|
328
|
+
): CancellableConnection | null {
|
|
329
|
+
const captured = snapshotPreviewRequest(req);
|
|
274
330
|
const request = serializeRequest(captured, `localhost:${port}`);
|
|
275
331
|
const isHead = captured.method === "HEAD";
|
|
276
332
|
const sinkHead = sink.head.bind(sink),
|
|
@@ -278,7 +334,7 @@ export class NetHelper {
|
|
|
278
334
|
const end = sink.end.bind(sink),
|
|
279
335
|
error = sink.error.bind(sink);
|
|
280
336
|
let delivered: Promise<void> | undefined;
|
|
281
|
-
let connection:
|
|
337
|
+
let connection: CancellableConnection | null = null;
|
|
282
338
|
let finished = false;
|
|
283
339
|
const finish: PreviewSink = {
|
|
284
340
|
head: (head: ResponseHead) => sinkHead(head),
|
|
@@ -300,7 +356,7 @@ export class NetHelper {
|
|
|
300
356
|
},
|
|
301
357
|
};
|
|
302
358
|
const parser = new ResponseParser(finish, isHead);
|
|
303
|
-
connection = this.
|
|
359
|
+
connection = this.openConnection(
|
|
304
360
|
port,
|
|
305
361
|
{
|
|
306
362
|
data: (bytes) => {
|
|
@@ -355,6 +411,17 @@ export class NetHelper {
|
|
|
355
411
|
|
|
356
412
|
/** Connects a host-side byte stream to the guest without HTTP response framing. */
|
|
357
413
|
connect(port: number, sink: ConnectionSink, host?: string): PreviewConnection | null {
|
|
414
|
+
const connection = this.openConnection(port, sink, host);
|
|
415
|
+
if (!connection) return null;
|
|
416
|
+
return { write: (bytes) => connection.write(bytes), close: () => connection.close() };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** `connect`, plus a `cancel` that drops the bytes not yet sent before the end. */
|
|
420
|
+
private openConnection(
|
|
421
|
+
port: number,
|
|
422
|
+
sink: ConnectionSink,
|
|
423
|
+
host?: string,
|
|
424
|
+
): CancellableConnection | null {
|
|
358
425
|
const captured: ConnectionSink = {
|
|
359
426
|
data: sink.data.bind(sink),
|
|
360
427
|
end: sink.end.bind(sink),
|
|
@@ -362,8 +429,11 @@ export class NetHelper {
|
|
|
362
429
|
};
|
|
363
430
|
const listener = this.listenerFor(port);
|
|
364
431
|
if (!this.live()) return null;
|
|
365
|
-
if (listener === undefined || this.sharedListeners.has(listener))
|
|
366
|
-
|
|
432
|
+
if (listener === undefined || this.sharedListeners.has(listener)) {
|
|
433
|
+
const socket = this.connectNamespace(port, captured, host);
|
|
434
|
+
// A namespace socket's close already drops what it has not written yet.
|
|
435
|
+
return socket && { ...socket, cancel: () => socket.close() };
|
|
436
|
+
}
|
|
367
437
|
sink = captured;
|
|
368
438
|
const bind = this.namespace.address(port);
|
|
369
439
|
if (!bind) return null;
|
|
@@ -381,15 +451,23 @@ export class NetHelper {
|
|
|
381
451
|
local,
|
|
382
452
|
};
|
|
383
453
|
let sending = this.ring.write(listener, Kind.Conn, json(announce));
|
|
454
|
+
let cancelled = false;
|
|
455
|
+
const unsent = () => !cancelled;
|
|
456
|
+
const close = () => {
|
|
457
|
+
if (!this.connections.delete(conn)) return;
|
|
458
|
+
sending = sending.then(() => this.ring.write(conn, Kind.End));
|
|
459
|
+
};
|
|
384
460
|
return {
|
|
385
461
|
write: (bytes) => {
|
|
386
462
|
if (!this.connections.has(conn)) return;
|
|
387
463
|
const owned = bytes.slice();
|
|
388
|
-
sending = sending.then(() => this.writeChunks(conn, owned));
|
|
464
|
+
sending = sending.then(() => this.writeChunks(conn, owned, unsent));
|
|
389
465
|
},
|
|
390
|
-
close
|
|
391
|
-
|
|
392
|
-
|
|
466
|
+
// A close sends what was written first; a cancel goes straight to the end.
|
|
467
|
+
close,
|
|
468
|
+
cancel: () => {
|
|
469
|
+
cancelled = true;
|
|
470
|
+
close();
|
|
393
471
|
},
|
|
394
472
|
};
|
|
395
473
|
}
|
|
@@ -1072,13 +1150,30 @@ export class NetHelper {
|
|
|
1072
1150
|
bytes: Uint8Array,
|
|
1073
1151
|
live?: () => boolean,
|
|
1074
1152
|
): Promise<void> {
|
|
1153
|
+
const dropped = live && (() => !live());
|
|
1075
1154
|
for (let at = 0; at < bytes.length; at += MAX_RECORD) {
|
|
1076
|
-
if (
|
|
1077
|
-
|
|
1155
|
+
if (dropped?.()) return;
|
|
1156
|
+
// A chunk still waiting for ring space is dropped too when the stream stops.
|
|
1157
|
+
await this.ring.write(handle, Kind.Data, bytes.subarray(at, at + MAX_RECORD), dropped);
|
|
1078
1158
|
}
|
|
1079
1159
|
}
|
|
1080
1160
|
}
|
|
1081
1161
|
|
|
1162
|
+
/** A preview request's caller fields, each read once. */
|
|
1163
|
+
function snapshotPreviewRequest(req: PreviewRequest): PreviewRequest {
|
|
1164
|
+
return { method: req.method, target: req.target, headers: req.headers, body: req.body };
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
/** A preview sink's callbacks, each read once and bound to the caller's sink. */
|
|
1168
|
+
function snapshotSink(sink: PreviewSink): PreviewSink {
|
|
1169
|
+
return {
|
|
1170
|
+
head: sink.head.bind(sink),
|
|
1171
|
+
data: sink.data.bind(sink),
|
|
1172
|
+
end: sink.end.bind(sink),
|
|
1173
|
+
error: sink.error.bind(sink),
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1082
1177
|
/** Read caller accessors once, before the final admission check or any allocation. */
|
|
1083
1178
|
function snapshotRequest(req: NetRequest): NetRequest {
|
|
1084
1179
|
const type = req.type,
|
package/browser/net/ring.ts
CHANGED
|
@@ -62,12 +62,18 @@ export class RingWriter extends RingBase {
|
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
64
|
* Appends one record, waiting while the ring is full. Records land in call order (writes are
|
|
65
|
-
* queued), so callers may fire and forget.
|
|
65
|
+
* queued), so callers may fire and forget. A record whose `dropped()` turns true before it is
|
|
66
|
+
* published is never published, and stops waiting for space.
|
|
66
67
|
*/
|
|
67
|
-
write(
|
|
68
|
+
write(
|
|
69
|
+
handle: number,
|
|
70
|
+
kind: number,
|
|
71
|
+
payload: Uint8Array = new Uint8Array(),
|
|
72
|
+
dropped?: () => boolean,
|
|
73
|
+
): Promise<void> {
|
|
68
74
|
if (payload.length > MAX_RECORD)
|
|
69
75
|
return Promise.reject(new RangeError(`ring record too large: ${payload.length}`));
|
|
70
|
-
this.queue = this.queue.then(() => this.append(handle, kind, payload));
|
|
76
|
+
this.queue = this.queue.then(() => this.append(handle, kind, payload, dropped));
|
|
71
77
|
return this.queue;
|
|
72
78
|
}
|
|
73
79
|
|
|
@@ -78,10 +84,16 @@ export class RingWriter extends RingBase {
|
|
|
78
84
|
Atomics.notify(this.cells, SPACE);
|
|
79
85
|
}
|
|
80
86
|
|
|
81
|
-
private async append(
|
|
87
|
+
private async append(
|
|
88
|
+
handle: number,
|
|
89
|
+
kind: number,
|
|
90
|
+
payload: Uint8Array,
|
|
91
|
+
dropped?: () => boolean,
|
|
92
|
+
): Promise<void> {
|
|
82
93
|
const size = align4(RECORD_HEADER + payload.length);
|
|
83
|
-
|
|
84
|
-
|
|
94
|
+
const skip = () => this.disposed || dropped?.() === true;
|
|
95
|
+
while (!skip() && CAPACITY - this.used() < size) await this.waitForSpace(size);
|
|
96
|
+
if (skip()) return;
|
|
85
97
|
this.headerView.setInt32(0, handle, true);
|
|
86
98
|
this.headerView.setInt32(4, kind, true);
|
|
87
99
|
this.headerView.setInt32(8, payload.length, true);
|
package/browser/net/runtime.ts
CHANGED
|
@@ -143,9 +143,9 @@ function project(
|
|
|
143
143
|
live();
|
|
144
144
|
return helper.listenerFor(port);
|
|
145
145
|
},
|
|
146
|
-
request(port, request, sink) {
|
|
146
|
+
request(port, request, sink, options) {
|
|
147
147
|
live();
|
|
148
|
-
return helper.request(port, request, sink);
|
|
148
|
+
return helper.request(port, request, sink, options);
|
|
149
149
|
},
|
|
150
150
|
connect(port, sink) {
|
|
151
151
|
live();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { allocateSharedControl } from "../memory/control.js";
|
|
2
2
|
export interface NotificationConnection {
|
|
3
3
|
readonly port: MessagePort;
|
|
4
|
-
/** Sequence,
|
|
4
|
+
/** Sequence, external lifetime fence, then the id of the guest's active park (0: none). */
|
|
5
5
|
readonly state: Int32Array;
|
|
6
6
|
}
|
|
7
7
|
|
|
@@ -12,33 +12,130 @@ export interface NotificationWord {
|
|
|
12
12
|
|
|
13
13
|
export type NotificationRelayRequest =
|
|
14
14
|
| { readonly type: "arm"; readonly words: readonly NotificationWord[] }
|
|
15
|
+
/** The guest is about to block having observed `values`, one per armed word. It stored `id`
|
|
16
|
+
* in state[2] first and clears it when the blocking call returns. */
|
|
17
|
+
| { readonly type: "park"; readonly id: number; readonly values: readonly number[] }
|
|
15
18
|
| { readonly type: "dispose" };
|
|
16
19
|
|
|
17
20
|
export type NotificationRelayReply =
|
|
18
21
|
| { readonly type: "armed" }
|
|
19
22
|
| { readonly type: "error"; readonly message: string };
|
|
20
23
|
|
|
24
|
+
/** Page to relay Worker: one message adds a guest connection, the other removes it. */
|
|
25
|
+
export type NotificationRelayControl =
|
|
26
|
+
| (NotificationConnection & { readonly id: number })
|
|
27
|
+
| { readonly drop: number };
|
|
28
|
+
|
|
29
|
+
/** Relay Worker to page, about one connection. */
|
|
30
|
+
export interface NotificationRelayNotice {
|
|
31
|
+
readonly id: number;
|
|
32
|
+
readonly type: "failed" | "disposed" | "dropped";
|
|
33
|
+
}
|
|
34
|
+
|
|
21
35
|
export interface NotificationBridge {
|
|
22
36
|
readonly connection: NotificationConnection;
|
|
23
|
-
/** Settles
|
|
37
|
+
/** Settles once the relay can no longer publish to this connection: after the relay
|
|
38
|
+
* dropped it, or after the host's termination operation returns for the last one. */
|
|
24
39
|
readonly closed: Promise<void>;
|
|
25
40
|
dispose(): Promise<void>;
|
|
26
41
|
}
|
|
27
42
|
|
|
28
|
-
/**
|
|
43
|
+
/** One relay Worker serves every bridge on this page that names the same Worker URL. It
|
|
44
|
+
* lives while any of those bridges does and is terminated with the last one. */
|
|
45
|
+
class SharedRelay {
|
|
46
|
+
private readonly worker: Worker;
|
|
47
|
+
/** Live bridges by connection id: their dispose, for relay-reported failures. */
|
|
48
|
+
private readonly members = new Map<number, () => Promise<void>>();
|
|
49
|
+
/** Removed bridges waiting for the relay to acknowledge their drop. */
|
|
50
|
+
private readonly drops = new Map<number, ReturnType<typeof Promise.withResolvers<void>>>();
|
|
51
|
+
private nextId = 1;
|
|
52
|
+
|
|
53
|
+
constructor(
|
|
54
|
+
private readonly url: string,
|
|
55
|
+
private readonly registry: Map<string, SharedRelay>,
|
|
56
|
+
) {
|
|
57
|
+
this.worker = new Worker(url, { type: "module" });
|
|
58
|
+
this.worker.onerror = () => {
|
|
59
|
+
for (const dispose of Array.from(this.members.values())) void dispose().catch(() => {});
|
|
60
|
+
};
|
|
61
|
+
this.worker.onmessage = ({ data }: MessageEvent<NotificationRelayNotice>) => {
|
|
62
|
+
const id = data?.id;
|
|
63
|
+
if (data?.type === "dropped") return this.acknowledge(id);
|
|
64
|
+
void this.members
|
|
65
|
+
.get(id)?.()
|
|
66
|
+
.catch(() => {});
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Posts the connection; throws before the relay knows it. */
|
|
71
|
+
add(port: MessagePort, state: Int32Array, dispose: () => Promise<void>): number {
|
|
72
|
+
const id = this.nextId++;
|
|
73
|
+
this.worker.postMessage({ id, port, state } satisfies NotificationRelayControl, [port]);
|
|
74
|
+
this.members.set(id, dispose);
|
|
75
|
+
return id;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Resolves once the relay dropped the connection, or once its Worker was terminated. */
|
|
79
|
+
remove(id: number): Promise<void> {
|
|
80
|
+
const known = this.members.delete(id);
|
|
81
|
+
if (!this.members.size) return this.stop();
|
|
82
|
+
if (!known) return Promise.resolve();
|
|
83
|
+
const dropped = Promise.withResolvers<void>();
|
|
84
|
+
this.drops.set(id, dropped);
|
|
85
|
+
try {
|
|
86
|
+
this.worker.postMessage({ drop: id } satisfies NotificationRelayControl);
|
|
87
|
+
} catch {
|
|
88
|
+
this.acknowledge(id);
|
|
89
|
+
}
|
|
90
|
+
return dropped.promise;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private acknowledge(id: number): void {
|
|
94
|
+
const dropped = this.drops.get(id);
|
|
95
|
+
this.drops.delete(id);
|
|
96
|
+
dropped?.resolve();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private stop(): Promise<void> {
|
|
100
|
+
if (this.registry.get(this.url) === this) this.registry.delete(this.url);
|
|
101
|
+
let stopped: Promise<void>;
|
|
102
|
+
try {
|
|
103
|
+
stopped = Promise.resolve(this.worker.terminate());
|
|
104
|
+
} catch (error) {
|
|
105
|
+
stopped = Promise.reject(error);
|
|
106
|
+
}
|
|
107
|
+
// Termination supersedes outstanding acknowledgments: those drops settle with its result.
|
|
108
|
+
for (const [id, dropped] of Array.from(this.drops)) {
|
|
109
|
+
this.drops.delete(id);
|
|
110
|
+
stopped.then(dropped.resolve, dropped.reject);
|
|
111
|
+
}
|
|
112
|
+
return stopped;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const relays = new Map<string, SharedRelay>();
|
|
117
|
+
|
|
118
|
+
function relayFor(url: string): SharedRelay {
|
|
119
|
+
let relay = relays.get(url);
|
|
120
|
+
if (!relay) relays.set(url, (relay = new SharedRelay(url, relays)));
|
|
121
|
+
return relay;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Page-owned. No guest closure is needed to fence or stop forwarding to the guest. */
|
|
29
125
|
export function createNotificationBridge(workerUrl: string): NotificationBridge {
|
|
30
126
|
const channel = new MessageChannel();
|
|
31
127
|
let state: Int32Array;
|
|
32
|
-
let
|
|
128
|
+
let relay: SharedRelay;
|
|
33
129
|
try {
|
|
34
|
-
state = new Int32Array(allocateSharedControl(
|
|
35
|
-
|
|
130
|
+
state = new Int32Array(allocateSharedControl(12));
|
|
131
|
+
relay = relayFor(workerUrl);
|
|
36
132
|
} catch (error) {
|
|
37
133
|
channel.port1.close();
|
|
38
134
|
channel.port2.close();
|
|
39
135
|
throw error;
|
|
40
136
|
}
|
|
41
137
|
let disposed = false;
|
|
138
|
+
let id = 0;
|
|
42
139
|
let finish!: () => void;
|
|
43
140
|
let fail!: (reason: unknown) => void;
|
|
44
141
|
const closed = new Promise<void>((resolve, reject) => {
|
|
@@ -52,21 +149,11 @@ export function createNotificationBridge(workerUrl: string): NotificationBridge
|
|
|
52
149
|
Atomics.add(state, 0, 1);
|
|
53
150
|
Atomics.notify(state, 0);
|
|
54
151
|
channel.port2.close();
|
|
55
|
-
|
|
56
|
-
Promise.resolve(worker.terminate()).then(finish, fail);
|
|
57
|
-
} catch (error) {
|
|
58
|
-
fail(error);
|
|
59
|
-
}
|
|
152
|
+
relay.remove(id).then(finish, fail);
|
|
60
153
|
return closed;
|
|
61
154
|
};
|
|
62
|
-
worker.onerror = () => {
|
|
63
|
-
void dispose().catch(() => {});
|
|
64
|
-
};
|
|
65
|
-
worker.onmessage = () => {
|
|
66
|
-
void dispose().catch(() => {});
|
|
67
|
-
};
|
|
68
155
|
try {
|
|
69
|
-
|
|
156
|
+
id = relay.add(channel.port1, state, dispose);
|
|
70
157
|
} catch (error) {
|
|
71
158
|
channel.port1.close();
|
|
72
159
|
void dispose().catch(() => {});
|
|
@@ -160,6 +160,20 @@ export class NotificationWaitScope {
|
|
|
160
160
|
initialCoverage: boolean,
|
|
161
161
|
): NotificationWaitSet {
|
|
162
162
|
let covered = initialCoverage;
|
|
163
|
+
// The relay waits on the source words only while this guest is parked. Every wait attempt
|
|
164
|
+
// posts a fresh park id, stored in state[2] while the guest is blocked; the relay publishes
|
|
165
|
+
// only by consuming it. Ids are never reactivated, so a relay that fails to claim one can
|
|
166
|
+
// drop it without stranding a sleeping guest.
|
|
167
|
+
const active = this.connection.state;
|
|
168
|
+
let nextPark = 1;
|
|
169
|
+
const park = (observed: NotificationSample): void => {
|
|
170
|
+
const values = parkValues(observed, network !== undefined, process !== undefined);
|
|
171
|
+
const id = nextPark;
|
|
172
|
+
nextPark = nextPark === 0x7fffffff ? 1 : nextPark + 1;
|
|
173
|
+
atomicsStore(active, 2, id);
|
|
174
|
+
this.send({ type: "park", id, values });
|
|
175
|
+
};
|
|
176
|
+
const retire = () => atomicsStore(active, 2, 0);
|
|
163
177
|
const disposed = () => this.status === "closed" || atomicsLoad(this.connection.state, 1) !== 0;
|
|
164
178
|
const sample = (): NotificationSample => ({
|
|
165
179
|
sequence: atomicsLoad(this.wake, 0),
|
|
@@ -176,18 +190,36 @@ export class NotificationWaitScope {
|
|
|
176
190
|
},
|
|
177
191
|
sample,
|
|
178
192
|
wait: (observed, deadline) => {
|
|
193
|
+
// Disposal stores the fence before it moves the sequence, so any sequence change it
|
|
194
|
+
// caused is seen together with the fence: every return reports "closed" first.
|
|
195
|
+
const settled = (result: NotificationWaitResult): NotificationWaitResult =>
|
|
196
|
+
disposed() ? "closed" : result;
|
|
179
197
|
if (disposed()) return "closed";
|
|
180
|
-
|
|
181
|
-
if (changed(observed, current)) return "not-equal";
|
|
198
|
+
if (changed(observed, sample())) return settled("not-equal");
|
|
182
199
|
const remaining = deadline === Infinity ? Infinity : maximum(0, deadline - now());
|
|
183
|
-
if (remaining === 0) return "timed-out";
|
|
200
|
+
if (remaining === 0) return settled("timed-out");
|
|
201
|
+
try {
|
|
202
|
+
park(observed);
|
|
203
|
+
} catch {
|
|
204
|
+
// An unreachable relay cannot wake this guest: close the set instead of sleeping.
|
|
205
|
+
retire();
|
|
206
|
+
this.cleanupStep(() => this.dispose());
|
|
207
|
+
return "closed";
|
|
208
|
+
}
|
|
209
|
+
// Recheck after activating the park: a change the relay saw while it was inactive is
|
|
210
|
+
// visible here, so the relay dropping that park loses nothing.
|
|
211
|
+
if (changed(observed, sample())) {
|
|
212
|
+
retire();
|
|
213
|
+
return settled("not-equal");
|
|
214
|
+
}
|
|
184
215
|
const result = atomicsWait(
|
|
185
216
|
this.wake,
|
|
186
217
|
0,
|
|
187
218
|
observed.sequence,
|
|
188
219
|
covered ? remaining : minimum(remaining, 10),
|
|
189
220
|
);
|
|
190
|
-
|
|
221
|
+
retire();
|
|
222
|
+
return settled(result);
|
|
191
223
|
},
|
|
192
224
|
setProcessSubscriptions(subscriptions) {
|
|
193
225
|
covered =
|
|
@@ -231,6 +263,15 @@ export class NotificationWaitScope {
|
|
|
231
263
|
}
|
|
232
264
|
}
|
|
233
265
|
|
|
266
|
+
/** Values in armed word order: filesystem, then network and process when armed. */
|
|
267
|
+
function parkValues(sample: NotificationSample, network: boolean, process: boolean): number[] {
|
|
268
|
+
const filesystem = sample.filesystem;
|
|
269
|
+
if (network && process) return [filesystem, sample.network!, sample.process!];
|
|
270
|
+
if (network) return [filesystem, sample.network!];
|
|
271
|
+
if (process) return [filesystem, sample.process!];
|
|
272
|
+
return [filesystem];
|
|
273
|
+
}
|
|
274
|
+
|
|
234
275
|
function changed(before: NotificationSample, after: NotificationSample): boolean {
|
|
235
276
|
return (
|
|
236
277
|
before.sequence !== after.sequence ||
|