npm.io
6.1.0 • Published 1 week ago

pullout

Licence
MIT
Version
6.1.0
Deps
0
Size
8 kB
Vulns
0
Weekly
0
Stars
2

Pullout License NPM version Build Status Coverage Status

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!');
  • pipe-io - pipe streams and handle events

License

MIT

Keywords