Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export {
INestApplication,
INestApplicationContext,
INestMicroservice,
ITransportServer,
InjectionToken,
IntrospectionResult,
MessageEvent,
Expand Down
1 change: 1 addition & 0 deletions packages/common/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './http/index.js';
export * from './injectable.interface.js';
export * from './microservices/nest-hybrid-application-options.interface.js';
export * from './microservices/pre-request-hook.interface.js';
export * from './microservices/transport-server.interface.js';
export * from './middleware/index.js';
export * from './modules/index.js';
export * from './nest-application-context.interface.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Interface describing the shape of a transport server.
* Used as the return type of `getTransportServer()` to provide
* autocomplete without creating circular dependencies.
*
* @publicApi
*/
export interface ITransportServer {
/**
* Starts the transport server.
* @param callback Function to be called upon initialization
*/
listen(callback: (...optionalParams: unknown[]) => any): any;

/**
* Closes the transport server (stops listening on port/connection).
*/
close(): any;
}
8 changes: 8 additions & 0 deletions packages/common/interfaces/nest-microservice.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ExceptionFilter } from './exceptions/exception-filter.interface.js';
import { CanActivate } from './features/can-activate.interface.js';
import { NestInterceptor } from './features/nest-interceptor.interface.js';
import { PipeTransform } from './features/pipe-transform.interface.js';
import { ITransportServer } from './microservices/transport-server.interface.js';
import { PreRequestHook } from './microservices/pre-request-hook.interface.js';
import { INestApplicationContext } from './nest-application-context.interface.js';
import { WebSocketAdapter } from './websockets/web-socket-adapter.interface.js';
Expand Down Expand Up @@ -99,4 +100,11 @@ export interface INestMicroservice extends INestApplicationContext {
* or a group of servers if there are more than one.
*/
unwrap<T>(): T;

/**
* Returns the underlying transport server instance.
* Use this to close only the transport (port/connection) without
* triggering the full application shutdown lifecycle.
*/
getTransportServer(): ITransportServer;
}
10 changes: 10 additions & 0 deletions packages/microservices/nest-microservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
CanActivate,
ExceptionFilter,
INestMicroservice,
ITransportServer,
NestInterceptor,
PipeTransform,
PreRequestHook,
Expand Down Expand Up @@ -359,6 +360,15 @@ export class NestMicroservice
throw new Error('"unwrap" method not supported by the underlying server');
}

/**
* Returns the underlying transport server instance.
* Use this to close only the transport (port/connection) without
* triggering the full application shutdown lifecycle.
*/
public getTransportServer(): ITransportServer {
return this.serverInstance;
}

protected async closeApplication(): Promise<any> {
this.socketModule && (await this.socketModule.close());
this.microservicesModule && (await this.microservicesModule.close());
Expand Down
4 changes: 2 additions & 2 deletions packages/microservices/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
import { ConsumerSerializer } from '../interfaces/serializer.interface.js';
import { IdentitySerializer } from '../serializers/identity.serializer.js';
import { transformPatternToRoute } from '../utils/index.js';
import { Logger, type LoggerService } from '@nestjs/common';
import { ITransportServer, Logger, type LoggerService } from '@nestjs/common';
import { loadPackage, loadPackageSync } from '@nestjs/common/internal';

/**
Expand All @@ -47,7 +47,7 @@ import { loadPackage, loadPackageSync } from '@nestjs/common/internal';
export abstract class Server<
EventsMap extends Record<string, Function> = Record<string, Function>,
Status extends string = string,
> {
> implements ITransportServer {
/**
* Unique transport identifier.
*/
Expand Down
18 changes: 18 additions & 0 deletions packages/microservices/test/nest-microservice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,22 @@ describe('NestMicroservice', () => {
expect(result).toBe(instance);
});
});

it('should return the transport server instance via getTransportServer()', () => {
const strategy = new (class extends Server {
listen = vi.fn();
close = vi.fn();
on = vi.fn();
unwrap = vi.fn();
})();

const instance = new NestMicroservice(
mockContainer,
{ strategy },
mockGraphInspector,
mockAppConfig,
);

expect(instance.getTransportServer()).toBe(strategy);
});
});