fetch-http2
Advanced tools
Comparing version
@@ -6,2 +6,3 @@ /// <reference types="node" /> | ||
import { ClientHttp2Stream, IncomingHttpHeaders, OutgoingHttpHeaders } from 'node:http2'; | ||
import { Http2TimeoutError } from './http2'; | ||
export type RequestInfo = string | URL; | ||
@@ -27,2 +28,3 @@ export interface RequestInit { | ||
} | ||
export { Http2TimeoutError }; | ||
export declare function fetch(input: RequestInfo, init?: RequestInit): Promise<Response>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fetch = void 0; | ||
exports.fetch = exports.Http2TimeoutError = void 0; | ||
const http2_1 = require("./http2"); | ||
Object.defineProperty(exports, "Http2TimeoutError", { enumerable: true, get: function () { return http2_1.Http2TimeoutError; } }); | ||
async function fetch(input, init) { | ||
@@ -9,3 +10,3 @@ // Parse input url | ||
// Send http request | ||
const res = await (0, http2_1._fetch)(url, { | ||
const res = await (0, http2_1.http2Fetch)(url, { | ||
method: init?.method, | ||
@@ -12,0 +13,0 @@ headers: init?.headers, |
@@ -5,4 +5,4 @@ /// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { IncomingHttpHeaders, OutgoingHttpHeaders, ClientHttp2Stream } from 'node:http2'; | ||
interface _FetchResponse { | ||
import { ClientHttp2Stream, IncomingHttpHeaders, OutgoingHttpHeaders } from 'node:http2'; | ||
interface Http2FetchResponse { | ||
status: number; | ||
@@ -14,3 +14,3 @@ statusText: string; | ||
} | ||
interface _FetchOptions { | ||
interface Http2FetchOptions { | ||
method?: string; | ||
@@ -22,3 +22,7 @@ headers?: OutgoingHttpHeaders; | ||
} | ||
export declare function _fetch(url: URL, options?: _FetchOptions): Promise<_FetchResponse>; | ||
export declare class Http2TimeoutError extends Error { | ||
code: string | number; | ||
constructor(message: string); | ||
} | ||
export declare function http2Fetch(url: URL, options?: Http2FetchOptions): Promise<Http2FetchResponse>; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports._fetch = void 0; | ||
exports.http2Fetch = exports.Http2TimeoutError = void 0; | ||
const node_http2_1 = require("node:http2"); | ||
const node_timers_1 = require("node:timers"); | ||
const node_http2_1 = require("node:http2"); | ||
const defaultPingInterval = 20000; | ||
async function _fetch(url, options) { | ||
class Http2TimeoutError extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = 'TimeoutError'; | ||
this.code = node_http2_1.constants.NGHTTP2_CANCEL; | ||
} | ||
} | ||
exports.Http2TimeoutError = Http2TimeoutError; | ||
async function http2Fetch(url, options) { | ||
// Construct url | ||
const { origin, pathname, search } = url; | ||
// Find or create http client | ||
const client = _httpClient(origin, { | ||
const client = httpClient(origin, { | ||
pingInterval: parsePingInterval(options?.keepAlive) | ||
@@ -23,3 +31,3 @@ }); | ||
// Fetch the headers | ||
const { headers, status } = await _sendRequest(req, options); | ||
const { headers, status } = await sendRequest(req, options); | ||
// Get status text | ||
@@ -32,10 +40,10 @@ const statusText = getStatusText(status); | ||
body: req, | ||
buffer: () => _responseBuffer(req) | ||
buffer: () => responseBuffer(req) | ||
}; | ||
} | ||
exports._fetch = _fetch; | ||
const _clientCache = {}; | ||
function _httpClient(origin, options) { | ||
exports.http2Fetch = http2Fetch; | ||
const clientCache = {}; | ||
function httpClient(origin, options) { | ||
// Look for cached client | ||
const cachedClient = _clientCache[origin]; | ||
const cachedClient = clientCache[origin]; | ||
// Return cached client if we have one | ||
@@ -48,3 +56,3 @@ if (cachedClient) { | ||
// Set client cache | ||
_clientCache[origin] = client; | ||
clientCache[origin] = client; | ||
// Setup keep alive | ||
@@ -59,3 +67,3 @@ let timer; | ||
(0, node_timers_1.clearInterval)(timer); | ||
_destroyClient(client, origin); | ||
destroyClient(client, origin); | ||
}; | ||
@@ -71,5 +79,5 @@ // Handle client errors | ||
} | ||
function _destroyClient(client, origin) { | ||
function destroyClient(client, origin) { | ||
// Remove the client from cache | ||
_clientCache[origin] = undefined; | ||
clientCache[origin] = undefined; | ||
// Close the client | ||
@@ -80,11 +88,14 @@ if (client.closed !== true) { | ||
} | ||
function _sendRequest(req, options) { | ||
function sendRequest(req, options) { | ||
return new Promise((resolve, reject) => { | ||
// Write request body if needed | ||
if (options && options.body) { | ||
if (options?.body) { | ||
req.write(options.body); | ||
} | ||
// Apply optional timeout | ||
if (options && typeof options.timeout === 'number') { | ||
req.setTimeout(options.timeout, reject); | ||
if (typeof options?.timeout === 'number') { | ||
req.setTimeout(options.timeout, () => { | ||
req.close(node_http2_1.constants.NGHTTP2_CANCEL); | ||
reject(new Http2TimeoutError(`Request timed out after ${options.timeout}ms`)); | ||
}); | ||
} | ||
@@ -102,3 +113,3 @@ // Add error handler | ||
} | ||
function _responseBuffer(req) { | ||
function responseBuffer(req) { | ||
return new Promise((resolve, reject) => { | ||
@@ -105,0 +116,0 @@ const chunks = []; |
{ | ||
"name": "fetch-http2", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Native http2 fetch implementation for Node.js", | ||
@@ -15,5 +15,3 @@ "author": "Andrew Barba <barba@hey.com>", | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"files": ["dist"], | ||
"keywords": [ | ||
@@ -32,12 +30,7 @@ "fetch", | ||
"devDependencies": { | ||
"@biomejs/biome": "^1.2.2", | ||
"@tsconfig/node16": "^16.1.1", | ||
"@types/chai": "^4.3.6", | ||
"@types/node": "^20.6.0", | ||
"@typescript-eslint/eslint-plugin": "^6.6.0", | ||
"@typescript-eslint/parser": "^6.6.0", | ||
"@types/node": "^20.6.2", | ||
"chai": "^4.3.8", | ||
"eslint": "^8.49.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-prettier": "^5.0.0", | ||
"prettier": "^3.0.3", | ||
"typescript": "^5.2.2", | ||
@@ -48,5 +41,5 @@ "vitest": "^0.34.4" | ||
"build": "tsc", | ||
"lint": "eslint src test", | ||
"lint": "biome ci src test", | ||
"test": "vitest --run --dir test" | ||
} | ||
} |
11778
5.43%7
-41.67%289
6.64%