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 pathwrite.js
390 lines (330 loc) · 10.9 KB
/
write.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import { logger } from '@libp2p/logger'
import { importer } from 'ipfs-unixfs-importer'
import {
decode
} from '@ipld/dag-pb'
import { createStat } from './stat.js'
import { createMkdir } from './mkdir.js'
import { addLink } from './utils/add-link.js'
import mergeOpts from 'merge-options'
import { createLock } from './utils/create-lock.js'
import { toAsyncIterator } from './utils/to-async-iterator.js'
import { toMfsPath } from './utils/to-mfs-path.js'
import { toPathComponents } from './utils/to-path-components.js'
import { toTrail } from './utils/to-trail.js'
import { updateTree } from './utils/update-tree.js'
import { updateMfsRoot } from './utils/update-mfs-root.js'
import errCode from 'err-code'
import {
MFS_MAX_CHUNK_SIZE
} from '../../utils.js'
import last from 'it-last'
import { withTimeoutOption } from 'ipfs-core-utils/with-timeout-option'
import {
parseMode,
parseMtime
} from 'ipfs-unixfs'
const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
const log = logger('ipfs:mfs:write')
/**
* @typedef {import('multiformats/cid').CIDVersion} CIDVersion
* @typedef {import('ipfs-unixfs').MtimeLike} MtimeLike
* @typedef {import('./').MfsContext} MfsContext
* @typedef {import('./utils/to-mfs-path').FilePath} FilePath
* @typedef {import('./utils/to-mfs-path').MfsPath} MfsPath
* @typedef {import('multiformats/hashes/interface').MultihashHasher} MultihashHasher
*
* @typedef {object} DefaultOptions
* @property {number} offset
* @property {number} length
* @property {boolean} create
* @property {boolean} truncate
* @property {boolean} rawLeaves
* @property {boolean} reduceSingleLeafToSelf
* @property {CIDVersion} cidVersion
* @property {string} hashAlg
* @property {boolean} parents
* @property {import('ipfs-core-types/src/root').AddProgressFn} progress
* @property {'trickle' | 'balanced'} strategy
* @property {boolean} flush
* @property {'raw' | 'file'} leafType
* @property {number} shardSplitThreshold
* @property {MtimeLike} [mtime]
* @property {number} [mode]
* @property {AbortSignal} [signal]
* @property {number} [timeout]
*/
/**
* @type {DefaultOptions}
*/
const defaultOptions = {
offset: 0, // the offset in the file to begin writing
length: Infinity, // how many bytes from the incoming buffer to write
create: false, // whether to create the file if it does not exist
truncate: false, // whether to truncate the file first
rawLeaves: false,
reduceSingleLeafToSelf: false,
cidVersion: 0,
hashAlg: 'sha2-256',
parents: false, // whether to create intermediate directories if they do not exist
progress: (bytes, path) => {},
strategy: 'trickle',
flush: true,
leafType: 'raw',
shardSplitThreshold: 1000
}
/**
* @param {MfsContext} context
*/
export function createWrite (context) {
/**
* @type {import('ipfs-core-types/src/files').API<{}>["write"]}
*/
async function mfsWrite (path, content, opts = {}) {
/** @type {DefaultOptions} */
const options = mergeOptions(defaultOptions, opts)
/** @type {AsyncIterable<Uint8Array>} */
let source
/** @type {MfsPath} */
let destination
/** @type {MfsPath} */
let parent
log('Reading source, destination and parent')
await createLock().readLock(async () => {
source = await toAsyncIterator(content)
destination = await toMfsPath(context, path, options)
parent = await toMfsPath(context, destination.mfsDirectory, options)
})()
log('Read source, destination and parent')
// @ts-expect-error - parent may be undefined
if (!options.parents && !parent.exists) {
throw errCode(new Error('directory does not exist'), 'ERR_NO_EXIST')
}
// @ts-expect-error
if (source == null) {
throw errCode(new Error('could not create source'), 'ERR_NO_SOURCE')
}
// @ts-expect-error
if (destination == null) {
throw errCode(new Error('could not create destination'), 'ERR_NO_DESTINATION')
}
if (!options.create && !destination.exists) {
throw errCode(new Error('file does not exist'), 'ERR_NO_EXIST')
}
if (destination.entryType !== 'file') {
throw errCode(new Error('not a file'), 'ERR_NOT_A_FILE')
}
return updateOrImport(context, path, source, destination, options)
}
return withTimeoutOption(mfsWrite)
}
/**
* @param {MfsContext} context
* @param {string} path
* @param {AsyncIterable<Uint8Array>} source
* @param {FilePath} destination
* @param {DefaultOptions} options
*/
const updateOrImport = async (context, path, source, destination, options) => {
const child = await write(context, source, destination, options)
// The slow bit is done, now add or replace the DAGLink in the containing directory
// re-reading the path to the containing folder in case it has changed in the interim
await createLock().writeLock(async () => {
const pathComponents = toPathComponents(path)
const fileName = pathComponents.pop()
if (fileName == null) {
throw errCode(new Error('source does not exist'), 'ERR_NO_EXIST')
}
let parentExists = false
try {
await createStat(context)(`/${pathComponents.join('/')}`, options)
parentExists = true
} catch (/** @type {any} */ err) {
if (err.code !== 'ERR_NOT_FOUND') {
throw err
}
}
if (!parentExists) {
await createMkdir(context)(`/${pathComponents.join('/')}`, options)
}
// get an updated mfs path in case the root changed while we were writing
const updatedPath = await toMfsPath(context, path, options)
const trail = await toTrail(context, updatedPath.mfsDirectory)
const parent = trail[trail.length - 1]
if (!parent) {
throw errCode(new Error('directory does not exist'), 'ERR_NO_EXIST')
}
if (!parent.type || !parent.type.includes('directory')) {
throw errCode(new Error(`cannot write to ${parent.name}: Not a directory`), 'ERR_NOT_A_DIRECTORY')
}
const parentBlock = await context.repo.blocks.get(parent.cid)
const parentNode = decode(parentBlock)
const result = await addLink(context, {
parent: parentNode,
name: fileName,
cid: child.cid,
size: child.size,
flush: options.flush,
shardSplitThreshold: options.shardSplitThreshold,
hashAlg: options.hashAlg,
cidVersion: options.cidVersion
})
parent.cid = result.cid
// update the tree with the new child
const newRootCid = await updateTree(context, trail, options)
// Update the MFS record with the new CID for the root of the tree
await updateMfsRoot(context, newRootCid, options)
})()
}
/**
* @param {MfsContext} context
* @param {AsyncIterable<Uint8Array>} source
* @param {FilePath} destination
* @param {DefaultOptions} options
*/
const write = async (context, source, destination, options) => {
if (destination.exists) {
log(`Overwriting file ${destination.cid} offset ${options.offset} length ${options.length}`)
} else {
log(`Writing file offset ${options.offset} length ${options.length}`)
}
/** @type {Array<() => AsyncIterable<Uint8Array>>} */
const sources = []
// pad start of file if necessary
if (options.offset > 0) {
if (destination.unixfs) {
log(`Writing first ${options.offset} bytes of original file`)
sources.push(
() => {
return destination.content({
offset: 0,
length: options.offset
})
}
)
if (destination.unixfs.fileSize() < options.offset) {
const extra = options.offset - destination.unixfs.fileSize()
log(`Writing zeros for extra ${extra} bytes`)
sources.push(
asyncZeroes(extra)
)
}
} else {
log(`Writing zeros for first ${options.offset} bytes`)
sources.push(
asyncZeroes(options.offset)
)
}
}
sources.push(
limitAsyncStreamBytes(source, options.length)
)
const content = countBytesStreamed(catAsyncIterators(sources), (bytesWritten) => {
if (destination.unixfs && !options.truncate) {
// if we've done reading from the new source and we are not going
// to truncate the file, add the end of the existing file to the output
const fileSize = destination.unixfs.fileSize()
if (fileSize > bytesWritten) {
log(`Writing last ${fileSize - bytesWritten} of ${fileSize} bytes from original file starting at offset ${bytesWritten}`)
return destination.content({
offset: bytesWritten
})
} else {
log('Not writing last bytes from original file')
}
}
return {
[Symbol.asyncIterator]: async function * () {}
}
})
/** @type {number | undefined} */
let mode
if (options.mode !== undefined && options.mode !== null) {
mode = parseMode(options.mode)
} else if (destination && destination.unixfs) {
mode = destination.unixfs.mode
}
/** @type {import('ipfs-unixfs').Mtime | undefined} */
let mtime
if (options.mtime != null) {
mtime = parseMtime(options.mtime)
} else if (destination && destination.unixfs) {
mtime = destination.unixfs.mtime
}
const hasher = await context.hashers.getHasher(options.hashAlg)
const result = await last(importer([{
content: content,
// persist mode & mtime if set previously
mode,
mtime
}], context.repo.blocks, {
progress: options.progress,
hasher,
cidVersion: options.cidVersion,
strategy: options.strategy,
rawLeaves: options.rawLeaves,
reduceSingleLeafToSelf: options.reduceSingleLeafToSelf,
leafType: options.leafType
}))
if (!result) {
throw errCode(new Error(`cannot write to ${parent.name}`), 'ERR_COULD_NOT_WRITE')
}
log(`Wrote ${result.cid}`)
return {
cid: result.cid,
size: result.size
}
}
/**
* @param {AsyncIterable<Uint8Array>} stream
* @param {number} limit
*/
const limitAsyncStreamBytes = (stream, limit) => {
return async function * _limitAsyncStreamBytes () {
let emitted = 0
for await (const buf of stream) {
emitted += buf.length
if (emitted > limit) {
yield buf.subarray(0, limit - emitted)
return
}
yield buf
}
}
}
/**
* @param {number} count
* @param {number} chunkSize
*/
const asyncZeroes = (count, chunkSize = MFS_MAX_CHUNK_SIZE) => {
const buf = new Uint8Array(chunkSize)
async function * _asyncZeroes () {
while (true) {
yield buf
}
}
return limitAsyncStreamBytes(_asyncZeroes(), count)
}
/**
* @param {Array<() => AsyncIterable<Uint8Array>>} sources
*/
const catAsyncIterators = async function * (sources) { // eslint-disable-line require-await
for (let i = 0; i < sources.length; i++) {
yield * sources[i]()
}
}
/**
* @param {AsyncIterable<Uint8Array>} source
* @param {(count: number) => AsyncIterable<Uint8Array>} notify
*/
const countBytesStreamed = async function * (source, notify) {
let wrote = 0
for await (const buf of source) {
wrote += buf.length
yield buf
}
for await (const buf of notify(wrote)) {
wrote += buf.length
yield buf
}
}