@push-stream-std/push-http-server (0.0.5)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-http-server@0.0.5"@push-stream-std/push-http-server": "0.0.5"About this package
push-http-server
HTTP server as a push-stream source.
Published as @push-stream-std/push-http-server.
Synopsis
import { httpServer, createHandler } from '@push-stream-std/push-http-server';
// Low-level: get raw req/res pairs
httpServer({ host: '127.0.0.1', port: 8080 }).pipe({
write({ req, res, source, sink }) {
console.log(req.method, req.url);
sink.end();
res.end('Hello\n');
},
end(err) { if (err) console.error(err); },
abort(err) { console.error('aborted:', err); },
paused: false
});
// High-level: get parsed request info
const handler = createHandler((req, res) => {
console.log(req.method, req.url, req.headers);
res.writeHead(200);
res.end(JSON.stringify({ body: req.body?.toString() }));
});
handler.listen(8080, () => console.log('listening'));
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-http-server
Description
Wraps Node.js http.createServer as a push-stream source. Two APIs are provided: httpServer emits { req, res, source, sink } objects for each connection, with source/sink being push-stream interfaces wrapping the request body and response. createHandler provides a higher-level API that emits { method, url, headers, body, res } objects after buffering the full request body.
API
httpServer(options)
options.host-string(default:'127.0.0.1') - Host to bind to.options.port-number(default:0) - Port to listen on.0picks a random available port.
Returns a push-stream source. Pipe it to a sink to receive connection objects:
connection.req- Node.jsIncomingMessageconnection.res- Node.jsServerResponseconnection.source- Push-stream source emitting request body chunksconnection.sink- Push-stream sink for writing responses
Call source.abort(err) to close the server.
createHandler(handlerFn)
handlerFn-function(req, res)- Called for each incoming request.
Returns a source-like object with additional methods:
source.pipe(sink)- Pipe to a sink that receives{ method, url, headers, body, res }objects.source.listen(port, [host], [callback])- Start listening on the given port.source.close([callback])- Close the server.source.abort(err)- Close the server with an error.
Tests
npm test
Tests cover source creation, emitting parsed requests with method/url/headers/body.
Requirements
Node.js 18 or later. ESM only.
License
MIT
Dependencies
Dependencies
| ID | Version |
|---|---|
| @push-stream-std/push-stream-base | * |