@statewalker/webrun-http-streams 0.2.1 → 0.2.4
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 +74 -3
- package/dist/duplex-site-builder.d.ts +1 -1
- package/dist/http1/encode.d.ts.map +1 -1
- package/dist/http1/headers.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +12 -6
- package/src/duplex-site-builder.ts +1 -1
- package/src/http1/encode.ts +2 -0
- package/src/http1/headers.ts +1 -0
package/README.md
CHANGED
|
@@ -3,6 +3,63 @@
|
|
|
3
3
|
HTTP request / response over a `Duplex` from any `webrun-streams-*` adapter.
|
|
4
4
|
Replaces the retired `webrun-http` + `webrun-http-port` packages.
|
|
5
5
|
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @statewalker/webrun-http-streams
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
One runtime dependency, [`@statewalker/webrun-streams`](../webrun-streams); no
|
|
13
|
+
peer dependencies. ESM only (`"type": "module"`). Needs standard `Request`,
|
|
14
|
+
`Response`, `ReadableStream`, `TextEncoder` and `TextDecoder` — present in
|
|
15
|
+
browsers, Node 18+, Deno, Bun and Cloudflare Workers.
|
|
16
|
+
|
|
17
|
+
Pair it with a transport adapter to get a `Duplex`:
|
|
18
|
+
[`-ws`](../webrun-streams-ws), [`webrun-rpc`](../webrun-rpc),
|
|
19
|
+
[`-webrtc`](../webrun-streams-webrtc), [`-libp2p`](../webrun-streams-libp2p),
|
|
20
|
+
[`-livekit`](../webrun-streams-livekit), [`-peerjs`](../webrun-streams-peerjs).
|
|
21
|
+
|
|
22
|
+
## Getting started
|
|
23
|
+
|
|
24
|
+
Pick a transport, then move standard `Request` / `Response` objects across it.
|
|
25
|
+
Here the transport is an in-process `MessageChannel`, so the whole exchange runs
|
|
26
|
+
in one process with no network:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { connect, serve } from "@statewalker/webrun-rpc";
|
|
30
|
+
import { fetchOverDuplex, serveFetchOverDuplex } from "@statewalker/webrun-http-streams";
|
|
31
|
+
|
|
32
|
+
// --- server side ---
|
|
33
|
+
const channel = new MessageChannel();
|
|
34
|
+
const stop = await serve(
|
|
35
|
+
{ port: channel.port2 },
|
|
36
|
+
serveFetchOverDuplex(async (request) => {
|
|
37
|
+
const url = new URL(request.url);
|
|
38
|
+
return Response.json({ path: url.pathname, method: request.method });
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// --- client side ---
|
|
43
|
+
const { call, close } = await connect({ port: channel.port1 });
|
|
44
|
+
const response = await fetchOverDuplex(call, new Request("http://local/api/todo/7"));
|
|
45
|
+
|
|
46
|
+
console.log(response.status); // 200
|
|
47
|
+
console.log(await response.json()); // { path: "/api/todo/7", method: "GET" }
|
|
48
|
+
|
|
49
|
+
await close();
|
|
50
|
+
await stop();
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Swap `@statewalker/webrun-rpc` for
|
|
54
|
+
[`-ws`](../webrun-streams-ws), [`-webrtc`](../webrun-streams-webrtc),
|
|
55
|
+
[`-libp2p`](../webrun-streams-libp2p), [`-livekit`](../webrun-streams-livekit)
|
|
56
|
+
or [`-peerjs`](../webrun-streams-peerjs) and nothing else changes — that is the
|
|
57
|
+
whole point of the `Duplex` seam.
|
|
58
|
+
|
|
59
|
+
Streaming bodies work as you would expect: a `Response` whose body is a
|
|
60
|
+
`ReadableStream` streams across the transport chunk by chunk, which is what
|
|
61
|
+
makes server-sent events work over a `MessagePort` or a WebRTC link.
|
|
62
|
+
|
|
6
63
|
## Layers
|
|
7
64
|
|
|
8
65
|
Three of them sit on the `Duplex` seam, plus one older transport-agnostic pair
|
|
@@ -73,7 +130,7 @@ signal terminates the call.
|
|
|
73
130
|
|
|
74
131
|
```ts
|
|
75
132
|
import { DuplexSiteBuilder } from "@statewalker/webrun-http-streams";
|
|
76
|
-
import { serve } from "@statewalker/webrun-
|
|
133
|
+
import { serve } from "@statewalker/webrun-rpc";
|
|
77
134
|
|
|
78
135
|
const stop = await new DuplexSiteBuilder()
|
|
79
136
|
.setHandler(siteHandler) // (Request) => Promise<Response>
|
|
@@ -88,7 +145,7 @@ own; endpoints, files and auth belong to the `SiteHandler` producer (typically
|
|
|
88
145
|
`SiteBuilder`).
|
|
89
146
|
|
|
90
147
|
> Not runnable as written above: it needs a live `MessagePort` and
|
|
91
|
-
> `@statewalker/webrun-
|
|
148
|
+
> `@statewalker/webrun-rpc`, which this package does not depend on.
|
|
92
149
|
> `start` accepts any `Serve<P>`, so an in-process one is enough to exercise
|
|
93
150
|
> it. Calling `start()` before `setHandler()` throws.
|
|
94
151
|
|
|
@@ -314,6 +371,20 @@ exception.
|
|
|
314
371
|
| `PEER_ERROR_HEADER` | `"x-webrun-error"`. |
|
|
315
372
|
| `MessageCodec`, `ByteSource`, `RequestEnvelope`, `ResponseEnvelope`, `DecodedRequest`, `DecodedResponse`, `ResponseCodecOptions` | The codec seam's types. |
|
|
316
373
|
|
|
374
|
+
## Dependencies
|
|
375
|
+
|
|
376
|
+
| Dependency | Kind | Why |
|
|
377
|
+
| --- | --- | --- |
|
|
378
|
+
| [`@statewalker/webrun-streams`](../webrun-streams) | runtime | The `Duplex` seam, chunk collectors and error serialisation. |
|
|
379
|
+
|
|
380
|
+
No peer dependencies and no runtime dependencies outside the workspace. Relies
|
|
381
|
+
on the platform for `Request`, `Response`, `ReadableStream`, `TextEncoder` and
|
|
382
|
+
`TextDecoder`.
|
|
383
|
+
|
|
384
|
+
Downstream consumers in this workspace:
|
|
385
|
+
[`webrun-http-browser`](../webrun-http-browser) and
|
|
386
|
+
[`webrun-rpc-http`](../webrun-rpc-http).
|
|
387
|
+
|
|
317
388
|
## Scripts
|
|
318
389
|
|
|
319
390
|
```sh
|
|
@@ -324,4 +395,4 @@ pnpm lint # biome check src tests
|
|
|
324
395
|
|
|
325
396
|
## License
|
|
326
397
|
|
|
327
|
-
MIT
|
|
398
|
+
MIT © statewalker — see [LICENSE](../../LICENSE).
|
|
@@ -12,7 +12,7 @@ export type SiteHandler = (request: Request) => Promise<Response>;
|
|
|
12
12
|
* ```ts
|
|
13
13
|
* import { SiteBuilder } from "@statewalker/webrun-site-builder";
|
|
14
14
|
* import { DuplexSiteBuilder } from "@statewalker/webrun-http-streams";
|
|
15
|
-
* import { serve } from "@statewalker/webrun-
|
|
15
|
+
* import { serve } from "@statewalker/webrun-rpc";
|
|
16
16
|
*
|
|
17
17
|
* const handler = new SiteBuilder()
|
|
18
18
|
* .setEndpoint("/api/time", () => new Response(new Date().toISOString()))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../src/http1/encode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAgBnF,MAAM,MAAM,wBAAwB,GAAG;IACrC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;CACxB,CAAC;AAMF;;;;;;GAMG;AACH,wBAAgB,WAAW,CACzB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,wBAAwB,GAC7B;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,
|
|
1
|
+
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../src/http1/encode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAgBnF,MAAM,MAAM,wBAAwB,GAAG;IACrC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;CACxB,CAAC;AAMF;;;;;;GAMG;AACH,wBAAgB,WAAW,CACzB,GAAG,EA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,KAAK,EAAE,MAAM,GAAG,IAAI,CAExE;AAED,sFAAsF;AACtF,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAEzD;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAQ7D;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,UAAU,EAClB,cAAc,EAAE,MAAM,EACtB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,UAAU,CAAC,CA6BrB;AAED,wBAAgB,MAAM,CAAC,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAGlE;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,UAAU,CAG/E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,SAAS,CAmBvE;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAExD;AAED,MAAM,MAAM,WAAW,GACnB;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GACnB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAErB;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CA+BhF"}
|
package/dist/index.js
CHANGED
|
@@ -1213,7 +1213,7 @@ function abortReason(signal) {
|
|
|
1213
1213
|
* ```ts
|
|
1214
1214
|
* import { SiteBuilder } from "@statewalker/webrun-site-builder";
|
|
1215
1215
|
* import { DuplexSiteBuilder } from "@statewalker/webrun-http-streams";
|
|
1216
|
-
* import { serve } from "@statewalker/webrun-
|
|
1216
|
+
* import { serve } from "@statewalker/webrun-rpc";
|
|
1217
1217
|
*
|
|
1218
1218
|
* const handler = new SiteBuilder()
|
|
1219
1219
|
* .setEndpoint("/api/time", () => new Response(new Date().toISOString()))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/webrun-http-streams",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "HTTP request/response over Duplex — merge of webrun-http + webrun-http-port",
|
|
@@ -15,26 +15,32 @@
|
|
|
15
15
|
"url": "git@github.com:statewalker/webrun-wire.git"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
|
-
".":
|
|
18
|
+
".": {
|
|
19
|
+
"source": "./src/index.ts",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
19
23
|
},
|
|
20
24
|
"files": [
|
|
21
25
|
"dist",
|
|
22
26
|
"src"
|
|
23
27
|
],
|
|
24
28
|
"dependencies": {
|
|
25
|
-
"@statewalker/webrun-streams": "0.
|
|
29
|
+
"@statewalker/webrun-streams": "^0.2.1"
|
|
26
30
|
},
|
|
27
31
|
"devDependencies": {
|
|
28
|
-
"@
|
|
32
|
+
"@biomejs/biome": "^2.5.15",
|
|
33
|
+
"@types/node": "^26.6.4",
|
|
29
34
|
"rimraf": "^6.1.3",
|
|
30
|
-
"rolldown": "^1.2.
|
|
35
|
+
"rolldown": "^1.2.12",
|
|
31
36
|
"typescript": "^7.0.2",
|
|
32
|
-
"vitest": "^
|
|
37
|
+
"vitest": "^5.0.3"
|
|
33
38
|
},
|
|
34
39
|
"sideEffects": false,
|
|
35
40
|
"publishConfig": {
|
|
36
41
|
"access": "public"
|
|
37
42
|
},
|
|
43
|
+
"types": "./dist/index.d.ts",
|
|
38
44
|
"scripts": {
|
|
39
45
|
"build": "rimraf dist && rolldown -c && tsc --emitDeclarationOnly --declaration",
|
|
40
46
|
"test": "vitest run",
|
|
@@ -15,7 +15,7 @@ export type SiteHandler = (request: Request) => Promise<Response>;
|
|
|
15
15
|
* ```ts
|
|
16
16
|
* import { SiteBuilder } from "@statewalker/webrun-site-builder";
|
|
17
17
|
* import { DuplexSiteBuilder } from "@statewalker/webrun-http-streams";
|
|
18
|
-
* import { serve } from "@statewalker/webrun-
|
|
18
|
+
* import { serve } from "@statewalker/webrun-rpc";
|
|
19
19
|
*
|
|
20
20
|
* const handler = new SiteBuilder()
|
|
21
21
|
* .setEndpoint("/api/time", () => new Response(new Date().toISOString()))
|
package/src/http1/encode.ts
CHANGED
|
@@ -50,6 +50,7 @@ export function splitTarget(
|
|
|
50
50
|
// Both capture groups are mandatory in the regex above (neither is
|
|
51
51
|
// `?`-quantified), so a successful match always populates them — an empty
|
|
52
52
|
// string is possible, `undefined` is not.
|
|
53
|
+
// biome-ignore lint/style/noNonNullAssertion: mandatory capture group, per the comment above.
|
|
53
54
|
const rawAuthority = match[1]!;
|
|
54
55
|
if (rawAuthority === "") {
|
|
55
56
|
throw new HttpParseError(`url has no authority: ${JSON.stringify(url)}`);
|
|
@@ -60,6 +61,7 @@ export function splitTarget(
|
|
|
60
61
|
const authority = at === -1 ? rawAuthority : rawAuthority.slice(at + 1);
|
|
61
62
|
assertValidHost(authority);
|
|
62
63
|
|
|
64
|
+
// biome-ignore lint/style/noNonNullAssertion: mandatory capture group, per the comment above.
|
|
63
65
|
const rawTarget = match[2]!;
|
|
64
66
|
const target = rawTarget === "" ? "/" : rawTarget.startsWith("/") ? rawTarget : `/${rawTarget}`;
|
|
65
67
|
assertValidTarget(target);
|
package/src/http1/headers.ts
CHANGED
|
@@ -199,6 +199,7 @@ export function parseContentLength(values: string[]): number | undefined {
|
|
|
199
199
|
// build, not in this package's own (which does not enable the flag, so it
|
|
200
200
|
// cannot catch the regression). It was lost once already, in a rebase that
|
|
201
201
|
// resolved toward a refactor of this function.
|
|
202
|
+
// biome-ignore lint/style/noNonNullAssertion: load-bearing for consumers building with noUncheckedIndexedAccess -- see above; it was lost once already.
|
|
202
203
|
const raw = [...unique][0]!;
|
|
203
204
|
if (!/^\d{1,15}$/.test(raw)) {
|
|
204
205
|
throw new HttpParseError(`invalid Content-Length: ${JSON.stringify(raw)}`);
|