mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
feat(core): expose listening stream from http adapter host
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { Observable, Subject } from 'rxjs';
|
||||
import { AbstractHttpAdapter } from '../adapters/http-adapter';
|
||||
|
||||
/**
|
||||
@@ -16,6 +17,8 @@ export class HttpAdapterHost<
|
||||
T extends AbstractHttpAdapter = AbstractHttpAdapter,
|
||||
> {
|
||||
private _httpAdapter?: T;
|
||||
private _listen$ = new Subject<void>();
|
||||
private isListening = false;
|
||||
|
||||
/**
|
||||
* Accessor for the underlying `HttpAdapter`
|
||||
@@ -35,4 +38,31 @@ export class HttpAdapterHost<
|
||||
get httpAdapter(): T {
|
||||
return this._httpAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Observable that allows to subscribe to the `listen` event.
|
||||
* This event is emitted when the HTTP application is listening for incoming requests.
|
||||
*/
|
||||
get listen$(): Observable<void> {
|
||||
return this._listen$.asObservable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the listening state of the application.
|
||||
*/
|
||||
set listening(listening: boolean) {
|
||||
this.isListening = listening;
|
||||
|
||||
if (listening) {
|
||||
this._listen$.next();
|
||||
this._listen$.complete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a boolean indicating whether the application is listening for incoming requests.
|
||||
*/
|
||||
get listening(): boolean {
|
||||
return this.isListening;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,8 +294,12 @@ export class NestApplication
|
||||
public async listen(port: number | string, hostname: string): Promise<any>;
|
||||
public async listen(port: number | string, ...args: any[]): Promise<any> {
|
||||
this.assertNotInPreviewMode('listen');
|
||||
!this.isInitialized && (await this.init());
|
||||
|
||||
if (!this.isInitialized) {
|
||||
await this.init();
|
||||
}
|
||||
|
||||
const httpAdapterHost = this.container.getHttpAdapterHostRef();
|
||||
return new Promise((resolve, reject) => {
|
||||
const errorHandler = (e: any) => {
|
||||
this.logger.error(e?.toString?.());
|
||||
@@ -323,6 +327,8 @@ export class NestApplication
|
||||
if (address) {
|
||||
this.httpServer.removeListener('error', errorHandler);
|
||||
this.isListening = true;
|
||||
|
||||
httpAdapterHost.listening = true;
|
||||
resolve(this.httpServer);
|
||||
}
|
||||
if (isCallbackInOriginalArgs) {
|
||||
|
||||
@@ -2,11 +2,27 @@ import { expect } from 'chai';
|
||||
import { HttpAdapterHost } from '../../helpers/http-adapter-host';
|
||||
|
||||
describe('HttpAdapterHost', () => {
|
||||
const applicationRefHost = new HttpAdapterHost();
|
||||
let applicationRefHost: HttpAdapterHost;
|
||||
beforeEach(() => {
|
||||
applicationRefHost = new HttpAdapterHost();
|
||||
});
|
||||
|
||||
it('should wrap application reference', () => {
|
||||
const ref = {};
|
||||
applicationRefHost.httpAdapter = ref as any;
|
||||
|
||||
expect(applicationRefHost.httpAdapter).to.be.eql(ref);
|
||||
});
|
||||
|
||||
it('should emit listen event when listening is set to true', done => {
|
||||
applicationRefHost.listen$.subscribe(() => {
|
||||
expect(applicationRefHost.listening).to.be.true;
|
||||
done();
|
||||
});
|
||||
applicationRefHost.listening = true;
|
||||
});
|
||||
|
||||
it('listening should return false if the application isnt listening yet', () => {
|
||||
expect(applicationRefHost.listening).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user