Skip to content

Commit 71c3ee2

Browse files
feat: resolve socket options
1 parent 3e215c2 commit 71c3ee2

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

__tests__/services/socket-test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,27 @@ describe('services/socket', () => {
228228
expect(ioServerUseStub).to.calledTwice;
229229
});
230230

231+
it('should correctly start microservice with async io params', async () => {
232+
const listenStub = sandbox.stub(ms.getIoServer(), 'listen');
233+
234+
// Configure io server stubs
235+
const ioServerUseStub = sandbox.stub(ms.getIoServer(), 'use');
236+
const ioServerOnStub = sandbox.stub(ms.getIoServer(), 'on');
237+
238+
ms.setParams({
239+
ioServerOptions: () => ioServerOptions,
240+
});
241+
242+
// Skip startWorkers
243+
sandbox.stub(axios, 'request').rejects(new Error('ECONNREFUSED'));
244+
245+
await ms.start();
246+
247+
expect(listenStub).to.calledOnceWith(ms.getHttpServer(), ioServerOptions);
248+
expect(ioServerOnStub).to.calledOnce;
249+
expect(ioServerUseStub).to.calledTwice;
250+
});
251+
231252
it('should correctly apply connection middlewares', async () => {
232253
const socket = { handshake: { headers: {} }, data: {} } as IoSocket;
233254
const nextStub = sandbox.stub();

src/interfaces/services/i-socket.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
IAbstractMicroserviceParams,
88
} from '@interfaces/services/i-abstract-microservice';
99
import type { TJsonRPC } from '@interfaces/services/i-gateway';
10+
import type SocketMs from '@services/socket';
1011

1112
export interface ISocketOptions extends IAbstractMicroserviceOptions {
1213
listener: string;
@@ -16,7 +17,9 @@ export interface ISocketOptions extends IAbstractMicroserviceOptions {
1617
}
1718

1819
export interface ISocketParams extends IAbstractMicroserviceParams {
19-
ioServerOptions?: Partial<ServerOptions>;
20+
ioServerOptions?:
21+
| Partial<ServerOptions>
22+
| ((ms: SocketMs) => Partial<ServerOptions> | Promise<Partial<ServerOptions>>);
2023
signRoomOptions: { secretKey: string };
2124
makeRoomName?: (
2225
roomName: string,

src/services/socket.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,18 @@ class Socket extends AbstractMicroservice {
533533
/**
534534
* Run microservice
535535
*/
536-
public start(): Promise<void | void[]> {
536+
public async start(): Promise<void | void[]> {
537537
const { name, version, listener, eventWorkers } = this.options;
538538
const { ioServerOptions } = this.params;
539539
const [host, port] = listener.split(':');
540540

541541
this.logDriver(() => `${name} start. Version: ${version}`, LogType.INFO);
542542

543543
this.configureIoServer();
544-
this.ioServer.listen(this.httpServer, ioServerOptions);
544+
this.ioServer.listen(
545+
this.httpServer,
546+
typeof ioServerOptions === 'function' ? await ioServerOptions(this) : ioServerOptions,
547+
);
545548

546549
const server = this.httpServer.listen(Number(port), host, () => {
547550
this.logDriver(

0 commit comments

Comments
 (0)