Skip to content

Commit aaa3d04

Browse files
authored
chore: upgrade deps with new typedefs (#3550)
Upgrades to new version with types - Uses default aegir ts config - Fixes all ts errors - Fully types core-api in ipfs-core-types package - Makes ipfs-core implement types from ipfs-core-types package - Removes duplicate types, ipfs-core-types as single source of type truth - Reduces use of external APIs by internal components in ipfs-core - Switches to named exports BREAKING CHANGE: all core api methods now have types, some method signatures have changed, named exports are now used by the http, grpc and ipfs client modules
1 parent 0a6b013 commit aaa3d04

File tree

156 files changed

+1521
-709
lines changed

Some content is hidden

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

156 files changed

+1521
-709
lines changed

.aegir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const getPort = require('aegir/utils/get-port')
66
/** @type {import('aegir').PartialOptions} */
77
module.exports = {
88
build: {
9-
bundlesizeMax: '110kB'
9+
bundlesizeMax: '106kB'
1010
},
1111
test: {
1212
async before (options) {

README.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
- [Install](#install)
4040
- [Next Steps](#next-steps)
4141
- [Usage](#usage)
42-
- [`ipfsHttpClient([options])`](#ipfshttpclientoptions)
42+
- [`create([options])`](#createoptions)
4343
- [Parameters](#parameters)
4444
- [Options](#options)
4545
- [Returns](#returns)
@@ -92,7 +92,7 @@ Both the Current and Active LTS versions of Node.js are supported. Please see [n
9292

9393
## Usage
9494

95-
#### `ipfsHttpClient([options])`
95+
#### `create([options])`
9696

9797
> create an instance of the HTTP API client
9898
@@ -124,16 +124,16 @@ Alternatively it can be an object which may have the following keys:
124124
#### Example
125125

126126
```JavaScript
127-
const createClient = require('ipfs-http-client')
127+
const { create } = require('ipfs-http-client')
128128

129129
// connect to the default API address http://localhost:5001
130-
const client = createClient()
130+
const client = create()
131131

132132
// connect to a different API
133-
const client = createClient('http://127.0.0.1:5002')
133+
const client = create('http://127.0.0.1:5002')
134134

135135
// connect using a URL
136-
const client = createClient(new URL('http://127.0.0.1:5002'))
136+
const client = create(new URL('http://127.0.0.1:5002'))
137137

138138
// call Core API methods
139139
const { cid } = await client.add('Hello world!')
@@ -195,9 +195,8 @@ Returns an async iterable that yields `{ path, content }` objects suitable for p
195195
##### Example
196196

197197
```js
198-
const IpfsHttpClient = require('ipfs-http-client')
199-
const { globSource } = IpfsHttpClient
200-
const ipfs = IpfsHttpClient()
198+
const { create, globSource } = require('ipfs-http-client')
199+
const ipfs = create()
201200

202201
const file = await ipfs.add(globSource('./docs', { recursive: true }))
203202
console.log(file)
@@ -230,9 +229,8 @@ Returns an async iterable that yields `{ path, content }` objects suitable for p
230229
##### Example
231230

232231
```js
233-
const IpfsHttpClient = require('ipfs-http-client')
234-
const { urlSource } = IpfsHttpClient
235-
const ipfs = IpfsHttpClient()
232+
const { create, urlSource } = require('ipfs-http-client')
233+
const ipfs = create()
236234

237235
const file = await ipfs.add(urlSource('https://ipfs.io/images/ipfs-logo.svg'))
238236
console.log(file)
@@ -265,19 +263,19 @@ To interact with the API, you need to have a local daemon running. It needs to b
265263
### Importing the module and usage
266264

267265
```javascript
268-
const ipfsClient = require('ipfs-http-client')
266+
const { create } = require('ipfs-http-client')
269267

270268
// connect to ipfs daemon API server
271-
const ipfs = ipfsClient('http://localhost:5001') // (the default in Node.js)
269+
const ipfs = create('http://localhost:5001') // (the default in Node.js)
272270

273271
// or connect with multiaddr
274-
const ipfs = ipfsClient('/ip4/127.0.0.1/tcp/5001')
272+
const ipfs = create('/ip4/127.0.0.1/tcp/5001')
275273

276274
// or using options
277-
const ipfs = ipfsClient({ host: 'localhost', port: '5001', protocol: 'http' })
275+
const ipfs = create({ host: 'localhost', port: '5001', protocol: 'http' })
278276

279277
// or specifying a specific API path
280-
const ipfs = ipfsClient({ host: '1.1.1.1', port: '80', apiPath: '/ipfs/api/v0' })
278+
const ipfs = create({ host: '1.1.1.1', port: '80', apiPath: '/ipfs/api/v0' })
281279
```
282280

283281
### In a web browser
@@ -334,7 +332,7 @@ const ipfs = window.IpfsHttpClient()
334332
If you wish to send custom headers with each request made by this library, for example, the Authorization header. You can use the config to do so:
335333

336334
```js
337-
const ipfs = ipfsClient({
335+
const ipfs = create({
338336
host: 'localhost',
339337
port: 5001,
340338
protocol: 'http',
@@ -350,9 +348,9 @@ To set a global timeout for _all_ requests pass a value for the `timeout` option
350348

351349
```js
352350
// Timeout after 10 seconds
353-
const ipfs = ipfsClient({ timeout: 10000 })
351+
const ipfs = create({ timeout: 10000 })
354352
// Timeout after 2 minutes
355-
const ipfs = ipfsClient({ timeout: '2m' })
353+
const ipfs = create({ timeout: '2m' })
356354
// see https://www.npmjs.com/package/parse-duration for valid string values
357355
```
358356

package.json

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
"leadMaintainer": "Alex Potsides <[email protected]>",
1212
"files": [
1313
"src",
14-
"dist"
14+
"dist",
15+
"!dist/*.tsbuildinfo"
1516
],
1617
"main": "src/index.js",
18+
"types": "./dist/src/index.d.ts",
1719
"browser": {
1820
"./src/lib/multipart-request.js": "./src/lib/multipart-request.browser.js",
1921
"ipfs-utils/src/files/glob-source": false,
@@ -22,20 +24,11 @@
2224
"http": false,
2325
"https": false
2426
},
25-
"typesVersions": {
26-
"*": {
27-
"*": [
28-
"dist/*",
29-
"dist/*/index"
30-
]
31-
}
32-
},
3327
"repository": {
3428
"type": "git",
3529
"url": "git+https://github.com/ipfs/js-ipfs.git"
3630
},
3731
"scripts": {
38-
"prepare": "npm run build",
3932
"build": "aegir build",
4033
"test": "aegir test",
4134
"test:node": "aegir test -t node",
@@ -53,17 +46,17 @@
5346
"dependencies": {
5447
"abort-controller": "^3.0.0",
5548
"any-signal": "^2.1.2",
56-
"bignumber.js": "^9.0.1",
5749
"cids": "^1.1.5",
5850
"debug": "^4.1.1",
59-
"form-data": "^3.0.0",
51+
"form-data": "^4.0.0",
6052
"ipfs-core-types": "^0.3.1",
6153
"ipfs-core-utils": "^0.7.2",
62-
"ipfs-utils": "^6.0.1",
54+
"ipfs-unixfs": "^4.0.1",
55+
"ipfs-utils": "^6.0.4",
6356
"ipld-block": "^0.11.0",
64-
"ipld-dag-cbor": "^0.17.0",
65-
"ipld-dag-pb": "^0.20.0",
66-
"ipld-raw": "^6.0.0",
57+
"ipld-dag-cbor": "^0.18.0",
58+
"ipld-dag-pb": "^0.22.0",
59+
"ipld-raw": "^7.0.0",
6760
"it-last": "^1.0.4",
6861
"it-map": "^1.0.4",
6962
"it-tar": "^1.2.2",
@@ -80,13 +73,12 @@
8073
"uint8arrays": "^2.1.3"
8174
},
8275
"devDependencies": {
83-
"aegir": "^31.0.0",
84-
"delay": "^4.4.0",
76+
"aegir": "^32.1.0",
77+
"delay": "^5.0.0",
8578
"go-ipfs": "0.8.0",
86-
"ipfs-core": "^0.5.4",
87-
"ipfsd-ctl": "^7.2.0",
79+
"ipfsd-ctl": "^8.0.0",
8880
"it-all": "^1.0.4",
89-
"it-concat": "^1.0.1",
81+
"it-concat": "^1.0.3",
9082
"it-first": "^1.0.4",
9183
"nock": "^13.0.2",
9284
"rimraf": "^3.0.2"

src/add-all.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ const { AbortController } = require('native-abort-controller')
1111
/**
1212
* @typedef {import('ipfs-utils/src/types').ProgressFn} IPFSUtilsHttpUploadProgressFn
1313
* @typedef {import('ipfs-core-types/src/root').AddProgressFn} IPFSCoreAddProgressFn
14+
* @typedef {import('./types').HTTPClientExtraOptions} HTTPClientExtraOptions
15+
* @typedef {import('ipfs-core-types/src/root').API<HTTPClientExtraOptions>} RootAPI
16+
* @typedef {import('ipfs-core-types/src/root').AddResult} AddResult
1417
*/
1518

1619
module.exports = configure((api) => {
1720
/**
18-
* @type {import('.').Implements<typeof import('ipfs-core/src/components/add-all/index')>}
21+
* @type {RootAPI["addAll"]}
1922
*/
2023
async function * addAll (source, options = {}) {
2124
// allow aborting requests on body errors
@@ -30,6 +33,7 @@ module.exports = configure((api) => {
3033
// `{ total, loaded}` passed to `onUploadProgress` and `multipart.total`
3134
// in which case we disable progress updates to be written out.
3235
const [progressFn, onUploadProgress] = typeof options.progress === 'function'
36+
// @ts-ignore tsc picks up the node codepath
3337
? createProgressHandler(total, parts, options.progress)
3438
: [undefined, undefined]
3539

@@ -104,9 +108,9 @@ const createOnUploadProgress = (size, parts, progress) => {
104108

105109
/**
106110
* @param {any} input
107-
* @returns {import('ipfs-core-types/src/files').UnixFSEntry}
108111
*/
109112
function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
113+
/** @type {AddResult} */
110114
const output = {
111115
path: name,
112116
cid: new CID(hash),
@@ -124,6 +128,5 @@ function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
124128
}
125129
}
126130

127-
// @ts-ignore
128131
return output
129132
}

src/add.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ const last = require('it-last')
55
const configure = require('./lib/configure')
66

77
/**
8-
* @param {import("./lib/core").ClientOptions} options
8+
* @typedef {import('./types').HTTPClientExtraOptions} HTTPClientExtraOptions
9+
* @typedef {import('ipfs-core-types/src/root').API<HTTPClientExtraOptions>} RootAPI
10+
*/
11+
12+
/**
13+
* @param {import('./types').Options} options
914
*/
1015
module.exports = (options) => {
1116
const all = addAll(options)
1217
return configure(() => {
1318
/**
14-
* @type {import('.').Implements<typeof import('ipfs-core/src/components/add')>}
19+
* @type {RootAPI["add"]}
1520
*/
1621
async function add (input, options = {}) {
17-
// @ts-ignore - last may return undefind if source is empty
22+
// @ts-ignore - last may return undefined if source is empty
1823
return await last(all(input, options))
1924
}
2025
return add

src/bitswap/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict'
22

3+
/**
4+
* @param {import('../types').Options} config
5+
*/
36
module.exports = config => ({
47
wantlist: require('./wantlist')(config),
58
wantlistForPeer: require('./wantlist-for-peer')(config),

src/bitswap/stat.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
'use strict'
22

3-
const { BigNumber } = require('bignumber.js')
43
const CID = require('cids')
54
const configure = require('../lib/configure')
65
const toUrlSearchParams = require('../lib/to-url-search-params')
76

7+
/**
8+
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
9+
* @typedef {import('ipfs-core-types/src/bitswap').API<HTTPClientExtraOptions>} BitswapAPI
10+
*/
11+
812
module.exports = configure(api => {
913
/**
10-
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/stat')>}
14+
* @type {BitswapAPI["stat"]}
1115
*/
1216
async function stat (options = {}) {
1317
const res = await api.post('bitswap/stat', {
@@ -22,16 +26,19 @@ module.exports = configure(api => {
2226
return stat
2327
})
2428

29+
/**
30+
* @param {any} res
31+
*/
2532
function toCoreInterface (res) {
2633
return {
2734
provideBufLen: res.ProvideBufLen,
28-
wantlist: (res.Wantlist || []).map(k => new CID(k['/'])),
35+
wantlist: (res.Wantlist || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/'])),
2936
peers: (res.Peers || []),
30-
blocksReceived: new BigNumber(res.BlocksReceived),
31-
dataReceived: new BigNumber(res.DataReceived),
32-
blocksSent: new BigNumber(res.BlocksSent),
33-
dataSent: new BigNumber(res.DataSent),
34-
dupBlksReceived: new BigNumber(res.DupBlksReceived),
35-
dupDataReceived: new BigNumber(res.DupDataReceived)
37+
blocksReceived: BigInt(res.BlocksReceived),
38+
dataReceived: BigInt(res.DataReceived),
39+
blocksSent: BigInt(res.BlocksSent),
40+
dataSent: BigInt(res.DataSent),
41+
dupBlksReceived: BigInt(res.DupBlksReceived),
42+
dupDataReceived: BigInt(res.DupDataReceived)
3643
}
3744
}

src/bitswap/unwant.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ const CID = require('cids')
44
const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

7+
/**
8+
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
9+
* @typedef {import('ipfs-core-types/src/bitswap').API<HTTPClientExtraOptions>} BitswapAPI
10+
*/
11+
712
module.exports = configure(api => {
813
/**
9-
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/unwant')>}
14+
* @type {BitswapAPI["unwant"]}
1015
*/
1116
async function unwant (cid, options = {}) {
1217
const res = await api.post('bitswap/unwant', {

src/bitswap/wantlist-for-peer.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ const CID = require('cids')
44
const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

7+
/**
8+
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
9+
* @typedef {import('ipfs-core-types/src/bitswap').API<HTTPClientExtraOptions>} BitswapAPI
10+
*/
11+
712
module.exports = configure(api => {
813
/**
9-
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/wantlist-for-peer')>}
14+
* @type {BitswapAPI["wantlistForPeer"]}
1015
*/
1116
async function wantlistForPeer (peerId, options = {}) {
1217
// @ts-ignore - CID|string seems to confuse typedef
@@ -22,7 +27,7 @@ module.exports = configure(api => {
2227
headers: options.headers
2328
})).json()
2429

25-
return (res.Keys || []).map(k => new CID(k['/']))
30+
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/']))
2631
}
2732
return wantlistForPeer
2833
})

src/bitswap/wantlist.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ const CID = require('cids')
44
const configure = require('../lib/configure')
55
const toUrlSearchParams = require('../lib/to-url-search-params')
66

7+
/**
8+
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
9+
* @typedef {import('ipfs-core-types/src/bitswap').API<HTTPClientExtraOptions>} BitswapAPI
10+
*/
11+
712
module.exports = configure(api => {
813
/**
9-
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/wantlist')>}
14+
* @type {BitswapAPI["wantlist"]}
1015
*/
1116
async function wantlist (options = {}) {
1217
const res = await (await api.post('bitswap/wantlist', {
@@ -16,7 +21,7 @@ module.exports = configure(api => {
1621
headers: options.headers
1722
})).json()
1823

19-
return (res.Keys || []).map(k => new CID(k['/']))
24+
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/']))
2025
}
2126
return wantlist
2227
})

src/block/get.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ const CID = require('cids')
55
const configure = require('../lib/configure')
66
const toUrlSearchParams = require('../lib/to-url-search-params')
77

8+
/**
9+
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
10+
* @typedef {import('ipfs-core-types/src/block').API<HTTPClientExtraOptions>} BlockAPI
11+
*/
12+
813
module.exports = configure(api => {
914
/**
10-
* @type {import('..').Implements<typeof import('ipfs-core/src/components/block/get')>}
15+
* @type {BlockAPI["get"]}
1116
*/
1217
async function get (cid, options = {}) {
1318
// @ts-ignore - CID|string seems to confuse typedef

0 commit comments

Comments
 (0)