Skip to content

Commit

Permalink
feat: upgrade to the new multiformats (#3556)
Browse files Browse the repository at this point in the history
- Replaces the old [interface-ipld-format](https://github.com/ipld/interface-ipld-format) stack with the new [multiformats](https://github.com/multiformats/js-multiformats) stack.
- The Block API takes/returns `Uint8Array`s instead of [ipld-block](https://github.com/ipld/js-ipld-block) objects

BREAKING CHANGE: ipld-formats no longer supported, use multiformat BlockCodecs instead

Co-authored-by: Rod Vagg <rod@vagg.org>
Co-authored-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
3 people authored Jul 19, 2021
1 parent 81d251f commit a4f04fc
Show file tree
Hide file tree
Showing 58 changed files with 487 additions and 469 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: '89KB'
bundlesizeMax: '66KB'
},
test: {
async before (options) {
Expand Down
26 changes: 11 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,38 @@
"lint": "aegir lint",
"coverage": "npx nyc -r html npm run test:node -- --bail",
"clean": "rimraf ./dist",
"dep-check": "aegir dep-check -i ipfs-core -i rimraf -i ipfs-core-types -i abort-controller -i ipld"
"dep-check": "aegir dep-check -i ipfs-core -i rimraf -i ipfs-core-types -i abort-controller"
},
"dependencies": {
"@ipld/dag-cbor": "^6.0.5",
"@ipld/dag-pb": "^2.1.3",
"abort-controller": "^3.0.0",
"any-signal": "^2.1.2",
"cids": "^1.1.6",
"err-code": "^3.0.1",
"debug": "^4.1.1",
"form-data": "^4.0.0",
"ipfs-core-types": "^0.5.2",
"ipfs-core-utils": "^0.8.3",
"ipfs-unixfs": "^4.0.3",
"ipfs-utils": "^8.1.2",
"ipld-block": "^0.11.0",
"ipld-dag-cbor": "^1.0.0",
"ipld-dag-pb": "^0.22.1",
"ipld-raw": "^7.0.0",
"ipfs-utils": "^8.1.4",
"it-first": "^1.0.6",
"it-last": "^1.0.4",
"it-map": "^1.0.4",
"it-tar": "^3.0.0",
"it-to-stream": "^1.0.0",
"merge-options": "^3.0.4",
"multiaddr": "^9.0.1",
"multibase": "^4.0.2",
"multicodec": "^3.0.1",
"multihashes": "^4.0.2",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.1",
"nanoid": "^3.1.12",
"native-abort-controller": "^1.0.3",
"parse-duration": "^1.0.0",
"stream-to-it": "^0.2.2",
"uint8arrays": "^2.1.3"
"uint8arrays": "^2.1.6"
},
"devDependencies": {
"aegir": "^33.0.0",
"aegir": "^34.0.2",
"delay": "^5.0.0",
"go-ipfs": "0.8.0",
"ipfsd-ctl": "^8.0.1",
"ipfsd-ctl": "^9.0.0",
"it-all": "^1.0.4",
"it-concat": "^2.0.0",
"it-first": "^1.0.4",
Expand Down
12 changes: 9 additions & 3 deletions src/add-all.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const CID = require('cids')
const { CID } = require('multiformats/cid')
const toCamel = require('./lib/object-to-camel')
const configure = require('./lib/configure')
const multipartRequest = require('./lib/multipart-request')
Expand Down Expand Up @@ -107,13 +107,19 @@ const createOnUploadProgress = (size, parts, progress) => {
}

/**
* @param {any} input
* @param {object} input
* @param {string} input.name
* @param {string} input.hash
* @param {string} input.size
* @param {string} [input.mode]
* @param {number} [input.mtime]
* @param {number} [input.mtimeNsecs]
*/
function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
/** @type {AddResult} */
const output = {
path: name,
cid: new CID(hash),
cid: CID.parse(hash),
size: parseInt(size)
}

Expand Down
4 changes: 2 additions & 2 deletions src/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

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

Expand Down Expand Up @@ -32,7 +32,7 @@ module.exports = configure(api => {
function toCoreInterface (res) {
return {
provideBufLen: res.ProvideBufLen,
wantlist: (res.Wantlist || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/'])),
wantlist: (res.Wantlist || []).map((/** @type {{ '/': string }} */ k) => CID.parse(k['/'])),
peers: (res.Peers || []),
blocksReceived: BigInt(res.BlocksReceived),
dataReceived: BigInt(res.DataReceived),
Expand Down
4 changes: 1 addition & 3 deletions src/bitswap/unwant.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

Expand All @@ -18,8 +17,7 @@ module.exports = configure(api => {
timeout: options.timeout,
signal: options.signal,
searchParams: toUrlSearchParams({
// @ts-ignore - CID|string seems to confuse typedef
arg: typeof cid === 'string' ? cid : new CID(cid).toString(),
arg: cid.toString(),
...options
}),
headers: options.headers
Expand Down
9 changes: 3 additions & 6 deletions src/bitswap/wantlist-for-peer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

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

Expand All @@ -14,20 +14,17 @@ module.exports = configure(api => {
* @type {BitswapAPI["wantlistForPeer"]}
*/
async function wantlistForPeer (peerId, options = {}) {
// @ts-ignore - CID|string seems to confuse typedef
peerId = typeof peerId === 'string' ? peerId : new CID(peerId).toString()

const res = await (await api.post('bitswap/wantlist', {
timeout: options.timeout,
signal: options.signal,
searchParams: toUrlSearchParams({
...options,
peer: peerId
peer: peerId.toString()
}),
headers: options.headers
})).json()

return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/']))
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => CID.parse(k['/']))
}
return wantlistForPeer
})
4 changes: 2 additions & 2 deletions src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

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

Expand All @@ -21,7 +21,7 @@ module.exports = configure(api => {
headers: options.headers
})).json()

return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/']))
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => CID.parse(k['/']))
}
return wantlist
})
7 changes: 1 addition & 6 deletions src/block/get.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const Block = require('ipld-block')
const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

Expand All @@ -15,9 +13,6 @@ module.exports = configure(api => {
* @type {BlockAPI["get"]}
*/
async function get (cid, options = {}) {
// @ts-ignore - CID|string seems to confuse typedef
cid = new CID(cid)

const res = await api.post('block/get', {
timeout: options.timeout,
signal: options.signal,
Expand All @@ -28,7 +23,7 @@ module.exports = configure(api => {
headers: options.headers
})

return new Block(new Uint8Array(await res.arrayBuffer()), cid)
return new Uint8Array(await res.arrayBuffer())
}
return get
})
32 changes: 2 additions & 30 deletions src/block/put.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const Block = require('ipld-block')
const CID = require('cids')
const multihash = require('multihashes')
const { CID } = require('multiformats/cid')
const multipartRequest = require('../lib/multipart-request')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')
Expand All @@ -19,38 +17,12 @@ module.exports = configure(api => {
* @type {BlockAPI["put"]}
*/
async function put (data, options = {}) {
if (Block.isBlock(data)) {
const { name, length } = multihash.decode(data.cid.multihash)
options = {
...options,
format: data.cid.codec,
mhtype: name,
mhlen: length,
version: data.cid.version
}
// @ts-ignore - data is typed as block so TS complains about
// Uint8Array assignment.
data = data.data
} else if (options.cid) {
const cid = new CID(options.cid)
const { name, length } = multihash.decode(cid.multihash)
options = {
...options,
format: cid.codec,
mhtype: name,
mhlen: length,
version: cid.version
}
delete options.cid
}

// allow aborting requests on body errors
const controller = new AbortController()
const signal = abortSignal(controller.signal, options.signal)

let res
try {
// @ts-ignore https://github.com/ipfs/js-ipfs-utils/issues/90
const response = await api.post('block/put', {
timeout: options.timeout,
signal: signal,
Expand All @@ -72,7 +44,7 @@ module.exports = configure(api => {
throw err
}

return new Block((/** @type {Uint8Array} */ data), new CID(res.Key))
return CID.parse(res.Key)
}

return put
Expand Down
6 changes: 3 additions & 3 deletions src/block/rm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

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

Expand All @@ -23,7 +23,7 @@ module.exports = configure(api => {
timeout: options.timeout,
signal: options.signal,
searchParams: toUrlSearchParams({
arg: cid.map(cid => new CID(cid).toString()),
arg: cid.map(cid => cid.toString()),
'stream-channels': true,
...options
}),
Expand All @@ -44,7 +44,7 @@ module.exports = configure(api => {
function toCoreInterface (removed) {
/** @type {RmResult} */
const out = {
cid: new CID(removed.Hash)
cid: CID.parse(removed.Hash)
}

if (removed.Error) {
Expand Down
6 changes: 3 additions & 3 deletions src/block/stat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

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

Expand All @@ -18,14 +18,14 @@ module.exports = configure(api => {
timeout: options.timeout,
signal: options.signal,
searchParams: toUrlSearchParams({
arg: new CID(cid).toString(),
arg: cid.toString(),
...options
}),
headers: options.headers
})
const data = await res.json()

return { cid: new CID(data.Key), size: data.Size }
return { cid: CID.parse(data.Key), size: data.Size }
}

return stat
Expand Down
3 changes: 1 addition & 2 deletions src/cat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const CID = require('cids')
const configure = require('./lib/configure')
const toUrlSearchParams = require('./lib/to-url-search-params')

Expand All @@ -18,7 +17,7 @@ module.exports = configure(api => {
timeout: options.timeout,
signal: options.signal,
searchParams: toUrlSearchParams({
arg: typeof path === 'string' ? path : new CID(path).toString(),
arg: path.toString(),
...options
}),
headers: options.headers
Expand Down
Loading

0 comments on commit a4f04fc

Please sign in to comment.