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

Commit

Permalink
runtime specific things - bring libp2p bundles here (#895)
Browse files Browse the repository at this point in the history
* feat: move runtime dependent configs (repo and config) to runtime folder
* feat: bring libp2p bundles to this repo
  • Loading branch information
daviddias committed Jul 4, 2017
1 parent ea8de20 commit 0afdba1
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const gulp = require('gulp')
const parallel = require('async/parallel')
const series = require('async/series')
const createTempRepo = require('./test/utils/create-repo-node.js')
const createTempRepo = require('./test/utils/create-repo-nodejs.js')
const HTTPAPI = require('./src/http-api')
const leftPad = require('left-pad')

Expand Down
26 changes: 18 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
},
"main": "src/core/index.js",
"browser": {
"libp2p-ipfs-nodejs": "libp2p-ipfs-browser",
"./src/core/default-repo.js": "./src/core/default-repo-browser.js",
"./src/core/components/init-assets.js": false,
"./test/utils/create-repo-node.js": "./test/utils/create-repo-browser.js",
"./src/core/runtime/config-nodejs.js": "./src/core/runtime/config-browser.js",
"./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js",
"./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js",
"./test/utils/create-repo-nodejs.js": "./test/utils/create-repo-browser.js",
"stream": "readable-stream"
},
"engines": {
Expand Down Expand Up @@ -110,9 +111,18 @@
"is-ipfs": "^0.3.0",
"isstream": "^0.1.2",
"joi": "^10.6.0",
"libp2p": "^0.9.1",
"libp2p-floodsub": "~0.9.4",
"libp2p-ipfs-browser": "~0.25.0",
"libp2p-ipfs-nodejs": "~0.26.0",
"libp2p-kad-dht": "^0.1.0",
"libp2p-mdns": "^0.7.0",
"libp2p-multiplex": "^0.4.3",
"libp2p-railing": "^0.5.1",
"libp2p-secio": "^0.6.8",
"libp2p-spdy": "^0.10.6",
"libp2p-swarm": "^0.29.1",
"libp2p-tcp": "^0.10.1",
"libp2p-webrtc-star": "^0.11.0",
"libp2p-websockets": "^0.10.0",
"lodash.flatmap": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.has": "^4.5.2",
Expand All @@ -125,9 +135,9 @@
"multihashes": "~0.4.5",
"once": "^1.4.0",
"path-exists": "^3.0.0",
"peer-book": "~0.4.0",
"peer-id": "~0.8.7",
"peer-info": "~0.9.2",
"peer-book": "^0.4.0",
"peer-id": "^0.8.7",
"peer-info": "^0.9.2",
"promisify-es6": "^1.0.2",
"pull-file": "^1.0.0",
"pull-paramap": "^1.2.2",
Expand Down
6 changes: 1 addition & 5 deletions src/core/components/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
'use strict'

const isNode = require('detect-node')

const defaultNodes = isNode
? require('../../init-files/default-config-node.json').Bootstrap
: require('../../init-files/default-config-browser.json').Bootstrap
const defaultNodes = require('../runtime/config-nodejs.json').Bootstrap

module.exports = function bootstrap (self) {
return {
Expand Down
6 changes: 1 addition & 5 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const peerId = require('peer-id')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const isNode = require('detect-node')
const promisify = require('promisify-es6')
const config = require('../runtime/config-nodejs.json')

const addDefaultAssets = require('./init-assets')

Expand Down Expand Up @@ -37,10 +37,6 @@ module.exports = function init (self) {
opts.bits = Number(opts.bits) || 2048
opts.log = opts.log || function () {}

const config = isNode
? require('../../init-files/default-config-node.json')
: require('../../init-files/default-config-browser.json')

waterfall([
// Verify repo does not yet exist.
(cb) => self._repo.exists(cb),
Expand Down
3 changes: 2 additions & 1 deletion src/core/components/libp2p.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const Node = require('libp2p-ipfs-nodejs')
// libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified
const Node = require('../runtime/libp2p-nodejs')
const promisify = require('promisify-es6')
const get = require('lodash.get')

Expand Down
3 changes: 2 additions & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ const debug = require('debug')
const extend = require('deep-extend')
const EventEmitter = require('events')

const defaultRepo = require('./default-repo')
const boot = require('./boot')
const components = require('./components')
// replaced by repo-browser when running in the browser
const defaultRepo = require('./runtime/repo-nodejs')

class IPFS extends EventEmitter {
constructor (options) {
Expand Down
67 changes: 67 additions & 0 deletions src/core/runtime/libp2p-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

const WS = require('libp2p-websockets')
const WebRTCStar = require('libp2p-webrtc-star')
const spdy = require('libp2p-spdy')
const multiplex = require('libp2p-multiplex')
const secio = require('libp2p-secio')
const Railing = require('libp2p-railing')
const libp2p = require('libp2p')

function mapMuxers (list) {
return list.map((pref) => {
if (typeof pref !== 'string') {
return pref
}
switch (pref.trim().toLowerCase()) {
case 'spdy':
return spdy
case 'multiplex':
return multiplex
default:
throw new Error(pref + ' muxer not available')
}
})
}

function getMuxers (options) {
if (options) {
return mapMuxers(options)
} else {
return [multiplex, spdy]
}
}

class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
const webRTCStar = new WebRTCStar()

const modules = {
transport: [
new WS(),
webRTCStar
],
connection: {
muxer: getMuxers(options.muxer),
crypto: [
secio
]
},
discovery: []
}

if (options.webRTCStar) {
modules.discovery.push(webRTCStar.discovery)
}

if (options.bootstrap) {
const r = new Railing(options.bootstrap)
modules.discovery.push(r)
}

super(modules, peerInfo, peerBook, options)
}
}

module.exports = Node
80 changes: 80 additions & 0 deletions src/core/runtime/libp2p-nodejs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict'

const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const Railing = require('libp2p-railing')
const spdy = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const multiplex = require('libp2p-multiplex')
const secio = require('libp2p-secio')
const libp2p = require('libp2p')

function mapMuxers (list) {
return list.map((pref) => {
if (typeof pref !== 'string') {
return pref
}
switch (pref.trim().toLowerCase()) {
case 'spdy': return spdy
case 'multiplex': return multiplex
default:
throw new Error(pref + ' muxer not available')
}
})
}

function getMuxers (muxers) {
const muxerPrefs = process.env.LIBP2P_MUXER
if (muxerPrefs && !muxers) {
return mapMuxers(muxerPrefs.split(','))
} else if (muxers) {
return mapMuxers(muxers)
} else {
return [multiplex, spdy]
}
}

class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}

const modules = {
transport: [
new TCP(),
new WS()
],
connection: {
muxer: getMuxers(options.muxer),
crypto: [ secio ]
},
discovery: []
}

if (options.dht) {
modules.DHT = KadDHT
}

if (options.mdns) {
const mdns = new MulticastDNS(peerInfo, 'ipfs.local')
modules.discovery.push(mdns)
}

if (options.bootstrap) {
const r = new Railing(options.bootstrap)
modules.discovery.push(r)
}

if (options.modules && options.modules.transport) {
options.modules.transport.forEach((t) => modules.transport.push(t))
}

if (options.modules && options.modules.discovery) {
options.modules.discovery.forEach((d) => modules.discovery.push(d))
}

super(modules, peerInfo, peerBook, options)
}
}

module.exports = Node
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/core/bitswap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const CID = require('cids')
const Buffer = require('safe-buffer').Buffer

// This gets replaced by '../utils/create-repo-browser.js' in the browser
const createTempRepo = require('../utils/create-repo-node.js')
const createTempRepo = require('../utils/create-repo-nodejs.js')

const IPFS = require('../../src/core')

Expand Down
5 changes: 2 additions & 3 deletions test/core/bootstrap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ chai.use(dirtyChai)

const isNode = require('detect-node')

// This gets replaced by require('../utils/create-repo-browser.js')
// in the browser
const createTempRepo = require('../utils/create-repo-node.js')
// This gets replaced by `create-repo-browser.js` in the browser
const createTempRepo = require('../utils/create-repo-nodejs.js')

const IPFS = require('../../src/core')

Expand Down
5 changes: 2 additions & 3 deletions test/core/create-node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ const series = require('async/series')
const isNode = require('detect-node')
const IPFS = require('../../src/core')

// This gets replaced by require('../utils/create-repo-browser.js')
// in the browser
const createTempRepo = require('../utils/create-repo-node.js')
// This gets replaced by `create-repo-browser.js` in the browser
const createTempRepo = require('../utils/create-repo-nodejs.js')

describe('create node', () => {
it('custom repoPath', (done) => {
Expand Down
2 changes: 1 addition & 1 deletion test/core/files-sharding.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const pull = require('pull-stream')
const Buffer = require('safe-buffer').Buffer

const IPFS = require('../../src/core')
const createTempRepo = require('../utils/create-repo-node.js')
const createTempRepo = require('../utils/create-repo-nodejs.js')

describe('files dir', () => {
const files = []
Expand Down
5 changes: 2 additions & 3 deletions test/core/init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ const multihash = require('multihashes')
const CID = require('cids')
const IPFS = require('../../src/core')

// This gets replaced by require('../utils/create-repo-browser.js')
// in the browser
const createTempRepo = require('../utils/create-repo-node.js')
// This gets replaced by `create-repo-browser.js` in the browser
const createTempRepo = require('../utils/create-repo-nodejs.js')

describe('init', () => {
if (!isNode) { return }
Expand Down
2 changes: 1 addition & 1 deletion test/http-api/spec/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const expect = require('chai').expect
const qs = require('qs')
const defaultList = require('../../../src/init-files/default-config-node.json').Bootstrap
const defaultList = require('../../../src/core/runtime/config-nodejs.json').Bootstrap

module.exports = (http) => {
describe('/bootstrap', () => {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/utils/ipfs-factory-instance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const each = require('async/each')

const defaultConfig = require('./default-config.json')
const IPFS = require('../../../src/core')
const createTempRepo = require('../create-repo-node')
const createTempRepo = require('../create-repo-nodejs')

module.exports = Factory

Expand Down

0 comments on commit 0afdba1

Please sign in to comment.