Skip to content

Commit cf5fcbd

Browse files
bugfix(@nestjs/core) nest seems to fails silently nestjs#287
1 parent fde1696 commit cf5fcbd

9 files changed

Lines changed: 63 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 4.6.0
2-
- **core** add ability to inject application reference (`APP_REF` token)
2+
- **core**: [feature] add ability to inject `express` reference (`EXPRESS_REF` token)
3+
- **core**: [feature] `NestFactory.create()` now accepts third argument `HttpsOptions`
4+
- **core**: [bugfix] Nest seems to fails silently #287
5+
- **core**: [bugfix] WebSocket Gateway - Secure websockets (`wss://`) #384
6+
- **core**: [bugfix] can't select/get from context when using `NestFactory.createMicroservice()` #398
37
- **core**: [bugfix] interceptor `$stream` observable returns another observable instead of the response object #376
48
- **core**: [bugfix] `Observable.throw` from controller results in unhandled rejection promise #373
59

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface HttpsOptions {
2+
pfx?: any;
3+
key?: any;
4+
passphrase?: string;
5+
cert?: any;
6+
ca?: any;
7+
crl?: any;
8+
ciphers?: string;
9+
honorCipherOrder?: boolean;
10+
requestCert?: boolean;
11+
rejectUnauthorized?: boolean;
12+
NPNProtocols?: any;
13+
SNICallback?: (servername: string, cb: (err: Error, ctx: any) => any) => any;
14+
}

src/common/interfaces/nest-microservice.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { ExceptionFilter } from './exceptions/exception-filter.interface';
33
import { PipeTransform } from './pipe-transform.interface';
44
import { NestInterceptor } from './nest-interceptor.interface';
55
import { CanActivate } from './can-activate.interface';
6+
import { INestApplicationContext } from './nest-application-context.interface';
67

7-
export interface INestMicroservice {
8+
export interface INestMicroservice extends INestApplicationContext {
89
/**
910
* Starts the microservice.
1011
*

src/core/injector/injector.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ export class Injector {
307307
}
308308
return component;
309309
} catch (e) {
310-
if (e instanceof UndefinedDependencyException) {
311-
throw e;
310+
if (e instanceof RuntimeException) {
311+
return null;
312312
}
313-
return null;
313+
throw e;
314314
}
315315
}
316316

src/core/injector/module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { InterceptorsConsumer } from './../interceptors/interceptors-consumer';
1919
import { GuardsConsumer } from './../guards/guards-consumer';
2020
import { ModulesContainer } from './modules-container';
2121
import { Reflector } from '../services/reflector.service';
22-
import { APP_REF } from './tokens';
22+
import { EXPRESS_REF } from './tokens';
2323

2424
export interface CustomComponent {
2525
provide: any;
@@ -127,8 +127,8 @@ export class Module {
127127
}
128128

129129
public addApplicationRef(applicationRef: any) {
130-
this._components.set(APP_REF, {
131-
name: APP_REF,
130+
this._components.set(EXPRESS_REF, {
131+
name: EXPRESS_REF,
132132
metatype: applicationRef,
133133
isResolved: true,
134134
instance: applicationRef,

src/core/injector/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const APP_REF = 'APP_REF';
1+
export const EXPRESS_REF = 'EXPRESS_REF';

src/core/nest-application.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as http from 'http';
2+
import * as https from 'https';
23
import * as optional from 'optional';
34
import * as bodyParser from 'body-parser';
45
import iterate from 'iterare';
@@ -33,6 +34,7 @@ import { RoutesResolver } from './router/routes-resolver';
3334
import { MicroservicesPackageNotFoundException } from './errors/exceptions/microservices-package-not-found.exception';
3435
import { MiddlewaresContainer } from './middlewares/container';
3536
import { NestApplicationContext } from './nest-application-context';
37+
import { HttpsOptions } from '@nestjs/common/interfaces/https-options.interface';
3638

3739
const { SocketModule } =
3840
optional('@nestjs/websockets/socket-module') || ({} as any);
@@ -59,12 +61,15 @@ export class NestApplication extends NestApplicationContext
5961
private readonly microservices = [];
6062
private isInitialized = false;
6163

62-
constructor(container: NestContainer, private readonly express) {
64+
constructor(
65+
container: NestContainer,
66+
private readonly express,
67+
private readonly httpsOptions: HttpsOptions = null,
68+
) {
6369
super(container, [], null);
6470

65-
const modules = this.container.getModules().values();
66-
this.contextModule = modules.next().value;
67-
this.httpServer = http.createServer(express);
71+
this.selectContextModule();
72+
this.httpServer = this.createServer();
6873

6974
const ioAdapter = IoAdapter ? new IoAdapter(this.httpServer) : null;
7075
this.config = new ApplicationConfig(ioAdapter);
@@ -75,6 +80,18 @@ export class NestApplication extends NestApplicationContext
7580
);
7681
}
7782

83+
public selectContextModule() {
84+
const modules = this.container.getModules().values();
85+
this.contextModule = modules.next().value;
86+
}
87+
88+
public createServer(): any {
89+
if (!this.httpsOptions) {
90+
return http.createServer(this.express);
91+
}
92+
return https.createServer(this.httpsOptions, this.express);
93+
}
94+
7895
public async setupModules() {
7996
this.socketModule && this.socketModule.setup(this.container, this.config);
8097

@@ -173,7 +190,11 @@ export class NestApplication extends NestApplicationContext
173190
}
174191

175192
public async listen(port: number | string, callback?: () => void);
176-
public async listen(port: number | string, hostname: string, callback?: () => void);
193+
public async listen(
194+
port: number | string,
195+
hostname: string,
196+
callback?: () => void,
197+
);
177198
public async listen(port: number | string, ...args) {
178199
!this.isInitialized && (await this.init());
179200

src/core/nest-factory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { MetadataScanner } from './metadata-scanner';
1919
import { MicroservicesPackageNotFoundException } from './errors/exceptions/microservices-package-not-found.exception';
2020
import { NestApplicationContext } from './nest-application-context';
21+
import { HttpsOptions } from '@nestjs/common/interfaces/https-options.interface';
2122

2223
const { NestMicroservice } =
2324
optional('@nestjs/microservices/nest-microservice') || ({} as any);
@@ -41,10 +42,11 @@ export class NestFactoryStatic {
4142
public async create(
4243
module,
4344
express = ExpressAdapter.create(),
45+
httpsOptions: HttpsOptions = null,
4446
): Promise<INestApplication> {
4547
await this.initialize(module, express);
4648
return this.createNestInstance<NestApplication>(
47-
new NestApplication(this.container, express),
49+
new NestApplication(this.container, express, httpsOptions),
4850
);
4951
}
5052

src/microservices/nest-microservice.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ import { CustomTransportStrategy } from '@nestjs/microservices';
2222
import { Module } from '@nestjs/core/injector/module';
2323
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
2424
import { OnModuleDestroy } from '@nestjs/common/interfaces';
25+
import { NestApplicationContext } from '@nestjs/core/nest-application-context';
2526

2627
const { SocketModule } =
2728
optional('@nestjs/websockets/socket-module') || ({} as any);
2829
const { IoAdapter } =
2930
optional('@nestjs/websockets/adapters/io-adapter') || ({} as any);
3031

31-
export class NestMicroservice implements INestMicroservice {
32+
export class NestMicroservice extends NestApplicationContext
33+
implements INestMicroservice {
3234
private readonly logger = new Logger(NestMicroservice.name, true);
3335
private readonly microservicesModule = new MicroservicesModule();
3436
private readonly socketModule = SocketModule ? new SocketModule() : null;
@@ -41,9 +43,11 @@ export class NestMicroservice implements INestMicroservice {
4143
private isInitHookCalled = false;
4244

4345
constructor(
44-
private readonly container: NestContainer,
46+
container: NestContainer,
4547
config: MicroserviceConfiguration = {},
4648
) {
49+
super(container, [], null);
50+
4751
const ioAdapter = IoAdapter ? new IoAdapter() : null;
4852
this.config = new ApplicationConfig(ioAdapter);
4953

0 commit comments

Comments
 (0)