UNPKG

3.8 kBJavaScriptView Raw
1"use strict";
2/**
3 * This file is part of the @egodigital/egoose distribution.
4 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
5 *
6 * @egodigital/egoose is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * @egodigital/egoose is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19const _ = require("lodash");
20const index_1 = require("../index");
21const events_1 = require("../events");
22const isStream = require("is-stream");
23/**
24 * Returns a value as buffer.
25 *
26 * @param {any} data The input data.
27 * @param {string} [enc] The custom encoding for string to use. Default: utf8
28 *
29 * @return {Promise<Buffer>} The promise with the buffer.
30 */
31async function asBuffer(data, enc) {
32 enc = index_1.normalizeString(enc);
33 if ('' === enc) {
34 enc = 'utf8';
35 }
36 return await asBufferInner(data, enc, 0);
37}
38exports.asBuffer = asBuffer;
39async function asBufferInner(data, enc, level) {
40 if (level > 63) {
41 throw new Error('StackOverflow: asBufferInner()');
42 }
43 if (Buffer.isBuffer(data)) {
44 return data;
45 }
46 if (isStream(data)) {
47 return await readAll(data, enc);
48 }
49 if (_.isFunction(data)) {
50 return await asBufferInner(await Promise.resolve(data(enc)), enc, level + 1);
51 }
52 return Buffer.from(index_1.toStringSafe(data), enc);
53}
54/**
55 * Reads the content of a stream.
56 *
57 * @param {Stream.Readable} stream The stream.
58 * @param {string} [enc] The custom (string) encoding to use.
59 *
60 * @returns {Promise<Buffer>} The promise with the content.
61 */
62function readAll(stream, enc) {
63 enc = index_1.normalizeString(enc);
64 if ('' === enc) {
65 enc = undefined;
66 }
67 return new Promise((resolve, reject) => {
68 let buff;
69 let dataListener;
70 let endListener;
71 let errorListener;
72 let completedInvoked = false;
73 const COMPLETED = (err) => {
74 if (completedInvoked) {
75 return;
76 }
77 completedInvoked = true;
78 events_1.tryRemoveListener(stream, 'data', dataListener);
79 events_1.tryRemoveListener(stream, 'end', endListener);
80 events_1.tryRemoveListener(stream, 'error', errorListener);
81 if (err) {
82 reject(err);
83 }
84 else {
85 resolve(buff);
86 }
87 };
88 errorListener = (err) => {
89 if (err) {
90 COMPLETED(err);
91 }
92 };
93 dataListener = (chunk) => {
94 try {
95 if (!chunk || chunk.length < 1) {
96 return;
97 }
98 if (_.isString(chunk)) {
99 chunk = Buffer.from(chunk, enc);
100 }
101 buff = Buffer.concat([buff, chunk]);
102 }
103 catch (e) {
104 COMPLETED(e);
105 }
106 };
107 endListener = () => {
108 COMPLETED(null);
109 };
110 try {
111 stream.on('error', errorListener);
112 buff = Buffer.alloc(0);
113 stream.once('end', endListener);
114 stream.on('data', dataListener);
115 }
116 catch (e) {
117 COMPLETED(e);
118 }
119 });
120}
121exports.readAll = readAll;
122//# sourceMappingURL=index.js.map
\No newline at end of file