From 0a09d765318e2cf836113be602df4ac22dd16b94 Mon Sep 17 00:00:00 2001 From: "Samuel EF. Tinnerholm" Date: Sun, 24 May 2026 19:48:14 +0300 Subject: [PATCH] fix: TS SDK ws-client + polymarket_us non-null guards + router fetch timeout Fixes #223 Fixes #281 Fixes #284 --- core/src/exchanges/polymarket_us/websocket.ts | 10 ++++++++-- sdks/typescript/pmxt/router.ts | 1 + sdks/typescript/pmxt/ws-client.ts | 10 ++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/core/src/exchanges/polymarket_us/websocket.ts b/core/src/exchanges/polymarket_us/websocket.ts index 860b41c8..04079110 100644 --- a/core/src/exchanges/polymarket_us/websocket.ts +++ b/core/src/exchanges/polymarket_us/websocket.ts @@ -75,8 +75,11 @@ export class PolymarketUSWebSocket { await this.ensureInitialized(); if (!this.bookSubscriptions.has(slug)) { + if (!this.socket) { + throw new Error('[polymarket_us] Socket not available after connect'); + } this.bookSubscriptions.add(slug); - this.socket!.subscribeMarketData(`book:${slug}`, [slug]); + this.socket.subscribeMarketData(`book:${slug}`, [slug]); } const dataPromise = new Promise((resolve, reject) => { @@ -97,8 +100,11 @@ export class PolymarketUSWebSocket { await this.ensureInitialized(); if (!this.tradeSubscriptions.has(slug)) { + if (!this.socket) { + throw new Error('[polymarket_us] Socket not available after connect'); + } this.tradeSubscriptions.add(slug); - this.socket!.subscribeTrades(`trade:${slug}`, [slug]); + this.socket.subscribeTrades(`trade:${slug}`, [slug]); } const dataPromise = new Promise((resolve, reject) => { diff --git a/sdks/typescript/pmxt/router.ts b/sdks/typescript/pmxt/router.ts index 669afebc..d7eddfec 100644 --- a/sdks/typescript/pmxt/router.ts +++ b/sdks/typescript/pmxt/router.ts @@ -297,6 +297,7 @@ export class Router extends Exchange { method: 'POST', headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() }, body: JSON.stringify({ args: [query], credentials: this.getCredentials() }), + signal: AbortSignal.timeout(30_000), }); if (!response.ok) { const body = await response.json().catch(() => ({})); diff --git a/sdks/typescript/pmxt/ws-client.ts b/sdks/typescript/pmxt/ws-client.ts index 353ec9e0..65d8974d 100644 --- a/sdks/typescript/pmxt/ws-client.ts +++ b/sdks/typescript/pmxt/ws-client.ts @@ -240,7 +240,10 @@ export class SidecarWsClient { message.credentials = credentials; } - this.ws!.send(JSON.stringify(message)); + if (!this.ws) { + throw new PmxtError('[ws-client] Cannot send: WebSocket not connected'); + } + this.ws.send(JSON.stringify(message)); return this.waitForData(requestId, timeoutMs); } @@ -278,7 +281,10 @@ export class SidecarWsClient { message.credentials = credentials; } - this.ws!.send(JSON.stringify(message)); + if (!this.ws) { + throw new PmxtError('[ws-client] Cannot send: WebSocket not connected'); + } + this.ws.send(JSON.stringify(message)); // Wait for first data event await this.waitForData(requestId, timeoutMs);