@statewalker/webrun-http-browser 0.6.4 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/README.md +319 -65
  2. package/dist/core/data-calls.d.ts +17 -0
  3. package/dist/core/data-calls.d.ts.map +1 -1
  4. package/dist/core/service-worker-control.d.ts +3 -1
  5. package/dist/core/service-worker-control.d.ts.map +1 -1
  6. package/dist/index.d.ts +3 -0
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +3907 -266
  9. package/dist/link/errors.d.ts +2 -0
  10. package/dist/link/errors.d.ts.map +1 -0
  11. package/dist/link/http-link.d.ts +58 -0
  12. package/dist/link/http-link.d.ts.map +1 -0
  13. package/dist/link/index.d.ts +4 -0
  14. package/dist/link/index.d.ts.map +1 -0
  15. package/dist/link/locks.d.ts +39 -0
  16. package/dist/link/locks.d.ts.map +1 -0
  17. package/dist/link/rendezvous.d.ts +21 -0
  18. package/dist/link/rendezvous.d.ts.map +1 -0
  19. package/dist/relay/index-sw.d.ts +12 -2
  20. package/dist/relay/index-sw.d.ts.map +1 -1
  21. package/dist/relay/index.d.ts +66 -8
  22. package/dist/relay/index.d.ts.map +1 -1
  23. package/dist/relay-sw.js +1160 -55
  24. package/dist/relay-worker.js +1161 -55
  25. package/dist/sw/handler-endpoint.d.ts +81 -0
  26. package/dist/sw/handler-endpoint.d.ts.map +1 -0
  27. package/dist/sw/handler-table.d.ts +53 -0
  28. package/dist/sw/handler-table.d.ts.map +1 -0
  29. package/dist/sw/http-sw-dispatcher.d.ts +162 -21
  30. package/dist/sw/http-sw-dispatcher.d.ts.map +1 -1
  31. package/dist/sw/index.d.ts +5 -0
  32. package/dist/sw/index.d.ts.map +1 -1
  33. package/dist/sw/protocol.d.ts +115 -0
  34. package/dist/sw/protocol.d.ts.map +1 -0
  35. package/dist/sw/sw-dispatcher.d.ts +2 -1
  36. package/dist/sw/sw-dispatcher.d.ts.map +1 -1
  37. package/dist/sw/sw-http-adapter.d.ts +199 -0
  38. package/dist/sw/sw-http-adapter.d.ts.map +1 -0
  39. package/dist/sw/sw-websocket.d.ts +16 -0
  40. package/dist/sw/sw-websocket.d.ts.map +1 -0
  41. package/dist/sw/worker-service.d.ts +45 -0
  42. package/dist/sw/worker-service.d.ts.map +1 -0
  43. package/dist/sw/worker-url.d.ts +9 -0
  44. package/dist/sw/worker-url.d.ts.map +1 -0
  45. package/dist/sw-worker.js +2075 -210
  46. package/dist/sw.d.ts +2 -0
  47. package/dist/sw.d.ts.map +1 -1
  48. package/dist/sw.js +3853 -480
  49. package/dist/worker.d.ts +10 -0
  50. package/dist/worker.d.ts.map +1 -0
  51. package/dist/worker.js +3507 -0
  52. package/dist/ws/client.d.ts +73 -0
  53. package/dist/ws/client.d.ts.map +1 -0
  54. package/dist/ws/frames.d.ts +68 -0
  55. package/dist/ws/frames.d.ts.map +1 -0
  56. package/dist/ws/index.d.ts +6 -0
  57. package/dist/ws/index.d.ts.map +1 -0
  58. package/dist/ws/server.d.ts +53 -0
  59. package/dist/ws/server.d.ts.map +1 -0
  60. package/dist/ws/socket-core.d.ts +91 -0
  61. package/dist/ws/socket-core.d.ts.map +1 -0
  62. package/dist/ws/upgrade.d.ts +93 -0
  63. package/dist/ws/upgrade.d.ts.map +1 -0
  64. package/package.json +10 -5
  65. package/src/core/data-calls.ts +52 -2
  66. package/src/core/service-worker-control.ts +7 -1
  67. package/src/index.ts +3 -0
  68. package/src/link/errors.ts +3 -0
  69. package/src/link/http-link.ts +222 -0
  70. package/src/link/index.ts +3 -0
  71. package/src/link/locks.ts +97 -0
  72. package/src/link/rendezvous.ts +60 -0
  73. package/src/relay/index-sw.ts +84 -12
  74. package/src/relay/index.ts +205 -29
  75. package/src/sw/handler-endpoint.ts +278 -0
  76. package/src/sw/handler-table.ts +130 -0
  77. package/src/sw/http-sw-dispatcher.ts +649 -90
  78. package/src/sw/index.ts +5 -0
  79. package/src/sw/protocol.ts +103 -0
  80. package/src/sw/sw-dispatcher.ts +12 -30
  81. package/src/sw/sw-http-adapter.ts +613 -0
  82. package/src/sw/sw-websocket.ts +43 -0
  83. package/src/sw/worker-service.ts +133 -0
  84. package/src/sw/worker-url.ts +19 -0
  85. package/src/sw-worker.ts +8 -3
  86. package/src/sw.ts +2 -0
  87. package/src/worker.ts +9 -0
  88. package/src/ws/client.ts +346 -0
  89. package/src/ws/frames.ts +119 -0
  90. package/src/ws/index.ts +11 -0
  91. package/src/ws/server.ts +132 -0
  92. package/src/ws/socket-core.ts +251 -0
  93. package/src/ws/upgrade.ts +200 -0
@@ -0,0 +1,2 @@
1
+ export declare function errorMessage(error: unknown): string;
2
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/link/errors.ts"],"names":[],"mappings":"AAAA,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEnD"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * The version of the HTTP-over-`MessagePort` protocol this package speaks,
3
+ * sent in every hand-shake that sets up a link. Version 1 was this package's
4
+ * own `handleStreams` / `sendStream` (no backpressure); version 2 is
5
+ * webrun-rpc's `connect` / `serve` over `overPipe` with webrun-http-streams'
6
+ * `fetchOverDuplex` / `serveFetchOverDuplex`: one port per link, calls
7
+ * multiplexed on it, each call a stream with a one-chunk window in both
8
+ * directions, and cancellation that reaches the handler.
9
+ */
10
+ export declare const HTTP_LINK_PROTOCOL = "webrun-http-link/2";
11
+ export interface HttpLinkOptions {
12
+ /**
13
+ * Per-stream inactivity timeout in ms (webrun-rpc's): a stream with no chunk
14
+ * in either direction for that long fails. Unset — the default — means
15
+ * none: a slow consumer is throttled, never failed.
16
+ */
17
+ streamTimeout?: number;
18
+ }
19
+ /** The calling end of a link: sends requests, gets responses. */
20
+ export interface HttpLink {
21
+ /** Sends `request` and resolves with the response, whose body streams. */
22
+ send(request: Request): Promise<Response>;
23
+ /**
24
+ * Requests whose response has not finished yet: still waiting for the head,
25
+ * or with a body still streaming.
26
+ */
27
+ readonly inflight: number;
28
+ /** Resolves when `inflight` falls to 0. */
29
+ whenIdle(): Promise<void>;
30
+ /**
31
+ * Closes the link. Requests in flight fail with `reason` (default: the link
32
+ * was closed) instead of hanging.
33
+ */
34
+ close(reason?: Error): Promise<void>;
35
+ readonly closed: boolean;
36
+ }
37
+ /**
38
+ * Why a request failed when its link went away under it: the far side (the
39
+ * handler, or the ServiceWorker) was lost. The request may or may not have
40
+ * been applied.
41
+ */
42
+ export declare class LinkLostError extends Error {
43
+ constructor(message?: string);
44
+ }
45
+ /** Opens the calling end of a link on `port` (one port per link). */
46
+ export declare function connectHttpLink(port: MessagePort, { streamTimeout }?: HttpLinkOptions): Promise<HttpLink>;
47
+ /**
48
+ * Serves `handler` on the answering end of a link. Resolves with a teardown
49
+ * that abandons the requests still running (their handlers see the
50
+ * cancellation) and releases the port.
51
+ */
52
+ export declare function serveHttpLink(port: MessagePort, handler: (request: Request) => Response | Promise<Response>, { streamTimeout }?: HttpLinkOptions): Promise<() => Promise<void>>;
53
+ /**
54
+ * Wraps a `send` so the link knows its requests in flight (for draining and
55
+ * load-based routing) and can fail them all at once when it is closed.
56
+ */
57
+ export declare function trackedLink(send: (request: Request) => Promise<Response>, release: () => Promise<void> | void): HttpLink;
58
+ //# sourceMappingURL=http-link.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-link.d.ts","sourceRoot":"","sources":["../../src/link/http-link.ts"],"names":[],"mappings":"AAGA;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,uBAAuB,CAAC;AAEvD,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,iEAAiE;AACjE,MAAM,WAAW,QAAQ;IACvB,0EAA0E;IAC1E,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1C;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,2CAA2C;IAC3C,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B;;;OAGG;IACH,KAAK,CAAC,MAAM,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAC1B;AAED;;;;GAIG;AACH,qBAAa,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAO,SAAuD,EAGzE;CACF;AASD,qEAAqE;AACrE,wBAAsB,eAAe,CACnC,IAAI,EAAE,WAAW,EACjB,EAAE,aAAa,EAAE,GAAE,eAAoB,GACtC,OAAO,CAAC,QAAQ,CAAC,CAcnB;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,WAAW,EACjB,OAAO,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,EAC3D,EAAE,aAAa,EAAE,GAAE,eAAoB,GACtC,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CAiB9B;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,EAC7C,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,GAClC,QAAQ,CAuEV"}
@@ -0,0 +1,4 @@
1
+ export * from "./http-link.js";
2
+ export { holdLock, locksAvailable, newLockName, watchLock } from "./locks.js";
3
+ export { PortRendezvous } from "./rendezvous.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/link/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC9E,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Liveness through Web Locks.
3
+ *
4
+ * A `MessagePort` never reports that the other side died, but a Web Lock
5
+ * does: a lock held by a page, a worker or a ServiceWorker is released by the
6
+ * browser the moment that context goes away, and a request waiting for it is
7
+ * granted then. So each side that must notice the other's death holds a lock
8
+ * of its own and watches the other's:
9
+ *
10
+ * - a handler holds an owner lock; the ServiceWorker watches it, and fails the
11
+ * handler's requests in flight when it is granted (the tab closed, the
12
+ * worker was terminated);
13
+ * - the ServiceWorker holds a router lock; handlers watch it, and fail their
14
+ * calls through it when it is granted (the browser stopped the worker).
15
+ *
16
+ * Every function here degrades to a no-op where `navigator.locks` is missing.
17
+ */
18
+ export declare function locksAvailable(): boolean;
19
+ /** A fresh lock name: `<prefix>:<uuid>`. */
20
+ export declare function newLockName(prefix: string): string;
21
+ /**
22
+ * Takes the lock `name` and holds it until the returned release is called or
23
+ * this context dies. Resolves once the lock is held; with no Web Locks,
24
+ * resolves at once.
25
+ */
26
+ export declare function holdLock(name: string): Promise<() => void>;
27
+ /**
28
+ * Calls `onLost` once the lock `name` is granted to this context — that is,
29
+ * once whoever holds it is gone — and releases it at once. Returns a function
30
+ * that stops watching. With no Web Locks, never calls `onLost`.
31
+ */
32
+ export declare function watchLock(name: string, onLost: () => void): () => void;
33
+ /**
34
+ * The names of the locks held right now in this origin, or `undefined` where
35
+ * that cannot be asked. A backup for `watchLock`: a grant can be late, a query
36
+ * cannot.
37
+ */
38
+ export declare function heldLockNames(): Promise<Set<string> | undefined>;
39
+ //# sourceMappingURL=locks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"locks.d.ts","sourceRoot":"","sources":["../../src/link/locks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAeH,wBAAgB,cAAc,IAAI,OAAO,CAExC;AAED,4CAA4C;AAC5C,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAElD;AAED;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,CAAC,CAgBhE;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAYtE;AAED;;;;GAIG;AACH,wBAAsB,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC,CAStE"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Matches replies to requests by id, for the control messages that hand
3
+ * WebSocket ports across: each waiting request has a deadline, and all of a
4
+ * peer's waiting requests can be failed at once when the peer is lost.
5
+ */
6
+ export declare class PortRendezvous<T> {
7
+ private readonly _prefix;
8
+ private readonly _waiting;
9
+ private _counter;
10
+ constructor(_prefix?: string);
11
+ /** A fresh id for a request. */
12
+ newId(): string;
13
+ get size(): number;
14
+ /** Waits for the reply to `id`, failing after `timeoutMs`. */
15
+ wait(id: string, timeoutMs: number, describe?: string): Promise<T>;
16
+ /** Delivers the reply to `id`. `false` when nobody waits for it (it timed out). */
17
+ settle(id: string, value: T): boolean;
18
+ fail(id: string, error: Error): void;
19
+ failAll(error: Error): void;
20
+ }
21
+ //# sourceMappingURL=rendezvous.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rendezvous.d.ts","sourceRoot":"","sources":["../../src/link/rendezvous.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,cAAc,CAAC,CAAC;IAWf,OAAO,CAAC,QAAQ,CAAC,OAAO;IAVpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAOrB;IACJ,OAAO,CAAC,QAAQ,CAAK;IAErB,YAA6B,OAAO,SAAM,EAAI;IAE9C,gCAAgC;IAChC,KAAK,IAAI,MAAM,CAEd;IAED,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,8DAA8D;IAC9D,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,SAAc,GAAG,OAAO,CAAC,CAAC,CAAC,CAQtE;IAED,mFAAmF;IACnF,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAOpC;IAED,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAMnC;IAED,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAE1B;CACF"}
@@ -62,8 +62,18 @@ export interface RelayServiceWorkerOptions {
62
62
  } & MountSpec>;
63
63
  /** Paths the relay never claims. Checked before the table. */
64
64
  exclude?: (url: URL) => boolean;
65
- /** Refuse a registration from the wrong client. Default: everyone may. */
66
- canRegister?: (client: Client, key: string) => boolean | Promise<boolean>;
65
+ /**
66
+ * Refuse a registration from the wrong client. Default: everyone may.
67
+ *
68
+ * `path` is where the registrant asks to be mounted (`undefined` for the
69
+ * `/~<key>/` addressing), so a host can vet where a key lands, not only who
70
+ * takes it: on an origin whose name is not a secret, a page could otherwise
71
+ * mount an allowed key at `/index.html` and outrank `/` on longest-prefix.
72
+ * Also consulted for `UNREGISTER`, with the path the key is mounted at.
73
+ */
74
+ canRegister?: (client: Client, key: string, path?: string) => boolean | Promise<boolean>;
75
+ /** Per-stream inactivity timeout of the links to services, in ms. Default none. */
76
+ streamTimeout?: number;
67
77
  /** Default `"last-wins"`, the behaviour before this option existed. */
68
78
  takeover?: "first-wins" | "last-wins";
69
79
  /** Stamp headers on responses the relay makes. Not applied to network fetches. */
@@ -1 +1 @@
1
- {"version":3,"file":"index-sw.d.ts","sourceRoot":"","sources":["../../src/relay/index-sw.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,UAAU,EAAiB,MAAM,kBAAkB,CAAC;AAGlF,+CAA+C;AAC/C,MAAM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AAuB,CACrC,IAAI,EAAE,wBAAwB,EAC9B,OAAO,GAAE,yBAA8B,GACtC,MAAM,IAAI,CAmNZ"}
@@ -1,5 +1,7 @@
1
1
  import type { HttpHandler } from "@statewalker/webrun-http-streams";
2
2
  import type { MessageTarget } from "../core/message-target.js";
3
+ import { type HttpLinkOptions } from "../link/http-link.js";
4
+ import { type PortWebSocketClass, type WebSocketClassOptions, type WebSocketConnection } from "../ws/client.js";
3
5
  export * from "./split-service-url.js";
4
6
  /**
5
7
  * Returns a MessagePort that transparently bridges messages to/from the
@@ -28,7 +30,7 @@ export interface InitServiceWorkerOptions {
28
30
  * `ServiceWorkerControlError` if activation takes longer than `timeout`.
29
31
  */
30
32
  export declare function initServiceWorker(options: InitServiceWorkerOptions): Promise<ServiceWorker>;
31
- export interface ServiceOptions {
33
+ export interface ServiceOptions extends HttpLinkOptions {
32
34
  key: string;
33
35
  /**
34
36
  * Where this service is mounted on the relay origin, e.g. `/` or `/peers/`.
@@ -38,15 +40,60 @@ export interface ServiceOptions {
38
40
  port: MessageTarget;
39
41
  }
40
42
  /**
41
- * Registers `handler` as the server for the given service `key` on the relay.
42
- * Returns a cleanup function that unregisters the service.
43
+ * What a service answers to a `CONNECT` that speaks `HTTP_LINK_PROTOCOL`. A
44
+ * service of version 1 answers `true` instead, and the caller then falls back
45
+ * to version 1's transport: both ends of a call must speak the same one, and
46
+ * the relay, the caller and the service may each run another version.
43
47
  */
44
- export declare function initHttpService(handler: HttpHandler, { key, path, port }: ServiceOptions): Promise<() => void>;
48
+ export interface ConnectReply {
49
+ accepted: true;
50
+ wire: string;
51
+ }
52
+ /** A `CONNECT` asking a service for a socket. */
53
+ export interface WebSocketConnectParams {
54
+ type: "ws";
55
+ key: string;
56
+ url: string;
57
+ protocols?: string[];
58
+ headers?: [string, string][];
59
+ }
60
+ /**
61
+ * Serves a service's side of one `CONNECT`: an HTTP link on `callPort`, or a
62
+ * WebSocket upgrade run through `handler`.
63
+ */
64
+ export declare function answerConnect(handler: HttpHandler, data: unknown, callPort: MessagePort | undefined, options?: HttpLinkOptions): Promise<unknown>;
65
+ /**
66
+ * Registers `handler` as the server for the given service `key` on the relay:
67
+ * it answers HTTP requests and WebSocket upgrades. Returns a function that
68
+ * unregisters the service; its promise settles once the relay acknowledged.
69
+ */
70
+ export declare function initHttpService(handler: HttpHandler, { key, path, port, streamTimeout }: ServiceOptions): Promise<() => Promise<void>>;
45
71
  /**
46
72
  * Sends a `Request` to the service registered under `key` on the relay and
47
- * resolves with the corresponding `Response`.
73
+ * resolves with the corresponding `Response`, whose body streams with
74
+ * backpressure.
75
+ */
76
+ export declare function callHttpService(request: Request, { key, port, streamTimeout }: ServiceOptions): Promise<Response>;
77
+ /**
78
+ * Sends `request` on a port a service accepted with `reply`: over an HTTP link
79
+ * when the service speaks `HTTP_LINK_PROTOCOL`, over version 1's transport
80
+ * when it answered `true`. The link is closed once the response is done.
48
81
  */
49
- export declare function callHttpService(request: Request, { key, port }: ServiceOptions): Promise<Response>;
82
+ export declare function sendOverConnection(port: MessagePort, reply: boolean | ConnectReply, request: Request, options?: HttpLinkOptions): Promise<Response>;
83
+ /**
84
+ * Opens the transport of a socket to the service registered under `key` on
85
+ * the relay: the service runs the upgrade through its handler, and the
86
+ * socket's port comes back through the relay. After that, the socket runs
87
+ * between the caller and the service directly.
88
+ */
89
+ export declare function callWebSocketService(url: string | URL, protocols: string[], { key, port, timeout }: Pick<ServiceOptions, "key" | "port"> & {
90
+ timeout?: number;
91
+ }): Promise<WebSocketConnection>;
92
+ /**
93
+ * A WHATWG `WebSocket` class whose sockets reach the service registered under
94
+ * `key` on the relay.
95
+ */
96
+ export declare function relayWebSocketClass(service: Pick<ServiceOptions, "key" | "port">, options?: WebSocketClassOptions): PortWebSocketClass;
50
97
  export interface RelayWindowHandlerOptions {
51
98
  swUrl?: string;
52
99
  scopeUrl?: string;
@@ -86,8 +133,19 @@ export interface RegisterConnectionsHandlerOptions {
86
133
  key: string;
87
134
  /** Where this service is mounted; see `ServiceOptions.path`. */
88
135
  path?: string;
89
- handler: (event: MessageEvent, data: unknown, port: MessagePort) => boolean | Promise<boolean>;
136
+ /**
137
+ * Answers one `CONNECT`. Its result is the reply; wrap a result carrying a
138
+ * port in `withTransfer`.
139
+ */
140
+ handler: (event: MessageEvent, data: unknown, port: MessagePort) => unknown | Promise<unknown>;
90
141
  communicationPort: MessageTarget;
91
142
  }
92
- export declare function registerConnectionsHandler({ key, path, handler, communicationPort, }: RegisterConnectionsHandlerOptions): Promise<() => void>;
143
+ /**
144
+ * Registers `key` on the relay and answers its `CONNECT`s with `handler`.
145
+ * Resolves with a function that unregisters it. That function's promise
146
+ * settles once the relay acknowledged the `UNREGISTER`, so a caller can stop
147
+ * its services and only then close the connection: closing first tore the
148
+ * ports down before the message landed, and left the mount behind.
149
+ */
150
+ export declare function registerConnectionsHandler({ key, path, handler, communicationPort, }: RegisterConnectionsHandlerOptions): Promise<() => Promise<void>>;
93
151
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/relay/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAC;AAGpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAQ/D,cAAc,wBAAwB,CAAC;AAavC;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,YAAY,CAAC,EAAE,yBAAyB,GAAG,WAAW,CAU1F;AAED,MAAM,WAAW,wBAAwB;IACvC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,OAAO,CAAC,aAAa,CAAC,CAEjG;AAgBD,MAAM,W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