@statewalker/webrun-http-browser 0.6.5 → 0.7.1
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/README.md +302 -52
- package/dist/core/data-calls.d.ts +17 -0
- package/dist/core/data-calls.d.ts.map +1 -1
- package/dist/core/service-worker-control.d.ts +3 -1
- package/dist/core/service-worker-control.d.ts.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4218 -301
- package/dist/link/async-dispose.d.ts +10 -0
- package/dist/link/async-dispose.d.ts.map +1 -0
- package/dist/link/errors.d.ts +2 -0
- package/dist/link/errors.d.ts.map +1 -0
- package/dist/link/http-link.d.ts +72 -0
- package/dist/link/http-link.d.ts.map +1 -0
- package/dist/link/index.d.ts +4 -0
- package/dist/link/index.d.ts.map +1 -0
- package/dist/link/locks.d.ts +39 -0
- package/dist/link/locks.d.ts.map +1 -0
- package/dist/link/rendezvous.d.ts +21 -0
- package/dist/link/rendezvous.d.ts.map +1 -0
- package/dist/relay/index-sw.d.ts +12 -2
- package/dist/relay/index-sw.d.ts.map +1 -1
- package/dist/relay/index.d.ts +66 -8
- package/dist/relay/index.d.ts.map +1 -1
- package/dist/relay-sw.js +1328 -64
- package/dist/relay-worker.js +1329 -64
- package/dist/sw/handler-endpoint.d.ts +81 -0
- package/dist/sw/handler-endpoint.d.ts.map +1 -0
- package/dist/sw/handler-table.d.ts +53 -0
- package/dist/sw/handler-table.d.ts.map +1 -0
- package/dist/sw/http-sw-dispatcher.d.ts +162 -21
- package/dist/sw/http-sw-dispatcher.d.ts.map +1 -1
- package/dist/sw/index.d.ts +5 -0
- package/dist/sw/index.d.ts.map +1 -1
- package/dist/sw/protocol.d.ts +115 -0
- package/dist/sw/protocol.d.ts.map +1 -0
- package/dist/sw/sw-dispatcher.d.ts.map +1 -1
- package/dist/sw/sw-http-adapter.d.ts +199 -0
- package/dist/sw/sw-http-adapter.d.ts.map +1 -0
- package/dist/sw/sw-websocket.d.ts +16 -0
- package/dist/sw/sw-websocket.d.ts.map +1 -0
- package/dist/sw/worker-service.d.ts +45 -0
- package/dist/sw/worker-service.d.ts.map +1 -0
- package/dist/sw/worker-url.d.ts +9 -0
- package/dist/sw/worker-url.d.ts.map +1 -0
- package/dist/sw-worker.js +2403 -265
- package/dist/sw.d.ts +2 -0
- package/dist/sw.d.ts.map +1 -1
- package/dist/sw.js +4189 -546
- package/dist/worker.d.ts +10 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +3780 -0
- package/dist/ws/client.d.ts +73 -0
- package/dist/ws/client.d.ts.map +1 -0
- package/dist/ws/frames.d.ts +68 -0
- package/dist/ws/frames.d.ts.map +1 -0
- package/dist/ws/index.d.ts +6 -0
- package/dist/ws/index.d.ts.map +1 -0
- package/dist/ws/server.d.ts +53 -0
- package/dist/ws/server.d.ts.map +1 -0
- package/dist/ws/socket-core.d.ts +91 -0
- package/dist/ws/socket-core.d.ts.map +1 -0
- package/dist/ws/upgrade.d.ts +93 -0
- package/dist/ws/upgrade.d.ts.map +1 -0
- package/package.json +10 -5
- package/src/core/data-calls.ts +52 -2
- package/src/core/service-worker-control.ts +7 -1
- package/src/index.ts +3 -0
- package/src/link/async-dispose.ts +22 -0
- package/src/link/errors.ts +3 -0
- package/src/link/http-link.ts +238 -0
- package/src/link/index.ts +3 -0
- package/src/link/locks.ts +97 -0
- package/src/link/rendezvous.ts +60 -0
- package/src/relay/index-sw.ts +84 -12
- package/src/relay/index.ts +203 -27
- package/src/sw/handler-endpoint.ts +278 -0
- package/src/sw/handler-table.ts +130 -0
- package/src/sw/http-sw-dispatcher.ts +649 -90
- package/src/sw/index.ts +5 -0
- package/src/sw/protocol.ts +103 -0
- package/src/sw/sw-dispatcher.ts +1 -20
- package/src/sw/sw-http-adapter.ts +613 -0
- package/src/sw/sw-websocket.ts +43 -0
- package/src/sw/worker-service.ts +133 -0
- package/src/sw/worker-url.ts +19 -0
- package/src/sw-worker.ts +8 -3
- package/src/sw.ts +2 -0
- package/src/worker.ts +9 -0
- package/src/ws/client.ts +346 -0
- package/src/ws/frames.ts +119 -0
- package/src/ws/index.ts +11 -0
- package/src/ws/server.ts +132 -0
- package/src/ws/socket-core.ts +251 -0
- package/src/ws/upgrade.ts +200 -0
package/src/sw/index.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
+
export * from "./handler-endpoint.js";
|
|
2
|
+
export * from "./handler-table.js";
|
|
1
3
|
export * from "./http-sw-dispatcher.js";
|
|
2
4
|
export * from "./sw-dispatcher.js";
|
|
5
|
+
export * from "./sw-http-adapter.js";
|
|
6
|
+
export * from "./sw-websocket.js";
|
|
7
|
+
export * from "./worker-service.js";
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The messages between pages, Dedicated Workers and the same-origin
|
|
3
|
+
* ServiceWorker. Internal: the shapes may change between versions, which is
|
|
4
|
+
* why every hand-shake carries `HTTP_LINK_PROTOCOL`.
|
|
5
|
+
*
|
|
6
|
+
* THREE PORTS PER HANDLER LINK. A page makes the channels and hands one end of
|
|
7
|
+
* each to the handler (itself, or a worker) and the other to the
|
|
8
|
+
* ServiceWorker; after that it carries nothing:
|
|
9
|
+
*
|
|
10
|
+
* - `down`: ServiceWorker → handler, the requests the handler serves;
|
|
11
|
+
* - `ctl`: a raw port next to them, for what HTTP over a stream cannot carry,
|
|
12
|
+
* transferred `MessagePort`s (WebSocket hand-over);
|
|
13
|
+
* - `up` (workers only): handler → ServiceWorker, the requests the handler
|
|
14
|
+
* sends through the router (to other handlers).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Page → ServiceWorker: attach a handler link. The name is the one version 1 used. */
|
|
18
|
+
export const ATTACH_CALL = "UPDATE_COMMUNICATION_PORT";
|
|
19
|
+
/** Page → ServiceWorker: remove a handler link now. */
|
|
20
|
+
export const DETACH_CALL = "webrun-http:detach";
|
|
21
|
+
/** Page → ServiceWorker: stop routing to a link, let its requests finish, then remove it. */
|
|
22
|
+
export const DRAIN_CALL = "webrun-http:drain";
|
|
23
|
+
/** Page → ServiceWorker: open a WebSocket to a handler. */
|
|
24
|
+
export const WS_CONNECT_CALL = "webrun-http:ws-connect";
|
|
25
|
+
/** ServiceWorker → pages: attach your links again (the ServiceWorker restarted). */
|
|
26
|
+
export const NEED_LINKS_CALL = "webrun-http:need-links";
|
|
27
|
+
/**
|
|
28
|
+
* Page → ServiceWorker, plain messages (no reply). A message to the
|
|
29
|
+
* ServiceWorker object is an event: it starts a stopped worker and resets its
|
|
30
|
+
* idle timer. Port messages do neither.
|
|
31
|
+
*/
|
|
32
|
+
export const WAKE_MESSAGE = "webrun-http:wake";
|
|
33
|
+
export const KEEPALIVE_MESSAGE = "webrun-http:keepalive";
|
|
34
|
+
|
|
35
|
+
export interface AttachParams {
|
|
36
|
+
/** `HTTP_LINK_PROTOCOL`; absent from version 1 pages. */
|
|
37
|
+
wire?: string;
|
|
38
|
+
key: string;
|
|
39
|
+
/** The handler slot. Attaching an id that is attached already replaces it. */
|
|
40
|
+
id?: string;
|
|
41
|
+
/** Token of this attachment, named by `drain` / `detach`. */
|
|
42
|
+
owner?: string;
|
|
43
|
+
/** A Web Lock the handler holds while it lives. */
|
|
44
|
+
ownerLock?: string;
|
|
45
|
+
/** Whatever the host wants a `route` function to see. */
|
|
46
|
+
meta?: unknown;
|
|
47
|
+
/** Which ports follow `down` in the transfer list, in this order. */
|
|
48
|
+
ctl?: boolean;
|
|
49
|
+
up?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface AttachReply {
|
|
53
|
+
/** `HTTP_LINK_PROTOCOL`: proves the worker speaks it. */
|
|
54
|
+
accepted: string;
|
|
55
|
+
/** The Web Lock the ServiceWorker holds while it lives; absent without Web Locks. */
|
|
56
|
+
routerLock?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface DrainParams {
|
|
60
|
+
key: string;
|
|
61
|
+
id: string;
|
|
62
|
+
owner: string;
|
|
63
|
+
/** How long requests in flight may run before the link is closed anyway. */
|
|
64
|
+
ms?: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface NeedLinksParams {
|
|
68
|
+
/** Links made for this router are alive; only the others need attaching again. */
|
|
69
|
+
routerLock?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The `ctl` port's messages. */
|
|
73
|
+
export type ControlMessage =
|
|
74
|
+
// ServiceWorker → handler: a caller asks for a socket.
|
|
75
|
+
| {
|
|
76
|
+
t: "ws-connect";
|
|
77
|
+
id: string;
|
|
78
|
+
url: string;
|
|
79
|
+
protocols: string[];
|
|
80
|
+
headers?: [string, string][];
|
|
81
|
+
}
|
|
82
|
+
// handler → ServiceWorker: the answer (`port` is transferred).
|
|
83
|
+
| { t: "ws-accept"; id: string; protocol: string; port: MessagePort }
|
|
84
|
+
| { t: "ws-refuse"; id: string; status: number; message: string }
|
|
85
|
+
// handler → ServiceWorker: the handler itself asks for a socket to another handler.
|
|
86
|
+
| { t: "ws-open"; id: string; url: string; protocols: string[] }
|
|
87
|
+
| { t: "ws-opened"; id: string; protocol: string; port: MessagePort }
|
|
88
|
+
| { t: "ws-failed"; id: string; message: string };
|
|
89
|
+
|
|
90
|
+
/** Page ↔ worker: what `attachWorker` and `serveHttpInWorker` exchange. */
|
|
91
|
+
export const WORKER_HELLO = "webrun-http:worker-hello";
|
|
92
|
+
|
|
93
|
+
export type WorkerControlMessage =
|
|
94
|
+
// worker → page, once: the worker serves, and holds this owner lock.
|
|
95
|
+
| { type: "ready"; ownerLock?: string }
|
|
96
|
+
// page → worker: a fresh link (ports: down, ctl, up).
|
|
97
|
+
| { type: "link"; key: string; routerLock?: string }
|
|
98
|
+
// page → worker: drop the links of this key.
|
|
99
|
+
| { type: "unlink"; key: string }
|
|
100
|
+
// worker → page: my links are dead and a call waits; attach me again.
|
|
101
|
+
| { type: "relink" }
|
|
102
|
+
// worker → page: a call is in flight; keep the ServiceWorker awake.
|
|
103
|
+
| { type: "keepalive" };
|
package/src/sw/sw-dispatcher.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
handleClaimRequests,
|
|
9
9
|
ServiceWorkerControlError,
|
|
10
10
|
} from "../core/service-worker-control.js";
|
|
11
|
+
import { resolveWorkerUrl } from "./worker-url.js";
|
|
11
12
|
|
|
12
13
|
export interface SwPortHandlerOptions {
|
|
13
14
|
key: string;
|
|
@@ -347,23 +348,3 @@ export class SwPortDispatcher {
|
|
|
347
348
|
return index;
|
|
348
349
|
}
|
|
349
350
|
}
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Resolve a `serviceWorkerUrl` option the way a page would: relative to the
|
|
353
|
-
* current document. Absolute urls pass through untouched. A url that cannot
|
|
354
|
-
* be resolved is reported with the option name and the offending value,
|
|
355
|
-
* because the raw `TypeError: Failed to construct 'URL': Invalid URL` says
|
|
356
|
-
* neither.
|
|
357
|
-
*/
|
|
358
|
-
function resolveWorkerUrl(serviceWorkerUrl: string): URL {
|
|
359
|
-
const base = globalThis.location?.href;
|
|
360
|
-
try {
|
|
361
|
-
return new URL(serviceWorkerUrl, base);
|
|
362
|
-
} catch (error) {
|
|
363
|
-
throw new Error(
|
|
364
|
-
`Invalid serviceWorkerUrl: ${JSON.stringify(serviceWorkerUrl)}` +
|
|
365
|
-
(base ? ` (relative to ${base})` : " (no document to resolve it against)"),
|
|
366
|
-
{ cause: error },
|
|
367
|
-
);
|
|
368
|
-
}
|
|
369
|
-
}
|