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

Commit 4376121

Browse files
hugomrdiasAlan Shaw
authored andcommitted
fix: remove non default ipld formats in the browser (#1980)
BREAKING CHANGE: Browser application bundles now include only `ipld-dag-pb`, `ipld-dag-cbor` and `ipld-raw` IPLD codecs. Other codecs should be added manually, see https://github.com/ipfs/js-ipfs/blob/master/README.md#optionsipld for details. * In Node.js `require('ipfs')` * all IPLD formats included * In browser application bundle `require('ipfs')` bundled with webpack/browserify/etc. * only `ipld-dag-pb`, `ipld-dag-cbor` and `ipld-raw` included * CDN bundle `<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>` * all IPLD formats included Co-Authored-By: hugomrdias <mail@hugodias.me>
1 parent 5f62e99 commit 4376121

File tree

7 files changed

+245
-43
lines changed

7 files changed

+245
-43
lines changed

.aegir.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const preloadNode = MockPreloadNode.createNode()
1010
module.exports = {
1111
webpack: {
1212
resolve: {
13-
mainFields: ['browser', 'main']
13+
mainFields: ['browser', 'main'],
14+
aliasFields: ['browser', 'browser-all-ipld-formats'],
1415
}
1516
},
1617
karma: {

README.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,146 @@ Enable and configure experimental features.
326326
327327
Modify the default IPFS node config. This object will be *merged* with the default config; it will not replace it.
328328
329+
##### `options.ipld`
330+
331+
| Type | Default |
332+
|------|---------|
333+
| object | [`ipld-nodejs.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/ipld-nodejs.js) in Node.js, [`ipld-browser.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/ipld-browser.js) in browsers |
334+
335+
Modify the default IPLD config. This object will be *merged* with the default config; it will not replace it. Check IPLD [docs](https://github.com/ipld/js-ipld#ipld-constructor) for more information on the available options.
336+
337+
> Browser config does **NOT** include by default all the IPLD formats. Only `ipld-dag-pb`, `ipld-dag-cbor` and `ipld-raw` are included.
338+
339+
To add support for other formats we provide two options, one sync and another async.
340+
341+
Examples for the sync option:
342+
343+
<details><summary>ESM Environments</summary>
344+
345+
```js
346+
import ipldGit from 'ipld-git'
347+
import ipldBitcoin from 'ipld-bitcoin'
348+
349+
const node = new IPFS(
350+
{
351+
ipld: {
352+
formats: [ipldGit, ipldBitcoin]
353+
}
354+
}
355+
)
356+
```
357+
</details>
358+
<details><summary>Commonjs Environments</summary>
359+
360+
```js
361+
const node = new IPFS(
362+
{
363+
ipld: {
364+
formats: [require('ipld-git'), require('ipld-bitcoin')]
365+
}
366+
}
367+
)
368+
```
369+
</details>
370+
<details><summary>Using script tags</summary>
371+
372+
```html
373+
<script src="https://unpkg.com/ipfs"></script>
374+
<script src="https://unpkg.com/ipld-git"></script>
375+
<script src="https://unpkg.com/ipld-bitcoin"></script>
376+
<script>
377+
const node = new self.IPFS(
378+
{
379+
ipld: {
380+
formats: [self.ipldGit, self.ipldBitcoin]
381+
}
382+
}
383+
)
384+
</script>
385+
```
386+
</details>
387+
388+
Examples for the async option:
389+
390+
391+
<details><summary>ESM Environments</summary>
392+
393+
```js
394+
const node = new IPFS(
395+
{
396+
ipld: {
397+
async loadFormat (codec) {
398+
if (codec === 'git-raw') {
399+
return import('ipld-git') // This is a dynamic import
400+
} else {
401+
throw new Error('unable to load format ' + multicodec.print[codec])
402+
}
403+
}
404+
}
405+
}
406+
)
407+
```
408+
> For more information about dynamic imports please check [webpack docs](https://webpack.js.org/guides/code-splitting/#dynamic-imports) or search your bundler documention.
409+
410+
Using dynamic imports will tell your bundler to create a separate file (normally called *chunk*) that will **only** be requested by the browser if it's really needed. This strategy will reduce your bundle size and load times without removing any functionality.
411+
412+
With Webpack IPLD formats can even be grouped together using magic comments `import(/* webpackChunkName: "ipld-formats" */ 'ipld-git')` to produce a single file with all of them.
413+
414+
</details>
415+
<details><summary>Commonjs Environments</summary>
416+
417+
```js
418+
const node = new IPFS(
419+
{
420+
ipld: {
421+
async loadFormat (codec) {
422+
if (codec === 'git-raw') {
423+
return require('ipld-git')
424+
} else {
425+
throw new Error('unable to load format ' + multicodec.print[codec])
426+
}
427+
}
428+
}
429+
}
430+
)
431+
```
432+
</details>
433+
434+
<details><summary>Using Script tags</summary>
435+
436+
```js
437+
<script src="https://unpkg.com/ipfs"></script>
438+
<script>
439+
440+
const load = (url, cb) => {
441+
const script = document.createElement('script')
442+
script.src = url
443+
script.onload = () => cb()
444+
script.onerror = () => cb(new Error('Unable to load script'))
445+
document.body.appendChild(script);
446+
};
447+
448+
const node = new self.IPFS(
449+
{
450+
ipld: {
451+
loadFormat (codec, cb) {
452+
switch (codec) {
453+
case 'git-raw':
454+
return load('https://unpkg.com/ipld-git', cb)
455+
case 'bitcoin-block':
456+
return load('https://unpkg.com/ipld-bitcoin', cb)
457+
default:
458+
throw new Error('unable to load format ' + multicodec.print[codec])
459+
}
460+
}
461+
}
462+
}
463+
)
464+
</script>
465+
```
466+
</details>
467+
468+
329469
##### `options.libp2p`
330470

331471
| Type | Default |

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
"./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js",
1717
"./src/core/runtime/preload-nodejs.js": "./src/core/runtime/preload-browser.js",
1818
"./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js",
19+
"./src/core/runtime/ipld-nodejs.js": "./src/core/runtime/ipld-browser.js",
1920
"./test/utils/create-repo-nodejs.js": "./test/utils/create-repo-browser.js",
2021
"stream": "readable-stream",
2122
"joi": "joi-browser"
2223
},
24+
"browser-all-ipld-formats": {
25+
"./src/core/runtime/ipld-browser.js": "./src/core/runtime/ipld-browser-all.js"
26+
},
2327
"engines": {
2428
"node": ">=10.0.0",
2529
"npm": ">=6.0.0"
@@ -115,9 +119,11 @@
115119
"ipfs-unixfs-importer": "~0.38.5",
116120
"ipld": "~0.21.1",
117121
"ipld-bitcoin": "~0.1.8",
122+
"ipld-dag-cbor": "~0.13.1",
118123
"ipld-dag-pb": "~0.15.3",
119124
"ipld-ethereum": "^2.0.1",
120125
"ipld-git": "~0.3.0",
126+
"ipld-raw": "^2.0.1",
121127
"ipld-zcash": "~0.1.6",
122128
"ipns": "~0.5.0",
123129
"is-ipfs": "~0.6.0",

src/core/index.js

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,7 @@ const components = require('./components')
2525
const defaultRepo = require('./runtime/repo-nodejs')
2626
const preload = require('./preload')
2727
const mfsPreload = require('./mfs-preload')
28-
29-
// All known (non-default) IPLD formats
30-
const IpldFormats = {
31-
get 'bitcoin-block' () {
32-
return require('ipld-bitcoin')
33-
},
34-
get 'eth-account-snapshot' () {
35-
return require('ipld-ethereum').ethAccountSnapshot
36-
},
37-
get 'eth-block' () {
38-
return require('ipld-ethereum').ethBlock
39-
},
40-
get 'eth-block-list' () {
41-
return require('ipld-ethereum').ethBlockList
42-
},
43-
get 'eth-state-trie' () {
44-
return require('ipld-ethereum').ethStateTrie
45-
},
46-
get 'eth-storage-trie' () {
47-
return require('ipld-ethereum').ethStorageTrie
48-
},
49-
get 'eth-tx' () {
50-
return require('ipld-ethereum').ethTx
51-
},
52-
get 'eth-tx-trie' () {
53-
return require('ipld-ethereum').ethTxTrie
54-
},
55-
get 'git-raw' () {
56-
return require('ipld-git')
57-
},
58-
get 'zcash-block' () {
59-
return require('ipld-zcash')
60-
}
61-
}
28+
const ipldOptions = require('./runtime/ipld-nodejs')
6229

6330
class IPFS extends EventEmitter {
6431
constructor (options) {
@@ -106,14 +73,7 @@ class IPFS extends EventEmitter {
10673
this._peerInfo = undefined
10774
this._bitswap = undefined
10875
this._blockService = new BlockService(this._repo)
109-
this._ipld = new Ipld({
110-
blockService: this._blockService,
111-
loadFormat: (codec, callback) => {
112-
this.log('Loading IPLD format', codec)
113-
if (IpldFormats[codec]) return callback(null, IpldFormats[codec])
114-
callback(new Error(`Missing IPLD format "${codec}"`))
115-
}
116-
})
76+
this._ipld = new Ipld(ipldOptions(this._blockService, this._options.ipld, this.log))
11777
this._preload = preload(this)
11878
this._mfsPreload = mfsPreload(this)
11979
this._ipns = undefined

src/core/runtime/ipld-browser-all.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
const mergeOptions = require('merge-options')
3+
4+
module.exports = (blockService, options = {}) => {
5+
return mergeOptions.call(
6+
// ensure we have the defaults formats even if the user overrides `formats: []`
7+
{ concatArrays: true },
8+
{
9+
blockService: blockService,
10+
formats: [
11+
require('ipld-dag-cbor'),
12+
require('ipld-dag-pb'),
13+
require('ipld-raw'),
14+
require('ipld-bitcoin'),
15+
require('ipld-ethereum').ethAccountSnapshot,
16+
require('ipld-ethereum').ethBlock,
17+
require('ipld-ethereum').ethBlockList,
18+
require('ipld-ethereum').ethStateTrie,
19+
require('ipld-ethereum').ethStorageTrie,
20+
require('ipld-ethereum').ethTx,
21+
require('ipld-ethereum').ethTxTrie,
22+
require('ipld-git'),
23+
require('ipld-zcash')
24+
]
25+
}, options)
26+
}

src/core/runtime/ipld-browser.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
const mergeOptions = require('merge-options')
3+
const ipldDagCbor = require('ipld-dag-cbor')
4+
const ipldDagPb = require('ipld-dag-pb')
5+
const ipldRaw = require('ipld-raw')
6+
7+
module.exports = (blockService, options = {}) => {
8+
return mergeOptions.call(
9+
// ensure we have the defaults formats even if the user overrides `formats: []`
10+
{ concatArrays: true },
11+
{
12+
blockService: blockService,
13+
formats: [ipldDagCbor, ipldDagPb, ipldRaw]
14+
}, options)
15+
}

src/core/runtime/ipld-nodejs.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict'
2+
const mergeOptions = require('merge-options')
3+
const ipldDagCbor = require('ipld-dag-cbor')
4+
const ipldDagPb = require('ipld-dag-pb')
5+
const ipldRaw = require('ipld-raw')
6+
7+
// All known (non-default) IPLD formats
8+
const IpldFormats = {
9+
get 'bitcoin-block' () {
10+
return require('ipld-bitcoin')
11+
},
12+
get 'eth-account-snapshot' () {
13+
return require('ipld-ethereum').ethAccountSnapshot
14+
},
15+
get 'eth-block' () {
16+
return require('ipld-ethereum').ethBlock
17+
},
18+
get 'eth-block-list' () {
19+
return require('ipld-ethereum').ethBlockList
20+
},
21+
get 'eth-state-trie' () {
22+
return require('ipld-ethereum').ethStateTrie
23+
},
24+
get 'eth-storage-trie' () {
25+
return require('ipld-ethereum').ethStorageTrie
26+
},
27+
get 'eth-tx' () {
28+
return require('ipld-ethereum').ethTx
29+
},
30+
get 'eth-tx-trie' () {
31+
return require('ipld-ethereum').ethTxTrie
32+
},
33+
get 'git-raw' () {
34+
return require('ipld-git')
35+
},
36+
get 'zcash-block' () {
37+
return require('ipld-zcash')
38+
}
39+
}
40+
41+
module.exports = (blockService, options = {}, log) => {
42+
return mergeOptions.call(
43+
// ensure we have the defaults formats even if the user overrides `formats: []`
44+
{ concatArrays: true },
45+
{
46+
blockService: blockService,
47+
formats: [ipldDagCbor, ipldDagPb, ipldRaw],
48+
loadFormat: (codec, callback) => {
49+
log('Loading IPLD format', codec)
50+
if (IpldFormats[codec]) return callback(null, IpldFormats[codec])
51+
callback(new Error(`Missing IPLD format "${codec}"`))
52+
}
53+
}, options)
54+
}

0 commit comments

Comments
 (0)