1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const assert_1 = __importDefault(require("assert"));
|
7 | const into_stream_1 = __importDefault(require("into-stream"));
|
8 | class FileBlob {
|
9 | constructor({ mode = 0o100644, contentType, data }) {
|
10 | assert_1.default(typeof mode === 'number');
|
11 | assert_1.default(typeof data === 'string' || Buffer.isBuffer(data));
|
12 | this.type = 'FileBlob';
|
13 | this.mode = mode;
|
14 | this.contentType = contentType;
|
15 | this.data = data;
|
16 | }
|
17 | static async fromStream({ mode = 0o100644, contentType, stream, }) {
|
18 | assert_1.default(typeof mode === 'number');
|
19 | assert_1.default(typeof stream.pipe === 'function');
|
20 | const chunks = [];
|
21 | await new Promise((resolve, reject) => {
|
22 | stream.on('data', chunk => chunks.push(Buffer.from(chunk)));
|
23 | stream.on('error', error => reject(error));
|
24 | stream.on('end', () => resolve());
|
25 | });
|
26 | const data = Buffer.concat(chunks);
|
27 | return new FileBlob({ mode, contentType, data });
|
28 | }
|
29 | toStream() {
|
30 | return into_stream_1.default(this.data);
|
31 | }
|
32 | }
|
33 | exports.default = FileBlob;
|