Skip to content

Commit c553824

Browse files
committed
chore: swap ipfs-block-service for interface-blockstore and remove multihashing deps
1 parent 63827a4 commit c553824

Some content is hidden

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

41 files changed

+328
-442
lines changed

packages/ipfs-unixfs-exporter/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,30 @@
4141
"crypto-browserify": "^3.12.0",
4242
"detect-node": "^2.0.4",
4343
"events": "^3.3.0",
44-
"ipfs-core-types": "^0.3.1",
44+
"ipfs-core-types": "^0.5.2",
4545
"ipfs-unixfs-importer": "^7.0.3",
4646
"it-all": "^1.0.5",
4747
"it-buffer-stream": "^2.0.0",
4848
"it-first": "^1.0.6",
4949
"merge-options": "^3.0.4",
50+
"murmurhash3js-revisited": "^3.0.0",
5051
"native-abort-controller": "^1.0.3",
5152
"nyc": "^15.0.0",
5253
"readable-stream": "^3.6.0",
5354
"rimraf": "^3.0.2",
54-
"sinon": "^10.0.0",
55+
"sinon": "^11.1.1",
5556
"uint8arrays": "^2.1.2",
5657
"util": "^0.12.3"
5758
},
5859
"dependencies": {
59-
"@ipld/dag-cbor": "^5.0.0",
60-
"@ipld/dag-pb": "^1.1.0",
60+
"@ipld/dag-cbor": "^6.0.4",
61+
"@ipld/dag-pb": "^2.0.2",
6162
"err-code": "^3.0.1",
6263
"hamt-sharding": "^2.0.0",
64+
"interface-blockstore": "^0.0.5",
6365
"ipfs-unixfs": "^4.0.3",
6466
"it-last": "^1.0.5",
65-
"multicodec": "^3.0.1",
66-
"multiformats": "^8.0.3",
67-
"multihashing-async": "^2.1.0"
67+
"multiformats": "^9.0.4"
6868
},
6969
"types": "dist/src/index.d.ts",
7070
"files": [

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const last = require('it-last')
77

88
/**
99
* @typedef {import('ipfs-unixfs').UnixFS} UnixFS
10-
* @typedef {import('ipfs-unixfs-importer/src/types').BlockAPI} BlockAPI
10+
* @typedef {import('interface-blockstore').Blockstore} Blockstore
1111
* @typedef {import('./types').ExporterOptions} ExporterOptions
1212
* @typedef {import('./types').UnixFSFile} UnixFSFile
1313
* @typedef {import('./types').UnixFSDirectory} UnixFSDirectory
@@ -62,10 +62,10 @@ const cidAndRest = (path) => {
6262

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

7777
while (true) {
78-
const result = await resolve(cid, name, entryPath, toResolve, startingDepth, blockService, 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, blockService, options = {}) {
9999

100100
/**
101101
* @param {string | CID} path
102-
* @param {BlockAPI} blockService
102+
* @param {Blockstore} blockstore
103103
* @param {ExporterOptions} [options]
104104
*/
105-
async function exporter (path, blockService, options = {}) {
106-
const result = await last(walkPath(path, blockService, 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, blockService, options = {}) {
114114

115115
/**
116116
* @param {string | CID} path
117-
* @param {BlockAPI} blockService
117+
* @param {Blockstore} blockstore
118118
* @param {ExporterOptions} [options]
119119
*/
120-
async function * recursive (path, blockService, options = {}) {
121-
const node = await exporter(path, blockService, 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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const dagCbor = require('@ipld/dag-cbor')
1111
/**
1212
* @type {Resolver}
1313
*/
14-
const resolve = async (cid, name, path, toResolve, resolve, depth, blockService, options) => {
15-
const block = await blockService.get(cid)
16-
const object = dagCbor.decode(block.bytes)
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)
1717
let subObject = object
1818
let subPath = path
1919

@@ -33,7 +33,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockService,
3333
name,
3434
path,
3535
cid,
36-
node: block.bytes,
36+
node: block,
3737
depth,
3838
size: block.length,
3939
content: async function * () {
@@ -62,7 +62,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, blockService,
6262
name,
6363
path,
6464
cid,
65-
node: block.bytes,
65+
node: block,
6666
depth,
6767
size: block.length,
6868
content: async function * () {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const rawContent = (node) => {
3232
/**
3333
* @type {Resolver}
3434
*/
35-
const resolve = async (cid, name, path, toResolve, resolve, depth, blockService, 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
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
22

33
const errCode = require('err-code')
4-
const multicodec = require('multicodec')
4+
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')
59

610
/**
7-
* @typedef {import('../').BlockAPI} BlockAPI
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,24 +16,23 @@ const multicodec = require('multicodec')
1516
* @type {{ [ key: string ]: Resolver }}
1617
*/
1718
const resolvers = {
18-
[multicodec.DAG_PB]: require('./unixfs-v1'),
19-
[multicodec.RAW]: require('./raw'),
20-
[multicodec.DAG_CBOR]: require('./dag-cbor'),
21-
[multicodec.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, blockService, options) {
28+
function resolve (cid, name, path, toResolve, depth, blockstore, options) {
2829
const resolver = resolvers[cid.code]
2930

3031
if (!resolver) {
31-
// @ts-ignore - A `CodecCode` is expected, but a number is just fine
32-
throw errCode(new Error(`No resolver for codec ${multicodec.getName(cid.code)}`), 'ERR_NO_RESOLVER')
32+
throw errCode(new Error(`No resolver for code ${cid.code}`), 'ERR_NO_RESOLVER')
3333
}
3434

35-
return resolver(cid, name, path, toResolve, resolve, depth, blockService, options)
35+
return resolver(cid, name, path, toResolve, resolve, depth, blockstore, options)
3636
}
3737

3838
module.exports = resolve

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

Lines changed: 5 additions & 5 deletions
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, blockService, 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 block = await blockService.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(block.bytes),
46+
content: rawContent(block),
4747
depth,
48-
size: block.bytes.length,
49-
node: block.bytes
48+
size: block.length,
49+
node: block
5050
}
5151
}
5252
}

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

Lines changed: 2 additions & 2 deletions
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, blockService) => {
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, blockService)
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, blockService, 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

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ const { UnixFS } = require('ipfs-unixfs')
66
const errCode = require('err-code')
77
const dagPb = require('@ipld/dag-pb')
88
const dagCbor = require('@ipld/dag-cbor')
9-
const mc = require('multicodec')
9+
const raw = require('multiformats/codecs/raw')
1010

1111
/**
1212
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
13-
* @typedef {import('ipfs-unixfs-importer/src/types').BlockAPI} BlockService
13+
* @typedef {import('interface-blockstore').Blockstore} Blockstore
1414
* @typedef {import('@ipld/dag-pb').PBNode} PBNode
1515
*
16-
* @param {BlockService} blockService
16+
* @param {Blockstore} blockstore
1717
* @param {PBNode} node
1818
* @param {number} start
1919
* @param {number} end
2020
* @param {number} streamPosition
2121
* @param {ExporterOptions} options
2222
* @returns {AsyncIterable<Uint8Array>}
2323
*/
24-
async function * emitBytes (blockService, node, start, end, streamPosition = 0, options) {
24+
async function * emitBytes (blockstore, node, start, end, streamPosition = 0, options) {
2525
// a `raw` node
2626
if (node instanceof Uint8Array) {
2727
const buf = extractDataFromBlock(node, streamPosition, start, end)
@@ -68,26 +68,25 @@ async function * emitBytes (blockService, node, start, end, streamPosition = 0,
6868
if ((start >= childStart && start < childEnd) || // child has offset byte
6969
(end > childStart && end <= childEnd) || // child has end byte
7070
(start < childStart && end > childEnd)) { // child is between offset and end bytes
71-
const block = await blockService.get(childLink.Hash, {
71+
const block = await blockstore.get(childLink.Hash, {
7272
signal: options.signal
7373
})
7474
let child
7575
switch (childLink.Hash.code) {
76-
case mc.DAG_PB:
77-
child = await dagPb.decode(block.bytes)
76+
case dagPb.code:
77+
child = await dagPb.decode(block)
7878
break
79-
case mc.RAW:
80-
child = block.bytes
79+
case raw.code:
80+
child = block
8181
break
82-
case mc.DAG_CBOR:
83-
child = await dagCbor.decode(block.bytes)
82+
case dagCbor.code:
83+
child = await dagCbor.decode(block)
8484
break
8585
default:
86-
// @ts-ignore - A `CodecCode` is expected, but a number is just fine
87-
throw Error(`Unsupported codec: ${mc.getName(childLink.Hash.code)}`)
86+
throw Error(`Unsupported codec: ${childLink.Hash.code}`)
8887
}
8988

90-
for await (const buf of emitBytes(blockService, child, start, end, streamPosition, options)) {
89+
for await (const buf of emitBytes(blockstore, child, start, end, streamPosition, options)) {
9190
streamPosition += buf.length
9291

9392
yield buf
@@ -102,7 +101,7 @@ async function * emitBytes (blockService, node, start, end, streamPosition = 0,
102101
/**
103102
* @type {import('../').UnixfsV1Resolver}
104103
*/
105-
const fileContent = (cid, node, unixfs, path, resolve, depth, blockService) => {
104+
const fileContent = (cid, node, unixfs, path, resolve, depth, blockstore) => {
106105
/**
107106
* @param {ExporterOptions} options
108107
*/
@@ -121,7 +120,7 @@ const fileContent = (cid, node, unixfs, path, resolve, depth, blockService) => {
121120
const start = offset
122121
const end = offset + length
123122

124-
return emitBytes(blockService, node, start, end, 0, options)
123+
return emitBytes(blockstore, node, start, end, 0, options)
125124
}
126125

127126
return yieldFileContent

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { decode } = require('@ipld/dag-pb')
44

55
/**
6-
* @typedef {import('ipfs-unixfs-importer/src/types').BlockAPI} BlockAPI
6+
* @typedef {import('interface-blockstore').Blockstore} Blockstore
77
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
88
* @typedef {import('../../../types').Resolve} Resolve
99
* @typedef {import('../../../types').UnixfsV1DirectoryContent} UnixfsV1DirectoryContent
@@ -14,13 +14,13 @@ const { decode } = require('@ipld/dag-pb')
1414
/**
1515
* @type {UnixfsV1Resolver}
1616
*/
17-
const hamtShardedDirectoryContent = (cid, node, unixfs, path, resolve, depth, blockService) => {
17+
const hamtShardedDirectoryContent = (cid, node, unixfs, path, resolve, depth, blockstore) => {
1818
/**
1919
* @param {ExporterOptions} options
2020
*
2121
*/
2222
function yieldHamtDirectoryContent (options = {}) {
23-
return listDirectory(node, path, resolve, depth, blockService, options)
23+
return listDirectory(node, path, resolve, depth, blockstore, options)
2424
}
2525

2626
return yieldHamtDirectoryContent
@@ -31,27 +31,27 @@ const hamtShardedDirectoryContent = (cid, node, unixfs, path, resolve, depth, bl
3131
* @param {string} path
3232
* @param {Resolve} resolve
3333
* @param {number} depth
34-
* @param {BlockAPI} blockService
34+
* @param {Blockstore} blockstore
3535
* @param {ExporterOptions} options
3636
*
3737
* @returns {UnixfsV1DirectoryContent}
3838
*/
39-
async function * listDirectory (node, path, resolve, depth, blockService, options) {
39+
async function * listDirectory (node, path, resolve, depth, blockstore, options) {
4040
const links = node.Links
4141

4242
for (const link of links) {
4343
const name = link.Name != null ? link.Name.substring(2) : null
4444

4545
if (name) {
46-
const result = await resolve(link.Hash, name, `${path}/${name}`, [], depth + 1, blockService, options)
46+
const result = await resolve(link.Hash, name, `${path}/${name}`, [], depth + 1, blockstore, options)
4747

4848
yield result.entry
4949
} else {
5050
// descend into subshard
51-
const block = await blockService.get(link.Hash)
52-
node = decode(block.bytes)
51+
const block = await blockstore.get(link.Hash)
52+
node = decode(block)
5353

54-
for await (const file of listDirectory(node, path, resolve, depth, blockService, options)) {
54+
for await (const file of listDirectory(node, path, resolve, depth, blockstore, options)) {
5555
yield file
5656
}
5757
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const validateOffsetAndLength = require('../../../utils/validate-offset-and-leng
1111
/**
1212
* @type {UnixfsV1Resolver}
1313
*/
14-
const rawContent = (cid, node, unixfs, path, resolve, depth, blockService) => {
14+
const rawContent = (cid, node, unixfs, path, resolve, depth, blockstore) => {
1515
/**
1616
* @param {ExporterOptions} options
1717
*/

0 commit comments

Comments
 (0)