This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathadd-all.js
133 lines (121 loc) · 4.26 KB
/
add-all.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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'
/**
* @typedef {import('ipfs-utils/src/types').ProgressFn} IPFSUtilsHttpUploadProgressFn
* @typedef {import('ipfs-core-types/src/root').AddProgressFn} IPFSCoreAddProgressFn
* @typedef {import('./types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/root').API<HTTPClientExtraOptions>} RootAPI
* @typedef {import('ipfs-core-types/src/root').AddResult} AddResult
*/
export const createAddAll = configure((api) => {
/**
* @type {RootAPI["addAll"]}
*/
async function * addAll (source, options = {}) {
// allow aborting requests on body errors
const controller = new AbortController()
const signal = abortSignal(controller.signal, options.signal)
const { headers, body, total, parts } =
await 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.
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
})
/**
* 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 {IPFSCoreAddProgressFn} progress
* @returns {[IPFSCoreAddProgressFn|undefined, 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 {IPFSCoreAddProgressFn} progress
* @returns {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 {AddResult} */
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
}