fix(bridge): preserve QQ runtime failures across session updates
Surface connection failures immediately to waiting requests and keep the original error visible until the session reconnects. Ignore late login updates from the failed connection.
This commit is contained in:
4 files changed
+269
-40
No files matched your search
@@ -563,6 +563,7 @@ export class QqHookClient extends EventEmitter {
|
||||
private recvHelloResolver: Deferred<QqHookHello> | null = null;
|
||||
private controlWriteChain: Promise<unknown> = Promise.resolve();
|
||||
private closed = false;
|
||||
private runtimeFailure: Error | null = null;
|
||||
private loginState: QqHookLoginState = { loggedIn: false, uin: '0', uinNumber: 0n };
|
||||
private readonly loginWaiters = new Set<Deferred<QqHookLoginState>>();
|
||||
|
||||
@@ -615,10 +616,7 @@ export class QqHookClient extends EventEmitter {
|
||||
|
||||
async waitForLogin({ timeoutMs = 0 } = {}): Promise<QqHookLoginState> {
|
||||
await this.connect();
|
||||
|
||||
if (this.closed) {
|
||||
throw new Error('qq_hook client is closed');
|
||||
}
|
||||
this.assertUsable();
|
||||
|
||||
if (this.loginState.loggedIn) {
|
||||
return this.getLoginState();
|
||||
@@ -692,9 +690,7 @@ export class QqHookClient extends EventEmitter {
|
||||
}
|
||||
|
||||
async connect(): Promise<QqHookHello> {
|
||||
if (this.closed) {
|
||||
throw new Error('qq_hook client is closed');
|
||||
}
|
||||
this.assertUsable();
|
||||
|
||||
if (this.controlSocket && this.controlHello) {
|
||||
return this.controlHello;
|
||||
@@ -714,9 +710,7 @@ export class QqHookClient extends EventEmitter {
|
||||
}
|
||||
|
||||
async startRecv(onPacket?: (packet: QqHookPacket) => void): Promise<QqHookHello> {
|
||||
if (this.closed) {
|
||||
throw new Error('qq_hook client is closed');
|
||||
}
|
||||
this.assertUsable();
|
||||
|
||||
if (typeof onPacket === 'function') {
|
||||
this.on('packet', onPacket);
|
||||
@@ -996,6 +990,16 @@ export class QqHookClient extends EventEmitter {
|
||||
this.controlWriteChain = Promise.resolve();
|
||||
}
|
||||
|
||||
private assertUsable(): void {
|
||||
if (this.runtimeFailure) {
|
||||
throw this.runtimeFailure;
|
||||
}
|
||||
|
||||
if (this.closed) {
|
||||
throw new Error('qq_hook client is closed');
|
||||
}
|
||||
}
|
||||
|
||||
private rejectLoginWaiters(error: Error): void {
|
||||
for (const waiter of this.loginWaiters) {
|
||||
waiter.reject(error);
|
||||
@@ -1033,26 +1037,28 @@ export class QqHookClient extends EventEmitter {
|
||||
const socket = this.controlSocket;
|
||||
|
||||
if (!socket || socket.destroyed) {
|
||||
return Promise.reject(new Error('control pipe is not connected'));
|
||||
return Promise.reject(
|
||||
this.runtimeFailure ?? new Error('control pipe is not connected'),
|
||||
);
|
||||
}
|
||||
|
||||
const writePromise = this.controlWriteChain.then(
|
||||
() =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
if (!this.controlSocket || this.controlSocket.destroyed) {
|
||||
reject(new Error('control pipe is not connected'));
|
||||
return;
|
||||
}
|
||||
const writePromise = this.controlWriteChain.then(() => {
|
||||
this.assertUsable();
|
||||
|
||||
socket.write(payload, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
if (this.controlSocket !== socket || socket.destroyed) {
|
||||
throw new Error('control pipe is not connected');
|
||||
}
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
socket.write(payload, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
this.controlWriteChain = writePromise.catch(() => {});
|
||||
return writePromise;
|
||||
}
|
||||
@@ -1132,6 +1138,11 @@ export class QqHookClient extends EventEmitter {
|
||||
),
|
||||
);
|
||||
|
||||
if (this.closed || this.runtimeFailure) {
|
||||
socket.destroy();
|
||||
this.assertUsable();
|
||||
}
|
||||
|
||||
if (kind === 'control') {
|
||||
this.controlSocket = socket;
|
||||
} else {
|
||||
@@ -1146,6 +1157,8 @@ export class QqHookClient extends EventEmitter {
|
||||
phase = 'hello';
|
||||
const hello = await this.waitForHello(kind === 'recv');
|
||||
|
||||
this.assertUsable();
|
||||
|
||||
if (kind === 'control') {
|
||||
this.controlHello = hello;
|
||||
} else {
|
||||
@@ -1301,6 +1314,10 @@ export class QqHookClient extends EventEmitter {
|
||||
}
|
||||
|
||||
private handleControlFrame(frame: PipeFrame): void {
|
||||
if (this.runtimeFailure) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame.op === PipeOp.hello) {
|
||||
this.resolveHello(frame, false);
|
||||
return;
|
||||
@@ -1346,6 +1363,16 @@ export class QqHookClient extends EventEmitter {
|
||||
frame.status,
|
||||
frame.requestId,
|
||||
);
|
||||
|
||||
if (frame.requestId === 0 && frame.cmd === 'runtime') {
|
||||
this.runtimeFailure = error;
|
||||
this.rejectControlPending(error);
|
||||
this.rejectRecvPending(error);
|
||||
this.rejectLoginWaiters(error);
|
||||
this.emit('runtimeError', error);
|
||||
return;
|
||||
}
|
||||
|
||||
const ack = this.pendingAcks.get(frame.requestId);
|
||||
|
||||
if (ack) {
|
||||
@@ -1363,6 +1390,10 @@ export class QqHookClient extends EventEmitter {
|
||||
}
|
||||
|
||||
private handleRecvFrame(frame: PipeFrame): void {
|
||||
if (this.runtimeFailure) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame.op === PipeOp.hello) {
|
||||
this.resolveHello(frame, true);
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user