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

Commit 64823c7

Browse files
authored
feat/no wrtc by default (#884)
* feat: no wrtc by default, support multiple WebRTC impl for Node.js
1 parent fcef848 commit 64823c7

File tree

12 files changed

+191
-63
lines changed

12 files changed

+191
-63
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
**/node_modules/
22
**/*.log
33
test/repo-tests*
4+
**/bundle.js
45

56
# Logs
67
logs
@@ -35,4 +36,4 @@ lib
3536
dist
3637
test/test-data/go-ipfs-repo/LOCK
3738
test/test-data/go-ipfs-repo/LOG
38-
test/test-data/go-ipfs-repo/LOG.old
39+
test/test-data/go-ipfs-repo/LOG.old

README.md

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,28 @@
77
<h3 align="center">The JavaScript implementation of the IPFS protocol.</h3>
88

99
<p align="center">
10-
<a href="http://ipn.io">
11-
<img src="https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square" />
12-
</a>
13-
<a href="http://ipfs.io/">
14-
<img src="https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square" />
15-
</a>
16-
<a href="http://webchat.freenode.net/?channels=%23ipfs">
17-
<img src="https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square" />
18-
</a>
10+
<a href="http://ipn.io"><img src="https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square" /></a>
11+
<a href="http://ipfs.io/"><img src="https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square" /></a>
12+
<a href="http://webchat.freenode.net/?channels=%23ipfs"><img src="https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square" /></a>
1913
<br>
20-
<a href="https://waffle.io/ipfs/js-ipfs">
21-
<img src="https://img.shields.io/badge/pm-waffle-blue.svg?style=flat-square" />
22-
</a>
23-
<a href="https://github.com/ipfs/interface-ipfs-core">
24-
<img src="https://img.shields.io/badge/interface--ipfs--core-API%20Docs-blue.svg">
25-
</a>
26-
<a href="https://github.com/ipfs/interface-ipfs-core/issues/55">
27-
<img src="https://img.shields.io/badge/interface--ipfs--core-Updates-blue.svg">
28-
</a>
14+
<a href="https://waffle.io/ipfs/js-ipfs"><img src="https://img.shields.io/badge/pm-waffle-blue.svg?style=flat-square" /></a>
15+
<a href="https://github.com/ipfs/interface-ipfs-core"><img src="https://img.shields.io/badge/interface--ipfs--core-API%20Docs-blue.svg"></a>
16+
<a href="https://github.com/ipfs/interface-ipfs-core/issues/55"><img src="https://img.shields.io/badge/interface--ipfs--core-Updates-blue.svg"></a>
2917
</p>
3018

3119
<p align="center">
32-
<a href="https://travis-ci.org/ipfs/js-ipfs">
33-
<img src="https://travis-ci.org/ipfs/js-ipfs.svg?branch=master" />
34-
</a>
35-
<a href="https://circleci.com/gh/ipfs/js-ipfs">
36-
<img src="https://circleci.com/gh/ipfs/js-ipfs.svg?style=svg" />
37-
</a>
38-
<a href="https://coveralls.io/github/ipfs/js-ipfs?branch=master">
39-
<img src="https://coveralls.io/repos/github/ipfs/js-ipfs/badge.svg?branch=master">
40-
</a>
20+
<a href="https://travis-ci.org/ipfs/js-ipfs"><img src="https://travis-ci.org/ipfs/js-ipfs.svg?branch=master" /></a>
21+
<a href="https://circleci.com/gh/ipfs/js-ipfs"><img src="https://circleci.com/gh/ipfs/js-ipfs.svg?style=svg" /></a>
22+
<a href="https://coveralls.io/github/ipfs/js-ipfs?branch=master"><img src="https://coveralls.io/repos/github/ipfs/js-ipfs/badge.svg?branch=master"></a>
4123
<br>
42-
<a href="https://david-dm.org/ipfs/js-ipfs">
43-
<img src="https://david-dm.org/ipfs/js-ipfs.svg?style=flat-square" />
44-
</a>
45-
<a href="https://github.com/feross/standard">
46-
<img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square">
47-
</a>
48-
<a href="https://github.com/RichardLitt/standard-readme">
49-
<img src="https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square" />
50-
</a>
51-
<a href="">
52-
<img src="https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square" />
53-
</a>
54-
<a href="">
55-
<img src="https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square" />
56-
</a>
24+
<a href="https://david-dm.org/ipfs/js-ipfs"><img src="https://david-dm.org/ipfs/js-ipfs.svg?style=flat-square" /></a>
25+
<a href="https://github.com/feross/standard"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square"></a>
26+
<a href="https://github.com/RichardLitt/standard-readme"><img src="https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square" /></a>
27+
<a href=""><img src="https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square" /></a>
28+
<a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square" /></a>
5729
<br>
5830
<!-- Hidding this until we have SauceLabs situation figured out, right now it is just misleading
59-
<a href="https://saucelabs.com/u/js-ipfs">
60-
<img src="https://saucelabs.com/browser-matrix/js-ipfs.svg" />
61-
</a>
62-
-->
31+
<a href="https://saucelabs.com/u/js-ipfs"><img src="https://saucelabs.com/browser-matrix/js-ipfs.svg" /></a> -->
6332
</p>
6433

6534
### Project status
@@ -138,18 +107,17 @@ In order to use js-ipfs as a CLI, you must install it with the `global` flag. Ru
138107
$ npm install ipfs --global
139108
```
140109

141-
The CLI is available by using the command `jsipfs` in your terminal. This is aliased, instead of using `ipfs`, to make sure it does not conflict with the Go implementation.
142-
143-
### Use in the browser with browserify, webpack or any bundler
110+
The CLI is available by using the command `jsipfs` in your terminal. This is aliased, instead of using `ipfs`, to make sure it does not conflict with the [Go implementation](https://github.com/ipfs/go-ipfs).
144111

145-
You can find examples of how to do this bundling at: `https://github.com/ipfs/js-ipfs/tree/master/examples`. If you are using webpack, make sure to use version 2 or above, otherwise it won't work.
112+
### Use in the browser
146113

147-
### Use in a browser using a script tag
114+
##### With browserify, webpack or any bundler
148115

149-
Loading this module in a browser (using a `<script>` tag) makes the `Ipfs` object available in the global namespace.
116+
Find examples of how to do this bundling at: `https://github.com/ipfs/js-ipfs/tree/master/examples`. If you are using webpack, make sure to use version 2 or above, otherwise it won't work.
150117

151-
The last published version of the package become [available for download](https://unpkg.com/ipfs/dist/) from [unpkg](https://unpkg.com/) and thus you may use it as the source:
118+
##### With script tag
152119

120+
Loading this module in a browser (using a `<script>` tag) makes the `Ipfs` object available in the global namespace. The last published version of the package become [available for download](https://unpkg.com/ipfs/dist/) from [unpkg](https://unpkg.com/) and thus you may use it as the source:
153121

154122
```html
155123
<!-- loading the minified version -->
@@ -186,6 +154,14 @@ Commands:
186154
- default API port: `5002`
187155
- default Bootstrap is off, to enable it set `IPFS_BOOTSTRAP=1`
188156

157+
If you want to use WebRTC in your local daemon, you will need to install it separatly by installing globally one of the following modules:
158+
159+
- [wrtc](https://npmjs.org/wrtc)
160+
- [electron-webrtc](https://npmjs.org/electron-webrtc)
161+
162+
This is a separate step because there isn't still a good open source WebRTC implementation for Node.js that runs in all the envinronments correctly.
163+
Example: `npm install electron-webrtc --global`.
164+
189165
### HTTP-API
190166

191167
The HTTP-API exposed by the js-ipfs daemon follows the [`http-api-spec`](https://github.com/ipfs/http-api-spec). You can use any of the IPFS HTTP-API client libraries with it, such as: [js-ipfs-api](https://github.com/ipfs/js-ipfs-api).
@@ -220,6 +196,11 @@ When starting a node, you can:
220196
// https://github.com/ipfs/js-ipfs-repo
221197
const repo = <IPFS Repo instance or repo path>
222198

199+
// If you want to use the WebRTC transport in Node.js, you have to add it separately. Note, WebRTC comes out of the box in the Browser!
200+
const wrtc = require('wrtc') // or require('electron-webrtc')()
201+
const WStar = require('libp2p-webrtc-star')
202+
const wstar = WStar({ wrtc: wrtc })
203+
223204
const node = new IPFS({
224205
repo: repo,
225206
init: true, // default
@@ -241,6 +222,12 @@ const node = new IPFS({
241222
'/ip4/127.0.0.1/tcp/1337'
242223
]
243224
}
225+
},
226+
libp2p: { // add custom modules to the libp2p stack of your node
227+
modules: { // here we show how to add WebRTC. Note, WebRTC comes out of the box in the Browser! You just need to do this for Node.js
228+
transport: [wstar]
229+
discovery: [wstar.discovery]
230+
}
244231
}
245232
})
246233

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Let us know if you find any issue or if you want to contribute and add a new tut
1313
- [Transfer files and connect to the to other nodes](./transfer-files)
1414
- [Use IPFS to explore the Ethereum BlockChain](./explore-ethereum)
1515
- [Create and resolve through graphs with the dag API](./dag)
16+
- [Use js-ipfs in electron](./electron)

examples/electron/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# js-ipfs in Electron
2+
3+
> This example is heavily inspired by [electron-quick-start](https://github.com/electron/electron-quick-start).
4+
5+
**DISCLAIMER:** This example is still a work in progress, it currently doesn't work due to the usage of native dependencies that Electron is not supporting.
6+
7+
To try it by yourself, do:
8+
9+
```
10+
> npm install
11+
> ./node_modules/.bin/electron-rebuild
12+
# or
13+
> ./build.sh
14+
#
15+
# You can also try to use `npm start` to see where electron errors
16+
```

examples/electron/build.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Electron's version.
2+
export npm_config_target=1.6.11
3+
# The architecture of Electron, can be ia32 or x64.
4+
export npm_config_arch=x64
5+
export npm_config_target_arch=x64
6+
# Download headers for Electron.
7+
export npm_config_disturl=https://atom.io/download/electron
8+
# Tell node-pre-gyp that we are building for Electron.
9+
export npm_config_runtime=electron
10+
# Tell node-pre-gyp to build module from source code.
11+
export npm_config_build_from_source=true
12+
# Install all dependencies, and store cache to ~/.electron-gyp.
13+
HOME=~/.electron-gyp npm install

examples/electron/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>IPFS</title>
6+
</head>
7+
<body>
8+
<h1>IPFS in electron!</h1>
9+
<h2>now check your console</h2>
10+
<script>require('./renderer.js')</script>
11+
</body>
12+
</html>

examples/electron/main.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict'
2+
3+
const electron = require('electron')
4+
const app = electron.app
5+
const BrowserWindow = electron.BrowserWindow
6+
7+
const IPFS = require('ipfs')
8+
const path = require('path')
9+
const url = require('url')
10+
11+
let mainWindow
12+
13+
function createWindow () {
14+
mainWindow = new BrowserWindow({width: 800, height: 600})
15+
16+
// and load the index.html of the app.
17+
mainWindow.loadURL(url.format({
18+
pathname: path.join(__dirname, 'index.html'),
19+
protocol: 'file:',
20+
slashes: true
21+
}))
22+
23+
// Open the DevTools.
24+
mainWindow.webContents.openDevTools()
25+
26+
// Emitted when the window is closed.
27+
mainWindow.on('closed', () => {
28+
mainWindow = null
29+
})
30+
}
31+
32+
app.on('ready', () => {
33+
createWindow()
34+
35+
// Spawn your IPFS node \o/
36+
const node = new IPFS()
37+
38+
node.on('ready', () => {
39+
node.id((err, id) => {
40+
if (err) {
41+
return console.log(err)
42+
}
43+
console.log(id)
44+
})
45+
})
46+
})
47+
48+
// Quit when all windows are closed.
49+
app.on('window-all-closed', () => {
50+
if (process.platform !== 'darwin') { app.quit() }
51+
})
52+
53+
app.on('activate', () => {
54+
if (mainWindow === null) { createWindow() }
55+
})
56+
57+
// In this file you can include the rest of your app's specific main process
58+
// code. You can also put them in separate files and require them here.

examples/electron/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "js-ipfs-in-electron",
3+
"version": "0.0.0",
4+
"description": "A minimal Electron application with js-ipfs",
5+
"main": "main.js",
6+
"scripts": {
7+
"start": "electron .",
8+
"postinstall": "electron-rebuild"
9+
},
10+
"keywords": [
11+
"Electron",
12+
"IPFS",
13+
"Example"
14+
],
15+
"author": "David Dias <[email protected]>",
16+
"license": "MIT",
17+
"devDependencies": {
18+
"electron": "~1.6.11",
19+
"electron-rebuild": "^1.5.11",
20+
"ipfs": "ipfs/js-ipfs#feat\/no-wrtc-by-default"
21+
}
22+
}

examples/electron/renderer.js

Whitespace-only changes.

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"expose-loader": "^0.7.3",
7373
"form-data": "^2.2.0",
7474
"gulp": "^3.9.1",
75-
"interface-ipfs-core": "~0.29.0",
75+
"interface-ipfs-core": "~0.29.1",
7676
"ipfsd-ctl": "~0.21.0",
7777
"left-pad": "^1.1.3",
7878
"lodash": "^4.17.4",
@@ -81,14 +81,14 @@
8181
"nexpect": "^0.5.0",
8282
"pre-commit": "^1.2.2",
8383
"pretty-bytes": "^4.0.2",
84-
"qs": "^6.4.0",
84+
"qs": "^6.5.0",
8585
"random-fs": "^1.0.3",
8686
"rimraf": "^2.6.1",
8787
"stream-to-promise": "^2.2.0",
8888
"transform-loader": "^0.2.4"
8989
},
9090
"dependencies": {
91-
"async": "^2.4.1",
91+
"async": "^2.5.0",
9292
"bl": "^1.2.1",
9393
"boom": "^5.1.0",
9494
"cids": "^0.5.0",
@@ -98,7 +98,7 @@
9898
"hapi": "^16.4.3",
9999
"hapi-set-header": "^1.0.2",
100100
"hoek": "^4.1.1",
101-
"ipfs-api": "^14.0.3",
101+
"ipfs-api": "^14.0.4",
102102
"ipfs-bitswap": "~0.13.1",
103103
"ipfs-block": "~0.6.0",
104104
"ipfs-block-service": "~0.9.1",
@@ -111,8 +111,8 @@
111111
"isstream": "^0.1.2",
112112
"joi": "^10.6.0",
113113
"libp2p-floodsub": "~0.9.4",
114-
"libp2p-ipfs-browser": "~0.24.1",
115-
"libp2p-ipfs-nodejs": "~0.25.2",
114+
"libp2p-ipfs-browser": "~0.25.0",
115+
"libp2p-ipfs-nodejs": "~0.26.0",
116116
"lodash.flatmap": "^4.5.0",
117117
"lodash.get": "^4.4.2",
118118
"lodash.has": "^4.5.2",
@@ -132,13 +132,13 @@
132132
"pull-file": "^1.0.0",
133133
"pull-paramap": "^1.2.2",
134134
"pull-pushable": "^2.1.1",
135-
"pull-sort": "^1.0.0",
135+
"pull-sort": "^1.0.1",
136136
"pull-stream": "^3.6.0",
137137
"pull-stream-to-stream": "^1.3.4",
138138
"pull-zip": "^2.0.1",
139139
"read-pkg-up": "^2.0.0",
140140
"readable-stream": "1.1.14",
141-
"safe-buffer": "^5.1.0",
141+
"safe-buffer": "^5.1.1",
142142
"stream-to-pull-stream": "^1.7.2",
143143
"tar-stream": "^1.5.4",
144144
"temp": "^0.8.3",

src/core/components/libp2p.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ module.exports = function libp2p (self) {
1818
mdns: get(config, 'Discovery.MDNS.Enabled'),
1919
webRTCStar: get(config, 'Discovery.webRTCStar.Enabled'),
2020
bootstrap: get(config, 'Bootstrap'),
21-
dht: self._options.EXPERIMENTAL.dht
21+
dht: get(self._options, 'EXPERIMENTAL.dht'),
22+
modules: get(self._options, 'libp2p.modules')
2223
}
2324

2425
self._libp2pNode = new Node(self._peerInfo, self._peerInfoBook, options)

src/http-api/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const setHeader = require('hapi-set-header')
88
const once = require('once')
99

1010
const IPFS = require('../core')
11+
const WStar = require('libp2p-webrtc-star')
1112
const errorHandler = require('./error-handler')
1213

1314
function uriToMultiaddr (uri) {
@@ -31,6 +32,22 @@ function HttpApi (repo, config) {
3132

3233
series([
3334
(cb) => {
35+
const libp2p = {
36+
modules: {}
37+
}
38+
39+
// Attempt to use any of the WebRTC versions available globally
40+
let electronWebRTC
41+
let wrtc
42+
try { electronWebRTC = require('electron-webrtc')() } catch (err) {}
43+
try { wrtc = require('wrtc') } catch (err) {}
44+
45+
if (electronWebRTC || wrtc) {
46+
const wstar = new WStar(electronWebRTC || wrtc)
47+
libp2p.modules.transport = [wstar]
48+
libp2p.modules.discovery = [wstar.discovery]
49+
}
50+
3451
// try-catch so that programmer errors are not swallowed during testing
3552
try {
3653
// start the daemon

0 commit comments

Comments
 (0)