From 17797cc05ffe6c98eaf17de4c59ed83ccfc2ae11 Mon Sep 17 00:00:00 2001 From: Aleksandr Borovskii Date: Tue, 24 Nov 2026 04:30:56 +0100 Subject: [PATCH 1/2] feat(repeater): identify the process in the connection handshake Generate a random instance ID once per process and send it in the Socket.IO handshake auth together with the CLI version and the host name, so the server can tell apart two processes that use the same Repeater ID. The fields go into auth rather than the deploy runtime payload because servers ignore unknown auth keys but reject unknown runtime properties. The startup line prints the instance ID so it can be matched with what the server logged. --- src/Config/CliInfo.spec.ts | 23 +++++++++++ src/Config/CliInfo.ts | 3 ++ src/Repeater/DefaultRepeaterServer.spec.ts | 45 ++++++++++++++++++++- src/Repeater/DefaultRepeaterServer.ts | 12 +++++- src/Repeater/ServerRepeaterLauncher.spec.ts | 20 ++++++++- src/Repeater/ServerRepeaterLauncher.ts | 6 ++- 6 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 src/Config/CliInfo.spec.ts diff --git a/src/Config/CliInfo.spec.ts b/src/Config/CliInfo.spec.ts new file mode 100644 index 00000000..39dc0266 --- /dev/null +++ b/src/Config/CliInfo.spec.ts @@ -0,0 +1,23 @@ +import { CliInfo } from './CliInfo'; + +describe('CliInfo', () => { + const uuid = + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; + + it('should assign a random UUID v4 as the instance ID', () => { + // act + const info = new CliInfo(process.cwd()); + + // assert + expect(info.instanceId).toMatch(uuid); + }); + + it('should assign a different instance ID to every process', () => { + // act + const first = new CliInfo(process.cwd()); + const second = new CliInfo(process.cwd()); + + // assert + expect(first.instanceId).not.toBe(second.instanceId); + }); +}); diff --git a/src/Config/CliInfo.ts b/src/Config/CliInfo.ts index ab136cff..67920384 100644 --- a/src/Config/CliInfo.ts +++ b/src/Config/CliInfo.ts @@ -1,11 +1,14 @@ import { sync } from 'find-up'; import path from 'node:path'; import { readFileSync } from 'node:fs'; +import { randomUUID } from 'node:crypto'; export class CliInfo { public readonly cwd: string; public readonly version: string; public readonly distribution: string | undefined; + // Per process, so the server can tell apart two processes sharing a Repeater ID. + public readonly instanceId: string = randomUUID(); constructor(cwd: string) { const packagePath = this.getPackagePath(cwd); diff --git a/src/Repeater/DefaultRepeaterServer.spec.ts b/src/Repeater/DefaultRepeaterServer.spec.ts index a2c98edf..047a5840 100644 --- a/src/Repeater/DefaultRepeaterServer.spec.ts +++ b/src/Repeater/DefaultRepeaterServer.spec.ts @@ -5,9 +5,11 @@ import { } from './DefaultRepeaterServer'; import { RepeaterErrorCodes, RepeaterServerEvents } from './RepeaterServer'; import { ProxyFactory } from '../Utils'; +import { CliInfo } from '../Config'; import { instance, mock, reset } from 'ts-mockito'; import io from 'socket.io-client'; import { EventEmitter } from 'node:events'; +import { hostname } from 'node:os'; jest.mock('socket.io-client', () => jest.fn()); @@ -67,6 +69,10 @@ describe('DefaultRepeaterServer', () => { uri: 'http://localhost:8080', token: 'dummy-token' }; + const info = { + version: '13.18.0', + instanceId: '5a1c4a52-30c4-4d4e-9f43-1d0c1f6f2d8b' + } as CliInfo; const proxyFactoryMock = mock(); const lookupMock = io as unknown as jest.MockedFunction<() => FakeSocket>; @@ -97,7 +103,11 @@ describe('DefaultRepeaterServer', () => { socket = new FakeSocket(); lookupMock.mockReturnValue(socket); - server = new DefaultRepeaterServer(instance(proxyFactoryMock), options); + server = new DefaultRepeaterServer( + instance(proxyFactoryMock), + options, + info + ); }); afterEach(() => { @@ -107,6 +117,39 @@ describe('DefaultRepeaterServer', () => { }); describe('connect', () => { + it('should identify the process in the handshake auth', async () => { + // act + await connect(); + + // assert + expect(lookupMock).toHaveBeenCalledWith( + options.uri, + expect.objectContaining({ + auth: { + token: options.token, + domain: 'example.com', + instanceId: info.instanceId, + version: info.version, + hostname: hostname() + } + }) + ); + }); + + it('should keep the instance ID by reusing the socket on reconnection', async () => { + // arrange + await connect(); + + // act + socket.emit('connect_error', connectionError()); + jest.advanceTimersByTime(6_500); + socket.emit('disconnect', 'io server disconnect'); + + // assert + expect(socket.connect).toHaveBeenCalledTimes(2); + expect(lookupMock).toHaveBeenCalledTimes(1); + }); + it('should reconnect after a retryable connection error', async () => { // arrange await connect(); diff --git a/src/Repeater/DefaultRepeaterServer.ts b/src/Repeater/DefaultRepeaterServer.ts index f29b8b9a..f5eaa859 100644 --- a/src/Repeater/DefaultRepeaterServer.ts +++ b/src/Repeater/DefaultRepeaterServer.ts @@ -1,4 +1,5 @@ import { logger, ProxyFactory } from '../Utils'; +import { CliInfo } from '../Config/CliInfo'; import { DeployCommandOptions, DeploymentRuntime, @@ -24,6 +25,7 @@ import io, { Socket } from 'socket.io-client'; import parser from 'socket.io-msgpack-parser'; import { captureException, captureMessage, withScope } from '@sentry/node'; import { EventEmitter, once } from 'node:events'; +import { hostname as osHostname } from 'node:os'; export interface DefaultRepeaterServerOptions { readonly uri: string; @@ -112,7 +114,8 @@ export class DefaultRepeaterServer implements RepeaterServer { constructor( @inject(ProxyFactory) private readonly proxyFactory: ProxyFactory, @inject(DefaultRepeaterServerOptions) - private readonly options: DefaultRepeaterServerOptions + private readonly options: DefaultRepeaterServerOptions, + @inject(CliInfo) private readonly info: CliInfo ) {} public disconnect() { @@ -176,9 +179,14 @@ export class DefaultRepeaterServer implements RepeaterServer { rejectUnauthorized: !this.options.insecure }) : undefined, + // Servers that predate instanceId/version/hostname ignore unknown auth + // keys, unlike the deploy runtime payload, which rejects them. auth: { token: this.options.token, - domain: hostname + domain: hostname, + instanceId: this.info.instanceId, + version: this.info.version, + hostname: osHostname() } }); diff --git a/src/Repeater/ServerRepeaterLauncher.spec.ts b/src/Repeater/ServerRepeaterLauncher.spec.ts index 45bd1f5e..29d4e59d 100644 --- a/src/Repeater/ServerRepeaterLauncher.spec.ts +++ b/src/Repeater/ServerRepeaterLauncher.spec.ts @@ -20,6 +20,7 @@ describe('ServerRepeaterLauncher', () => { beforeEach(() => { loggerSpy = spy(logger); when(loggerSpy.warn(anything(), anything())).thenReturn(); + when(loggerSpy.log(anything(), anything(), anything())).thenReturn(); when(mockedRepeaterServer.on(anything(), anything())).thenCall( (event: string, handler: (payload?: unknown) => unknown) => handlers.set(event, handler) @@ -34,7 +35,10 @@ describe('ServerRepeaterLauncher', () => { instance(mock()), instance(mock()), {} as RequestExecutorOptions, - { version: '1.0.0' } as CliInfo + { + version: '1.0.0', + instanceId: '5a1c4a52-30c4-4d4e-9f43-1d0c1f6f2d8b' + } as CliInfo ); }); @@ -44,6 +48,20 @@ describe('ServerRepeaterLauncher', () => { }); describe('run', () => { + it('should print the instance ID in the startup line', async () => { + // act + await launcher.run('8PY6wKetVqmfMC9K9g4A7Y'); + + // assert + verify( + loggerSpy.log( + 'Starting the Repeater (%s, instance ID: %s)...', + '1.0.0', + '5a1c4a52-30c4-4d4e-9f43-1d0c1f6f2d8b' + ) + ).once(); + }); + it('should log a reconnecting warning on a reconnection attempt', async () => { // arrange await launcher.run('8PY6wKetVqmfMC9K9g4A7Y'); diff --git a/src/Repeater/ServerRepeaterLauncher.ts b/src/Repeater/ServerRepeaterLauncher.ts index b59ce149..8038d689 100644 --- a/src/Repeater/ServerRepeaterLauncher.ts +++ b/src/Repeater/ServerRepeaterLauncher.ts @@ -108,7 +108,11 @@ export class ServerRepeaterLauncher implements RepeaterLauncher { await this.startupManager.run(() => this.close()); } - logger.log('Starting the Repeater (%s)...', this.info.version); + logger.log( + 'Starting the Repeater (%s, instance ID: %s)...', + this.info.version, + this.info.instanceId + ); this.repeaterId = repeaterId; From c46d4cbd8c29ad43f678ae7dccd8ec050c105340 Mon Sep 17 00:00:00 2001 From: Aleksandr Borovskii Date: Tue, 24 Nov 2026 09:52:01 +0100 Subject: [PATCH 2/2] refactor(repeater): shorten the instance ID and print it once deployed Use 8 random hex characters instead of a UUID. The ID only has to tell apart the few processes sharing one Repeater ID, and a short one is easier to read out to support. Print it on the "started" line, after the deployment succeeds. --- src/Config/CliInfo.spec.ts | 7 ++----- src/Config/CliInfo.ts | 5 +++-- src/Repeater/DefaultRepeaterServer.spec.ts | 2 +- src/Repeater/ServerRepeaterLauncher.spec.ts | 17 ++++++++++++----- src/Repeater/ServerRepeaterLauncher.ts | 12 ++++++------ 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Config/CliInfo.spec.ts b/src/Config/CliInfo.spec.ts index 39dc0266..cb9c6163 100644 --- a/src/Config/CliInfo.spec.ts +++ b/src/Config/CliInfo.spec.ts @@ -1,15 +1,12 @@ import { CliInfo } from './CliInfo'; describe('CliInfo', () => { - const uuid = - /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; - - it('should assign a random UUID v4 as the instance ID', () => { + it('should assign 8 random hex characters as the instance ID', () => { // act const info = new CliInfo(process.cwd()); // assert - expect(info.instanceId).toMatch(uuid); + expect(info.instanceId).toMatch(/^[0-9a-f]{8}$/); }); it('should assign a different instance ID to every process', () => { diff --git a/src/Config/CliInfo.ts b/src/Config/CliInfo.ts index 67920384..cde1cd0e 100644 --- a/src/Config/CliInfo.ts +++ b/src/Config/CliInfo.ts @@ -1,14 +1,15 @@ import { sync } from 'find-up'; import path from 'node:path'; import { readFileSync } from 'node:fs'; -import { randomUUID } from 'node:crypto'; +import { randomBytes } from 'node:crypto'; export class CliInfo { public readonly cwd: string; public readonly version: string; public readonly distribution: string | undefined; // Per process, so the server can tell apart two processes sharing a Repeater ID. - public readonly instanceId: string = randomUUID(); + // Only those few processes need to differ, so 32 bits keep it short to read out. + public readonly instanceId: string = randomBytes(4).toString('hex'); constructor(cwd: string) { const packagePath = this.getPackagePath(cwd); diff --git a/src/Repeater/DefaultRepeaterServer.spec.ts b/src/Repeater/DefaultRepeaterServer.spec.ts index 047a5840..b89c79a2 100644 --- a/src/Repeater/DefaultRepeaterServer.spec.ts +++ b/src/Repeater/DefaultRepeaterServer.spec.ts @@ -71,7 +71,7 @@ describe('DefaultRepeaterServer', () => { }; const info = { version: '13.18.0', - instanceId: '5a1c4a52-30c4-4d4e-9f43-1d0c1f6f2d8b' + instanceId: '3f9a1c07' } as CliInfo; const proxyFactoryMock = mock(); const lookupMock = io as unknown as jest.MockedFunction<() => FakeSocket>; diff --git a/src/Repeater/ServerRepeaterLauncher.spec.ts b/src/Repeater/ServerRepeaterLauncher.spec.ts index 29d4e59d..afa565ba 100644 --- a/src/Repeater/ServerRepeaterLauncher.spec.ts +++ b/src/Repeater/ServerRepeaterLauncher.spec.ts @@ -20,6 +20,7 @@ describe('ServerRepeaterLauncher', () => { beforeEach(() => { loggerSpy = spy(logger); when(loggerSpy.warn(anything(), anything())).thenReturn(); + when(loggerSpy.log(anything(), anything())).thenReturn(); when(loggerSpy.log(anything(), anything(), anything())).thenReturn(); when(mockedRepeaterServer.on(anything(), anything())).thenCall( (event: string, handler: (payload?: unknown) => unknown) => @@ -37,7 +38,7 @@ describe('ServerRepeaterLauncher', () => { {} as RequestExecutorOptions, { version: '1.0.0', - instanceId: '5a1c4a52-30c4-4d4e-9f43-1d0c1f6f2d8b' + instanceId: '3f9a1c07' } as CliInfo ); }); @@ -48,16 +49,22 @@ describe('ServerRepeaterLauncher', () => { }); describe('run', () => { - it('should print the instance ID in the startup line', async () => { - // act + it('should print the instance ID once the Repeater is deployed', async () => { + // arrange + when(mockedRepeaterServer.deploy(anything(), anything())).thenResolve({ + repeaterId: '8PY6wKetVqmfMC9K9g4A7Y' + }); await launcher.run('8PY6wKetVqmfMC9K9g4A7Y'); + // act + await handlers.get(RepeaterServerEvents.CONNECTED)?.(); + // assert verify( loggerSpy.log( - 'Starting the Repeater (%s, instance ID: %s)...', + 'The Repeater (%s) started, instance ID: %s', '1.0.0', - '5a1c4a52-30c4-4d4e-9f43-1d0c1f6f2d8b' + '3f9a1c07' ) ).once(); }); diff --git a/src/Repeater/ServerRepeaterLauncher.ts b/src/Repeater/ServerRepeaterLauncher.ts index 8038d689..ea370bb0 100644 --- a/src/Repeater/ServerRepeaterLauncher.ts +++ b/src/Repeater/ServerRepeaterLauncher.ts @@ -108,11 +108,7 @@ export class ServerRepeaterLauncher implements RepeaterLauncher { await this.startupManager.run(() => this.close()); } - logger.log( - 'Starting the Repeater (%s, instance ID: %s)...', - this.info.version, - this.info.instanceId - ); + logger.log('Starting the Repeater (%s)...', this.info.version); this.repeaterId = repeaterId; @@ -212,7 +208,11 @@ export class ServerRepeaterLauncher implements RepeaterLauncher { }, this.getRuntime() ); - logger.log('The Repeater (%s) started', this.info.version); + logger.log( + 'The Repeater (%s) started, instance ID: %s', + this.info.version, + this.info.instanceId + ); } catch { // noop }