fix(strcuture): refactor responsibilities

This commit is contained in:
2025-05-19 17:28:05 +00:00
parent 8fb67922a5
commit 465148d553
62 changed files with 1414 additions and 2066 deletions

View File

@ -1,5 +1,5 @@
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { SmartProxy } from '../ts/index.js';
import { SmartProxy, type IRouteConfig } from '../ts/index.js';
/**
* Test that verifies mutex prevents race conditions during concurrent route updates
@ -42,10 +42,10 @@ tap.test('should handle concurrent route updates without race conditions', async
},
action: {
type: 'forward' as const,
targetUrl: `https://localhost:${3001 + i}`,
target: { host: 'localhost', port: 3001 + i },
tls: {
mode: 'terminate' as const,
certificate: 'auto'
certificate: 'auto' as const
}
}
}
@ -57,7 +57,7 @@ tap.test('should handle concurrent route updates without race conditions', async
// Verify final state
const currentRoutes = proxy['settings'].routes;
tools.expect(currentRoutes.length).toEqual(2); // Initial route + last update
expect(currentRoutes.length).toEqual(2); // Initial route + last update
await proxy.stop();
});
@ -95,7 +95,7 @@ tap.test('should serialize route updates with mutex', async (tools) => {
maxConcurrent = Math.max(maxConcurrent, concurrent);
// If mutex is working, only one update should run at a time
tools.expect(concurrent).toEqual(1);
expect(concurrent).toEqual(1);
const result = await originalUpdateRoutes(routes);
updateEndCount++;
@ -121,9 +121,9 @@ tap.test('should serialize route updates with mutex', async (tools) => {
await Promise.all(updates);
// All updates should have completed
tools.expect(updateStartCount).toEqual(5);
tools.expect(updateEndCount).toEqual(5);
tools.expect(maxConcurrent).toEqual(1); // Mutex ensures only one at a time
expect(updateStartCount).toEqual(5);
expect(updateEndCount).toEqual(5);
expect(maxConcurrent).toEqual(1); // Mutex ensures only one at a time
await proxy.stop();
});
@ -141,10 +141,10 @@ tap.test('should preserve challenge route state during cert manager recreation',
match: { ports: [443] },
action: {
type: 'forward' as const,
targetUrl: 'https://localhost:3001',
target: { host: 'localhost', port: 3001 },
tls: {
mode: 'terminate' as const,
certificate: 'auto'
certificate: 'auto' as const
}
}
}],
@ -167,31 +167,31 @@ tap.test('should preserve challenge route state during cert manager recreation',
await proxy.start();
// Initial creation
tools.expect(certManagerCreationCount).toEqual(1);
expect(certManagerCreationCount).toEqual(1);
// Multiple route updates
for (let i = 0; i < 3; i++) {
await proxy.updateRoutes([
...settings.routes,
...settings.routes as IRouteConfig[],
{
name: `dynamic-route-${i}`,
match: { ports: [9000 + i] },
action: {
type: 'forward' as const,
targetUrl: `http://localhost:${5000 + i}`
target: { host: 'localhost', port: 5000 + i }
}
}
]);
}
// Certificate manager should be recreated for each update
tools.expect(certManagerCreationCount).toEqual(4); // 1 initial + 3 updates
expect(certManagerCreationCount).toEqual(4); // 1 initial + 3 updates
// State should be preserved (challenge route active)
const globalState = proxy['globalChallengeRouteActive'];
tools.expect(globalState).toBeDefined();
expect(globalState).toBeDefined();
await proxy.stop();
});
export default tap;
export default tap.start();