Pullout

Pull out data from stream.
Install
npm i pullout
pullout(stream[, type = 'string', {limit}])
Type could be string or buffer.
limit is the maximum size in bytes. When more data arrives, pullout stops reading and rejects with an error with code: 'ELIMIT', so a huge or endless stream (for example a chunked http request without Content-Length) is never fully buffered. The stream is not destroyed, so you can still use it, for example to send an error response.
import fs from 'node:fs';
import {pullout} from 'pullout';
const readStream = fs.createReadStream(__filename);
const data = await pullout(readStream);
console.log(data);
With a limit:
import {pullout} from 'pullout';
import {tryToCatch} from 'try-to-catch';
const [error, body] = await tryToCatch(pullout, request, 'string', {
limit: 1024 * 1024,
});
if (error?.code === 'ELIMIT')
response.end('Request body is too large!');
Related
- pipe-io - pipe streams and handle events
License
MIT