1.0.5 • Published 9 months ago
@joneslloyd/h2alagut v1.0.5
h2Alagút
h2Alagút is a fetch adapter for Node.js that uses HTTP/2 to perform requests and can tunnel through an HTTP/1 proxy via HTTP CONNECT. The name is inspired by the Hungarian word alagút (meaning "tunnel").
Features
- HTTP/2: Utilizes Node's built‑in http2 module.
- Proxy Tunneling: Automatically tunnels through an HTTP/1 proxy (via HTTP CONNECT) when a
proxyconfiguration is provided. - Fetch-like API: Provides a drop‑in replacement for the standard Fetch API. The returned
Responseobject supports properties such asstatusand methods liketext()andjson().
Installation
npm install h2alagut or yarn add h2alagut or pnpm add h2alagut
Usage
import { fetch } from "h2alagut";
const response = await fetch("https://example.com");
console.log(response.statusCode);Proxy Configuration
const response = await fetch("https://example.com", {
proxy: {
host: "your-proxy-server",
port: 8080,
auth: "username:password", // Optional
},
});Response Object
The Response object returned by h2Alagút supports the following properties and methods:
statusCode: The HTTP status code.text(): Returns the response body as a string.json(): Parses the response body as JSON.arrayBuffer(): Returns the response body as anArrayBuffer.
Timeout Handling
You can specify a timeout for the request:
const response = await fetch("https://example.com", {
timeout: 5000, // Timeout in milliseconds
});Error Handling
The adapter throws errors for:
- Invalid URLs
- Proxy authentication failures
- Request timeouts
- HTTP/2 negotiation failures
Example
import { fetch } from "h2alagut";
try {
const response = await fetch("https://example.com", {
proxy: {
host: "proxy.example.com",
port: 8080,
auth: "user:pass",
},
timeout: 3000,
});
console.log(response.status);
console.log(await response.text());
} catch (error) {
console.error("Request failed:", error.message);
}License
MIT