Skip to content

Commit

Permalink
chore: upgrade deps with new typedefs (#3550)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain authored Mar 31, 2021
1 parent 0a6b013 commit aaa3d04
Show file tree
Hide file tree
Showing 156 changed files with 1,521 additions and 709 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getPort = require('aegir/utils/get-port')
/** @type {import('aegir').PartialOptions} */
module.exports = {
build: {
bundlesizeMax: '110kB'
bundlesizeMax: '106kB'
},
test: {
async before (options) {
Expand Down
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
- [Install](#install)
- [Next Steps](#next-steps)
- [Usage](#usage)
- [`ipfsHttpClient([options])`](#ipfshttpclientoptions)
- [`create([options])`](#createoptions)
- [Parameters](#parameters)
- [Options](#options)
- [Returns](#returns)
Expand Down Expand Up @@ -92,7 +92,7 @@ Both the Current and Active LTS versions of Node.js are supported. Please see [n

## Usage

#### `ipfsHttpClient([options])`
#### `create([options])`

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

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

// connect to the default API address http://localhost:5001
const client = createClient()
const client = create()

// connect to a different API
const client = createClient('http://127.0.0.1:5002')
const client = create('http://127.0.0.1:5002')

// connect using a URL
const client = createClient(new URL('http://127.0.0.1:5002'))
const client = create(new URL('http://127.0.0.1:5002'))

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

```js
const IpfsHttpClient = require('ipfs-http-client')
const { globSource } = IpfsHttpClient
const ipfs = IpfsHttpClient()
const { create, globSource } = require('ipfs-http-client')
const ipfs = create()

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

```js
const IpfsHttpClient = require('ipfs-http-client')
const { urlSource } = IpfsHttpClient
const ipfs = IpfsHttpClient()
const { create, urlSource } = require('ipfs-http-client')
const ipfs = create()

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

```javascript
const ipfsClient = require('ipfs-http-client')
const { create } = require('ipfs-http-client')

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

// or connect with multiaddr
const ipfs = ipfsClient('/ip4/127.0.0.1/tcp/5001')
const ipfs = create('/ip4/127.0.0.1/tcp/5001')

// or using options
const ipfs = ipfsClient({ host: 'localhost', port: '5001', protocol: 'http' })
const ipfs = create({ host: 'localhost', port: '5001', protocol: 'http' })

// or specifying a specific API path
const ipfs = ipfsClient({ host: '1.1.1.1', port: '80', apiPath: '/ipfs/api/v0' })
const ipfs = create({ host: '1.1.1.1', port: '80', apiPath: '/ipfs/api/v0' })
```

### In a web browser
Expand Down Expand Up @@ -334,7 +332,7 @@ const ipfs = window.IpfsHttpClient()
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:

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

```js
// Timeout after 10 seconds
const ipfs = ipfsClient({ timeout: 10000 })
const ipfs = create({ timeout: 10000 })
// Timeout after 2 minutes
const ipfs = ipfsClient({ timeout: '2m' })
const ipfs = create({ timeout: '2m' })
// see https://www.npmjs.com/package/parse-duration for valid string values
```

Expand Down
34 changes: 13 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
"leadMaintainer": "Alex Potsides <alex@achingbrain.net>",
"files": [
"src",
"dist"
"dist",
"!dist/*.tsbuildinfo"
],
"main": "src/index.js",
"types": "./dist/src/index.d.ts",
"browser": {
"./src/lib/multipart-request.js": "./src/lib/multipart-request.browser.js",
"ipfs-utils/src/files/glob-source": false,
Expand All @@ -22,20 +24,11 @@
"http": false,
"https": false
},
"typesVersions": {
"*": {
"*": [
"dist/*",
"dist/*/index"
]
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/js-ipfs.git"
},
"scripts": {
"prepare": "npm run build",
"build": "aegir build",
"test": "aegir test",
"test:node": "aegir test -t node",
Expand All @@ -53,17 +46,17 @@
"dependencies": {
"abort-controller": "^3.0.0",
"any-signal": "^2.1.2",
"bignumber.js": "^9.0.1",
"cids": "^1.1.5",
"debug": "^4.1.1",
"form-data": "^3.0.0",
"form-data": "^4.0.0",
"ipfs-core-types": "^0.3.1",
"ipfs-core-utils": "^0.7.2",
"ipfs-utils": "^6.0.1",
"ipfs-unixfs": "^4.0.1",
"ipfs-utils": "^6.0.4",
"ipld-block": "^0.11.0",
"ipld-dag-cbor": "^0.17.0",
"ipld-dag-pb": "^0.20.0",
"ipld-raw": "^6.0.0",
"ipld-dag-cbor": "^0.18.0",
"ipld-dag-pb": "^0.22.0",
"ipld-raw": "^7.0.0",
"it-last": "^1.0.4",
"it-map": "^1.0.4",
"it-tar": "^1.2.2",
Expand All @@ -80,13 +73,12 @@
"uint8arrays": "^2.1.3"
},
"devDependencies": {
"aegir": "^31.0.0",
"delay": "^4.4.0",
"aegir": "^32.1.0",
"delay": "^5.0.0",
"go-ipfs": "0.8.0",
"ipfs-core": "^0.5.4",
"ipfsd-ctl": "^7.2.0",
"ipfsd-ctl": "^8.0.0",
"it-all": "^1.0.4",
"it-concat": "^1.0.1",
"it-concat": "^1.0.3",
"it-first": "^1.0.4",
"nock": "^13.0.2",
"rimraf": "^3.0.2"
Expand Down
9 changes: 6 additions & 3 deletions src/add-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ const { AbortController } = require('native-abort-controller')
/**
* @typedef {import('ipfs-utils/src/types').ProgressFn} IPFSUtilsHttpUploadProgressFn
* @typedef {import('ipfs-core-types/src/root').AddProgressFn} IPFSCoreAddProgressFn
* @typedef {import('./types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/root').API<HTTPClientExtraOptions>} RootAPI
* @typedef {import('ipfs-core-types/src/root').AddResult} AddResult
*/

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

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

/**
* @param {any} input
* @returns {import('ipfs-core-types/src/files').UnixFSEntry}
*/
function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
/** @type {AddResult} */
const output = {
path: name,
cid: new CID(hash),
Expand All @@ -124,6 +128,5 @@ function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
}
}

// @ts-ignore
return output
}
11 changes: 8 additions & 3 deletions src/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ const last = require('it-last')
const configure = require('./lib/configure')

/**
* @param {import("./lib/core").ClientOptions} options
* @typedef {import('./types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/root').API<HTTPClientExtraOptions>} RootAPI
*/

/**
* @param {import('./types').Options} options
*/
module.exports = (options) => {
const all = addAll(options)
return configure(() => {
/**
* @type {import('.').Implements<typeof import('ipfs-core/src/components/add')>}
* @type {RootAPI["add"]}
*/
async function add (input, options = {}) {
// @ts-ignore - last may return undefind if source is empty
// @ts-ignore - last may return undefined if source is empty
return await last(all(input, options))
}
return add
Expand Down
3 changes: 3 additions & 0 deletions src/bitswap/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'

/**
* @param {import('../types').Options} config
*/
module.exports = config => ({
wantlist: require('./wantlist')(config),
wantlistForPeer: require('./wantlist-for-peer')(config),
Expand Down
25 changes: 16 additions & 9 deletions src/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
'use strict'

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

/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/bitswap').API<HTTPClientExtraOptions>} BitswapAPI
*/

module.exports = configure(api => {
/**
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/stat')>}
* @type {BitswapAPI["stat"]}
*/
async function stat (options = {}) {
const res = await api.post('bitswap/stat', {
Expand All @@ -22,16 +26,19 @@ module.exports = configure(api => {
return stat
})

/**
* @param {any} res
*/
function toCoreInterface (res) {
return {
provideBufLen: res.ProvideBufLen,
wantlist: (res.Wantlist || []).map(k => new CID(k['/'])),
wantlist: (res.Wantlist || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/'])),
peers: (res.Peers || []),
blocksReceived: new BigNumber(res.BlocksReceived),
dataReceived: new BigNumber(res.DataReceived),
blocksSent: new BigNumber(res.BlocksSent),
dataSent: new BigNumber(res.DataSent),
dupBlksReceived: new BigNumber(res.DupBlksReceived),
dupDataReceived: new BigNumber(res.DupDataReceived)
blocksReceived: BigInt(res.BlocksReceived),
dataReceived: BigInt(res.DataReceived),
blocksSent: BigInt(res.BlocksSent),
dataSent: BigInt(res.DataSent),
dupBlksReceived: BigInt(res.DupBlksReceived),
dupDataReceived: BigInt(res.DupDataReceived)
}
}
7 changes: 6 additions & 1 deletion src/bitswap/unwant.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/bitswap').API<HTTPClientExtraOptions>} BitswapAPI
*/

module.exports = configure(api => {
/**
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/unwant')>}
* @type {BitswapAPI["unwant"]}
*/
async function unwant (cid, options = {}) {
const res = await api.post('bitswap/unwant', {
Expand Down
9 changes: 7 additions & 2 deletions src/bitswap/wantlist-for-peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/bitswap').API<HTTPClientExtraOptions>} BitswapAPI
*/

module.exports = configure(api => {
/**
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/wantlist-for-peer')>}
* @type {BitswapAPI["wantlistForPeer"]}
*/
async function wantlistForPeer (peerId, options = {}) {
// @ts-ignore - CID|string seems to confuse typedef
Expand All @@ -22,7 +27,7 @@ module.exports = configure(api => {
headers: options.headers
})).json()

return (res.Keys || []).map(k => new CID(k['/']))
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/']))
}
return wantlistForPeer
})
9 changes: 7 additions & 2 deletions src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/bitswap').API<HTTPClientExtraOptions>} BitswapAPI
*/

module.exports = configure(api => {
/**
* @type {import('..').Implements<typeof import('ipfs-core/src/components/bitswap/wantlist')>}
* @type {BitswapAPI["wantlist"]}
*/
async function wantlist (options = {}) {
const res = await (await api.post('bitswap/wantlist', {
Expand All @@ -16,7 +21,7 @@ module.exports = configure(api => {
headers: options.headers
})).json()

return (res.Keys || []).map(k => new CID(k['/']))
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/']))
}
return wantlist
})
7 changes: 6 additions & 1 deletion src/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/block').API<HTTPClientExtraOptions>} BlockAPI
*/

module.exports = configure(api => {
/**
* @type {import('..').Implements<typeof import('ipfs-core/src/components/block/get')>}
* @type {BlockAPI["get"]}
*/
async function get (cid, options = {}) {
// @ts-ignore - CID|string seems to confuse typedef
Expand Down
Loading

0 comments on commit aaa3d04

Please sign in to comment.