Skip to content

Commit ae7f64d

Browse files
committed
chore: declare interface types in .d.ts file
- Moves all types into `.d.ts` files so they do not get duplicated in generated types - Switches to named exports - Loosens mtime definition for unixfs - Exports utility functions for converting mtimes and modes - Runs ts check during linting BREAKING CHANGE: switches to named exports
1 parent 9a2b5f2 commit ae7f64d

Some content is hidden

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

59 files changed

+614
-543
lines changed

packages/ipfs-unixfs-exporter/README.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
- [Raw entries](#raw-entries)
2727
- [CBOR entries](#cbor-entries)
2828
- [`entry.content({ offset, length })`](#entrycontent-offset-length-)
29-
- [`exporter.path(cid, ipld)`](#exporterpathcid-ipld)
30-
- [`exporter.recursive(cid, ipld)`](#exporterrecursivecid-ipld)
29+
- [`walkPath(cid, ipld)`](#walkpathcid-ipld)
30+
- [`recursive(cid, ipld)`](#recursivecid-ipld)
3131
- [Contribute](#contribute)
3232
- [License](#license)
3333

@@ -43,8 +43,8 @@
4343

4444
```js
4545
// import a file and export it again
46-
const importer = require('ipfs-unixfs-importer')
47-
const exporter = require('ipfs-unixfs-exporter')
46+
const { importer } = require('ipfs-unixfs-importer')
47+
const { exporter } = require('ipfs-unixfs-exporter')
4848

4949
const files = []
5050

@@ -80,7 +80,7 @@ console.info(bytes) // 0, 1, 2, 3
8080
#### API
8181

8282
```js
83-
const exporter = require('ipfs-unixfs-exporter')
83+
const { exporter } = require('ipfs-unixfs-exporter')
8484
```
8585

8686
### `exporter(cid, ipld, options)`
@@ -202,28 +202,32 @@ for await (const entry of dir.content({
202202
// `entries` contains the first 5 files/directories in the directory
203203
```
204204
205-
### `exporter.path(cid, ipld)`
205+
### `walkPath(cid, ipld)`
206206
207-
`exporter.path` will return an async iterator that yields entries for all segments in a path:
207+
`walkPath` will return an async iterator that yields entries for all segments in a path:
208208
209209
```javascript
210+
const { walkPath } = require('ipfs-unixfs-exporter')
211+
210212
const entries = []
211213

212-
for await (const entry of exporter.path('Qmfoo/foo/bar/baz.txt', ipld)) {
214+
for await (const entry of walkPath('Qmfoo/foo/bar/baz.txt', ipld)) {
213215
entries.push(entry)
214216
}
215217

216218
// entries contains 4x `entry` objects
217219
```
218220
219-
### `exporter.recursive(cid, ipld)`
221+
### `recursive(cid, ipld)`
220222
221-
`exporter.recursive` will return an async iterator that yields all entries beneath a given CID or IPFS path, as well as the containing directory.
223+
`recursive` will return an async iterator that yields all entries beneath a given CID or IPFS path, as well as the containing directory.
222224
223225
```javascript
226+
const { recursive } = require('ipfs-unixfs-exporter')
227+
224228
const entries = []
225229

226-
for await (const child of exporter.recursive('Qmfoo/foo/bar', ipld)) {
230+
for await (const child of recursive('Qmfoo/foo/bar', ipld)) {
227231
entries.push(entry)
228232
}
229233

packages/ipfs-unixfs-exporter/package.json

+11-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
"fs": false
99
},
1010
"scripts": {
11-
"prepare": "aegir build --no-bundle",
1211
"test": "aegir test",
12+
"build": "aegir build",
1313
"clean": "rimraf ./dist",
14-
"lint": "aegir lint",
14+
"lint": "aegir ts --check && 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 -i ipfs-core-types"
16+
"depcheck": "aegir dep-check -i @types/mocha -i @types/sinon -i nyc -i abort-controller -i rimraf -i ipfs-core-types -i copy"
1717
},
1818
"repository": {
1919
"type": "git",
@@ -35,18 +35,20 @@
3535
"@types/mocha": "^8.2.1",
3636
"@types/sinon": "^9.0.10",
3737
"abort-controller": "^3.0.0",
38-
"aegir": "^30.3.0",
38+
"aegir": "^31.0.4",
39+
"copy": "^0.3.2",
3940
"detect-node": "^2.0.4",
40-
"ipfs-core-types": "^0.3.0",
41+
"ipfs-core-types": "^0.3.1",
4142
"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",
43+
"ipld": "^0.29.0",
44+
"ipld-block": "^0.11.1",
45+
"ipld-dag-pb": "^0.22.1",
46+
"ipld-in-memory": "^8.0.0",
4547
"it-all": "^1.0.5",
4648
"it-buffer-stream": "^2.0.0",
4749
"it-first": "^1.0.6",
4850
"merge-options": "^3.0.4",
49-
"multicodec": "^2.1.0",
51+
"multicodec": "^3.0.1",
5052
"native-abort-controller": "^1.0.3",
5153
"nyc": "^15.0.0",
5254
"rimraf": "^3.0.2",

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

+13-66
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,12 @@ const resolve = require('./resolvers')
66
const last = require('it-last')
77

88
/**
9-
* @typedef {import('ipfs-unixfs')} UnixFS
9+
* @typedef {import('ipfs-unixfs').UnixFS} UnixFS
1010
* @typedef {import('ipld-dag-pb').DAGNode} DAGNode
11-
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
12-
*
13-
* @typedef {object} UnixFSFile
14-
* @property {'file'} type
15-
* @property {string} name
16-
* @property {string} path
17-
* @property {CID} cid
18-
* @property {number} depth
19-
* @property {UnixFS} unixfs
20-
* @property {DAGNode} node
21-
* @property {(options?: ExporterOptions) => AsyncIterable<Uint8Array>} content
22-
*
23-
* @typedef {object} UnixFSDirectory
24-
* @property {'directory'} type
25-
* @property {string} name
26-
* @property {string} path
27-
* @property {CID} cid
28-
* @property {number} depth
29-
* @property {UnixFS} unixfs
30-
* @property {DAGNode} node
31-
* @property {(options?: ExporterOptions) => AsyncIterable<UnixFSEntry>} content
32-
*
33-
* @typedef {object} ObjectNode
34-
* @property {'object'} type
35-
* @property {string} name
36-
* @property {string} path
37-
* @property {CID} cid
38-
* @property {number} depth
39-
* @property {Uint8Array} node
40-
* @property {(options?: ExporterOptions) => AsyncIterable<any>} content
41-
*
42-
* @typedef {object} RawNode
43-
* @property {'raw'} type
44-
* @property {string} name
45-
* @property {string} path
46-
* @property {CID} cid
47-
* @property {number} depth
48-
* @property {Uint8Array} node
49-
* @property {(options?: ExporterOptions) => AsyncIterable<Uint8Array>} content
50-
*
51-
* @typedef {object} IdentityNode
52-
* @property {'identity'} type
53-
* @property {string} name
54-
* @property {string} path
55-
* @property {CID} cid
56-
* @property {number} depth
57-
* @property {Uint8Array} node
58-
* @property {(options?: ExporterOptions) => AsyncIterable<Uint8Array>} content
59-
*
60-
* @typedef {UnixFSFile | UnixFSDirectory | ObjectNode | RawNode | IdentityNode} UnixFSEntry
61-
*/
62-
63-
/**
64-
* @typedef {object} ExporterOptions
65-
* @property {number} [offset=0]
66-
* @property {number} [length]
67-
* @property {AbortSignal} [signal]
68-
* @property {number} [timeout]
11+
* @typedef {import('ipld')} IPLD
12+
* @typedef {import('./types').ExporterOptions} ExporterOptions
13+
* @typedef {import('./types').UnixFSDirectory} UnixFSDirectory
14+
* @typedef {import('./types').UnixFSEntry} UnixFSEntry
6915
*/
7016

7117
const toPathComponents = (path = '') => {
@@ -115,7 +61,7 @@ const cidAndRest = (path) => {
11561
* @param {IPLD} ipld
11662
* @param {ExporterOptions} [options]
11763
*/
118-
const walkPath = async function * (path, ipld, options = {}) {
64+
async function * walkPath (path, ipld, options = {}) {
11965
let {
12066
cid,
12167
toResolve
@@ -152,7 +98,7 @@ const walkPath = async function * (path, ipld, options = {}) {
15298
* @param {IPLD} ipld
15399
* @param {ExporterOptions} [options]
154100
*/
155-
const exporter = async (path, ipld, options = {}) => {
101+
async function exporter (path, ipld, options = {}) {
156102
const result = await last(walkPath(path, ipld, options))
157103

158104
if (!result) {
@@ -167,7 +113,7 @@ const exporter = async (path, ipld, options = {}) => {
167113
* @param {IPLD} ipld
168114
* @param {ExporterOptions} [options]
169115
*/
170-
const recursive = async function * (path, ipld, options = {}) {
116+
async function * recursive (path, ipld, options = {}) {
171117
const node = await exporter(path, ipld, options)
172118

173119
if (!node) {
@@ -202,7 +148,8 @@ const recursive = async function * (path, ipld, options = {}) {
202148
}
203149
}
204150

205-
exporter.path = walkPath
206-
exporter.recursive = recursive
207-
208-
module.exports = exporter
151+
module.exports = {
152+
exporter,
153+
walkPath,
154+
recursive
155+
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ const CID = require('cids')
44
const errCode = require('err-code')
55

66
/**
7-
* @type {import('./').Resolver}
7+
* @typedef {import('../types').Resolver} Resolver
8+
*/
9+
10+
/**
11+
* @type {Resolver}
812
*/
913
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
1014
const object = await ipld.get(cid, options)
@@ -29,6 +33,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
2933
cid,
3034
node: block,
3135
depth,
36+
size: block.data.length,
3237
content: async function * () {
3338
yield object
3439
}
@@ -57,6 +62,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
5762
cid,
5863
node: block,
5964
depth,
65+
size: block.data.length,
6066
content: async function * () {
6167
yield object
6268
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const validateOffsetAndLength = require('../utils/validate-offset-and-length')
66
const mh = require('multihashing-async').multihash
77

88
/**
9-
* @typedef {import('../').ExporterOptions} ExporterOptions
9+
* @typedef {import('../types').ExporterOptions} ExporterOptions
10+
* @typedef {import('../types').Resolver} Resolver
1011
*/
1112

1213
/**
@@ -29,7 +30,7 @@ const rawContent = (node) => {
2930
}
3031

3132
/**
32-
* @type {import('./').Resolver}
33+
* @type {Resolver}
3334
*/
3435
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
3536
if (toResolve.length) {
@@ -46,6 +47,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
4647
cid,
4748
content: rawContent(buf.digest),
4849
depth,
50+
size: buf.length,
4951
node: buf.digest
5052
}
5153
}

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

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

55
/**
6-
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
7-
* @typedef {import('../').ExporterOptions} ExporterOptions
8-
* @typedef {import('../').UnixFSEntry} UnixFSEntry
96
* @typedef {import('cids')} CID
7+
* @typedef {import('ipld')} IPLD
8+
* @typedef {import('../types').ExporterOptions} ExporterOptions
9+
* @typedef {import('../types').UnixFSEntry} UnixFSEntry
10+
* @typedef {import('../types').Resolver} Resolver
11+
* @typedef {import('../types').Resolve} Resolve
1012
*/
1113

1214
/**
13-
* @typedef {object} NextResult
14-
* @property {CID} cid
15-
* @property {string} name
16-
* @property {string} path
17-
* @property {string[]} toResolve
18-
*
19-
* @typedef {object} ResolveResult
20-
* @property {UnixFSEntry} entry
21-
* @property {NextResult} [next]
22-
*/
23-
24-
/**
25-
*
26-
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], depth: number, ipld: IPLD, options: ExporterOptions) => Promise<ResolveResult>} Resolve
27-
*
28-
* @typedef {(cid: CID, name: string, path: string, toResolve: string[], resolve: Resolve, depth: number, ipld: IPLD, options: ExporterOptions) => Promise<ResolveResult>} Resolver
29-
*
3015
* @type {{ [ key: string ]: Resolver }}
3116
*/
3217
const resolvers = {

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const extractDataFromBlock = require('../utils/extract-data-from-block')
55
const validateOffsetAndLength = require('../utils/validate-offset-and-length')
66

77
/**
8-
* @typedef {import('../').ExporterOptions} ExporterOptions
8+
* @typedef {import('../types').ExporterOptions} ExporterOptions
99
*/
1010

1111
/**
@@ -28,7 +28,7 @@ const rawContent = (node) => {
2828
}
2929

3030
/**
31-
* @type {import('./').Resolver}
31+
* @type {import('../types').Resolver}
3232
*/
3333
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
3434
if (toResolve.length) {
@@ -45,6 +45,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
4545
cid,
4646
content: rawContent(buf),
4747
depth,
48+
size: buf.length,
4849
node: buf
4950
}
5051
}

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict'
22

33
/**
4-
* @typedef {import('../../../').ExporterOptions} ExporterOptions
5-
* @typedef {import('../').UnixfsV1DirectoryContent} UnixfsV1DirectoryContent
6-
*
7-
* @type {import('../').UnixfsV1Resolver}
4+
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
5+
* @typedef {import('../../../types').UnixfsV1DirectoryContent} UnixfsV1DirectoryContent
6+
* @typedef {import('../../../types').UnixfsV1Resolver} UnixfsV1Resolver
7+
*/
8+
9+
/**
10+
* @type {UnixfsV1Resolver}
811
*/
912
const directoryContent = (cid, node, unixfs, path, resolve, depth, ipld) => {
1013
/**

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
const extractDataFromBlock = require('../../../utils/extract-data-from-block')
44
const validateOffsetAndLength = require('../../../utils/validate-offset-and-length')
5-
const UnixFS = require('ipfs-unixfs')
5+
const { UnixFS } = require('ipfs-unixfs')
66
const errCode = require('err-code')
77

88
/**
9-
* @typedef {import('../../../').ExporterOptions} ExporterOptions
10-
* @typedef {import('ipfs-core-types/src/ipld').IPLD} IPLD
9+
* @typedef {import('../../../types').ExporterOptions} ExporterOptions
10+
* @typedef {import('ipld')} IPLD
1111
* @typedef {import('ipld-dag-pb').DAGNode} DAGNode
12-
*
12+
*/
13+
14+
/**
1315
* @param {IPLD} ipld
1416
* @param {DAGNode} node
1517
* @param {number} start
@@ -62,8 +64,7 @@ async function * emitBytes (ipld, node, start, end, streamPosition = 0, options)
6264
(end > childStart && end <= childEnd) || // child has end byte
6365
(start < childStart && end > childEnd)) { // child is between offset and end bytes
6466
const child = await ipld.get(childLink.Hash, {
65-
signal: options.signal,
66-
timeout: options.timeout
67+
signal: options.signal
6768
})
6869

6970
for await (const buf of emitBytes(ipld, child, start, end, streamPosition, options)) {

0 commit comments

Comments
 (0)