@esign-ai/esign-cli 0.0.0-stage → 1.0.0
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/README.md +44 -2
- package/bin/esign.js +76 -0
- package/install.js +271 -0
- package/package.json +25 -4
package/README.md
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# esign-cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
e签宝命令行工具(public 版 `esign`)。
|
|
4
|
+
|
|
5
|
+
本包不内嵌二进制:安装时自动检测操作系统与架构,从官方 CDN 下载对应的可执行文件并校验 SHA256。
|
|
6
|
+
`checksums.txt` 必须可获取且包含当前平台条目;校验失败时安装会直接失败。
|
|
7
|
+
|
|
8
|
+
## 安装
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install -g @esign-ai/esign-cli
|
|
12
|
+
esign --version
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
需要 Node >= 14。安装完成后提供 `esign` 与 `esign-cli` 两个等价命令。
|
|
16
|
+
|
|
17
|
+
## 更新
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm update -g @esign-ai/esign-cli
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
请通过 npm 更新,不要使用 CLI 内置的自更新能力,以免与 npm 管理的版本不一致。
|
|
24
|
+
|
|
25
|
+
## 环境变量
|
|
26
|
+
|
|
27
|
+
| 变量 | 说明 |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `ESIGN_CLI_BASE_URL` | 覆盖默认下载源 `https://asset.esign.cn/esign-cli`(内网镜像或本地调试;覆盖值允许 http) |
|
|
30
|
+
| `HTTPS_PROXY` / `https_proxy` / `HTTP_PROXY` / `http_proxy` | 设置后下载改走 curl 并遵循代理 |
|
|
31
|
+
|
|
32
|
+
## 已知限制
|
|
33
|
+
|
|
34
|
+
- 运行 `esign` 命令需要 Node 运行时(由本包的启动器调用二进制)。
|
|
35
|
+
- `npm install --ignore-scripts` 会跳过二进制下载;此时执行 `node <包目录>/install.js` 补下,或去掉该参数重装。
|
|
36
|
+
- pnpm 只读包目录场景会自动回退下载到 `~/.esign/npm/`。
|
|
37
|
+
- yarn 2+(Plug'n'Play)未验证,建议使用 npm。
|
|
38
|
+
|
|
39
|
+
## 支持的平台
|
|
40
|
+
|
|
41
|
+
| 系统 | 架构 |
|
|
42
|
+
|---|---|
|
|
43
|
+
| Linux | amd64、arm64 |
|
|
44
|
+
| macOS | amd64(Intel)、arm64(Apple Silicon) |
|
|
45
|
+
| Windows | amd64、arm64 |
|
package/bin/esign.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const { spawn } = require('child_process');
|
|
7
|
+
|
|
8
|
+
const IS_WIN = process.platform === 'win32';
|
|
9
|
+
const BINARY_NAME = IS_WIN ? 'esign.exe' : 'esign';
|
|
10
|
+
|
|
11
|
+
function resolveBinary() {
|
|
12
|
+
const localPath = path.join(__dirname, '..', 'binaries', BINARY_NAME);
|
|
13
|
+
if (fs.existsSync(localPath)) {
|
|
14
|
+
return localPath;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const locationFile = path.join(__dirname, '..', 'binaries', '.location.json');
|
|
18
|
+
if (fs.existsSync(locationFile)) {
|
|
19
|
+
try {
|
|
20
|
+
const recorded = JSON.parse(fs.readFileSync(locationFile, 'utf8'));
|
|
21
|
+
if (recorded.path && fs.existsSync(recorded.path)) {
|
|
22
|
+
return recorded.path;
|
|
23
|
+
}
|
|
24
|
+
} catch (e) {
|
|
25
|
+
// .location.json 损坏时继续走报错分支
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function signalExitCode(signal) {
|
|
33
|
+
const map = { SIGHUP: 1, SIGINT: 2, SIGQUIT: 3, SIGTERM: 15 };
|
|
34
|
+
return 128 + (map[signal] || 0);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function main() {
|
|
38
|
+
const binPath = resolveBinary();
|
|
39
|
+
if (!binPath) {
|
|
40
|
+
console.error('[esign-cli] 未找到 esign 二进制文件。');
|
|
41
|
+
console.error('[esign-cli] 常见原因:安装时跳过了脚本(npm install --ignore-scripts、pnpm 默认配置等)。');
|
|
42
|
+
console.error('[esign-cli] 修复方式(任选其一):');
|
|
43
|
+
console.error('[esign-cli] 1. 重新安装且允许脚本: npm install -g @esign-ai/esign-cli');
|
|
44
|
+
console.error(`[esign-cli] 2. 手动补下二进制: node ${path.join(__dirname, 'install.js')}`);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const child = spawn(binPath, process.argv.slice(2), {
|
|
49
|
+
stdio: 'inherit',
|
|
50
|
+
windowsHide: false,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
child.on('error', (err) => {
|
|
54
|
+
console.error(`[esign-cli] 启动二进制失败: ${err.message}`);
|
|
55
|
+
process.exit(1);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
child.on('exit', (code, signal) => {
|
|
59
|
+
if (signal) {
|
|
60
|
+
process.exit(signalExitCode(signal));
|
|
61
|
+
}
|
|
62
|
+
process.exit(code === null ? 1 : code);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
['SIGINT', 'SIGTERM'].forEach((sig) => {
|
|
66
|
+
process.on(sig, () => {
|
|
67
|
+
try {
|
|
68
|
+
child.kill(sig);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
// 子进程已退出时忽略
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
main();
|
package/install.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const http = require('http');
|
|
7
|
+
const https = require('https');
|
|
8
|
+
const crypto = require('crypto');
|
|
9
|
+
const { spawnSync } = require('child_process');
|
|
10
|
+
|
|
11
|
+
const DEFAULT_BASE_URL = 'https://asset.esign.cn/esign-cli';
|
|
12
|
+
|
|
13
|
+
function log(msg) {
|
|
14
|
+
console.log(`[esign-cli] ${msg}`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function warn(msg) {
|
|
18
|
+
console.warn(`[esign-cli] WARN: ${msg}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function fail(msg) {
|
|
22
|
+
console.error(`[esign-cli] ${msg}`);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function mapPlatform(platform, arch) {
|
|
27
|
+
const osMap = { darwin: 'darwin', linux: 'linux', win32: 'windows' };
|
|
28
|
+
const archMap = { x64: 'amd64', arm64: 'arm64' };
|
|
29
|
+
if (!osMap[platform]) {
|
|
30
|
+
throw new Error(`不支持的操作系统: ${platform}(支持 darwin / linux / win32)`);
|
|
31
|
+
}
|
|
32
|
+
if (!archMap[arch]) {
|
|
33
|
+
throw new Error(`不支持的 CPU 架构: ${arch}(支持 x64 / arm64)`);
|
|
34
|
+
}
|
|
35
|
+
return { os: osMap[platform], arch: archMap[arch], ext: platform === 'win32' ? '.exe' : '' };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function cdnVersion(npmVersion) {
|
|
39
|
+
return npmVersion;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function buildAssetNames(npmVersion, target) {
|
|
43
|
+
return {
|
|
44
|
+
cdnName: `esign-cli-${cdnVersion(npmVersion)}-${target.os}-${target.arch}${target.ext}`,
|
|
45
|
+
localName: target.ext === '.exe' ? 'esign.exe' : 'esign',
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function buildUrl(base, npmVersion, name) {
|
|
50
|
+
return `${base.replace(/\/+$/, '')}/${cdnVersion(npmVersion)}/${name}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function parseChecksums(text, filename) {
|
|
54
|
+
for (const line of String(text).split('\n')) {
|
|
55
|
+
const parts = line.trim().split(/\s+/);
|
|
56
|
+
if (parts.length >= 2 && parts[0] && parts[1] === filename) {
|
|
57
|
+
return parts[0];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function resolveBaseUrl() {
|
|
64
|
+
const override = process.env.ESIGN_CLI_BASE_URL;
|
|
65
|
+
if (!override) {
|
|
66
|
+
return DEFAULT_BASE_URL;
|
|
67
|
+
}
|
|
68
|
+
if (!/^https?:\/\//.test(override)) {
|
|
69
|
+
throw new Error(`ESIGN_CLI_BASE_URL 必须以 http:// 或 https:// 开头: ${override}`);
|
|
70
|
+
}
|
|
71
|
+
return override;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function hasProxyEnv() {
|
|
75
|
+
return Boolean(
|
|
76
|
+
process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Node http/https 分支:跟随重定向;重定向到 http 仅在显式覆盖 base URL(测试/镜像)时允许
|
|
81
|
+
function nodeFetch(url, { allowHttp, redirects = 0 }) {
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
if (redirects > 5) {
|
|
84
|
+
return reject(new Error(`重定向次数过多: ${url}`));
|
|
85
|
+
}
|
|
86
|
+
const mod = url.startsWith('http:') ? http : https;
|
|
87
|
+
const req = mod.get(url, (res) => {
|
|
88
|
+
const status = res.statusCode || 0;
|
|
89
|
+
if (status >= 300 && status < 400 && res.headers.location) {
|
|
90
|
+
res.resume();
|
|
91
|
+
const next = new URL(res.headers.location, url).toString();
|
|
92
|
+
if (next.startsWith('http:') && !allowHttp) {
|
|
93
|
+
return reject(new Error(`拒绝跟随 http 重定向: ${next}`));
|
|
94
|
+
}
|
|
95
|
+
return nodeFetch(next, { allowHttp, redirects: redirects + 1 }).then(resolve, reject);
|
|
96
|
+
}
|
|
97
|
+
if (status !== 200) {
|
|
98
|
+
res.resume();
|
|
99
|
+
return reject(new Error(`HTTP ${status}: ${url}`));
|
|
100
|
+
}
|
|
101
|
+
const chunks = [];
|
|
102
|
+
let size = 0;
|
|
103
|
+
res.on('data', (chunk) => {
|
|
104
|
+
size += chunk.length;
|
|
105
|
+
if (size > 512 * 1024 * 1024) {
|
|
106
|
+
req.destroy(new Error('响应超过 512MB 上限'));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
chunks.push(chunk);
|
|
110
|
+
});
|
|
111
|
+
res.on('end', () => resolve(Buffer.concat(chunks)));
|
|
112
|
+
res.on('error', reject);
|
|
113
|
+
});
|
|
114
|
+
req.on('error', reject);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 代理场景交给 curl:由其原生处理 *_proxy 与 no_proxy 环境变量(macOS/Linux/Win10+ 自带)
|
|
119
|
+
function curlFetch(url) {
|
|
120
|
+
const result = spawnSync('curl', ['-fsSL', url], { encoding: 'buffer', maxBuffer: 512 * 1024 * 1024 });
|
|
121
|
+
if (result.error) {
|
|
122
|
+
throw new Error(`无法调用 curl: ${result.error.message}`);
|
|
123
|
+
}
|
|
124
|
+
if (result.status !== 0) {
|
|
125
|
+
throw new Error(`curl 退出码 ${result.status}: ${url}`);
|
|
126
|
+
}
|
|
127
|
+
return result.stdout;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function fetchBuffer(url, allowHttp) {
|
|
131
|
+
return hasProxyEnv() ? curlFetch(url) : nodeFetch(url, { allowHttp });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function sha256File(file) {
|
|
135
|
+
return new Promise((resolve, reject) => {
|
|
136
|
+
const hash = crypto.createHash('sha256');
|
|
137
|
+
fs.createReadStream(file)
|
|
138
|
+
.on('data', (chunk) => hash.update(chunk))
|
|
139
|
+
.on('end', () => resolve(hash.digest('hex')))
|
|
140
|
+
.on('error', reject);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function recordFallbackLocation(destPath) {
|
|
145
|
+
try {
|
|
146
|
+
const dir = path.join(__dirname, 'binaries');
|
|
147
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
148
|
+
fs.writeFileSync(path.join(dir, '.location.json'), `${JSON.stringify({ path: destPath }, null, 2)}\n`);
|
|
149
|
+
} catch (e) {
|
|
150
|
+
// 包目录只读(pnpm store):路径已打印在安装日志中
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function fetchExpectedHash(checksumsUrl, allowHttp, cdnName) {
|
|
155
|
+
let text;
|
|
156
|
+
try {
|
|
157
|
+
text = (await fetchBuffer(checksumsUrl, allowHttp)).toString('utf8');
|
|
158
|
+
} catch (e) {
|
|
159
|
+
fail(`无法获取 checksums.txt: ${e.message}`);
|
|
160
|
+
}
|
|
161
|
+
const hash = parseChecksums(text, cdnName);
|
|
162
|
+
if (!hash) fail(`checksums.txt 中没有 ${cdnName} 的条目`);
|
|
163
|
+
return hash;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function main() {
|
|
167
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
|
|
168
|
+
if (pkg.version === '0.0.0') {
|
|
169
|
+
fail('版本为占位值 0.0.0,拒绝下载。请在发布产物中运行本脚本,或先用 scripts/release-npm.sh 生成 staging 包');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let target;
|
|
173
|
+
try {
|
|
174
|
+
target = mapPlatform(process.platform, process.arch);
|
|
175
|
+
} catch (e) {
|
|
176
|
+
fail(e.message);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
let base;
|
|
180
|
+
try {
|
|
181
|
+
base = resolveBaseUrl();
|
|
182
|
+
} catch (e) {
|
|
183
|
+
fail(e.message);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const allowHttp = base.startsWith('http://');
|
|
187
|
+
const { cdnName, localName } = buildAssetNames(pkg.version, target);
|
|
188
|
+
const binaryUrl = buildUrl(base, pkg.version, cdnName);
|
|
189
|
+
const checksumsUrl = buildUrl(base, pkg.version, 'checksums.txt');
|
|
190
|
+
|
|
191
|
+
const primaryDir = path.join(__dirname, 'binaries');
|
|
192
|
+
let destDir = primaryDir;
|
|
193
|
+
let usedFallback = false;
|
|
194
|
+
try {
|
|
195
|
+
fs.mkdirSync(primaryDir, { recursive: true });
|
|
196
|
+
fs.accessSync(primaryDir, fs.constants.W_OK);
|
|
197
|
+
} catch (e) {
|
|
198
|
+
destDir = path.join(os.homedir(), '.esign', 'npm', `${pkg.version}-${target.os}-${target.arch}`);
|
|
199
|
+
fs.mkdirSync(destDir, { recursive: true });
|
|
200
|
+
usedFallback = true;
|
|
201
|
+
warn(`包目录不可写(${e.code || e.message}),二进制回退安装到: ${destDir}`);
|
|
202
|
+
}
|
|
203
|
+
const destPath = path.join(destDir, localName);
|
|
204
|
+
|
|
205
|
+
// 幂等短路:二进制已存在时仅重新校验,不再下载
|
|
206
|
+
if (fs.existsSync(destPath)) {
|
|
207
|
+
const expected = await fetchExpectedHash(checksumsUrl, allowHttp, cdnName);
|
|
208
|
+
const actual = await sha256File(destPath);
|
|
209
|
+
if (actual !== expected) {
|
|
210
|
+
fail(`已存在的二进制 SHA256 校验失败: ${destPath},请删除该文件后重新安装`);
|
|
211
|
+
}
|
|
212
|
+
log(`二进制已存在且校验通过,跳过下载: ${destPath}`);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const tmpPath = path.join(destDir, '.download.tmp');
|
|
217
|
+
log(`下载 ${binaryUrl}`);
|
|
218
|
+
try {
|
|
219
|
+
fs.writeFileSync(tmpPath, await fetchBuffer(binaryUrl, allowHttp));
|
|
220
|
+
} catch (e) {
|
|
221
|
+
try {
|
|
222
|
+
fs.unlinkSync(tmpPath);
|
|
223
|
+
} catch (_) {
|
|
224
|
+
// 临时文件可能未创建
|
|
225
|
+
}
|
|
226
|
+
fail(`下载失败: ${e.message}`);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const expected = await fetchExpectedHash(checksumsUrl, allowHttp, cdnName);
|
|
230
|
+
if (!expected) {
|
|
231
|
+
try {
|
|
232
|
+
fs.unlinkSync(tmpPath);
|
|
233
|
+
} catch (_) {
|
|
234
|
+
// 忽略清理失败
|
|
235
|
+
}
|
|
236
|
+
fail(`checksums.txt 中没有 ${cdnName} 的条目`);
|
|
237
|
+
}
|
|
238
|
+
const actual = await sha256File(tmpPath);
|
|
239
|
+
if (actual !== expected) {
|
|
240
|
+
try {
|
|
241
|
+
fs.unlinkSync(tmpPath);
|
|
242
|
+
} catch (_) {
|
|
243
|
+
// 忽略清理失败
|
|
244
|
+
}
|
|
245
|
+
fail(`SHA256 校验失败: 期望 ${expected},实际 ${actual}`);
|
|
246
|
+
}
|
|
247
|
+
log('SHA256 校验通过');
|
|
248
|
+
|
|
249
|
+
fs.renameSync(tmpPath, destPath);
|
|
250
|
+
if (process.platform !== 'win32') {
|
|
251
|
+
fs.chmodSync(destPath, 0o755);
|
|
252
|
+
}
|
|
253
|
+
log(`安装完成: ${destPath}`);
|
|
254
|
+
|
|
255
|
+
if (usedFallback) {
|
|
256
|
+
recordFallbackLocation(destPath);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (require.main === module) {
|
|
261
|
+
main().catch((e) => fail(e.message));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
module.exports = {
|
|
265
|
+
mapPlatform,
|
|
266
|
+
cdnVersion,
|
|
267
|
+
buildAssetNames,
|
|
268
|
+
buildUrl,
|
|
269
|
+
parseChecksums,
|
|
270
|
+
resolveBaseUrl,
|
|
271
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esign-ai/esign-cli",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "e签宝命令行工具(安装时自动下载对应平台二进制)",
|
|
5
|
+
"bin": {
|
|
6
|
+
"esign": "bin/esign.js",
|
|
7
|
+
"esign-cli": "bin/esign.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"postinstall": "node install.js",
|
|
11
|
+
"test": "node --test install.test.js",
|
|
12
|
+
"prepublishOnly": "node -e \"if(require('./package.json').version==='0.0.0'){console.error('refusing to publish placeholder version 0.0.0');process.exit(1)}\""
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"bin/",
|
|
16
|
+
"install.js"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=14"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/esign/esign-cli.git",
|
|
24
|
+
"directory": "packaging/npm"
|
|
25
|
+
},
|
|
26
|
+
"private": false
|
|
27
|
+
}
|