Skip to content

Commit 6cfd51d

Browse files
authored
fix: use ipfs-core-types instead of redefining ipld (#121)
Adds timeout and signal options to exporter
1 parent 1ceb097 commit 6cfd51d

25 files changed

+64
-51
lines changed

packages/ipfs-unixfs-exporter/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ Entries with a `dag-cbor` codec `CID` return JavaScript object entries:
159159
name: 'foo.txt',
160160
path: 'Qmbar/foo.txt',
161161
cid: CID, // see https://github.com/multiformats/js-cid
162-
node: Object, // see https://github.com/ipld/js-ipld-dag-cbor
162+
node: Uint8Array,
163+
content: function // returns an async iterator that yields a single object - see https://github.com/ipld/js-ipld-dag-cbor
163164
}
164165
```
165166

packages/ipfs-unixfs-exporter/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"clean": "rimraf ./dist",
1414
"lint": "aegir lint",
1515
"coverage": "nyc -s npm run test -t node && nyc report --reporter=html",
16-
"depcheck": "aegir dep-check -i @types/mocha -i @types/sinon -i nyc -i abort-controller -i rimraf"
16+
"depcheck": "aegir dep-check -i @types/mocha -i @types/sinon -i nyc -i abort-controller -i rimraf -i ipfs-core-types"
1717
},
1818
"repository": {
1919
"type": "git",
@@ -37,6 +37,7 @@
3737
"abort-controller": "^3.0.0",
3838
"aegir": "^30.3.0",
3939
"detect-node": "^2.0.4",
40+
"ipfs-core-types": "^0.3.0",
4041
"ipfs-unixfs-importer": "^6.0.0",
4142
"ipld": "^0.28.0",
4243
"ipld-dag-pb": "^0.21.0",

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const last = require('it-last')
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
1112
*
1213
* @typedef {object} UnixFSFile
1314
* @property {'file'} type
@@ -35,7 +36,8 @@ const last = require('it-last')
3536
* @property {string} path
3637
* @property {CID} cid
3738
* @property {number} depth
38-
* @property {any} node
39+
* @property {Uint8Array} node
40+
* @property {(options?: ExporterOptions) => AsyncIterable<any>} content
3941
*
4042
* @typedef {object} RawNode
4143
* @property {'raw'} type
@@ -59,13 +61,11 @@ const last = require('it-last')
5961
*/
6062

6163
/**
62-
* @typedef {object} IPLDResolver
63-
* @property {(cid: CID, options?: any) => Promise<any>} get
64-
* @property {(node: any, codec: number, options?: any) => Promise<CID>} put
65-
*
6664
* @typedef {object} ExporterOptions
6765
* @property {number} [offset=0]
6866
* @property {number} [length]
67+
* @property {AbortSignal} [signal]
68+
* @property {number} [timeout]
6969
*/
7070

7171
const toPathComponents = (path = '') => {
@@ -112,7 +112,7 @@ const cidAndRest = (path) => {
112112

113113
/**
114114
* @param {string | CID} path
115-
* @param {IPLDResolver} ipld
115+
* @param {IPLD} ipld
116116
* @param {ExporterOptions} [options]
117117
*/
118118
const walkPath = async function * (path, ipld, options = {}) {
@@ -149,7 +149,7 @@ const walkPath = async function * (path, ipld, options = {}) {
149149

150150
/**
151151
* @param {string | CID} path
152-
* @param {IPLDResolver} ipld
152+
* @param {IPLD} ipld
153153
* @param {ExporterOptions} [options]
154154
*/
155155
const exporter = async (path, ipld, options = {}) => {
@@ -164,7 +164,7 @@ const exporter = async (path, ipld, options = {}) => {
164164

165165
/**
166166
* @param {string | CID} path
167-
* @param {IPLDResolver} ipld
167+
* @param {IPLD} ipld
168168
* @param {ExporterOptions} [options]
169169
*/
170170
const recursive = async function * (path, ipld, options = {}) {

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const errCode = require('err-code')
77
* @type {import('./').Resolver}
88
*/
99
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
10-
const node = await ipld.get(cid, options)
11-
let subObject = node
10+
const object = await ipld.get(cid, options)
11+
const block = await ipld.get(new CID(1, 'raw', cid.multihash))
12+
let subObject = object
1213
let subPath = path
1314

1415
while (toResolve.length) {
@@ -26,8 +27,11 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
2627
name,
2728
path,
2829
cid,
29-
node,
30-
depth
30+
node: block,
31+
depth,
32+
content: async function * () {
33+
yield object
34+
}
3135
},
3236
next: {
3337
cid: subObject[prop],
@@ -51,8 +55,11 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
5155
name,
5256
path,
5357
cid,
54-
node,
55-
depth
58+
node: block,
59+
depth,
60+
content: async function * () {
61+
yield object
62+
}
5663
}
5764
}
5865
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const errCode = require('err-code')
44

55
/**
6-
* @typedef {import('../').IPLDResolver} IPLDResolver
6+
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
77
* @typedef {import('../').ExporterOptions} ExporterOptions
88
* @typedef {import('../').UnixFSEntry} UnixFSEntry
99
* @typedef {import('cids')} CID
@@ -23,9 +23,9 @@ const errCode = require('err-code')
2323

2424
/**
2525
*
26-
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], depth: number, ipld: IPLDResolver, options: ExporterOptions) => Promise<ResolveResult>} Resolve
26+
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], depth: number, ipld: IPLD, options: ExporterOptions) => Promise<ResolveResult>} Resolve
2727
*
28-
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], resolve: Resolve, depth: number, ipld: IPLDResolver, options: ExporterOptions) => Promise<ResolveResult>} Resolver
28+
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], resolve: Resolve, depth: number, ipld: IPLD, options: ExporterOptions) => Promise<ResolveResult>} Resolver
2929
*
3030
* @type {{ [ key: string ]: Resolver }}
3131
*/

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const errCode = require('err-code')
77

88
/**
99
* @typedef {import('../../../').ExporterOptions} ExporterOptions
10-
* @typedef {import('../../../').IPLDResolver} IPLDResolver
10+
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
1111
* @typedef {import('ipld-dag-pb').DAGNode} DAGNode
1212
*
13-
* @param {IPLDResolver} ipld
13+
* @param {IPLD} ipld
1414
* @param {DAGNode} node
1515
* @param {number} start
1616
* @param {number} end
@@ -61,7 +61,10 @@ async function * emitBytes (ipld, node, start, end, streamPosition = 0, options)
6161
if ((start >= childStart && start < childEnd) || // child has offset byte
6262
(end > childStart && end <= childEnd) || // child has end byte
6363
(start < childStart && end > childEnd)) { // child is between offset and end bytes
64-
const child = await ipld.get(childLink.Hash, options)
64+
const child = await ipld.get(childLink.Hash, {
65+
signal: options.signal,
66+
timeout: options.timeout
67+
})
6568

6669
for await (const buf of emitBytes(ipld, child, start, end, streamPosition, options)) {
6770
streamPosition += buf.length

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @typedef {import('../../../').ExporterOptions} ExporterOptions
55
* @typedef {import('ipld-dag-pb').DAGNode} DAGNode
66
* @typedef {import('../../').Resolve} Resolve
7-
* @typedef {import('../../../').IPLDResolver} IPLDResolver
7+
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
88
* @typedef {import('../').UnixfsV1DirectoryContent} UnixfsV1DirectoryContent
99
*
1010
* @type {import('../').UnixfsV1Resolver}
@@ -26,7 +26,7 @@ const hamtShardedDirectoryContent = (cid, node, unixfs, path, resolve, depth, ip
2626
* @param {string} path
2727
* @param {Resolve} resolve
2828
* @param {number} depth
29-
* @param {IPLDResolver} ipld
29+
* @param {IPLD} ipld
3030
* @param {ExporterOptions} options
3131
*
3232
* @returns {UnixfsV1DirectoryContent}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const findShardCid = require('../../utils/find-cid-in-shard')
66

77
/**
88
* @typedef {import('../../').ExporterOptions} ExporterOptions
9-
* @typedef {import('../../').IPLDResolver} IPLDResolver
9+
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
1010
* @typedef {import('../').UnixFSEntry} UnixFSEntry
1111
* @typedef {import('cids')} CID
1212
* @typedef {import('ipld-dag-pb').DAGNode} DAGNode
@@ -28,7 +28,7 @@ const findLinkCid = (node, name) => {
2828
* @typedef {AsyncIterable<UnixFSEntry> | Iterable<UnixFSEntry>} UnixfsV1DirectoryContent
2929
*
3030
* @typedef {UnixfsV1FileContent | UnixfsV1DirectoryContent} UnixfsV1Content
31-
* @typedef {(cid: CID, node: DAGNode, unixfs: UnixFS, path: string, resolve: Resolve, depth: number, ipld: IPLDResolver) => (options: ExporterOptions) => UnixfsV1Content } UnixfsV1Resolver
31+
* @typedef {(cid: CID, node: DAGNode, unixfs: UnixFS, path: string, resolve: Resolve, depth: number, ipld: IPLD) => (options: ExporterOptions) => UnixfsV1Content } UnixfsV1Resolver
3232
*
3333
* @type {{ [key: string]: UnixfsV1Resolver }}
3434
*/

packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const multihashing = require('multihashing-async')
55

66
/**
77
* @typedef {import('../').ExporterOptions} ExporterOptions
8-
* @typedef {import('../').IPLDResolver} IPLDResolver
8+
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
99
* @typedef {import('cids')} CID
1010
*/
1111

@@ -90,7 +90,7 @@ const toBucketPath = (position) => {
9090
*
9191
* @param {import('ipld-dag-pb').DAGNode} node
9292
* @param {string} name
93-
* @param {IPLDResolver} ipld
93+
* @param {IPLD} ipld
9494
* @param {ShardTraversalContext} [context]
9595
* @param {ExporterOptions} [options]
9696
* @returns {Promise<CID|null>}

packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const SHARD_SPLIT_THRESHOLD = 10
3030
describe('exporter sharded', function () {
3131
this.timeout(30000)
3232

33-
/** @type {import('../src').IPLDResolver} */
33+
/** @type {import('ipfs-core-types/src/ipld').IPLD} */
3434
let ipld
3535
/** @type {import('ipfs-unixfs-importer').BlockAPI} */
3636
let block

packages/ipfs-unixfs-exporter/test/exporter-subtree.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const ONE_MEG = Math.pow(1024, 2)
1818
const exporter = require('./../src')
1919

2020
describe('exporter subtree', () => {
21-
/** @type {import('../src').IPLDResolver} */
21+
/** @type {import('ipfs-core-types/src/ipld').IPLD} */
2222
let ipld
2323
/** @type {import('ipfs-unixfs-importer').BlockAPI} */
2424
let block

packages/ipfs-unixfs-exporter/test/exporter.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const uint8ArrayConcat = require('uint8arrays/concat')
2929
const ONE_MEG = Math.pow(1024, 2)
3030

3131
describe('exporter', () => {
32-
/** @type {import('../src').IPLDResolver} */
32+
/** @type {import('ipfs-core-types/src/ipld').IPLD} */
3333
let ipld
3434
/** @type {import('ipfs-unixfs-importer').BlockAPI} */
3535
let block
@@ -134,7 +134,7 @@ describe('exporter', () => {
134134
}
135135

136136
/**
137-
* @param {import('../src').IPLDResolver} ipld
137+
* @param {import('ipfs-core-types/src/ipld').IPLD} ipld
138138
* @param {'file' | 'directory' | 'raw'} type
139139
* @param {Uint8Array | ArrayLike<number> | undefined} data
140140
* @param {{ node: DAGNode, cid: CID }[]} children
@@ -959,7 +959,7 @@ describe('exporter', () => {
959959
throw new Error('Unexpected type')
960960
}
961961

962-
expect(exported.node).to.deep.equal(node)
962+
return expect(first(exported.content())).to.eventually.deep.equal(node)
963963
})
964964

965965
it('errors when exporting a node with no resolver', async () => {

packages/ipfs-unixfs-exporter/test/helpers/block.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const multicodec = require('multicodec')
88
const mh = require('multihashing-async').multihash
99

1010
/**
11-
* @param {import('../../src/').IPLDResolver} ipld
11+
* @param {import('ipfs-core-types/src/ipld').IPLD} ipld
1212
*/
1313
function createBlockApi (ipld) {
1414
// make ipld behave like the block api, some tests need to pull

packages/ipfs-unixfs-exporter/test/helpers/collect-leaf-cids.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* @param {import('cids')} cid
5-
* @param {import('../../src').IPLDResolver} ipld
5+
* @param {import('ipfs-core-types/src/ipld').IPLD} ipld
66
*/
77
module.exports = function (cid, ipld) {
88
/**

packages/ipfs-unixfs-exporter/test/import-export-dir-sharding.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const uint8ArrayConcat = require('uint8arrays/concat')
2222
*/
2323

2424
describe('builder: directory sharding', () => {
25-
/** @type {import('../src').IPLDResolver} */
25+
/** @type {import('ipfs-core-types/src/ipld').IPLD} */
2626
let ipld
2727
/** @type {import('ipfs-unixfs-importer').BlockAPI} */
2828
let block

packages/ipfs-unixfs-exporter/test/import-export-nested-dir.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const uint8ArrayConcat = require('uint8arrays/concat')
1616

1717
describe('import and export: directory', () => {
1818
const rootHash = 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK'
19-
/** @type {import('../src').IPLDResolver} */
19+
/** @type {import('ipfs-core-types/src/ipld').IPLD} */
2020
let ipld
2121
/** @type {import('ipfs-unixfs-importer').BlockAPI} */
2222
let block

packages/ipfs-unixfs-exporter/test/import-export.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('import and export', function () {
3030
const importerOptions = { strategy: strategy }
3131

3232
describe('using builder: ' + strategy, () => {
33-
/** @type {import('../src').IPLDResolver} */
33+
/** @type {import('ipfs-core-types/src/ipld').IPLD} */
3434
let ipld
3535
/** @type {import('ipfs-unixfs-importer').BlockAPI} */
3636
let block

packages/ipfs-unixfs-exporter/test/importer.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const last = require('it-last')
3030
const CID = require('cids')
3131

3232
/**
33-
* @typedef {import('../src').IPLDResolver} IPLDResolver
33+
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
3434
* @typedef {import('ipfs-unixfs-importer').BlockAPI} BlockAPI
3535
* @typedef {import('ipld-dag-pb').DAGNode} DAGNode
3636
*/
@@ -200,7 +200,7 @@ const strategyOverrides = {
200200

201201
/**
202202
* @param {BlockAPI} block
203-
* @param {IPLDResolver} ipld
203+
* @param {IPLD} ipld
204204
* @param {import('ipfs-unixfs-importer').UserImporterOptions} options
205205
* @param {*} expected
206206
*/
@@ -233,7 +233,7 @@ const checkLeafNodeTypes = async (block, ipld, options, expected) => {
233233

234234
/**
235235
* @param {BlockAPI} block
236-
* @param {IPLDResolver} ipld
236+
* @param {IPLD} ipld
237237
* @param {import('ipfs-unixfs-importer').UserImporterOptions} options
238238
* @param {*} expected
239239
*/
@@ -355,7 +355,7 @@ strategies.forEach((strategy) => {
355355
describe('importer: ' + strategy, function () {
356356
this.timeout(30 * 1000)
357357

358-
/** @type {IPLDResolver} */
358+
/** @type {IPLD} */
359359
let ipld
360360
/** @type {BlockAPI} */
361361
let block
@@ -1076,7 +1076,7 @@ strategies.forEach((strategy) => {
10761076
})
10771077

10781078
describe('configuration', () => {
1079-
/** @type {IPLDResolver} */
1079+
/** @type {IPLD} */
10801080
let ipld
10811081
/** @type {BlockAPI} */
10821082
let block

packages/ipfs-unixfs-exporter/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "aegir/src/config/tsconfig.aegir.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "dist",
5+
"importsNotUsedAsValues": "preserve"
56
},
67
"include": [
78
"src",

packages/ipfs-unixfs-importer/test/benchmark.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const CHUNK_SIZE = 65536
1818
describe.skip('benchmark', function () {
1919
this.timeout(30 * 1000)
2020

21-
/** @type {import('./helpers/block').IPLDResolver} */
21+
/** @type {import('./helpers/block').IPLD} */
2222
let ipld
2323
/** @type {import('../src').BlockAPI} */
2424
let block

packages/ipfs-unixfs-importer/test/builder-only-hash.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const blockApi = require('./helpers/block')
1212
const defaultOptions = require('../src/options')
1313

1414
describe('builder: onlyHash', () => {
15-
/** @type {import('./helpers/block').IPLDResolver} */
15+
/** @type {import('./helpers/block').IPLD} */
1616
let ipld
1717
/** @type {import('../src').BlockAPI} */
1818
let block

packages/ipfs-unixfs-importer/test/builder.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
1515
const defaultOptions = require('../src/options')
1616

1717
describe('builder', () => {
18-
/** @type {import('./helpers/block').IPLDResolver} */
18+
/** @type {import('./helpers/block').IPLD} */
1919
let ipld
2020
/** @type {import('../src').BlockAPI} */
2121
let block

0 commit comments

Comments
 (0)