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

Commit 11f2292

Browse files
committed
chore: lintin g
1 parent 1bdb8ae commit 11f2292

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

packages/ipfs-grpc-server/src/utils/web-socket-server.js

+30-26
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,41 @@ class Messages extends EventEmitter {
3333

3434
this._wss = wss
3535
this.multiaddr = ''
36+
37+
wss.on('connection', (ws, request) => {
38+
ws.on('error', error => debug(`WebSocket Error: ${error.stack}`))
39+
40+
ws.once('message', (buf) => {
41+
const path = request.url
42+
const metadata = fromHeaders(buf)
43+
const channel = new WebSocketMessageChannel(ws)
44+
45+
this.emit('data', {
46+
path,
47+
metadata,
48+
channel
49+
})
50+
})
51+
})
52+
53+
wss.on('error', error => this.emit('error', error))
3654
}
3755

38-
async stop () {
56+
stop () {
3957
return new Promise((resolve) => {
4058
this._wss.close(() => resolve())
4159
})
4260
}
61+
62+
ready () {
63+
return new Promise((resolve) => {
64+
this._wss.on('listening', () => {
65+
this.multiaddr = `/ip4/${this._wss.address().address}/tcp/${this._wss.address().port}/ws`
66+
67+
resolve(this)
68+
})
69+
})
70+
}
4371
}
4472

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

5785
const messages = new Messages(wss)
5886

59-
wss.on('connection', function connection (ws, request) {
60-
ws.on('error', error => debug(`WebSocket Error: ${error.stack}`))
61-
62-
ws.once('message', function incoming (buf) {
63-
const path = request.url
64-
const metadata = fromHeaders(buf)
65-
const channel = new WebSocketMessageChannel(ws)
66-
67-
messages.emit('data', {
68-
path,
69-
metadata,
70-
channel
71-
})
72-
})
73-
})
74-
75-
wss.on('error', error => messages.emit('error', error))
76-
77-
return new Promise((resolve) => {
78-
wss.on('listening', () => {
79-
messages.multiaddr = `/ip4/${wss.address().address}/tcp/${wss.address().port}/ws`
80-
81-
resolve(messages)
82-
})
83-
})
87+
return messages.ready()
8488
}

0 commit comments

Comments
 (0)