@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.
Files changed (48) hide show
  1. package/browser/net/helper.ts +114 -19
  2. package/browser/net/ring.ts +18 -6
  3. package/browser/net/runtime.ts +2 -2
  4. package/browser/notification-relay.ts +105 -18
  5. package/browser/notification-wait.ts +45 -4
  6. package/browser/notification-worker.ts +169 -76
  7. package/browser/preview.ts +15 -2
  8. package/core/filesystem.ts +4 -0
  9. package/core/wasi.ts +49 -0
  10. package/dist/browser/net/helper.js +83 -18
  11. package/dist/browser/net/ring.js +6 -5
  12. package/dist/browser/net/runtime.js +2 -2
  13. package/dist/browser/notification-relay.js +78 -15
  14. package/dist/browser/notification-wait.js +37 -5
  15. package/dist/browser/notification-worker.js +146 -67
  16. package/dist/core/wasi.js +40 -0
  17. package/dist/fs/backend.js +7 -0
  18. package/dist/fs/client.js +69 -15
  19. package/dist/fs/dirty-admission.js +15 -8
  20. package/dist/fs/indexeddb.js +288 -96
  21. package/dist/fs/memory.js +106 -20
  22. package/dist/fs/multitab-backend.js +121 -37
  23. package/dist/fs/multitab-protocol.js +1 -0
  24. package/dist/fs/persistent.js +70 -16
  25. package/dist/fs/protocol.js +15 -7
  26. package/dist/fs/publication-attachment.js +9 -7
  27. package/dist/fs/service.js +34 -9
  28. package/dist/fs/shared.js +96 -6
  29. package/dist/proc/command-stdio.js +1 -1
  30. package/dist/tooling/program/program-transformer.json +1 -1
  31. package/fs/backend.ts +11 -0
  32. package/fs/client.ts +93 -16
  33. package/fs/dirty-admission.ts +22 -7
  34. package/fs/guest-watch.ts +1 -1
  35. package/fs/indexeddb.ts +379 -110
  36. package/fs/memory.ts +120 -19
  37. package/fs/multitab-backend.ts +206 -61
  38. package/fs/multitab-protocol.ts +1 -0
  39. package/fs/persistent.ts +82 -16
  40. package/fs/protocol.ts +26 -7
  41. package/fs/publication-attachment.ts +9 -7
  42. package/fs/service.ts +35 -7
  43. package/fs/shared.ts +106 -8
  44. package/package.json +1 -1
  45. package/proc/command-stdio.ts +1 -1
  46. package/testing/idle-workspace-cancellation-browser.ts +22 -13
  47. package/testing/indexeddb-storage-browser.ts +435 -1
  48. 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
- NotificationConnection,
7
+ NotificationRelayControl,
8
+ NotificationRelayNotice,
8
9
  NotificationRelayReply,
9
10
  NotificationRelayRequest,
10
11
  NotificationWord,
11
12
  } from "./notification-relay.js";
12
13
 
13
- const waiters = createNotificationWaiters(import.meta.url);
14
- const waitForWord = waiters.wait;
14
+ /** A guest sends at most filesystem, network and process words. */
15
+ const MAX_WORDS = 3;
15
16
 
16
- // One observer and pending reply per armed word. Disposal clears these roots before
17
- // terminating the relay; late replies must not recreate the cleared slots.
18
- const observers: (Promise<void> | undefined)[] = [];
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
- function clearRoot<T>(roots: (T | undefined)[], slot: number): void {
28
- if (slot < roots.length) roots[slot] = undefined;
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
- async function observe(word: NotificationWord, state: Int32Array, slot: number): Promise<void> {
32
- let seen = Atomics.load(word.cells, word.index);
33
- while (!Atomics.load(state, 1)) {
34
- const waiting = waitForWord(word.cells, word.index, seen);
35
- if (!waiting.async) {
36
- seen = Atomics.load(word.cells, word.index);
37
- publish(state);
38
- continue;
39
- }
40
- pendingWaits[slot] = waiting.value;
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
- await waiting.value;
43
- } finally {
44
- clearRoot(pendingWaits, slot);
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
- async function observeSafely(
52
- word: NotificationWord,
53
- state: Int32Array,
54
- reply: (value: NotificationRelayReply) => void,
55
- slot: number,
56
- ): Promise<void> {
57
- try {
58
- await observe(word, state, slot);
59
- } catch (error) {
60
- reply({ type: "error", message: String(error) });
61
- globalThis.postMessage("failed");
62
- } finally {
63
- clearRoot(observers, slot);
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
- function arm(
68
- words: readonly NotificationWord[],
69
- state: Int32Array,
70
- reply: (value: NotificationRelayReply) => void,
71
- ): void {
72
- for (let slot = 0; slot < words.length; slot++)
73
- observers[slot] = observeSafely(words[slot]!, state, reply, slot);
74
- reply({ type: "armed" });
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 connect({ port, state }: NotificationConnection): void {
78
- let armed = false;
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<NotificationConnection | NotificationBlockingRequest>) => {
103
- if ("type" in data) notificationBlockingWait(data);
104
- else connect(data);
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
  };
@@ -1,11 +1,24 @@
1
- import type { ConnectionSink, PreviewConnection, PreviewSink } from "./net/helper.js";
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
- request(port: number, request: PreviewRequest, sink: PreviewSink): boolean;
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
 
@@ -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
- return this.requestConnection(port, req, sink) !== null;
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.connect(port, {
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
- return this.connectNamespace(port, captured, host);
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
- if (!this.connections.delete(conn))
305
- return;
306
- sending = sending.then(() => this.ring.write(conn, Kind.End));
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 (live && !live())
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) {
@@ -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
- while (!this.disposed && CAPACITY - this.used() < size)
50
+ const skip = () => this.disposed || dropped?.() === true;
51
+ while (!skip() && CAPACITY - this.used() < size)
51
52
  await this.waitForSpace(size);
52
- if (this.disposed)
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();