From b4d6d648bd5b926ac2d391ca984f55ce7b5825b5 Mon Sep 17 00:00:00 2001 From: direkturcrypto Date: Fri, 27 Mar 2026 13:49:52 +0700 Subject: [PATCH] feat: auto-patch clob-client proxy support via postinstall script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The proxy patch in node_modules is lost on every fresh npm install. This postinstall script automatically patches @polymarket/clob-client http-helpers to inject HttpsProxyAgent for all polymarket.com requests. Runs automatically after npm install — no manual patching needed on VPS deploy. Co-Authored-By: Claude Opus 4.6 --- package.json | 3 +- scripts/patch-clob-client.cjs | 87 +++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 scripts/patch-clob-client.cjs diff --git a/package.json b/package.json index 0d7238c..ef6b850 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "sniper-dev": "DRY_RUN=true nodemon --ignore 'data/*.json' src/sniper.js", "sniper-tui": "DRY_RUN=false node src/sniper-tui.js", "sniper-tui-sim": "DRY_RUN=true node src/sniper-tui.js", - "sniper-tui-dev": "DRY_RUN=true nodemon --ignore 'data/*.json' src/sniper-tui.js" + "sniper-tui-dev": "DRY_RUN=true nodemon --ignore 'data/*.json' src/sniper-tui.js", + "postinstall": "node scripts/patch-clob-client.cjs" }, "keywords": [ "polymarket", diff --git a/scripts/patch-clob-client.cjs b/scripts/patch-clob-client.cjs new file mode 100644 index 0000000..4d66dc3 --- /dev/null +++ b/scripts/patch-clob-client.cjs @@ -0,0 +1,87 @@ +/** + * patch-clob-client.cjs + * + * Patches @polymarket/clob-client to inject proxy support. + * Runs automatically via `npm install` (postinstall hook). + * + * What it does: + * - Adds HttpsProxyAgent import to http-helpers/index.js + * - Injects proxy agent into every axios request to polymarket.com + * - Reads PROXY_URL from process.env at runtime + */ + +const fs = require('fs'); +const path = require('path'); + +const TARGET = path.join( + __dirname, + '..', + 'node_modules', + '@polymarket', + 'clob-client', + 'dist', + 'http-helpers', + 'index.js', +); + +if (!fs.existsSync(TARGET)) { + console.log('[patch] @polymarket/clob-client not found — skipping'); + process.exit(0); +} + +let code = fs.readFileSync(TARGET, 'utf8'); + +// Check if already patched +if (code.includes('getProxyAgent')) { + console.log('[patch] @polymarket/clob-client already patched — skipping'); + process.exit(0); +} + +// Find the line after the axios import to inject proxy code +const AXIOS_IMPORT = `require("axios")`; +if (!code.includes(AXIOS_IMPORT)) { + console.error('[patch] Could not find axios import in http-helpers — skipping'); + process.exit(0); +} + +// Inject proxy imports and helper after axios import line +const PROXY_CODE = ` +// Proxy support for Polymarket API (auto-patched by scripts/patch-clob-client.cjs) +const https_proxy_agent_1 = require("https-proxy-agent"); +const getProxyAgent = () => { + if (process.env.PROXY_URL) { + return new https_proxy_agent_1.HttpsProxyAgent(process.env.PROXY_URL); + } + return undefined; +};`; + +// Insert after the browser_or_node require line +const INSERT_AFTER = `require("browser-or-node");`; +if (!code.includes(INSERT_AFTER)) { + console.error('[patch] Could not find browser-or-node import — skipping'); + process.exit(0); +} + +code = code.replace(INSERT_AFTER, INSERT_AFTER + PROXY_CODE); + +// Patch the request function to inject proxy agent +const REQUEST_CONFIG = `const config = { method, url: endpoint, headers, data, params };`; +const PATCHED_CONFIG = `const config = { method, url: endpoint, headers, data, params }; + // Add proxy agent for Polymarket API + if (endpoint && endpoint.includes('polymarket.com')) { + const agent = getProxyAgent(); + if (agent) { + config.httpsAgent = agent; + config.proxy = false; + } + }`; + +if (!code.includes(REQUEST_CONFIG)) { + console.error('[patch] Could not find request config line — skipping'); + process.exit(0); +} + +code = code.replace(REQUEST_CONFIG, PATCHED_CONFIG); + +fs.writeFileSync(TARGET, code, 'utf8'); +console.log('[patch] @polymarket/clob-client patched with proxy support ✅');