ghaction-upx/node_modules/get-stream/index.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-01-17 01:36:42 -07:00
'use strict';
2020-04-03 02:12:31 -06:00
const pump = require('pump');
2020-01-17 01:36:42 -07:00
const bufferStream = require('./buffer-stream');
2020-04-03 02:12:31 -06:00
class MaxBufferError extends Error {
constructor() {
super('maxBuffer exceeded');
this.name = 'MaxBufferError';
}
}
function getStream(inputStream, options) {
2020-01-17 01:36:42 -07:00
if (!inputStream) {
return Promise.reject(new Error('Expected a stream'));
}
2020-04-03 02:12:31 -06:00
options = Object.assign({maxBuffer: Infinity}, options);
2020-01-17 01:36:42 -07:00
2020-04-03 02:12:31 -06:00
const {maxBuffer} = options;
2020-01-17 01:36:42 -07:00
2020-04-03 02:12:31 -06:00
let stream;
return new Promise((resolve, reject) => {
const rejectPromise = error => {
if (error) { // A null check
error.bufferedData = stream.getBufferedValue();
2020-01-17 01:36:42 -07:00
}
2020-04-03 02:12:31 -06:00
reject(error);
2020-01-17 01:36:42 -07:00
};
2020-04-03 02:12:31 -06:00
stream = pump(inputStream, bufferStream(options), error => {
if (error) {
rejectPromise(error);
return;
}
resolve();
});
2020-01-17 01:36:42 -07:00
stream.on('data', () => {
if (stream.getBufferedLength() > maxBuffer) {
2020-04-03 02:12:31 -06:00
rejectPromise(new MaxBufferError());
2020-01-17 01:36:42 -07:00
}
});
2020-04-03 02:12:31 -06:00
}).then(() => stream.getBufferedValue());
2020-01-17 01:36:42 -07:00
}
module.exports = getStream;
2020-04-03 02:12:31 -06:00
module.exports.buffer = (stream, options) => getStream(stream, Object.assign({}, options, {encoding: 'buffer'}));
module.exports.array = (stream, options) => getStream(stream, Object.assign({}, options, {array: true}));
module.exports.MaxBufferError = MaxBufferError;