source: trip-planner-front/node_modules/get-stream/index.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.2 KB
Line 
1'use strict';
2const pump = require('pump');
3const bufferStream = require('./buffer-stream');
4
5class MaxBufferError extends Error {
6 constructor() {
7 super('maxBuffer exceeded');
8 this.name = 'MaxBufferError';
9 }
10}
11
12function getStream(inputStream, options) {
13 if (!inputStream) {
14 return Promise.reject(new Error('Expected a stream'));
15 }
16
17 options = Object.assign({maxBuffer: Infinity}, options);
18
19 const {maxBuffer} = options;
20
21 let stream;
22 return new Promise((resolve, reject) => {
23 const rejectPromise = error => {
24 if (error) { // A null check
25 error.bufferedData = stream.getBufferedValue();
26 }
27 reject(error);
28 };
29
30 stream = pump(inputStream, bufferStream(options), error => {
31 if (error) {
32 rejectPromise(error);
33 return;
34 }
35
36 resolve();
37 });
38
39 stream.on('data', () => {
40 if (stream.getBufferedLength() > maxBuffer) {
41 rejectPromise(new MaxBufferError());
42 }
43 });
44 }).then(() => stream.getBufferedValue());
45}
46
47module.exports = getStream;
48module.exports.buffer = (stream, options) => getStream(stream, Object.assign({}, options, {encoding: 'buffer'}));
49module.exports.array = (stream, options) => getStream(stream, Object.assign({}, options, {array: true}));
50module.exports.MaxBufferError = MaxBufferError;
Note: See TracBrowser for help on using the repository browser.