-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhost.ts
287 lines (249 loc) · 9.89 KB
/
host.ts
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*--------------------------------------------------------------------------
@sidewinder/server
The MIT License (MIT)
Copyright (c) 2022 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/
import { createServer as createHttpServer, Server, IncomingMessage } from 'http'
import { createServer as createHttpsServer, ServerOptions } from 'https'
import { Application, RequestHandler } from 'express'
import { Socket } from 'net'
import { WebSocketServer } from 'ws'
import { HttpService } from './http/index'
import { WebSocketService, WebService } from './rpc/index'
import express from 'express'
import { v4 } from 'uuid'
import ws from 'ws'
export interface AutoScalingOptions {
/**
* Sets an http GET endpoint that can be inspected by an auto scaling process. This endpoint
* will return a response indicating if this host is oversaturated as indicated by the
* this threshold property. If connections exceed the configured threshold this endpoint
* will respond with status 500, otherwise 200.
*/
endpoint: string
/**
* The maximum number of connections before the auto scaling endpoint begins returning
* 500 status response codes. This is only used if `healthEnabled` is set to true.
*/
threshold: number
}
export interface HostOptions {
/**
* Sends a `ping` signal to each connected socket to prevent inactive sockets being terminated by a load balancer.
*
* (Default is 8000)
*/
keepAliveTimeout: number
/**
* Disables client message compression.
*
* (Default is false)
*/
disableFrameCompression: boolean
/**
* Sets the maximum number of concurrent Web Sockets able to connect to this Host.
*
* (Default is 16384)
*/
maxSocketCount: number
/**
* Configuration options for auto scaling web processes in load balanced in environments.
*
* (Default is undefined)
*/
autoScaling?: AutoScalingOptions
}
function defaultHostOptions(options: Partial<HostOptions>): HostOptions {
return {
keepAliveTimeout: options.keepAliveTimeout !== undefined ? options.keepAliveTimeout : 8000,
disableFrameCompression: options.disableFrameCompression !== undefined ? options.disableFrameCompression : false,
maxSocketCount: options.maxSocketCount !== undefined ? options.maxSocketCount : 16384,
autoScaling: options.autoScaling !== undefined ? options.autoScaling : undefined,
}
}
/**
* A Host is a network service mount. It handles the details of listening
* to network interfaces and allows multiple WebService, WebSocketService
* and Express middleware to be mounted as services.
*/
export class Host {
private server!: Server // deferred till listen
private readonly options: HostOptions
private readonly wsserver: WebSocketServer
private readonly application: Application
private readonly services: Map<string, WebSocketService<any>>
private readonly sockets: Map<number, ws.WebSocket>
private readonly keepAliveInterval: NodeJS.Timer
private socketOrdinal: number
private socketCount: number
private listening: boolean
private disposed: boolean
constructor(options: Partial<HostOptions> = {}) {
this.options = defaultHostOptions(options)
this.services = new Map<string, WebSocketService<any>>()
this.sockets = new Map<number, ws.WebSocket>()
this.application = express()
this.wsserver = new WebSocketServer({ noServer: true, ...(this.options.disableFrameCompression ? { perMessageDeflate: false } : {}) })
this.keepAliveInterval = setInterval(() => this.keepAlive())
this.socketOrdinal = 0
this.socketCount = 0
this.listening = false
this.disposed = false
this.configureAutoScaling()
}
/** Uses a WebSocketService to the specified path */
public use(path: string, service: WebSocketService<any, any>): void
/** Uses a WebService to the specified path */
public use(path: string, service: WebService<any, any>): void
/** Uses a HttpService to the specified path */
public use(path: string, service: HttpService): void
/** Uses express middleware on the specified path */
public use(path: string, service: RequestHandler): void
/** Uses a WebSocketService */
public use(service: WebSocketService<any, any>): void
/** Uses a WebService */
public use(service: WebService<any, any>): void
/** Uses a HttpService */
public use(service: HttpService): void
/** Uses express middleware */
public use(middleware: RequestHandler): void
/** Uses a service */
public use(...args: any[]): void {
this.assertDisposed()
if (args.length > 2 || args.length < 1) throw Error('Invalid parameters on use()')
const [path, service] = args.length === 2 ? [args[0], args[1]] : ['/', args[0]]
if (service instanceof WebSocketService) {
this.services.set(path, service)
} else if (service instanceof WebService) {
this.application.post(path, (req, res) => service.accept(v4(), req, res))
} else if (service instanceof HttpService) {
this.application.use(path, (req, res) => service.accept(v4(), req, res))
} else {
this.application.use(path, service)
}
}
/** Listens on the given port and optional hostname */
public listen(port: number, hostname: string = '0.0.0.0', options: ServerOptions = {}): Promise<void> {
this.assertDisposed()
this.assertNotListening()
this.listening = true
return new Promise((resolve) => {
this.server = createHttpServer(this.application)
this.server.on('upgrade', (request, socket, head) => this.upgrade(request, socket as Socket, head))
this.server.listen(port, hostname, () => resolve())
})
}
/** Listens on the given port and optional hostname. Requires key and cert properties to passed as options. */
public listenTls(port: number, hostname: string = '0.0.0.0', options: ServerOptions = {}): Promise<void> {
this.assertDisposed()
this.assertNotListening()
this.listening = true
return new Promise((resolve) => {
this.server = createHttpsServer(options, this.application)
this.server.on('upgrade', (request, socket, head) => this.upgrade(request, socket as Socket, head))
this.server.listen(port, hostname, () => resolve())
})
}
/** Disposes this host and terminates all connections */
public async dispose(): Promise<void> {
if (this.disposed) return
this.disposed = true
return new Promise((resolve, reject) => {
clearInterval(this.keepAliveInterval)
for (const [socketId, socket] of this.sockets) {
this.sockets.delete(socketId)
socket.close()
}
this.wsserver.close((error) => {
if (error) return reject(error)
if (!this.server) return resolve()
this.server.close((error) => {
if (error) return reject(error)
resolve()
})
})
})
}
private async upgrade(request: IncomingMessage, socket: Socket, head: any) {
if (this.exceedSocketCount()) {
socket.destroy()
return
}
const url = new URL(request.url!, 'http://domain.com/')
if (!this.services.has(url.pathname!)) {
socket.destroy()
return
}
try {
const clientId = v4()
const service = this.services.get(url.pathname)!
const authorized = await service.upgrade(clientId, request)
if (!authorized) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n')
socket.destroy()
return
}
this.wsserver.handleUpgrade(request, socket, head, (socket: ws.WebSocket) => {
socket.on('close', () => this.decrementSocketCount())
this.incrementSocketCount()
service.accept(clientId, socket)
this.sockets.set(this.nextSocketOrdinal(), socket)
})
} catch (error) {
console.error('[Host] Error upgrading connection: ', error)
socket.write('HTTP/1.1 500 Internal Server Error\r\n\r\n')
socket.destroy()
}
}
private configureAutoScaling() {
if (this.options.autoScaling === undefined) return
this.application.get(this.options.autoScaling.endpoint, (_, res) => {
const status = this.socketCount >= this.options.autoScaling!.threshold ? 500 : 200
res.status(status).json({ status })
})
}
private keepAlive() {
for (const [ordinal, socket] of this.sockets) {
socket.ping(void 0, false, (error) => {
if (error === undefined || error === null) return
try {
socket.close()
} catch {}
this.sockets.delete(ordinal)
})
}
}
private nextSocketOrdinal() {
return this.socketOrdinal++
}
private incrementSocketCount() {
this.socketCount += 1
}
private decrementSocketCount() {
this.socketCount -= 1
}
private exceedSocketCount() {
return this.socketCount >= this.options.maxSocketCount
}
private assertNotListening() {
if (this.listening) throw Error('Host can only listen once')
}
private assertDisposed() {
if (this.disposed) throw Error('Host is disposed')
}
}