@im-more/dsh-onebot 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/cordis.patch.yml +11 -0
- package/lib/index.js +2003 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +55 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +140 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +40 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +33 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/cqcode.d.ts +29 -0
- package/lib/types/cqcode.d.ts.map +1 -0
- package/lib/types/cqcode.js +73 -0
- package/lib/types/cqcode.js.map +1 -0
- package/lib/types/decode.d.ts +106 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +433 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +32 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +117 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +18 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +27 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +7 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +6 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/modes/segment.d.ts +16 -0
- package/lib/types/modes/segment.d.ts.map +1 -0
- package/lib/types/modes/segment.js +80 -0
- package/lib/types/modes/segment.js.map +1 -0
- package/lib/types/modes/shared.d.ts +101 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +208 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/request.d.ts +64 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +108 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +93 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +161 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/transport/http.d.ts +29 -0
- package/lib/types/transport/http.d.ts.map +1 -0
- package/lib/types/transport/http.js +158 -0
- package/lib/types/transport/http.js.map +1 -0
- package/lib/types/transport/shared.d.ts +48 -0
- package/lib/types/transport/shared.d.ts.map +1 -0
- package/lib/types/transport/shared.js +75 -0
- package/lib/types/transport/shared.js.map +1 -0
- package/lib/types/transport/ws-reverse.d.ts +42 -0
- package/lib/types/transport/ws-reverse.d.ts.map +1 -0
- package/lib/types/transport/ws-reverse.js +178 -0
- package/lib/types/transport/ws-reverse.js.map +1 -0
- package/lib/types/transport/ws.d.ts +40 -0
- package/lib/types/transport/ws.d.ts.map +1 -0
- package/lib/types/transport/ws.js +233 -0
- package/lib/types/transport/ws.js.map +1 -0
- package/lib/types/types.d.ts +495 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +191 -0
- package/lib/types/types.js.map +1 -0
- package/package.json +68 -3
- package/src/bot.ts +230 -0
- package/src/config.ts +66 -0
- package/src/cqcode.ts +85 -0
- package/src/decode.ts +524 -0
- package/src/errors.ts +134 -0
- package/src/identity.ts +34 -0
- package/src/index.ts +18 -0
- package/src/modes/segment.ts +109 -0
- package/src/modes/shared.ts +277 -0
- package/src/request.ts +128 -0
- package/src/send.ts +233 -0
- package/src/transport/http.ts +221 -0
- package/src/transport/shared.ts +98 -0
- package/src/transport/ws-reverse.ts +236 -0
- package/src/transport/ws.ts +286 -0
- package/src/types.ts +743 -0
- package/README.md +0 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send.js","sourceRoot":"","sources":["../../src/send.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,aAAa,GASd,MAAM,oCAAoC,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,U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CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;SAC5D,CAAA;IACH,CAAC,CAAC,CAAA;IACF,OAAO,EAAE,IAAI,EAAE,CAAA;AACjB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,IAAoB,EACpB,GAAc,EACd,IAAa,EACb,EAAU;IAEV,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;IAC3C,OAAO,aAAa,CAAC,GAAG,EAAE,aAAa,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;AAC3D,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,IAAoB,EACpB,OAAmB,EACnB,KAAa;IAEb,MAAM,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA;AAC9D,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,IAAoB,EACpB,OAAmB,EACnB,KAAa;IAEb,MAAM,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,CAAA;AAC/D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,OAAO,aAAa,CAAC,GAAG,CAAC,CAAA;AAC3B,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP transport (`protocol: 'http'`): an outbound `POST {endpoint}/{action}` client and an inbound event route.
|
|
3
|
+
*
|
|
4
|
+
* Both directions run at once, as in the upstream plugin. The outbound call authenticates with the standard
|
|
5
|
+
* `Authorization: Bearer` header; the inbound route checks `X-Signature` (HMAC-SHA1 over the re-serialized parsed body,
|
|
6
|
+
* the upstream behaviour implementations in the field verify) and `X-Self-ID`, and answers `204` only after the host
|
|
7
|
+
* accepted the event.
|
|
8
|
+
*/
|
|
9
|
+
import type { OneBotBot } from '../bot.ts';
|
|
10
|
+
/** The HTTP transport of one bot. */
|
|
11
|
+
export declare class OneBotHttp {
|
|
12
|
+
private readonly bot;
|
|
13
|
+
private dispose;
|
|
14
|
+
private warnedNoSecret;
|
|
15
|
+
constructor(bot: OneBotBot);
|
|
16
|
+
/**
|
|
17
|
+
* Claim the event route and install the action caller.
|
|
18
|
+
*/
|
|
19
|
+
start(): Promise<void>;
|
|
20
|
+
/** Release the event route. */
|
|
21
|
+
stop(): Promise<void>;
|
|
22
|
+
/** Log a start failure before throwing it, because `Bot.start` only stores a connect failure on `bot.error`. */
|
|
23
|
+
private fail;
|
|
24
|
+
/** One action call against `{endpoint}/{action}`. */
|
|
25
|
+
private call;
|
|
26
|
+
/** Check and deliver one event report. */
|
|
27
|
+
private handle;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/transport/http.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAmC1C,qCAAqC;AACrC,qBAAa,UAAU;IAIT,OAAO,CAAC,QAAQ,CAAC,GAAG;IAHhC,OAAO,CAAC,OAAO,CAA0B;IACzC,OAAO,CAAC,cAAc,CAAQ;gBAED,GAAG,EAAE,SAAS;IAE3C;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAkC5B,+BAA+B;IACzB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAK3B,gHAAgH;IAChH,OAAO,CAAC,IAAI;IAKZ,qDAAqD;YACvC,IAAI;IAuDlB,0CAA0C;YAC5B,MAAM;CA6DrB"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP transport (`protocol: 'http'`): an outbound `POST {endpoint}/{action}` client and an inbound event route.
|
|
3
|
+
*
|
|
4
|
+
* Both directions run at once, as in the upstream plugin. The outbound call authenticates with the standard
|
|
5
|
+
* `Authorization: Bearer` header; the inbound route checks `X-Signature` (HMAC-SHA1 over the re-serialized parsed body,
|
|
6
|
+
* the upstream behaviour implementations in the field verify) and `X-Self-ID`, and answers `204` only after the host
|
|
7
|
+
* accepted the event.
|
|
8
|
+
*/
|
|
9
|
+
import { routePath } from "../config.js";
|
|
10
|
+
import { decodePayload } from "../decode.js";
|
|
11
|
+
import { OneBotHttpError } from "../errors.js";
|
|
12
|
+
import { installRequest } from "../request.js";
|
|
13
|
+
import { headerOf, matchesSecret, readBody, signatureOf, writeStatus, } from "./shared.js";
|
|
14
|
+
/** The HTTP service's failure carries the response it refused; recover it structurally. */
|
|
15
|
+
function responseOf(cause) {
|
|
16
|
+
if (typeof cause !== 'object' || cause === null || !('response' in cause))
|
|
17
|
+
return undefined;
|
|
18
|
+
const response = cause.response;
|
|
19
|
+
return response instanceof Response ? response : undefined;
|
|
20
|
+
}
|
|
21
|
+
/** The HTTP transport of one bot. */
|
|
22
|
+
export class OneBotHttp {
|
|
23
|
+
bot;
|
|
24
|
+
dispose;
|
|
25
|
+
warnedNoSecret = false;
|
|
26
|
+
constructor(bot) {
|
|
27
|
+
this.bot = bot;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Claim the event route and install the action caller.
|
|
31
|
+
*/
|
|
32
|
+
async start() {
|
|
33
|
+
const webServer = this.bot.ctx.get('webServer');
|
|
34
|
+
if (webServer === undefined) {
|
|
35
|
+
throw this.fail('onebot: protocol "http" needs a profile-provided webServer to receive event reports');
|
|
36
|
+
}
|
|
37
|
+
const path = routePath(this.bot.config.path);
|
|
38
|
+
try {
|
|
39
|
+
this.dispose = webServer.register({
|
|
40
|
+
kind: 'exact',
|
|
41
|
+
path,
|
|
42
|
+
auth: 'anonymous',
|
|
43
|
+
handler: (request, response) => this.handle(request, response),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
throw this.fail(`onebot: cannot claim the http event route ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
48
|
+
}
|
|
49
|
+
const endpoint = this.bot.config.endpoint;
|
|
50
|
+
installRequest(this.bot, (action, params) => this.call(endpoint, action, params));
|
|
51
|
+
this.bot.user = {
|
|
52
|
+
id: this.bot.config.selfId,
|
|
53
|
+
name: this.bot.config.selfId,
|
|
54
|
+
isBot: true,
|
|
55
|
+
};
|
|
56
|
+
this.bot.online();
|
|
57
|
+
}
|
|
58
|
+
/** Release the event route. */
|
|
59
|
+
async stop() {
|
|
60
|
+
this.dispose?.();
|
|
61
|
+
this.dispose = undefined;
|
|
62
|
+
}
|
|
63
|
+
/** Log a start failure before throwing it, because `Bot.start` only stores a connect failure on `bot.error`. */
|
|
64
|
+
fail(message) {
|
|
65
|
+
this.bot.ctx.logger.error(message);
|
|
66
|
+
return new Error(message);
|
|
67
|
+
}
|
|
68
|
+
/** One action call against `{endpoint}/{action}`. */
|
|
69
|
+
async call(endpoint, action, params) {
|
|
70
|
+
const url = `${endpoint.replace(/\/+$/, '')}/${action}`;
|
|
71
|
+
const { token } = this.bot.config;
|
|
72
|
+
let response;
|
|
73
|
+
try {
|
|
74
|
+
response = await this.bot.ctx.http(url, {
|
|
75
|
+
method: 'POST',
|
|
76
|
+
data: params,
|
|
77
|
+
...(token === ''
|
|
78
|
+
? {}
|
|
79
|
+
: { headers: { Authorization: `Bearer ${token}` } }),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (cause) {
|
|
83
|
+
const refused = responseOf(cause);
|
|
84
|
+
if (refused !== undefined) {
|
|
85
|
+
const body = await refused.text().catch(() => '');
|
|
86
|
+
throw new OneBotHttpError(`onebot: ${action} was refused with HTTP ${refused.status}${body === '' ? '' : `: ${body.slice(0, 200)}`}`, refused.status, { cause });
|
|
87
|
+
}
|
|
88
|
+
throw new OneBotHttpError(`onebot: ${action} request failed: ${cause instanceof Error ? cause.message : String(cause)}`, 0, { cause });
|
|
89
|
+
}
|
|
90
|
+
let body;
|
|
91
|
+
try {
|
|
92
|
+
body = await response.json();
|
|
93
|
+
}
|
|
94
|
+
catch (cause) {
|
|
95
|
+
throw new OneBotHttpError(`onebot: ${action} answered with a body that is not JSON`, response.status, { cause });
|
|
96
|
+
}
|
|
97
|
+
if (typeof body !== 'object' || body === null) {
|
|
98
|
+
throw new OneBotHttpError(`onebot: ${action} answered with a body that is not an action envelope`, response.status);
|
|
99
|
+
}
|
|
100
|
+
return body;
|
|
101
|
+
}
|
|
102
|
+
/** Check and deliver one event report. */
|
|
103
|
+
async handle(request, response) {
|
|
104
|
+
try {
|
|
105
|
+
if (request.method !== 'POST') {
|
|
106
|
+
writeStatus(response, 405, { allow: 'POST' });
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const { secret } = this.bot.config;
|
|
110
|
+
const signature = headerOf(request, 'x-signature');
|
|
111
|
+
if (secret === '') {
|
|
112
|
+
if (!this.warnedNoSecret) {
|
|
113
|
+
this.warnedNoSecret = true;
|
|
114
|
+
this.bot.ctx.logger.warn('onebot: secret is empty, so event reports are accepted without X-Signature');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else if (signature === undefined || signature === '') {
|
|
118
|
+
writeStatus(response, 401);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const body = await readBody(request);
|
|
122
|
+
let parsed;
|
|
123
|
+
try {
|
|
124
|
+
parsed = JSON.parse(body);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
this.bot.ctx.logger.debug('onebot: event report is not JSON: %s', error instanceof Error ? error.message : String(error));
|
|
128
|
+
writeStatus(response, 400);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (secret !== '' &&
|
|
132
|
+
!matchesSecret(signatureOf(secret, parsed), signature)) {
|
|
133
|
+
writeStatus(response, 403);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (headerOf(request, 'x-self-id') !== this.bot.config.selfId) {
|
|
137
|
+
writeStatus(response, 403);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (typeof parsed !== 'object' || parsed === null) {
|
|
141
|
+
writeStatus(response, 400);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const session = decodePayload(this.bot, parsed);
|
|
145
|
+
if (session !== undefined)
|
|
146
|
+
await this.bot.receive(session);
|
|
147
|
+
writeStatus(response, 204);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
this.bot.ctx.logger.warn('onebot: event report failed: %s', error instanceof Error ? error.message : String(error));
|
|
151
|
+
if (!response.headersSent)
|
|
152
|
+
writeStatus(response, 500);
|
|
153
|
+
else
|
|
154
|
+
response.end();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../../src/transport/http.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAE9C,OAAO,EACL,QAAQ,EACR,aAAa,EACb,QAAQ,EACR,WAAW,EACX,WAAW,GACZ,MAAM,aAAa,CAAA;AAepB,2FAA2F;AAC3F,SAAS,UAAU,CAAC,KAAc;IAChC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,CAAC,UAAU,IAAI,KAAK,CAAC;QACvE,OAAO,SAA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type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import type { Duplex } from 'node:stream';
|
|
3
|
+
/**
|
|
4
|
+
* Compare a received credential with the configured one without leaking its length through early exits.
|
|
5
|
+
* @param expected - the configured secret or token.
|
|
6
|
+
* @param received - the presented value, when one was sent.
|
|
7
|
+
* @returns whether they are equal.
|
|
8
|
+
*/
|
|
9
|
+
export declare function matchesSecret(expected: string, received: string | undefined): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* One header of an inbound request, when it arrived exactly once.
|
|
12
|
+
* @param request - the inbound request.
|
|
13
|
+
* @param name - lower-case header name.
|
|
14
|
+
* @returns the header value, or undefined when absent or repeated.
|
|
15
|
+
*/
|
|
16
|
+
export declare function headerOf(request: IncomingMessage, name: string): string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Read a request body to its end. The adapter imposes no size limit, as the upstream plugin does not.
|
|
19
|
+
* @param request - the inbound request.
|
|
20
|
+
* @returns the decoded body.
|
|
21
|
+
*/
|
|
22
|
+
export declare function readBody(request: IncomingMessage): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Answer an inbound request with an empty response.
|
|
25
|
+
* @param response - the server response.
|
|
26
|
+
* @param status - HTTP status code.
|
|
27
|
+
* @param headers - extra response headers.
|
|
28
|
+
*/
|
|
29
|
+
export declare function writeStatus(response: ServerResponse, status: number, headers?: Record<string, string>): void;
|
|
30
|
+
/**
|
|
31
|
+
* Refuse an HTTP upgrade that the adapter does not accept, before handing the socket to a WebSocket server.
|
|
32
|
+
* @param socket - the raw connection.
|
|
33
|
+
* @param status - HTTP status code.
|
|
34
|
+
* @param reason - reason phrase.
|
|
35
|
+
*/
|
|
36
|
+
export declare function rejectUpgrade(socket: Duplex, status: number, reason: string): void;
|
|
37
|
+
/**
|
|
38
|
+
* The `X-Signature` value of one event report.
|
|
39
|
+
*
|
|
40
|
+
* The upstream plugin signs the re-serialized parsed body rather than the raw bytes; this keeps that behaviour because
|
|
41
|
+
* it is what implementations in the field have been verifying against for years.
|
|
42
|
+
*
|
|
43
|
+
* @param secret - the configured `secret`.
|
|
44
|
+
* @param body - the parsed event payload.
|
|
45
|
+
* @returns `sha1=<hex digest>`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function signatureOf(secret: string, body: unknown): string;
|
|
48
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../../src/transport/shared.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAChE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GAAG,SAAS,GAC3B,OAAO,CAMT;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,MAAM,GACX,MAAM,GAAG,SAAS,CAGpB;AAED;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAQxE;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,cAAc,EACxB,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,GACnC,IAAI,CAGN;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACb,IAAI,CAKN;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,MAAM,CAKjE"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Compare a received credential with the configured one without leaking its length through early exits.
|
|
4
|
+
* @param expected - the configured secret or token.
|
|
5
|
+
* @param received - the presented value, when one was sent.
|
|
6
|
+
* @returns whether they are equal.
|
|
7
|
+
*/
|
|
8
|
+
export function matchesSecret(expected, received) {
|
|
9
|
+
if (received === undefined)
|
|
10
|
+
return false;
|
|
11
|
+
const left = Buffer.from(expected, 'utf8');
|
|
12
|
+
const right = Buffer.from(received, 'utf8');
|
|
13
|
+
if (left.length !== right.length)
|
|
14
|
+
return false;
|
|
15
|
+
return timingSafeEqual(left, right);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* One header of an inbound request, when it arrived exactly once.
|
|
19
|
+
* @param request - the inbound request.
|
|
20
|
+
* @param name - lower-case header name.
|
|
21
|
+
* @returns the header value, or undefined when absent or repeated.
|
|
22
|
+
*/
|
|
23
|
+
export function headerOf(request, name) {
|
|
24
|
+
const value = request.headers[name];
|
|
25
|
+
return typeof value === 'string' ? value : undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Read a request body to its end. The adapter imposes no size limit, as the upstream plugin does not.
|
|
29
|
+
* @param request - the inbound request.
|
|
30
|
+
* @returns the decoded body.
|
|
31
|
+
*/
|
|
32
|
+
export async function readBody(request) {
|
|
33
|
+
const chunks = [];
|
|
34
|
+
for await (const chunk of request) {
|
|
35
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
36
|
+
}
|
|
37
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Answer an inbound request with an empty response.
|
|
41
|
+
* @param response - the server response.
|
|
42
|
+
* @param status - HTTP status code.
|
|
43
|
+
* @param headers - extra response headers.
|
|
44
|
+
*/
|
|
45
|
+
export function writeStatus(response, status, headers = {}) {
|
|
46
|
+
response.writeHead(status, headers);
|
|
47
|
+
response.end();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Refuse an HTTP upgrade that the adapter does not accept, before handing the socket to a WebSocket server.
|
|
51
|
+
* @param socket - the raw connection.
|
|
52
|
+
* @param status - HTTP status code.
|
|
53
|
+
* @param reason - reason phrase.
|
|
54
|
+
*/
|
|
55
|
+
export function rejectUpgrade(socket, status, reason) {
|
|
56
|
+
socket.write(`HTTP/1.1 ${status} ${reason}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`);
|
|
57
|
+
socket.destroy();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The `X-Signature` value of one event report.
|
|
61
|
+
*
|
|
62
|
+
* The upstream plugin signs the re-serialized parsed body rather than the raw bytes; this keeps that behaviour because
|
|
63
|
+
* it is what implementations in the field have been verifying against for years.
|
|
64
|
+
*
|
|
65
|
+
* @param secret - the configured `secret`.
|
|
66
|
+
* @param body - the parsed event payload.
|
|
67
|
+
* @returns `sha1=<hex digest>`.
|
|
68
|
+
*/
|
|
69
|
+
export function signatureOf(secret, body) {
|
|
70
|
+
const digest = createHmac('sha1', secret)
|
|
71
|
+
.update(JSON.stringify(body))
|
|
72
|
+
.digest('hex');
|
|
73
|
+
return `sha1=${digest}`;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/transport/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAIzD;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAC3B,QAAgB,EAChB,QAA4B;IAE5B,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO,KAAK,CAAA;IACxC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;IAC1C,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;I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WAAW,CAAC,MAAc,EAAE,IAAa;IACvD,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC;SACtC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;SAC5B,MAAM,CAAC,KAAK,CAAC,CAAA;IAChB,OAAO,QAAQ,MAAM,EAAE,CAAA;AACzB,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reverse WebSocket transport (`protocol: 'ws-reverse'`).
|
|
3
|
+
*
|
|
4
|
+
* The implementation dials the harness; the adapter owns the endpoint through the profile's `webServer` and keeps
|
|
5
|
+
* listening between connections. A connection that fails the handshake checks is refused and dropped, because the
|
|
6
|
+
* implementation's configuration can change without a restart of this process.
|
|
7
|
+
*/
|
|
8
|
+
import { type WebSocket as Socket } from 'ws';
|
|
9
|
+
import type { OneBotBot } from '../bot.ts';
|
|
10
|
+
/** The reverse WebSocket server of one bot. */
|
|
11
|
+
export declare class OneBotWsServer {
|
|
12
|
+
private readonly bot;
|
|
13
|
+
private server;
|
|
14
|
+
private dispose;
|
|
15
|
+
private connection;
|
|
16
|
+
private requests;
|
|
17
|
+
private warnedNoToken;
|
|
18
|
+
constructor(bot: OneBotBot);
|
|
19
|
+
/**
|
|
20
|
+
* Claim the upgrade route. The endpoint then stays available across implementation restarts.
|
|
21
|
+
*/
|
|
22
|
+
start(): Promise<void>;
|
|
23
|
+
/** Release the route and close the connected implementation. */
|
|
24
|
+
stop(): Promise<void>;
|
|
25
|
+
/** Log a start failure before throwing it, because `Bot.start` only stores a connect failure on `bot.error`. */
|
|
26
|
+
private fail;
|
|
27
|
+
/** Validate one upgrade request and hand the accepted socket to `ws`. */
|
|
28
|
+
private upgrade;
|
|
29
|
+
/**
|
|
30
|
+
* Take over one accepted socket. A newer connection replaces the previous one.
|
|
31
|
+
* @param socket - the upgraded WebSocket connection of the implementation.
|
|
32
|
+
*/
|
|
33
|
+
accept(socket: Socket): void;
|
|
34
|
+
private closeConnection;
|
|
35
|
+
/** The current connection closed: refuse its pending calls and wait for the implementation to dial again. */
|
|
36
|
+
private drop;
|
|
37
|
+
/** Dispatch one received frame: an event, an action answer, or noise. */
|
|
38
|
+
private dispatch;
|
|
39
|
+
/** Publish one event; a failed intake is logged because the platform will not redeliver it. */
|
|
40
|
+
private receive;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=ws-reverse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ws-reverse.d.ts","sourceRoot":"","sources":["../../../src/transport/ws-reverse.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,OAAO,EAAmB,KAAK,SAAS,IAAI,MAAM,EAAE,MAAM,IAAI,CAAA;AAC9D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAyB1C,+CAA+C;AAC/C,qBAAa,cAAc;IAOb,OAAO,CAAC,QAAQ,CAAC,GAAG;IANhC,OAAO,CAAC,MAAM,CAA6B;IAC3C,OAAO,CAAC,OAAO,CAA0B;IACzC,OAAO,CAAC,UAAU,CAAoB;IACtC,OAAO,CAAC,QAAQ,CAA4B;IAC5C,OAAO,CAAC,aAAa,CAAQ;gBAEA,GAAG,EAAE,SAAS;IAE3C;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA4B5B,gEAAgE;IAC1D,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAW3B,gHAAgH;IAChH,OAAO,CAAC,IAAI;IAKZ,yEAAyE;IACzE,OAAO,CAAC,OAAO;IAqCf;;;OAGG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAuB5B,OAAO,CAAC,eAAe;IAgBvB,6GAA6G;IAC7G,OAAO,CAAC,IAAI;IAYZ,yEAAyE;IACzE,OAAO,CAAC,QAAQ;IAkBhB,+FAA+F;YACjF,OAAO;CAyBtB"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reverse WebSocket transport (`protocol: 'ws-reverse'`).
|
|
3
|
+
*
|
|
4
|
+
* The implementation dials the harness; the adapter owns the endpoint through the profile's `webServer` and keeps
|
|
5
|
+
* listening between connections. A connection that fails the handshake checks is refused and dropped, because the
|
|
6
|
+
* implementation's configuration can change without a restart of this process.
|
|
7
|
+
*/
|
|
8
|
+
import { Status } from '@deepseek-ai/cordis-plugin-im-core';
|
|
9
|
+
import { WebSocketServer } from 'ws';
|
|
10
|
+
import { routePath } from "../config.js";
|
|
11
|
+
import { decodePayload, isPayload } from "../decode.js";
|
|
12
|
+
import { ActionRequests, installRequest, isResponse, rejectRequests, } from "../request.js";
|
|
13
|
+
import { headerOf, matchesSecret, rejectUpgrade } from "./shared.js";
|
|
14
|
+
/** The reverse WebSocket server of one bot. */
|
|
15
|
+
export class OneBotWsServer {
|
|
16
|
+
bot;
|
|
17
|
+
server;
|
|
18
|
+
dispose;
|
|
19
|
+
connection;
|
|
20
|
+
requests;
|
|
21
|
+
warnedNoToken = false;
|
|
22
|
+
constructor(bot) {
|
|
23
|
+
this.bot = bot;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Claim the upgrade route. The endpoint then stays available across implementation restarts.
|
|
27
|
+
*/
|
|
28
|
+
async start() {
|
|
29
|
+
const webServer = this.bot.ctx.get('webServer');
|
|
30
|
+
if (webServer === undefined) {
|
|
31
|
+
throw this.fail('onebot: protocol "ws-reverse" needs a profile-provided webServer; use protocol "ws" instead');
|
|
32
|
+
}
|
|
33
|
+
const path = routePath(this.bot.config.path);
|
|
34
|
+
const server = new WebSocketServer({ noServer: true });
|
|
35
|
+
try {
|
|
36
|
+
this.dispose = webServer.registerUpgrade({
|
|
37
|
+
path,
|
|
38
|
+
auth: 'anonymous',
|
|
39
|
+
handler: (request, socket, head) => {
|
|
40
|
+
this.upgrade(server, request, socket, head);
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
server.close();
|
|
46
|
+
throw this.fail(`onebot: cannot claim the ws-reverse route ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
47
|
+
}
|
|
48
|
+
this.server = server;
|
|
49
|
+
}
|
|
50
|
+
/** Release the route and close the connected implementation. */
|
|
51
|
+
async stop() {
|
|
52
|
+
this.dispose?.();
|
|
53
|
+
this.dispose = undefined;
|
|
54
|
+
this.closeConnection();
|
|
55
|
+
const server = this.server;
|
|
56
|
+
this.server = undefined;
|
|
57
|
+
if (server !== undefined) {
|
|
58
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Log a start failure before throwing it, because `Bot.start` only stores a connect failure on `bot.error`. */
|
|
62
|
+
fail(message) {
|
|
63
|
+
this.bot.ctx.logger.error(message);
|
|
64
|
+
return new Error(message);
|
|
65
|
+
}
|
|
66
|
+
/** Validate one upgrade request and hand the accepted socket to `ws`. */
|
|
67
|
+
upgrade(server, request, socket, head) {
|
|
68
|
+
const { token } = this.bot.config;
|
|
69
|
+
if (token === '') {
|
|
70
|
+
if (!this.warnedNoToken) {
|
|
71
|
+
this.warnedNoToken = true;
|
|
72
|
+
this.bot.ctx.logger.warn('onebot: token is empty, so reverse WebSocket connections are accepted without Authorization');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const presented = headerOf(request, 'authorization')?.replace(/^Bearer\s+/i, '');
|
|
77
|
+
if (!matchesSecret(token, presented)) {
|
|
78
|
+
rejectUpgrade(socket, 401, 'Unauthorized');
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (headerOf(request, 'x-client-role') !== 'Universal') {
|
|
83
|
+
rejectUpgrade(socket, 403, 'Forbidden');
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (headerOf(request, 'x-self-id') !== this.bot.config.selfId) {
|
|
87
|
+
rejectUpgrade(socket, 403, 'Forbidden');
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
server.handleUpgrade(request, socket, head, (ws) => {
|
|
91
|
+
this.accept(ws);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Take over one accepted socket. A newer connection replaces the previous one.
|
|
96
|
+
* @param socket - the upgraded WebSocket connection of the implementation.
|
|
97
|
+
*/
|
|
98
|
+
accept(socket) {
|
|
99
|
+
this.closeConnection(1008, 'replaced by a new connection');
|
|
100
|
+
this.connection = socket;
|
|
101
|
+
const requests = new ActionRequests((frame) => socket.send(JSON.stringify(frame)), this.bot.config.ws.responseTimeout);
|
|
102
|
+
this.requests = requests;
|
|
103
|
+
installRequest(this.bot, requests.request);
|
|
104
|
+
socket.on('message', (raw) => {
|
|
105
|
+
this.dispatch(raw.toString(), requests);
|
|
106
|
+
});
|
|
107
|
+
socket.on('close', () => {
|
|
108
|
+
this.drop(socket);
|
|
109
|
+
});
|
|
110
|
+
this.bot.user = {
|
|
111
|
+
id: this.bot.config.selfId,
|
|
112
|
+
name: this.bot.config.selfId,
|
|
113
|
+
isBot: true,
|
|
114
|
+
};
|
|
115
|
+
this.bot.online();
|
|
116
|
+
}
|
|
117
|
+
closeConnection(code = 1000, reason = 'adapter stopped') {
|
|
118
|
+
const socket = this.connection;
|
|
119
|
+
const requests = this.requests;
|
|
120
|
+
this.connection = undefined;
|
|
121
|
+
this.requests = undefined;
|
|
122
|
+
requests?.rejectAll(new Error('onebot: the reverse WebSocket connection has closed'));
|
|
123
|
+
try {
|
|
124
|
+
socket?.close(code, reason);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
// The socket is already closed or closing; nothing left to release.
|
|
128
|
+
void error;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** The current connection closed: refuse its pending calls and wait for the implementation to dial again. */
|
|
132
|
+
drop(socket) {
|
|
133
|
+
if (this.connection !== socket)
|
|
134
|
+
return;
|
|
135
|
+
this.connection = undefined;
|
|
136
|
+
const failure = new Error('onebot: the reverse WebSocket connection has closed');
|
|
137
|
+
this.requests?.rejectAll(failure);
|
|
138
|
+
this.requests = undefined;
|
|
139
|
+
rejectRequests(this.bot, failure);
|
|
140
|
+
this.bot.status = Status.RECONNECT;
|
|
141
|
+
}
|
|
142
|
+
/** Dispatch one received frame: an event, an action answer, or noise. */
|
|
143
|
+
dispatch(text, requests) {
|
|
144
|
+
let parsed;
|
|
145
|
+
try {
|
|
146
|
+
parsed = JSON.parse(text);
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
this.bot.ctx.logger.warn('onebot: cannot parse a WebSocket frame: %s', error instanceof Error ? error.message : String(error));
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (isPayload(parsed)) {
|
|
153
|
+
void this.receive(parsed);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (isResponse(parsed) && requests.resolve(parsed))
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
/** Publish one event; a failed intake is logged because the platform will not redeliver it. */
|
|
160
|
+
async receive(data) {
|
|
161
|
+
if (data.post_type === 'meta_event') {
|
|
162
|
+
this.bot.ctx.logger.debug('onebot: %s heartbeat ignored', data.meta_event_type ?? 'meta');
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const session = decodePayload(this.bot, data);
|
|
166
|
+
if (session === undefined) {
|
|
167
|
+
this.bot.ctx.logger.debug('onebot: ignored %s event', data.post_type ?? 'unknown');
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
await this.bot.receive(session);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
this.bot.ctx.logger.error('onebot: intake failed: %s', error instanceof Error ? error.message : String(error));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=ws-reverse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3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IAAa;QACjC,IAAI,IAAI,CAAC,SAAS,KAAK,YAAY,EAAE,CAAC;YACpC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CACvB,8BAA8B,EAC9B,IAAI,CAAC,eAAe,IAAI,MAAM,CAC/B,CAAA;YACD,OAAM;QACR,CAAC;QACD,MAAM,OAAO,GAAG,aAAa,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;QAC7C,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CACvB,0BAA0B,EAC1B,IAAI,CAAC,SAAS,IAAI,SAAS,CAC5B,CAAA;YACD,OAAM;QACR,CAAC;QACD,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;QACjC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CACvB,2BAA2B,EAC3B,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CACvD,CAAA;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Forward WebSocket transport (`protocol: 'ws'`).
|
|
3
|
+
*
|
|
4
|
+
* The adapter dials the implementation's WebSocket endpoint and dials again forever: `ws.retryTimes` is a fast-retry
|
|
5
|
+
* budget, not a give-up count, so an implementation that starts after the harness still comes online without a
|
|
6
|
+
* restart. Configuration failures are the caller's to fix and throw out of `connect()`; transport failures retry.
|
|
7
|
+
*/
|
|
8
|
+
import type { OneBotBot } from '../bot.ts';
|
|
9
|
+
/** The forward WebSocket client of one bot. */
|
|
10
|
+
export declare class OneBotWsClient {
|
|
11
|
+
private readonly bot;
|
|
12
|
+
private controller;
|
|
13
|
+
private running;
|
|
14
|
+
private socket;
|
|
15
|
+
constructor(bot: OneBotBot);
|
|
16
|
+
/**
|
|
17
|
+
* Validate the endpoint and start the reconnect loop.
|
|
18
|
+
*
|
|
19
|
+
* Returning before the first dial is deliberate: `Bot.start` awaits `connect()`, so waiting here would hold up
|
|
20
|
+
* plugin initialization for as long as the implementation is late.
|
|
21
|
+
*/
|
|
22
|
+
start(): Promise<void>;
|
|
23
|
+
/** Stop the loop and wait for it to unwind. */
|
|
24
|
+
stop(): Promise<void>;
|
|
25
|
+
/** Log a start failure before throwing it, because `Bot.start` only stores a connect failure on `bot.error`. */
|
|
26
|
+
private fail;
|
|
27
|
+
private closeSocket;
|
|
28
|
+
private loop;
|
|
29
|
+
/** Dial the endpoint and resolve once the socket is open. */
|
|
30
|
+
private dial;
|
|
31
|
+
/**
|
|
32
|
+
* Take over one open socket: install the action caller, check the identity, publish events until the socket closes.
|
|
33
|
+
*/
|
|
34
|
+
private accept;
|
|
35
|
+
/** Dispatch one received frame: an event, an action answer, or noise. */
|
|
36
|
+
private dispatch;
|
|
37
|
+
/** Publish one event; a failed intake is logged because the platform will not redeliver it. */
|
|
38
|
+
private receive;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=ws.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../../src/transport/ws.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAsC1C,+CAA+C;AAC/C,qBAAa,cAAc;IAKb,OAAO,CAAC,QAAQ,CAAC,GAAG;IAJhC,OAAO,CAAC,UAAU,CAA6B;IAC/C,OAAO,CAAC,OAAO,CAA2B;IAC1C,OAAO,CAAC,MAAM,CAA0B;gBAEX,GAAG,EAAE,SAAS;IAE3C;;;;;OAKG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAW5B,+CAA+C;IACzC,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ3B,gHAAgH;IAChH,OAAO,CAAC,IAAI;IAKZ,OAAO,CAAC,WAAW;YAWL,IAAI;IAoClB,6DAA6D;IAC7D,OAAO,CAAC,IAAI;IAgDZ;;OAEG;YACW,MAAM;IAqDpB,yEAAyE;IACzE,OAAO,CAAC,QAAQ;IAkBhB,+FAA+F;YACjF,OAAO;CAyBtB"}
|