Skip to content

Commit eaa8449

Browse files
authored
chore: declare interface types in .d.ts file (#122)
If we don't declare ts interfaces in a .d.ts file they get duplicated in our generated .d.ts files. We should pull in types from `ipfs-core-types` for the block api but it's yet to be fully typed so this is draft PR that can be finished after the IPFS top level types have improved. - 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 eaa8449

Some content is hidden

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

68 files changed

+698
-586
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

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "1.0.0",
44
"description": "JS implementation of the IPFS UnixFS",
55
"scripts": {
6-
"postinstall": "rm -rf package-lock.json packages/*/package-lock.json",
76
"reset": "lerna run clean && rimraf packages/*/node_modules node_modules",
87
"test": "lerna run test",
98
"coverage": "lerna run coverage",
@@ -17,7 +16,6 @@
1716
"update-contributors": "aegir release --lint=false --test=false --bump=false --build=false --changelog=false --commit=false --tag=false --push=false --ghrelease=false --docs=false --publish=false"
1817
},
1918
"devDependencies": {
20-
"aegir": "^30.3.0",
2119
"lerna": "^3.22.1"
2220
},
2321
"repository": {
+28-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
'use strict'
22

3+
/** @type {import('aegir').Options["build"]["config"]} */
4+
const buildConfig = {
5+
plugins: [
6+
{
7+
name: 'node built ins',
8+
setup (build) {
9+
build.onResolve({ filter: /^stream$/ }, () => {
10+
return { path: require.resolve('readable-stream') }
11+
})
12+
build.onResolve({ filter: /^crypto$/ }, () => {
13+
return { path: require.resolve('crypto-browserify') }
14+
})
15+
build.onResolve({ filter: /^cborg$/ }, () => {
16+
return { path: require.resolve('cborg') }
17+
})
18+
}
19+
}
20+
]
21+
}
22+
23+
/** @type {import('aegir').PartialOptions} */
324
module.exports = {
4-
karma: {
5-
browserNoActivityTimeout: 1000 * 1000,
25+
build: {
26+
config: buildConfig
627
},
7-
webpack: {
8-
node: {
9-
// needed by the cbor module
10-
stream: true,
11-
12-
// needed by the core-util-is module
13-
Buffer: true
28+
test: {
29+
browser: {
30+
config: {
31+
buildConfig
32+
}
1433
}
1534
}
1635
}

packages/ipfs-unixfs-exporter/.npmignore

-3
This file was deleted.

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

+16-9
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 -i util -i crypto-browserify -i events -i readable-stream"
1718
},
1819
"repository": {
1920
"type": "git",
@@ -35,23 +36,29 @@
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": "^32.0.0",
40+
"copy": "^0.3.2",
41+
"crypto-browserify": "^3.12.0",
3942
"detect-node": "^2.0.4",
40-
"ipfs-core-types": "^0.3.0",
43+
"events": "^3.3.0",
44+
"ipfs-core-types": "^0.3.1",
4145
"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",
46+
"ipld": "^0.29.0",
47+
"ipld-block": "^0.11.1",
48+
"ipld-dag-pb": "^0.22.1",
49+
"ipld-in-memory": "^8.0.0",
4550
"it-all": "^1.0.5",
4651
"it-buffer-stream": "^2.0.0",
4752
"it-first": "^1.0.6",
4853
"merge-options": "^3.0.4",
49-
"multicodec": "^2.1.0",
54+
"multicodec": "^3.0.1",
5055
"native-abort-controller": "^1.0.3",
5156
"nyc": "^15.0.0",
57+
"readable-stream": "^3.6.0",
5258
"rimraf": "^3.0.2",
5359
"sinon": "^9.2.4",
54-
"uint8arrays": "^2.1.2"
60+
"uint8arrays": "^2.1.2",
61+
"util": "^0.12.3"
5562
},
5663
"dependencies": {
5764
"cids": "^1.1.5",

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
}

0 commit comments

Comments
 (0)