@rsksmart/rif-storage
Version:
Library integrating distributed storage projects
79 lines (78 loc) • 3.76 kB
JavaScript
;
/* eslint-env browser */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toFormData = void 0;
const utils_1 = require("../../utils");
function toFormData(input) {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
const formData = new FormData();
for (const [path, entry] of Object.entries(input)) {
if (Buffer.isBuffer(entry.data)) {
formData.append(path, new Blob([entry.data.buffer], { type: entry.contentType }), path);
}
else if (utils_1.isReadable(entry.data)) {
// In the browser there's _currently_ no streaming upload, buffer up our
// async iterator chunks and append a big Blob :(
// One day, this will be browser streams
const bufs = [];
try {
for (var _b = (e_1 = void 0, __asyncValues(entry.data)), _c; _c = yield _b.next(), !_c.done;) {
const chunk = _c.value;
bufs.push(chunk);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
formData.append(path, new Blob([Buffer.concat(bufs).buffer], { type: entry.contentType }), path);
}
else {
throw new Error('Unknown type of data!');
}
}
return formData;
});
}
exports.toFormData = toFormData;
// eslint-disable-next-line require-await
function prepareData(data) {
return __awaiter(this, void 0, void 0, function* () {
if (Buffer.isBuffer(data) || typeof data === 'string') {
return data;
}
if (utils_1.isReadable(data)) {
return new Promise(resolve => {
const buffers = [];
data.on('data', function (d) {
buffers.push(d);
});
data.on('end', function () {
resolve(Buffer.concat(buffers));
});
});
}
return toFormData(data);
});
}
exports.default = prepareData;