Skip to content

Commit 47812f5

Browse files
refactor(@nestsjs) replace underlying http server (express)
1 parent afa9903 commit 47812f5

File tree

7 files changed

+33
-13
lines changed

7 files changed

+33
-13
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './class-serializer.interceptors';
1+
export * from './class-serializer.interceptor';
22
export * from './decorators';

packages/core/adapters/express-adapter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { RouterMethodFactory } from '../helpers/router-method-factory';
77

88
export class ExpressAdapter implements HttpServer {
99
private readonly routerMethodFactory = new RouterMethodFactory();
10+
private httpServer = null;
1011

1112
constructor(private readonly instance) {}
1213

@@ -87,7 +88,11 @@ export class ExpressAdapter implements HttpServer {
8788
}
8889

8990
getHttpServer() {
90-
return this.instance;
91+
return this.httpServer;
92+
}
93+
94+
setHttpServer(httpServer) {
95+
this.httpServer = httpServer;
9196
}
9297

9398
getInstance() {

packages/core/nest-application.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,17 @@ export class NestApplication extends NestApplicationContext
104104
const isExpress = this.isExpress();
105105

106106
if (isHttpsEnabled && isExpress) {
107-
return https.createServer(
107+
const server = https.createServer(
108108
this.appOptions.httpsOptions,
109-
this.httpAdapter.getHttpServer(),
109+
this.httpAdapter.getInstance(),
110110
);
111+
(this.httpAdapter as ExpressAdapter).setHttpServer(server);
112+
return server;
111113
}
112114
if (isExpress) {
113-
return http.createServer(this.httpAdapter.getHttpServer());
115+
const server = http.createServer(this.httpAdapter.getInstance());
116+
(this.httpAdapter as ExpressAdapter).setHttpServer(server);
117+
return server;
114118
}
115119
return this.httpAdapter;
116120
}
@@ -169,7 +173,7 @@ export class NestApplication extends NestApplicationContext
169173
}
170174

171175
public isMiddlewareApplied(httpAdapter: HttpServer, name: string): boolean {
172-
const app = this.httpAdapter.getHttpServer();
176+
const app = this.httpAdapter.getInstance();
173177
return (
174178
!!app._router &&
175179
!!app._router.stack &&
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
export * from './client-grpc';
2+
export * from './client-mqtt';
3+
export * from './client-nats';
14
export * from './client-proxy';
2-
export * from './client-proxy-factory';
5+
export * from './client-proxy-factory';
6+
export * from './client-redis';
7+
export * from './client-tcp';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
export * from './server';
2+
export * from './server-grpc';
3+
export * from './server-mqtt';
4+
export * from './server-nats';
5+
export * from './server-redis';
6+
export * from './server-tcp';

packages/websockets/adapters/io-adapter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { CONNECTION_EVENT, DISCONNECT_EVENT } from '../constants';
99
import { MessageMappingProperties } from '../gateway-metadata-explorer';
1010

1111
export class IoAdapter implements WebSocketAdapter {
12-
private readonly httpServer: Server;
12+
protected readonly httpServer: Server;
1313

1414
constructor(appOrHttpServer?: INestApplicationContext | Server) {
1515
if (appOrHttpServer && appOrHttpServer instanceof NestApplication) {
16-
this.httpServer = appOrHttpServer.getHttpServer();
16+
this.httpServer = appOrHttpServer.getUnderlyingHttpServer();
1717
} else {
1818
this.httpServer = appOrHttpServer as Server;
1919
}
@@ -82,8 +82,9 @@ export class IoAdapter implements WebSocketAdapter {
8282
const lastElement = payload[payload.length - 1];
8383
const isAck = isFunction(lastElement);
8484
if (isAck) {
85+
const size = payload.length - 1;
8586
return {
86-
data: payload.slice(0, payload.length - 1),
87+
data: size === 1 ? payload[0] : payload.slice(0, size),
8788
ack: lastElement,
8889
};
8990
}

packages/websockets/adapters/ws-adapter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import { MessageMappingProperties } from '../gateway-metadata-explorer';
1515
let wsPackage: any = {};
1616

1717
export class WsAdapter implements WebSocketAdapter {
18-
private readonly logger = new Logger(WsAdapter.name);
19-
private readonly httpServer: Server;
18+
protected readonly logger = new Logger(WsAdapter.name);
19+
protected readonly httpServer: Server;
2020

2121
constructor(appOrHttpServer?: INestApplicationContext | Server) {
2222
wsPackage = loadPackage('ws', 'WsAdapter');
2323
if (appOrHttpServer && appOrHttpServer instanceof NestApplication) {
24-
this.httpServer = appOrHttpServer.getHttpServer();
24+
this.httpServer = appOrHttpServer.getUnderlyingHttpServer();
2525
} else {
2626
this.httpServer = appOrHttpServer as Server;
2727
}

0 commit comments

Comments
 (0)