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

Commit

Permalink
feat/no wrtc by default (#884)
Browse files Browse the repository at this point in the history
* feat: no wrtc by default, support multiple WebRTC impl for Node.js
  • Loading branch information
daviddias authored Jun 29, 2017
1 parent fcef848 commit 64823c7
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 63 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
**/node_modules/
**/*.log
test/repo-tests*
**/bundle.js

# Logs
logs
Expand Down Expand Up @@ -35,4 +36,4 @@ lib
dist
test/test-data/go-ipfs-repo/LOCK
test/test-data/go-ipfs-repo/LOG
test/test-data/go-ipfs-repo/LOG.old
test/test-data/go-ipfs-repo/LOG.old
93 changes: 40 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,28 @@
<h3 align="center">The JavaScript implementation of the IPFS protocol.</h3>

<p align="center">
<a href="http://ipn.io">
<img src="https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square" />
</a>
<a href="http://ipfs.io/">
<img src="https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square" />
</a>
<a href="http://webchat.freenode.net/?channels=%23ipfs">
<img src="https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square" />
</a>
<a href="http://ipn.io"><img src="https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square" /></a>
<a href="http://ipfs.io/"><img src="https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square" /></a>
<a href="http://webchat.freenode.net/?channels=%23ipfs"><img src="https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square" /></a>
<br>
<a href="https://waffle.io/ipfs/js-ipfs">
<img src="https://img.shields.io/badge/pm-waffle-blue.svg?style=flat-square" />
</a>
<a href="https://github.com/ipfs/interface-ipfs-core">
<img src="https://img.shields.io/badge/interface--ipfs--core-API%20Docs-blue.svg">
</a>
<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>
<a href="https://waffle.io/ipfs/js-ipfs"><img src="https://img.shields.io/badge/pm-waffle-blue.svg?style=flat-square" /></a>
<a href="https://github.com/ipfs/interface-ipfs-core"><img src="https://img.shields.io/badge/interface--ipfs--core-API%20Docs-blue.svg"></a>
<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>
</p>

<p align="center">
<a href="https://travis-ci.org/ipfs/js-ipfs">
<img src="https://travis-ci.org/ipfs/js-ipfs.svg?branch=master" />
</a>
<a href="https://circleci.com/gh/ipfs/js-ipfs">
<img src="https://circleci.com/gh/ipfs/js-ipfs.svg?style=svg" />
</a>
<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>
<a href="https://travis-ci.org/ipfs/js-ipfs"><img src="https://travis-ci.org/ipfs/js-ipfs.svg?branch=master" /></a>
<a href="https://circleci.com/gh/ipfs/js-ipfs"><img src="https://circleci.com/gh/ipfs/js-ipfs.svg?style=svg" /></a>
<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>
<br>
<a href="https://david-dm.org/ipfs/js-ipfs">
<img src="https://david-dm.org/ipfs/js-ipfs.svg?style=flat-square" />
</a>
<a href="https://github.com/feross/standard">
<img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square">
</a>
<a href="https://github.com/RichardLitt/standard-readme">
<img src="https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square" />
</a>
<a href="">
<img src="https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square" />
</a>
<a href="">
<img src="https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square" />
</a>
<a href="https://david-dm.org/ipfs/js-ipfs"><img src="https://david-dm.org/ipfs/js-ipfs.svg?style=flat-square" /></a>
<a href="https://github.com/feross/standard"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square"></a>
<a href="https://github.com/RichardLitt/standard-readme"><img src="https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square" /></a>
<a href=""><img src="https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square" /></a>
<a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square" /></a>
<br>
<!-- Hidding this until we have SauceLabs situation figured out, right now it is just misleading
<a href="https://saucelabs.com/u/js-ipfs">
<img src="https://saucelabs.com/browser-matrix/js-ipfs.svg" />
</a>
-->
<a href="https://saucelabs.com/u/js-ipfs"><img src="https://saucelabs.com/browser-matrix/js-ipfs.svg" /></a> -->
</p>

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

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.

### Use in the browser with browserify, webpack or any bundler
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).

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.
### Use in the browser

### Use in a browser using a script tag
##### With browserify, webpack or any bundler

Loading this module in a browser (using a `<script>` tag) makes the `Ipfs` object available in the global namespace.
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.

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:
##### With script tag

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:

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

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:

- [wrtc](https://npmjs.org/wrtc)
- [electron-webrtc](https://npmjs.org/electron-webrtc)

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.
Example: `npm install electron-webrtc --global`.

### HTTP-API

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).
Expand Down Expand Up @@ -220,6 +196,11 @@ When starting a node, you can:
// https://github.com/ipfs/js-ipfs-repo
const repo = <IPFS Repo instance or repo path>

// 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!
const wrtc = require('wrtc') // or require('electron-webrtc')()
const WStar = require('libp2p-webrtc-star')
const wstar = WStar({ wrtc: wrtc })

const node = new IPFS({
repo: repo,
init: true, // default
Expand All @@ -241,6 +222,12 @@ const node = new IPFS({
'/ip4/127.0.0.1/tcp/1337'
]
}
},
libp2p: { // add custom modules to the libp2p stack of your node
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
transport: [wstar]
discovery: [wstar.discovery]
}
}
})

Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Let us know if you find any issue or if you want to contribute and add a new tut
- [Transfer files and connect to the to other nodes](./transfer-files)
- [Use IPFS to explore the Ethereum BlockChain](./explore-ethereum)
- [Create and resolve through graphs with the dag API](./dag)
- [Use js-ipfs in electron](./electron)
16 changes: 16 additions & 0 deletions examples/electron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# js-ipfs in Electron

> This example is heavily inspired by [electron-quick-start](https://github.com/electron/electron-quick-start).
**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.

To try it by yourself, do:

```
> npm install
> ./node_modules/.bin/electron-rebuild
# or
> ./build.sh
#
# You can also try to use `npm start` to see where electron errors
```
13 changes: 13 additions & 0 deletions examples/electron/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Electron's version.
export npm_config_target=1.6.11
# The architecture of Electron, can be ia32 or x64.
export npm_config_arch=x64
export npm_config_target_arch=x64
# Download headers for Electron.
export npm_config_disturl=https://atom.io/download/electron
# Tell node-pre-gyp that we are building for Electron.
export npm_config_runtime=electron
# Tell node-pre-gyp to build module from source code.
export npm_config_build_from_source=true
# Install all dependencies, and store cache to ~/.electron-gyp.
HOME=~/.electron-gyp npm install
12 changes: 12 additions & 0 deletions examples/electron/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>IPFS</title>
</head>
<body>
<h1>IPFS in electron!</h1>
<h2>now check your console</h2>
<script>require('./renderer.js')</script>
</body>
</html>
58 changes: 58 additions & 0 deletions examples/electron/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict'

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

const IPFS = require('ipfs')
const path = require('path')
const url = require('url')

let mainWindow

function createWindow () {
mainWindow = new BrowserWindow({width: 800, height: 600})

// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))

// Open the DevTools.
mainWindow.webContents.openDevTools()

// Emitted when the window is closed.
mainWindow.on('closed', () => {
mainWindow = null
})
}

app.on('ready', () => {
createWindow()

// Spawn your IPFS node \o/
const node = new IPFS()

node.on('ready', () => {
node.id((err, id) => {
if (err) {
return console.log(err)
}
console.log(id)
})
})
})

// Quit when all windows are closed.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') { app.quit() }
})

app.on('activate', () => {
if (mainWindow === null) { createWindow() }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
22 changes: 22 additions & 0 deletions examples/electron/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "js-ipfs-in-electron",
"version": "0.0.0",
"description": "A minimal Electron application with js-ipfs",
"main": "main.js",
"scripts": {
"start": "electron .",
"postinstall": "electron-rebuild"
},
"keywords": [
"Electron",
"IPFS",
"Example"
],
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"devDependencies": {
"electron": "~1.6.11",
"electron-rebuild": "^1.5.11",
"ipfs": "ipfs/js-ipfs#feat\/no-wrtc-by-default"
}
}
Empty file added examples/electron/renderer.js
Empty file.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"expose-loader": "^0.7.3",
"form-data": "^2.2.0",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.29.0",
"interface-ipfs-core": "~0.29.1",
"ipfsd-ctl": "~0.21.0",
"left-pad": "^1.1.3",
"lodash": "^4.17.4",
Expand All @@ -81,14 +81,14 @@
"nexpect": "^0.5.0",
"pre-commit": "^1.2.2",
"pretty-bytes": "^4.0.2",
"qs": "^6.4.0",
"qs": "^6.5.0",
"random-fs": "^1.0.3",
"rimraf": "^2.6.1",
"stream-to-promise": "^2.2.0",
"transform-loader": "^0.2.4"
},
"dependencies": {
"async": "^2.4.1",
"async": "^2.5.0",
"bl": "^1.2.1",
"boom": "^5.1.0",
"cids": "^0.5.0",
Expand All @@ -98,7 +98,7 @@
"hapi": "^16.4.3",
"hapi-set-header": "^1.0.2",
"hoek": "^4.1.1",
"ipfs-api": "^14.0.3",
"ipfs-api": "^14.0.4",
"ipfs-bitswap": "~0.13.1",
"ipfs-block": "~0.6.0",
"ipfs-block-service": "~0.9.1",
Expand All @@ -111,8 +111,8 @@
"isstream": "^0.1.2",
"joi": "^10.6.0",
"libp2p-floodsub": "~0.9.4",
"libp2p-ipfs-browser": "~0.24.1",
"libp2p-ipfs-nodejs": "~0.25.2",
"libp2p-ipfs-browser": "~0.25.0",
"libp2p-ipfs-nodejs": "~0.26.0",
"lodash.flatmap": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.has": "^4.5.2",
Expand All @@ -132,13 +132,13 @@
"pull-file": "^1.0.0",
"pull-paramap": "^1.2.2",
"pull-pushable": "^2.1.1",
"pull-sort": "^1.0.0",
"pull-sort": "^1.0.1",
"pull-stream": "^3.6.0",
"pull-stream-to-stream": "^1.3.4",
"pull-zip": "^2.0.1",
"read-pkg-up": "^2.0.0",
"readable-stream": "1.1.14",
"safe-buffer": "^5.1.0",
"safe-buffer": "^5.1.1",
"stream-to-pull-stream": "^1.7.2",
"tar-stream": "^1.5.4",
"temp": "^0.8.3",
Expand Down
3 changes: 2 additions & 1 deletion src/core/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = function libp2p (self) {
mdns: get(config, 'Discovery.MDNS.Enabled'),
webRTCStar: get(config, 'Discovery.webRTCStar.Enabled'),
bootstrap: get(config, 'Bootstrap'),
dht: self._options.EXPERIMENTAL.dht
dht: get(self._options, 'EXPERIMENTAL.dht'),
modules: get(self._options, 'libp2p.modules')
}

self._libp2pNode = new Node(self._peerInfo, self._peerInfoBook, options)
Expand Down
17 changes: 17 additions & 0 deletions src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const setHeader = require('hapi-set-header')
const once = require('once')

const IPFS = require('../core')
const WStar = require('libp2p-webrtc-star')
const errorHandler = require('./error-handler')

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

series([
(cb) => {
const libp2p = {
modules: {}
}

// Attempt to use any of the WebRTC versions available globally
let electronWebRTC
let wrtc
try { electronWebRTC = require('electron-webrtc')() } catch (err) {}
try { wrtc = require('wrtc') } catch (err) {}

if (electronWebRTC || wrtc) {
const wstar = new WStar(electronWebRTC || wrtc)
libp2p.modules.transport = [wstar]
libp2p.modules.discovery = [wstar.discovery]
}

// try-catch so that programmer errors are not swallowed during testing
try {
// start the daemon
Expand Down

0 comments on commit 64823c7

Please sign in to comment.