Improve type safety and error logging in realtime WebSocket handler#30
Conversation
Co-authored-by: skullcrushercmd <93234024+skullcrushercmd@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR tightens TypeScript types in the realtime WebSocket proxy and aims to standardize error logging by replacing ad-hoc console.error usage with the shared logError utility.
Changes:
- Introduces a
RealtimeMessageunion type and applies it to the buffered message queue. - Types
upstreamWsasNodeWebSocket | nulland removesanycasts around WS construction and event handlers. - Replaces
console.errorcalls withlogErrorfor upstream errors and usage-update failures.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| updateUserTokenUsage(total, userApiKey).catch(err => { | ||
| logError('Realtime usage update failed', { | ||
| error: err, | ||
| userApiKey, | ||
| totalTokens: total, | ||
| }); | ||
| }); |
There was a problem hiding this comment.
logError is being called with a message string as the first argument and a context object as the second, but logError’s signature is logError(error, request?). As written, the context object will be treated as the (optional) request and the actual error/context won’t be captured. Consider passing a single structured object (e.g., with message and the caught error/details) as the first argument and, if available, the WS req as the second. Also avoid logging the raw API key in details—redact or hash it before including it in any logged context.
| (upstreamWs as any).on('error', (err: any) => { | ||
| console.error('Realtime Upstream Error:', err); | ||
| upstreamWs.on('error', (err: any) => { | ||
| logError('Realtime Upstream Error', err); |
There was a problem hiding this comment.
logError('Realtime Upstream Error', err) passes the message string as the error and the actual err as the request parameter, which drops the upstream error details in logs. Update this to log the actual error (and optionally include a message via a structured error object), and make sure to handle the returned promise (await it or attach a .catch) to avoid unhandled rejections.
| logError('Realtime Upstream Error', err); | |
| logError({ message: 'Realtime Upstream Error', error: err }).catch(() => { | |
| // Swallow logging errors to avoid unhandled rejections in the WebSocket error handler | |
| }); |
Pulls in AnyScan PRs #14 through #30: - #14 bbe4cf2 fix: triage and fix the 11 baseline test failures - #15 d43d05f feat(followon): stream port-scan results into host-scan tasks - #16 0918271 feat: GraphQL endpoint and introspection discovery rules - #17 f2eb2d7 feat: dependency-manifests path profile - #18 feat: container-orchestration path profile - #19 feat: build-artifacts path profile - #20 feat: cicd-configs path profile - #21 feat: mobile-artifacts path profile - #22 feat: cloud-storage-listing path profile - #23 feat: MLOps tech fingerprints - #24 feat: modern JS framework tech fingerprints - #25 feat: verbose stack-trace disclosure detector - #26 feat: modern credential-token detectors (GH PAT v2 / JWT alg:none / Cloudflare / Datadog) - #27 feat: HTTP header-policy detectors (CORS/HSTS/CSP) - #28 e97012f9 fix(worker): reserve egress bandwidth for agentd via tc/qdisc + safety rate cap - #29 d4986a7 feat(operator): shared shell + /app/overview page (pilot) - #30 ef60f6f1 feat(worker): fetch inventory policy from control plane at agentd startup Co-authored-by: skullcmd <skullcmd@anyvm.tech>
upstreamWsandmessageBufferwere typed asany/any[], bypassing type safety, andconsole.errorwas used inconsistently instead of the importedlogError.Changes
RealtimeMessage = string | Buffer | ArrayBuffer | Uint8Arrayfor buffer entriesupstreamWs: NodeWebSocket | null,messageBuffer: RealtimeMessage[]anycasts: Eliminatednew (NodeWebSocket as any)(...)and all(upstreamWs as any).on(...)call sitesconsole.errorwithlogErrorfor upstream errors and usage update failures; usage failures now include structured context (error,userApiKey,totalTokens)Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.