UNPKG

ipfs-http-client

Version:
79 lines 2.37 kB
import { CID } from 'multiformats/cid'; import { objectToCamel } from './lib/object-to-camel.js'; import { configure } from './lib/configure.js'; import { multipartRequest } from 'ipfs-core-utils/multipart-request'; import { toUrlSearchParams } from './lib/to-url-search-params.js'; import { abortSignal } from './lib/abort-signal.js'; import { AbortController } from 'native-abort-controller'; export const createAddAll = configure(api => { async function* addAll(source, options = {}) { const controller = new AbortController(); const signal = abortSignal(controller.signal, options.signal); const {headers, body, total, parts} = await multipartRequest(source, controller, options.headers); const [progressFn, onUploadProgress] = typeof options.progress === 'function' ? createProgressHandler(total, parts, options.progress) : [ undefined, undefined ]; const res = await api.post('add', { searchParams: toUrlSearchParams({ 'stream-channels': true, ...options, progress: Boolean(progressFn) }), onUploadProgress, signal, headers, body }); for await (let file of res.ndjson()) { file = objectToCamel(file); if (file.hash !== undefined) { yield toCoreInterface(file); } else if (progressFn) { progressFn(file.bytes || 0, file.name); } } } return addAll; }); const createProgressHandler = (total, parts, progress) => parts ? [ undefined, createOnUploadProgress(total, parts, progress) ] : [ progress, undefined ]; const createOnUploadProgress = (size, parts, progress) => { let index = 0; const count = parts.length; return ({loaded, total}) => { const position = Math.floor(loaded / total * size); while (index < count) { const {start, end, name} = parts[index]; if (position < end) { progress(position - start, name); break; } else { progress(end - start, name); index += 1; } } }; }; function toCoreInterface({name, hash, size, mode, mtime, mtimeNsecs}) { const output = { path: name, cid: 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; }