dynamic-self-register-proxy 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/proxy.js +47 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamic-self-register-proxy",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Dynamic reverse proxy with self-registration API - applications can register themselves and receive an automatically assigned port",
5
5
  "main": "proxy-client.js",
6
6
  "bin": {
package/proxy.js CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  const express = require('express');
4
4
  const { createProxyMiddleware } = require('http-proxy-middleware');
5
+ const { setupLogging } = require('./logger');
6
+
7
+ // Initialisation du logging
8
+ setupLogging('PROXY');
5
9
 
6
10
  const app = express();
7
11
  app.use(express.json());
@@ -148,19 +152,20 @@ async function checkServerHealth(path, route) {
148
152
  }
149
153
 
150
154
  /**
151
- * Désenregistre un serveur (utilisé par le health check)
155
+ * Désenregistre un serveur
152
156
  * Utilise le mutex pour éviter les conflits avec les inscriptions
153
157
  * @param {string} path - Le chemin à désenregistrer
158
+ * @param {string} reason - La raison du désenregistrement (pour les logs)
154
159
  * @returns {Promise<void>}
155
160
  */
156
- async function unregisterServer(path) {
161
+ async function unregisterServer(path, reason = 'unhealthy') {
157
162
  await registrationMutex.acquire();
158
163
  try {
159
164
  const route = registry.routes.get(path);
160
165
  if (route) {
161
166
  registry.routes.delete(path);
162
167
  registry.usedPorts.delete(route.port);
163
- console.log(`[HEALTH CHECK] Unregistered ${path} (was on port ${route.port}) - server unhealthy`);
168
+ console.log(`[UNREGISTER] ${path} (was on port ${route.port}) - reason: ${reason}`);
164
169
  }
165
170
  } finally {
166
171
  registrationMutex.release();
@@ -220,7 +225,7 @@ async function performHealthChecks() {
220
225
 
221
226
  for (const { path, isHealthy } of results) {
222
227
  if (!isHealthy) {
223
- await unregisterServer(path);
228
+ await unregisterServer(path, 'failed health check');
224
229
  }
225
230
  }
226
231
 
@@ -275,15 +280,29 @@ app.post('/proxy/register', async (req, res) => {
275
280
  // Vérifie si le path existe déjà
276
281
  if (registry.routes.has(normalizedPath)) {
277
282
  const existing = registry.routes.get(normalizedPath);
278
- return res.status(409).json({
279
- success: false,
280
- error: 'Path already registered',
281
- existing: {
282
- path: normalizedPath,
283
- port: existing.port,
284
- name: existing.name
285
- }
286
- });
283
+
284
+ // On vérifie si le serveur existant est réellement encore en vie
285
+ // On relâche temporairement le mutex pour ne pas bloquer tout le proxy pendant le fetch
286
+ registrationMutex.release();
287
+ const isAlive = await checkServerHealth(normalizedPath, existing);
288
+ await registrationMutex.acquire();
289
+
290
+ // Si le serveur est mort, on l'écrase sans erreur
291
+ if (!isAlive) {
292
+ console.log(`[REGISTER] Path ${normalizedPath} was occupied by a dead server. Overwriting...`);
293
+ registry.usedPorts.delete(existing.port);
294
+ registry.routes.delete(normalizedPath);
295
+ } else {
296
+ return res.status(409).json({
297
+ success: false,
298
+ error: 'Path already registered',
299
+ existing: {
300
+ path: normalizedPath,
301
+ port: existing.port,
302
+ name: existing.name
303
+ }
304
+ });
305
+ }
287
306
  }
288
307
 
289
308
  // Attribution du port
@@ -852,6 +871,21 @@ const persistentProxyMiddleware = createProxyMiddleware({
852
871
  if (res.headersSent) return;
853
872
 
854
873
  console.error(`[PROXY ERROR] ${req.path}:`, err.message);
874
+
875
+ // Si la connexion est refusée, le serveur est probablement mort.
876
+ // On le désenregistre immédiatement pour éviter d'autres erreurs.
877
+ if (err.code === 'ECONNREFUSED' || err.code === 'ETIMEDOUT') {
878
+ const route = findRouteForPath(req.path);
879
+ if (route) {
880
+ const routePath = [...registry.routes.entries()]
881
+ .find(([, v]) => v === route)?.[0];
882
+ if (routePath) {
883
+ unregisterServer(routePath, `connection error (${err.code})`).catch(() => {});
884
+ console.log(`[PROXY] Auto-unregistered ${routePath} due to connection error`);
885
+ }
886
+ }
887
+ }
888
+
855
889
  res.status(502).json({
856
890
  error: 'Proxy error',
857
891
  message: err.message,