UNPKG

linkinator

Version:

Find broken links, missing images, etc in your HTML. Scurry around your site and find all those broken links.

53 lines (52 loc) 1.83 kB
import { Readable } from 'node:stream'; /** * Drains a response body stream without consuming its data. * This is important for connection pooling - if the body is not consumed, * the underlying TCP connection may not be returned to the pool, leading * to port exhaustion under high load. * @param body The response body stream to drain */ export async function drainStream(body) { if (!body) return; try { if ('cancel' in body && typeof body.cancel === 'function') { // Web ReadableStream - cancel it to release the connection await body.cancel(); } else if ('destroy' in body && typeof body.destroy === 'function') { // Node.js Readable stream - destroy it body.destroy(); } } catch { // Ignore errors when draining - the connection will be cleaned up eventually } } /** * Converts a response body stream to a Node.js Readable stream. * Handles both Web ReadableStreams (from fetch) and Node.js Readable streams (from local server). * @param body The response body stream * @returns A Node.js Readable stream */ export function toNodeReadable(body) { // Check if body is already a Node.js Readable stream if (body && 'pipe' in body) { // Already a Node.js Readable stream (from local server) return body; } // Web ReadableStream (from fetch), convert it return Readable.fromWeb(body); } /** * Buffers a stream's contents into a single Buffer. * @param stream The stream to buffer * @returns Promise that resolves to the buffered content */ export async function bufferStream(stream) { const chunks = []; for await (const chunk of stream) { chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); } return Buffer.concat(chunks); }