UNPKG

kubo-rpc-client-esm-cjs

Version:
159 lines 7.87 kB
"use strict"; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } 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); } }; var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAddAll = void 0; const cid_1 = require("multiformats/cid"); const object_to_camel_js_1 = require("./lib/object-to-camel.js"); const configure_js_1 = require("./lib/configure.js"); const multipart_request_1 = require("ipfs-core-utils/multipart-request"); const to_url_search_params_js_1 = require("./lib/to-url-search-params.js"); const abort_signal_js_1 = require("./lib/abort-signal.js"); exports.createAddAll = (0, configure_js_1.configure)((api) => { /** * @type {import('./types.js').RootAPI["addAll"]} */ function addAll(source, options = {}) { return __asyncGenerator(this, arguments, function* addAll_1() { var _a, e_1, _b, _c; // allow aborting requests on body errors const controller = new AbortController(); const signal = (0, abort_signal_js_1.abortSignal)(controller.signal, options.signal); const { headers, body, total, parts } = yield __await((0, multipart_request_1.multipartRequest)(source, controller, options.headers) // In browser response body only starts streaming once upload is // complete, at which point all the progress updates are invalid. If // length of the content is computable we can interpret progress from // `{ total, loaded}` passed to `onUploadProgress` and `multipart.total` // in which case we disable progress updates to be written out. ); // In browser response body only starts streaming once upload is // complete, at which point all the progress updates are invalid. If // length of the content is computable we can interpret progress from // `{ total, loaded}` passed to `onUploadProgress` and `multipart.total` // in which case we disable progress updates to be written out. const [progressFn, onUploadProgress] = typeof options.progress === 'function' ? createProgressHandler(total, parts, options.progress) : [undefined, undefined]; const res = yield __await(api.post('add', { searchParams: (0, to_url_search_params_js_1.toUrlSearchParams)(Object.assign(Object.assign({ 'stream-channels': true }, options), { progress: Boolean(progressFn) })), onUploadProgress, signal, headers, body })); try { for (var _d = true, _e = __asyncValues(res.ndjson()), _f; _f = yield __await(_e.next()), _a = _f.done, !_a;) { _c = _f.value; _d = false; try { let file = _c; file = (0, object_to_camel_js_1.objectToCamel)(file); if (file.hash !== undefined) { yield yield __await(toCoreInterface(file)); } else if (progressFn) { progressFn(file.bytes || 0, file.name); } } finally { _d = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e)); } finally { if (e_1) throw e_1.error; } } }); } return addAll; }); /** * Returns simple progress callback when content length isn't computable or a * progress event handler that calculates progress from upload progress events. * * @param {number} total * @param {{name:string, start:number, end:number}[]|null} parts * @param {import('./types.js').IPFSCoreAddProgressFn} progress * @returns {[import('./types.js').IPFSCoreAddProgressFn|undefined, import('./types.js').IPFSUtilsHttpUploadProgressFn|undefined]} */ const createProgressHandler = (total, parts, progress) => parts ? [undefined, createOnUploadProgress(total, parts, progress)] : [progress, undefined]; /** * Creates a progress handler that interpolates progress from upload progress * events and total size of the content that is added. * * @param {number} size - actual content size * @param {{name:string, start:number, end:number}[]} parts * @param {import('./types.js').IPFSCoreAddProgressFn} progress * @returns {import('./types.js').IPFSUtilsHttpUploadProgressFn} */ const createOnUploadProgress = (size, parts, progress) => { let index = 0; const count = parts.length; return ({ loaded, total }) => { // Derive position from the current progress. const position = Math.floor(loaded / total * size); while (index < count) { const { start, end, name } = parts[index]; // If within current part range report progress and break the loop if (position < end) { progress(position - start, name); break; // If passed current part range report final byte for the chunk and // move to next one. } else { progress(end - start, name); index += 1; } } }; }; /** * @param {object} input * @param {string} input.name * @param {string} input.hash * @param {string} input.size * @param {string} [input.mode] * @param {number} [input.mtime] * @param {number} [input.mtimeNsecs] */ function toCoreInterface({ name, hash, size, mode, mtime, mtimeNsecs }) { /** @type {import('./types.js').AddResult} */ const output = { path: name, cid: cid_1.CID.parse(hash), size: parseInt(size) }; if (mode != null) { output.mode = parseInt(mode, 8); } if (mtime != null) { output.mtime = { secs: mtime, nsecs: mtimeNsecs || 0 }; } return output; } //# sourceMappingURL=add-all.js.map