Skip to content

Commit

Permalink
Fix functions compatible with AbstractHttpAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
cpfriend1721994 committed Apr 6, 2024
1 parent e4ce1d4 commit 72255f4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
44 changes: 34 additions & 10 deletions adapters/hyper-express-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ import * as bodyparser from 'body-parser';
import cors from 'cors';
import { Server, ServerConstructorOptions, Request, Response } from 'hyper-express';
import LiveDirectory from 'live-directory';
// import { Duplex, pipeline } from 'stream';
import { pipeline } from 'stream';
import { Duplex, pipeline } from 'stream';
import { NestHyperExpressBodyParserOptions } from '../interfaces/nest-hyper-express-body-parser-options.interface';
import { NestHyperExpressBodyParserType } from '../interfaces/nest-hyper-express-body-parser.interface';
import { ServeStaticOptions } from '../interfaces/serve-static-options.interface';
Expand All @@ -57,14 +56,22 @@ export class HyperExpressAdapter extends AbstractHttpAdapter<
> {
private readonly routerMethodFactory = new RouterMethodFactory();
private readonly logger = new Logger(HyperExpressAdapter.name);
// private readonly openConnections = new Set<Duplex>();
private readonly openConnections = new Set<Duplex>();

constructor(private opts?: ServerConstructorOptions) {
super();
super(new Server())
if (opts) {
this.opts = opts;
this.httpServer = this.instance = new Server(this.opts);
}
this.httpServer = this.instance = new Server(this.opts);
}

port: number;
once() {}
removeListener() { }

address() {
return `0.0.0.0:${this.port}`;
}

public reply(response: any, body: any, statusCode?: number) {
Expand Down Expand Up @@ -170,12 +177,17 @@ export class HyperExpressAdapter extends AbstractHttpAdapter<
fn && fn(port);
});
}
// public listen(port: any, ...args: any[]): any {
// return this.httpServer.listen(Number(port), ...args);
// }

public close() {
if (!this.httpServer) {
this.closeOpenConnections();

if (!this.instance) {
return undefined;
}
this.closeOpenConnections();
return new Promise(resolve => this.instance.close(resolve));
}

public set(...args: any[]) {
Expand Down Expand Up @@ -239,8 +251,8 @@ export class HyperExpressAdapter extends AbstractHttpAdapter<
this.opts.cert_file_name = options.httpsOptions.cert;
this.opts.passphrase = options.httpsOptions.passphrase;
}
if (this.opts && options?.forceCloseConnections) {
this.opts.auto_close = options.forceCloseConnections;
if (options?.forceCloseConnections) {
this.trackOpenConnections();
}
this.httpServer = new Server(this.opts);
}
Expand Down Expand Up @@ -428,8 +440,20 @@ export class HyperExpressAdapter extends AbstractHttpAdapter<
}
}

private trackOpenConnections() {
// this.httpServer.on('connection', (socket: Duplex) => {
// this.openConnections.add(socket);

// socket.on('close', () => this.openConnections.delete(socket));
// });
}

private closeOpenConnections() {
return new Promise(resolve => this.httpServer.close(resolve));
for (const socket of this.openConnections) {
socket.destroy();
this.openConnections.delete(socket);
}
return Promise.resolve(this.instance.close());
}

private isMiddlewareApplied(name: string): boolean {
Expand Down
3 changes: 3 additions & 0 deletions interfaces/nest-hyper-express-application.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { INestApplication, HttpServer } from '@nestjs/common';
// import type { Server as CoreHttpServer } from 'http';
// import type { Server as CoreHttpsServer } from 'https';
import type { Request, Response, Server } from 'hyper-express/types/index';
import { NestHyperExpressBodyParserOptions } from './nest-hyper-express-body-parser-options.interface';
import { NestHyperExpressBodyParserType } from './nest-hyper-express-body-parser.interface';
Expand All @@ -12,6 +14,7 @@ import { ServeStaticOptions } from './serve-static-options.interface';
* @publicApi
*/
export interface NestHyperExpressApplication<
// TServer extends CoreHttpServer | CoreHttpsServer = CoreHttpServer,
TServer extends Server = Server,
> extends INestApplication<TServer> {
/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nnmt/platform-hyper-express",
"version": "0.3.3",
"version": "0.3.4",
"description": "Hyper-express Adapter for NestJS (@nnmt/platform-hyper-express)",
"author": "Nguyen Nhat Minh Tu",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion sample/nest-sample-1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nnmt/platform-hyper-express": "^0.3.3",
"@nnmt/platform-hyper-express": "^0.3.4",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
Expand Down
2 changes: 1 addition & 1 deletion sample/nest-sample-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nnmt/platform-hyper-express": "^0.3.3",
"@nnmt/platform-hyper-express": "^0.3.4",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
Expand Down

0 comments on commit 72255f4

Please sign in to comment.