-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathConnection.ts
More file actions
244 lines (194 loc) · 5.62 KB
/
Connection.ts
File metadata and controls
244 lines (194 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { IncomingMessage as HTTPIncomingMessage } from 'http'
import AsyncLock from 'async-lock'
import WebSocket from 'ws'
import {
CloseEvent, ConnectionTimeout, Forbidden, WsReadyStates,
} from '@hocuspocus/common'
import Document from './Document.js'
import { IncomingMessage } from './IncomingMessage.js'
import { OutgoingMessage } from './OutgoingMessage.js'
import { MessageReceiver } from './MessageReceiver.js'
import { Debugger } from './Debugger.js'
import { onStatelessPayload } from './types.js'
export class Connection {
webSocket: WebSocket
context: any
document: Document
pingInterval: NodeJS.Timeout
pongReceived = true
request: HTTPIncomingMessage
timeout: number
callbacks: any = {
onClose: [(document: Document, event?: CloseEvent) => null],
beforeHandleMessage: (connection: Connection, update: Uint8Array) => Promise,
statelessCallback: () => Promise,
}
socketId: string
lock: AsyncLock
readOnly: Boolean
logger: Debugger
/**
* Constructor.
*/
constructor(
connection: WebSocket,
request: HTTPIncomingMessage,
document: Document,
timeout: number,
socketId: string,
context: any,
readOnly = false,
logger: Debugger,
) {
this.webSocket = connection
this.context = context
this.document = document
this.request = request
this.timeout = timeout
this.socketId = socketId
this.readOnly = readOnly
this.logger = logger
this.lock = new AsyncLock()
this.webSocket.binaryType = 'nodebuffer'
this.document.addConnection(this)
this.pingInterval = setInterval(this.check.bind(this), this.timeout)
this.webSocket.on('close', this.boundClose)
this.webSocket.on('pong', this.boundHandlePong)
this.sendCurrentAwareness()
}
boundClose = this.close.bind(this)
boundHandlePong = this.handlePong.bind(this)
handlePong() {
this.pongReceived = true
}
/**
* Set a callback that will be triggered when the connection is closed
*/
onClose(callback: (document: Document, event?: CloseEvent) => void): Connection {
this.callbacks.onClose.push(callback)
return this
}
/**
* Set a callback that will be triggered when an stateless message is received
*/
onStatelessCallback(callback: (payload: onStatelessPayload) => Promise<void>): Connection {
this.callbacks.statelessCallback = callback
return this
}
/**
* Set a callback that will be triggered before an message is handled
*/
beforeHandleMessage(callback: (connection: Connection, update: Uint8Array) => Promise<any>): Connection {
this.callbacks.beforeHandleMessage = callback
return this
}
/**
* Send the given message
*/
send(message: any): void {
if (
this.webSocket.readyState === WsReadyStates.Closing
|| this.webSocket.readyState === WsReadyStates.Closed
) {
this.close()
}
try {
this.webSocket.send(message, (error: any) => {
if (error != null) this.close()
})
} catch (exception) {
this.close()
}
}
/**
* Send a stateless message with payload
*/
public sendStateless(payload: string): void {
const message = new OutgoingMessage(this.document.name)
.writeStateless(payload)
this.logger.log({
direction: 'out',
type: message.type,
category: message.category,
})
this.send(
message.toUint8Array(),
)
}
/**
* Graceful wrapper around the WebSocket close method.
*/
close(event?: CloseEvent): void {
this.lock.acquire('close', (done: Function) => {
if (this.pingInterval) {
clearInterval(this.pingInterval)
}
if (this.document.hasConnection(this)) {
this.document.removeConnection(this)
this.callbacks.onClose.forEach((callback: (arg0: Document, arg1?: CloseEvent) => any) => callback(this.document, event))
}
this.webSocket.removeListener('close', this.boundClose)
this.webSocket.removeListener('pong', this.boundHandlePong)
done()
})
}
/**
* Check if pong was received and close the connection otherwise
* @private
*/
private check(): void {
if (!this.pongReceived) {
return this.close(ConnectionTimeout)
}
if (this.document.hasConnection(this)) {
this.pongReceived = false
try {
this.webSocket.ping()
} catch (error) {
this.close(ConnectionTimeout)
}
}
}
/**
* Send the current document awareness to the client, if any
* @private
*/
private sendCurrentAwareness(): void {
if (!this.document.hasAwarenessStates()) {
return
}
const awarenessMessage = new OutgoingMessage(this.document.name)
.createAwarenessUpdateMessage(this.document.awareness)
this.logger.log({
direction: 'out',
type: awarenessMessage.type,
category: awarenessMessage.category,
})
this.send(awarenessMessage.toUint8Array())
}
/**
* Handle an incoming message
* @public
*/
public handleMessage(data: Uint8Array): void {
const message = new IncomingMessage(data)
const documentName = message.readVarString()
if (documentName !== this.document.name) return
message.writeVarString(documentName)
this.callbacks.beforeHandleMessage(this, data)
.then(() => {
new MessageReceiver(
message,
this.logger,
).apply(this.document, this)
})
.catch((e: any) => {
console.log('closing connection because of exception', e)
this.close({
code: 'code' in e ? e.code : Forbidden.code,
reason: 'reason' in e ? e.reason : Forbidden.reason,
})
})
}
}
export default Connection