Skip to content

Commit bab1985

Browse files
vmxrvaggachingbrain
authored
fix: use @ipld/dag-pb instead of ipld-dag-pb (#116)
Swaps out the old ipld stack for the new multiformats stack. Removes dependency on the js-ipfs API to enable easier reuse. BREAKING CHANGE: uses new multiformats stack and takes a blockservice instead of the block api Co-authored-by: Rod Vagg <[email protected]> Co-authored-by: achingbrain <[email protected]>
1 parent dc2d400 commit bab1985

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+741
-844
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: node_js
22
cache: npm
3+
dist: focal
34

45
branches:
56
only:

packages/ipfs-unixfs-exporter/.aegir.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const buildConfig = {
2323
/** @type {import('aegir').PartialOptions} */
2424
module.exports = {
2525
build: {
26+
bundlesizeMax: '34KB',
2627
config: buildConfig
2728
},
2829
test: {

packages/ipfs-unixfs-exporter/package.json

+11-12
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"test": "aegir test",
1313
"build": "aegir build",
1414
"clean": "rimraf ./dist",
15-
"lint": "aegir ts --check && aegir lint",
15+
"lint": "aegir ts -p check && aegir lint",
1616
"coverage": "nyc -s npm run test -t node && nyc report --reporter=html",
17-
"depcheck": "aegir dep-check -i @types/mocha -i @types/sinon -i nyc -i abort-controller -i rimraf -i ipfs-core-types -i copy -i util -i crypto-browserify -i events -i readable-stream"
17+
"depcheck": "aegir dep-check -i @types/mocha -i @types/sinon -i nyc -i abort-controller -i rimraf -i copy -i util -i crypto-browserify -i events -i readable-stream -i interface-blockstore"
1818
},
1919
"repository": {
2020
"type": "git",
@@ -36,37 +36,36 @@
3636
"@types/mocha": "^8.2.1",
3737
"@types/sinon": "^10.0.0",
3838
"abort-controller": "^3.0.0",
39-
"aegir": "^33.1.0",
39+
"aegir": "^34.0.0",
4040
"copy": "^0.3.2",
4141
"crypto-browserify": "^3.12.0",
4242
"detect-node": "^2.0.4",
4343
"events": "^3.3.0",
44-
"ipfs-core-types": "^0.3.1",
4544
"ipfs-unixfs-importer": "^7.0.3",
46-
"ipld": "^0.29.0",
47-
"ipld-block": "^0.11.1",
48-
"ipld-dag-pb": "^0.22.2",
49-
"ipld-in-memory": "^8.0.0",
5045
"it-all": "^1.0.5",
5146
"it-buffer-stream": "^2.0.0",
5247
"it-first": "^1.0.6",
5348
"merge-options": "^3.0.4",
54-
"multicodec": "^3.0.1",
49+
"murmurhash3js-revisited": "^3.0.0",
5550
"native-abort-controller": "^1.0.3",
5651
"nyc": "^15.0.0",
5752
"readable-stream": "^3.6.0",
5853
"rimraf": "^3.0.2",
59-
"sinon": "^10.0.0",
54+
"sinon": "^11.1.1",
6055
"uint8arrays": "^2.1.2",
6156
"util": "^0.12.3"
6257
},
6358
"dependencies": {
64-
"cids": "^1.1.5",
59+
"@ipld/dag-cbor": "^6.0.4",
60+
"@ipld/dag-pb": "^2.0.2",
6561
"err-code": "^3.0.1",
6662
"hamt-sharding": "^2.0.0",
63+
"interface-blockstore": "^1.0.0",
6764
"ipfs-unixfs": "^4.0.3",
6865
"it-last": "^1.0.5",
69-
"multihashing-async": "^2.1.0"
66+
"multiformats": "^9.0.4",
67+
"murmurhash3js-revisited": "^3.0.0",
68+
"uint8arrays": "^2.1.5"
7069
},
7170
"types": "dist/src/index.d.ts",
7271
"files": [

packages/ipfs-unixfs-exporter/src/index.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use strict'
22

33
const errCode = require('err-code')
4-
const CID = require('cids')
4+
const { CID } = require('multiformats/cid')
55
const resolve = require('./resolvers')
66
const last = require('it-last')
77

88
/**
99
* @typedef {import('ipfs-unixfs').UnixFS} UnixFS
10-
* @typedef {import('ipld-dag-pb').DAGNode} DAGNode
11-
* @typedef {import('ipld')} IPLD
10+
* @typedef {import('interface-blockstore').Blockstore} Blockstore
1211
* @typedef {import('./types').ExporterOptions} ExporterOptions
1312
* @typedef {import('./types').UnixFSFile} UnixFSFile
1413
* @typedef {import('./types').UnixFSDirectory} UnixFSDirectory
@@ -32,14 +31,15 @@ const toPathComponents = (path = '') => {
3231
const cidAndRest = (path) => {
3332
if (path instanceof Uint8Array) {
3433
return {
35-
cid: new CID(path),
34+
cid: CID.decode(path),
3635
toResolve: []
3736
}
3837
}
3938

40-
if (CID.isCID(path)) {
39+
const cid = CID.asCID(path)
40+
if (cid) {
4141
return {
42-
cid: path,
42+
cid,
4343
toResolve: []
4444
}
4545
}
@@ -52,7 +52,7 @@ const cidAndRest = (path) => {
5252
const output = toPathComponents(path)
5353

5454
return {
55-
cid: new CID(output[0]),
55+
cid: CID.parse(output[0]),
5656
toResolve: output.slice(1)
5757
}
5858
}
@@ -62,10 +62,10 @@ const cidAndRest = (path) => {
6262

6363
/**
6464
* @param {string | CID} path
65-
* @param {IPLD} ipld
65+
* @param {Blockstore} blockstore
6666
* @param {ExporterOptions} [options]
6767
*/
68-
async function * walkPath (path, ipld, options = {}) {
68+
async function * walkPath (path, blockstore, options = {}) {
6969
let {
7070
cid,
7171
toResolve
@@ -75,7 +75,7 @@ async function * walkPath (path, ipld, options = {}) {
7575
const startingDepth = toResolve.length
7676

7777
while (true) {
78-
const result = await resolve(cid, name, entryPath, toResolve, startingDepth, ipld, options)
78+
const result = await resolve(cid, name, entryPath, toResolve, startingDepth, blockstore, options)
7979

8080
if (!result.entry && !result.next) {
8181
throw errCode(new Error(`Could not resolve ${path}`), 'ERR_NOT_FOUND')
@@ -99,11 +99,11 @@ async function * walkPath (path, ipld, options = {}) {
9999

100100
/**
101101
* @param {string | CID} path
102-
* @param {IPLD} ipld
102+
* @param {Blockstore} blockstore
103103
* @param {ExporterOptions} [options]
104104
*/
105-
async function exporter (path, ipld, options = {}) {
106-
const result = await last(walkPath(path, ipld, options))
105+
async function exporter (path, blockstore, options = {}) {
106+
const result = await last(walkPath(path, blockstore, options))
107107

108108
if (!result) {
109109
throw errCode(new Error(`Could not resolve ${path}`), 'ERR_NOT_FOUND')
@@ -114,11 +114,11 @@ async function exporter (path, ipld, options = {}) {
114114

115115
/**
116116
* @param {string | CID} path
117-
* @param {IPLD} ipld
117+
* @param {Blockstore} blockstore
118118
* @param {ExporterOptions} [options]
119119
*/
120-
async function * recursive (path, ipld, options = {}) {
121-
const node = await exporter(path, ipld, options)
120+
async function * recursive (path, blockstore, options = {}) {
121+
const node = await exporter(path, blockstore, options)
122122

123123
if (!node) {
124124
return

packages/ipfs-unixfs-exporter/src/resolvers/dag-cbor.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict'
22

3-
const CID = require('cids')
3+
const { CID } = require('multiformats/cid')
44
const errCode = require('err-code')
5+
const dagCbor = require('@ipld/dag-cbor')
56

67
/**
78
* @typedef {import('../types').Resolver} Resolver
@@ -10,9 +11,9 @@ const errCode = require('err-code')
1011
/**
1112
* @type {Resolver}
1213
*/
13-
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
14-
const object = await ipld.get(cid, options)
15-
const block = await ipld.get(new CID(1, 'raw', cid.multihash))
14+
const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, options) => {
15+
const block = await blockstore.get(cid)
16+
const object = dagCbor.decode(block)
1617
let subObject = object
1718
let subPath = path
1819

@@ -24,7 +25,8 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
2425
toResolve.shift()
2526
subPath = `${subPath}/${prop}`
2627

27-
if (CID.isCID(subObject[prop])) {
28+
const subObjectCid = CID.asCID(subObject[prop])
29+
if (subObjectCid) {
2830
return {
2931
entry: {
3032
type: 'object',
@@ -39,7 +41,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
3941
}
4042
},
4143
next: {
42-
cid: subObject[prop],
44+
cid: subObjectCid,
4345
name: prop,
4446
path: subPath,
4547
toResolve

packages/ipfs-unixfs-exporter/src/resolvers/identity.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const errCode = require('err-code')
44
const extractDataFromBlock = require('../utils/extract-data-from-block')
55
const validateOffsetAndLength = require('../utils/validate-offset-and-length')
6-
const mh = require('multihashing-async').multihash
6+
const mh = require('multiformats/hashes/digest')
77

88
/**
99
* @typedef {import('../types').ExporterOptions} ExporterOptions
@@ -32,12 +32,11 @@ const rawContent = (node) => {
3232
/**
3333
* @type {Resolver}
3434
*/
35-
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
35+
const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, options) => {
3636
if (toResolve.length) {
3737
throw errCode(new Error(`No link named ${path} found in raw node ${cid}`), 'ERR_NOT_FOUND')
3838
}
39-
40-
const buf = await mh.decode(cid.multihash)
39+
const buf = await mh.decode(cid.multihash.bytes)
4140

4241
return {
4342
entry: {
@@ -47,7 +46,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
4746
cid,
4847
content: rawContent(buf.digest),
4948
depth,
50-
size: buf.length,
49+
size: buf.digest.length,
5150
node: buf.digest
5251
}
5352
}

packages/ipfs-unixfs-exporter/src/resolvers/index.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
const errCode = require('err-code')
44

5+
const dagPb = require('@ipld/dag-pb')
6+
const dagCbor = require('@ipld/dag-cbor')
7+
const raw = require('multiformats/codecs/raw')
8+
const { identity } = require('multiformats/hashes/identity')
9+
510
/**
6-
* @typedef {import('cids')} CID
7-
* @typedef {import('ipld')} IPLD
8-
* @typedef {import('../types').ExporterOptions} ExporterOptions
9-
* @typedef {import('../types').UnixFSEntry} UnixFSEntry
1011
* @typedef {import('../types').Resolver} Resolver
1112
* @typedef {import('../types').Resolve} Resolve
1213
*/
@@ -15,23 +16,23 @@ const errCode = require('err-code')
1516
* @type {{ [ key: string ]: Resolver }}
1617
*/
1718
const resolvers = {
18-
'dag-pb': require('./unixfs-v1'),
19-
raw: require('./raw'),
20-
'dag-cbor': require('./dag-cbor'),
21-
identity: require('./identity')
19+
[dagPb.code]: require('./unixfs-v1'),
20+
[raw.code]: require('./raw'),
21+
[dagCbor.code]: require('./dag-cbor'),
22+
[identity.code]: require('./identity')
2223
}
2324

2425
/**
2526
* @type {Resolve}
2627
*/
27-
function resolve (cid, name, path, toResolve, depth, ipld, options) {
28-
const resolver = resolvers[cid.codec]
28+
function resolve (cid, name, path, toResolve, depth, blockstore, options) {
29+
const resolver = resolvers[cid.code]
2930

3031
if (!resolver) {
31-
throw errCode(new Error(`No resolver for codec ${cid.codec}`), 'ERR_NO_RESOLVER')
32+
throw errCode(new Error(`No resolver for code ${cid.code}`), 'ERR_NO_RESOLVER')
3233
}
3334

34-
return resolver(cid, name, path, toResolve, resolve, depth, ipld, options)
35+
return resolver(cid, name, path, toResolve, resolve, depth, blockstore, options)
3536
}
3637

3738
module.exports = resolve

packages/ipfs-unixfs-exporter/src/resolvers/raw.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ const rawContent = (node) => {
3030
/**
3131
* @type {import('../types').Resolver}
3232
*/
33-
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
33+
const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, options) => {
3434
if (toResolve.length) {
3535
throw errCode(new Error(`No link named ${path} found in raw node ${cid}`), 'ERR_NOT_FOUND')
3636
}
3737

38-
const buf = await ipld.get(cid, options)
38+
const block = await blockstore.get(cid, options)
3939

4040
return {
4141
entry: {
4242
type: 'raw',
4343
name,
4444
path,
4545
cid,
46-
content: rawContent(buf),
46+
content: rawContent(block),
4747
depth,
48-
size: buf.length,
49-
node: buf
48+
size: block.length,
49+
node: block
5050
}
5151
}
5252
}

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/directory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @type {UnixfsV1Resolver}
1111
*/
12-
const directoryContent = (cid, node, unixfs, path, resolve, depth, ipld) => {
12+
const directoryContent = (cid, node, unixfs, path, resolve, depth, blockstore) => {
1313
/**
1414
* @param {ExporterOptions} [options]
1515
* @returns {UnixfsV1DirectoryContent}
@@ -20,7 +20,7 @@ const directoryContent = (cid, node, unixfs, path, resolve, depth, ipld) => {
2020
const links = node.Links.slice(offset, length)
2121

2222
for (const link of links) {
23-
const result = await resolve(link.Hash, link.Name, `${path}/${link.Name}`, [], depth + 1, ipld, options)
23+
const result = await resolve(link.Hash, link.Name || '', `${path}/${link.Name || ''}`, [], depth + 1, blockstore, options)
2424

2525
if (result.entry) {
2626
yield result.entry

0 commit comments

Comments
 (0)