@okx_ai/okx-trade-cli 1.3.0-beta.2 → 1.3.0-beta.3
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/dist/index.js +483 -925
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/scripts/postinstall.js +111 -2
package/package.json
CHANGED
package/scripts/postinstall.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// Shared postinstall
|
|
2
|
+
// Shared postinstall script — do not edit the copies in packages/*/scripts/
|
|
3
3
|
// This file is the single source of truth; copies are generated during build.
|
|
4
4
|
|
|
5
|
-
import { readFileSync } from 'node:fs';
|
|
5
|
+
import { readFileSync, createWriteStream, mkdirSync, chmodSync, existsSync, unlinkSync } from 'node:fs';
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
7
|
import { dirname, join } from 'node:path';
|
|
8
|
+
import { homedir, platform, arch } from 'node:os';
|
|
9
|
+
import { get as httpsGet } from 'node:https';
|
|
10
|
+
import { get as httpGet } from 'node:http';
|
|
11
|
+
|
|
8
12
|
|
|
9
13
|
try {
|
|
10
14
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
@@ -18,3 +22,108 @@ try {
|
|
|
18
22
|
} catch {
|
|
19
23
|
// Silently ignore errors to avoid blocking installation
|
|
20
24
|
}
|
|
25
|
+
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
// DoH binary download (best-effort, never blocks npm install)
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
const CDN_SOURCES = [
|
|
31
|
+
{ host: 'static.okx.com', protocol: 'https' },
|
|
32
|
+
{ host: 'static.oklink.com', protocol: 'https' },
|
|
33
|
+
];
|
|
34
|
+
const CDN_PATH_PREFIX = '/upgradeapp/doh/prepub'; // TODO: 更换为正式路径
|
|
35
|
+
const DOWNLOAD_TIMEOUT_MS = 30_000;
|
|
36
|
+
const BIN_DIR = join(homedir(), '.okx', 'bin');
|
|
37
|
+
|
|
38
|
+
function getPlatformDir() {
|
|
39
|
+
const p = platform();
|
|
40
|
+
const a = arch();
|
|
41
|
+
const map = {
|
|
42
|
+
'darwin-arm64': 'darwin-arm64',
|
|
43
|
+
'darwin-x64': 'darwin-x64',
|
|
44
|
+
'linux-x64': 'linux-x64',
|
|
45
|
+
'win32-x64': 'win32-x64',
|
|
46
|
+
};
|
|
47
|
+
return map[`${p}-${a}`] ?? null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getBinaryName() {
|
|
51
|
+
return platform() === 'win32' ? 'okx-doh-resolver.exe' : 'okx-doh-resolver';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function download(url, destPath, timeoutMs) {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
let redirects = 0;
|
|
57
|
+
const maxRedirects = 5;
|
|
58
|
+
|
|
59
|
+
function doRequest(requestUrl) {
|
|
60
|
+
const reqFn = requestUrl.startsWith('https') ? httpsGet : httpGet;
|
|
61
|
+
const req = reqFn(requestUrl, { timeout: timeoutMs }, (res) => {
|
|
62
|
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
63
|
+
redirects++;
|
|
64
|
+
if (redirects > maxRedirects) {
|
|
65
|
+
reject(new Error(`Too many redirects (${maxRedirects})`));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
doRequest(res.headers.location);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (res.statusCode !== 200) {
|
|
73
|
+
reject(new Error(`HTTP ${res.statusCode}`));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const file = createWriteStream(destPath);
|
|
78
|
+
res.pipe(file);
|
|
79
|
+
file.on('finish', () => file.close(resolve));
|
|
80
|
+
file.on('error', (err) => {
|
|
81
|
+
try { unlinkSync(destPath); } catch { /* ignore */ }
|
|
82
|
+
reject(err);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
req.on('error', reject);
|
|
87
|
+
req.on('timeout', () => {
|
|
88
|
+
req.destroy();
|
|
89
|
+
reject(new Error('Download timed out'));
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
doRequest(url);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function downloadDohBinary() {
|
|
98
|
+
if (process.env.OKX_DOH_BINARY_PATH) return;
|
|
99
|
+
|
|
100
|
+
const platformDir = getPlatformDir();
|
|
101
|
+
if (!platformDir) return;
|
|
102
|
+
|
|
103
|
+
const binaryName = getBinaryName();
|
|
104
|
+
const destPath = join(BIN_DIR, binaryName);
|
|
105
|
+
|
|
106
|
+
mkdirSync(BIN_DIR, { recursive: true });
|
|
107
|
+
|
|
108
|
+
const urlPath = `${CDN_PATH_PREFIX}/${platformDir}/${binaryName}`;
|
|
109
|
+
|
|
110
|
+
for (const { host, protocol } of CDN_SOURCES) {
|
|
111
|
+
const url = `${protocol}://${host}${urlPath}`;
|
|
112
|
+
try {
|
|
113
|
+
await download(url, destPath, DOWNLOAD_TIMEOUT_MS);
|
|
114
|
+
if (platform() !== 'win32') {
|
|
115
|
+
chmodSync(destPath, 0o755);
|
|
116
|
+
}
|
|
117
|
+
process.stderr.write(` ✓ DoH resolver downloaded from ${host}\n`);
|
|
118
|
+
return;
|
|
119
|
+
} catch {
|
|
120
|
+
try { unlinkSync(destPath); } catch { /* ignore */ }
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
process.stderr.write(' ⓘ DoH resolver not available (download failed), using direct connection.\n');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
downloadDohBinary().catch(() => {
|
|
128
|
+
// Never block npm install
|
|
129
|
+
});
|