Commit 0cbb248a authored by nanahira's avatar nanahira

bump version and rework interceptor thing

parent 776ab058
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -31,19 +31,18 @@
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"peerDependencies": {
"@nestjs/common": "^8.2.6",
"@nestjs/core": "^8.2.6",
"@nestjs/common": "^8.4.6",
"@nestjs/core": "^8.4.6",
"koishi": "^4.7.2",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.5.4"
},
"devDependencies": {
"@nestjs/platform-express": "^8.2.3",
"@nestjs/platform-fastify": "^8.3.1",
"@nestjs/testing": "^8.2.3",
"@nestjs/platform-express": "^8.4.6",
"@nestjs/platform-fastify": "^8.4.6",
"@nestjs/testing": "^8.4.6",
"@types/jest": "^27.0.3",
"@types/koa": "^2.13.4",
"@types/koa-bodyparser": "^4.3.3",
"@types/koa-bodyparser": "^4.3.7",
"@types/lodash": "^4.14.175",
"@types/node": "^16.10.2",
"@types/supertest": "^2.0.11",
......@@ -55,21 +54,22 @@
"express": "^4.17.1",
"jest": "^27.4.4",
"prettier": "^2.4.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"supertest": "^6.1.6",
"ts-jest": "^27.1.1",
"typescript": "^4.6.4"
},
"dependencies": {
"@nestjs/platform-ws": "^8.1.2",
"@nestjs/websockets": "^8.1.2",
"@nestjs/platform-ws": "^8.4.6",
"@nestjs/websockets": "^8.4.6",
"@types/ws": "^8.5.3",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"koishi-decorators": "^2.1.0",
"lodash": "^4.17.21",
"typed-reflector": "^1.0.10",
"ws": "^8.6.0"
"ws": "^8.7.0"
},
"jest": {
"moduleFileExtensions": [
......
import { App, Command, Context, Plugin } from 'koishi';
import { App, Command } from 'koishi';
import {
Inject,
Injectable,
......@@ -16,18 +16,17 @@ import { KoishiMetascanService } from './providers/koishi-metascan.service';
import { KOISHI_MODULE_OPTIONS, KoishiIpSym } from './utility/koishi.constants';
import { KoishiLoggerService } from './providers/koishi-logger.service';
import { KoishiHttpDiscoveryService } from './koishi-http-discovery/koishi-http-discovery.service';
import { Filter, ReplacedContext } from './utility/replaced-context';
import { applySelector } from 'koishi-decorators';
import WebSocket from 'ws';
import { KoishiNestRouter } from './utility/koa-router';
import './utility/commander-with-interceptor';
import './utility/koishi.workarounds';
import './utility/koishi.declares';
@Injectable()
export class KoishiService
extends App
implements OnModuleInit, OnModuleDestroy
{
private readonly _interceptors: KoishiCommandInterceptorRegistration[];
constructor(
@Inject(KOISHI_MODULE_OPTIONS)
private readonly koishiModuleOptions: KoishiModuleOptions,
......@@ -40,7 +39,7 @@ export class KoishiService
port: 0,
});
this.baseDir ??= process.cwd();
this._interceptors = this.koishiModuleOptions.globalInterceptors || [];
this.interceptors = this.koishiModuleOptions.globalInterceptors;
this.router = new KoishiNestRouter();
this._nestKoaTmpInstance.use((ctx, next) => {
ctx.request.ip = ctx.req[KoishiIpSym];
......@@ -98,23 +97,4 @@ export class KoishiService
) {
return this.metascan.addInterceptors(command, interceptorDefs);
}
private cloneContext(
filter: Filter,
plugin: Plugin,
interceptors: KoishiCommandInterceptorRegistration[],
): Context {
return new ReplacedContext(filter, this, plugin, interceptors);
}
withInterceptors(interceptors: KoishiCommandInterceptorRegistration[]) {
return this.cloneContext(this.filter, this.plugin, [
...this._interceptors,
...interceptors,
]);
}
override fork(filter: Filter, _plugin: Plugin) {
return this.cloneContext(filter, _plugin, this._interceptors);
}
}
......@@ -9,6 +9,7 @@ import {
} from '../utility/koishi.constants';
import { KoishiModuleOptions } from '../utility/koishi.interfaces';
import { Context } from 'koishi';
import {} from '../utility/koishi.workarounds';
@Injectable()
export class KoishiInjectionService {
......
import { KoishiCommandInterceptorRegistration } from './koishi.interfaces';
import { IntercepterManagerService } from './koishi.workarounds';
interface ContextInterceptorMeta {
interceptors: KoishiCommandInterceptorRegistration[];
}
declare module 'koishi' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Context {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Meta extends ContextInterceptorMeta {}
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Context extends ContextInterceptorMeta {
$interceptorManager: IntercepterManagerService;
withInterceptors(
interceptors: KoishiCommandInterceptorRegistration[],
): Context;
}
}
import { Command, Commander } from 'koishi';
import { ReplacedContext } from './replaced-context';
import { Command, Commander, Context } from 'koishi';
import { KoishiService } from '../koishi.service';
import { KoishiCommandInterceptorRegistration } from './koishi.interfaces';
// command interceptor supports
const oldCommand = Commander.prototype.command;
Commander.prototype.command = function (this: Commander, ...args: any[]) {
const command: Command = oldCommand.call(this, ...args);
const ctx = this.caller;
const interceptors = (ctx as ReplacedContext)._interceptors;
const interceptors = ctx.interceptors;
if (interceptors?.length) {
(ctx.app as KoishiService).addInterceptors(command, interceptors);
}
return command;
};
export class IntercepterManagerService {
constructor(private ctx: Context) {}
protected get caller(): Context {
return this[Context.current] || this.ctx;
}
withInterceptors(
interceptors: KoishiCommandInterceptorRegistration[],
): Context {
const ctx = this.caller;
return ctx['fork']({
interceptors: [...(ctx.interceptors || []), ...interceptors],
});
}
}
Context.service('$interceptorManager', {
constructor: IntercepterManagerService,
methods: ['withInterceptors'],
});
import { Context, Plugin, Session } from 'koishi';
import { KoishiService } from '../koishi.service';
import { KoishiCommandInterceptorRegistration } from './koishi.interfaces';
export type Filter = (session: Session) => boolean;
export class ReplacedContext extends Context {
constructor(
private _filter: Filter,
private _app: KoishiService,
private __plugin: Plugin = null,
public _interceptors: KoishiCommandInterceptorRegistration[] = [],
) {
super(_filter, _app, __plugin);
}
private cloneContext(
filter: Filter,
plugin: Plugin,
interceptors: KoishiCommandInterceptorRegistration[],
): Context {
return new ReplacedContext(filter, this._app, plugin, interceptors);
}
withInterceptors(interceptors: KoishiCommandInterceptorRegistration[]) {
return this.cloneContext(this.filter, this.plugin, [
...this._interceptors,
...interceptors,
]);
}
override fork(filter: Filter, _plugin: Plugin) {
return this.cloneContext(filter, _plugin, this._interceptors);
}
}
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