Skip to content

Commit

Permalink
feat: switch to esm (#3879)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain authored Sep 22, 2021
1 parent 95c1fee commit 3dcf3ec
Show file tree
Hide file tree
Showing 187 changed files with 1,418 additions and 1,717 deletions.
2 changes: 1 addition & 1 deletion .aegir.js → .aegir.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
port: port
}, {
type: 'go',
ipfsHttpModule: require('./'),
ipfsHttpModule: await import('./src/index.js'),
ipfsBin: require('go-ipfs').path()
})

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ There are significant and breaking API changes in this release. Please see the [
* `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:

```js
const last = require('it-last')
import last from 'it-last'
await last(ipfs.name.resolve('/ipns/QmHash'))
```

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Alternatively it can be an object which may have the following keys:
#### Example

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

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

```js
const { create, globSource } = require('ipfs-http-client')
import { create, globSource } from 'ipfs-http-client'
const ipfs = create()

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

```js
const { create, urlSource } = require('ipfs-http-client')
import { create, urlSource } from 'ipfs-http-client'
const ipfs = create()

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

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

// connect to ipfs daemon API server
const ipfs = create('http://localhost:5001') // (the default in Node.js)
Expand Down
34 changes: 22 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@
"bugs": "https://github.com/ipfs/js-ipfs/issues",
"license": "(Apache-2.0 OR MIT)",
"leadMaintainer": "Alex Potsides <[email protected]>",
"type": "module",
"main": "src/index.js",
"types": "types/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/*.tsbuildinfo"
"*",
"!**/*.tsbuildinfo"
],
"main": "src/index.js",
"types": "./dist/src/index.d.ts",
"exports": {
".": {
"import": "./src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"publishConfig": {
"directory": "dist"
},
"browser": {
"./src/lib/multipart-request.js": "./src/lib/multipart-request.browser.js",
"ipfs-utils/src/files/glob-source": false,
"go-ipfs": false,
"ipfs-core-utils/src/files/normalise-input": "ipfs-core-utils/src/files/normalise-input/index.browser.js",
Expand All @@ -34,8 +47,8 @@
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"test:webworker": "aegir test -t webworker",
"test:electron-main": "aegir test -t electron-main",
"test:electron-renderer": "aegir test -t electron-renderer",
"test:electron-main": "aegir build --esm-tests && aegir test -t electron-main -f ./dist/cjs/node-test/*.spec.js",
"test:electron-renderer": "aegir build --esm-tests && aegir test -t electron-renderer -f ./dist/cjs/browser-test/*.spec.js",
"lint": "aegir ts -p check && aegir lint",
"clean": "rimraf ./dist",
"dep-check": "aegir dep-check -i ipfs-core -i rimraf -i ipfs-core-types -i abort-controller"
Expand All @@ -47,24 +60,21 @@
"any-signal": "^2.1.2",
"debug": "^4.1.1",
"err-code": "^3.0.1",
"form-data": "^4.0.0",
"ipfs-core-types": "^0.7.3",
"ipfs-core-utils": "^0.10.5",
"ipfs-utils": "^8.1.4",
"it-first": "^1.0.6",
"it-last": "^1.0.4",
"it-to-stream": "^1.0.0",
"merge-options": "^3.0.4",
"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": "^3.0.0"
},
"devDependencies": {
"aegir": "^35.0.3",
"aegir": "^35.1.0",
"delay": "^5.0.0",
"go-ipfs": "0.9.1",
"ipfsd-ctl": "^10.0.3",
Expand Down
19 changes: 9 additions & 10 deletions src/add-all.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict'

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

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

module.exports = configure((api) => {
export const createAddAll = configure((api) => {
/**
* @type {RootAPI["addAll"]}
*/
Expand Down Expand Up @@ -50,7 +49,7 @@ module.exports = configure((api) => {
})

for await (let file of res.ndjson()) {
file = toCamel(file)
file = objectToCamel(file)

if (file.hash !== undefined) {
yield toCoreInterface(file)
Expand Down
11 changes: 5 additions & 6 deletions src/add.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const addAll = require('./add-all')
const last = require('it-last')
const configure = require('./lib/configure')
import { createAddAll } from './add-all.js'
import last from 'it-last'
import { configure } from './lib/configure.js'

/**
* @typedef {import('./types').HTTPClientExtraOptions} HTTPClientExtraOptions
Expand All @@ -12,8 +11,8 @@ const configure = require('./lib/configure')
/**
* @param {import('./types').Options} options
*/
module.exports = (options) => {
const all = addAll(options)
export function createAdd (options) {
const all = createAddAll(options)
return configure(() => {
/**
* @type {RootAPI["add"]}
Expand Down
19 changes: 12 additions & 7 deletions src/bitswap/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict'
import { createWantlist } from './wantlist.js'
import { createWantlistForPeer } from './wantlist-for-peer.js'
import { createStat } from './stat.js'
import { createUnwant } from './unwant.js'

/**
* @param {import('../types').Options} config
*/
module.exports = config => ({
wantlist: require('./wantlist')(config),
wantlistForPeer: require('./wantlist-for-peer')(config),
stat: require('./stat')(config),
unwant: require('./unwant')(config)
})
export function createBitswap (config) {
return {
wantlist: createWantlist(config),
wantlistForPeer: createWantlistForPeer(config),
unwant: createUnwant(config),
stat: createStat(config)
}
}
9 changes: 4 additions & 5 deletions src/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict'

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

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

module.exports = configure(api => {
export const createStat = configure(api => {
/**
* @type {BitswapAPI["stat"]}
*/
Expand Down
7 changes: 3 additions & 4 deletions src/bitswap/unwant.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

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

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

module.exports = configure(api => {
export const createUnwant = configure(api => {
/**
* @type {BitswapAPI["unwant"]}
*/
Expand Down
9 changes: 4 additions & 5 deletions src/bitswap/wantlist-for-peer.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict'

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

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

module.exports = configure(api => {
export const createWantlistForPeer = configure(api => {
/**
* @type {BitswapAPI["wantlistForPeer"]}
*/
Expand Down
9 changes: 4 additions & 5 deletions src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict'

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

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

module.exports = configure(api => {
export const createWantlist = configure(api => {
/**
* @type {BitswapAPI["wantlist"]}
*/
Expand Down
7 changes: 3 additions & 4 deletions src/block/get.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

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

/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/block').API<HTTPClientExtraOptions>} BlockAPI
*/

module.exports = configure(api => {
export const createGet = configure(api => {
/**
* @type {BlockAPI["get"]}
*/
Expand Down
20 changes: 13 additions & 7 deletions src/block/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
'use strict'

import { createGet } from './get.js'
import { createPut } from './put.js'
import { createRm } from './rm.js'
import { createStat } from './stat.js'

/**
* @param {import('../types').Options} config
*/
module.exports = config => ({
get: require('./get')(config),
stat: require('./stat')(config),
put: require('./put')(config),
rm: require('./rm')(config)
})
export function createBlock (config) {
return {
get: createGet(config),
put: createPut(config),
rm: createRm(config),
stat: createStat(config)
}
}
17 changes: 8 additions & 9 deletions src/block/put.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
'use strict'

const { CID } = require('multiformats/cid')
const multipartRequest = require('../lib/multipart-request')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')
const abortSignal = require('../lib/abort-signal')
const { AbortController } = require('native-abort-controller')
import { CID } from 'multiformats/cid'
import { multipartRequest } from 'ipfs-core-utils/multipart-request'
import { configure } from '../lib/configure.js'
import { toUrlSearchParams } from '../lib/to-url-search-params.js'
import { abortSignal } from '../lib/abort-signal.js'
import { AbortController } from 'native-abort-controller'

/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/block').API<HTTPClientExtraOptions>} BlockAPI
*/

module.exports = configure(api => {
export const createPut = configure(api => {
/**
* @type {BlockAPI["put"]}
*/
Expand All @@ -31,7 +30,7 @@ module.exports = configure(api => {
)
})
res = await response.json()
} catch (err) {
} catch (/** @type {any} */ err) {
// Retry with "protobuf"/"cbor" format for go-ipfs
// TODO: remove when https://github.com/ipfs/go-cid/issues/75 resolved
if (options.format === 'dag-pb') {
Expand Down
9 changes: 4 additions & 5 deletions src/block/rm.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict'

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

/**
* @typedef {import('../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/block').API<HTTPClientExtraOptions>} BlockAPI
* @typedef {import('ipfs-core-types/src/block').RmResult} RmResult
*/

module.exports = configure(api => {
export const createRm = configure(api => {
/**
* @type {BlockAPI["rm"]}
*/
Expand Down
Loading

0 comments on commit 3dcf3ec

Please sign in to comment.