Skip to content

Commit 1ecf063

Browse files
committed
Revived etherpad proxy
1 parent 2fa2dbf commit 1ecf063

File tree

8 files changed

+1622
-2729
lines changed

8 files changed

+1622
-2729
lines changed

.github/workflows/lint-package-lock.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
node_modules/*
1+
node_modules/*
22
dirty.db
33
settings.json
4+
.idea

app.js renamed to app.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

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'
78
// load the settings
89
const loadSettings = () => {
9-
const fs = require('fs');
1010
let settings;
1111
try {
1212
settings = fs.readFileSync('settings.json', 'utf8');
@@ -25,16 +25,19 @@ if (settings.dbType === 'dirty') console.error('DirtyDB is not recommend for pro
2525
const backendIds = Object.keys(settings.backends);
2626

2727
// An object of our proxy instances
28-
const proxies = {};
28+
const proxies: { [key: string]: httpProxy<http.IncomingMessage, http.ServerResponse<http.IncomingMessage>> } = {};
2929

3030
// Making availableBackend globally available.
31-
let availableBackends;
31+
let availableBackends: {
32+
up: Array<string>,
33+
available: Array<string>,
34+
};
3235
(async () => {
3336
checkAvailability(
3437
settings.backends,
3538
settings.checkInterval,
3639
settings.maxPadsPerInstance);
37-
});
40+
})();
3841
// And now grab them every X duration
3942
setInterval(async () => {
4043
availableBackends = await checkAvailability(
@@ -44,21 +47,21 @@ setInterval(async () => {
4447
}, settings.checkInterval);
4548

4649
// Creating our database connection
47-
const db = new ueberdb.Database(settings.dbType, settings.dbSettings);
50+
const db = new Database(settings.dbType, settings.dbSettings);
4851

4952
// 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) => {
5154
if (res) {
5255
// console.log('backend: ', backend);
5356
if (proxies[backend]) {
54-
proxies[backend].web(req, res, (e) => {
57+
proxies[backend].web(req, res,{}, (e) => {
5558
console.error(e);
5659
});
5760
}
5861
}
5962
if (socket && head) {
6063
if (proxies[backend]) {
61-
proxies[backend].ws(req, socket, head, (e) => {
64+
proxies[backend].ws(req, socket, head, {}, (e) => {
6265
console.error(e);
6366
});
6467
}
@@ -67,7 +70,7 @@ const initiateRoute = (backend, req, res, socket, head) => {
6770

6871
// Create dynamically assigned routes based on padIds and ensure that a route for
6972
// 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) => {
7174
// If the route isn't for a specific padID IE it's for a static file
7275
// we can use any of the backends but now let's use the first :)
7376
if (!padId) {
@@ -77,6 +80,7 @@ const createRoute = (padId, req, res, socket, head) => {
7780
}
7881

7982
// pad specific backend required, do we have a backend already?
83+
// @ts-ignore
8084
db.get(`padId:${padId}`, (e, r) => {
8185
if (r && r.backend) {
8286
// console.log(`database hit: ${padId} <> ${r.backend}`);
@@ -112,11 +116,12 @@ const createRoute = (padId, req, res, socket, head) => {
112116
});
113117
};
114118

115-
db.init(() => {
119+
db.init();
120+
116121
// Create the backends.
117122
for (const backendId of backendIds) {
118123
/* eslint-disable-next-line new-cap */
119-
proxies[backendId] = new httpProxy.createProxyServer({
124+
proxies[backendId] = httpProxy.createProxyServer({
120125
target: {
121126
host: settings.backends[backendId].host,
122127
port: settings.backends[backendId].port,
@@ -125,16 +130,16 @@ db.init(() => {
125130
}
126131
// Create the routes for web traffic to those backends.
127132
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];
131136
console.log(`initial request to /p/${padId}`);
132137
}
133-
if (!padId) {
138+
if (padId === null) {
134139
const searchParams = new URLSearchParams(req.url);
135140
padId = searchParams.get('/socket.io/?padId');
136141
}
137-
createRoute(padId, req, res, null, null);
142+
createRoute(padId as string, req, res, null, null);
138143
});
139144

140145
proxyServer.on('error', (e) => {
@@ -149,4 +154,3 @@ db.init(() => {
149154
});
150155

151156
proxyServer.listen(settings.port);
152-
});

checkAvailability.js renamed to checkAvailability.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
'use strict';
1+
import superagent from 'superagent'
22

3-
exports.checkAvailability = async (backends, interval, maxPadsPerInstance) => {
4-
const superagent = require('superagent');
3+
export type Backend = {
4+
host: string,
5+
port: number,
6+
}
7+
8+
export type Backends = {
9+
[key: string]: Backend,
10+
}
11+
12+
13+
14+
export const checkAvailability = async (backends: Backends, _interval: number, maxPadsPerInstance: number) => {
515
let available = Object.keys(backends);
616
let up = Object.keys(backends);
717
for (const backendId of Object.keys(backends)) {

0 commit comments

Comments
 (0)