Skip to content

Commit a4f04fc

Browse files
vmxrvaggachingbrain
authored
feat: upgrade to the new multiformats (#3556)
- 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 <[email protected]> Co-authored-by: achingbrain <[email protected]>
1 parent 81d251f commit a4f04fc

Some content is hidden

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

58 files changed

+487
-469
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: '89KB'
9+
bundlesizeMax: '66KB'
1010
},
1111
test: {
1212
async before (options) {

package.json

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,38 @@
4141
"lint": "aegir lint",
4242
"coverage": "npx nyc -r html npm run test:node -- --bail",
4343
"clean": "rimraf ./dist",
44-
"dep-check": "aegir dep-check -i ipfs-core -i rimraf -i ipfs-core-types -i abort-controller -i ipld"
44+
"dep-check": "aegir dep-check -i ipfs-core -i rimraf -i ipfs-core-types -i abort-controller"
4545
},
4646
"dependencies": {
47+
"@ipld/dag-cbor": "^6.0.5",
48+
"@ipld/dag-pb": "^2.1.3",
4749
"abort-controller": "^3.0.0",
4850
"any-signal": "^2.1.2",
49-
"cids": "^1.1.6",
51+
"err-code": "^3.0.1",
5052
"debug": "^4.1.1",
5153
"form-data": "^4.0.0",
5254
"ipfs-core-types": "^0.5.2",
5355
"ipfs-core-utils": "^0.8.3",
54-
"ipfs-unixfs": "^4.0.3",
55-
"ipfs-utils": "^8.1.2",
56-
"ipld-block": "^0.11.0",
57-
"ipld-dag-cbor": "^1.0.0",
58-
"ipld-dag-pb": "^0.22.1",
59-
"ipld-raw": "^7.0.0",
56+
"ipfs-utils": "^8.1.4",
57+
"it-first": "^1.0.6",
6058
"it-last": "^1.0.4",
6159
"it-map": "^1.0.4",
6260
"it-tar": "^3.0.0",
6361
"it-to-stream": "^1.0.0",
6462
"merge-options": "^3.0.4",
65-
"multiaddr": "^9.0.1",
66-
"multibase": "^4.0.2",
67-
"multicodec": "^3.0.1",
68-
"multihashes": "^4.0.2",
63+
"multiaddr": "^10.0.0",
64+
"multiformats": "^9.4.1",
6965
"nanoid": "^3.1.12",
7066
"native-abort-controller": "^1.0.3",
7167
"parse-duration": "^1.0.0",
7268
"stream-to-it": "^0.2.2",
73-
"uint8arrays": "^2.1.3"
69+
"uint8arrays": "^2.1.6"
7470
},
7571
"devDependencies": {
76-
"aegir": "^33.0.0",
72+
"aegir": "^34.0.2",
7773
"delay": "^5.0.0",
7874
"go-ipfs": "0.8.0",
79-
"ipfsd-ctl": "^8.0.1",
75+
"ipfsd-ctl": "^9.0.0",
8076
"it-all": "^1.0.4",
8177
"it-concat": "^2.0.0",
8278
"it-first": "^1.0.4",

src/add-all.js

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

3-
const CID = require('cids')
3+
const { CID } = require('multiformats/cid')
44
const toCamel = require('./lib/object-to-camel')
55
const configure = require('./lib/configure')
66
const multipartRequest = require('./lib/multipart-request')
@@ -107,13 +107,19 @@ const createOnUploadProgress = (size, parts, progress) => {
107107
}
108108

109109
/**
110-
* @param {any} input
110+
* @param {object} input
111+
* @param {string} input.name
112+
* @param {string} input.hash
113+
* @param {string} input.size
114+
* @param {string} [input.mode]
115+
* @param {number} [input.mtime]
116+
* @param {number} [input.mtimeNsecs]
111117
*/
112118
function toCoreInterface ({ name, hash, size, mode, mtime, mtimeNsecs }) {
113119
/** @type {AddResult} */
114120
const output = {
115121
path: name,
116-
cid: new CID(hash),
122+
cid: CID.parse(hash),
117123
size: parseInt(size)
118124
}
119125

src/bitswap/stat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

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

@@ -32,7 +32,7 @@ module.exports = configure(api => {
3232
function toCoreInterface (res) {
3333
return {
3434
provideBufLen: res.ProvideBufLen,
35-
wantlist: (res.Wantlist || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/'])),
35+
wantlist: (res.Wantlist || []).map((/** @type {{ '/': string }} */ k) => CID.parse(k['/'])),
3636
peers: (res.Peers || []),
3737
blocksReceived: BigInt(res.BlocksReceived),
3838
dataReceived: BigInt(res.DataReceived),

src/bitswap/unwant.js

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

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

@@ -18,8 +17,7 @@ module.exports = configure(api => {
1817
timeout: options.timeout,
1918
signal: options.signal,
2019
searchParams: toUrlSearchParams({
21-
// @ts-ignore - CID|string seems to confuse typedef
22-
arg: typeof cid === 'string' ? cid : new CID(cid).toString(),
20+
arg: cid.toString(),
2321
...options
2422
}),
2523
headers: options.headers

src/bitswap/wantlist-for-peer.js

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

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

@@ -14,20 +14,17 @@ module.exports = configure(api => {
1414
* @type {BitswapAPI["wantlistForPeer"]}
1515
*/
1616
async function wantlistForPeer (peerId, options = {}) {
17-
// @ts-ignore - CID|string seems to confuse typedef
18-
peerId = typeof peerId === 'string' ? peerId : new CID(peerId).toString()
19-
2017
const res = await (await api.post('bitswap/wantlist', {
2118
timeout: options.timeout,
2219
signal: options.signal,
2320
searchParams: toUrlSearchParams({
2421
...options,
25-
peer: peerId
22+
peer: peerId.toString()
2623
}),
2724
headers: options.headers
2825
})).json()
2926

30-
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/']))
27+
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => CID.parse(k['/']))
3128
}
3229
return wantlistForPeer
3330
})

src/bitswap/wantlist.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

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

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

24-
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => new CID(k['/']))
24+
return (res.Keys || []).map((/** @type {{ '/': string }} */ k) => CID.parse(k['/']))
2525
}
2626
return wantlist
2727
})

src/block/get.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict'
22

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

@@ -15,9 +13,6 @@ module.exports = configure(api => {
1513
* @type {BlockAPI["get"]}
1614
*/
1715
async function get (cid, options = {}) {
18-
// @ts-ignore - CID|string seems to confuse typedef
19-
cid = new CID(cid)
20-
2116
const res = await api.post('block/get', {
2217
timeout: options.timeout,
2318
signal: options.signal,
@@ -28,7 +23,7 @@ module.exports = configure(api => {
2823
headers: options.headers
2924
})
3025

31-
return new Block(new Uint8Array(await res.arrayBuffer()), cid)
26+
return new Uint8Array(await res.arrayBuffer())
3227
}
3328
return get
3429
})

src/block/put.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict'
22

3-
const Block = require('ipld-block')
4-
const CID = require('cids')
5-
const multihash = require('multihashes')
3+
const { CID } = require('multiformats/cid')
64
const multipartRequest = require('../lib/multipart-request')
75
const configure = require('../lib/configure')
86
const toUrlSearchParams = require('../lib/to-url-search-params')
@@ -19,38 +17,12 @@ module.exports = configure(api => {
1917
* @type {BlockAPI["put"]}
2018
*/
2119
async function put (data, options = {}) {
22-
if (Block.isBlock(data)) {
23-
const { name, length } = multihash.decode(data.cid.multihash)
24-
options = {
25-
...options,
26-
format: data.cid.codec,
27-
mhtype: name,
28-
mhlen: length,
29-
version: data.cid.version
30-
}
31-
// @ts-ignore - data is typed as block so TS complains about
32-
// Uint8Array assignment.
33-
data = data.data
34-
} else if (options.cid) {
35-
const cid = new CID(options.cid)
36-
const { name, length } = multihash.decode(cid.multihash)
37-
options = {
38-
...options,
39-
format: cid.codec,
40-
mhtype: name,
41-
mhlen: length,
42-
version: cid.version
43-
}
44-
delete options.cid
45-
}
46-
4720
// allow aborting requests on body errors
4821
const controller = new AbortController()
4922
const signal = abortSignal(controller.signal, options.signal)
5023

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

75-
return new Block((/** @type {Uint8Array} */ data), new CID(res.Key))
47+
return CID.parse(res.Key)
7648
}
7749

7850
return put

src/block/rm.js

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

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

@@ -23,7 +23,7 @@ module.exports = configure(api => {
2323
timeout: options.timeout,
2424
signal: options.signal,
2525
searchParams: toUrlSearchParams({
26-
arg: cid.map(cid => new CID(cid).toString()),
26+
arg: cid.map(cid => cid.toString()),
2727
'stream-channels': true,
2828
...options
2929
}),
@@ -44,7 +44,7 @@ module.exports = configure(api => {
4444
function toCoreInterface (removed) {
4545
/** @type {RmResult} */
4646
const out = {
47-
cid: new CID(removed.Hash)
47+
cid: CID.parse(removed.Hash)
4848
}
4949

5050
if (removed.Error) {

0 commit comments

Comments
 (0)