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

Commit 9a40109

Browse files
authored
feat: switch to esm (#3879)
Refactors the code to be ESM. Dual publishes CJS and ESM for maximum compatibility. There are no default exports, so code similar to: ```js import IPFS from 'ipfs' const ipfs = await IPFS.create() ``` should be refactored to: ```js import { create } as IPFS from 'ipfs' const ipfs = await create() ``` BREAKING CHANGE: There are no default exports and everything is now dual published as ESM/CJS
1 parent 4126a5a commit 9a40109

File tree

1,024 files changed

+9298
-7976
lines changed

Some content is hidden

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

1,024 files changed

+9298
-7976
lines changed

.github/workflows/test.yml

+137-126
Large diffs are not rendered by default.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ tsconfig-check.aegir.json
3333

3434
# Operating system files
3535
.DS_Store
36+
types

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ $ npm install ipfs-core
8585
Then start a node in your app:
8686

8787
```javascript
88-
const IPFS = require('ipfs-core')
88+
import * as IPFS from 'ipfs-core'
8989

9090
const ipfs = await IPFS.create()
9191
const { cid } = await ipfs.add('Hello world')

docs/DAEMON.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The IPFS Daemon exposes the API defined in the [HTTP API spec](https://docs.ipfs
2323
If you want a programmatic way to spawn a IPFS Daemon using JavaScript, check out the [ipfsd-ctl](https://github.com/ipfs/js-ipfsd-ctl) module.
2424

2525
```javascript
26-
const { createFactory } = require('ipfsd-ctl')
26+
import { createFactory } from 'ipfsd-ctl'
2727
const factory = createFactory({
2828
type: 'proc' // or 'js' to run in a separate process
2929
})

docs/FAQ.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ Yes, however, bear in mind that there isn't a 100% stable solution to use WebRTC
7373
To add WebRTC support in a IPFS node instance, do:
7474

7575
```JavaScript
76-
const wrtc = require('wrtc') // or require('electron-webrtc')()
77-
const WebRTCStar = require('libp2p-webrtc-star')
76+
import wrtc from 'wrtc' // or 'electron-webrtc'
77+
import WebRTCStar from 'libp2p-webrtc-star'
7878

7979
const node = await IPFS.create({
8080
repo: 'your-repo-path',

docs/IPLD.md

+22-16
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,41 @@ If your application requires support for extra codecs, you can configure them as
4949
1. Configure the [IPLD layer](https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/docs/MODULE.md#optionsipld) of your IPFS daemon to support the codec. This step is necessary so the node knows how to prepare data received over HTTP to be passed to IPLD for serialization:
5050

5151
```javascript
52-
const ipfs = require('ipfs')
52+
import { create } from 'ipfs'
53+
import customBlockCodec from 'custom-blockcodec'
54+
import customMultibase from 'custom-multibase'
55+
import customMultihasher from 'custom-multihasher'
5356

54-
const node = await ipfs({
57+
const node = await create({
5558
ipld: {
5659
// either specify BlockCodecs as part of the `codecs` list
5760
codecs: [
58-
require('custom-blockcodec')
61+
customBlockCodec
5962
],
6063

6164
// and/or supply a function to load them dynamically
6265
loadCodec: async (codecNameOrCode) => {
63-
return require(codecNameOrCode)
66+
return import(codecNameOrCode)
6467
},
6568

6669
// either specify Multibase codecs as part of the `bases` list
6770
bases: [
68-
require('custom-multibase')
71+
customMultibase
6972
],
7073

7174
// and/or supply a function to load them dynamically
7275
loadBase: async (baseNameOrCode) => {
73-
return require(baseNameOrCode)
76+
return import(baseNameOrCode)
7477
},
7578

7679
// either specify Multihash hashers as part of the `hashers` list
7780
hashers: [
78-
require('custom-multibase')
81+
customMultihasher
7982
],
8083

8184
// and/or supply a function to load them dynamically
8285
loadHasher: async (hashNameOrCode) => {
83-
return require(hashNameOrCode)
86+
return import(hashNameOrCode)
8487
}
8588
}
8689
})
@@ -89,39 +92,42 @@ If your application requires support for extra codecs, you can configure them as
8992
2. Configure your IPFS HTTP API Client to support the codec. This is necessary so that the client can send the data to the IPFS node over HTTP:
9093

9194
```javascript
92-
const ipfsHttpClient = require('ipfs-http-client')
95+
import { create } from 'ipfs-http-client'
96+
import customBlockCodec from 'custom-blockcodec'
97+
import customMultibase from 'custom-multibase'
98+
import customMultihasher from 'custom-multihasher'
9399

94-
const client = ipfsHttpClient({
100+
const client = create({
95101
url: 'http://127.0.0.1:5002',
96102
ipld: {
97103
// either specify BlockCodecs as part of the `codecs` list
98104
codecs: [
99-
require('custom-blockcodec')
105+
customBlockCodec
100106
],
101107

102108
// and/or supply a function to load them dynamically
103109
loadCodec: async (codecNameOrCode) => {
104-
return require(codecNameOrCode)
110+
return import(codecNameOrCode)
105111
},
106112

107113
// either specify Multibase codecs as part of the `bases` list
108114
bases: [
109-
require('custom-multibase')
115+
customMultibase
110116
],
111117

112118
// and/or supply a function to load them dynamically
113119
loadBase: async (baseNameOrCode) => {
114-
return require(baseNameOrCode)
120+
return import(baseNameOrCode)
115121
},
116122

117123
// either specify Multihash hashers as part of the `hashers` list
118124
hashers: [
119-
require('custom-multibase')
125+
customMultihasher
120126
],
121127

122128
// and/or supply a function to load them dynamically
123129
loadHasher: async (hashNameOrCode) => {
124-
return require(hashNameOrCode)
130+
return import(hashNameOrCode)
125131
}
126132
}
127133
})

docs/MIGRATION-TO-ASYNC-AWAIT.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const peerId = PeerId.createFromB58String(peerIdStr)
100100
You can get hold of the `PeerId` class using npm or in a script tag:
101101

102102
```js
103-
const PeerId = require('peer-id')
103+
import PeerId from 'peer-id'
104104
const peerId = PeerId.createFromB58String(peerIdStr)
105105
```
106106

@@ -128,7 +128,7 @@ You can get hold of the `PeerInfo` class using npm or in a script tag:
128128

129129
```js
130130
const PeerInfo = require('peer-info')
131-
const PeerId = require('peer-id')
131+
import PeerId from 'peer-id'
132132
const peerInfo = new PeerInfo(PeerId.createFromB58String(info.id))
133133
info.addrs.forEach(addr => peerInfo.multiaddrs.add(addr))
134134
```
@@ -217,7 +217,7 @@ readable.on('end', () => {
217217
Becomes:
218218

219219
```js
220-
const toStream = require('it-to-stream')
220+
import toStream from 'it-to-stream'
221221
const readable = toStream.readable(ipfs.cat('QmHash'))
222222
const decoder = new TextDecoder()
223223

@@ -285,7 +285,7 @@ console.log(decoder.decode(data))
285285
...which, by the way, could more succinctly be written as:
286286

287287
```js
288-
const toBuffer = require('it-to-buffer')
288+
import toBuffer from 'it-to-buffer'
289289
const decoder = new TextDecoder()
290290
const data = await toBuffer(ipfs.cat('QmHash'))
291291
console.log(decoder.decode(data))
@@ -321,7 +321,7 @@ pipeline(
321321
Becomes:
322322

323323
```js
324-
const toStream = require('it-to-stream')
324+
import toStream from 'it-to-stream'
325325
const { pipeline, Writable } = require('stream')
326326
const decoder = new TextDecoder()
327327

@@ -353,7 +353,7 @@ Use `it-pipe` and a [for/await](https://developer.mozilla.org/en-US/docs/Web/Jav
353353
e.g.
354354

355355
```js
356-
const fs = require('fs')
356+
import fs from 'fs'
357357
const { pipeline } = require('stream')
358358

359359
const items = []
@@ -378,7 +378,7 @@ pipeline(
378378
Becomes:
379379

380380
```js
381-
const fs = require('fs')
381+
import fs from 'fs'
382382
const pipe = require('it-pipe')
383383

384384
const items = []
@@ -400,9 +400,9 @@ console.log(items)
400400
...which, by the way, could more succinctly be written as:
401401

402402
```js
403-
const fs = require('fs')
403+
import fs from 'fs'
404404
const pipe = require('it-pipe')
405-
const all = require('it-all')
405+
import all from 'it-all'
406406

407407
const items = await pipe(
408408
fs.createReadStream('/path/to/file'),
@@ -420,7 +420,7 @@ Convert the async iterable to a readable stream.
420420
e.g.
421421

422422
```js
423-
const fs = require('fs')
423+
import fs from 'fs'
424424
const { pipeline } = require('stream')
425425

426426
const items = []
@@ -445,8 +445,8 @@ pipeline(
445445
Becomes:
446446

447447
```js
448-
const toStream = require('it-to-stream')
449-
const fs = require('fs')
448+
import toStream from 'it-to-stream'
449+
import fs from 'fs'
450450
const { pipeline } = require('stream')
451451

452452
const items = []
@@ -568,7 +568,7 @@ Becomes:
568568

569569
```js
570570
const pipe = require('it-pipe')
571-
const concat = require('it-concat')
571+
import concat from 'it-concat'
572572
const decoder = new TextDecoder()
573573

574574
const data = await pipe(
@@ -590,7 +590,7 @@ Use `it-pipe` and `it-all` to collect all items from an async iterable.
590590
e.g.
591591

592592
```js
593-
const fs = require('fs')
593+
import fs from 'fs'
594594
const toPull = require('stream-to-pull-stream')
595595

596596
pull(
@@ -605,7 +605,7 @@ pull(
605605
Becomes:
606606

607607
```js
608-
const fs = require('fs')
608+
import fs from 'fs'
609609

610610
const file = await ipfs.add(fs.createReadStream('/path/to/file'))
611611

@@ -619,7 +619,7 @@ Convert the async iterable to a pull stream.
619619
e.g.
620620

621621
```js
622-
const fs = require('fs')
622+
import fs from 'fs'
623623
const toPull = require('stream-to-pull-stream')
624624

625625
pull(
@@ -634,7 +634,7 @@ pull(
634634
Becomes:
635635

636636
```js
637-
const fs = require('fs')
637+
import fs from 'fs'
638638
const streamToPull = require('stream-to-pull-stream')
639639
const itToPull = require('async-iterator-to-pull-stream')
640640

@@ -685,7 +685,7 @@ for await (const file of addSource) {
685685
Alternatively you can buffer up the results using the `it-all` utility:
686686

687687
```js
688-
const all = require('it-all')
688+
import all from 'it-all'
689689

690690
const results = await all(ipfs.addAll([
691691
{ path: 'root/1.txt', content: 'one' },
@@ -744,7 +744,7 @@ Reading files.
744744
e.g.
745745

746746
```js
747-
const fs = require('fs')
747+
import fs from 'fs'
748748

749749
const data = await ipfs.cat('/ipfs/QmHash')
750750

@@ -759,8 +759,8 @@ Becomes:
759759

760760
```js
761761
const pipe = require('it-pipe')
762-
const toIterable = require('stream-to-it')
763-
const fs = require('fs')
762+
import toIterable from 'stream-to-it'
763+
import fs from 'fs'
764764

765765
// Note that as chunks arrive they are written to the file and memory can be freed and re-used
766766
await pipe(
@@ -774,8 +774,8 @@ console.log('done')
774774
Alternatively you can buffer up the chunks using the `it-concat` utility (not recommended!):
775775

776776
```js
777-
const fs = require('fs')
778-
const concat = require('it-concat')
777+
import fs from 'fs'
778+
import concat from 'it-concat'
779779

780780
const data = await concat(ipfs.cat('/ipfs/QmHash'))
781781

@@ -812,7 +812,7 @@ for await (const file of filesSource) {
812812
Alternatively you can buffer up the directory listing using the `it-all` utility:
813813

814814
```js
815-
const all = require('it-all')
815+
import all from 'it-all'
816816

817817
const results = await all(ipfs.ls('/ipfs/QmHash'))
818818

@@ -905,7 +905,7 @@ files.forEach(file => {
905905
Becomes:
906906

907907
```js
908-
const fs = require('fs')
908+
import fs from 'fs'
909909
const ipfs = IpfsHttpClient()
910910

911911
const file = await ipfs.add(fs.createReadStream('/path/to/file.txt'))

0 commit comments

Comments
 (0)