@agentunion/fastaun-browser 0.5.6 → 0.5.9
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 +104 -0
- package/_packed_docs/CHANGELOG.md +104 -0
- package/_packed_docs/INDEX.md +12 -11
- package/_packed_docs/KITE_DOCS_GUIDE.md +4 -3
- package/_packed_docs/agent.md/SCHEMA.md +83 -58
- package/_packed_docs/agent.md/examples/codeagent-claudecode.md +1 -1
- package/_packed_docs/agent.md/examples/openclaw-lobster.md +1 -1
- package/_packed_docs/agent.md/examples/signed-openclaw-lobster.md +1 -1
- package/_packed_docs/agent.md/examples//347/276/244/347/273/204-/345/274/200/345/217/221/345/233/242/351/230/237.md +21 -0
- package/_packed_docs/audit/AUN/346/234/215/345/212/241Go/345/214/226/351/207/215/346/236/204/347/262/276/347/273/206/345/214/226/346/272/220/347/240/201/345/256/241/346/237/245-20260718.md +3 -3
- package/_packed_docs/aun/345/205/254/347/275/221/346/265/213/350/257/225/350/277/220/350/241/214/346/214/207/345/215/227.md +7 -0
- package/_packed_docs/aun/345/210/206/345/270/203/345/274/217/346/265/213/350/257/225/350/277/220/350/241/214/346/214/207/345/215/227.md +422 -331
- package/_packed_docs/aun/346/265/213/350/257/225/350/277/220/350/241/214/346/214/207/345/215/227.md +598 -398
- package/_packed_docs/group-message-rpc-alignment-gaps.md +741 -0
- package/_packed_docs/message-online-push-alignment.md +572 -0
- package/_packed_docs/protocol/06-/346/234/215/345/212/241/345/215/217/350/256/256.md +1 -1
- package/_packed_docs/sdk/02-WebSocket/345/215/217/350/256/256.md +10 -10
- package/_packed_docs/sdk/03-/346/240/270/345/277/203/346/246/202/345/277/265.md +2 -2
- package/_packed_docs/sdk/04-/350/277/236/346/216/245/344/270/216/350/256/244/350/257/201.md +30 -10
- package/_packed_docs/sdk/06-API/346/211/213/345/206/214.md +23 -20
- package/_packed_docs/sdk/07-/351/224/231/350/257/257/345/244/204/347/220/206.md +3 -3
- package/_packed_docs/sdk/08-/346/234/200/344/275/263/345/256/236/350/267/265.md +5 -6
- package/_packed_docs/sdk/09-group-rpc-manual.md +11 -7
- package/_packed_docs/sdk/09-message-rpc-manual.md +5 -3
- package/_packed_docs/sdk/09-storage-rpc-manual.md +14 -3
- package/_packed_docs/sdk/AUN_DOCS_GUIDE.md +19 -17
- package/_packed_docs/sdk/CHANGELOG-0.5.6.md +73 -0
- package/_packed_docs/sdk/INDEX.md +22 -22
- package/_packed_docs/sdk/README.md +6 -5
- package/dist/agent-md-schema.d.ts +14 -0
- package/dist/agent-md-schema.d.ts.map +1 -0
- package/dist/agent-md-schema.js +103 -0
- package/dist/agent-md-schema.js.map +1 -0
- package/dist/agent-md.d.ts +7 -0
- package/dist/agent-md.d.ts.map +1 -1
- package/dist/agent-md.js +230 -10
- package/dist/agent-md.js.map +1 -1
- package/dist/aid-store.d.ts +5 -1
- package/dist/aid-store.d.ts.map +1 -1
- package/dist/aid-store.js +24 -4
- package/dist/aid-store.js.map +1 -1
- package/dist/aid.d.ts +2 -0
- package/dist/aid.d.ts.map +1 -1
- package/dist/aid.js +29 -7
- package/dist/aid.js.map +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +1 -0
- package/dist/auth.js.map +1 -1
- package/dist/bundle.js +30253 -21955
- package/dist/cert-utils.d.ts +1 -0
- package/dist/cert-utils.d.ts.map +1 -1
- package/dist/cert-utils.js +36 -16
- package/dist/cert-utils.js.map +1 -1
- package/dist/client/delivery.d.ts +40 -5
- package/dist/client/delivery.d.ts.map +1 -1
- package/dist/client/delivery.js +719 -221
- package/dist/client/delivery.js.map +1 -1
- package/dist/client/group-state.js +2 -2
- package/dist/client/group-state.js.map +1 -1
- package/dist/client/lifecycle.d.ts +15 -1
- package/dist/client/lifecycle.d.ts.map +1 -1
- package/dist/client/lifecycle.js +463 -151
- package/dist/client/lifecycle.js.map +1 -1
- package/dist/client/rpc-pipeline.d.ts +25 -6
- package/dist/client/rpc-pipeline.d.ts.map +1 -1
- package/dist/client/rpc-pipeline.js +407 -123
- package/dist/client/rpc-pipeline.js.map +1 -1
- package/dist/client/runtime.d.ts +1 -3
- package/dist/client/runtime.d.ts.map +1 -1
- package/dist/client/runtime.js +4 -7
- package/dist/client/runtime.js.map +1 -1
- package/dist/client/v2-e2ee.d.ts +20 -2
- package/dist/client/v2-e2ee.d.ts.map +1 -1
- package/dist/client/v2-e2ee.js +434 -112
- package/dist/client/v2-e2ee.js.map +1 -1
- package/dist/client.d.ts +22 -10
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +500 -116
- package/dist/client.js.map +1 -1
- package/dist/facades.d.ts +23 -1
- package/dist/facades.d.ts.map +1 -1
- package/dist/facades.js +55 -7
- package/dist/facades.js.map +1 -1
- package/dist/group-id.d.ts.map +1 -1
- package/dist/group-id.js +2 -17
- package/dist/group-id.js.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/seq-tracker.d.ts.map +1 -1
- package/dist/seq-tracker.js +8 -2
- package/dist/seq-tracker.js.map +1 -1
- package/dist/tools/cross-sdk-agent.js +23 -1
- package/dist/tools/cross-sdk-agent.js.map +1 -1
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +8 -1
- package/dist/transport.js.map +1 -1
- package/dist/validators.d.ts.map +1 -1
- package/dist/validators.js +8 -14
- package/dist/validators.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +6 -5
package/dist/client/lifecycle.js
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
|
-
import { StateError, ValidationError } from '../errors.js';
|
|
2
1
|
import { ConnectionState } from '../types.js';
|
|
2
|
+
import { AUNError, AuthError, ConnectionError, PermissionError, StateError, TimeoutError, ValidationError, } from '../errors.js';
|
|
3
|
+
function shouldRetryInitialConnect(error) {
|
|
4
|
+
if (error instanceof AuthError) {
|
|
5
|
+
const message = String(error.message ?? '').toLowerCase();
|
|
6
|
+
return message.includes('long_connection_already_exists')
|
|
7
|
+
|| message.includes('long connection already exists')
|
|
8
|
+
|| message.includes('aid_login1_failed')
|
|
9
|
+
|| message.includes('aid_login2_failed');
|
|
10
|
+
}
|
|
11
|
+
if (error instanceof PermissionError || error instanceof ValidationError || error instanceof StateError) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
if (error instanceof ConnectionError || error instanceof TimeoutError) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
if (error instanceof AUNError) {
|
|
18
|
+
return error.retryable;
|
|
19
|
+
}
|
|
20
|
+
// 浏览器底层 WebSocket/网络异常不一定继承 SDK 错误类型,默认视为瞬时错误。
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
3
23
|
const PUBLIC_CONNECTION_OPTION_KEYS = new Set([
|
|
4
24
|
'auto_reconnect',
|
|
5
25
|
'connect_timeout',
|
|
@@ -70,9 +90,144 @@ export class LifecycleController {
|
|
|
70
90
|
constructor(runtime) {
|
|
71
91
|
this.runtime = runtime;
|
|
72
92
|
}
|
|
93
|
+
beginConnectionAttempt() {
|
|
94
|
+
const client = this.runtime.client;
|
|
95
|
+
if (client._closing || client.state === ConnectionState.CLOSED) {
|
|
96
|
+
throw new StateError('connect not allowed on a closed client');
|
|
97
|
+
}
|
|
98
|
+
if (client._disconnecting) {
|
|
99
|
+
throw new StateError('connect not allowed while disconnect is in progress');
|
|
100
|
+
}
|
|
101
|
+
if (client._connectDone) {
|
|
102
|
+
throw new StateError('connect already in progress');
|
|
103
|
+
}
|
|
104
|
+
const owner = new AbortController();
|
|
105
|
+
let resolveDone;
|
|
106
|
+
const done = new Promise((resolve) => { resolveDone = resolve; });
|
|
107
|
+
client._connectAbort = owner;
|
|
108
|
+
client._connectDoneOwner = owner;
|
|
109
|
+
client._connectDone = done;
|
|
110
|
+
client._connectDoneResolve = resolveDone;
|
|
111
|
+
client._connectEventDispatchDepth = Number(client._connectEventDispatchDepth ?? 0);
|
|
112
|
+
return owner;
|
|
113
|
+
}
|
|
114
|
+
finishConnectionAttempt(owner) {
|
|
115
|
+
const client = this.runtime.client;
|
|
116
|
+
if (client._connectDoneOwner !== owner)
|
|
117
|
+
return;
|
|
118
|
+
const resolveDone = client._connectDoneResolve;
|
|
119
|
+
if (client._connectAbort === owner)
|
|
120
|
+
client._connectAbort = null;
|
|
121
|
+
client._connectDoneOwner = null;
|
|
122
|
+
client._connectDone = null;
|
|
123
|
+
client._connectDoneResolve = null;
|
|
124
|
+
resolveDone?.();
|
|
125
|
+
}
|
|
126
|
+
ownsConnectionAttempt(owner) {
|
|
127
|
+
if (!owner)
|
|
128
|
+
return true;
|
|
129
|
+
const client = this.runtime.client;
|
|
130
|
+
return client._connectAbort === owner
|
|
131
|
+
&& !owner.signal.aborted
|
|
132
|
+
&& !client._closing
|
|
133
|
+
&& !client._disconnecting;
|
|
134
|
+
}
|
|
135
|
+
assertConnectionAttemptOwner(owner) {
|
|
136
|
+
if (owner && !this.ownsConnectionAttempt(owner)) {
|
|
137
|
+
throw new StateError('connection attempt superseded');
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
assertAttemptOwners(connectionOwner, reconnectOwner) {
|
|
141
|
+
this.assertConnectionAttemptOwner(connectionOwner);
|
|
142
|
+
this.runtime.client._assertReconnectOwner?.(reconnectOwner);
|
|
143
|
+
}
|
|
144
|
+
async waitForAttempt(operation, connectionOwner, reconnectOwner) {
|
|
145
|
+
const owner = connectionOwner ?? reconnectOwner;
|
|
146
|
+
if (!owner)
|
|
147
|
+
return operation;
|
|
148
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
let settled = false;
|
|
151
|
+
const finish = (callback) => {
|
|
152
|
+
if (settled)
|
|
153
|
+
return;
|
|
154
|
+
settled = true;
|
|
155
|
+
owner.signal.removeEventListener('abort', onAbort);
|
|
156
|
+
callback();
|
|
157
|
+
};
|
|
158
|
+
const onAbort = () => finish(() => reject(connectionOwner
|
|
159
|
+
? new StateError('connection attempt superseded')
|
|
160
|
+
: new ConnectionError('reconnect attempt superseded')));
|
|
161
|
+
owner.signal.addEventListener('abort', onAbort, { once: true });
|
|
162
|
+
if (owner.signal.aborted) {
|
|
163
|
+
onAbort();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
operation.then((value) => finish(() => {
|
|
167
|
+
try {
|
|
168
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
169
|
+
resolve(value);
|
|
170
|
+
}
|
|
171
|
+
catch (err) {
|
|
172
|
+
reject(err);
|
|
173
|
+
}
|
|
174
|
+
}), (err) => finish(() => reject(err)));
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
async publishConnectionEvent(owner, event, payload) {
|
|
178
|
+
const client = this.runtime.client;
|
|
179
|
+
this.assertConnectionAttemptOwner(owner);
|
|
180
|
+
client._connectEventDispatchDepth = Number(client._connectEventDispatchDepth ?? 0) + 1;
|
|
181
|
+
try {
|
|
182
|
+
await client._dispatcher.publish(event, payload);
|
|
183
|
+
}
|
|
184
|
+
finally {
|
|
185
|
+
client._connectEventDispatchDepth -= 1;
|
|
186
|
+
}
|
|
187
|
+
return this.ownsConnectionAttempt(owner);
|
|
188
|
+
}
|
|
189
|
+
async cancelConnectionAttemptAndWait() {
|
|
190
|
+
const client = this.runtime.client;
|
|
191
|
+
const owner = client._connectDoneOwner;
|
|
192
|
+
const done = client._connectDone;
|
|
193
|
+
if (!owner || !done)
|
|
194
|
+
return;
|
|
195
|
+
if (client._connectAbort === owner)
|
|
196
|
+
client._connectAbort = null;
|
|
197
|
+
owner.abort();
|
|
198
|
+
// READY 事件按顺序同步分发;handler 内等待所属 connect 会自锁。
|
|
199
|
+
if (Number(client._connectEventDispatchDepth ?? 0) > 0)
|
|
200
|
+
return;
|
|
201
|
+
let timeout = null;
|
|
202
|
+
try {
|
|
203
|
+
await Promise.race([
|
|
204
|
+
done,
|
|
205
|
+
new Promise((_, reject) => {
|
|
206
|
+
timeout = setTimeout(() => reject(new StateError('timed out waiting for connect attempt to stop')), 1_000);
|
|
207
|
+
}),
|
|
208
|
+
]);
|
|
209
|
+
}
|
|
210
|
+
finally {
|
|
211
|
+
if (timeout !== null)
|
|
212
|
+
clearTimeout(timeout);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** 首连后台重试使用的完整尝试;不在失败时再次创建重连任务。 */
|
|
216
|
+
async retryInitialConnect(opts, reconnectOwner) {
|
|
217
|
+
const client = this.runtime.client;
|
|
218
|
+
client._assertReconnectOwner?.(reconnectOwner);
|
|
219
|
+
this.runtime.lifecycle.setState('standby');
|
|
220
|
+
client._gatewayUrl = null;
|
|
221
|
+
client._gatewayCandidates = [];
|
|
222
|
+
return this.connect(opts, true, reconnectOwner);
|
|
223
|
+
}
|
|
73
224
|
async authenticate(options = {}) {
|
|
74
225
|
const client = this.runtime.client;
|
|
75
226
|
const tStart = Date.now();
|
|
227
|
+
const inheritedAttempt = client._activeAuthenticateAttempt;
|
|
228
|
+
let connectionOwner = inheritedAttempt?.connectionOwner;
|
|
229
|
+
const reconnectOwner = inheritedAttempt?.reconnectOwner;
|
|
230
|
+
let ownsPublicAttempt = false;
|
|
76
231
|
const target = client._currentAid?.aid ?? client._aid ?? '';
|
|
77
232
|
if (!target || !client._currentAid?.isPrivateKeyValid()) {
|
|
78
233
|
throw new StateError('authenticate requires a loaded AID with a valid private key');
|
|
@@ -87,11 +242,24 @@ export class LifecycleController {
|
|
|
87
242
|
if ('aid' in options || 'access_token' in options || 'token' in options || 'kite_token' in options) {
|
|
88
243
|
throw new ValidationError('authenticate options must not include aid or token fields; load an AID object first');
|
|
89
244
|
}
|
|
245
|
+
if (!connectionOwner && !reconnectOwner) {
|
|
246
|
+
connectionOwner = this.beginConnectionAttempt();
|
|
247
|
+
ownsPublicAttempt = true;
|
|
248
|
+
}
|
|
249
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
90
250
|
this.runtime.lifecycle.setState('connecting');
|
|
91
251
|
try {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
252
|
+
let discovered;
|
|
253
|
+
if (typeof client._resolveGatewayCandidatesForAid === 'function') {
|
|
254
|
+
discovered = await this.waitForAttempt(Promise.resolve(client._resolveGatewayCandidatesForAid(target)), connectionOwner, reconnectOwner);
|
|
255
|
+
}
|
|
256
|
+
else if (client._gatewayUrl) {
|
|
257
|
+
discovered = [String(client._gatewayUrl).trim()];
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
const gateway = await this.waitForAttempt(Promise.resolve(client._resolveGatewayForAid(target)), connectionOwner, reconnectOwner);
|
|
261
|
+
discovered = [String(gateway).trim()];
|
|
262
|
+
}
|
|
95
263
|
const gateways = [...new Set(discovered
|
|
96
264
|
.map((item) => String(item ?? '').trim()).filter(Boolean))];
|
|
97
265
|
if (gateways.length === 0)
|
|
@@ -101,7 +269,7 @@ export class LifecycleController {
|
|
|
101
269
|
let lastError = null;
|
|
102
270
|
for (const candidate of gateways) {
|
|
103
271
|
try {
|
|
104
|
-
result = await client._auth.authenticate(candidate, target);
|
|
272
|
+
result = await this.waitForAttempt(Promise.resolve(client._auth.authenticate(candidate, target)), connectionOwner, reconnectOwner);
|
|
105
273
|
gateway = candidate;
|
|
106
274
|
break;
|
|
107
275
|
}
|
|
@@ -114,11 +282,12 @@ export class LifecycleController {
|
|
|
114
282
|
}
|
|
115
283
|
if (result === null)
|
|
116
284
|
throw (lastError instanceof Error ? lastError : new Error(String(lastError)));
|
|
285
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
117
286
|
client._gatewayCandidates = gateways;
|
|
118
287
|
this.runtime.lifecycle.setGatewayUrl(gatewayFromAuthResult(result, gateway));
|
|
119
288
|
let loadedIdentity = null;
|
|
120
289
|
try {
|
|
121
|
-
loadedIdentity = await client._auth.loadIdentityOrNone(target);
|
|
290
|
+
loadedIdentity = await this.waitForAttempt(Promise.resolve(client._auth.loadIdentityOrNone(target)), connectionOwner, reconnectOwner);
|
|
122
291
|
}
|
|
123
292
|
catch (exc) {
|
|
124
293
|
client._clientLog.debug(`authenticate identity reload skipped: ${exc instanceof Error ? exc.message : String(exc)}`);
|
|
@@ -130,13 +299,19 @@ export class LifecycleController {
|
|
|
130
299
|
return result;
|
|
131
300
|
}
|
|
132
301
|
catch (err) {
|
|
302
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
133
303
|
this.runtime.lifecycle.setState('standby');
|
|
134
304
|
this.runtime.lifecycle.setError(err instanceof Error ? err : new Error(String(err)), 'AUTHENTICATE_FAILED');
|
|
135
305
|
client._clientLog.debug(`authenticate exit (error): elapsed=${Date.now() - tStart}ms err=${err instanceof Error ? err.message : String(err)}`);
|
|
136
306
|
throw err;
|
|
137
307
|
}
|
|
308
|
+
finally {
|
|
309
|
+
if (ownsPublicAttempt && connectionOwner) {
|
|
310
|
+
this.finishConnectionAttempt(connectionOwner);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
138
313
|
}
|
|
139
|
-
async connect(opts) {
|
|
314
|
+
async connect(opts, fromInitialReconnect = false, reconnectOwner) {
|
|
140
315
|
const client = this.runtime.client;
|
|
141
316
|
const tStart = Date.now();
|
|
142
317
|
if (opts !== undefined && opts !== null && typeof opts === 'object') {
|
|
@@ -150,177 +325,314 @@ export class LifecycleController {
|
|
|
150
325
|
if (!target || !client._currentAid?.isPrivateKeyValid()) {
|
|
151
326
|
throw new StateError('connect requires a loaded AID with a valid private key');
|
|
152
327
|
}
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
options
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
options.retry = opts.retry;
|
|
166
|
-
if (opts?.retry_initial_delay !== undefined || opts?.retry_max_delay !== undefined || opts?.retry_max_attempts !== undefined) {
|
|
167
|
-
const baseRetry = isRecord(options.retry)
|
|
168
|
-
? { ...options.retry }
|
|
169
|
-
: { initial_delay: 1, max_delay: 64, max_attempts: 0 };
|
|
170
|
-
options.retry = {
|
|
171
|
-
...baseRetry,
|
|
172
|
-
...(opts.retry_initial_delay !== undefined ? { initial_delay: opts.retry_initial_delay } : {}),
|
|
173
|
-
...(opts.retry_max_delay !== undefined ? { max_delay: opts.retry_max_delay } : {}),
|
|
174
|
-
...(opts.retry_max_attempts !== undefined ? { max_attempts: opts.retry_max_attempts } : {}),
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
if (opts?.connection_kind !== undefined)
|
|
178
|
-
options.connection_kind = opts.connection_kind;
|
|
179
|
-
if (opts?.short_ttl_ms !== undefined)
|
|
180
|
-
options.short_ttl_ms = opts.short_ttl_ms;
|
|
181
|
-
if (opts?.delivery_mode !== undefined)
|
|
182
|
-
options.delivery_mode = opts.delivery_mode;
|
|
183
|
-
if (opts?.extra_info !== undefined)
|
|
184
|
-
options.extra_info = opts.extra_info;
|
|
185
|
-
if (opts?.background_sync !== undefined)
|
|
186
|
-
options.background_sync = opts.background_sync;
|
|
187
|
-
client._clientLog.debug(`connect enter: state=${client._state} aid=${client._aid ?? '-'}`);
|
|
188
|
-
const publicState = client.state;
|
|
189
|
-
const allowed = new Set([
|
|
190
|
-
ConnectionState.STANDBY,
|
|
191
|
-
ConnectionState.AUTHENTICATED,
|
|
192
|
-
ConnectionState.RETRY_BACKOFF,
|
|
193
|
-
ConnectionState.CONNECTION_FAILED,
|
|
194
|
-
]);
|
|
195
|
-
if (!allowed.has(publicState)) {
|
|
196
|
-
client._clientLog.debug(`connect exit (error): elapsed=${Date.now() - tStart}ms err=invalid_state state=${client._state}`);
|
|
197
|
-
throw new StateError(`connect not allowed in state ${publicState}`);
|
|
198
|
-
}
|
|
199
|
-
if (publicState === ConnectionState.RETRY_BACKOFF && client._reconnectAbort) {
|
|
200
|
-
client._reconnectAbort.abort();
|
|
201
|
-
this.runtime.lifecycle.clearReconnectState();
|
|
202
|
-
}
|
|
203
|
-
if (publicState === ConnectionState.CONNECTION_FAILED) {
|
|
204
|
-
this.runtime.lifecycle.setRetryAttempt(0);
|
|
205
|
-
this.runtime.lifecycle.setError(null, null);
|
|
206
|
-
}
|
|
207
|
-
this.runtime.lifecycle.setNextRetryAt(null);
|
|
208
|
-
let authResult = null;
|
|
209
|
-
if (!client._gatewayUrl) {
|
|
210
|
-
authResult = await client.authenticate();
|
|
211
|
-
}
|
|
212
|
-
this.runtime.lifecycle.setState('connecting');
|
|
213
|
-
const gateway = String(client._gatewayUrl ?? '').trim();
|
|
214
|
-
const accessToken = accessTokenFromAuthResult(authResult) || cachedAccessToken(client);
|
|
215
|
-
const params = {
|
|
216
|
-
...options,
|
|
217
|
-
gateway,
|
|
218
|
-
...(Array.isArray(client._gatewayCandidates) && client._gatewayCandidates.length > 0
|
|
219
|
-
? { gateways: [...client._gatewayCandidates] }
|
|
220
|
-
: {}),
|
|
221
|
-
...(accessToken ? { access_token: accessToken } : {}),
|
|
222
|
-
};
|
|
223
|
-
const normalized = client._normalizeConnectParams(params);
|
|
224
|
-
this.runtime.lifecycle.setSession(normalized, client._buildSessionOptions(normalized));
|
|
225
|
-
const connectTimeoutSec = client._sessionOptions.timeouts.connect;
|
|
226
|
-
client._transport.setConnectTimeout(connectTimeoutSec != null ? connectTimeoutSec : 5);
|
|
227
|
-
const callTimeoutSec = client._sessionOptions.timeouts.call;
|
|
228
|
-
client._transport.setTimeout(callTimeoutSec != null ? callTimeoutSec : 35);
|
|
229
|
-
this.runtime.lifecycle.setClosing(false);
|
|
230
|
-
const gateways = client._resolveGateways(normalized);
|
|
231
|
-
let lastErr = null;
|
|
232
|
-
for (const gw of gateways) {
|
|
233
|
-
try {
|
|
234
|
-
const gwParams = { ...normalized, gateway: gw };
|
|
235
|
-
await client._connectOnce(gwParams, true);
|
|
236
|
-
client._clientLog.debug(`connect exit: elapsed=${Date.now() - tStart}ms state=${client._state}`);
|
|
237
|
-
return;
|
|
328
|
+
const connectionOwner = fromInitialReconnect ? undefined : this.beginConnectionAttempt();
|
|
329
|
+
try {
|
|
330
|
+
const options = {};
|
|
331
|
+
if (opts?.auto_reconnect !== undefined)
|
|
332
|
+
options.auto_reconnect = opts.auto_reconnect;
|
|
333
|
+
if (opts?.heartbeat_interval !== undefined)
|
|
334
|
+
options.heartbeat_interval = opts.heartbeat_interval;
|
|
335
|
+
if (opts?.connect_timeout !== undefined || opts?.call_timeout !== undefined) {
|
|
336
|
+
options.timeouts = {
|
|
337
|
+
...(opts.connect_timeout !== undefined ? { connect: opts.connect_timeout } : {}),
|
|
338
|
+
...(opts.call_timeout !== undefined ? { call: opts.call_timeout } : {}),
|
|
339
|
+
};
|
|
238
340
|
}
|
|
239
|
-
|
|
240
|
-
|
|
341
|
+
if (opts?.retry !== undefined)
|
|
342
|
+
options.retry = opts.retry;
|
|
343
|
+
if (opts?.retry_initial_delay !== undefined || opts?.retry_max_delay !== undefined || opts?.retry_max_attempts !== undefined) {
|
|
344
|
+
const baseRetry = isRecord(options.retry)
|
|
345
|
+
? { ...options.retry }
|
|
346
|
+
: { initial_delay: 1, max_delay: 64, max_attempts: 0 };
|
|
347
|
+
options.retry = {
|
|
348
|
+
...baseRetry,
|
|
349
|
+
...(opts.retry_initial_delay !== undefined ? { initial_delay: opts.retry_initial_delay } : {}),
|
|
350
|
+
...(opts.retry_max_delay !== undefined ? { max_delay: opts.retry_max_delay } : {}),
|
|
351
|
+
...(opts.retry_max_attempts !== undefined ? { max_attempts: opts.retry_max_attempts } : {}),
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
if (opts?.connection_kind !== undefined)
|
|
355
|
+
options.connection_kind = opts.connection_kind;
|
|
356
|
+
if (opts?.short_ttl_ms !== undefined)
|
|
357
|
+
options.short_ttl_ms = opts.short_ttl_ms;
|
|
358
|
+
if (opts?.delivery_mode !== undefined)
|
|
359
|
+
options.delivery_mode = opts.delivery_mode;
|
|
360
|
+
if (opts?.extra_info !== undefined)
|
|
361
|
+
options.extra_info = opts.extra_info;
|
|
362
|
+
if (opts?.background_sync !== undefined)
|
|
363
|
+
options.background_sync = opts.background_sync;
|
|
364
|
+
client._clientLog.debug(`connect enter: state=${client._state} aid=${client._aid ?? '-'}`);
|
|
365
|
+
const publicState = client.state;
|
|
366
|
+
const allowed = new Set([
|
|
367
|
+
ConnectionState.STANDBY,
|
|
368
|
+
ConnectionState.AUTHENTICATED,
|
|
369
|
+
ConnectionState.RETRY_BACKOFF,
|
|
370
|
+
ConnectionState.CONNECTION_FAILED,
|
|
371
|
+
]);
|
|
372
|
+
const hasReconnectController = Boolean(client._reconnectAbort || client._reconnectActive);
|
|
373
|
+
if (!allowed.has(publicState) && !(hasReconnectController && !fromInitialReconnect)) {
|
|
374
|
+
client._clientLog.debug(`connect exit (error): elapsed=${Date.now() - tStart}ms err=invalid_state state=${client._state}`);
|
|
375
|
+
throw new StateError(`connect not allowed in state ${publicState}`);
|
|
376
|
+
}
|
|
377
|
+
// 显式 connect 开启新的连接意图,必须取消 standby/retry_backoff 窗口中
|
|
378
|
+
// 尚未完成的旧重连代;内部首连重试不能取消自己的控制器。
|
|
379
|
+
if (!fromInitialReconnect && hasReconnectController) {
|
|
380
|
+
await client._cancelReconnectAndWait();
|
|
381
|
+
}
|
|
382
|
+
else if (fromInitialReconnect) {
|
|
383
|
+
client._assertReconnectOwner?.(reconnectOwner);
|
|
384
|
+
}
|
|
385
|
+
if (publicState === ConnectionState.CONNECTION_FAILED) {
|
|
386
|
+
this.runtime.lifecycle.setRetryAttempt(0);
|
|
387
|
+
this.runtime.lifecycle.setError(null, null);
|
|
388
|
+
}
|
|
389
|
+
this.runtime.lifecycle.setNextRetryAt(null);
|
|
390
|
+
// 认证/发现发生在 session 参数可归一化之前,先保存会话选项,
|
|
391
|
+
// 这样首连失败也能沿用同一套退避与 auto_reconnect 配置。
|
|
392
|
+
this.runtime.lifecycle.setSession(null, client._buildSessionOptions({
|
|
393
|
+
...options,
|
|
394
|
+
connection_kind: opts?.connection_kind ?? 'long',
|
|
395
|
+
}));
|
|
396
|
+
let authResult = null;
|
|
397
|
+
if (!client._gatewayUrl) {
|
|
398
|
+
const previousAttempt = client._activeAuthenticateAttempt;
|
|
399
|
+
client._activeAuthenticateAttempt = { connectionOwner, reconnectOwner };
|
|
241
400
|
try {
|
|
242
|
-
await
|
|
401
|
+
authResult = await this.waitForAttempt(Promise.resolve(client.authenticate()), connectionOwner, reconnectOwner);
|
|
402
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
243
403
|
}
|
|
244
|
-
catch (
|
|
245
|
-
|
|
404
|
+
catch (err) {
|
|
405
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
406
|
+
const connectError = err instanceof Error ? err : new Error(String(err));
|
|
407
|
+
this.runtime.lifecycle.setError(connectError, 'CONNECT_FAILED');
|
|
408
|
+
if (!fromInitialReconnect && client._sessionOptions?.auto_reconnect && shouldRetryInitialConnect(connectError)
|
|
409
|
+
&& typeof client._handleTransportDisconnect === 'function') {
|
|
410
|
+
try {
|
|
411
|
+
await client._handleTransportDisconnect(connectError, undefined, opts ?? {});
|
|
412
|
+
}
|
|
413
|
+
catch (reconnectErr) {
|
|
414
|
+
client._clientLog.warn(`initial reconnect scheduling failed: ${reconnectErr instanceof Error ? reconnectErr.message : String(reconnectErr)}`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
throw err;
|
|
246
418
|
}
|
|
247
|
-
|
|
248
|
-
client.
|
|
419
|
+
finally {
|
|
420
|
+
if (client._activeAuthenticateAttempt?.connectionOwner === connectionOwner
|
|
421
|
+
&& client._activeAuthenticateAttempt?.reconnectOwner === reconnectOwner) {
|
|
422
|
+
client._activeAuthenticateAttempt = previousAttempt ?? null;
|
|
423
|
+
}
|
|
249
424
|
}
|
|
250
|
-
|
|
251
|
-
|
|
425
|
+
}
|
|
426
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
427
|
+
this.runtime.lifecycle.setState('connecting');
|
|
428
|
+
const gateway = String(client._gatewayUrl ?? '').trim();
|
|
429
|
+
const accessToken = accessTokenFromAuthResult(authResult) || cachedAccessToken(client);
|
|
430
|
+
const params = {
|
|
431
|
+
...options,
|
|
432
|
+
gateway,
|
|
433
|
+
...(Array.isArray(client._gatewayCandidates) && client._gatewayCandidates.length > 0
|
|
434
|
+
? { gateways: [...client._gatewayCandidates] }
|
|
435
|
+
: {}),
|
|
436
|
+
...(accessToken ? { access_token: accessToken } : {}),
|
|
437
|
+
};
|
|
438
|
+
const normalized = client._normalizeConnectParams(params);
|
|
439
|
+
this.runtime.lifecycle.setSession(normalized, client._buildSessionOptions(normalized));
|
|
440
|
+
const connectTimeoutSec = client._sessionOptions.timeouts.connect;
|
|
441
|
+
client._transport.setConnectTimeout(connectTimeoutSec != null ? connectTimeoutSec : 5);
|
|
442
|
+
const callTimeoutSec = client._sessionOptions.timeouts.call;
|
|
443
|
+
client._transport.setTimeout(callTimeoutSec != null ? callTimeoutSec : 35);
|
|
444
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
445
|
+
this.runtime.lifecycle.setClosing(false);
|
|
446
|
+
const gateways = client._resolveGateways(normalized);
|
|
447
|
+
let lastErr = null;
|
|
448
|
+
for (const gw of gateways) {
|
|
449
|
+
try {
|
|
450
|
+
const gwParams = { ...normalized, gateway: gw };
|
|
451
|
+
await this.waitForAttempt(Promise.resolve(client._connectOnce(gwParams, true, reconnectOwner)), connectionOwner, reconnectOwner);
|
|
452
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
453
|
+
client._clientLog.debug(`connect exit: elapsed=${Date.now() - tStart}ms state=${client._state}`);
|
|
454
|
+
return;
|
|
252
455
|
}
|
|
456
|
+
catch (err) {
|
|
457
|
+
lastErr = err;
|
|
458
|
+
this.assertAttemptOwners(connectionOwner, reconnectOwner);
|
|
459
|
+
try {
|
|
460
|
+
await client._transport.close();
|
|
461
|
+
}
|
|
462
|
+
catch (closeErr) {
|
|
463
|
+
client._clientLog.debug(`connect gateway cleanup skipped: ${closeErr instanceof Error ? closeErr.message : String(closeErr)}`);
|
|
464
|
+
}
|
|
465
|
+
if (gateways.length > 1) {
|
|
466
|
+
client._clientLog.warn(`connect: gateway ${gw} failed, trying next: ${err instanceof Error ? err.message : String(err)}`);
|
|
467
|
+
}
|
|
468
|
+
if (client._state === 'connecting' || client._state === 'authenticating') {
|
|
469
|
+
this.runtime.lifecycle.setState('connecting');
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
if (client._state === 'connecting' || client._state === 'authenticating') {
|
|
474
|
+
this.runtime.lifecycle.setState(client._currentAid || client._aid ? 'standby' : 'idle');
|
|
253
475
|
}
|
|
476
|
+
const connectError = lastErr instanceof Error ? lastErr : new Error(String(lastErr));
|
|
477
|
+
this.runtime.lifecycle.setError(connectError, 'CONNECT_FAILED');
|
|
478
|
+
if (!fromInitialReconnect && client._sessionOptions?.auto_reconnect && shouldRetryInitialConnect(connectError)
|
|
479
|
+
&& typeof client._handleTransportDisconnect === 'function') {
|
|
480
|
+
try {
|
|
481
|
+
// 首连与已建立连接的断线共享同一重连入口,保证退避、并发闸门和取消语义一致。
|
|
482
|
+
await client._handleTransportDisconnect(connectError, undefined, opts ?? {});
|
|
483
|
+
}
|
|
484
|
+
catch (reconnectErr) {
|
|
485
|
+
client._clientLog.warn(`initial reconnect scheduling failed: ${reconnectErr instanceof Error ? reconnectErr.message : String(reconnectErr)}`);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
client._clientLog.debug(`connect exit (error): elapsed=${Date.now() - tStart}ms err=${lastErr instanceof Error ? lastErr.message : String(lastErr)}`);
|
|
489
|
+
throw lastErr;
|
|
254
490
|
}
|
|
255
|
-
|
|
256
|
-
|
|
491
|
+
finally {
|
|
492
|
+
if (connectionOwner)
|
|
493
|
+
this.finishConnectionAttempt(connectionOwner);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
lifecycleStopEventIsReentrant() {
|
|
497
|
+
const client = this.runtime.client;
|
|
498
|
+
return Boolean(client._lifecycleStopActive)
|
|
499
|
+
&& Number(client._lifecycleStopEventDispatchDepth ?? 0) > 0;
|
|
500
|
+
}
|
|
501
|
+
scheduleReentrantClose() {
|
|
502
|
+
const client = this.runtime.client;
|
|
503
|
+
const tail = client._lifecycleStopTail;
|
|
504
|
+
if (!tail)
|
|
505
|
+
return;
|
|
506
|
+
void tail.then(() => this.close()).catch((err) => {
|
|
507
|
+
client._clientLog.warn(`reentrant close failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
async publishLifecycleStopEvent(payload) {
|
|
511
|
+
const client = this.runtime.client;
|
|
512
|
+
client._lifecycleStopEventDispatchDepth = Number(client._lifecycleStopEventDispatchDepth ?? 0) + 1;
|
|
513
|
+
try {
|
|
514
|
+
await client._dispatcher.publish('state_change', payload);
|
|
515
|
+
}
|
|
516
|
+
finally {
|
|
517
|
+
client._lifecycleStopEventDispatchDepth -= 1;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
async withLifecycleStop(operation, kind) {
|
|
521
|
+
const client = this.runtime.client;
|
|
522
|
+
const previous = client._lifecycleStopTail;
|
|
523
|
+
let release;
|
|
524
|
+
const gate = new Promise((resolve) => { release = resolve; });
|
|
525
|
+
// 首个 stop 操作要在当前调用栈内进入清理;只有已有 stop 时才排队等待。
|
|
526
|
+
// 这样既保持 stop 之间的串行化,也不让调用方观察到无意义的额外微任务延迟。
|
|
527
|
+
const tail = previous ? previous.catch(() => { }).then(() => gate) : gate;
|
|
528
|
+
client._lifecycleStopTail = tail;
|
|
529
|
+
if (previous)
|
|
530
|
+
await previous.catch(() => { });
|
|
531
|
+
client._lifecycleStopActive = true;
|
|
532
|
+
client._lifecycleStopKind = kind;
|
|
533
|
+
try {
|
|
534
|
+
return await operation();
|
|
535
|
+
}
|
|
536
|
+
finally {
|
|
537
|
+
client._lifecycleStopActive = false;
|
|
538
|
+
client._lifecycleStopKind = null;
|
|
539
|
+
release();
|
|
540
|
+
if (client._lifecycleStopTail === tail)
|
|
541
|
+
client._lifecycleStopTail = null;
|
|
257
542
|
}
|
|
258
|
-
this.runtime.lifecycle.setError(lastErr instanceof Error ? lastErr : new Error(String(lastErr)), 'CONNECT_FAILED');
|
|
259
|
-
client._clientLog.debug(`connect exit (error): elapsed=${Date.now() - tStart}ms err=${lastErr instanceof Error ? lastErr.message : String(lastErr)}`);
|
|
260
|
-
throw lastErr;
|
|
261
543
|
}
|
|
262
544
|
async disconnect() {
|
|
263
545
|
const client = this.runtime.client;
|
|
264
546
|
const tStart = Date.now();
|
|
265
547
|
client._clientLog.debug(`disconnect enter: state=${client._state}`);
|
|
266
|
-
if (client._closing)
|
|
267
|
-
client._clientLog.debug(`disconnect exit: elapsed=${Date.now() - tStart}ms reason=closing`);
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
if (![
|
|
271
|
-
ConnectionState.AUTHENTICATED,
|
|
272
|
-
ConnectionState.CONNECTING,
|
|
273
|
-
ConnectionState.READY,
|
|
274
|
-
ConnectionState.RETRY_BACKOFF,
|
|
275
|
-
ConnectionState.RECONNECTING,
|
|
276
|
-
ConnectionState.CONNECTION_FAILED,
|
|
277
|
-
].includes(client.state)) {
|
|
278
|
-
client._clientLog.debug(`disconnect exit: elapsed=${Date.now() - tStart}ms reason=not_connected`);
|
|
548
|
+
if (client._closing || client._disconnecting || this.lifecycleStopEventIsReentrant())
|
|
279
549
|
return;
|
|
550
|
+
client._disconnecting = true;
|
|
551
|
+
try {
|
|
552
|
+
return await this.withLifecycleStop(async () => {
|
|
553
|
+
if (client._closing) {
|
|
554
|
+
client._clientLog.debug(`disconnect exit: elapsed=${Date.now() - tStart}ms reason=closing`);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
const hasInitialReconnect = Boolean(client._reconnectActive || client._reconnectAbort || client._reconnectTask);
|
|
558
|
+
const hasConnect = Boolean(client._connectDone);
|
|
559
|
+
if (!([
|
|
560
|
+
ConnectionState.AUTHENTICATED,
|
|
561
|
+
ConnectionState.CONNECTING,
|
|
562
|
+
ConnectionState.READY,
|
|
563
|
+
ConnectionState.RETRY_BACKOFF,
|
|
564
|
+
ConnectionState.RECONNECTING,
|
|
565
|
+
ConnectionState.CONNECTION_FAILED,
|
|
566
|
+
].includes(client.state)) && !hasInitialReconnect && !hasConnect) {
|
|
567
|
+
client._clientLog.debug(`disconnect exit: elapsed=${Date.now() - tStart}ms reason=not_connected`);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
// 不等待持久化或 transport.close,先隔离已在途的 inline Push 回调。
|
|
571
|
+
client._delivery.resetInlineAckState();
|
|
572
|
+
const pullInvalidation = client._rpcPipeline?.invalidatePulls?.();
|
|
573
|
+
client._rpcPipeline?.stopPullGateWatchdogs?.();
|
|
574
|
+
await client._transport.close();
|
|
575
|
+
await pullInvalidation;
|
|
576
|
+
await client._cancelReconnectAndWait();
|
|
577
|
+
await this.cancelConnectionAttemptAndWait();
|
|
578
|
+
await client._saveSeqTrackerState();
|
|
579
|
+
client._stopBackgroundTasks();
|
|
580
|
+
// close() 可能在 transport.close 等待期间抢先声明关闭意图;此时不得回写 standby。
|
|
581
|
+
if (client._closing)
|
|
582
|
+
return;
|
|
583
|
+
this.runtime.lifecycle.resetForDisconnect('standby');
|
|
584
|
+
await this.publishLifecycleStopEvent({ state: client._publicState(client._state) });
|
|
585
|
+
client._clientLog.debug(`disconnect exit: elapsed=${Date.now() - tStart}ms`);
|
|
586
|
+
}, 'disconnect');
|
|
280
587
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
await client._saveSeqTrackerState();
|
|
284
|
-
client._stopBackgroundTasks();
|
|
285
|
-
if (client._reconnectAbort) {
|
|
286
|
-
client._reconnectAbort.abort();
|
|
287
|
-
this.runtime.lifecycle.clearReconnectState();
|
|
588
|
+
finally {
|
|
589
|
+
client._disconnecting = false;
|
|
288
590
|
}
|
|
289
|
-
await client._transport.close();
|
|
290
|
-
this.runtime.lifecycle.resetForDisconnect('standby');
|
|
291
|
-
await client._dispatcher.publish('state_change', { state: client._publicState(client._state) });
|
|
292
|
-
client._clientLog.debug(`disconnect exit: elapsed=${Date.now() - tStart}ms`);
|
|
293
591
|
}
|
|
294
592
|
async close() {
|
|
295
593
|
const client = this.runtime.client;
|
|
296
594
|
const tStart = Date.now();
|
|
297
595
|
client._clientLog.debug(`close enter: state=${client._state}`);
|
|
596
|
+
// 关闭意图必须在第一个 await 前生效,阻止迟到的连接/重连结果复活客户端。
|
|
298
597
|
this.runtime.lifecycle.setClosing(true);
|
|
299
|
-
// 不等待持久化、logout 或 transport;立即隔离已在途的 inline Push 回调。
|
|
300
598
|
client._delivery.resetInlineAckState();
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
599
|
+
if (this.lifecycleStopEventIsReentrant()) {
|
|
600
|
+
// 当前 stop 操作正在等待自己的 state_change handler;直接等待闸门会自锁。
|
|
601
|
+
if (client._lifecycleStopKind !== 'close')
|
|
602
|
+
this.scheduleReentrantClose();
|
|
603
|
+
return;
|
|
306
604
|
}
|
|
307
|
-
|
|
605
|
+
return this.withLifecycleStop(async () => {
|
|
606
|
+
const pullInvalidation = client._rpcPipeline?.invalidatePulls?.();
|
|
607
|
+
client._rpcPipeline?.stopPullGateWatchdogs?.();
|
|
608
|
+
client._stopBackgroundTasks();
|
|
609
|
+
if (client._state === 'idle' || client._state === 'closed') {
|
|
610
|
+
const reconnectCancellation = client._cancelReconnectAndWait();
|
|
611
|
+
const connectionCancellation = this.cancelConnectionAttemptAndWait();
|
|
612
|
+
await Promise.all([reconnectCancellation, connectionCancellation]);
|
|
613
|
+
await pullInvalidation;
|
|
614
|
+
await client._saveSeqTrackerState();
|
|
615
|
+
this.runtime.lifecycle.setState('closed');
|
|
616
|
+
client._resetSeqTrackingState();
|
|
617
|
+
client._clientLog.debug(`close exit: elapsed=${Date.now() - tStart}ms reason=already_idle`);
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
const reconnectCancellation = client._cancelReconnectAndWait();
|
|
621
|
+
const connectionCancellation = this.cancelConnectionAttemptAndWait();
|
|
622
|
+
try {
|
|
623
|
+
await client._transport.call('auth.logout', {});
|
|
624
|
+
}
|
|
625
|
+
catch (err) {
|
|
626
|
+
client._clientLog.warn(`auth.logout during close failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
627
|
+
}
|
|
628
|
+
await client._transport.close();
|
|
629
|
+
await Promise.all([pullInvalidation, reconnectCancellation, connectionCancellation]);
|
|
630
|
+
await client._saveSeqTrackerState();
|
|
308
631
|
this.runtime.lifecycle.setState('closed');
|
|
632
|
+
await this.publishLifecycleStopEvent({ state: client._publicState(client._state) });
|
|
309
633
|
client._resetSeqTrackingState();
|
|
310
|
-
client._clientLog.debug(`close exit: elapsed=${Date.now() - tStart}ms
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
try {
|
|
314
|
-
await client._transport.call('auth.logout', {});
|
|
315
|
-
}
|
|
316
|
-
catch {
|
|
317
|
-
// auth.logout 失败不影响关闭流程。
|
|
318
|
-
}
|
|
319
|
-
await client._transport.close();
|
|
320
|
-
this.runtime.lifecycle.setState('closed');
|
|
321
|
-
await client._dispatcher.publish('state_change', { state: client._publicState(client._state) });
|
|
322
|
-
client._resetSeqTrackingState();
|
|
323
|
-
client._clientLog.debug(`close exit: elapsed=${Date.now() - tStart}ms`);
|
|
634
|
+
client._clientLog.debug(`close exit: elapsed=${Date.now() - tStart}ms`);
|
|
635
|
+
}, 'close');
|
|
324
636
|
}
|
|
325
637
|
}
|
|
326
638
|
//# sourceMappingURL=lifecycle.js.map
|