dynamic-self-register-proxy 1.0.7 → 1.0.8

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 +43 -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.8",
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
@@ -148,19 +148,20 @@ async function checkServerHealth(path, route) {
148
148
  }
149
149
 
150
150
  /**
151
- * Désenregistre un serveur (utilisé par le health check)
151
+ * Désenregistre un serveur
152
152
  * Utilise le mutex pour éviter les conflits avec les inscriptions
153
153
  * @param {string} path - Le chemin à désenregistrer
154
+ * @param {string} reason - La raison du désenregistrement (pour les logs)
154
155
  * @returns {Promise<void>}
155
156
  */
156
- async function unregisterServer(path) {
157
+ async function unregisterServer(path, reason = 'unhealthy') {
157
158
  await registrationMutex.acquire();
158
159
  try {
159
160
  const route = registry.routes.get(path);
160
161
  if (route) {
161
162
  registry.routes.delete(path);
162
163
  registry.usedPorts.delete(route.port);
163
- console.log(`[HEALTH CHECK] Unregistered ${path} (was on port ${route.port}) - server unhealthy`);
164
+ console.log(`[UNREGISTER] ${path} (was on port ${route.port}) - reason: ${reason}`);
164
165
  }
165
166
  } finally {
166
167
  registrationMutex.release();
@@ -220,7 +221,7 @@ async function performHealthChecks() {
220
221
 
221
222
  for (const { path, isHealthy } of results) {
222
223
  if (!isHealthy) {
223
- await unregisterServer(path);
224
+ await unregisterServer(path, 'failed health check');
224
225
  }
225
226
  }
226
227
 
@@ -275,15 +276,29 @@ app.post('/proxy/register', async (req, res) => {
275
276
  // Vérifie si le path existe déjà
276
277
  if (registry.routes.has(normalizedPath)) {
277
278
  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
- });
279
+
280
+ // On vérifie si le serveur existant est réellement encore en vie
281
+ // On relâche temporairement le mutex pour ne pas bloquer tout le proxy pendant le fetch
282
+ registrationMutex.release();
283
+ const isAlive = await checkServerHealth(normalizedPath, existing);
284
+ await registrationMutex.acquire();
285
+
286
+ // Si le serveur est mort, on l'écrase sans erreur
287
+ if (!isAlive) {
288
+ console.log(`[REGISTER] Path ${normalizedPath} was occupied by a dead server. Overwriting...`);
289
+ registry.usedPorts.delete(existing.port);
290
+ registry.routes.delete(normalizedPath);
291
+ } else {
292
+ return res.status(409).json({
293
+ success: false,
294
+ error: 'Path already registered',
295
+ existing: {
296
+ path: normalizedPath,
297
+ port: existing.port,
298
+ name: existing.name
299
+ }
300
+ });
301
+ }
287
302
  }
288
303
 
289
304
  // Attribution du port
@@ -852,6 +867,21 @@ const persistentProxyMiddleware = createProxyMiddleware({
852
867
  if (res.headersSent) return;
853
868
 
854
869
  console.error(`[PROXY ERROR] ${req.path}:`, err.message);
870
+
871
+ // Si la connexion est refusée, le serveur est probablement mort.
872
+ // On le désenregistre immédiatement pour éviter d'autres erreurs.
873
+ if (err.code === 'ECONNREFUSED' || err.code === 'ETIMEDOUT') {
874
+ const route = findRouteForPath(req.path);
875
+ if (route) {
876
+ const routePath = [...registry.routes.entries()]
877
+ .find(([, v]) => v === route)?.[0];
878
+ if (routePath) {
879
+ unregisterServer(routePath, `connection error (${err.code})`).catch(() => {});
880
+ console.log(`[PROXY] Auto-unregistered ${routePath} due to connection error`);
881
+ }
882
+ }
883
+ }
884
+
855
885
  res.status(502).json({
856
886
  error: 'Proxy error',
857
887
  message: err.message,