h2lean 0.4.2 → 0.8.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 +38 -0
- package/lib/client-session.js +859 -73
- package/lib/client-stream.js +191 -15
- package/lib/client.js +33 -1
- package/lib/compat.js +76 -29
- package/lib/frames.js +23 -5
- package/lib/headers.js +190 -4
- package/lib/hpack.js +49 -2
- package/lib/server.js +9 -1
- package/lib/session.js +704 -49
- package/lib/stream.js +245 -20
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -29,6 +29,7 @@ Up to **8× less CPU per request** and **6.8× more requests per second** than `
|
|
|
29
29
|
- [Secure server (h2s / TLS)](#secure-server-h2s--tls)
|
|
30
30
|
- [Client — `connect()`](#client--connect)
|
|
31
31
|
- [Fastify and other frameworks](#fastify-and-other-frameworks)
|
|
32
|
+
- [h2lean as the HTTP/2 client of an undici `fetch`](#h2lean-as-the-http2-client-of-an-undici-fetch)
|
|
32
33
|
- [Monkey-patching for frameworks without injection](#monkey-patching-for-frameworks-without-injection)
|
|
33
34
|
- 🛠️ [Advanced Tuning](#️-advanced-tuning)
|
|
34
35
|
- 🔒 [Security](#-security)
|
|
@@ -196,6 +197,43 @@ fastify.get('/', async () => ({ hello: 'from h2lean under Fastify' }));
|
|
|
196
197
|
fastify.listen({ port: 8080 });
|
|
197
198
|
```
|
|
198
199
|
|
|
200
|
+
### h2lean as the HTTP/2 client of an undici `fetch`
|
|
201
|
+
|
|
202
|
+
`connect()` is the client surface, but an application rarely calls it — it calls `fetch` or `undici.request` and never sees the transport. Monkey-patching `node:http2` is one way to put h2lean there; the cleaner way is undici's own seam: **every** request above undici's API funnels through one method, `Dispatcher#dispatch(options, handler)`. Subclass `Dispatcher`, answer that method with `h2lean.connect()`, and `request()`, `fetch()`, `pipeline()`, an `Agent` or a `RetryAgent` on top all run on h2lean's HTTP/2 — frame parsing, HPACK and flow control are ours, undici's machinery untouched. No global patch, and the choice is per call site:
|
|
203
|
+
|
|
204
|
+
```javascript
|
|
205
|
+
const h2lean = require('h2lean');
|
|
206
|
+
const { Dispatcher, fetch } = require('undici');
|
|
207
|
+
|
|
208
|
+
class H2leanDispatcher extends Dispatcher {
|
|
209
|
+
// The single seam. `handler` is undici's (onRequestStart, onResponseStart,
|
|
210
|
+
// onResponseData, onResponseEnd, onResponseError); the controller handed to it
|
|
211
|
+
// is ours — pause()/resume() is undici's backpressure (it becomes flow-control
|
|
212
|
+
// credit) and abort() is an AbortSignal (it becomes RST_STREAM).
|
|
213
|
+
dispatch (options, handler) {
|
|
214
|
+
const stream = this.sessionFor(new URL(options.origin)) // one h2lean session per origin
|
|
215
|
+
.request(/* ':method', ':path', ':scheme', ':authority', then the fields */);
|
|
216
|
+
// ... five stream events forwarded to the handler, the body written, return true
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const res = await fetch('http://localhost:8080/', { dispatcher: new H2leanDispatcher() });
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
[`examples/undici-h2-client.js`](./examples/undici-h2-client.js) is the complete version — 268 lines, 56 of them comments — and its peer is node's **own** `node:http2` server, so every 200 is node's nghttp2 accepting what h2lean's client put on the wire. It covers the controller contract, `AbortSignal` → `RST_STREAM`, string/`Readable`/async-iterable request bodies, trailers, session reuse, and undici's `connect`/`disconnect` events:
|
|
224
|
+
|
|
225
|
+
```
|
|
226
|
+
event connect -> http://127.0.0.1:52108
|
|
227
|
+
undici.request GET -> 200 via node:http2 "hello from node:http2 (method=GET, x-test=kept)\n"
|
|
228
|
+
fetch POST /echo -> 200 "round trip"
|
|
229
|
+
streaming POST -> 200 "chunk-1:chunk-2:chunk-3" trailer {"x-checksum":"ok"}
|
|
230
|
+
connections used -> 1 socket, 3 streams
|
|
231
|
+
cancel via signal -> AbortError: This operation was aborted
|
|
232
|
+
event disconnect -> http://127.0.0.1:52108
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
undici also has its own HTTP/2 (`allowH2: true` on a `Client` over TLS, `H2CClient` over cleartext) — what it has no way to do is select somebody else's protocol engine, until you replace the `Dispatcher`. For the other direction, h2lean as the server speaking to a third-party client, the test suite and `npm run spec` are the interop gate.
|
|
236
|
+
|
|
199
237
|
### Monkey-patching for frameworks without injection
|
|
200
238
|
|
|
201
239
|
If a framework requires `node:http2` internally and offers no injection point, patch the builtin's exports **before anything else loads** — every later `require('node:http2')` receives the same (mutated) module object:
|