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

Commit

Permalink
chore: lintin g
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Nov 20, 2020
1 parent 1bdb8ae commit 11f2292
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions packages/ipfs-grpc-server/src/utils/web-socket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,41 @@ class Messages extends EventEmitter {

this._wss = wss
this.multiaddr = ''

wss.on('connection', (ws, request) => {
ws.on('error', error => debug(`WebSocket Error: ${error.stack}`))

ws.once('message', (buf) => {
const path = request.url
const metadata = fromHeaders(buf)
const channel = new WebSocketMessageChannel(ws)

this.emit('data', {
path,
metadata,
channel
})
})
})

wss.on('error', error => this.emit('error', error))
}

async stop () {
stop () {
return new Promise((resolve) => {
this._wss.close(() => resolve())
})
}

ready () {
return new Promise((resolve) => {
this._wss.on('listening', () => {
this.multiaddr = `/ip4/${this._wss.address().address}/tcp/${this._wss.address().port}/ws`

resolve(this)
})
})
}
}

module.exports = async (ipfs, options = {}) => {
Expand All @@ -56,29 +84,5 @@ module.exports = async (ipfs, options = {}) => {

const messages = new Messages(wss)

wss.on('connection', function connection (ws, request) {
ws.on('error', error => debug(`WebSocket Error: ${error.stack}`))

ws.once('message', function incoming (buf) {
const path = request.url
const metadata = fromHeaders(buf)
const channel = new WebSocketMessageChannel(ws)

messages.emit('data', {
path,
metadata,
channel
})
})
})

wss.on('error', error => messages.emit('error', error))

return new Promise((resolve) => {
wss.on('listening', () => {
messages.multiaddr = `/ip4/${wss.address().address}/tcp/${wss.address().port}/ws`

resolve(messages)
})
})
return messages.ready()
}

0 comments on commit 11f2292

Please sign in to comment.