Skip to content

Commit

Permalink
fix: use @ipld/dag-pb instead of ipld-dag-pb
Browse files Browse the repository at this point in the history
Use the rewrite of the dag-pb codec.
  • Loading branch information
vmx committed Feb 16, 2021
1 parent 010ab47 commit 9794e85
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-importer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
"sinon": "^9.0.1"
},
"dependencies": {
"@ipld/dag-pb": "0.0.1",
"bl": "^4.0.0",
"err-code": "^2.0.0",
"hamt-sharding": "^1.0.0",
"ipfs-unixfs": "^2.0.4",
"ipfs-utils": "^5.0.0",
"ipld-dag-pb": "^0.20.0",
"it-all": "^1.0.1",
"it-batch": "^1.0.3",
"it-first": "^1.0.1",
Expand Down
7 changes: 4 additions & 3 deletions packages/ipfs-unixfs-importer/src/dag-builder/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
const UnixFS = require('ipfs-unixfs')
const persist = require('../utils/persist')
const {
DAGNode
} = require('ipld-dag-pb')
encode,
prepare
} = require('@ipld/dag-pb')

const dirBuilder = async (item, block, options) => {
const unixfs = new UnixFS({
Expand All @@ -13,7 +14,7 @@ const dirBuilder = async (item, block, options) => {
mode: item.mode
})

const buffer = new DAGNode(unixfs.marshal()).serialize()
const buffer = encode(prepare({ Data: unixfs.marshal() }))
const cid = await persist(buffer, block, options)
const path = item.path

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
const UnixFS = require('ipfs-unixfs')
const persist = require('../../utils/persist')
const {
DAGNode
} = require('ipld-dag-pb')
encode,
prepare
} = require('@ipld/dag-pb')

async function * bufferImporter (file, source, block, options) {
for await (let buffer of source) {
Expand All @@ -27,7 +28,7 @@ async function * bufferImporter (file, source, block, options) {
mode: file.mode
})

buffer = new DAGNode(unixfs.marshal()).serialize()
buffer = encode(prepare({ Data: unixfs.marshal() }))
}

return {
Expand Down
27 changes: 19 additions & 8 deletions packages/ipfs-unixfs-importer/src/dag-builder/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const errCode = require('err-code')
const UnixFS = require('ipfs-unixfs')
const persist = require('../../utils/persist')
const {
DAGNode,
DAGLink
} = require('ipld-dag-pb')
encode,
prepare
} = require('@ipld/dag-pb')
const all = require('it-all')
const parallelBatch = require('it-parallel-batch')
const mh = require('multihashing-async').multihash
Expand Down Expand Up @@ -66,7 +66,7 @@ const reduce = (file, block, options) => {
})

const multihash = mh.decode(leaf.cid.multihash)
buffer = new DAGNode(leaf.unixfs.marshal()).serialize()
buffer = encode(prepare({ Data: leaf.unixfs.marshal() }))

leaf.cid = await persist(buffer, block, {
...options,
Expand Down Expand Up @@ -109,7 +109,11 @@ const reduce = (file, block, options) => {
// node is a leaf buffer
f.addBlockSize(leaf.size)

return new DAGLink(leaf.name, leaf.size, leaf.cid)
return {
Name: leaf.name === undefined ? '' : leaf.name,
Tsize: leaf.size,
Hash: leaf.cid
}
}

if (!leaf.unixfs.data) {
Expand All @@ -120,11 +124,18 @@ const reduce = (file, block, options) => {
f.addBlockSize(leaf.unixfs.data.length)
}

return new DAGLink(leaf.name, leaf.size, leaf.cid)
return {
Name: leaf.name === undefined ? '' : leaf.name,
Tsize: leaf.size,
Hash: leaf.cid
}
})

const node = new DAGNode(f.marshal(), links)
const buffer = node.serialize()
const node = {
Data: f.marshal(),
Links: links
}
const buffer = encode(prepare(node))
const cid = await persist(buffer, block, options)

return {
Expand Down
16 changes: 10 additions & 6 deletions packages/ipfs-unixfs-importer/src/dir-flat.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const {
DAGLink,
DAGNode
} = require('ipld-dag-pb')
encode,
prepare
} = require('@ipld/dag-pb')
const UnixFS = require('ipfs-unixfs')
const Dir = require('./dir')
const persist = require('./utils/persist')
Expand Down Expand Up @@ -65,7 +65,11 @@ class DirFlat extends Dir {
}
}

links.push(new DAGLink(children[i], child.size, child.cid))
links.push({
Name: children[i],
Tsize: child.size,
Hash: child.cid
})
}

const unixfs = new UnixFS({
Expand All @@ -74,8 +78,8 @@ class DirFlat extends Dir {
mode: this.mode
})

const node = new DAGNode(unixfs.marshal(), links)
const buffer = node.serialize()
const node = { Data: unixfs.marshal(), Links: links }
const buffer = encode(prepare(node))
const cid = await persist(buffer, block, this.options)
const size = buffer.length + node.Links.reduce((acc, curr) => acc + curr.Tsize, 0)

Expand Down
31 changes: 23 additions & 8 deletions packages/ipfs-unixfs-importer/src/dir-sharded.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const {
DAGLink,
DAGNode
} = require('ipld-dag-pb')
encode,
prepare
} = require('@ipld/dag-pb')
const UnixFS = require('ipfs-unixfs')
const multihashing = require('multihashing-async')
const Dir = require('./dir')
Expand Down Expand Up @@ -110,7 +110,11 @@ async function * flush (path, bucket, block, shardRoot, options) {
shard = subShard
}

links.push(new DAGLink(labelPrefix, shard.size, shard.cid))
links.push({
Name: labelPrefix,
Tsize: shard.size,
Hash: shard.cid
})
childrenSize += shard.size
} else if (typeof child.value.flush === 'function') {
const dir = child.value
Expand All @@ -123,7 +127,11 @@ async function * flush (path, bucket, block, shardRoot, options) {
}

const label = labelPrefix + child.key
links.push(new DAGLink(label, flushedDir.size, flushedDir.cid))
links.push({
Name: label,
Tsize: flushedDir.size,
Hash: flushedDir.cid
})

childrenSize += flushedDir.size
} else {
Expand All @@ -136,7 +144,11 @@ async function * flush (path, bucket, block, shardRoot, options) {
const label = labelPrefix + child.key
const size = value.size

links.push(new DAGLink(label, size, value.cid))
links.push({
Name: label,
Tsize: size,
Hash: value.cid
})
childrenSize += size
}
}
Expand All @@ -153,8 +165,11 @@ async function * flush (path, bucket, block, shardRoot, options) {
mode: shardRoot && shardRoot.mode
})

const node = new DAGNode(dir.marshal(), links)
const buffer = node.serialize()
const node = {
Data: dir.marshal(),
Links: links
}
const buffer = encode(prepare(node))
const cid = await persist(buffer, block, options)
const size = buffer.length + childrenSize

Expand Down

0 comments on commit 9794e85

Please sign in to comment.