Skip to content

Commit d1f7546

Browse files
committed
WIP: all in using js-multiformats for everying
- One test suite is commented out - bignumber.js was added as dependency to work around a type bug - some types should work, but don't, hence there are too many `// @ts-ignores` - webworker tests are broken, due to a bug in js-multiformats
1 parent 6f7d6f4 commit d1f7546

34 files changed

+648
-658
lines changed

packages/ipfs-unixfs-exporter/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
"detect-node": "^2.0.4",
4040
"ipfs-core-types": "^0.3.0",
4141
"ipfs-unixfs-importer": "^6.0.1",
42-
"ipld": "^0.28.0",
43-
"ipld-dag-pb": "^0.21.0",
44-
"ipld-in-memory": "^7.0.0",
4542
"it-all": "^1.0.5",
4643
"it-buffer-stream": "^2.0.0",
4744
"it-first": "^1.0.6",
@@ -54,7 +51,8 @@
5451
"uint8arrays": "^2.1.2"
5552
},
5653
"dependencies": {
57-
"cids": "^1.1.5",
54+
"@ipld/dag-cbor": "^3.0.0",
55+
"bignumber.js": "^9.0.1",
5856
"err-code": "^3.0.1",
5957
"hamt-sharding": "^2.0.0",
6058
"ipfs-unixfs": "^3.0.1",

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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
1010
* @typedef {import('ipld-dag-pb').DAGNode} DAGNode
11-
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
11+
* @typedef {import('ipfs-core-types/src/block-service').BlockService} BlockService
1212
*
1313
* @typedef {object} UnixFSFile
1414
* @property {'file'} type
@@ -81,15 +81,19 @@ const toPathComponents = (path = '') => {
8181
*/
8282
const cidAndRest = (path) => {
8383
if (path instanceof Uint8Array) {
84+
console.log('vmx: index: path:', path)
8485
return {
85-
cid: new CID(path),
86+
// @ts-ignore
87+
cid: CID.decode(path),
8688
toResolve: []
8789
}
8890
}
8991

90-
if (CID.isCID(path)) {
92+
// @ts-ignore
93+
const cid = CID.asCID(path)
94+
if (cid) {
9195
return {
92-
cid: path,
96+
cid,
9397
toResolve: []
9498
}
9599
}
@@ -102,7 +106,8 @@ const cidAndRest = (path) => {
102106
const output = toPathComponents(path)
103107

104108
return {
105-
cid: new CID(output[0]),
109+
// @ts-ignore
110+
cid: CID.parse(output[0]),
106111
toResolve: output.slice(1)
107112
}
108113
}
@@ -112,20 +117,20 @@ const cidAndRest = (path) => {
112117

113118
/**
114119
* @param {string | CID} path
115-
* @param {IPLD} ipld
120+
* @param {BlockService} blockService
116121
* @param {ExporterOptions} [options]
117122
*/
118-
const walkPath = async function * (path, ipld, options = {}) {
123+
const walkPath = async function * (path, blockService, options = {}) {
119124
let {
120125
cid,
121126
toResolve
122127
} = cidAndRest(path)
123-
let name = cid.toBaseEncodedString()
128+
let name = cid.toString()
124129
let entryPath = name
125130
const startingDepth = toResolve.length
126131

127132
while (true) {
128-
const result = await resolve(cid, name, entryPath, toResolve, startingDepth, ipld, options)
133+
const result = await resolve(cid, name, entryPath, toResolve, startingDepth, blockService, options)
129134

130135
if (!result.entry && !result.next) {
131136
throw errCode(new Error(`Could not resolve ${path}`), 'ERR_NOT_FOUND')
@@ -149,11 +154,11 @@ const walkPath = async function * (path, ipld, options = {}) {
149154

150155
/**
151156
* @param {string | CID} path
152-
* @param {IPLD} ipld
157+
* @param {BlockService} blockService
153158
* @param {ExporterOptions} [options]
154159
*/
155-
const exporter = async (path, ipld, options = {}) => {
156-
const result = await last(walkPath(path, ipld, options))
160+
const exporter = async (path, blockService, options = {}) => {
161+
const result = await last(walkPath(path, blockService, options))
157162

158163
if (!result) {
159164
throw errCode(new Error(`Could not resolve ${path}`), 'ERR_NOT_FOUND')
@@ -164,11 +169,11 @@ const exporter = async (path, ipld, options = {}) => {
164169

165170
/**
166171
* @param {string | CID} path
167-
* @param {IPLD} ipld
172+
* @param {BlockService} blockService
168173
* @param {ExporterOptions} [options]
169174
*/
170-
const recursive = async function * (path, ipld, options = {}) {
171-
const node = await exporter(path, ipld, options)
175+
const recursive = async function * (path, blockService, options = {}) {
176+
const node = await exporter(path, blockService, options)
172177

173178
if (!node) {
174179
return

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

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

3-
const CID = require('cids')
3+
const CID = require('multiformats/cid')
44
const errCode = require('err-code')
5+
// @ts-ignore
6+
const dagCbor = require('@ipld/dag-cbor')
57

68
/**
79
* @type {import('./').Resolver}
810
*/
9-
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
10-
const object = await ipld.get(cid, options)
11-
const block = await ipld.get(new CID(1, 'raw', cid.multihash))
11+
const resolve = async (cid, name, path, toResolve, resolve, depth, blockService, options) => {
12+
const block = await blockService.get(cid)
13+
const object = dagCbor.decode(block.data)
1214
let subObject = object
1315
let subPath = path
1416

@@ -20,21 +22,25 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
2022
toResolve.shift()
2123
subPath = `${subPath}/${prop}`
2224

23-
if (CID.isCID(subObject[prop])) {
25+
// @ts-ignore
26+
const subObjectCid = CID.asCID(subObject[prop])
27+
if (subObjectCid) {
2428
return {
2529
entry: {
2630
type: 'object',
2731
name,
2832
path,
33+
// @ts-ignore
2934
cid,
35+
// @ts-ignore
3036
node: block,
3137
depth,
3238
content: async function * () {
3339
yield object
3440
}
3541
},
3642
next: {
37-
cid: subObject[prop],
43+
cid: subObjectCid,
3844
name: prop,
3945
path: subPath,
4046
toResolve
@@ -45,7 +51,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
4551
subObject = subObject[prop]
4652
} else {
4753
// cannot resolve further
48-
throw errCode(new Error(`No property named ${prop} found in cbor node ${cid.toBaseEncodedString()}`), 'ERR_NO_PROP')
54+
throw errCode(new Error(`No property named ${prop} found in cbor node ${cid.toString()}`), 'ERR_NO_PROP')
4955
}
5056
}
5157

@@ -54,7 +60,9 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
5460
type: 'object',
5561
name,
5662
path,
63+
// @ts-ignore
5764
cid,
65+
// @ts-ignore
5866
node: block,
5967
depth,
6068
content: async function * () {

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

+6-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('../').ExporterOptions} ExporterOptions
@@ -31,18 +31,19 @@ const rawContent = (node) => {
3131
/**
3232
* @type {import('./').Resolver}
3333
*/
34-
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
34+
const resolve = async (cid, name, path, toResolve, resolve, depth, blockService, options) => {
3535
if (toResolve.length) {
36-
throw errCode(new Error(`No link named ${path} found in raw node ${cid.toBaseEncodedString()}`), 'ERR_NOT_FOUND')
36+
throw errCode(new Error(`No link named ${path} found in raw node ${cid.toString()}`), 'ERR_NOT_FOUND')
3737
}
38-
39-
const buf = await mh.decode(cid.multihash)
38+
// @ts-ignore
39+
const buf = await mh.decode(cid.multihash.bytes)
4040

4141
return {
4242
entry: {
4343
type: 'identity',
4444
name,
4545
path,
46+
// @ts-ignore
4647
cid,
4748
content: rawContent(buf.digest),
4849
depth,

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

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

33
const errCode = require('err-code')
4+
const multicodec = require('multicodec')
45

56
/**
6-
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
7+
* @typedef {import('ipfs-core-types/src/block-service').BlockService} BlockService
78
* @typedef {import('../').ExporterOptions} ExporterOptions
89
* @typedef {import('../').UnixFSEntry} UnixFSEntry
910
* @typedef {import('cids')} CID
@@ -23,30 +24,30 @@ const errCode = require('err-code')
2324

2425
/**
2526
*
26-
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], depth: number, ipld: IPLD, options: ExporterOptions) => Promise<ResolveResult>} Resolve
27+
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], depth: number, blockService: BlockService, options: ExporterOptions) => Promise<ResolveResult>} Resolve
2728
*
28-
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], resolve: Resolve, depth: number, ipld: IPLD, options: ExporterOptions) => Promise<ResolveResult>} Resolver
29+
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], resolve: Resolve, depth: number, blockService: BlockService, options: ExporterOptions) => Promise<ResolveResult>} Resolver
2930
*
3031
* @type {{ [ key: string ]: Resolver }}
3132
*/
3233
const resolvers = {
33-
'dag-pb': require('./unixfs-v1'),
34-
raw: require('./raw'),
35-
'dag-cbor': require('./dag-cbor'),
36-
identity: require('./identity')
34+
[multicodec.DAG_PB]: require('./unixfs-v1'),
35+
[multicodec.RAW]: require('./raw'),
36+
[multicodec.DAG_CBOR]: require('./dag-cbor'),
37+
[multicodec.IDENTITY]: require('./identity')
3738
}
3839

3940
/**
4041
* @type {Resolve}
4142
*/
42-
function resolve (cid, name, path, toResolve, depth, ipld, options) {
43-
const resolver = resolvers[cid.codec]
43+
function resolve (cid, name, path, toResolve, depth, blockService, options) {
44+
const resolver = resolvers[cid.code]
4445

4546
if (!resolver) {
46-
throw errCode(new Error(`No resolver for codec ${cid.codec}`), 'ERR_NO_RESOLVER')
47+
throw errCode(new Error(`No resolver for codec ${multicodec.getName(cid.code)}`), 'ERR_NO_RESOLVER')
4748
}
4849

49-
return resolver(cid, name, path, toResolve, resolve, depth, ipld, options)
50+
return resolver(cid, name, path, toResolve, resolve, depth, blockService, options)
5051
}
5152

5253
module.exports = resolve

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

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

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

4040
return {
4141
entry: {
4242
type: 'raw',
4343
name,
4444
path,
45+
// @ts-ignore
4546
cid,
46-
content: rawContent(buf),
47+
content: rawContent(block.data),
4748
depth,
48-
node: buf
49+
// @ts-ignore
50+
node: block.bytes
4951
}
5052
}
5153
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @type {import('../').UnixfsV1Resolver}
88
*/
9-
const directoryContent = (cid, node, unixfs, path, resolve, depth, ipld) => {
9+
const directoryContent = (cid, node, unixfs, path, resolve, depth, blockService) => {
1010
/**
1111
* @param {ExporterOptions} [options]
1212
* @returns {UnixfsV1DirectoryContent}
@@ -17,7 +17,7 @@ const directoryContent = (cid, node, unixfs, path, resolve, depth, ipld) => {
1717
const links = node.Links.slice(offset, length)
1818

1919
for (const link of links) {
20-
const result = await resolve(link.Hash, link.Name, `${path}/${link.Name}`, [], depth + 1, ipld, options)
20+
const result = await resolve(link.Hash, link.Name, `${path}/${link.Name}`, [], depth + 1, blockService, options)
2121

2222
if (result.entry) {
2323
yield result.entry

0 commit comments

Comments
 (0)