@tencent-connect/qqbot-nodejs 1.0.4 → 1.0.5
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/CHANGELOG.md +96 -0
- package/LICENSE +21 -0
- package/README.md +167 -86
- package/README.zh-CN.md +280 -0
- package/USAGE.md +80 -27
- package/dist/QQBot.d.ts +33 -3
- package/dist/QQBot.d.ts.map +1 -1
- package/dist/QQBot.js +84 -34
- package/dist/QQBot.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/protocol/api/token.d.ts.map +1 -1
- package/dist/protocol/api/token.js +6 -2
- package/dist/protocol/api/token.js.map +1 -1
- package/dist/protocol/gateway/constants.d.ts +6 -1
- package/dist/protocol/gateway/constants.d.ts.map +1 -1
- package/dist/protocol/gateway/constants.js +6 -1
- package/dist/protocol/gateway/constants.js.map +1 -1
- package/dist/protocol/gateway/errors.d.ts +22 -0
- package/dist/protocol/gateway/errors.d.ts.map +1 -0
- package/dist/protocol/gateway/errors.js +24 -0
- package/dist/protocol/gateway/errors.js.map +1 -0
- package/dist/protocol/gateway/gateway-connection.d.ts +45 -4
- package/dist/protocol/gateway/gateway-connection.d.ts.map +1 -1
- package/dist/protocol/gateway/gateway-connection.js +252 -143
- package/dist/protocol/gateway/gateway-connection.js.map +1 -1
- package/dist/protocol/gateway/reconnect.d.ts +24 -1
- package/dist/protocol/gateway/reconnect.d.ts.map +1 -1
- package/dist/protocol/gateway/reconnect.js +42 -6
- package/dist/protocol/gateway/reconnect.js.map +1 -1
- package/dist/protocol/index.d.ts +4 -3
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +3 -2
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/utils/file-utils.d.ts +2 -2
- package/dist/protocol/utils/file-utils.js +2 -2
- package/dist/protocol/utils/image-size.d.ts +3 -3
- package/dist/protocol/utils/image-size.js +3 -3
- package/dist/protocol/utils/ref-index-store.d.ts +1 -1
- package/dist/protocol/utils/ref-index-store.js +1 -1
- package/package.json +23 -7
- package/src/QQBot.ts +93 -37
- package/src/index.ts +5 -0
- package/src/protocol/api/token.ts +6 -2
- package/src/protocol/gateway/constants.ts +6 -1
- package/src/protocol/gateway/errors.ts +32 -0
- package/src/protocol/gateway/gateway-connection.ts +288 -157
- package/src/protocol/gateway/reconnect.ts +61 -7
- package/src/protocol/index.ts +10 -1
- package/src/protocol/utils/file-utils.ts +2 -2
- package/src/protocol/utils/image-size.ts +3 -3
- package/src/protocol/utils/ref-index-store.ts +1 -1
|
@@ -21,28 +21,80 @@ export interface CloseAction {
|
|
|
21
21
|
reason: string;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/** Connection-level reconnect budget. */
|
|
25
|
+
export interface ReconnectPolicy {
|
|
26
|
+
/**
|
|
27
|
+
* Consecutive reconnect attempts before giving up. Defaults to `Infinity`
|
|
28
|
+
* ({@link MAX_RECONNECT_ATTEMPTS}): only fatal closes end the loop.
|
|
29
|
+
*/
|
|
30
|
+
maxAttempts: number;
|
|
31
|
+
/** Backoff delays (ms) indexed by attempt; the last entry repeats. Defaults to {@link RECONNECT_DELAYS}. */
|
|
32
|
+
delays: readonly number[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const DEFAULT_RECONNECT_POLICY: ReconnectPolicy = {
|
|
36
|
+
maxAttempts: MAX_RECONNECT_ATTEMPTS,
|
|
37
|
+
delays: RECONNECT_DELAYS,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** Merge a partial policy with defaults, rejecting values that would break the retry loop. */
|
|
41
|
+
export function resolveReconnectPolicy(policy?: Partial<ReconnectPolicy>): ReconnectPolicy {
|
|
42
|
+
const maxAttempts = policy?.maxAttempts ?? DEFAULT_RECONNECT_POLICY.maxAttempts;
|
|
43
|
+
const delays = policy?.delays ?? DEFAULT_RECONNECT_POLICY.delays;
|
|
44
|
+
const isUnlimited = maxAttempts === Number.POSITIVE_INFINITY;
|
|
45
|
+
if (!isUnlimited && (!Number.isInteger(maxAttempts) || maxAttempts < 0)) {
|
|
46
|
+
throw new RangeError(
|
|
47
|
+
`reconnect.maxAttempts must be a non-negative integer or Infinity, got ${maxAttempts}`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
if (delays.length === 0 || delays.some((d) => !Number.isFinite(d) || d < 0)) {
|
|
51
|
+
throw new RangeError("reconnect.delays must be a non-empty list of non-negative numbers");
|
|
52
|
+
}
|
|
53
|
+
return { maxAttempts, delays: [...delays] };
|
|
54
|
+
}
|
|
55
|
+
|
|
24
56
|
export class ReconnectState {
|
|
25
57
|
private attempts = 0;
|
|
26
58
|
private lastConnectTime = 0;
|
|
27
59
|
private quickDisconnectCount = 0;
|
|
60
|
+
private readonly policy: ReconnectPolicy;
|
|
28
61
|
|
|
29
62
|
constructor(
|
|
30
63
|
private readonly accountId: string,
|
|
31
64
|
private readonly log?: Logger,
|
|
32
|
-
|
|
65
|
+
policy?: Partial<ReconnectPolicy>,
|
|
66
|
+
) {
|
|
67
|
+
this.policy = resolveReconnectPolicy(policy);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Socket opened — starts the quick-disconnect window. Does not reset the budget. */
|
|
71
|
+
onSocketOpen(): void {
|
|
72
|
+
this.lastConnectTime = Date.now();
|
|
73
|
+
}
|
|
33
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Session established (READY / RESUMED) — resets the reconnect budget.
|
|
77
|
+
*
|
|
78
|
+
* Resetting on socket open instead would let "open then immediately kicked"
|
|
79
|
+
* loops (e.g. repeated 4004) retry forever.
|
|
80
|
+
*/
|
|
34
81
|
onConnected(): void {
|
|
35
82
|
this.attempts = 0;
|
|
36
83
|
this.lastConnectTime = Date.now();
|
|
37
84
|
}
|
|
38
85
|
|
|
39
86
|
isExhausted(): boolean {
|
|
40
|
-
return this.attempts >=
|
|
87
|
+
return this.attempts >= this.policy.maxAttempts;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Maximum consecutive attempts allowed by the active policy. */
|
|
91
|
+
get maxAttempts(): number {
|
|
92
|
+
return this.policy.maxAttempts;
|
|
41
93
|
}
|
|
42
94
|
|
|
43
95
|
getNextDelay(customDelay?: number): number {
|
|
44
|
-
const
|
|
45
|
-
|
|
96
|
+
const { delays } = this.policy;
|
|
97
|
+
const delay = customDelay ?? delays[Math.min(this.attempts, delays.length - 1)];
|
|
46
98
|
this.attempts++;
|
|
47
99
|
this.log?.debug?.(`[${this.accountId}] Reconnecting in ${delay}ms (attempt ${this.attempts})`);
|
|
48
100
|
return delay;
|
|
@@ -111,7 +163,7 @@ export class ReconnectState {
|
|
|
111
163
|
if (code >= GatewayCloseCode.SERVER_ERROR_START && code <= GatewayCloseCode.SERVER_ERROR_END) {
|
|
112
164
|
this.log?.info(`[${this.accountId}] Internal error (${code}), will re-identify`);
|
|
113
165
|
return {
|
|
114
|
-
shouldReconnect: !isAborted
|
|
166
|
+
shouldReconnect: !isAborted,
|
|
115
167
|
clearSession: true,
|
|
116
168
|
refreshToken: true,
|
|
117
169
|
fatal: false,
|
|
@@ -132,7 +184,7 @@ export class ReconnectState {
|
|
|
132
184
|
);
|
|
133
185
|
this.quickDisconnectCount = 0;
|
|
134
186
|
return {
|
|
135
|
-
shouldReconnect: !isAborted
|
|
187
|
+
shouldReconnect: !isAborted,
|
|
136
188
|
reconnectDelay: RATE_LIMIT_DELAY,
|
|
137
189
|
clearSession: false,
|
|
138
190
|
refreshToken: false,
|
|
@@ -144,8 +196,10 @@ export class ReconnectState {
|
|
|
144
196
|
this.quickDisconnectCount = 0;
|
|
145
197
|
}
|
|
146
198
|
|
|
199
|
+
// A remote 1000 is a server-side graceful close (deploy / drain), not a
|
|
200
|
+
// client shutdown: local closes retire the socket before its close event.
|
|
147
201
|
return {
|
|
148
|
-
shouldReconnect: !isAborted
|
|
202
|
+
shouldReconnect: !isAborted,
|
|
149
203
|
clearSession: false,
|
|
150
204
|
refreshToken: false,
|
|
151
205
|
fatal: false,
|
package/src/protocol/index.ts
CHANGED
|
@@ -61,6 +61,8 @@ export {
|
|
|
61
61
|
GatewayCloseCode,
|
|
62
62
|
GatewayEvent,
|
|
63
63
|
GatewayOp,
|
|
64
|
+
HANDSHAKE_TIMEOUT_MS,
|
|
65
|
+
INVALID_SESSION_DELAY,
|
|
64
66
|
InteractionType,
|
|
65
67
|
MAX_QUICK_DISCONNECT_COUNT,
|
|
66
68
|
MAX_RECONNECT_ATTEMPTS,
|
|
@@ -69,7 +71,13 @@ export {
|
|
|
69
71
|
RECONNECT_DELAYS,
|
|
70
72
|
} from "./gateway/constants.js";
|
|
71
73
|
export { decodeGatewayMessageData, readOptionalMessageSceneExt } from "./gateway/codec.js";
|
|
72
|
-
export {
|
|
74
|
+
export {
|
|
75
|
+
ReconnectState,
|
|
76
|
+
resolveReconnectPolicy,
|
|
77
|
+
type CloseAction,
|
|
78
|
+
type ReconnectPolicy,
|
|
79
|
+
} from "./gateway/reconnect.js";
|
|
80
|
+
export { GatewayError, GatewayErrorCode } from "./gateway/errors.js";
|
|
73
81
|
export {
|
|
74
82
|
dispatchEvent,
|
|
75
83
|
type DispatchResult,
|
|
@@ -80,6 +88,7 @@ export {
|
|
|
80
88
|
export {
|
|
81
89
|
GatewayConnection,
|
|
82
90
|
type GatewayConnectionOptions,
|
|
91
|
+
type GatewayDisconnect,
|
|
83
92
|
type PersistedSession,
|
|
84
93
|
type SessionPersistencePort,
|
|
85
94
|
} from "./gateway/gateway-connection.js";
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Pure helpers: filename sanitization, size constants, file size formatting.
|
|
5
5
|
* The SDK does NOT bundle SSRF / remote-fetch logic — those are concerns
|
|
6
|
-
* of the host application
|
|
7
|
-
*
|
|
6
|
+
* of the host application and should be implemented at the integration
|
|
7
|
+
* layer.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { MediaFileType } from "../types.js";
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
*
|
|
6
6
|
* The header parsers are all pure functions. URL probing requires a
|
|
7
7
|
* caller-provided {@link ImageBytesFetcher} so this module stays free of
|
|
8
|
-
* networking / SSRF concerns — the
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* networking / SSRF concerns — the embedding application is responsible
|
|
9
|
+
* for enforcing public-network-only / hostname-allowlist policies before
|
|
10
|
+
* any byte hits this code.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import { Buffer } from "node:buffer";
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* Path resolution is **delegated to the embedder** via the constructor:
|
|
15
15
|
* the SDK itself does not know where the host application wants its data
|
|
16
|
-
* (`~/.
|
|
16
|
+
* (e.g. `~/.myapp/qqbot/data/ref-index.jsonl`, `/var/lib/qqbot/...`).
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import fs from "node:fs";
|