chore(qa): freeze independent apikey sample Y after X connection guard
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import net from 'node:net';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import { boundedGptPreflight } from './payload.mjs';
|
||||
export async function inspectorClosed() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = net.createConnection({ host: '127.0.0.1', port: 9229 }); socket.setTimeout(1500);
|
||||
socket.once('connect', () => { socket.destroy(); reject(Error('Inspector already open.')); });
|
||||
socket.once('error', e => { socket.destroy(); e.code === 'ECONNREFUSED' ? resolve(true) : reject(e); });
|
||||
socket.once('timeout', () => { socket.destroy(); reject(Error('Inspector state unknown.')); });
|
||||
});
|
||||
}
|
||||
class Protocol {
|
||||
constructor(socket) {
|
||||
this.socket = socket; this.nextId = 1; this.pending = new Map();
|
||||
socket.addEventListener('message', event => {
|
||||
const m = JSON.parse(String(event.data)); if (!m.id) return;
|
||||
const p = this.pending.get(m.id); if (!p) return; this.pending.delete(m.id); clearTimeout(p.timer);
|
||||
m.error ? p.reject(Error(m.error.message)) : p.resolve(m.result);
|
||||
});
|
||||
socket.addEventListener('close', () => { for (const p of this.pending.values()) { clearTimeout(p.timer); p.reject(Error('Inspector closed; outcome unknown.')); } this.pending.clear(); });
|
||||
}
|
||||
send(method, params, timeout = 10000) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = this.nextId++, timer = setTimeout(() => { this.pending.delete(id); reject(Error('Inspector timeout; do not replay.')); }, timeout);
|
||||
this.pending.set(id, { resolve, reject, timer }); this.socket.send(JSON.stringify({ id, method, params }));
|
||||
});
|
||||
}
|
||||
}
|
||||
export async function invokeGptProbe(input) {
|
||||
await inspectorClosed(); let socket, protocol, enabled = false;
|
||||
try {
|
||||
process._debugProcess(input.pid); enabled = true;
|
||||
let targets;
|
||||
for (let i = 0; i < 40 && !targets; i++) {
|
||||
await delay(100);
|
||||
try { targets = await (await fetch('http://127.0.0.1:9229/json/list', { signal: AbortSignal.timeout(700) })).json(); } catch {}
|
||||
}
|
||||
assert.equal(targets?.length, 1);
|
||||
assert.ok(targets[0].webSocketDebuggerUrl.startsWith('ws://127.0.0.1:9229/'));
|
||||
socket = new WebSocket(targets[0].webSocketDebuggerUrl);
|
||||
await new Promise((resolve, reject) => { socket.addEventListener('open', resolve, { once: true }); socket.addEventListener('error', reject, { once: true }); });
|
||||
protocol = new Protocol(socket);
|
||||
const identity = await protocol.send('Runtime.evaluate', { expression: 'process.pid', returnByValue: true });
|
||||
assert.equal(identity.result.value, input.pid);
|
||||
const expression = '(' + boundedGptPreflight.toString() + ')(' + JSON.stringify(input) + ')';
|
||||
const result = await protocol.send('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true }, 345000);
|
||||
if (result.exceptionDetails) throw Error(result.exceptionDetails.exception?.description ?? result.exceptionDetails.text);
|
||||
return { result: result.result.value, expression };
|
||||
} finally {
|
||||
if (protocol && enabled) {
|
||||
await protocol.send('Runtime.evaluate', { expression: "setTimeout(() => process.getBuiltinModule('inspector').close(), 25); 'closing'", returnByValue: true });
|
||||
await delay(200);
|
||||
}
|
||||
socket?.close();
|
||||
await inspectorClosed();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user