@kotori-bot/loader 1.5.2-beta.1 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -27
- package/lib/class/loader.js +661 -248
- package/lib/class/runner.d.ts +2 -0
- package/lib/class/runner.js +313 -239
- package/lib/constants.d.ts +4 -2
- package/lib/constants.js +81 -13
- package/lib/decorators/index.d.ts +1 -0
- package/lib/decorators/index.js +161 -20
- package/lib/decorators/plugin.d.ts +7 -0
- package/lib/decorators/plugin.js +50 -0
- package/lib/decorators/utils.d.ts +2 -1
- package/lib/decorators/utils.js +132 -82
- package/lib/index.d.ts +1 -0
- package/lib/index.js +837 -26
- package/lib/service/adapter.d.ts +15 -0
- package/lib/service/adapter.js +80 -0
- package/lib/service/database.js +28 -1
- package/lib/service/file.d.ts +2 -2
- package/lib/service/file.js +65 -27
- package/lib/service/server.d.ts +50 -20
- package/lib/service/server.js +143 -90
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +29 -0
- package/lib/types/internal.d.ts +6 -0
- package/lib/types/internal.js +12 -0
- package/lib/types/server.d.ts +27 -0
- package/lib/types/server.js +29 -0
- package/lib/utils/log.js +53 -16
- package/lib/utils/logger.d.ts +1 -0
- package/lib/utils/logger.js +92 -52
- package/locales/common.json +28 -28
- package/package.json +5 -5
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Api, Adapter as OriginAdapter } from '@kotori-bot/core';
|
|
2
|
+
import { WsRouteHandler } from '../types/server';
|
|
3
|
+
export declare namespace Adapter {
|
|
4
|
+
abstract class WebSocket<T extends Api = Api> extends OriginAdapter<T> {
|
|
5
|
+
private isSetup;
|
|
6
|
+
private destroyFn?;
|
|
7
|
+
protected destroy(): void;
|
|
8
|
+
protected setup(): void;
|
|
9
|
+
abstract handle<T extends object>(data: T): void;
|
|
10
|
+
connection?: (ws: Parameters<WsRouteHandler>[0], req: Parameters<WsRouteHandler>[1]) => void;
|
|
11
|
+
start(): void;
|
|
12
|
+
stop(): void;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export default Adapter;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/loader
|
|
4
|
+
* @Version 1.6.0-beta.1
|
|
5
|
+
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/6/6 21:03:54
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
"use strict";
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/service/adapter.ts
|
|
32
|
+
var adapter_exports = {};
|
|
33
|
+
__export(adapter_exports, {
|
|
34
|
+
Adapter: () => Adapter,
|
|
35
|
+
default: () => adapter_default
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(adapter_exports);
|
|
38
|
+
var import_core = require("@kotori-bot/core");
|
|
39
|
+
var Adapter;
|
|
40
|
+
((Adapter2) => {
|
|
41
|
+
class WebSocket extends import_core.Adapter {
|
|
42
|
+
isSetup = false;
|
|
43
|
+
destroyFn;
|
|
44
|
+
destroy() {
|
|
45
|
+
if (!this.destroyFn) return;
|
|
46
|
+
this.destroyFn();
|
|
47
|
+
this.isSetup = false;
|
|
48
|
+
}
|
|
49
|
+
setup() {
|
|
50
|
+
if (this.isSetup) return;
|
|
51
|
+
this.ctx.inject("server");
|
|
52
|
+
this.destroyFn = this.ctx.server.wss(`/adapter/${this.identity}`, (ws, req) => {
|
|
53
|
+
if (this.connection) this.connection(ws, req);
|
|
54
|
+
ws.on("message", (raw) => {
|
|
55
|
+
let data;
|
|
56
|
+
try {
|
|
57
|
+
data = JSON.parse(raw.toString());
|
|
58
|
+
} catch (e) {
|
|
59
|
+
this.ctx.logger.error(`Data parse error: ${e instanceof Error ? e.message : e}`);
|
|
60
|
+
}
|
|
61
|
+
if (data) this.handle(data);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
this.isSetup = true;
|
|
65
|
+
}
|
|
66
|
+
connection;
|
|
67
|
+
start() {
|
|
68
|
+
this.setup();
|
|
69
|
+
}
|
|
70
|
+
stop() {
|
|
71
|
+
this.destroy();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
Adapter2.WebSocket = WebSocket;
|
|
75
|
+
})(Adapter || (Adapter = {}));
|
|
76
|
+
var adapter_default = Adapter;
|
|
77
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
78
|
+
0 && (module.exports = {
|
|
79
|
+
Adapter
|
|
80
|
+
});
|
package/lib/service/database.js
CHANGED
|
@@ -1,2 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/loader
|
|
4
|
+
* @Version 1.6.0-beta.1
|
|
5
|
+
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/6/6 21:03:54
|
|
10
|
+
*/
|
|
11
|
+
|
|
1
12
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
|
+
|
|
27
|
+
// src/service/database.ts
|
|
28
|
+
var database_exports = {};
|
|
29
|
+
module.exports = __toCommonJS(database_exports);
|
package/lib/service/file.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Context, Service, createConfig, loadConfig, saveConfig } from '@kotori-bot/core';
|
|
1
|
+
import { Context, JsonMap, Service, createConfig, loadConfig, saveConfig } from '@kotori-bot/core';
|
|
2
2
|
export declare class File extends Service {
|
|
3
3
|
constructor(ctx: Context);
|
|
4
4
|
getDir(): string;
|
|
5
5
|
getFile(filename: string): string;
|
|
6
|
-
load<
|
|
6
|
+
load<O = undefined, T extends Parameters<typeof loadConfig>[1] = 'json'>(filename: string, type?: T, init?: T extends 'text' ? string : object): O extends undefined ? T extends "text" ? string : JsonMap : O;
|
|
7
7
|
save(filename: string, data: Parameters<typeof saveConfig>[1], type?: Parameters<typeof saveConfig>[2]): void;
|
|
8
8
|
create(filename: string, data?: Parameters<typeof createConfig>[1], type?: Parameters<typeof createConfig>[2]): void;
|
|
9
9
|
}
|
package/lib/service/file.js
CHANGED
|
@@ -1,28 +1,66 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/loader
|
|
4
|
+
* @Version 1.6.0-beta.1
|
|
5
|
+
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/6/6 21:03:54
|
|
10
|
+
*/
|
|
11
|
+
|
|
1
12
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/service/file.ts
|
|
32
|
+
var file_exports = {};
|
|
33
|
+
__export(file_exports, {
|
|
34
|
+
File: () => File,
|
|
35
|
+
default: () => file_default
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(file_exports);
|
|
38
|
+
var import_core = require("@kotori-bot/core");
|
|
39
|
+
var import_node_path = require("path");
|
|
40
|
+
var File = class extends import_core.Service {
|
|
41
|
+
constructor(ctx) {
|
|
42
|
+
super(ctx, {}, "file");
|
|
43
|
+
}
|
|
44
|
+
getDir() {
|
|
45
|
+
return (0, import_node_path.join)(this.ctx.baseDir.data, ...this.ctx.identity ? this.ctx.identity.split("/") : []);
|
|
46
|
+
}
|
|
47
|
+
getFile(filename) {
|
|
48
|
+
return (0, import_node_path.join)(this.getDir(), filename);
|
|
49
|
+
}
|
|
50
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
51
|
+
load(filename, type, init) {
|
|
52
|
+
return (0, import_core.loadConfig)(this.getFile(filename), type, init);
|
|
53
|
+
}
|
|
54
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
55
|
+
save(filename, data, type) {
|
|
56
|
+
(0, import_core.saveConfig)(this.getFile(filename), data, type);
|
|
57
|
+
}
|
|
58
|
+
create(filename, data, type) {
|
|
59
|
+
(0, import_core.createConfig)(this.getFile(filename), data, type);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var file_default = File;
|
|
63
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
64
|
+
0 && (module.exports = {
|
|
65
|
+
File
|
|
66
|
+
});
|
package/lib/service/server.d.ts
CHANGED
|
@@ -1,33 +1,63 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import { Context, Service } from '@kotori-bot/core';
|
|
3
|
-
import { IncomingMessage } from 'node:http';
|
|
4
|
-
import
|
|
5
|
-
import Ws from 'ws';
|
|
4
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
|
+
import { HttpRouteHandler, HttpRoutes, WsRouteHandler } from '../types/server';
|
|
6
6
|
interface ServerConfig {
|
|
7
7
|
port: number;
|
|
8
8
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
interface BodyParserOptions {
|
|
10
|
+
inflate?: boolean | undefined;
|
|
11
|
+
limit?: number | string | undefined;
|
|
12
|
+
type?: string | string[] | ((req: IncomingMessage) => any) | undefined;
|
|
13
|
+
verify?(req: IncomingMessage, res: ServerResponse, buf: Buffer, encoding: string): void;
|
|
14
|
+
}
|
|
15
|
+
interface UrlencodedOptions extends BodyParserOptions {
|
|
16
|
+
extended?: boolean | undefined;
|
|
17
|
+
parameterLimit?: number | undefined;
|
|
18
|
+
}
|
|
19
|
+
interface ServeStaticOptions<R extends ServerResponse = ServerResponse> {
|
|
20
|
+
acceptRanges?: boolean | undefined;
|
|
21
|
+
cacheControl?: boolean | undefined;
|
|
22
|
+
dotfiles?: string | undefined;
|
|
23
|
+
etag?: boolean | undefined;
|
|
24
|
+
extensions?: string[] | false | undefined;
|
|
25
|
+
fallthrough?: boolean | undefined;
|
|
26
|
+
immutable?: boolean | undefined;
|
|
27
|
+
index?: boolean | string | string[] | undefined;
|
|
28
|
+
lastModified?: boolean | undefined;
|
|
29
|
+
maxAge?: number | string | undefined;
|
|
30
|
+
redirect?: boolean | undefined;
|
|
31
|
+
setHeaders?: ((res: R, path: string, stat: any) => any) | undefined;
|
|
32
|
+
}
|
|
33
|
+
interface RouterOptions {
|
|
34
|
+
caseSensitive?: boolean | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* @default false
|
|
37
|
+
* @since 4.5.0
|
|
38
|
+
*/
|
|
39
|
+
mergeParams?: boolean | undefined;
|
|
40
|
+
strict?: boolean | undefined;
|
|
41
|
+
}
|
|
42
|
+
export declare class Server extends Service<ServerConfig> implements HttpRoutes {
|
|
13
43
|
private app;
|
|
14
44
|
private server;
|
|
15
45
|
private wsServer;
|
|
16
|
-
private
|
|
46
|
+
private wsRoutes;
|
|
17
47
|
constructor(ctx: Context, config: ServerConfig);
|
|
18
48
|
start(): void;
|
|
19
49
|
stop(): void;
|
|
20
|
-
get:
|
|
21
|
-
post:
|
|
22
|
-
patch:
|
|
23
|
-
put:
|
|
24
|
-
delete:
|
|
25
|
-
all:
|
|
26
|
-
use:
|
|
27
|
-
router:
|
|
28
|
-
json: (options?:
|
|
29
|
-
static: (
|
|
30
|
-
urlencoded: (options?:
|
|
31
|
-
wss(path:
|
|
50
|
+
get<P extends string>(path: P, ...callback: HttpRouteHandler<P>[]): void;
|
|
51
|
+
post<P extends string>(path: P, ...callback: HttpRouteHandler<P>[]): void;
|
|
52
|
+
patch<P extends string>(path: P, ...callback: HttpRouteHandler<P>[]): void;
|
|
53
|
+
put<P extends string>(path: P, ...callback: HttpRouteHandler<P>[]): void;
|
|
54
|
+
delete<P extends string>(path: P, ...callback: HttpRouteHandler<P>[]): void;
|
|
55
|
+
all<P extends string>(path: P, ...callback: HttpRouteHandler<P>[]): void;
|
|
56
|
+
use<P extends string>(path: P | HttpRouteHandler | HttpRoutes, ...callback: (HttpRouteHandler<P> | HttpRoutes)[]): void;
|
|
57
|
+
router: (options?: RouterOptions) => HttpRoutes;
|
|
58
|
+
json: (options?: BodyParserOptions) => HttpRouteHandler;
|
|
59
|
+
static: (root: string, options?: ServeStaticOptions) => HttpRouteHandler;
|
|
60
|
+
urlencoded: (options?: UrlencodedOptions) => HttpRouteHandler;
|
|
61
|
+
wss<P extends string>(path: P, callback: WsRouteHandler<P>): () => boolean;
|
|
32
62
|
}
|
|
33
63
|
export default Server;
|
package/lib/service/server.js
CHANGED
|
@@ -1,93 +1,146 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/loader
|
|
4
|
+
* @Version 1.6.0-beta.1
|
|
5
|
+
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/6/6 21:03:54
|
|
10
|
+
*/
|
|
11
|
+
|
|
1
12
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
13
|
+
var __create = Object.create;
|
|
14
|
+
var __defProp = Object.defineProperty;
|
|
15
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
16
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
17
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
18
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
19
|
+
var __export = (target, all) => {
|
|
20
|
+
for (var name in all)
|
|
21
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
4
22
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
23
|
+
var __copyProps = (to, from, except, desc) => {
|
|
24
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
25
|
+
for (let key of __getOwnPropNames(from))
|
|
26
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
27
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
28
|
+
}
|
|
29
|
+
return to;
|
|
30
|
+
};
|
|
31
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
32
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
33
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
34
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
35
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
36
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
37
|
+
mod
|
|
38
|
+
));
|
|
39
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
40
|
+
|
|
41
|
+
// src/service/server.ts
|
|
42
|
+
var server_exports = {};
|
|
43
|
+
__export(server_exports, {
|
|
44
|
+
Server: () => Server,
|
|
45
|
+
default: () => server_default
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(server_exports);
|
|
48
|
+
var import_core = require("@kotori-bot/core");
|
|
49
|
+
var import_node_http = require("http");
|
|
50
|
+
var import_path_to_regexp = require("path-to-regexp");
|
|
51
|
+
var import_express = __toESM(require("express"));
|
|
52
|
+
var import_ws = __toESM(require("ws"));
|
|
53
|
+
var Server = class extends import_core.Service {
|
|
54
|
+
app;
|
|
55
|
+
server;
|
|
56
|
+
wsServer;
|
|
57
|
+
wsRoutes = /* @__PURE__ */ new Map();
|
|
58
|
+
constructor(ctx, config) {
|
|
59
|
+
super(ctx, config, "server");
|
|
60
|
+
this.app = (0, import_express.default)();
|
|
61
|
+
this.app.use(import_express.default.json());
|
|
62
|
+
this.app.use("/", (req, res, next) => {
|
|
63
|
+
let isWebui = false;
|
|
64
|
+
ctx[import_core.Symbols.modules].forEach((module2) => {
|
|
65
|
+
if (isWebui) return;
|
|
66
|
+
if (module2[0].pkg.name === "@kotori-bot/kotori-plugin-webui") isWebui = true;
|
|
67
|
+
});
|
|
68
|
+
if (isWebui || req.url !== "/") {
|
|
69
|
+
next();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
res.setHeader("Content-type", "text/html");
|
|
73
|
+
res.send(
|
|
74
|
+
/* html */
|
|
75
|
+
`<h1>Welcome to kotori!</h1>`
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
this.server = (0, import_node_http.createServer)(this.app);
|
|
79
|
+
this.wsServer = new import_ws.default.Server({ noServer: true });
|
|
80
|
+
this.server.on("upgrade", (req, socket, head) => {
|
|
81
|
+
this.wsServer.handleUpgrade(req, socket, head, (ws) => {
|
|
82
|
+
this.wsServer.emit("connection", ws, req);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
this.wsServer.on("connection", (ws, req) => {
|
|
86
|
+
let triggered = false;
|
|
87
|
+
for (const [template, list] of this.wsRoutes.entries()) {
|
|
88
|
+
if (!req.url) continue;
|
|
89
|
+
const result = (0, import_path_to_regexp.match)(template, { decode: decodeURIComponent })(req.url);
|
|
90
|
+
if (!result) continue;
|
|
91
|
+
if (!triggered) triggered = true;
|
|
92
|
+
list.forEach((callback) => {
|
|
93
|
+
callback(ws, Object.assign(req, { params: result.params }));
|
|
71
94
|
});
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
95
|
+
}
|
|
96
|
+
if (!triggered) ws.close(1002);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
start() {
|
|
100
|
+
this.server.listen(this.config.port, () => {
|
|
101
|
+
this.ctx.logger.label("server").info(`http server start at http://127.0.0.1:${this.config.port}`);
|
|
102
|
+
this.ctx.logger.label("server").info(`websocket server start at ws://127.0.0.1:${this.config.port}`);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
stop() {
|
|
106
|
+
this.wsServer.close();
|
|
107
|
+
this.server.close();
|
|
108
|
+
}
|
|
109
|
+
get(path, ...callback) {
|
|
110
|
+
this.app.get(path, ...callback);
|
|
111
|
+
}
|
|
112
|
+
post(path, ...callback) {
|
|
113
|
+
this.app.post(path, ...callback);
|
|
114
|
+
}
|
|
115
|
+
patch(path, ...callback) {
|
|
116
|
+
this.app.patch(path, ...callback);
|
|
117
|
+
}
|
|
118
|
+
put(path, ...callback) {
|
|
119
|
+
this.app.put(path, ...callback);
|
|
120
|
+
}
|
|
121
|
+
delete(path, ...callback) {
|
|
122
|
+
this.app.delete(path, ...callback);
|
|
123
|
+
}
|
|
124
|
+
all(path, ...callback) {
|
|
125
|
+
this.app.all(path, ...callback);
|
|
126
|
+
}
|
|
127
|
+
use(path, ...callback) {
|
|
128
|
+
if (typeof path === "string") this.app.use(path, ...callback);
|
|
129
|
+
else this.app.use("/", path, ...callback);
|
|
130
|
+
}
|
|
131
|
+
router = import_express.default.Router;
|
|
132
|
+
json = import_express.default.json;
|
|
133
|
+
static = import_express.default.static;
|
|
134
|
+
urlencoded = import_express.default.urlencoded;
|
|
135
|
+
wss(path, callback) {
|
|
136
|
+
const list = this.wsRoutes.get(path) || /* @__PURE__ */ new Set();
|
|
137
|
+
list.add(callback);
|
|
138
|
+
this.wsRoutes.set(path, list);
|
|
139
|
+
return () => list.delete(callback);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
var server_default = Server;
|
|
143
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
144
|
+
0 && (module.exports = {
|
|
145
|
+
Server
|
|
146
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './server';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/loader
|
|
4
|
+
* @Version 1.6.0-beta.1
|
|
5
|
+
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/6/6 21:03:54
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
"use strict";
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
|
+
|
|
27
|
+
// src/types/index.ts
|
|
28
|
+
var types_exports = {};
|
|
29
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/loader
|
|
4
|
+
* @Version 1.6.0-beta.1
|
|
5
|
+
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/6/6 21:03:54
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
"use strict";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { NextFunction, Request, Response } from 'express';
|
|
3
|
+
import { IncomingMessage } from 'node:http';
|
|
4
|
+
import Ws from 'ws';
|
|
5
|
+
type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S;
|
|
6
|
+
type GetRouteParameter<S extends string> = RemoveTail<RemoveTail<RemoveTail<S, `/${string}`>, `-${string}`>, `.${string}`>;
|
|
7
|
+
interface ParamsDictionary {
|
|
8
|
+
[key: string]: string;
|
|
9
|
+
}
|
|
10
|
+
type RouteParameters<Route extends string> = string extends Route ? ParamsDictionary : Route extends `${string}(${string}` ? ParamsDictionary : Route extends `${string}:${infer Rest}` ? (GetRouteParameter<Rest> extends never ? ParamsDictionary : GetRouteParameter<Rest> extends `${infer ParamName}?` ? {
|
|
11
|
+
[P in ParamName]?: string;
|
|
12
|
+
} : {
|
|
13
|
+
[P in GetRouteParameter<Rest>]: string;
|
|
14
|
+
}) & (Rest extends `${GetRouteParameter<Rest>}${infer Next}` ? RouteParameters<Next> : unknown) : object;
|
|
15
|
+
export type HttpRouteHandler<P extends string = string> = (req: Request<RouteParameters<P>>, res: Response, next: NextFunction) => unknown;
|
|
16
|
+
type KeyList = 'get' | 'post' | 'patch' | 'put' | 'delete' | 'all';
|
|
17
|
+
export type WsRouteHandler<P extends string = ''> = (ws: Ws, req: IncomingMessage & {
|
|
18
|
+
params: RouteParameters<P>;
|
|
19
|
+
}) => void;
|
|
20
|
+
type HttpRoutesReflect = {
|
|
21
|
+
[K in KeyList]: <P extends string>(path: P, ...callback: HttpRouteHandler<P>[]) => unknown;
|
|
22
|
+
};
|
|
23
|
+
export interface HttpRoutes extends HttpRoutesReflect {
|
|
24
|
+
use<P extends string>(path: P, ...callback: (HttpRouteHandler<P> | HttpRoutes)[]): unknown;
|
|
25
|
+
use(...callback: (HttpRouteHandler | HttpRoutes)[]): unknown;
|
|
26
|
+
}
|
|
27
|
+
export {};
|