UNPKG

1.34 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const assert_1 = __importDefault(require("assert"));
7const into_stream_1 = __importDefault(require("into-stream"));
8class 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'); // is-stream
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}
33exports.default = FileBlob;