Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 4afd24a

Browse files
authored
chore: upgrade unixfs (#3827)
Upgrades to new ESM unixfs. The new version doesn't export the `Dir` and `ShardedDir` classes so just copy the relevant parts into the codebase.
1 parent 4b82caf commit 4afd24a

File tree

14 files changed

+375
-69
lines changed

14 files changed

+375
-69
lines changed

packages/interface-ipfs-core/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
"err-code": "^3.0.1",
5050
"interface-blockstore": "^1.0.0",
5151
"ipfs-core-types": "^0.7.0",
52-
"ipfs-unixfs": "^5.0.0",
53-
"ipfs-unixfs-importer": "^8.0.2",
52+
"ipfs-unixfs": "^6.0.3",
53+
"ipfs-unixfs-importer": "^9.0.3",
5454
"ipfs-utils": "^8.1.4",
5555
"ipns": "^0.13.3",
5656
"is-ipfs": "^6.0.1",

packages/ipfs-core-utils/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"browser-readablestream-to-it": "^1.0.1",
4747
"err-code": "^3.0.1",
4848
"ipfs-core-types": "^0.7.0",
49-
"ipfs-unixfs": "^5.0.0",
49+
"ipfs-unixfs": "^6.0.3",
5050
"ipfs-utils": "^8.1.4",
5151
"it-all": "^1.0.4",
5252
"it-map": "^1.0.4",

packages/ipfs-core/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@ipld/car": "^3.1.0",
6161
"@ipld/dag-cbor": "^6.0.5",
6262
"@ipld/dag-pb": "^2.1.3",
63+
"@multiformats/murmur3": "^1.0.1",
6364
"abort-controller": "^3.0.0",
6465
"array-shuffle": "^2.0.0",
6566
"blockstore-datastore-adapter": "^1.0.0",
@@ -79,9 +80,9 @@
7980
"ipfs-core-utils": "^0.10.1",
8081
"ipfs-http-client": "^52.0.1",
8182
"ipfs-repo": "^11.0.1",
82-
"ipfs-unixfs": "^5.0.0",
83-
"ipfs-unixfs-exporter": "^6.0.2",
84-
"ipfs-unixfs-importer": "^8.0.2",
83+
"ipfs-unixfs": "^6.0.3",
84+
"ipfs-unixfs-exporter": "^7.0.3",
85+
"ipfs-unixfs-importer": "^9.0.3",
8586
"ipfs-utils": "^8.1.4",
8687
"ipns": "^0.13.3",
8788
"is-domain-name": "^1.0.1",

packages/ipfs-core/src/components/files/chmod.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ const { recursive } = require('ipfs-unixfs-exporter')
1717
const last = require('it-last')
1818
const cp = require('./cp')
1919
const rm = require('./rm')
20-
// @ts-ignore - TODO: refactor this so it does not require a deep require
21-
const persist = require('ipfs-unixfs-importer/src/utils/persist')
20+
const persist = require('./utils/persist')
2221
const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
2322

2423
/**

packages/ipfs-core/src/components/files/cp.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ const addSourceToParent = async (context, source, childName, parent, options) =>
208208
const sourceBlock = await context.repo.blocks.get(source.cid)
209209
const {
210210
node,
211-
cid
211+
cid,
212+
size
212213
} = await addLink(context, {
213214
parentCid: parent.cid,
214215
size: sourceBlock.length,
@@ -222,7 +223,7 @@ const addSourceToParent = async (context, source, childName, parent, options) =>
222223

223224
parent.node = node
224225
parent.cid = cid
225-
parent.size = node.size
226+
parent.size = size
226227

227228
return parent
228229
}

packages/ipfs-core/src/components/files/utils/add-link.js

+12-32
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
'use strict'
22

3-
// @ts-ignore
43
const dagPb = require('@ipld/dag-pb')
5-
const { sha256, sha512 } = require('multiformats/hashes/sha2')
64
const { CID } = require('multiformats/cid')
75
const log = require('debug')('ipfs:mfs:core:utils:add-link')
86
const { UnixFS } = require('ipfs-unixfs')
9-
// @ts-ignore - refactor this to not need deep require
10-
const DirSharded = require('ipfs-unixfs-importer/src/dir-sharded')
11-
// @ts-ignore - refactor this to not need deep require
12-
const defaultImporterOptions = require('ipfs-unixfs-importer/src/options')
7+
const DirSharded = require('./dir-sharded')
138
const {
149
updateHamtDirectory,
1510
recreateHamtLevel,
@@ -223,6 +218,11 @@ const addToShardedDirectory = async (context, options) => {
223218
shard, path
224219
} = await addFileToShardedDirectory(context, options)
225220
const result = await last(shard.flush(context.repo.blocks))
221+
222+
if (!result) {
223+
throw new Error('No result from flushing shard')
224+
}
225+
226226
const block = await context.repo.blocks.get(result.cid)
227227
const node = dagPb.decode(block)
228228

@@ -269,44 +269,24 @@ const addFileToShardedDirectory = async (context, options) => {
269269
// start at the root bucket and descend, loading nodes as we go
270270
const rootBucket = await recreateInitialHamtLevel(options.parent.Links)
271271
const node = UnixFS.unmarshal(options.parent.Data)
272-
const importerOptions = defaultImporterOptions()
273-
274-
// NOTE vmx 2021-04-01: in ipfs the hash algorithm is a constant in unixfs
275-
// it's an implementation. Do the option conversion at the boundary between
276-
// ipfs and unixfs.
277-
let hasher
278-
switch (options.hashAlg) {
279-
case 'sha2-256':
280-
hasher = sha256
281-
break
282-
case 'sha2-512':
283-
hasher = sha512
284-
break
285-
default:
286-
throw new Error(`TODO vmx 2021-03-31: Proper error message for unsupported hash algorithms like ${options.hashAlg}`)
287-
}
288272

289273
const shard = new DirSharded({
290274
root: true,
291275
dir: true,
292-
parent: null,
293-
parentKey: null,
276+
parent: undefined,
277+
parentKey: undefined,
294278
path: '',
295279
dirty: true,
296280
flat: false,
297281
mode: node.mode
298-
}, {
299-
hamtHashFn: importerOptions.hamtHashFn,
300-
hamtHashCode: importerOptions.hamtHashCode,
301-
hamtBucketBits: importerOptions.hamtBucketBits,
302-
hasher,
303-
...options
304-
})
282+
}, options)
305283
shard._bucket = rootBucket
306284

307285
if (node.mtime) {
308286
// update mtime if previously set
309-
shard.mtime = new Date()
287+
shard.mtime = {
288+
secs: Math.round(Date.now() / 1000)
289+
}
310290
}
311291

312292
// load subshards until the bucket & position no longer changes

0 commit comments

Comments
 (0)