Commit 3b23fc5d authored by nanahira's avatar nanahira

rework with http server

parent 7eecf9c8
This diff is collapsed.
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
"peerDependencies": { "peerDependencies": {
"@nestjs/common": "^9.0.3 || ^8.0.0", "@nestjs/common": "^9.0.3 || ^8.0.0",
"@nestjs/core": "^9.0.3 || ^8.0.0", "@nestjs/core": "^9.0.3 || ^8.0.0",
"koishi": "^4.11.1", "koishi": "^4.11.4",
"rxjs": "^7.5.5" "rxjs": "^7.5.5"
}, },
"devDependencies": { "devDependencies": {
......
import { Command, Context } from 'koishi'; import { Command, Context, Router } from 'koishi';
import { import {
Inject, Inject,
Injectable, Injectable,
...@@ -10,15 +10,13 @@ import { ...@@ -10,15 +10,13 @@ import {
KoishiCommandInterceptorRegistration, KoishiCommandInterceptorRegistration,
KoishiModuleOptions, KoishiModuleOptions,
} from './utility/koishi.interfaces'; } from './utility/koishi.interfaces';
import { createServer, Server } from 'http'; import { Server } from 'http';
import Koa from 'koa'; import Koa from 'koa';
import KoaBodyParser from 'koa-bodyparser'; import KoaBodyParser from 'koa-bodyparser';
import { KoishiMetascanService } from './providers/koishi-metascan.service'; import { KoishiMetascanService } from './providers/koishi-metascan.service';
import { KOISHI_MODULE_OPTIONS, KoishiIpSym } from './utility/koishi.constants'; import { KOISHI_MODULE_OPTIONS, KoishiIpSym } from './utility/koishi.constants';
import { KoishiLoggerService } from './providers/koishi-logger.service'; import { KoishiLoggerService } from './providers/koishi-logger.service';
import { KoishiHttpDiscoveryService } from './koishi-http-discovery/koishi-http-discovery.service'; import { KoishiHttpDiscoveryService } from './koishi-http-discovery/koishi-http-discovery.service';
import WebSocket from 'ws';
import { KoishiNestRouter } from './utility/koa-router';
import './utility/koishi.workarounds'; import './utility/koishi.workarounds';
import './utility/koishi.declares'; import './utility/koishi.declares';
import { selectContext } from 'koishi-thirdeye'; import { selectContext } from 'koishi-thirdeye';
...@@ -43,7 +41,6 @@ export class KoishiService ...@@ -43,7 +41,6 @@ export class KoishiService
}); });
this.baseDir ??= process.cwd(); this.baseDir ??= process.cwd();
this.interceptors = this.koishiModuleOptions.globalInterceptors; this.interceptors = this.koishiModuleOptions.globalInterceptors;
this.router = new KoishiNestRouter();
this._nestKoaTmpInstance.use((ctx, next) => { this._nestKoaTmpInstance.use((ctx, next) => {
ctx.request.ip = ctx.req[KoishiIpSym]; ctx.request.ip = ctx.req[KoishiIpSym];
return next(); return next();
...@@ -63,18 +60,6 @@ export class KoishiService ...@@ -63,18 +60,6 @@ export class KoishiService
this.router._http = httpServer; this.router._http = httpServer;
} else { } else {
this.logger('app').info('No http adapters found from Nest application.'); this.logger('app').info('No http adapters found from Nest application.');
const tmpServer = createServer(this._nestKoaTmpInstance.callback());
this.router._http = tmpServer;
this.router._ws = new WebSocket.Server({
server: tmpServer,
});
this.router._ws.on('connection', (socket, request) => {
for (const manager of this.router.wsStack) {
if (manager.accept(socket, request)) return;
}
socket.close();
});
} }
} }
......
import KoaRouter from '@koa/router';
import { Context, MaybeArray, remove, WebSocketLayer } from 'koishi';
import { IncomingMessage } from 'http';
import WebSocket from 'ws';
export class KoishiNestRouter extends KoaRouter {
wsStack: WebSocketLayer[] = [];
/**
* hack into router methods to make sure that koa middlewares are disposable
*/
override register(...args: Parameters<KoaRouter['register']>) {
const layer = super.register(...args);
const context: Context = this[Context.current];
context?.state.disposables.push(() => {
remove(this.stack, layer);
});
return layer;
}
ws(
path: MaybeArray<string | RegExp>,
callback?: (socket: WebSocket, request: IncomingMessage) => void,
) {
const layer = new WebSocketLayer(this, path, callback);
this.wsStack.push(layer);
const context: Context = this[Context.current];
context?.state.disposables.push(() => layer.close());
return layer;
}
}
...@@ -186,13 +186,4 @@ describe('Koishi in Nest.js', () => { ...@@ -186,13 +186,4 @@ describe('Koishi in Nest.js', () => {
expect(command).toBeDefined(); expect(command).toBeDefined();
expect(command.execute({ options: {} })).resolves.toBe('miiii'); expect(command.execute({ options: {} })).resolves.toBe('miiii');
}); });
it('should handle partial dep', async () => {
koishiApp['ping'] = { ping: 'pong' };
expect(await koishiApp.waterfall(<EventName>'ping')).toBe('pong');
koishiApp['ping'] = undefined;
expect(await koishiApp.waterfall(<EventName>'ping')).toBeUndefined();
koishiApp['ping'] = { ping: 'pong' };
expect(await koishiApp.waterfall(<EventName>'ping')).toBe('pong');
});
}); });
...@@ -96,14 +96,6 @@ export class KoishiTestService { ...@@ -96,14 +96,6 @@ export class KoishiTestService {
async onAbstract(@PutValue('{{abstract.content}}') content: string) { async onAbstract(@PutValue('{{abstract.content}}') content: string) {
return content; return content;
} }
@UsingService('ping')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
@UseEvent('ping')
async onPing() {
return 'pong';
}
} }
@RegisterSchema() @RegisterSchema()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment