Skip to content

Commit

Permalink
feat!: convert to ESM and upgrade all deps (#84)
Browse files Browse the repository at this point in the history
Upgrade to the latest libp2p and convert to ESM

BREAKING CHANGE: this module is now ESM-only
  • Loading branch information
achingbrain committed Jun 23, 2022
1 parent 277b5d9 commit d88e0e7
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 233 deletions.
52 changes: 27 additions & 25 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
'use strict'
import { createLibp2p } from 'libp2p'
import { config } from './test/utils/create-libp2p.js'

const Libp2p = require('libp2p')
const PeerInfo = require('peer-info')
const { config } = require('./test/utils/create-libp2p')

let relay

module.exports = {
hooks: {
pre: async () => {
const peerInfo = await PeerInfo.create()
peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/24642/ws')

const defaultConfig = await config()

relay = new Libp2p({
export default {
test: {
async before () {
const defaultConfig = config()
const relay = await createLibp2p({
...defaultConfig,
peerInfo,
config: {
...defaultConfig.config,
relay: {
relay: {
enabled: true,
hop: {
enabled: true,
hop: {
enabled: true
}
active: true
}
},
addresses: {
listen: [
'/ip4/127.0.0.1/tcp/24642/ws'
]
}
})

await relay.start()

const addrs = relay.getMultiaddrs()

return {
relay,
env: {
RELAY_ADDRESS: addrs[0]
}
}
},
post: async () => {
await relay.stop()
async after (_, before) {
await before.relay.stop()
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pids

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
.coverage

# Coverage directory used by tools like istanbul
coverage
Expand Down
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ stages:
- cov

node_js:
- '12'
- '16'

os:
- linux
- osx
- windows

script: npx nyc -s npm run test:node -- --bail
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
script: npm run test:node -- --bail
after_success: npx codecov

jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir dep-check
- npm run lint

Expand Down
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"version": "2.0.2",
"description": "IPFS pub-sub room",
"main": "src/index.js",
"type": "module",
"scripts": {
"test": "aegir test",
"test:node": "aegir test -t node",
"test:node": "aegir test -t node --cov",
"test:browser": "aegir test -t browser",
"test:webworker": "aegir test -t webworker",
"build": "aegir build",
"lint": "aegir lint",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"coverage": "nyc --reporter=text --reporter=lcov --reporter=html npm run test:node",
"dep-check": "aegir dep-check"
},
"repository": {
Expand All @@ -35,22 +35,19 @@
"homepage": "https://github.com/ipfs-shipyard/ipfs-pubsub-room#readme",
"dependencies": {
"hyperdiff": "^2.0.5",
"it-pipe": "^1.1.0",
"it-pipe": "^2.0.3",
"lodash.clonedeep": "^4.5.0",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"aegir": "^20.5.1",
"chai": "^4.2.0",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"libp2p": "0.27.0-rc.0",
"libp2p-gossipsub": "0.2.1",
"libp2p-mplex": "^0.9.3",
"libp2p-secio": "^0.12.2",
"libp2p-websockets": "^0.13.2",
"peer-info": "^0.17.1",
"rimraf": "^3.0.0"
"@chainsafe/libp2p-gossipsub": "^2.0.0",
"@libp2p/floodsub": "^1.0.6",
"@libp2p/mplex": "^1.1.0",
"@libp2p/websockets": "^1.0.7",
"@multiformats/multiaddr": "^10.2.0",
"aegir": "^37.3.0",
"delay": "^5.0.0",
"libp2p": "^0.37.3"
},
"contributors": [
"Alex Potsides <alex@achingbrain.net>",
Expand All @@ -62,5 +59,11 @@
"Pedro Teixeira <i@pgte.me>",
"haad <haad@headbanggames.com>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
]
],
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
}
}
29 changes: 11 additions & 18 deletions src/connection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'use strict'
import EventEmitter from 'events'
import { pipe } from 'it-pipe'
import PROTOCOL from './protocol.js'
import encoding from './encoding.js'

const EventEmitter = require('events')
const pipe = require('it-pipe')

const PROTOCOL = require('./protocol')
const encoding = require('./encoding')

module.exports = class Connection extends EventEmitter {
export default class Connection extends EventEmitter {
constructor (remoteId, libp2p, room) {
super()
this._remoteId = remoteId
Expand Down Expand Up @@ -47,8 +44,8 @@ module.exports = class Connection extends EventEmitter {
return // early
}

const peerInfo = this._libp2p.peerStore.get(this._remoteId)
const { stream } = await this._libp2p.dialProtocol(peerInfo, PROTOCOL)
const peer = await this._libp2p.peerStore.get(this._remoteId)
const { stream } = await this._libp2p.dialProtocol(peer.id, PROTOCOL)
this._connection = new FiFoMessageQueue()

pipe(this._connection, stream, async (source) => {
Expand All @@ -58,20 +55,16 @@ module.exports = class Connection extends EventEmitter {
for await (const message of source) {
this.emit('message', message)
}

this.emit('disconnect')
})
.then(() => {
this.emit('disconnect')
}, (err) => {
.catch((err) => {
this.emit('error', err)
})
}

_isConnectedToRemote () {
for (const peerId of this._libp2p.connections.keys()) {
if (peerId === this._remoteId) {
return true
}
}
return this._libp2p.getConnections(this._remoteId).length !== 0
}
}

Expand Down
46 changes: 23 additions & 23 deletions src/direct-connection-handler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
'use strict'

const EventEmitter = require('events')
const pipe = require('it-pipe')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
import EventEmitter from 'events'
import { pipe } from 'it-pipe'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import PROTOCOL from './protocol.js'

export const emitter = new EventEmitter()

export function handle (libp2p) {
// can only register one handler for the protocol
libp2p.handle(PROTOCOL, handler).catch(err => {
if (err.code !== 'ERR_PROTOCOL_HANDLER_ALREADY_REGISTERED') {
console.error(err) // eslint-disable-line no-console
}
})
}

const emitter = new EventEmitter()
export function unhandle (libp2p) {
libp2p.unhandle(PROTOCOL, handler)
}

function handler ({ connection, stream }) {
const peerId = connection.remotePeer.toB58String()
const peerId = connection.remotePeer.toString()

pipe(
stream,
Expand All @@ -16,7 +29,7 @@ function handler ({ connection, stream }) {
let msg

try {
msg = JSON.parse(message.toString())
msg = JSON.parse(uint8ArrayToString(message))
} catch (err) {
emitter.emit('warning', err.message)
continue // early
Expand All @@ -27,24 +40,11 @@ function handler ({ connection, stream }) {
continue // early
}

const topicIDs = msg.topicIDs
if (!Array.isArray(topicIDs)) {
emitter.emit('warning', 'no topic IDs')
continue // early
}

msg.data = uint8ArrayFromString(msg.data, 'hex')
msg.seqno = uint8ArrayFromString(msg.seqno.padStart(msg.seqno.length % 2 === 0 ? msg.seqno.length : msg.seqno.length + 1, '0'), 'hex')
msg.seqno = BigInt(msg.seqno)

topicIDs.forEach((topic) => {
emitter.emit(topic, msg)
})
emitter.emit(msg.topic, msg)
}
}
)
}

exports = module.exports = {
handler: handler,
emitter: emitter
}
10 changes: 4 additions & 6 deletions src/encoding.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'

const { fromString: uint8arrayFromString } = require('uint8arrays/from-string')

module.exports = (_message) => {
let message = _message
export default (message) => {
if (!(message instanceof Uint8Array)) {
message = uint8arrayFromString(message)
return uint8arrayFromString(message)
}

return message
}
Loading

0 comments on commit d88e0e7

Please sign in to comment.