Skip to content

Commit

Permalink
refactor(wip): async iterators
Browse files Browse the repository at this point in the history
Switches JS API to async iterators where possible.

Includes switch to libp2p config override via  ipfs/js-ipfs#2591

BREAKING CHANGE: switched to Async Iterators version of JS API
https://blog.ipfs.io/2020-02-01-async-await-refactor/
  • Loading branch information
lidel committed Apr 26, 2020
1 parent 9467fd2 commit 930e670
Show file tree
Hide file tree
Showing 19 changed files with 2,014 additions and 2,744 deletions.
79 changes: 0 additions & 79 deletions add-on/src/lib/dir-view.js

This file was deleted.

49 changes: 42 additions & 7 deletions add-on/src/lib/ipfs-client/embedded-chromesockets/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
const browser = require('webextension-polyfill')

const { optionDefaults } = require('../../options')
const chromeSocketsBundle = require('./libp2p-bundle')
const mergeOptions = require('merge-options')
const getPort = require('get-port')
const { getIPv4, getIPv6 } = require('webrtc-ips')

const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')

const multiaddr = require('multiaddr')
const maToUri = require('multiaddr-to-uri')
const multiaddr2httpUrl = (ma) => maToUri(ma.includes('/http') ? ma : multiaddr(ma).encapsulate('/http'))

const debug = require('debug')
const log = debug('ipfs-companion:client:embedded:config')
log.error = debug('ipfs-companion:client:embedded:config:error')

// additional default js-ipfs config specific to runtime with chrome.sockets APIs
const chromeDefaultOpts = {
config: {
Expand All @@ -25,11 +32,10 @@ const chromeDefaultOpts = {
Swarm: [
// optional ws-star signaling provides a backup for non-LAN peer discovery
// (this will be removed when autorelay and DHT are stable in js-ipfs)
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
'/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star'
],
// Delegated Content and Peer Routing: https://github.com/ipfs/js-ipfs/pull/2195
Delegates: // [] // TODO: enable delegates
[
Delegates: [
'/dns4/node1.delegate.ipfs.io/tcp/443/https',
'/dns4/node0.delegate.ipfs.io/tcp/443/https'
]
Expand All @@ -42,8 +48,8 @@ const chromeDefaultOpts = {
},
Swarm: {
ConnMgr: {
LowWater: 100,
HighWater: 250
LowWater: 50,
HighWater: 150
}
},
Bootstrap: [
Expand Down Expand Up @@ -113,7 +119,36 @@ async function buildConfig (opts, log) {
// merge configs
const finalOpts = {
start: false,
libp2p: chromeSocketsBundle
// a function that customizes libp2p config: https://github.com/ipfs/js-ipfs/pull/2591
libp2p: ({ libp2pOptions, peerInfo }) => {
libp2pOptions.modules = mergeOptions.call({ concatArrays: true }, libp2pOptions.modules, {
transports: [TCP]
})

libp2pOptions.modules = mergeOptions.call({ concatArrays: true }, libp2pOptions.modules, {
peerDiscovery: [MulticastDNS]
})

libp2pOptions.config = mergeOptions(libp2pOptions.config, {
peerDiscovery: {
autoDial: true,
mdns: {
enabled: true
},
bootstrap: {
enabled: true
},
webRTCStar: {
enabled: true
}
}
})

libp2pOptions.metrics = { enabled: false }

log('initializing libp2p with libp2pOptions', libp2pOptions)
return new Libp2p(libp2pOptions)
}
}
const ipfsNodeConfig = mergeOptions(defaultOpts, userOpts, chromeOpts, finalOpts)

Expand Down
107 changes: 0 additions & 107 deletions add-on/src/lib/ipfs-client/embedded-chromesockets/libp2p-bundle.js

This file was deleted.

91 changes: 0 additions & 91 deletions add-on/src/lib/ipfs-client/embedded-chromesockets/libp2p.js

This file was deleted.

22 changes: 2 additions & 20 deletions add-on/src/lib/ipfs-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,11 @@ async function _reloadIpfsClientDependents (instance, opts) {
}
}

const movedFilesApis = ['add', 'addPullStream', 'addReadableStream', 'cat', 'catPullStream', 'catReadableStream', 'get', 'getPullStream', 'getReadableStream']

// This enables use of dependencies without worrying if they already migrated to the new API.
function easeApiChanges (ipfs) {
if (!ipfs) return
// Handle the move of regular files api to top level
// https://github.com/ipfs/interface-ipfs-core/pull/378
// https://github.com/ipfs/js-ipfs/releases/tag/v0.34.0-pre.0
movedFilesApis.forEach(cmd => {
// Fix old backend (add new methods)
if (typeof ipfs[cmd] !== 'function' && ipfs.files && typeof ipfs.files[cmd] === 'function') {
ipfs[cmd] = ipfs.files[cmd]
// console.log(`[ipfs-companion] fixed missing ipfs.${cmd}: added an alias for ipfs.files.${cmd}`)
}
// Fix new backend (add old methods)
// This ensures ipfs-postmsg-proxy always works and can be migrated later
if (ipfs.files && typeof ipfs.files[cmd] !== 'function' && typeof ipfs[cmd] === 'function') {
ipfs.files[cmd] = ipfs[cmd]
// console.log(`[ipfs-companion] fixed missing ipfs.files.${cmd}: added an alias for ipfs.${cmd}`)
}
})
// no-op: used in past, not used atm
// if (!ipfs) return
}

exports.movedFilesApis = movedFilesApis
exports.initIpfsClient = initIpfsClient
exports.destroyIpfsClient = destroyIpfsClient
Loading

0 comments on commit 930e670

Please sign in to comment.