UNPKG

macoolka-stream

Version:

`macoolka-stream` is a library contains common stream functions.

149 lines 4.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.readChunkFromStream = exports.stringToStream = exports.streamToString = exports.streamSize = exports.isStream = exports.streamToBuffer = void 0; /** * Common function for Stream * @desczh * Stream 常用函数 * @file */ var stream_1 = require("stream"); var Task_1 = require("fp-ts/Task"); /** * Returns a Buffer with data read from the stream. * @desczh * 读取流到Buffer * @since 0.2.0 */ function streamToBuffer(stream) { return function () { return new Promise(function (resolve, reject) { var buffersCache = []; stream.on('data', function (data) { buffersCache.push(data); }); stream.on('end', function () { resolve(Buffer.concat(buffersCache)); }); stream.on('error', function (error) { reject(error); }); }); }; } exports.streamToBuffer = streamToBuffer; /** * Determines whether the passed value is a `Stream`. * @desczh * 是否输入为`Stream` * @since 0.2.0 */ var isStream = function (a) { return a instanceof stream_1.Stream; }; exports.isStream = isStream; /** * Returns stream's size * @desczh * 获得流的长度 * @since 0.2.0 */ function streamSize(stream) { return function () { return new Promise(function (resolve, reject) { var result = 0; stream.on('data', function (data) { result += data.byteLength; // buffersCache.push(data) }); stream.on('end', function () { resolve(result); }); stream.on('error', function (error) { reject(error); }); }); }; } exports.streamSize = streamSize; /** * Returns a string with data read from the stream. * @desczh * 把流转换为文本 * @since 0.2.0 */ var streamToString = function (_a) { var _b = _a.encoding, encoding = _b === void 0 ? 'utf8' : _b; return function (stream) { return Task_1.task.map(streamToBuffer(stream), (function (buffer) { return buffer.toString(encoding); })); }; }; exports.streamToString = streamToString; /** * Returns a stream with text. * @desczh * 把文本转换为流 * @since 0.2.2 */ var stringToStream = function (_a) { var _b = _a.encoding, encoding = _b === void 0 ? 'utf8' : _b; return function (a) { var s = new stream_1.Readable(); s._read = function noop() { }; // redundant? see update below s.push(a, encoding); s.push(null); return s; }; }; exports.stringToStream = stringToStream; /** * Reads a certain amount of bytes from the beginning of a Stream, returning a Buffer. * The amount of data read might be smaller if the stream ends before it could return the amount of data requested. * * If the `peek` argument is true, the data is put back into the beginning of the stream, so it can be consumed by another function * * Note that this function will pause the stream, so you might need to call the `resume` method on it to make it flow again. * * If passing a stream that has already ended, the function could enter into an infinite loop and return a Promise that never resolves. It's your responsibility to ensure that streams passed to this function still have data to return. * * @desczh * 从流的开始位置读取给定的长度的内容块 * 如果peek为真,数据将放回到流中。 * * @since 0.2.0 */ var readChunkFromStream = function (_a) { var size = _a.size, peek = _a.peek; return function (stream) { // Ensure the stream isn't flowing stream.pause(); // Returns a promise that resolves when we have read enough data from the stream. return function () { return new Promise(function (resolve, reject) { // Callbacks on events var errorEvent = function (err) { reject(err); }; var readableEvent = function () { // If we don't have enough data, and the stream hasn't ended, this will return null var data = stream.read(size); if (data) { if (peek === true) { // Put the data we read back into the stream stream.unshift(data); } // Stop listening on callbacks stream.removeListener('error', errorEvent); // Return the data resolve(data); } else { // We need to wait longer for more data stream.once('readable', readableEvent); } }; // Listen to the readable event and in case of error stream.once('readable', readableEvent); stream.on('error', errorEvent); }); }; }; }; exports.readChunkFromStream = readChunkFromStream; //# sourceMappingURL=index.js.map