@unireq/http2
v1.0.2
Published
HTTP/2 transport for unireq using Node.js http2 module
Readme
@unireq/http2
Dedicated HTTP/2 transport powered by Node's http2 module. Use it when you need strict HTTP/2 semantics (multiplexing, server push, explicit ALPN) instead of the default Undici-based HTTP/1.1 transport.
Installation
pnpm add @unireq/http2Quick Start
import { client, retry, backoff } from '@unireq/core';
import { http2, Http2Connector } from '@unireq/http2';
const h2 = client(
http2('https://h2.example.com', new Http2Connector({ sessionTimeout: 45_000 })),
retry(undefined, [backoff()], { tries: 3 }),
);
const resp = await h2.get('/products');Features
| Export | Description |
| --- | --- |
| http2(uri?, connector?) | Returns a transport with streams, http2, serverPush capabilities |
| Http2Connector | Default connector with ALPN negotiation, session caching, graceful teardown |
| Http2ConnectorOptions | { enablePush?, sessionTimeout? } configuration |
URL Resolution
- Pass
http2('https://api.example.com')to prefix relative paths with the base - Without a base URI, all URLs must be absolute
- The first request lazily establishes a session; subsequent requests reuse it
Custom Connectors
class InstrumentedConnector extends Http2Connector {
async request(client, ctx) {
const start = performance.now();
try {
return await super.request(client, ctx);
} finally {
metrics.timing('http2.duration', performance.now() - start);
}
}
}
const api = client(http2('https://api.internal', new InstrumentedConnector()));Error Handling
- Connection failures, GOAWAY frames, and request timeouts bubble up as rejected promises
- Wrap the transport with
retry/backofffor resilience - The connector automatically removes broken sessions from cache
Documentation
Full documentation available at unireq.dev
License
MIT
