Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 7165bf7

Browse files
authored
docs: add upgrade guide for ESM release (#4098)
Adds upgrade guide and updates core-api docs with API changes
1 parent 74aee8b commit 7165bf7

16 files changed

+157
-33
lines changed

docs/MODULE.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,14 @@ Aside from the default export, `ipfs` exports various types and utilities that a
377377
- [`crypto`](https://www.npmjs.com/package/libp2p-crypto)
378378
- [`isIPFS`](https://www.npmjs.com/package/is-ipfs)
379379
- [`Buffer`](https://www.npmjs.com/package/buffer)
380-
- [`PeerId`](https://www.npmjs.com/package/peer-id)
380+
- [`PeerId`](https://docs.libp2p.io/concepts/peer-id/)
381381
- [`PeerInfo`](https://www.npmjs.com/package/peer-info)
382382
- [`multiaddr`](https://www.npmjs.com/package/multiaddr)
383383
- [`multibase`](https://www.npmjs.com/package/multibase)
384384
- [`multihash`](https://www.npmjs.com/package/multihashes)
385385
- [`multihashing`](https://www.npmjs.com/package/multihashing-async)
386386
- [`multicodec`](https://www.npmjs.com/package/multicodec)
387-
- [`CID`](https://www.npmjs.com/package/cids)
387+
- [`CID`](https://docs.ipfs.io/concepts/content-addressing)
388388

389389
These can be accessed like this, for example:
390390

docs/core-api/BITSWAP.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ A great source of [examples][] can be found in the tests for this API.
6262

6363
| Name | Type | Default | Description |
6464
| ---- | ---- | ------- | ----------- |
65-
| peerId | [PeerId][], [CID][], `String` or `Uint8Array` | A peer ID to return the wantlist for |
65+
| peerId | [PeerId][] | A peer ID to return the wantlist for |
6666

6767
### Options
6868

@@ -159,7 +159,7 @@ The returned object contains the following keys:
159159

160160
- `provideBufLen` is an integer.
161161
- `wantlist` (array of [CID][cid]s)
162-
- `peers` (array of peer IDs represented by strings)
162+
- `peers` (array of [PeerId][peerId]s)
163163
- `blocksReceived` is a [BigInt][1]
164164
- `dataReceived` is a [BigInt][1]
165165
- `blocksSent` is a [BigInt][1]
@@ -192,6 +192,6 @@ A great source of [examples][] can be found in the tests for this API.
192192

193193
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
194194
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/bitswap
195-
[cid]: https://www.npmjs.com/package/cids
196-
[peerid]: https://www.npmjs.com/package/peer-id
195+
[cid]: https://docs.ipfs.io/concepts/content-addressing
196+
[peerid]: https://docs.libp2p.io/concepts/peer-id/
197197
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

docs/core-api/BLOCK.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,4 @@ A great source of [examples][] can be found in the tests for this API.
226226
[multihash]: https://github.com/multiformats/multihash
227227
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/block
228228
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
229-
[cid]: https://www.npmjs.com/package/cids
229+
[cid]: https://docs.ipfs.io/concepts/content-addressing

docs/core-api/DAG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,6 @@ console.log(result)
289289
A great source of [examples][] can be found in the tests for this API.
290290

291291
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/dag
292-
[cid]: https://www.npmjs.com/package/cids
292+
[cid]: https://docs.ipfs.io/concepts/content-addressing
293293
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
294294
[CAR file]: https://ipld.io/specs/transport/car/

docs/core-api/DHT.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
| Name | Type | Description |
4141
| ---- | ---- | ----------- |
42-
| peerId | [PeerID][] or [CID][] | The Peer ID of the node to find |
42+
| peerId | [PeerID][] | The Peer ID of the node to find |
4343

4444
### Options
4545

@@ -61,7 +61,7 @@ An optional object which may have the following keys:
6161
```JavaScript
6262
const info = await ipfs.dht.findPeer('QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt')
6363

64-
console.log(info.id)
64+
console.log(info.id.toString())
6565
/*
6666
QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt
6767
*/
@@ -191,15 +191,15 @@ Prints objects like:
191191
192192
{
193193
extra: 'dial backoff',
194-
id: 'QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z',
194+
id: PeerId('QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z'),
195195
responses: [
196196
{
197197
addrs: [
198198
Multiaddr(/ip4/127.0.0.1/tcp/4001),
199199
Multiaddr(/ip4/172.20.0.3/tcp/4001),
200200
Multiaddr(/ip4/35.178.190.196/tcp/1024)
201201
],
202-
id: 'QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8'
202+
id: PeerId('QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8')
203203
}
204204
],
205205
type: 1
@@ -258,15 +258,15 @@ Prints objects like:
258258
259259
{
260260
extra: 'dial backoff',
261-
id: 'QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z',
261+
id: PeerId('QmWtewmnzJiQevJPSmG9s8aC7yRfK2WXTCdRc1pCbDFu6z'),
262262
responses: [
263263
{
264264
addrs: [
265265
Multiaddr(/ip4/127.0.0.1/tcp/4001),
266266
Multiaddr(/ip4/172.20.0.3/tcp/4001),
267267
Multiaddr(/ip4/35.178.190.196/tcp/1024)
268268
],
269-
id: 'QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8'
269+
id: PeerId('QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8')
270270
}
271271
],
272272
type: 1
@@ -288,7 +288,7 @@ A great source of [examples][] can be found in the tests for this API.
288288

289289
## `ipfs.dht.query(peerId, [options])`
290290

291-
> Find the closest Peer IDs to a given Peer ID by querying the DHT.
291+
> Find the closest Peer IDs to a given Peer ID or CID by querying the DHT.
292292
293293
### Parameters
294294

@@ -331,7 +331,7 @@ Prints objects like:
331331
Multiaddr(/ip4/172.20.0.3/tcp/4001),
332332
Multiaddr(/ip4/35.178.190.196/tcp/1024)
333333
],
334-
id: 'QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8'
334+
id: PeerId('QmRz5Nth4jTFuJJKcjyb6uwvrhxWbruRvamKY2PJxwJKw8')
335335
}
336336
],
337337
type: 1
@@ -345,6 +345,6 @@ https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b9
345345
A great source of [examples][] can be found in the tests for this API.
346346

347347
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/dht
348-
[peerid]: https://www.npmjs.com/package/peer-id
349-
[cid]: https://www.npmjs.com/package/cids
348+
[peerid]: https://docs.libp2p.io/concepts/peer-id/
349+
[cid]: https://docs.ipfs.io/concepts/content-addressing
350350
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

docs/core-api/FILES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ for await (const file of ipfs.files.ls('/screenshots')) {
10241024

10251025
[b]: https://www.npmjs.com/package/buffer
10261026
[file]: https://developer.mozilla.org/en-US/docs/Web/API/File
1027-
[cid]: https://www.npmjs.com/package/cids
1027+
[cid]: https://docs.ipfs.io/concepts/content-addressing
10281028
[blob]: https://developer.mozilla.org/en-US/docs/Web/API/Blob
10291029
[IPFS Path]: https://www.npmjs.com/package/is-ipfs#isipfspathpath
10301030
[MFS Path]: https://docs.ipfs.io/guides/concepts/mfs/

docs/core-api/KEY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,5 +288,5 @@ console.log(key)
288288
A great source of [examples][] can be found in the tests for this API.
289289

290290
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/key
291-
[cid]: https://www.npmjs.com/package/cids
291+
[cid]: https://docs.ipfs.io/concepts/content-addressing
292292
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

docs/core-api/MISCELLANEOUS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,5 @@ A great source of [examples](https://github.com/ipfs/js-ipfs/blob/master/package
300300
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/miscellaneous
301301
[rs]: https://www.npmjs.com/package/readable-stream
302302
[ps]: https://www.npmjs.com/package/pull-stream
303-
[cid]: https://www.npmjs.com/package/cids
303+
[cid]: https://docs.ipfs.io/concepts/content-addressing
304304
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

docs/core-api/NAME.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ A great source of [examples][examples-pubsub] can be found in the tests for this
226226

227227
| Name | Type | Description |
228228
| ---- | ---- | ----------- |
229-
| value | `String` | An IPNS address such as `/ipns/ipfs.io` |
229+
| value | `PeerId` or `string` | An IPNS address such as `/ipns/ipfs.io` |
230230

231231
### Options
232232

@@ -261,5 +261,5 @@ A great source of [examples][] can be found in the tests for this API.
261261

262262
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/name
263263
[examples-pubsub]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/name-pubsub
264-
[cid]: https://www.npmjs.com/package/cids
264+
[cid]: https://docs.ipfs.io/concepts/content-addressing
265265
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

docs/core-api/PIN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -634,5 +634,5 @@ A great source of [examples][] can be found in the tests for this API.
634634
[Stat]: #stat
635635
[PinCount]: #pincount
636636
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/pin
637-
[cid]: https://www.npmjs.com/package/cids
637+
[cid]: https://docs.ipfs.io/concepts/content-addressing
638638
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

docs/core-api/REFS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,6 @@ for await (const ref of ipfs.refs.local()) {
112112

113113
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/files-regular
114114
[b]: https://www.npmjs.com/package/buffer
115-
[cid]: https://www.npmjs.com/package/cids
115+
[cid]: https://docs.ipfs.io/concepts/content-addressing
116116
[blob]: https://developer.mozilla.org/en-US/docs/Web/API/Blob
117117
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

docs/core-api/REPO.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,5 @@ console.log(version)
136136
```
137137

138138
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
139-
[cid]: https://www.npmjs.com/package/cids
139+
[cid]: https://docs.ipfs.io/concepts/content-addressing
140140
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

docs/core-api/STATS.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ An optional object which may have the following keys:
3434

3535
| Name | Type | Default | Description |
3636
| ---- | ---- | ------- | ----------- |
37-
| peer | [PeerId][], [CID][] or `String` | `undefined` | Specifies a peer to print bandwidth for |
37+
| peer | [PeerId][] | `undefined` | Specifies a peer to print bandwidth for |
3838
| proto | `String` | `undefined` | Specifies a protocol to print bandwidth for |
3939
| poll | `boolean` | `undefined` | Is used to yield bandwidth info at an interval |
4040
| interval | `Number` | `undefined` | The time interval to wait between updating output, if `poll` is `true` |
@@ -71,5 +71,5 @@ A great source of [examples][] can be found in the tests for this API.
7171
[bigNumber]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
7272
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/stats
7373
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
74-
[cid]: https://www.npmjs.com/package/cids
75-
[peerid]: https://www.npmjs.com/package/peer-id
74+
[cid]: https://docs.ipfs.io/concepts/content-addressing
75+
[peerid]: https://docs.libp2p.io/concepts/peer-id/

docs/core-api/SWARM.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ A great source of [examples][] can be found in the tests for this API.
8383

8484
| Name | Type | Description |
8585
| ---- | ---- | ----------- |
86-
| addr | [MultiAddr][] | The object to search for references |
86+
| addr | [MultiAddr][] or [PeerId][] | The PeerId or Multiaddr to connect to |
8787

8888
### Options
8989

@@ -116,7 +116,7 @@ A great source of [examples][] can be found in the tests for this API.
116116

117117
| Name | Type | Description |
118118
| ---- | ---- | ----------- |
119-
| addr | [MultiAddr][] | The object to search for references |
119+
| addr | [MultiAddr][] or [PeerId][] | The PeerId or Multiaddr to disconnect from |
120120

121121
### Options
122122

@@ -228,3 +228,4 @@ A great source of [examples][] can be found in the tests for this API.
228228
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/swarm
229229
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
230230
[MultiAddr]: https://github.com/multiformats/js-multiaddr
231+
[peerid]: https://docs.libp2p.io/concepts/peer-id/

docs/upgrading/v0.62-v0.63.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<!--Specify versions for migration below-->
2+
# Migrating to [email protected] and [email protected] <!-- omit in toc -->
3+
4+
> A migration guide for refactoring your application code from `[email protected]` to `[email protected]`
5+
6+
## Table of Contents <!-- omit in toc -->
7+
8+
- [ESM](#esm)
9+
- [[email protected]](#libp2p037x)
10+
- [PeerIds](#peerids)
11+
- [multiaddrs](#multiaddrs)
12+
13+
## ESM
14+
15+
The biggest change to `[email protected]` is that the module is now [ESM-only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
16+
17+
ESM is the module system for JavaScript, it allows us to structure our code in separate files without polluting a global namespace.
18+
19+
Other systems have tried to fill this gap, notably CommonJS, AMD, RequireJS and others, but ESM is [the official standard format](https://tc39.es/ecma262/#sec-modules) to package JavaScript code for reuse.
20+
21+
If you see errors similar to `Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in node_modules/ipfs/package.json` you are likely trying to load ESM code from a CJS environment via `require`. This is not possible, instead it must be loaded using `import`.
22+
23+
If your application is not yet ESM or you are not ready to port it to ESM, you can use the [dynamic `import` function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) to load `ipfs` at runtime from a CJS module:
24+
25+
```js
26+
async function loadIpfs () {
27+
const { create } = await import('ipfs-core')
28+
29+
const node = await create({
30+
// ... config here
31+
})
32+
33+
return node
34+
}
35+
```
36+
37+
38+
39+
`[email protected]` upgrades to `[email protected]`. This is a significant refactor that ports the entire stack to TypeScript and publishes all modules as ESM-only code.
40+
41+
Please see the [libp2p 0.37.x` upgrade guide](https://github.com/libp2p/js-libp2p/blob/master/doc/migrations/v0.36-v.037.md) for how this may affect your application.
42+
43+
## PeerIds
44+
45+
The core `libp2p` module and all supporting modules have now been ported to TypeScript in a complete ground-up rewrite. We took this opportunity to solve a few long-standing problems with some of the data types, particularly in how they relate to use in the browser.
46+
47+
One problem we have solved is that the `PeerId` objects used internally expose some cryptographic operations that require heavyweight libraries to be included in browser bundles due to there being no native web-crypto implementation of the algorithms used in those operations.
48+
49+
With `[email protected]` those operations have been encapsulated in the `@libp2p/crypto` module which means `PeerId` objects become a lot more lightweight and can now be exposed/accepted as core-api types so we can use them to differentiate between different data types instead of having to treat everything as strings.
50+
51+
The affected methods are:
52+
53+
```js
54+
// `peerId` must now be a `PeerId`, previously it was a `string`
55+
ipfs.bitswap.wantlistForPeer(peerId, options)
56+
57+
// Bitswp peers are now returned as `PeerId[]` instead of `string[]`
58+
ipfs.bitswap.stat(options)
59+
60+
// `peerId` must now be a `PeerId`
61+
ipfs.dht.findPeer(peerId, options)
62+
63+
// `peerIdOrCid` must now be a `PeerId` or a `CID`, previously it was a `string` or a `CID`
64+
ipfs.dht.query(peerIdOrCid, options)
65+
66+
// the following DHT events have their `from` field as `PeerId`, previously it was a `string`
67+
PeerResponseEvent
68+
ValueEvent
69+
DialingPeerEvent
70+
71+
// the following DHT events have had their `from` property removed because it is not exposed by go-ipfs so causes incompatibilities
72+
QueryErrorEvent
73+
FinalPeerEvent
74+
75+
// the folloing DHT events have had their `to` property removed because it is not exposed by go-ipfs so causes incompatibilities
76+
SendingQueryEvent
77+
78+
// the `providers` and `closer` properties (where applicable) of the following events have the `peerId` property specified as a `PeerId`, previously it was a `string`
79+
PeerResponseEvent
80+
PeerResponseEvent
81+
82+
// `value` can now be a string or a `PeerId`. If a string is passed it will be interpreted as a DNS address.
83+
ipfs.name.resolve(value, options)
84+
85+
// The return type of this method is now `Promise<PeerId[]>`, previously it was a `Promise<string[]>`
86+
ipfs.pubsub.peers(topic, options)
87+
88+
// `peerId` must now be a `PeerId`, previously it was a `string`
89+
ipfs.ping(peerId, options)
90+
91+
// the `peer` property of `options` must now be a `PeerId` when specified, previously it was a `string`
92+
ipfs.stats.bw(options)
93+
94+
// `multiaddrOrPeerId` must be a `Multiaddr` or `PeerId`, previously it was a `Multiaddr` or `string`
95+
ipfs.swarm.connect(multiaddrOrPeerId, options)
96+
97+
// `multiaddrOrPeerId` must be a `Multiaddr` or `PeerId`, previously it was a `Multiaddr` or `string`
98+
ipfs.swarm.disconnect(multiaddrOrPeerId, options)
99+
```
100+
101+
`PeerId`s can be created from strings using the `@libp2p/peer-id` module:
102+
103+
```js
104+
import { peerIdFromString } from '@libp2p/peer-id'
105+
106+
const peerId = peerIdFromString('Qmfoo')
107+
```
108+
109+
They can also be created using the `@libp2p/peer-id-factory` module:
110+
111+
```js
112+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
113+
114+
const peerId = await createEd25519PeerId()
115+
```
116+
117+
## multiaddrs
118+
119+
The `multiaddr` module has been ported to Typescript and is now published as ESM-only.
120+
121+
It has been renamed to `@multiformats/multiaddr` so please update your dependencies and replace usage in your code.
122+
123+
The API otherwise is compatible.
124+

lerna.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"message": "chore: publish",
1515
"createRelease": "github",
1616
"conventionalCommits": true,
17-
"verifyAccess": false,
18-
"contents": "dist"
17+
"verifyAccess": false
1918
},
2019
"version": {
2120
"allowBranch": ["master", "release/*"],

0 commit comments

Comments
 (0)