Skip to content

Commit b8a4512

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 b8a4512

Some content is hidden

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

60 files changed

+619
-544
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ jobs:
4646
- stage: check
4747
name: linting
4848
script:
49-
- npm --version
5049
- npm run lint
5150

5251
- stage: check

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-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
"scripts": {
1111
"prepare": "aegir build --no-bundle",
1212
"test": "aegir test",
13+
"build": "aegir build",
1314
"clean": "rimraf ./dist",
14-
"lint": "aegir lint",
15+
"lint": "aegir ts --check && aegir lint",
1516
"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"
17+
"depcheck": "aegir dep-check -i @types/mocha -i @types/sinon -i nyc -i abort-controller -i rimraf -i ipfs-core-types -i copy"
1718
},
1819
"repository": {
1920
"type": "git",
@@ -35,18 +36,20 @@
3536
"@types/mocha": "^8.2.1",
3637
"@types/sinon": "^9.0.10",
3738
"abort-controller": "^3.0.0",
38-
"aegir": "^30.3.0",
39+
"aegir": "^31.0.4",
40+
"copy": "^0.3.2",
3941
"detect-node": "^2.0.4",
40-
"ipfs-core-types": "^0.3.0",
42+
"ipfs-core-types": "^0.3.1",
4143
"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",
44+
"ipld": "^0.29.0",
45+
"ipld-block": "^0.11.1",
46+
"ipld-dag-pb": "^0.22.1",
47+
"ipld-in-memory": "^8.0.0",
4548
"it-all": "^1.0.5",
4649
"it-buffer-stream": "^2.0.0",
4750
"it-first": "^1.0.6",
4851
"merge-options": "^3.0.4",
49-
"multicodec": "^2.1.0",
52+
"multicodec": "^3.0.1",
5053
"native-abort-controller": "^1.0.3",
5154
"nyc": "^15.0.0",
5255
"rimraf": "^3.0.2",

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

+17-66
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,16 @@ 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').UnixFSFile} UnixFSFile
14+
* @typedef {import('./types').UnixFSDirectory} UnixFSDirectory
15+
* @typedef {import('./types').ObjectNode} ObjectNode
16+
* @typedef {import('./types').RawNode} RawNode
17+
* @typedef {import('./types').IdentityNode} IdentityNode
18+
* @typedef {import('./types').UnixFSEntry} UnixFSEntry
6919
*/
7020

7121
const toPathComponents = (path = '') => {
@@ -115,7 +65,7 @@ const cidAndRest = (path) => {
11565
* @param {IPLD} ipld
11666
* @param {ExporterOptions} [options]
11767
*/
118-
const walkPath = async function * (path, ipld, options = {}) {
68+
async function * walkPath (path, ipld, options = {}) {
11969
let {
12070
cid,
12171
toResolve
@@ -152,7 +102,7 @@ const walkPath = async function * (path, ipld, options = {}) {
152102
* @param {IPLD} ipld
153103
* @param {ExporterOptions} [options]
154104
*/
155-
const exporter = async (path, ipld, options = {}) => {
105+
async function exporter (path, ipld, options = {}) {
156106
const result = await last(walkPath(path, ipld, options))
157107

158108
if (!result) {
@@ -167,7 +117,7 @@ const exporter = async (path, ipld, options = {}) => {
167117
* @param {IPLD} ipld
168118
* @param {ExporterOptions} [options]
169119
*/
170-
const recursive = async function * (path, ipld, options = {}) {
120+
async function * recursive (path, ipld, options = {}) {
171121
const node = await exporter(path, ipld, options)
172122

173123
if (!node) {
@@ -202,7 +152,8 @@ const recursive = async function * (path, ipld, options = {}) {
202152
}
203153
}
204154

205-
exporter.path = walkPath
206-
exporter.recursive = recursive
207-
208-
module.exports = exporter
155+
module.exports = {
156+
exporter,
157+
walkPath,
158+
recursive
159+
}

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.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.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
/**

0 commit comments

Comments
 (0)