/
HttpServer.ts
513 lines (475 loc) · 20.6 KB
/
HttpServer.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import * as http from "http"
import {
JsonEncoderInterface,
RequestError,
ServerMessage,
ClientMessage,
ServerEventMessage,
ServerMetadata,
} from "typedapi-core"
import { ObjectProxy } from "../ObjectProxy"
import { LoggerInterface, TextLogger } from "../log"
import { SessionProviderInterface, MemorySessionProvider } from "../session"
import { ConnectionData, AuthData, } from "../auth"
import { MethodProxy } from "../MethodProxy"
import { EventsProxy } from "../EventsProxy"
import { ApiMap } from "../ApiMap"
import { EventCallbackMetadata } from "../events"
import { ApiItemMetadata } from "../decorators"
import { isApiClientMessageInterfaceArray } from "../clientDataReflections"
import { getUsageStatus } from "../getUsageStatus"
export interface HttpServerConfig {
serverMetadata?: ServerMetadata,
jsonEncoder?: JsonEncoderInterface
requestValidator?: { (request: http.IncomingMessage, response: http.ServerResponse): Promise<boolean> }
objectProxy?: ObjectProxy
logger?: LoggerInterface
sessionProvider?: SessionProviderInterface
sessionIdKey?: string
apiMap: ApiMap
maxMessageLength?: number
pollingWaitTime?: number
connectionLifetime?: number
connectionIdKey?: string
checkConnectionsInterval?: number
logStatusInterval?: number
}
const SESSION_ID_KEY_DEFAULT = "typedapi.sid"
const CONNECTION_ID_KEY_DEFAULT = "typedapi.cid"
const MAX_MESSAGE_LENGTH_DEFAULT = 256 * 1024
const POLLING_WAIT_TIME_DEFAULT = 1000 * 15
const CONNECTION_LIFETIME_DEFAULT = 1000 * 30
const CHECK_CONNECTIONS_INTERVAL_DEFAULT = 5000
const LOG_STATUS_INTERVAL_DEFAULT = 1000 * 60
interface HttpServerConnection {
id: string
sessionId: string
authData: AuthData
eventResponse?: http.ServerResponse
lastActivity: number
}
/**
* Http server implementation
* Tasks:
* - Receive http request
* - Validate request is it valid json with ClientMessage[]
* - Validate request is it valid data for current method
* - call api`s method
* - return response
* - hold polling connection and wait for event from EventsProxy, then send event
* - Authorizing clients using AuthDataResponse response from api method
*/
export class HttpServer {
private jsonEncoder: JsonEncoderInterface
private logger: LoggerInterface
private sessionProvider: SessionProviderInterface
private sessionIdKey: string
private connectionIdKey: string
private methodProxy: MethodProxy
private objectProxy: ObjectProxy
private apiMap: ApiMap
private maxMessageLength: number
private connections: Map<string, HttpServerConnection> = new Map
private connectionsData: Map<string, ConnectionData> = new Map
private eventsProxy: EventsProxy
private haveEvents = false
private pendingMessages: Map<string, string[]> = new Map
private pollingWaitTime: number
private connectionLifetime: number
private checkConnectionsTimer: NodeJS.Timeout
private serverMetadata: ServerMetadata
private logStatusInterval: number
private logStatusTimer: NodeJS.Timeout
constructor(private config: HttpServerConfig) {
this.jsonEncoder = config.jsonEncoder ?? JSON
this.logger = config.logger ?? new TextLogger(console.log, console.error)
this.sessionProvider = config.sessionProvider ?? new MemorySessionProvider
this.sessionIdKey = config.sessionIdKey ?? SESSION_ID_KEY_DEFAULT
this.objectProxy = config.objectProxy ?? new ObjectProxy
this.pollingWaitTime = config.pollingWaitTime ?? POLLING_WAIT_TIME_DEFAULT
this.connectionLifetime = config.connectionLifetime ?? CONNECTION_LIFETIME_DEFAULT
this.methodProxy = new MethodProxy({
apiMap: this.config.apiMap
})
this.apiMap = config.apiMap
this.maxMessageLength = config.maxMessageLength ?? MAX_MESSAGE_LENGTH_DEFAULT
this.haveEvents = this.apiMap.events.size > 0 || this.apiMap.parametricEvents.size > 0
this.eventsProxy = new EventsProxy(this.apiMap)
if (this.haveEvents) {
this.eventsProxy.onSend.subscribe(d => this.sendEvent(d.data, d.meta, d.eventMeta))
}
this.connectionIdKey = config.connectionIdKey ?? CONNECTION_ID_KEY_DEFAULT
this.checkConnectionsTimer = setInterval(() => this.checkConnections(), config.checkConnectionsInterval ?? CHECK_CONNECTIONS_INTERVAL_DEFAULT)
this.serverMetadata = config.serverMetadata ?? {
name: "TypedAPI HTTP Server",
version: "0",
}
if (this.apiMap.broadcastEvents.size) {
this.serverMetadata.broadcastEvents = Array.from(this.apiMap.broadcastEvents)
}
this.logStatusInterval = config.logStatusInterval ?? LOG_STATUS_INTERVAL_DEFAULT
this.logStatusTimer = setInterval(() => this.logStatus(), this.logStatusInterval)
}
async handleRequest(request: http.IncomingMessage, response: http.ServerResponse): Promise<void> {
if (this.config.requestValidator) {
const result = await this.config.requestValidator(request, response)
if (!result) {
response.statusCode = 300
response.end("Bad request")
return
}
}
response.setHeader("Access-Control-Allow-Origin", "http://localhost")
response.setHeader("Access-Control-Allow-Methods", "POST")
response.setHeader("Access-Control-Allow-Headers", "Content-Type")
response.setHeader("Access-Control-Allow-Credentials", "true")
response.setHeader("Access-Control-Max-Age", 1000)
if (request.method === "OPTIONS") {
response.statusCode = 204
response.end("")
return
}
if (request.method !== "POST") {
response.statusCode = 300
response.end("Bad request")
return
}
const body: Uint8Array[] = []
request.on("data", chunk => body.push(chunk))
request.on("end", () => this.handleStringRequest(Buffer.concat(body).toString(), request, response))
}
private async handleStringRequest(stringData: string, request: http.IncomingMessage, response: http.ServerResponse) {
try {
const responseData: ServerMessage[] = []
if (stringData.length > this.maxMessageLength) {
response.statusCode = 300
response.end("Bad request")
return
}
const data = this.jsonEncoder.parse(stringData)
if (!isApiClientMessageInterfaceArray(data)) {
throw new RequestError("Bad request data")
}
if (!data.length) {
throw new RequestError("No data")
}
const cookies = parseCookies(request)
let setCookiesArray: string[] | undefined
let sessionId = cookies[this.sessionIdKey]
if (sessionId) {
const authData = await this.sessionProvider.get(sessionId)
if (!authData) {
sessionId = undefined
}
}
if (!sessionId) {
sessionId = await this.sessionProvider.create()
const cookieValue = `${this.sessionIdKey}=${sessionId}; SameSite=None; Secure`
setCookiesArray = [cookieValue]
response.setHeader("Set-Cookie", setCookiesArray)
}
const authData = await this.sessionProvider.get(sessionId)
/* istanbul ignore next */
if (!authData) {
console.error("No auth data received")
response.statusCode = 500
response.end("Server error")
return
}
/* istanbul ignore next */
const connectionData: ConnectionData = {
authData: authData,
ip: request.socket.remoteAddress ?? "",
sessionId: sessionId,
}
let isNewConnection = true
let connectionId = cookies[this.connectionIdKey]
let connection: HttpServerConnection | undefined
if (this.haveEvents) {
connection = this.connections.get(connectionId ?? "")
if (!connectionId || !connection || connection.sessionId !== sessionId) {
connectionId = this.getNextConnectionId()
connection = {
authData: authData,
id: connectionId,
lastActivity: Date.now(),
sessionId: sessionId,
}
connectionData.connectionId = connectionId
this.connections.set(connectionId, connection)
this.connectionsData.set(connectionId, connectionData)
} else {
connectionData.connectionId = connectionId
isNewConnection = false
}
}
if (connection) {
connection.lastActivity = Date.now()
if (isNewConnection) {
const cookieValue = `${this.connectionIdKey}=${connectionId}; SameSite=None; Secure`
if (setCookiesArray) {
setCookiesArray.push(cookieValue)
} else {
setCookiesArray = [cookieValue]
}
response.setHeader("Set-Cookie", setCookiesArray)
}
}
if (data.length === 1 && data[0][1] === "_.ping") {
responseData.push([
"r",
data[0][0],
"pong",
])
} else if (data.length === 1 && data[0][1] === "_.polling" && connection) {
connection.lastActivity = Date.now()
const pendingMessages = this.pendingMessages.get(connection.id)
if (pendingMessages && pendingMessages.length > 0) {
response.setHeader("Content-Type", "application/json")
response.end("[" + pendingMessages.join(",") + "]")
this.pendingMessages.delete(connection.id)
} else if (isNewConnection) {
response.setHeader("Content-Type", "application/json")
response.end(this.jsonEncoder.stringify(responseData))
} else {
connection.eventResponse = response
}
return
} else {
for (const dataItem of data) {
const responseItem = await this.handleDataRequest(dataItem, connectionData)
responseData.push(responseItem)
}
}
response.setHeader("Content-Type", "application/json")
response.end(this.jsonEncoder.stringify(responseData))
} catch (err) {
this.logger.serverError("many", {}, err.message + "\n" + err.stack, { ip: "", authData: {} })
response.statusCode = 300
response.end("Bad request")
}
}
private async handleDataRequest(data: ClientMessage, connectionData: ConnectionData): Promise<ServerMessage> {
let returnValue: ServerMessage
try {
const method = data[1]
if (method.startsWith("_.")) {
returnValue = await this.handleSystemDataRequest(data, connectionData)
} else {
returnValue = await this.handleApiDataRequest(data, connectionData)
}
} catch (err) {
returnValue = this.objectProxy.responseFromError({
err,
connectionData,
requestId: data[0],
logger: this.logger,
input: data[2],
method: data[1],
})
}
return returnValue
}
private async handleApiDataRequest(data: ClientMessage, connectionData: ConnectionData): Promise<ServerMessage> {
const method = data[1]
const methodReflection = this.apiMap.methods.get(method)
if (!methodReflection) {
throw new RequestError(`Method ${method} not found`)
}
let startTime: number | undefined
if (methodReflection.metadata.logConfig !== false) startTime = Date.now()
const returnValue: ServerMessage = await this.methodProxy.callByClientMesssage(data, connectionData)
const responseReflection = methodReflection.reflection.return
if (returnValue[0] === "r" && responseReflection?.type === "injection") {
switch (responseReflection.injectionType) {
case "AuthDataServerResponse": {
const responseData = returnValue[2] as Record<string, unknown>
const newAuthData = responseData.newAuthData as AuthData
const oldAuthData = connectionData.authData
returnValue[2] = !!responseData.response
if (!newAuthData.id && oldAuthData.id) {
this.logger.event("logout", undefined, connectionData)
}
/* istanbul ignore next */
if (!connectionData.sessionId) {
throw new Error("No session id in connection data")
}
await this.sessionProvider.update(connectionData.sessionId, newAuthData)
connectionData.authData = newAuthData
/* istanbul ignore next */
if (!connectionData.connectionId) {
throw new Error("No connection id")
}
const currentConnection = this.connections.get(connectionData.connectionId)
this.connections.set(connectionData.connectionId, {
authData: newAuthData,
id: connectionData.connectionId,
lastActivity: Date.now(),
sessionId: connectionData.sessionId,
eventResponse: currentConnection && currentConnection.eventResponse ? currentConnection.eventResponse : undefined,
})
this.connectionsData.forEach(item => {
if (item.sessionId === connectionData.sessionId) {
item.authData = newAuthData
}
})
if (newAuthData.id && !oldAuthData.id) {
this.logger.event("login", undefined, connectionData)
}
break
}
default:
throw new Error(`Bad injection: ${responseReflection.injectionType}`)
}
}
if (startTime) {
const logConfig = methodReflection.metadata.logConfig
this.logger.methodCall(
method,
Date.now() - startTime,
logConfig !== "outputOnly" && logConfig !== "noData" ? data[2] : undefined,
logConfig !== "inputOnly" && logConfig !== "noData" ? returnValue[2] : undefined,
connectionData
)
}
return returnValue
}
private handleSystemDataRequest(data: ClientMessage, connectionData: ConnectionData): Promise<ServerMessage> {
const method = data[1]
if (this.haveEvents) {
if (method === "_.sub" || method === "_.unsub") {
return this.eventsProxy.subscribeToEventFromRequest(data, connectionData)
}
}
if (method === "_.v") {
return Promise.resolve(["r", data[0], this.serverMetadata.version])
}
if (method === "_.meta") {
return Promise.resolve(["r", data[0], this.serverMetadata])
}
throw new RequestError(`Method ${method} not found`)
}
private sendEvent(data: ServerEventMessage, meta: EventCallbackMetadata | undefined, eventMeta: ApiItemMetadata) {
const encodedData = this.jsonEncoder.stringify(data)
if (meta) {
switch (meta[0]) {
// for user
case "u":
this.connections.forEach(connection => {
if (connection.authData.id === meta[1]) this.sendToConnection(connection, encodedData)
})
break
// for connection
case "c": {
const connection = this.connections.get(meta[1])
if (connection) {
this.sendToConnection(connection, encodedData)
}
break
}
// for group
case "g":
this.connections.forEach(connection => {
if (connection.authData.groups && connection.authData.groups.indexOf(meta[1]) !== -1) this.sendToConnection(connection, encodedData)
})
break
// for session
case "s":
this.connections.forEach(connection => {
if (connection.sessionId === meta[1]) this.sendToConnection(connection, encodedData)
})
break
}
} else if (eventMeta.broadcastEvent) {
this.connections.forEach(connection => this.sendToConnection(connection, encodedData))
} else {
this.eventsProxy.getSubscriptions(data[1])?.forEach(connectionId => {
const connection = this.connections.get(connectionId)
/* istanbul ignore else */
if (connection) {
this.sendToConnection(connection, encodedData)
}
})
}
}
private sendToConnection(connection: HttpServerConnection, data: string) {
const response = connection.eventResponse
if (response) {
response.setHeader("Content-Type", "application/json")
response.end(`[${data}]`)
this.pendingMessages.delete(connection.id)
delete connection.eventResponse
} else {
const pendingArray = this.pendingMessages.get(connection.id)
if (pendingArray) {
pendingArray.push(data)
} else {
this.pendingMessages.set(connection.id, [data])
}
}
}
private connectionIdIndex = 0
private getNextConnectionId(): string {
if (this.connectionIdIndex >= Number.MAX_SAFE_INTEGER) this.connectionIdIndex = 0
return (++this.connectionIdIndex).toString()
}
private dropConnection(connectionId: string) {
this.connectionsData.delete(connectionId)
const connectionInfo = this.connections.get(connectionId)
/* istanbul ignore next */
if (!connectionInfo) {
throw new Error("No connection info")
}
/* istanbul ignore next */
if (connectionInfo.eventResponse) {
connectionInfo.eventResponse.setHeader("Content-Type", "application/json")
connectionInfo.eventResponse.end("[]")
delete connectionInfo.eventResponse
}
this.connections.delete(connectionId)
this.pendingMessages.delete(connectionId)
this.eventsProxy.dropConnection(connectionId)
}
private checkConnections() {
const deleteConnectionIds: string[] = []
const pollingMinActivity = Date.now() - this.pollingWaitTime
const sheduleMinActivity = Date.now() - this.connectionLifetime
this.connections.forEach(c => {
if (!c.eventResponse && c.lastActivity < sheduleMinActivity) {
deleteConnectionIds.push(c.id)
}
if (c.eventResponse && c.lastActivity < pollingMinActivity) {
c.eventResponse.setHeader("Content-Type", "application/json")
c.eventResponse.end("[]")
delete c.eventResponse
}
})
deleteConnectionIds.forEach(c => this.dropConnection(c))
}
destroy(): void {
clearInterval(this.checkConnectionsTimer)
clearInterval(this.logStatusTimer)
}
async logStatus(): Promise<void> {
const usage = await getUsageStatus()
this.logger.status({
usage,
usersOnline: this.connections.size
})
}
}
/**
* parse cookies implementation
*/
const parseCookies = (request: http.IncomingMessage) => {
const list: { [key: string]: string | undefined } = {},
rc = request.headers.cookie
rc && rc.split(";").forEach(function (cookie) {
const parts = cookie.split("=")
const part = parts.shift()
if (part) {
list[part.trim()] = decodeURI(parts.join("="))
}
})
return list
}