From e9e5d66ac9b87fc41205732bec2dcd320897ffa0 Mon Sep 17 00:00:00 2001 From: direkturcrypto Date: Sat, 28 Mar 2026 12:16:46 +0700 Subject: [PATCH] fix: patch CLOB client JSON.stringify to handle circular proxy agent The CLOB client errorHandling function does JSON.stringify on err.response.config which includes httpsAgent (HttpsProxyAgent with circular references). Inject a safe serializer that strips agent properties before serializing. Co-Authored-By: Claude Opus 4.6 --- scripts/patch-clob-client.cjs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scripts/patch-clob-client.cjs b/scripts/patch-clob-client.cjs index e984e66..1458a5c 100644 --- a/scripts/patch-clob-client.cjs +++ b/scripts/patch-clob-client.cjs @@ -66,6 +66,11 @@ axios_1.default.interceptors.request.use(function(cfg) { } return cfg; }); +// Safe JSON.stringify that strips circular agent refs +const _safeStringify = (obj) => JSON.stringify(obj, (key, val) => { + if (key === 'httpsAgent' || key === 'httpAgent' || key === 'agent') return undefined; + return val; +}); // ── End proxy patch ──────────────────────────────────────────────────────── `; @@ -95,5 +100,13 @@ if (!injected) { process.exit(0); } +// ── Patch errorHandling to avoid circular JSON serialization ──────────────── +// The CLOB client does JSON.stringify(err.response.config) which includes +// httpsAgent with circular references. Replace with safe serializer. +const jsonCount = (code.match(/JSON\.stringify\(/g) || []).length; +code = code.replace(/JSON\.stringify\(/g, '_safeStringify('); +const patchedCount = (code.match(/_safeStringify\(/g) || []).length; +console.log(`[patch] Replaced ${jsonCount} JSON.stringify calls with safe serializer`); + fs.writeFileSync(TARGET, code, 'utf8'); console.log('[patch] @polymarket/clob-client patched with proxy support ✅');