Skip to content

Commit 6ceeda1

Browse files
committed
refactor
1 parent 1f13313 commit 6ceeda1

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/Server.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class Server extends EventEmitter<ServerEvents> {
6464
this.socket = new WebSocketServer({ noServer: true })
6565

6666
this.log = debug(`lf:relay:${port}`)
67-
this.log("version", version)
67+
this.log("version", pkg.version)
6868
}
6969

7070
// SERVER
@@ -78,11 +78,14 @@ export class Server extends EventEmitter<ServerEvents> {
7878
})
7979

8080
// Introduction request
81-
.ws("/introduction/:peerId", (socket, { params: { peerId } }) => {
82-
this.log("received introduction request", peerId)
83-
this.openIntroductionConnection(socket, peerId)
84-
this.sockets.add(socket)
85-
})
81+
.ws(
82+
"/introduction/:peerId", //
83+
(socket, { params: { peerId } }) => {
84+
this.log("received introduction request", peerId)
85+
this.openIntroductionConnection(socket, peerId)
86+
this.sockets.add(socket)
87+
}
88+
)
8689

8790
// Connection request
8891
.ws(
@@ -114,16 +117,17 @@ export class Server extends EventEmitter<ServerEvents> {
114117
socket.close()
115118
socket.terminate()
116119
})
117-
return this.app.removeAllListeners()
120+
this.app.removeAllListeners()
118121
}
119122

120123
// DISCOVERY
121124

122125
private openIntroductionConnection(socket: WebSocket, peerId: PeerId) {
123126
this.peers[peerId] = socket
124127

125-
socket.on("message", this.handleIntroductionRequest(peerId))
126-
socket.on("close", this.closeIntroductionConnection(peerId))
128+
socket
129+
.on("message", this.handleIntroductionRequest(peerId))
130+
.on("close", this.closeIntroductionConnection(peerId))
127131

128132
this.emit("introduction", peerId)
129133
}
@@ -133,9 +137,16 @@ export class Server extends EventEmitter<ServerEvents> {
133137
const A = peerId // A and B always refer to peer peerIds
134138
const currentDocumentIds = this.documentIds[A] ?? []
135139

140+
const tryParse = <T>(s: Uint8Array): T | Error => {
141+
try {
142+
return unpack(s)
143+
} catch (error: any) {
144+
return new Error(error.toString())
145+
}
146+
}
147+
136148
const message = tryParse<Message.ClientToServer>(data)
137149
if (message instanceof Error) {
138-
// console.log("ERROR", message)
139150
this.emit("error", { error: message, data })
140151
return
141152
}
@@ -219,7 +230,7 @@ export class Server extends EventEmitter<ServerEvents> {
219230

220231
private openConnection({ socket, A, B, documentId }: ConnectRequestParams) {
221232
const socketA = socket
222-
// A and B always refer to peer peerIds.
233+
// A and B always refer to peerIds.
223234

224235
// `AseeksB` and `BseeksA` are keys for identifying this request and the reciprocal request
225236
// (which may or may not have already come in)
@@ -231,7 +242,6 @@ export class Server extends EventEmitter<ServerEvents> {
231242

232243
if (this.holding[BseeksA]) {
233244
// We already have a connection request from Bob; hook them up
234-
235245
const { socket: socketB, messages } = this.holding[BseeksA]
236246

237247
this.log(
@@ -261,16 +271,6 @@ export class Server extends EventEmitter<ServerEvents> {
261271
}
262272
}
263273

264-
const tryParse = <T>(s: Uint8Array): T | Error => {
265-
try {
266-
return unpack(s)
267-
} catch (error: any) {
268-
return new Error(error.toString())
269-
}
270-
}
271-
272-
const { version } = pkg
273-
274274
const logoPage = `
275275
<body style="background:black; display:flex; justify-content:center; align-items:center">
276276
<img src="https://raw.githubusercontent.com/local-first-web/branding/main/svg/relay-v.svg" width="50%" alt="@localfirst/relay logo"/>

0 commit comments

Comments
 (0)