UNPKG

@egodigital/egoose

Version:

Helper classes and functions for Node.js 10 or later.

122 lines 3.8 kB
"use strict"; /** * This file is part of the @egodigital/egoose distribution. * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/) * * @egodigital/egoose is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, version 3. * * @egodigital/egoose is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const index_1 = require("../index"); const events_1 = require("../events"); const isStream = require("is-stream"); /** * Returns a value as buffer. * * @param {any} data The input data. * @param {string} [enc] The custom encoding for string to use. Default: utf8 * * @return {Promise<Buffer>} The promise with the buffer. */ async function asBuffer(data, enc) { enc = index_1.normalizeString(enc); if ('' === enc) { enc = 'utf8'; } return await asBufferInner(data, enc, 0); } exports.asBuffer = asBuffer; async function asBufferInner(data, enc, level) { if (level > 63) { throw new Error('StackOverflow: asBufferInner()'); } if (Buffer.isBuffer(data)) { return data; } if (isStream(data)) { return await readAll(data, enc); } if (_.isFunction(data)) { return await asBufferInner(await Promise.resolve(data(enc)), enc, level + 1); } return Buffer.from(index_1.toStringSafe(data), enc); } /** * Reads the content of a stream. * * @param {Stream.Readable} stream The stream. * @param {string} [enc] The custom (string) encoding to use. * * @returns {Promise<Buffer>} The promise with the content. */ function readAll(stream, enc) { enc = index_1.normalizeString(enc); if ('' === enc) { enc = undefined; } return new Promise((resolve, reject) => { let buff; let dataListener; let endListener; let errorListener; let completedInvoked = false; const COMPLETED = (err) => { if (completedInvoked) { return; } completedInvoked = true; events_1.tryRemoveListener(stream, 'data', dataListener); events_1.tryRemoveListener(stream, 'end', endListener); events_1.tryRemoveListener(stream, 'error', errorListener); if (err) { reject(err); } else { resolve(buff); } }; errorListener = (err) => { if (err) { COMPLETED(err); } }; dataListener = (chunk) => { try { if (!chunk || chunk.length < 1) { return; } if (_.isString(chunk)) { chunk = Buffer.from(chunk, enc); } buff = Buffer.concat([buff, chunk]); } catch (e) { COMPLETED(e); } }; endListener = () => { COMPLETED(null); }; try { stream.on('error', errorListener); buff = Buffer.alloc(0); stream.once('end', endListener); stream.on('data', dataListener); } catch (e) { COMPLETED(e); } }); } exports.readAll = readAll; //# sourceMappingURL=index.js.map