Files
orca/src/main/browser/execution-route-socket-duplex.ts
T
OrcaWin 2847bbe9e8 [comment audit] Consolidate completed code comment cleanup
Combine the completed comment cleanup from 319 stacked PRs into one change against `main`, so the full CI suite validates the combined result once. This replaces the previous audit stack; no additional audit work is included.

The change shortens redundant comments across 2,753 source and shell-snapshot files (25,235 net lines removed). Conflicts preserve current-main executable code. Runtime behavior, test assertions, directives, licenses, generated notices, SAFETY rationales, and compatibility markers are preserved. The audit tracker remains local, outside the repository.

Validation:
- Equivalent source/emitted AST and executable tokens across all 2,750 source files, with separate checks for embedded shell, SQL, JavaScript, CSS, and generated fixtures.
- Identical executable lines in the three shell snapshots; protected comments unchanged.
- Changed-code quality gate passed with no new findings.
- 358 tests passed, 2 skipped across 23 focused test files.
- `git diff --check` passed.
2026-10-23 13:15:45 -07:00

101 lines
2.5 KiB
TypeScript

import { Duplex } from 'node:stream'
import {
BROWSER_NETWORK_TUNNEL_CONNECT_TIMEOUT_MS,
type BrowserNetworkTunnelSocket
} from './browser-network-tunnel-stream-state'
// Wait for connect before allowing a SOCKS success reply.
export function openExecutionRouteSocketAsDuplex(
socket: BrowserNetworkTunnelSocket,
options: { connectTimeoutMs?: number } = {}
): Promise<Duplex> {
return new Promise<Duplex>((resolve, reject) => {
let settled = false
// Transports may emit buffered data on nextTick before their connect microtask.
const earlyData: Buffer[] = []
let deliver = (bytes: Buffer): void => {
earlyData.push(bytes)
}
socket.on('data', (bytes) => {
deliver(Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes))
})
const swapDelivery = (next: (bytes: Buffer) => void): Buffer[] => {
deliver = next
return earlyData
}
const fail = (error: Error): void => {
if (settled) {
return
}
settled = true
clearTimeout(timeout)
socket.destroy()
reject(error)
}
const timeout = setTimeout(
() => fail(new Error('browser_local_route_connect_timeout')),
options.connectTimeoutMs ?? BROWSER_NETWORK_TUNNEL_CONNECT_TIMEOUT_MS
)
socket.on('error', (error) => {
if (!settled) {
fail(error)
}
})
socket.on('close', () => {
if (!settled) {
fail(new Error('browser_local_route_connect_closed'))
}
})
socket.on('connect', () => {
if (settled) {
return
}
settled = true
clearTimeout(timeout)
resolve(wrapConnectedSocket(socket, swapDelivery))
})
})
}
function wrapConnectedSocket(
socket: BrowserNetworkTunnelSocket,
swapDelivery: (next: (bytes: Buffer) => void) => Buffer[]
): Duplex {
const duplex = new Duplex({
read: () => {
socket.resume()
},
write: (chunk: Buffer, _encoding, callback) => {
socket.write(chunk, () => callback())
},
final: (callback) => {
socket.end()
callback()
},
destroy: (error, callback) => {
socket.destroy()
callback(error)
}
})
const buffered = swapDelivery((bytes) => {
if (!duplex.push(bytes)) {
socket.pause()
}
})
for (const bytes of buffered.splice(0)) {
if (!duplex.push(bytes)) {
socket.pause()
}
}
socket.on('end', () => {
duplex.push(null)
})
socket.on('error', (error) => {
duplex.destroy(error)
})
socket.on('close', () => {
duplex.destroy()
})
return duplex
}