Skip to content

Commit 3dcf3ec

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 95c1fee commit 3dcf3ec

File tree

187 files changed

+1418
-1717
lines changed

Some content is hidden

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

187 files changed

+1418
-1717
lines changed

.aegir.js renamed to .aegir.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
port: port
1717
}, {
1818
type: 'go',
19-
ipfsHttpModule: require('./'),
19+
ipfsHttpModule: await import('./src/index.js'),
2020
ipfsBin: require('go-ipfs').path()
2121
})
2222

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ There are significant and breaking API changes in this release. Please see the [
664664
* `name.resolve` now returns an async iterable. It yields increasingly more accurate resolved values as they are discovered until the best value is selected from the quorum of 16. The "best" resolved value is the last item yielded from the iterator. If you are interested only in this best value you could use `it-last` to extract it like so:
665665

666666
```js
667-
const last = require('it-last')
667+
import last from 'it-last'
668668
await last(ipfs.name.resolve('/ipns/QmHash'))
669669
```
670670

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Alternatively it can be an object which may have the following keys:
124124
#### Example
125125

126126
```JavaScript
127-
const { create } = require('ipfs-http-client')
127+
import { create } from 'ipfs-http-client'
128128

129129
// connect to the default API address http://localhost:5001
130130
const client = create()
@@ -195,7 +195,7 @@ Returns an async iterable that yields `{ path, content }` objects suitable for p
195195
##### Example
196196

197197
```js
198-
const { create, globSource } = require('ipfs-http-client')
198+
import { create, globSource } from 'ipfs-http-client'
199199
const ipfs = create()
200200

201201
const file = await ipfs.add(globSource('./docs', { recursive: true }))
@@ -229,7 +229,7 @@ Returns an async iterable that yields `{ path, content }` objects suitable for p
229229
##### Example
230230

231231
```js
232-
const { create, urlSource } = require('ipfs-http-client')
232+
import { create, urlSource } from 'ipfs-http-client'
233233
const ipfs = create()
234234

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

265265
```javascript
266-
const { create } = require('ipfs-http-client')
266+
import { create } from 'ipfs-http-client'
267267

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

package.json

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,28 @@
99
"bugs": "https://github.com/ipfs/js-ipfs/issues",
1010
"license": "(Apache-2.0 OR MIT)",
1111
"leadMaintainer": "Alex Potsides <[email protected]>",
12+
"type": "module",
13+
"main": "src/index.js",
14+
"types": "types/src/index.d.ts",
1215
"files": [
13-
"src",
14-
"dist",
15-
"!dist/*.tsbuildinfo"
16+
"*",
17+
"!**/*.tsbuildinfo"
1618
],
17-
"main": "src/index.js",
18-
"types": "./dist/src/index.d.ts",
19+
"exports": {
20+
".": {
21+
"import": "./src/index.js"
22+
}
23+
},
24+
"eslintConfig": {
25+
"extends": "ipfs",
26+
"parserOptions": {
27+
"sourceType": "module"
28+
}
29+
},
30+
"publishConfig": {
31+
"directory": "dist"
32+
},
1933
"browser": {
20-
"./src/lib/multipart-request.js": "./src/lib/multipart-request.browser.js",
2134
"ipfs-utils/src/files/glob-source": false,
2235
"go-ipfs": false,
2336
"ipfs-core-utils/src/files/normalise-input": "ipfs-core-utils/src/files/normalise-input/index.browser.js",
@@ -34,8 +47,8 @@
3447
"test:node": "aegir test -t node",
3548
"test:browser": "aegir test -t browser",
3649
"test:webworker": "aegir test -t webworker",
37-
"test:electron-main": "aegir test -t electron-main",
38-
"test:electron-renderer": "aegir test -t electron-renderer",
50+
"test:electron-main": "aegir build --esm-tests && aegir test -t electron-main -f ./dist/cjs/node-test/*.spec.js",
51+
"test:electron-renderer": "aegir build --esm-tests && aegir test -t electron-renderer -f ./dist/cjs/browser-test/*.spec.js",
3952
"lint": "aegir ts -p check && aegir lint",
4053
"clean": "rimraf ./dist",
4154
"dep-check": "aegir dep-check -i ipfs-core -i rimraf -i ipfs-core-types -i abort-controller"
@@ -47,24 +60,21 @@
4760
"any-signal": "^2.1.2",
4861
"debug": "^4.1.1",
4962
"err-code": "^3.0.1",
50-
"form-data": "^4.0.0",
5163
"ipfs-core-types": "^0.7.3",
5264
"ipfs-core-utils": "^0.10.5",
5365
"ipfs-utils": "^8.1.4",
5466
"it-first": "^1.0.6",
5567
"it-last": "^1.0.4",
56-
"it-to-stream": "^1.0.0",
5768
"merge-options": "^3.0.4",
5869
"multiaddr": "^10.0.0",
5970
"multiformats": "^9.4.1",
60-
"nanoid": "^3.1.12",
6171
"native-abort-controller": "^1.0.3",
6272
"parse-duration": "^1.0.0",
6373
"stream-to-it": "^0.2.2",
6474
"uint8arrays": "^3.0.0"
6575
},
6676
"devDependencies": {
67-
"aegir": "^35.0.3",
77+
"aegir": "^35.1.0",
6878
"delay": "^5.0.0",
6979
"go-ipfs": "0.9.1",
7080
"ipfsd-ctl": "^10.0.3",

src/add-all.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
'use strict'
21

3-
const { CID } = require('multiformats/cid')
4-
const toCamel = require('./lib/object-to-camel')
5-
const configure = require('./lib/configure')
6-
const multipartRequest = require('./lib/multipart-request')
7-
const toUrlSearchParams = require('./lib/to-url-search-params')
8-
const abortSignal = require('./lib/abort-signal')
9-
const { AbortController } = require('native-abort-controller')
2+
import { CID } from 'multiformats/cid'
3+
import { objectToCamel } from './lib/object-to-camel.js'
4+
import { configure } from './lib/configure.js'
5+
import { multipartRequest } from 'ipfs-core-utils/multipart-request'
6+
import { toUrlSearchParams } from './lib/to-url-search-params.js'
7+
import { abortSignal } from './lib/abort-signal.js'
8+
import { AbortController } from 'native-abort-controller'
109

1110
/**
1211
* @typedef {import('ipfs-utils/src/types').ProgressFn} IPFSUtilsHttpUploadProgressFn
@@ -16,7 +15,7 @@ const { AbortController } = require('native-abort-controller')
1615
* @typedef {import('ipfs-core-types/src/root').AddResult} AddResult
1716
*/
1817

19-
module.exports = configure((api) => {
18+
export const createAddAll = configure((api) => {
2019
/**
2120
* @type {RootAPI["addAll"]}
2221
*/
@@ -50,7 +49,7 @@ module.exports = configure((api) => {
5049
})
5150

5251
for await (let file of res.ndjson()) {
53-
file = toCamel(file)
52+
file = objectToCamel(file)
5453

5554
if (file.hash !== undefined) {
5655
yield toCoreInterface(file)

src/add.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
'use strict'
21

3-
const addAll = require('./add-all')
4-
const last = require('it-last')
5-
const configure = require('./lib/configure')
2+
import { createAddAll } from './add-all.js'
3+
import last from 'it-last'
4+
import { configure } from './lib/configure.js'
65

76
/**
87
* @typedef {import('./types').HTTPClientExtraOptions} HTTPClientExtraOptions
@@ -12,8 +11,8 @@ const configure = require('./lib/configure')
1211
/**
1312
* @param {import('./types').Options} options
1413
*/
15-
module.exports = (options) => {
16-
const all = addAll(options)
14+
export function createAdd (options) {
15+
const all = createAddAll(options)
1716
return configure(() => {
1817
/**
1918
* @type {RootAPI["add"]}

src/bitswap/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
'use strict'
1+
import { createWantlist } from './wantlist.js'
2+
import { createWantlistForPeer } from './wantlist-for-peer.js'
3+
import { createStat } from './stat.js'
4+
import { createUnwant } from './unwant.js'
25

36
/**
47
* @param {import('../types').Options} config
58
*/
6-
module.exports = config => ({
7-
wantlist: require('./wantlist')(config),
8-
wantlistForPeer: require('./wantlist-for-peer')(config),
9-
stat: require('./stat')(config),
10-
unwant: require('./unwant')(config)
11-
})
9+
export function createBitswap (config) {
10+
return {
11+
wantlist: createWantlist(config),
12+
wantlistForPeer: createWantlistForPeer(config),
13+
unwant: createUnwant(config),
14+
stat: createStat(config)
15+
}
16+
}

src/bitswap/stat.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
'use strict'
21

3-
const { CID } = require('multiformats/cid')
4-
const configure = require('../lib/configure')
5-
const toUrlSearchParams = require('../lib/to-url-search-params')
2+
import { CID } from 'multiformats/cid'
3+
import { configure } from '../lib/configure.js'
4+
import { toUrlSearchParams } from '../lib/to-url-search-params.js'
65

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

12-
module.exports = configure(api => {
11+
export const createStat = configure(api => {
1312
/**
1413
* @type {BitswapAPI["stat"]}
1514
*/

src/bitswap/unwant.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
'use strict'
21

3-
const configure = require('../lib/configure')
4-
const toUrlSearchParams = require('../lib/to-url-search-params')
2+
import { configure } from '../lib/configure.js'
3+
import { toUrlSearchParams } from '../lib/to-url-search-params.js'
54

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

11-
module.exports = configure(api => {
10+
export const createUnwant = configure(api => {
1211
/**
1312
* @type {BitswapAPI["unwant"]}
1413
*/

src/bitswap/wantlist-for-peer.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
'use strict'
21

3-
const { CID } = require('multiformats/cid')
4-
const configure = require('../lib/configure')
5-
const toUrlSearchParams = require('../lib/to-url-search-params')
2+
import { CID } from 'multiformats/cid'
3+
import { configure } from '../lib/configure.js'
4+
import { toUrlSearchParams } from '../lib/to-url-search-params.js'
65

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

12-
module.exports = configure(api => {
11+
export const createWantlistForPeer = configure(api => {
1312
/**
1413
* @type {BitswapAPI["wantlistForPeer"]}
1514
*/

0 commit comments

Comments
 (0)