1
1
'use strict' ;
2
2
3
- const httpProxy = require ( 'http-proxy' ) ;
4
- const http = require ( 'http' ) ;
5
- const ueberdb = require ( 'ueberdb2' ) ;
6
- const checkAvailability = require ( './checkAvailability' ) . checkAvailability ;
3
+ import httpProxy , { type ErrorCallback } from 'http-proxy' ;
4
+ import http from 'http' ;
5
+ import { Database } from 'ueberdb2' ;
6
+ import { checkAvailability } from './checkAvailability.ts'
7
+ import fs from 'fs'
7
8
// load the settings
8
9
const loadSettings = ( ) => {
9
- const fs = require ( 'fs' ) ;
10
10
let settings ;
11
11
try {
12
12
settings = fs . readFileSync ( 'settings.json' , 'utf8' ) ;
@@ -25,16 +25,19 @@ if (settings.dbType === 'dirty') console.error('DirtyDB is not recommend for pro
25
25
const backendIds = Object . keys ( settings . backends ) ;
26
26
27
27
// An object of our proxy instances
28
- const proxies = { } ;
28
+ const proxies : { [ key : string ] : httpProxy < http . IncomingMessage , http . ServerResponse < http . IncomingMessage > > } = { } ;
29
29
30
30
// Making availableBackend globally available.
31
- let availableBackends ;
31
+ let availableBackends : {
32
+ up : Array < string > ,
33
+ available : Array < string > ,
34
+ } ;
32
35
( async ( ) => {
33
36
checkAvailability (
34
37
settings . backends ,
35
38
settings . checkInterval ,
36
39
settings . maxPadsPerInstance ) ;
37
- } ) ;
40
+ } ) ( ) ;
38
41
// And now grab them every X duration
39
42
setInterval ( async ( ) => {
40
43
availableBackends = await checkAvailability (
@@ -44,21 +47,21 @@ setInterval(async () => {
44
47
} , settings . checkInterval ) ;
45
48
46
49
// Creating our database connection
47
- const db = new ueberdb . Database ( settings . dbType , settings . dbSettings ) ;
50
+ const db = new Database ( settings . dbType , settings . dbSettings ) ;
48
51
49
52
// Initiate the proxy routes to the backends
50
- const initiateRoute = ( backend , req , res , socket , head ) => {
53
+ const initiateRoute = ( backend : string , req : http . IncomingMessage , res : http . ServerResponse , socket : any , head : any ) => {
51
54
if ( res ) {
52
55
// console.log('backend: ', backend);
53
56
if ( proxies [ backend ] ) {
54
- proxies [ backend ] . web ( req , res , ( e ) => {
57
+ proxies [ backend ] . web ( req , res , { } , ( e ) => {
55
58
console . error ( e ) ;
56
59
} ) ;
57
60
}
58
61
}
59
62
if ( socket && head ) {
60
63
if ( proxies [ backend ] ) {
61
- proxies [ backend ] . ws ( req , socket , head , ( e ) => {
64
+ proxies [ backend ] . ws ( req , socket , head , { } , ( e ) => {
62
65
console . error ( e ) ;
63
66
} ) ;
64
67
}
@@ -67,7 +70,7 @@ const initiateRoute = (backend, req, res, socket, head) => {
67
70
68
71
// Create dynamically assigned routes based on padIds and ensure that a route for
69
72
// unique padIds are re-used and stuck to a backend -- padId <> backend association.
70
- const createRoute = ( padId , req , res , socket , head ) => {
73
+ const createRoute = ( padId : string | null , req , res , socket , head ) => {
71
74
// If the route isn't for a specific padID IE it's for a static file
72
75
// we can use any of the backends but now let's use the first :)
73
76
if ( ! padId ) {
@@ -77,6 +80,7 @@ const createRoute = (padId, req, res, socket, head) => {
77
80
}
78
81
79
82
// pad specific backend required, do we have a backend already?
83
+ // @ts -ignore
80
84
db . get ( `padId:${ padId } ` , ( e , r ) => {
81
85
if ( r && r . backend ) {
82
86
// console.log(`database hit: ${padId} <> ${r.backend}`);
@@ -112,11 +116,12 @@ const createRoute = (padId, req, res, socket, head) => {
112
116
} ) ;
113
117
} ;
114
118
115
- db . init ( ( ) => {
119
+ db . init ( ) ;
120
+
116
121
// Create the backends.
117
122
for ( const backendId of backendIds ) {
118
123
/* eslint-disable-next-line new-cap */
119
- proxies [ backendId ] = new httpProxy . createProxyServer ( {
124
+ proxies [ backendId ] = httpProxy . createProxyServer ( {
120
125
target : {
121
126
host : settings . backends [ backendId ] . host ,
122
127
port : settings . backends [ backendId ] . port ,
@@ -125,16 +130,16 @@ db.init(() => {
125
130
}
126
131
// Create the routes for web traffic to those backends.
127
132
const proxyServer = http . createServer ( ( req , res ) => {
128
- let padId ;
129
- if ( req . url . indexOf ( '/p/' ) !== - 1 ) {
130
- padId = req . url . split ( '/p/' ) [ 1 ] . split ( '?' ) [ 0 ] . split ( '/' ) [ 0 ] ;
133
+ let padId : string | null = null ;
134
+ if ( req . url ! . indexOf ( '/p/' ) !== - 1 ) {
135
+ padId = req . url ! . split ( '/p/' ) [ 1 ] . split ( '?' ) [ 0 ] . split ( '/' ) [ 0 ] ;
131
136
console . log ( `initial request to /p/${ padId } ` ) ;
132
137
}
133
- if ( ! padId ) {
138
+ if ( padId === null ) {
134
139
const searchParams = new URLSearchParams ( req . url ) ;
135
140
padId = searchParams . get ( '/socket.io/?padId' ) ;
136
141
}
137
- createRoute ( padId , req , res , null , null ) ;
142
+ createRoute ( padId as string , req , res , null , null ) ;
138
143
} ) ;
139
144
140
145
proxyServer . on ( 'error' , ( e ) => {
@@ -149,4 +154,3 @@ db.init(() => {
149
154
} ) ;
150
155
151
156
proxyServer . listen ( settings . port ) ;
152
- } ) ;
0 commit comments