dsh-zcode-startplan 0.5.2 → 1.0.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +1963 -672
  2. package/README.md +92 -472
  3. package/bin/cli.mjs +509 -450
  4. package/client.js +2459 -1002
  5. package/cordis.patch.yml +4 -2
  6. package/index.js +109 -11
  7. package/lib/adapter.js +476 -449
  8. package/lib/anthropic-body.js +513 -469
  9. package/lib/assemble.js +808 -542
  10. package/lib/auto-claim.js +229 -0
  11. package/lib/captcha/minter.js +61 -7
  12. package/lib/distribution-keys.js +202 -0
  13. package/lib/errors.js +281 -125
  14. package/lib/fallback.js +120 -0
  15. package/lib/local-endpoint.js +367 -352
  16. package/lib/manage-routes.js +1245 -652
  17. package/lib/model-catalog.js +57 -0
  18. package/lib/openai-gateway.js +776 -0
  19. package/lib/plan-client.js +942 -532
  20. package/lib/pool.js +771 -478
  21. package/lib/sse.js +429 -317
  22. package/lib/upstream-api.js +948 -611
  23. package/lib/upstream.js +294 -134
  24. package/lib/version-probe.js +110 -0
  25. package/package.json +84 -81
  26. package/scripts/analyze-grant-intervals.mjs +171 -0
  27. package/scripts/check-compat-rc3.mjs +21 -11
  28. package/scripts/debug-claimable.mjs +130 -0
  29. package/scripts/probe-marketing.mjs +203 -0
  30. package/scripts/restart-dsh.ps1 +119 -116
  31. package/scripts/test-claimed-pending.mjs +152 -0
  32. package/scripts/verify_compat.mjs +75 -64
  33. package/scripts/watch-marketing.mjs +155 -0
  34. package/tests/auto-claim.test.mjs +318 -0
  35. package/tests/claim-routes.test.mjs +438 -293
  36. package/tests/client-panel.test.mjs +1141 -356
  37. package/tests/distribution-keys.test.mjs +123 -0
  38. package/tests/e2e.test.mjs +388 -353
  39. package/tests/fallback-scrub.test.mjs +105 -0
  40. package/tests/launch-command.test.mjs +68 -0
  41. package/tests/openai-gateway.test.mjs +454 -0
  42. package/tests/pinned-models.test.mjs +505 -497
  43. package/tests/plan-client.test.mjs +582 -222
  44. package/tests/pool.test.mjs +319 -93
  45. package/tests/quota-blackhole.test.mjs +409 -0
  46. package/tests/restart-script.test.mjs +53 -0
  47. package/tests/routing-quota.test.mjs +251 -0
  48. package/tests/support/hanging-stream.mjs +48 -0
  49. package/tests/upstream-error-200.test.mjs +164 -125
  50. package/tests/upstream.test.mjs +330 -250
  51. package/tests/version-probe.test.mjs +172 -0
@@ -0,0 +1,123 @@
1
+ /**
2
+ * 分发 key 持久化(`lib/distribution-keys.js`)。
3
+ *
4
+ * 重点回归:**0.9.6 之前落盘的 key 没有 `id`**(旧格式只存 token)——缺 id 的
5
+ * key 面板永远删不掉(`publicDistributionKey` 投影 `id: undefined`,JSON 丢键,
6
+ * CRUD 请求体没有定位键)。真实报障「已创建的 id 丢失无法删除」。
7
+ */
8
+
9
+ import test from 'node:test'
10
+ import assert from 'node:assert/strict'
11
+ import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync, mkdirSync } from 'node:fs'
12
+ import { tmpdir } from 'node:os'
13
+ import { join } from 'node:path'
14
+
15
+ import {
16
+ hardenCredentialFile,
17
+ loadDistributionKeys,
18
+ saveDistributionKeys,
19
+ createDistributionKey,
20
+ publicDistributionKey,
21
+ DISTRIBUTION_KEY_PATTERN,
22
+ } from '../lib/distribution-keys.js'
23
+
24
+ function withDir(fn) {
25
+ const dir = mkdtempSync(join(tmpdir(), 'zsp-dist-keys-'))
26
+ try {
27
+ return fn(dir)
28
+ } finally {
29
+ rmSync(dir, { recursive: true, force: true })
30
+ }
31
+ }
32
+
33
+ const legacyFile = (dir, keys) => {
34
+ mkdirSync(dir, { recursive: true })
35
+ writeFileSync(join(dir, 'distribution-keys.json'), JSON.stringify({ keys }, null, 1))
36
+ }
37
+
38
+ test('loadDistributionKeys: 0.9.6 旧格式(无 id)自动补 dk_ id,token 原样保留', () => withDir((dir) => {
39
+ legacyFile(dir, [
40
+ { token: 'zsp-iqajClF9hcsgD7iuNZmLuw', name: '未命名', quotaTotal: null, accountId: null, enabled: true, createdAt: 1, usage: { inputTokens: 0, outputTokens: 0, requests: 0 } },
41
+ ])
42
+ const keys = loadDistributionKeys(dir)
43
+ assert.equal(keys.length, 1)
44
+ assert.match(keys[0].id, /^dk_[A-Za-z0-9_-]+$/, '补上稳定 id')
45
+ assert.equal(keys[0].token, 'zsp-iqajClF9hcsgD7iuNZmLuw', 'token 不变 = 凭据不受影响')
46
+ /** 迁移后的 key 走正常投影:面板有 id 可定位。 */
47
+ const view = publicDistributionKey(keys[0])
48
+ assert.equal(typeof view.id, 'string')
49
+ assert.ok(view.id.length > 0, '投影的 id 不再是 undefined(JSON 会丢 undefined 键)')
50
+ }))
51
+
52
+ test('loadDistributionKeys: 已有 id 不动(迁移幂等,id 跨重启稳定)', () => withDir((dir) => {
53
+ legacyFile(dir, [
54
+ { id: 'dk_keepme', token: 'zsp-hh6NYcPVl9Z_fJuSxpwOag', name: 'x', quotaTotal: null, accountId: null, enabled: true, createdAt: 1, usage: { inputTokens: 0, outputTokens: 0, requests: 0 } },
55
+ ])
56
+ const first = loadDistributionKeys(dir)
57
+ assert.equal(first[0].id, 'dk_keepme')
58
+ const second = loadDistributionKeys(dir)
59
+ assert.equal(second[0].id, 'dk_keepme', '重复加载不换 id')
60
+ }))
61
+
62
+ test('loadDistributionKeys: 新格式往返 / 坏文件 / 空 key 列表', () => withDir((dir) => {
63
+ const created = createDistributionKey('给台式机', { quotaTotal: 5_000_000 })
64
+ saveDistributionKeys(dir, [created])
65
+ const roundTrip = loadDistributionKeys(dir)
66
+ assert.equal(roundTrip.length, 1)
67
+ assert.equal(roundTrip[0].id, created.id)
68
+ assert.equal(roundTrip[0].token, created.token)
69
+ assert.match(roundTrip[0].token, DISTRIBUTION_KEY_PATTERN)
70
+
71
+ /**
72
+ * ⚠ 0.10.0 语义变更:坏文件**必须抛错**,不再静默返回空表。
73
+ * 静默空表 + 下一次 save = 全部 key 被覆盖清空(用完即 429,且日志零输出)。
74
+ */
75
+ writeFileSync(join(dir, 'distribution-keys.json'), '{ 坏 JSON')
76
+ assert.throws(() => loadDistributionKeys(dir), /损坏/)
77
+ /** 备份回调被调用,且损坏文件本身不被删改(留给用户抢救)。 */
78
+ let backupOf
79
+ assert.throws(() =>
80
+ loadDistributionKeys(dir, {
81
+ backup: (file) => {
82
+ backupOf = `${file}.corrupt-test`
83
+ return backupOf
84
+ },
85
+ }),
86
+ )
87
+ assert.equal(typeof backupOf, 'string')
88
+ /** 缺少 keys 数组同样是结构损坏。 */
89
+ writeFileSync(join(dir, 'distribution-keys.json'), '{"nokeys":1}')
90
+ assert.throws(() => loadDistributionKeys(dir), /结构不对/)
91
+ /** 文件不存在 = 首次运行,仍是空表(不是损坏)。 */
92
+ assert.deepEqual(loadDistributionKeys(join(dir, '不存在')), [])
93
+ }))
94
+
95
+ test('saveDistributionKeys: 落盘后收紧权限(POSIX mode + Windows ACL 尽力而为)', () => withDir((dir) => {
96
+ saveDistributionKeys(dir, [createDistributionKey('x')])
97
+ const file = join(dir, 'distribution-keys.json')
98
+ /**
99
+ * Windows 上 `fs.mode` 恒为 666(chmod 是空操作),真正的边界是 NTFS ACL ——
100
+ * 断言「调用了收紧逻辑」,而不是断言一个在 Windows 上必然为真的 mode。
101
+ */
102
+ const report = hardenCredentialFile(file)
103
+ assert.equal(report.ok, true)
104
+ assert.ok(['posix-mode', 'icacls'].includes(report.via), `实际 via=${report.via}`)
105
+ if (process.platform !== 'win32') assert.equal(statSync(file).mode & 0o777, 0o600)
106
+ }))
107
+
108
+ test('deleteDistributionKey 流程:按 id 删除后落盘文件里真正消失', () => withDir((dir) => {
109
+ /** 模拟旧格式加载 → 迁移 → 按 id 删除 → 落盘 的完整链路(assemble 同款)。 */
110
+ legacyFile(dir, [
111
+ { token: 'zsp-old1aaaaaaaaaaaaaaaaaa', name: '旧key', quotaTotal: null, accountId: null, enabled: true, createdAt: 1, usage: { inputTokens: 1, outputTokens: 2, requests: 1 } },
112
+ { token: 'zsp-old2aaaaaaaaaaaaaaaaaa', name: '留着', quotaTotal: null, accountId: null, enabled: true, createdAt: 2, usage: { inputTokens: 0, outputTokens: 0, requests: 0 } },
113
+ ])
114
+ const keys = loadDistributionKeys(dir)
115
+ const victim = keys.find((key) => key.token === 'zsp-old1aaaaaaaaaaaaaaaaaa')
116
+ assert.notEqual(victim?.id, undefined, '旧 key 迁移后有 id 可定位')
117
+ const remaining = keys.filter((key) => key.id !== victim.id)
118
+ saveDistributionKeys(dir, remaining)
119
+ const onDisk = JSON.parse(readFileSync(join(dir, 'distribution-keys.json'), 'utf8'))
120
+ assert.equal(onDisk.keys.length, 1)
121
+ assert.equal(onDisk.keys[0].token, 'zsp-old2aaaaaaaaaaaaaaaaaa')
122
+ assert.equal(typeof onDisk.keys[0].id, 'string', '落盘把补的 id 持久化了')
123
+ }))