@push-stream-std/push-fetch (0.0.3)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-fetch@0.0.3"@push-stream-std/push-fetch": "0.0.3"About this package
push-fetch
Fetch API as a push-stream source.
Published as @push-stream-std/push-fetch.
Synopsis
import { fetch } from '@push-stream-std/push-fetch';
fetch('https://jsonplaceholder.typicode.com/posts/1').pipe({
write(data) { process.stdout.write(data); },
end(err) { if (err) console.error(err); else process.stdout.write('\n'); },
abort(err) { console.error('aborted:', err); },
paused: false
});
Install
npm config set @push-stream-std:registry https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/
npm config set -- //hub.kl1.tenere.ai/api/packages/push-stream-std/npm/:_authToken "$PACKAGE_TOKEN"
npm install @push-stream-std/push-fetch
Description
Wraps the standard Fetch API as a push-stream source. The response body is streamed in chunks as Buffer objects. Supports all standard fetch options including method, headers, body, redirect, and credentials. Aborting the source via source.abort() cancels the underlying request using AbortController. HTTP errors (non-2xx status codes) are delivered as errors to the sink's end callback.
API
fetch(url, options)
url-string- The URL to fetch.options.method-string(default:'GET') - HTTP method.options.headers-object(default:{}) - Request headers.options.body-any(default:null) - Request body.options.redirect-string(default:'follow') - Redirect handling mode.options.credentials-string(default:'same-origin') - Credentials mode.
Returns a push-stream source. Pipe it to a sink to receive response body chunks (Buffer objects). Call source.abort(err) to cancel the request.
Tests
npm test
Tests cover text and JSON responses, HTTP error handling (500, 404), streaming chunked responses, custom fetch options, request abort, and backpressure with resume.
Requirements
Node.js 18 or later. ESM only.
License
MIT