@okx_ai/okx-trade-cli 1.3.2-beta.4 → 1.3.2-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okx_ai/okx-trade-cli",
3
- "version": "1.3.2-beta.4",
3
+ "version": "1.3.2-beta.5",
4
4
  "description": "OKX CLI - Command line tool for OKX exchange",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -0,0 +1,152 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * postinstall-download.js
4
+ *
5
+ * Downloads the platform-specific okx-doh-resolver binary from OKX CDN
6
+ * to ~/.okx/bin/. Two-layer CDN fallback:
7
+ * 1. static.okx.com (HTTPS, overseas)
8
+ * 2. pcdoh.qcxex.com (HTTP, domestic)
9
+ *
10
+ * This script MUST NOT block npm install — all errors are silently swallowed.
11
+ * If the binary cannot be downloaded the SDK falls back to direct connection.
12
+ */
13
+
14
+ import { createWriteStream, mkdirSync, chmodSync, existsSync, unlinkSync } from 'node:fs';
15
+ import { homedir, platform, arch } from 'node:os';
16
+ import { join } from 'node:path';
17
+ import { get as httpsGet } from 'node:https';
18
+ import { get as httpGet } from 'node:http';
19
+
20
+ // ---------------------------------------------------------------------------
21
+ // Config
22
+ // ---------------------------------------------------------------------------
23
+
24
+ const CDN_SOURCES = [
25
+ { host: 'static.okx.com', protocol: 'https' },
26
+ { host: 'pcdoh.qcxex.com', protocol: 'http' },
27
+ ];
28
+ const CDN_PATH_PREFIX = '/upgradeapp/doh/prepub';
29
+ const DOWNLOAD_TIMEOUT_MS = 30_000;
30
+ const BIN_DIR = join(homedir(), '.okx', 'bin');
31
+
32
+ // ---------------------------------------------------------------------------
33
+ // Platform detection
34
+ // ---------------------------------------------------------------------------
35
+
36
+ function getPlatformDir() {
37
+ const p = platform();
38
+ const a = arch();
39
+
40
+ const map = {
41
+ 'darwin-arm64': 'darwin-arm64',
42
+ 'darwin-x64': 'darwin-x64',
43
+ 'linux-x64': 'linux-x64',
44
+ 'win32-x64': 'win32-x64',
45
+ };
46
+
47
+ const key = `${p}-${a}`;
48
+ return map[key] ?? null;
49
+ }
50
+
51
+ function getBinaryName() {
52
+ return platform() === 'win32' ? 'okx-doh-resolver.exe' : 'okx-doh-resolver';
53
+ }
54
+
55
+ // ---------------------------------------------------------------------------
56
+ // Download helper (follows up to 5 redirects)
57
+ // ---------------------------------------------------------------------------
58
+
59
+ function download(url, destPath, timeoutMs) {
60
+ return new Promise((resolve, reject) => {
61
+ let redirects = 0;
62
+ const maxRedirects = 5;
63
+ const getter = url.startsWith('https') ? httpsGet : httpGet;
64
+
65
+ function doRequest(requestUrl) {
66
+ const reqFn = requestUrl.startsWith('https') ? httpsGet : getter;
67
+ const req = reqFn(requestUrl, { timeout: timeoutMs }, (res) => {
68
+ // Follow redirects
69
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
70
+ redirects++;
71
+ if (redirects > maxRedirects) {
72
+ reject(new Error(`Too many redirects (${maxRedirects})`));
73
+ return;
74
+ }
75
+ doRequest(res.headers.location);
76
+ return;
77
+ }
78
+
79
+ if (res.statusCode !== 200) {
80
+ reject(new Error(`HTTP ${res.statusCode}`));
81
+ return;
82
+ }
83
+
84
+ const file = createWriteStream(destPath);
85
+ res.pipe(file);
86
+ file.on('finish', () => file.close(resolve));
87
+ file.on('error', (err) => {
88
+ try { unlinkSync(destPath); } catch { /* ignore */ }
89
+ reject(err);
90
+ });
91
+ });
92
+
93
+ req.on('error', reject);
94
+ req.on('timeout', () => {
95
+ req.destroy();
96
+ reject(new Error('Download timed out'));
97
+ });
98
+ }
99
+
100
+ doRequest(url);
101
+ });
102
+ }
103
+
104
+ // ---------------------------------------------------------------------------
105
+ // Main
106
+ // ---------------------------------------------------------------------------
107
+
108
+ async function main() {
109
+ // Skip if OKX_DOH_BINARY_PATH is set (user manages their own binary)
110
+ if (process.env.OKX_DOH_BINARY_PATH) return;
111
+
112
+ const platformDir = getPlatformDir();
113
+ if (!platformDir) {
114
+ // Unsupported platform — silently skip
115
+ return;
116
+ }
117
+
118
+ const binaryName = getBinaryName();
119
+ const destPath = join(BIN_DIR, binaryName);
120
+
121
+ // Skip if binary already exists
122
+ if (existsSync(destPath)) return;
123
+
124
+ // Ensure target directory exists
125
+ mkdirSync(BIN_DIR, { recursive: true });
126
+
127
+ const urlPath = `${CDN_PATH_PREFIX}/${platformDir}/${binaryName}`;
128
+
129
+ for (const { host, protocol } of CDN_SOURCES) {
130
+ const url = `${protocol}://${host}${urlPath}`;
131
+ try {
132
+ await download(url, destPath, DOWNLOAD_TIMEOUT_MS);
133
+ // Make executable on Unix
134
+ if (platform() !== 'win32') {
135
+ chmodSync(destPath, 0o755);
136
+ }
137
+ process.stderr.write(` ✓ DoH resolver downloaded from ${host}\n`);
138
+ return;
139
+ } catch {
140
+ // Remove partial download
141
+ try { unlinkSync(destPath); } catch { /* ignore */ }
142
+ // Try next CDN source
143
+ }
144
+ }
145
+
146
+ // All CDN hosts failed — silently degrade
147
+ process.stderr.write(' ⓘ DoH resolver not available (download failed), using direct connection.\n');
148
+ }
149
+
150
+ main().catch(() => {
151
+ // Never block npm install
152
+ });
@@ -43,7 +43,7 @@ function getPlatformDir() {
43
43
  const map = {
44
44
  'darwin-arm64': 'darwin-arm64',
45
45
  'darwin-x64': 'darwin-x64',
46
- 'linux-arm64': 'linux-x64',
46
+ 'linux-arm64': 'linux-arm64',
47
47
  'linux-x64': 'linux-x64',
48
48
  'win32-arm64': 'win32-x64', // fallback: x64 binary via WoW64 emulation
49
49
  'win32-x64': 'win32-x64',
@@ -216,80 +216,3 @@ async function downloadPilotBinary() {
216
216
  downloadPilotBinary().catch(() => {
217
217
  // Never block npm install
218
218
  });
219
-
220
- // ---------------------------------------------------------------------------
221
- // okx-auth binary download (best-effort, never blocks npm install)
222
- // ---------------------------------------------------------------------------
223
-
224
- const AUTH_CDN_PATH_PREFIX = '/upgradeapp/tools/oauth';
225
-
226
- function getAuthBinaryName() {
227
- return platform() === 'win32' ? 'okx-auth.exe' : 'okx-auth';
228
- }
229
-
230
- async function downloadOkxAuthBinary() {
231
- if (process.env.OKX_AUTH_BIN) return;
232
-
233
- const platformDir = getPlatformDir();
234
- if (!platformDir) return;
235
-
236
- const binaryName = getAuthBinaryName();
237
- const destPath = join(BIN_DIR, binaryName);
238
- const tmpPath = destPath + '.auth.tmp';
239
-
240
- mkdirSync(BIN_DIR, { recursive: true });
241
-
242
- const checksumPath = `${AUTH_CDN_PATH_PREFIX}/${platformDir}/checksum.json`;
243
- const binaryPath = `${AUTH_CDN_PATH_PREFIX}/${platformDir}/${binaryName}`;
244
-
245
- for (const { host, protocol } of CDN_SOURCES) {
246
- try {
247
- const checksumUrl = `${protocol}://${host}${checksumPath}`;
248
- const raw = await downloadText(checksumUrl, DOWNLOAD_TIMEOUT_MS);
249
- const checksum = JSON.parse(raw);
250
-
251
- if (!checksum.sha256 || !checksum.size || !checksum.target) {
252
- throw new Error('Invalid checksum.json: missing sha256, size, or target');
253
- }
254
-
255
- if (checksum.target !== platformDir) {
256
- throw new Error(`Target mismatch: expected ${platformDir}, got ${checksum.target}`);
257
- }
258
-
259
- if (existsSync(destPath) && verifyBinary(destPath, checksum, platformDir)) {
260
- process.stderr.write(' ✓ okx-auth up to date (checksum match)\n');
261
- return;
262
- }
263
-
264
- const binaryUrl = `${protocol}://${host}${binaryPath}`;
265
- await download(binaryUrl, tmpPath, DOWNLOAD_TIMEOUT_MS);
266
-
267
- const actual = hashFile(tmpPath);
268
- if (actual.size !== checksum.size) {
269
- throw new Error(`Size mismatch: expected ${checksum.size}, got ${actual.size}`);
270
- }
271
- if (actual.sha256 !== checksum.sha256) {
272
- throw new Error(`SHA-256 mismatch: expected ${checksum.sha256}, got ${actual.sha256}`);
273
- }
274
-
275
- try { unlinkSync(destPath); } catch { /* ignore */ }
276
- renameSync(tmpPath, destPath);
277
-
278
- if (platform() !== 'win32') {
279
- chmodSync(destPath, 0o755);
280
- }
281
-
282
- process.stderr.write(` ✓ okx-auth downloaded and verified (${host})\n`);
283
- return;
284
- } catch (err) {
285
- try { unlinkSync(tmpPath); } catch { /* ignore */ }
286
- process.stderr.write(` [okx-auth] ${host} failed: ${err instanceof Error ? err.message : err}\n`);
287
- }
288
- }
289
-
290
- process.stderr.write(' ⓘ okx-auth not available (download failed). Set OKX_AUTH_BIN to provide a custom path, or re-run npm install to retry.\n');
291
- }
292
-
293
- downloadOkxAuthBinary().catch(() => {
294
- // Never block npm install
295
- });