@statewalker/webrun-http-browser 0.3.4 → 0.4.2
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 +87 -2
- package/dist/core/data-channels.d.ts.map +1 -1
- package/dist/core/message-target.d.ts +1 -15
- package/dist/core/message-target.d.ts.map +1 -1
- package/dist/http/http-send-recieve.d.ts +27 -14
- package/dist/http/http-send-recieve.d.ts.map +1 -1
- package/dist/index.js +250 -121
- package/dist/relay-sw.js +848 -66
- package/dist/sw/sw-dispatcher.d.ts.map +1 -1
- package/dist/sw-worker.js +848 -64
- package/dist/sw.js +879 -72
- package/package.json +4 -3
- package/src/core/data-channels.ts +42 -4
- package/src/core/message-target.ts +8 -18
- package/src/http/http-send-recieve.ts +27 -14
- package/src/sw/sw-dispatcher.ts +26 -1
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -30,6 +30,15 @@ the raw platform APIs, and this package fills them:
|
|
|
30
30
|
Combining both modes means the same handler code works in an app you
|
|
31
31
|
control *and* in an embed you don't.
|
|
32
32
|
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
npm install @statewalker/webrun-http-browser
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Browser-only — it needs `navigator.serviceWorker`, so a secure context
|
|
40
|
+
(`https://` or `localhost`) is required. No peer dependencies.
|
|
41
|
+
|
|
33
42
|
## How to use
|
|
34
43
|
|
|
35
44
|
```sh
|
|
@@ -38,7 +47,7 @@ npm install @statewalker/webrun-http-browser
|
|
|
38
47
|
|
|
39
48
|
| Subpath | Purpose |
|
|
40
49
|
| --- | --- |
|
|
41
|
-
| `@statewalker/webrun-http-browser` | Page-side relay API: `newRemoteRelayChannel`, `initHttpService`, `callHttpService`, `splitServiceUrl`, `initServiceWorker`, `newServiceWorkerPort`, `getRelayWindowMessageHandler`; the MessagePort call primitives (`callChannel`, `handleChannelCalls`, `newInvokationChannel`, `sendStream`, `handleStreams`, `newRegistry`); plus everything re-exported from `@statewalker/webrun-http-streams` (`HttpError`, the client/server stubs)
|
|
50
|
+
| `@statewalker/webrun-http-browser` | Page-side relay API: `newRemoteRelayChannel`, `initHttpService`, `callHttpService`, `splitServiceUrl`, `initServiceWorker`, `newServiceWorkerPort`, `getRelayWindowMessageHandler`; the MessagePort call primitives (`callChannel`, `handleChannelCalls`, `newInvokationChannel`, `sendStream`, `handleStreams`, `newRegistry`); plus everything re-exported from `@statewalker/webrun-http-streams` (`HttpError`, the client/server stubs), `@statewalker/webrun-streams` (stream and error helpers) and the `MessageTarget` family from `@statewalker/webrun-rpc` |
|
|
42
51
|
| `@statewalker/webrun-http-browser/sw` | Same-origin adapter classes: `SwHttpAdapter` (page), `SwHttpDispatcher` (SW), `startHttpDispatcher` bootstrap |
|
|
43
52
|
| `@statewalker/webrun-http-browser/relay-sw` | IIFE bundle of the relay SW runtime — load via `importScripts` from a loader script in your relay origin |
|
|
44
53
|
| `@statewalker/webrun-http-browser/sw-worker` | IIFE bundle of the same-origin SW runtime — ditto, for same-origin apps |
|
|
@@ -215,6 +224,82 @@ Why it's interesting:
|
|
|
215
224
|
no tooling. Shows how small the glue between a platform API and a
|
|
216
225
|
`(Request) ⇒ Response` handler can be.
|
|
217
226
|
|
|
227
|
+
## Exports
|
|
228
|
+
|
|
229
|
+
The package root re-exports everything from
|
|
230
|
+
[`@statewalker/webrun-streams`](../webrun-streams) and
|
|
231
|
+
[`@statewalker/webrun-http-streams`](../webrun-http-streams), so existing
|
|
232
|
+
imports keep working after those extractions. Its own surface is below.
|
|
233
|
+
|
|
234
|
+
### Relay mode
|
|
235
|
+
|
|
236
|
+
| Export | Kind | Purpose |
|
|
237
|
+
| --- | --- | --- |
|
|
238
|
+
| `newRemoteRelayChannel(opts?)` | function | Embeds the hidden relay iframe, handshakes a `MessageChannel`, resolves a `RemoteRelayChannel`. |
|
|
239
|
+
| `RemoteRelayChannelOptions` | interface | `baseUrl`, `url`, `container` — where the relay lives and what to append the iframe to. |
|
|
240
|
+
| `RemoteRelayChannel` | interface | `{ baseUrl, port, close() }`. |
|
|
241
|
+
| `initHttpService(handler, opts)` | function | Registers `handler` as the server for a service `key` on the relay. Returns a cleanup. |
|
|
242
|
+
| `callHttpService(request, opts)` | function | Sends a `Request` to the service under `key`; resolves its `Response`. |
|
|
243
|
+
| `ServiceOptions` | interface | `{ key: string; port: MessageTarget }` — shared by the two above. |
|
|
244
|
+
| `getRelayWindowMessageHandler(opts?)` | function | The `window.onmessage` handler that runs *inside* the relay iframe. |
|
|
245
|
+
| `RelayWindowHandlerOptions` | interface | `swUrl`, `scopeUrl` for that handler. |
|
|
246
|
+
| `splitServiceUrl(url, separator?)` | function | Splits a relay URL into service key + remaining path (default separator `~`). |
|
|
247
|
+
| `SplitServiceUrl` | interface | Its result shape. |
|
|
248
|
+
|
|
249
|
+
### ServiceWorker lifecycle
|
|
250
|
+
|
|
251
|
+
| Export | Kind | Purpose |
|
|
252
|
+
| --- | --- | --- |
|
|
253
|
+
| `initServiceWorker(opts)` | function | Registers a SW and resolves once it is activated **and controlling the page**. |
|
|
254
|
+
| `InitServiceWorkerOptions` | interface | `{ swUrl, scopeUrl?, type? }`. |
|
|
255
|
+
| `newServiceWorkerPort()` | function | A `MessagePort` that transparently bridges to the controlling SW. |
|
|
256
|
+
|
|
257
|
+
### Connection registry
|
|
258
|
+
|
|
259
|
+
| Export | Kind | Purpose |
|
|
260
|
+
| --- | --- | --- |
|
|
261
|
+
| `initializeConnection(opts)` | function | Sends `CONNECT` for a service `key`; resolves a `MessagePort`, or `null` if no such service. |
|
|
262
|
+
| `InitializeConnectionOptions` | interface | `{ key, communicationPort, ...extra }` — extra fields ride along in the CONNECT payload. |
|
|
263
|
+
| `registerConnectionsHandler(opts)` | function | Registers a `key` and answers inbound `CONNECT`s. Returns a cleanup that unregisters. |
|
|
264
|
+
| `RegisterConnectionsHandlerOptions` | interface | `{ key, handler, communicationPort }`. |
|
|
265
|
+
|
|
266
|
+
### Messaging primitives
|
|
267
|
+
|
|
268
|
+
| Export | Kind | Purpose |
|
|
269
|
+
| --- | --- | --- |
|
|
270
|
+
| `callChannel(target, type, data, port?)` | function | One typed request/response over a `MessageTarget`. |
|
|
271
|
+
| `handleChannelCalls(target, type, handler)` | function | Answer those calls. Returns an unsubscribe. |
|
|
272
|
+
| `ChannelCallHandler` | type | The handler signature the two above exchange. |
|
|
273
|
+
| `newInvokationChannel(opts)` | function | Multiplexed invocations over one target. |
|
|
274
|
+
| `InvocationChannel` / `NewInvocationChannelOptions` | interface | Its result and options. |
|
|
275
|
+
| `handleStreams(...)` / `StreamHandler<T>` | function / type | Stream-shaped invocations over the same channel. |
|
|
276
|
+
|
|
277
|
+
> **These stream primitives have no backpressure.** `sendStream`'s chunk sender
|
|
278
|
+
> discards the promise it is handed, so a fast producer over a slow consumer
|
|
279
|
+
> accumulates without bound; there is also no per-stream timeout and no chunking
|
|
280
|
+
> to a transport's message ceiling. `@statewalker/webrun-rpc`'s `duplexOverPort`
|
|
281
|
+
> is the replacement — one `Duplex` over one port, with the confirmation
|
|
282
|
+
> withheld until the consumer has pulled. Migrating this package onto it is
|
|
283
|
+
> planned, not done.
|
|
284
|
+
| `MessageTarget` / `MessageSource` / `MessageSink` / `MessageListener` | interface / type | The structural port view everything above accepts — a `MessagePort`, a `Worker`, or a SW bridge. Defined in [`@statewalker/webrun-streams`](../webrun-streams) and re-exported here. |
|
|
285
|
+
| `newRegistry(onError?)` | function | Small cleanup registry used for teardown. |
|
|
286
|
+
| `Registry` / `NewRegistryResult` / `CleanupAction` | interface / type | Its shapes. |
|
|
287
|
+
|
|
288
|
+
### HTTP over a port
|
|
289
|
+
|
|
290
|
+
| Export | Kind | Purpose |
|
|
291
|
+
| --- | --- | --- |
|
|
292
|
+
| `sendHttpRequest(port, request)` | function | **Deprecated.** Ship a `Request` over a `MessageTarget`, await the `Response`. |
|
|
293
|
+
| `handleHttpRequests(port, handler)` | function | **Deprecated.** Serve an `HttpHandler` on the other end of one. |
|
|
294
|
+
|
|
295
|
+
### Subpath entry points
|
|
296
|
+
|
|
297
|
+
| Entry | Purpose |
|
|
298
|
+
| --- | --- |
|
|
299
|
+
| `@statewalker/webrun-http-browser/sw` | `SwHttpAdapter` — the same-origin ServiceWorker adapter. |
|
|
300
|
+
| `@statewalker/webrun-http-browser/relay-sw` | IIFE relay SW runtime, loadable via `importScripts(...)`. |
|
|
301
|
+
| `@statewalker/webrun-http-browser/sw-worker` | IIFE same-origin SW runtime, loadable via `importScripts(...)`. |
|
|
302
|
+
|
|
218
303
|
## Internals
|
|
219
304
|
|
|
220
305
|
### Source layout
|
|
@@ -340,4 +425,4 @@ root).
|
|
|
340
425
|
|
|
341
426
|
## License
|
|
342
427
|
|
|
343
|
-
MIT © statewalker
|
|
428
|
+
MIT © statewalker — see [LICENSE](../../LICENSE).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-channels.d.ts","sourceRoot":"","sources":["../../src/core/data-channels.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAqBzD,MAAM,WAAW,iBAAiB;IAChC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,MAAM,CAAC,CAAC,GAAG,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,GAAG,SAAS,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAClF;AAED,MAAM,WAAW,2BAA2B;IAC1C,IAAI,EAAE,aAAa,CAAC;IACpB,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,KAAK,EAAE,WAAW,EAAE,KAAK,OAAO,CAAC;IACjE,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IACnC,SAAS,CAAC,EAAE,MAAM,MAAM,CAAC;CAC1B;AAED,wB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MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACnC,cAAc,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAmBlC;AAED,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAC7B,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EACvB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC5B,aAAa,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;AAElD,wBAAgB,aAAa,CAAC,CAAC,EAC7B,iBAAiB,EAAE,aAAa,EAChC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,GACxB,MAAM,IAAI,CAqBZ"}
|
|
@@ -1,16 +1,2 @@
|
|
|
1
|
-
export type MessageListener
|
|
2
|
-
/** An object we can listen for `"message"` events on. */
|
|
3
|
-
export interface MessageSource {
|
|
4
|
-
addEventListener(type: "message", listener: MessageListener): void;
|
|
5
|
-
removeEventListener(type: "message", listener: MessageListener): void;
|
|
6
|
-
start?(): void | Promise<void>;
|
|
7
|
-
}
|
|
8
|
-
/** An object we can post messages to (with optional transferable list). */
|
|
9
|
-
export interface MessageSink {
|
|
10
|
-
postMessage(message: unknown, transfer?: Transferable[]): void;
|
|
11
|
-
}
|
|
12
|
-
/** Full-duplex message target: both sends and receives. */
|
|
13
|
-
export interface MessageTarget extends MessageSource, MessageSink {
|
|
14
|
-
close?(): void | Promise<void>;
|
|
15
|
-
}
|
|
1
|
+
export type { MessageListener, MessageSink, MessageSource, MessageTarget, } from "@statewalker/webrun-rpc";
|
|
16
2
|
//# sourceMappingURL=message-target.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-target.d.ts","sourceRoot":"","sources":["../../src/core/message-target.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"message-target.d.ts","sourceRoot":"","sources":["../../src/core/message-target.ts"],"names":[],"mappings":"AAEA,YAAY,EACV,eAAe,EACf,WAAW,EACX,aAAa,EACb,aAAa,GACd,MAAM,yBAAyB,CAAC"}
|
|
@@ -1,23 +1,36 @@
|
|
|
1
1
|
import { type HttpHandler } from "@statewalker/webrun-http-streams";
|
|
2
2
|
import type { MessageTarget } from "../core/message-target.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* `
|
|
10
|
-
* `
|
|
4
|
+
* Serve an `HttpHandler` over a `MessageTarget`, using this package's own
|
|
5
|
+
* `handleStreams` transport.
|
|
6
|
+
*
|
|
7
|
+
* @deprecated Prefer the port stack in `@statewalker/webrun-rpc`: open a port
|
|
8
|
+
* (`multiplexPort` over one pipe, or `transferPortMux` where the platform can
|
|
9
|
+
* transfer a real `MessagePort`), turn it into a `Duplex` with
|
|
10
|
+
* `serveDuplexOverPort`, and serve HTTP on that with
|
|
11
|
+
* `httpServe(handler, options)` from `@statewalker/webrun-http-streams`.
|
|
12
|
+
*
|
|
13
|
+
* That path has backpressure; **this one does not.** `sendStream`'s chunk
|
|
14
|
+
* sender discards the promise it is given, so a fast producer over a slow
|
|
15
|
+
* consumer accumulates without bound. It also has no per-stream timeout and no
|
|
16
|
+
* chunking to a transport's message ceiling.
|
|
17
|
+
*
|
|
18
|
+
* Kept for existing ServiceWorker setups built on the `MessageTarget` surface.
|
|
11
19
|
*/
|
|
12
20
|
export declare function handleHttpRequests(communicationPort: MessageTarget, handler: HttpHandler): () => void;
|
|
13
21
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* `
|
|
22
|
+
* Ship a `Request` over a `MessageTarget` and await the `Response`, using this
|
|
23
|
+
* package's own `sendStream` transport.
|
|
24
|
+
*
|
|
25
|
+
* @deprecated Prefer the port stack in `@statewalker/webrun-rpc`: open a port
|
|
26
|
+
* (`multiplexPort` over one pipe, or `transferPortMux` where the platform can
|
|
27
|
+
* transfer a real `MessagePort`), turn it into a `Duplex` with
|
|
28
|
+
* `duplexOverPort`, and drive HTTP over it with `httpFetch` from
|
|
29
|
+
* `@statewalker/webrun-http-streams`.
|
|
30
|
+
*
|
|
31
|
+
* Same caveat as {@link handleHttpRequests}: the transport underneath this
|
|
32
|
+
* helper has no backpressure, no per-stream timeout, and no chunking to a
|
|
33
|
+
* transport's message ceiling. Kept for existing ServiceWorker setups.
|
|
21
34
|
*/
|
|
22
35
|
export declare function sendHttpRequest(communicationPort: MessageTarget, request: Request): Promise<Response>;
|
|
23
36
|
//# sourceMappingURL=http-send-recieve.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-send-recieve.d.ts","sourceRoot":"","sources":["../../src/http/http-send-recieve.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAMjB,MAAM,kCAAkC,CAAC;AAE1C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AA+B/D
|
|
1
|
+
{"version":3,"file":"http-send-recieve.d.ts","sourceRoot":"","sources":["../../src/http/http-send-recieve.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAMjB,MAAM,kCAAkC,CAAC;AAE1C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AA+B/D;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB,CAChC,iBAAiB,EAAE,aAAa,EAChC,OAAO,EAAE,WAAW,GACnB,MAAM,IAAI,CAOZ;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,eAAe,CACnC,iBAAiB,EAAE,aAAa,EAChC,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,QAAQ,CAAC,CAOnB"}
|