dsh-devdock 2.0.0 → 2.5.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.
@@ -99,13 +99,18 @@ export class NpmRunner {
99
99
  }
100
100
 
101
101
  if (result.code !== 0 && !options.allowFailure) {
102
- const firstLine = (result.stderr || result.stdout || '')
102
+ // npm 把 warning 打在 stderr 前几行,真正的 error(如 403 的 body)
103
+ // 在更后面。这里取「首个 error 行 + 末尾若干行」,而不是机械地取
104
+ // 前 3 行——那样只能看到 repository.url 规范化之类的 warning,把
105
+ // 真正的 403 / EOTP 原因漏掉。
106
+ const lines = (result.stderr || result.stdout || '')
103
107
  .split('\n')
104
108
  .map((line) => line.trim())
105
- .filter(Boolean)
106
- .slice(0, 3)
107
- .join(' / ');
108
- throw new NpmError('FAILED', `npm 执行失败(退出码 ${result.code}):${firstLine}`, result);
109
+ .filter(Boolean);
110
+ const firstError = lines.find((line) => /npm error|error code|EOTP|E403|E401|E402|E404/i.test(line));
111
+ const tail = lines.slice(-6).join(' / ');
112
+ const message = [firstError, tail].filter(Boolean).join(' | ');
113
+ throw new NpmError('FAILED', `npm 执行失败(退出码 ${result.code}):${message}`, result);
109
114
  }
110
115
  return result;
111
116
  }
package/src/host/store.js CHANGED
@@ -113,6 +113,42 @@ export function publicAccount(account) {
113
113
  * @param {object} raw
114
114
  * @returns {object|undefined}
115
115
  */
116
+ /**
117
+ * 归一化一条 Cloudflare 账号。
118
+ *
119
+ * 支持两种凭据形态,`authKind` 区分:
120
+ * - 'token' —— API Token(cfut_ / cfat_ 或旧 40 位字母数字),只存 token
121
+ * - 'global' —— Global API Key(cfk_ 或旧 37-45 位十六进制),存 email + token(=Key)
122
+ *
123
+ * 两种都必须填全才算有效;不完整的条目直接丢弃,避免留下「看起来存在但用不了」的账号。
124
+ *
125
+ * @param {object} raw
126
+ * @returns {object|undefined}
127
+ */
128
+ function normalizeCloudflareAccount(raw) {
129
+ if (!raw || typeof raw !== 'object') return undefined;
130
+ const authKind = raw.authKind === 'global' ? 'global' : 'token';
131
+ const token = typeof raw.token === 'string' ? raw.token.trim() : '';
132
+ const email = typeof raw.email === 'string' ? raw.email.trim() : '';
133
+ if (token === '') return undefined;
134
+ if (authKind === 'global' && email === '') return undefined;
135
+ const fallbackLogin = authKind === 'global' ? email : 'Cloudflare 账号';
136
+ const login = typeof raw.login === 'string' && raw.login.trim() !== ''
137
+ ? raw.login.trim()
138
+ : fallbackLogin;
139
+ return {
140
+ id: typeof raw.id === 'string' && raw.id !== '' ? raw.id : 'cf:' + login.toLowerCase(),
141
+ authKind,
142
+ login,
143
+ email,
144
+ token,
145
+ accountId: typeof raw.accountId === 'string' ? raw.accountId : '',
146
+ accountName: typeof raw.accountName === 'string' ? raw.accountName : '',
147
+ addedAt: typeof raw.addedAt === 'string' ? raw.addedAt : new Date(0).toISOString(),
148
+ lastUsedAt: typeof raw.lastUsedAt === 'string' ? raw.lastUsedAt : null,
149
+ };
150
+ }
151
+
116
152
  function normalizeNpmAccount(raw) {
117
153
  if (!raw || typeof raw !== 'object') return undefined;
118
154
  const login = typeof raw.login === 'string' ? raw.login.trim() : '';
@@ -145,6 +181,10 @@ function emptyState() {
145
181
  // 端点和可用动作都不同,混在一张表里会让每个消费者都要判断 provider。
146
182
  npmAccounts: [],
147
183
  activeNpmAccountId: null,
184
+ // Cloudflare 模块:令牌是 Cloudflare API Token 或 Global API Key,
185
+ // 校验端点是 api.cloudflare.com,动作是 Pages/Workers/Zones 管理。
186
+ cloudflareAccounts: [],
187
+ activeCloudflareAccountId: null,
148
188
  };
149
189
  }
150
190
 
@@ -373,6 +413,19 @@ function normalizeState(raw) {
373
413
  ? activeNpm
374
414
  : (state.npmAccounts[0]?.id ?? null);
375
415
 
416
+ // Cloudflare 账号表,与 npm 表同样:各自归一化、各自记 active。
417
+ const cfSeen = new Set();
418
+ for (const entry of Array.isArray(raw.cloudflareAccounts) ? raw.cloudflareAccounts : []) {
419
+ const account = normalizeCloudflareAccount(entry);
420
+ if (!account || cfSeen.has(account.id)) continue;
421
+ cfSeen.add(account.id);
422
+ state.cloudflareAccounts.push(account);
423
+ }
424
+ const activeCf = typeof raw.activeCloudflareAccountId === 'string' ? raw.activeCloudflareAccountId : null;
425
+ state.activeCloudflareAccountId = activeCf !== null && state.cloudflareAccounts.some((a) => a.id === activeCf)
426
+ ? activeCf
427
+ : (state.cloudflareAccounts[0]?.id ?? null);
428
+
376
429
  const active = typeof raw.activeAccountId === 'string' ? raw.activeAccountId : null;
377
430
  state.activeAccountId = active !== null && state.accounts.some((a) => a.id === active)
378
431
  ? active
@@ -562,6 +615,9 @@ export class GithubLinkStore {
562
615
  // field missing from this list is silently DROPPED on every write.
563
616
  npmAccounts: (current.npmAccounts ?? []).map((account) => ({ ...account })),
564
617
  activeNpmAccountId: current.activeNpmAccountId,
618
+ // Cloudflare 账号表同理克隆:漏掉就会在每次写入时静默丢弃。
619
+ cloudflareAccounts: (current.cloudflareAccounts ?? []).map((account) => ({ ...account })),
620
+ activeCloudflareAccountId: current.activeCloudflareAccountId,
565
621
  };
566
622
  const result = await mutator(draft);
567
623
  await this.write(draft);
@@ -604,6 +660,7 @@ export class GithubLinkStore {
604
660
  return [
605
661
  ...state.accounts.map((account) => account.token),
606
662
  ...(state.npmAccounts ?? []).map((account) => account.token),
663
+ ...(state.cloudflareAccounts ?? []).map((account) => account.token),
607
664
  ].filter((token) => typeof token === 'string' && token !== '');
608
665
  }
609
666
 
@@ -702,6 +759,138 @@ export class GithubLinkStore {
702
759
  });
703
760
  }
704
761
 
762
+ /* ---------------- Cloudflare 账号(第三张独立表) ---------------- */
763
+
764
+ /**
765
+ * Cloudflare 账号列表(不带令牌)。
766
+ * @returns {Promise<{accounts: object[], activeId: string|null}>}
767
+ */
768
+ async cloudflareAccounts() {
769
+ const state = await this.read();
770
+ return {
771
+ activeId: state.activeCloudflareAccountId,
772
+ accounts: state.cloudflareAccounts.map((account) => ({
773
+ id: account.id,
774
+ authKind: account.authKind,
775
+ login: account.login,
776
+ email: account.email,
777
+ accountId: account.accountId,
778
+ accountName: account.accountName,
779
+ addedAt: account.addedAt,
780
+ lastUsedAt: account.lastUsedAt,
781
+ })),
782
+ };
783
+ }
784
+
785
+ /**
786
+ * 当前 Cloudflare 账号(带令牌),没有则 undefined。
787
+ * @returns {Promise<object|undefined>}
788
+ */
789
+ async activeCloudflareAccount() {
790
+ const state = await this.read();
791
+ if (state.activeCloudflareAccountId === null) return undefined;
792
+ return state.cloudflareAccounts.find((account) => account.id === state.activeCloudflareAccountId);
793
+ }
794
+
795
+ /**
796
+ * 保存(或更新)一条 Cloudflare 账号,并把它设为当前。
797
+ * @param {{authKind?: string, login?: string, email?: string, token: string,
798
+ * accountId?: string, accountName?: string}} input
799
+ * @returns {Promise<object>} 不含令牌的账号
800
+ */
801
+ async saveCloudflareAccount(input) {
802
+ const account = normalizeCloudflareAccount({
803
+ ...input,
804
+ addedAt: new Date().toISOString(),
805
+ });
806
+ if (!account) throw new Error('Cloudflare 账号缺少令牌');
807
+ await this.mutate((state) => {
808
+ const index = state.cloudflareAccounts.findIndex((row) => row.id === account.id);
809
+ if (index >= 0) {
810
+ state.cloudflareAccounts[index] = { ...state.cloudflareAccounts[index], ...account };
811
+ } else {
812
+ state.cloudflareAccounts = [...state.cloudflareAccounts, account];
813
+ }
814
+ state.activeCloudflareAccountId = account.id;
815
+ return state;
816
+ });
817
+ return {
818
+ id: account.id,
819
+ authKind: account.authKind,
820
+ login: account.login,
821
+ email: account.email,
822
+ accountId: account.accountId,
823
+ accountName: account.accountName,
824
+ };
825
+ }
826
+
827
+ /**
828
+ * 删除一条 Cloudflare 账号;删掉当前账号时自动切到剩下的第一条。
829
+ * @param {string} id
830
+ * @returns {Promise<boolean>}
831
+ */
832
+ async deleteCloudflareAccount(id) {
833
+ return this.mutate((state) => {
834
+ const before = state.cloudflareAccounts.length;
835
+ state.cloudflareAccounts = state.cloudflareAccounts.filter((account) => account.id !== id);
836
+ if (state.cloudflareAccounts.length === before) return false;
837
+ if (state.activeCloudflareAccountId === id) {
838
+ state.activeCloudflareAccountId = state.cloudflareAccounts[0]?.id ?? null;
839
+ }
840
+ return true;
841
+ });
842
+ }
843
+
844
+ /**
845
+ * 切换当前 Cloudflare 账号。
846
+ * @param {string} id
847
+ * @returns {Promise<boolean>}
848
+ */
849
+ async setActiveCloudflareAccount(id) {
850
+ return this.mutate((state) => {
851
+ if (!state.cloudflareAccounts.some((account) => account.id === id)) return false;
852
+ state.activeCloudflareAccountId = id;
853
+ return true;
854
+ });
855
+ }
856
+
857
+ /**
858
+ * 更新一条 Cloudflare 账号的 accountId/accountName(登录后选了账户)。
859
+ * @param {string} id
860
+ * @param {{accountId?: string, accountName?: string}} patch
861
+ * @returns {Promise<object>} 不含令牌的账号
862
+ */
863
+ async updateCloudflareAccount(id, patch) {
864
+ return this.mutate((state) => {
865
+ const index = state.cloudflareAccounts.findIndex((account) => account.id === id);
866
+ if (index < 0) throw new Error('找不到这个 Cloudflare 账号');
867
+ if (typeof patch.accountId === 'string') state.cloudflareAccounts[index].accountId = patch.accountId;
868
+ if (typeof patch.accountName === 'string') state.cloudflareAccounts[index].accountName = patch.accountName;
869
+ const account = state.cloudflareAccounts[index];
870
+ return {
871
+ id: account.id,
872
+ authKind: account.authKind,
873
+ login: account.login,
874
+ email: account.email,
875
+ accountId: account.accountId,
876
+ accountName: account.accountName,
877
+ };
878
+ });
879
+ }
880
+
881
+ /**
882
+ * 记录一次成功使用。
883
+ * @param {string} id
884
+ * @returns {Promise<void>}
885
+ */
886
+ async touchCloudflareAccount(id) {
887
+ await this.mutate((state) => {
888
+ const index = state.cloudflareAccounts.findIndex((account) => account.id === id);
889
+ if (index >= 0) state.cloudflareAccounts[index].lastUsedAt = new Date().toISOString();
890
+ return state;
891
+ });
892
+ }
893
+
705
894
  /** Current settings (a copy — callers may not mutate the cache). */
706
895
  async settings() {
707
896
  const state = await this.read();