dsh-crate 0.0.0-stage → 0.5.1

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 (132) hide show
  1. package/LICENSE +27 -0
  2. package/README.md +288 -2
  3. package/README.zh.md +263 -0
  4. package/UPDATE-API-V1.md +207 -0
  5. package/assets/logo.svg +37 -0
  6. package/client/client.js +18964 -0
  7. package/cordis.patch.yml +4 -0
  8. package/lib/accelerate.js +194 -0
  9. package/lib/agents.js +67 -0
  10. package/lib/backup.js +688 -0
  11. package/lib/body-progress.js +173 -0
  12. package/lib/catalog-local-match.js +160 -0
  13. package/lib/catalog-npm.js +180 -0
  14. package/lib/changelog.js +462 -0
  15. package/lib/channels.js +64 -0
  16. package/lib/check.js +1557 -0
  17. package/lib/compatibility.js +265 -0
  18. package/lib/crate-catalog.js +179 -0
  19. package/lib/diagnostics.js +49 -0
  20. package/lib/discovery-compatibility.js +476 -0
  21. package/lib/drain-journal.js +164 -0
  22. package/lib/drain.js +1043 -0
  23. package/lib/dsh-cli.js +1373 -0
  24. package/lib/dsh-install.js +259 -0
  25. package/lib/entry-identity.js +85 -0
  26. package/lib/gist.js +369 -0
  27. package/lib/groups.js +108 -0
  28. package/lib/home-paths.js +29 -0
  29. package/lib/host-recovery.js +187 -0
  30. package/lib/hot.js +918 -0
  31. package/lib/http.js +252 -0
  32. package/lib/index.js +394 -0
  33. package/lib/install.js +1147 -0
  34. package/lib/lifecycle.js +37 -0
  35. package/lib/log-redaction.js +24 -0
  36. package/lib/log.js +204 -0
  37. package/lib/ndjson.js +154 -0
  38. package/lib/net.js +174 -0
  39. package/lib/official-desktop.js +619 -0
  40. package/lib/order.js +284 -0
  41. package/lib/patch.js +943 -0
  42. package/lib/pending-rows.js +142 -0
  43. package/lib/pending-updates.js +346 -0
  44. package/lib/pnpm-compat.js +972 -0
  45. package/lib/presets.js +282 -0
  46. package/lib/probe-context.js +20 -0
  47. package/lib/proc-text.js +69 -0
  48. package/lib/profile.js +1989 -0
  49. package/lib/recovery.js +1059 -0
  50. package/lib/region-probe.js +93 -0
  51. package/lib/regions.js +283 -0
  52. package/lib/registry.js +308 -0
  53. package/lib/release-candidates.js +115 -0
  54. package/lib/release-updates.js +70 -0
  55. package/lib/restart.js +688 -0
  56. package/lib/routes.js +7960 -0
  57. package/lib/settings.js +164 -0
  58. package/lib/shelfry-catalog.js +179 -0
  59. package/lib/snapshot.js +506 -0
  60. package/lib/source-migration.js +164 -0
  61. package/lib/sources.js +1005 -0
  62. package/lib/state.js +38 -0
  63. package/lib/store.js +163 -0
  64. package/lib/themes.js +350 -0
  65. package/lib/trial.js +125 -0
  66. package/lib/types/accelerate.d.ts +85 -0
  67. package/lib/types/agents.d.ts +52 -0
  68. package/lib/types/backup.d.ts +171 -0
  69. package/lib/types/body-progress.d.ts +94 -0
  70. package/lib/types/catalog-local-match.d.ts +39 -0
  71. package/lib/types/catalog-npm.d.ts +77 -0
  72. package/lib/types/changelog.d.ts +131 -0
  73. package/lib/types/channels.d.ts +56 -0
  74. package/lib/types/check.d.ts +381 -0
  75. package/lib/types/compatibility.d.ts +83 -0
  76. package/lib/types/crate-catalog.d.ts +48 -0
  77. package/lib/types/diagnostics.d.ts +29 -0
  78. package/lib/types/discovery-compatibility.d.ts +145 -0
  79. package/lib/types/drain-journal.d.ts +25 -0
  80. package/lib/types/drain.d.ts +306 -0
  81. package/lib/types/dsh-cli.d.ts +481 -0
  82. package/lib/types/dsh-install.d.ts +106 -0
  83. package/lib/types/entry-identity.d.ts +68 -0
  84. package/lib/types/gist.d.ts +79 -0
  85. package/lib/types/groups.d.ts +34 -0
  86. package/lib/types/home-paths.d.ts +13 -0
  87. package/lib/types/host-recovery.d.ts +95 -0
  88. package/lib/types/hot.d.ts +434 -0
  89. package/lib/types/http.d.ts +114 -0
  90. package/lib/types/index.d.ts +52 -0
  91. package/lib/types/install.d.ts +463 -0
  92. package/lib/types/lifecycle.d.ts +14 -0
  93. package/lib/types/log-redaction.d.ts +2 -0
  94. package/lib/types/log.d.ts +43 -0
  95. package/lib/types/ndjson.d.ts +52 -0
  96. package/lib/types/net.d.ts +94 -0
  97. package/lib/types/official-desktop.d.ts +59 -0
  98. package/lib/types/order.d.ts +100 -0
  99. package/lib/types/patch.d.ts +218 -0
  100. package/lib/types/pending-rows.d.ts +81 -0
  101. package/lib/types/pending-updates.d.ts +161 -0
  102. package/lib/types/pnpm-compat.d.ts +102 -0
  103. package/lib/types/presets.d.ts +94 -0
  104. package/lib/types/probe-context.d.ts +14 -0
  105. package/lib/types/proc-text.d.ts +29 -0
  106. package/lib/types/profile.d.ts +551 -0
  107. package/lib/types/recovery.d.ts +342 -0
  108. package/lib/types/region-probe.d.ts +50 -0
  109. package/lib/types/regions.d.ts +134 -0
  110. package/lib/types/registry.d.ts +160 -0
  111. package/lib/types/release-candidates.d.ts +42 -0
  112. package/lib/types/release-updates.d.ts +15 -0
  113. package/lib/types/restart.d.ts +279 -0
  114. package/lib/types/routes.d.ts +130 -0
  115. package/lib/types/settings.d.ts +112 -0
  116. package/lib/types/shelfry-catalog.d.ts +48 -0
  117. package/lib/types/snapshot.d.ts +93 -0
  118. package/lib/types/source-migration.d.ts +100 -0
  119. package/lib/types/sources.d.ts +391 -0
  120. package/lib/types/state.d.ts +20 -0
  121. package/lib/types/store.d.ts +47 -0
  122. package/lib/types/themes.d.ts +46 -0
  123. package/lib/types/trial.d.ts +61 -0
  124. package/lib/types/update-api-v1.d.ts +83 -0
  125. package/lib/types/updates.d.ts +273 -0
  126. package/lib/types/verify.d.ts +277 -0
  127. package/lib/update-api-v1.js +273 -0
  128. package/lib/updates.js +981 -0
  129. package/lib/verify.js +930 -0
  130. package/locale/en.json +5 -0
  131. package/locale/zh.json +6 -0
  132. package/package.json +113 -4
@@ -0,0 +1,342 @@
1
+ /**
2
+ * The recovery surface a failed market restart leaves behind.
3
+ *
4
+ * Why this exists: a restart button is only as good as what happens when the
5
+ * replacement does not come up. DSH's boot is all-or-nothing — the loader
6
+ * mounts every entry, then `assertEntriesActivated` throws for the first one
7
+ * that failed, so ONE broken plugin means `dsh web` exits and the browser is
8
+ * left holding a dead origin. The market's own UI dies with the host it was
9
+ * serving from, and the only way back used to be editing cordis.patch.yml by
10
+ * hand.
11
+ *
12
+ * So the detached restart helper (src/restart.ts) hands off to THIS module
13
+ * when the replacement it spawned never bound the port. This process is not
14
+ * part of the DSH tree, does not need the tree to load, and takes over the
15
+ * very origin the browser is already polling — which is what lets the market
16
+ * page still open in that tab offer a way out instead of a timeout, and lets
17
+ * a fresh visit to the URL find one too.
18
+ *
19
+ * What it does NOT do: it never boots DSH itself, never edits anything but
20
+ * the profile's own patch layer (plus `dsh.profile.bundles` for a
21
+ * disable-carrier), and never talks to the network. Its whole job is to take
22
+ * the user's choice of "which plugins should be on at the next start", write
23
+ * it through the same durable path the market's own toggles use, and then get
24
+ * out of the way so the real host can bind the port.
25
+ *
26
+ * Security posture: loopback only, same-origin enforced on every mutating
27
+ * request (a cross-site form post carries a foreign Origin and is refused),
28
+ * and the write set is not free input — only the plugin names this process
29
+ * was handed when the restart was scheduled can be switched.
30
+ */
31
+ import { type Server } from 'node:http';
32
+ /** One installed plugin as the recovery surface is allowed to see it. */
33
+ export interface RecoveryPlugin {
34
+ /** The installed package name — the only identity a write accepts. */
35
+ name: string;
36
+ /** Loader row ids this package owns in the profile patch layer. */
37
+ rows: string[];
38
+ /** Whether it stands enabled for the next boot. */
39
+ enabled: boolean;
40
+ /** Host infrastructure: switching it off breaks the chain itself. */
41
+ protected: boolean;
42
+ /** A bundle whose patch disables rows it does not own (see the toggle route). */
43
+ carrier: boolean;
44
+ /** Whether this surface may switch it at all. */
45
+ toggleable: boolean;
46
+ /** Why not, when `toggleable` is false; shown instead of a checkbox. */
47
+ note?: string;
48
+ }
49
+ /**
50
+ * Everything the recovery process needs, written by the market BEFORE the
51
+ * restart (so it survives the host it describes) and read by the helper's
52
+ * handoff.
53
+ */
54
+ export interface RecoveryConfig {
55
+ /** The origin the browser is polling; null when it could not be read. */
56
+ port: number | null;
57
+ profile: string;
58
+ profileDir: string;
59
+ patchPath: string;
60
+ /** Boot id of the process that scheduled the restart. */
61
+ bootId: string;
62
+ marketVersion: string;
63
+ scheduledAt: string;
64
+ logs: {
65
+ out: string;
66
+ err: string;
67
+ };
68
+ /** The replacement invocation, already platform-resolved (see respawnInvocation). */
69
+ spawn: {
70
+ file: string;
71
+ args: string[];
72
+ viaShell: boolean;
73
+ detached: boolean;
74
+ };
75
+ cwd: string;
76
+ plugins: RecoveryPlugin[];
77
+ /** Overridable for tests and for operators who want the port back sooner. */
78
+ idleTimeoutMs?: number;
79
+ /** How long a boot must keep answering before it counts as up (see BOOT_SETTLE_MS). */
80
+ settleMs?: number;
81
+ }
82
+ /** One plugin the boot refused to activate, as the log named it. */
83
+ export interface BootFailureEntry {
84
+ /** The loader entry name, verbatim — not yet matched to a package. */
85
+ name: string;
86
+ /** The reason the log gave, on one line. */
87
+ reason: string;
88
+ kind: 'failed' | 'pending' | 'unresolved';
89
+ }
90
+ /** What a failed boot's log says, reduced to what the UI can act on. */
91
+ export interface BootFailure {
92
+ /** One line naming the shape of the failure. */
93
+ summary: string;
94
+ entries: BootFailureEntry[];
95
+ /** The tail of the log, for the "what exactly happened" block. */
96
+ tail: string;
97
+ }
98
+ /** What one plugin's switch did (or why it could not). */
99
+ export interface RecoveryChange {
100
+ name: string;
101
+ from: boolean;
102
+ to: boolean;
103
+ error?: string;
104
+ }
105
+ /**
106
+ * Read the last failure DSH wrote into the replacement's log.
107
+ *
108
+ * The shapes come from @deepseek-ai/dsh-app-boot, which is the only thing
109
+ * that writes them: `assertEntriesLoaded` names unresolved modules as
110
+ * `plugin(s) failed to load: a, b`, and `assertEntriesActivated` prints
111
+ * `N entries did not activate` followed by one `<entry>: <reason>` line per
112
+ * failure — each reason being a stack, so every continuation line is
113
+ * indented. `boot()` then wraps the whole thing, and the CLI's fail-loud
114
+ * handler writes that wrapper to stderr, which the helper redirected to a
115
+ * file. Parsing the LAST marker is deliberate: after a recovery restart
116
+ * succeeds in writing a fix and the replacement fails again, the log holds
117
+ * both failures and only the newest one describes the tree as it stands.
118
+ *
119
+ * Anything unrecognized degrades to the log tail with no named entries,
120
+ * because "we could not tell which plugin" must not read as "no plugin
121
+ * failed".
122
+ * @param text - the replacement's captured stderr (and stdout), verbatim.
123
+ * @returns the failure, with entries matched to names rather than packages.
124
+ */
125
+ export declare function parseBootFailure(text: string): BootFailure;
126
+ /**
127
+ * Match the names a failure used to the plugins a write can name.
128
+ *
129
+ * DSH reports the LOADER entry name, which is the package name for a bundle
130
+ * row and a path for a local include; the market's inventory is keyed by
131
+ * package name and row id. The three comparisons below are the shapes that
132
+ * actually occur: an exact package name, an exact row id (a package may
133
+ * insert a row under another name), and a path whose basename without
134
+ * extension is the package. Unmatched names are returned as well, so the UI
135
+ * can say "DSH named this one and we cannot switch it" rather than silently
136
+ * dropping the only clue the user has.
137
+ * @param plugins - the inventory handed to this process.
138
+ * @param failure - the parsed failure.
139
+ * @returns implicated plugin names and the names that matched nothing.
140
+ */
141
+ export declare function matchFailureToPlugins(plugins: readonly RecoveryPlugin[], failure: BootFailure): {
142
+ implicated: Set<string>;
143
+ reasons: Map<string, BootFailureEntry>;
144
+ unmatched: BootFailureEntry[];
145
+ };
146
+ /** The plugin view the UI renders: inventory plus what the failure implicated. */
147
+ export interface RecoveryPluginView extends RecoveryPlugin {
148
+ /**
149
+ * The switch position the surface OPENS on, not the plugin's current state.
150
+ * For a plugin this boot blamed — and that can be switched at all — the fix
151
+ * is "off", so the box starts off: that is the contract the failure prompt
152
+ * makes ("the plugins DSH blamed are marked red and left unticked"), and
153
+ * leaving the user to untick a red row by hand is a weaker one.
154
+ */
155
+ enabled: boolean;
156
+ implicated: boolean;
157
+ /** The reason the boot named, when it named this plugin. */
158
+ reason?: string;
159
+ }
160
+ /** The whole payload both surfaces (the live page and the standalone one) read. */
161
+ export interface RecoveryPayload {
162
+ ok: true;
163
+ recovery: true;
164
+ profile: string;
165
+ bootId: string;
166
+ scheduledAt: string;
167
+ marketVersion: string;
168
+ failure: BootFailure;
169
+ plugins: RecoveryPluginView[];
170
+ /** Names DSH blamed that this surface cannot switch. */
171
+ unmatched: BootFailureEntry[];
172
+ /** Write errors from the last apply, when the choice could not be written in full. */
173
+ lastErrors: string[];
174
+ logPath: string;
175
+ }
176
+ /**
177
+ * Build the payload from the config and the parsed failure.
178
+ * @param config - the recovery config written before the restart.
179
+ * @param failure - the failure parsed from the replacement's log.
180
+ * @param lastErrors - write errors from the previous apply, if it did not land.
181
+ * @returns the payload both surfaces render.
182
+ */
183
+ export declare function recoveryPayload(config: RecoveryConfig, failure: BootFailure, lastErrors?: readonly string[]): RecoveryPayload;
184
+ /**
185
+ * Write one desired enable set through the durable layer.
186
+ *
187
+ * `enabled` is the FULL desired state, not a delta: the UI sends what its
188
+ * checkboxes say, and only the differences are written. That is what makes
189
+ * "uncheck everything I just installed, keep the rest" one request, and it is
190
+ * also why a plugin the UI did not mention keeps whatever it had.
191
+ *
192
+ * A disable-carrier moves in and out of `dsh.profile.bundles` as well, the
193
+ * same way the market's toggle route does it (its rows are not the only
194
+ * effect it has). Failures are collected rather than thrown: a partial write
195
+ * must be reported, not swallowed, and the caller then refuses to restart
196
+ * into a composition it could not fully express.
197
+ * @param config - the recovery config.
198
+ * @param enabled - names to leave enabled; every other toggleable plugin is disabled.
199
+ * @returns the changes attempted and the errors, if any.
200
+ */
201
+ export declare function applyRecovery(config: RecoveryConfig, enabled: readonly string[]): Promise<{
202
+ ok: boolean;
203
+ changes: RecoveryChange[];
204
+ errors: string[];
205
+ }>;
206
+ /** Probe one loopback port the way the restart helper does (connect, not bind). */
207
+ export declare function portListening(port: number, timeoutMs?: number): Promise<boolean>;
208
+ /**
209
+ * Start the replacement and watch whether it takes the port.
210
+ *
211
+ * The caller has already released the listener by the time this runs — the
212
+ * replacement cannot bind otherwise. A child that exits is a verdict on its
213
+ * own and returns immediately: waiting out the full timeout for a process
214
+ * that is already gone is the difference between a recovery page in five
215
+ * seconds and one in forty-five, and the market page's own poll has a
216
+ * deadline, and only once the answer has been STEADY — see BOOT_SETTLE_MS.
217
+ * @param config - the recovery config (spawn invocation and port).
218
+ * @returns whether the replacement really came up.
219
+ */
220
+ export declare function respawnAndWatch(config: RecoveryConfig): Promise<boolean>;
221
+ /** Bilingual copy for the standalone page (the market page has its own). */
222
+ declare const PAGE_TEXT: {
223
+ readonly zh: {
224
+ readonly title: 'DeepSeek Harness 启动失败';
225
+ readonly lead: '市场触发的重启没能起来,dsh 已退出。下面勾选下次启动要启用的插件——报错的插件已经标红并取消勾选。';
226
+ readonly failed: '本次启动报错的插件';
227
+ readonly plugins: '已安装插件(勾选 = 下次启动启用)';
228
+ readonly protected: '宿主基础设施,不能关闭';
229
+ readonly notToggleable: '无法在此关闭';
230
+ readonly none: '没有解析到具体是哪个插件报的错,下面是 dsh 的原始输出。';
231
+ readonly unmatched: 'DSH 点名但本页无法关闭的条目:';
232
+ readonly apply: '保存并重启';
233
+ readonly applying: '正在写入并重启…';
234
+ readonly restarting: '重启中:dsh 起来后本页会自动刷新;若又失败,会带着新的报错回到这里。';
235
+ readonly release: '释放端口并退出';
236
+ readonly log: '完整日志';
237
+ readonly error: '操作失败';
238
+ readonly idle: '长时间无人操作后本页会自动释放端口。';
239
+ readonly changes: '将要改动:';
240
+ readonly nothing: '没有需要改动的插件,直接重启。';
241
+ readonly writeFailed: '上次的选择没能完整写入,因此没有重启:';
242
+ };
243
+ readonly en: {
244
+ readonly title: 'DeepSeek Harness failed to start';
245
+ readonly lead: 'The restart the market triggered never came up, and dsh has exited. Tick the plugins to enable at the next start — the ones the boot blamed are highlighted in red and left unticked.';
246
+ readonly failed: 'Blamed by this boot';
247
+ readonly plugins: 'Installed plugins (ticked = enabled at the next start)';
248
+ readonly protected: 'host infrastructure — cannot be switched off';
249
+ readonly notToggleable: 'cannot be switched off here';
250
+ readonly none: 'The log did not name a plugin; the raw output is below.';
251
+ readonly unmatched: 'Named by DSH but not switchable here:';
252
+ readonly apply: 'Save and restart';
253
+ readonly applying: 'Writing and restarting…';
254
+ readonly restarting: 'Restarting: this page reloads itself once dsh is up; if it fails again you come back here with the new error.';
255
+ readonly release: 'Release the port and quit';
256
+ readonly log: 'Full log';
257
+ readonly error: 'The operation failed';
258
+ readonly idle: 'This page releases the port on its own after a long idle period.';
259
+ readonly changes: 'Changes:';
260
+ readonly nothing: 'Nothing to change — restarting.';
261
+ readonly writeFailed: 'The last choice could not be written in full, so nothing was restarted:';
262
+ };
263
+ };
264
+ type PageLang = keyof typeof PAGE_TEXT;
265
+ /**
266
+ * The standalone recovery page.
267
+ *
268
+ * A separate document rather than a React tree on purpose: the market's
269
+ * bundle lives in the composition that just failed to boot, so the one thing
270
+ * this page must not depend on is the composition. It reads its state from
271
+ * the JSON endpoints and renders the checklist with the same colours as the
272
+ * market page.
273
+ * @param lang - which copy to serve.
274
+ */
275
+ export declare function recoveryPageHtml(lang: PageLang): string;
276
+ /**
277
+ * What the person looking at the surface decided.
278
+ *
279
+ * The enable set travels WITH the decision, because the process that shows
280
+ * the checklist is not the process that writes it: `runRecovery` does the
281
+ * single patch-layer write the user asked for, and it can only do that if the
282
+ * ticks reach it. (They did not, once: the surface recorded the choice, went
283
+ * straight to booting the unchanged composition, and came back with the same
284
+ * error — the demo plugin's run is what exposed it.)
285
+ */
286
+ export type RecoveryDecision = {
287
+ kind: 'apply';
288
+ enabled: string[];
289
+ } | {
290
+ kind: 'released';
291
+ } | {
292
+ kind: 'idle';
293
+ };
294
+ /** The parts of the recovery server a test (or the CLI) needs to drive it. */
295
+ export interface RecoveryServer {
296
+ server: Server;
297
+ port: number;
298
+ url: string;
299
+ close: () => Promise<void>;
300
+ /**
301
+ * Resolves when the surface is done being shown: the user asked for the
302
+ * chosen composition to be written and booted (with the enable set they
303
+ * left ticked), asked for the port back, or simply walked away.
304
+ */
305
+ finished: Promise<RecoveryDecision>;
306
+ }
307
+ /**
308
+ * Serve the recovery surface on the port the failed host left behind.
309
+ * @param config - the recovery config written before the restart.
310
+ * @param failure - the failure parsed from the replacement's log.
311
+ * @param options - test seams: an explicit port, and an idle timeout override.
312
+ * @returns the running server and the outcome it settles on.
313
+ */
314
+ export declare function startRecoveryServer(config: RecoveryConfig, failure: BootFailure, options?: {
315
+ port?: number;
316
+ idleTimeoutMs?: number;
317
+ lastErrors?: readonly string[];
318
+ }): Promise<RecoveryServer>;
319
+ /**
320
+ * The recovery process end to end: parse the log, serve the surface, and on a
321
+ * user's decision write the choice down and try the boot again — rebinding
322
+ * and starting over when the new composition fails too, because the second
323
+ * failure is the one that says the first guess was wrong.
324
+ * @param config - the recovery config written before the restart.
325
+ * @param facts - what the helper observed (exit code, whether it ever bound).
326
+ * @param options - test seams.
327
+ * @returns the outcome, for the CLI's exit code.
328
+ */
329
+ export declare function runRecovery(config: RecoveryConfig, facts?: {
330
+ exitCode: number | null;
331
+ bound: boolean;
332
+ replacementPid?: number | null;
333
+ }, options?: {
334
+ port?: number;
335
+ idleTimeoutMs?: number;
336
+ }): Promise<'booted' | 'released' | 'idle'>;
337
+ /**
338
+ * The script entry: `node lib/recovery.js <config.json> [--exit=N] [--bound=0|1]`.
339
+ * @param argv - arguments after the node executable and this script.
340
+ */
341
+ export declare function runRecoveryCli(argv: readonly string[]): Promise<void>;
342
+ export {};
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Deciding a download region by asking the network, once.
3
+ *
4
+ * The alternative was reading the system time zone, and it is wrong for
5
+ * exactly the people who most need this to work: someone in Shanghai behind
6
+ * a corporate proxy or a VPN reaches the official registry perfectly well
7
+ * and would be routed onto mirrors they do not need, while a Chinese-locale
8
+ * machine sitting in Singapore would be routed onto mirrors that are further
9
+ * away than the origin. A time zone says where a clock is. It does not say
10
+ * what the network can reach, which is the only question here.
11
+ *
12
+ * So the probe measures the thing itself: it asks both registries for the
13
+ * same small document and takes whichever answers first. That is
14
+ * self-correcting in a way a lookup table cannot be — the machine behind the
15
+ * proxy measures fast official access and stays global, without anyone
16
+ * having to enumerate the exceptions.
17
+ *
18
+ * It runs ONCE, when no region has ever been decided, and its answer is
19
+ * persisted as the decision. Re-probing every boot would let a market
20
+ * silently change routes between runs, which is the kind of behaviour that
21
+ * makes "it was fast yesterday" impossible to debug.
22
+ */
23
+ import { type Region } from './regions.ts';
24
+ /**
25
+ * Ask every region's registry the same question; return whichever answers
26
+ * first.
27
+ *
28
+ * First-past-the-post rather than a latency comparison, deliberately. From
29
+ * inside China the official registry usually does answer eventually, so
30
+ * "did it fail" is the wrong test and "which came back first" is the right
31
+ * one. From outside, the mainland mirror is reachable but further, and the
32
+ * same rule picks global. One rule, both directions, no thresholds to tune.
33
+ *
34
+ * @param timeoutMs - how long to wait before giving up on all of them.
35
+ * @param env - environment, for the registry overrides.
36
+ * @returns the winning region, or `global` when nothing answered — an
37
+ * unreachable network is not evidence for switching routes.
38
+ */
39
+ export declare function probeRegion(timeoutMs?: number, env?: NodeJS.ProcessEnv): Promise<Region>;
40
+ /**
41
+ * The region to run under, probing only if nothing has ever decided one.
42
+ *
43
+ * @param stored - the region already on record, or undefined.
44
+ * @returns the region, and whether this call decided it (which is what earns
45
+ * the user a one-time notice explaining the choice).
46
+ */
47
+ export declare function resolveRegion(stored: Region | undefined, timeoutMs?: number, env?: NodeJS.ProcessEnv): Promise<{
48
+ region: Region;
49
+ probed: boolean;
50
+ }>;
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Download regions: which route the market's own network requests take.
3
+ *
4
+ * Almost every external request the market makes lands on npm's registry or
5
+ * on GitHub — the plugin catalog, update checks, package downloads, author
6
+ * avatars, README screenshots. From mainland China several of those can be
7
+ * slow, which is why this is ONE setting rather than a
8
+ * row of them: "npm mirror", "GitHub proxy" and "image proxy" are three
9
+ * spellings of a single question the user is actually being asked, which is
10
+ * where they are.
11
+ *
12
+ * The routing table is the single source of truth. Every consumer asks it
13
+ * rather than reaching for a hardcoded host, so adding a region is a table
14
+ * entry instead of a search across six modules.
15
+ *
16
+ * Each route has an environment escape hatch, following `DSHM_REGISTRY_URL`
17
+ * (src/registry.ts). The China route has service-specific public-proxy
18
+ * fallbacks; those come and go, and a user whose routes have died needs a way
19
+ * out that is not "wait for the next release".
20
+ */
21
+ /** A region the market can download from. */
22
+ export type Region = 'global' | 'china';
23
+ /** Every region a user may pick. */
24
+ export declare const REGIONS: readonly Region[];
25
+ /** Narrow an untrusted value to a Region, or null. */
26
+ export declare function asRegion(value: unknown): Region | null;
27
+ /**
28
+ * The npm registry the market and pnpm read, no trailing slash.
29
+ *
30
+ * Exported because callers need to tell "this region uses the default" from
31
+ * "this region names a mirror" — the difference between leaving a spawned
32
+ * pnpm's registry alone and setting it.
33
+ */
34
+ export declare const DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org";
35
+ /**
36
+ * Default npm registry for the China download region, no trailing slash.
37
+ *
38
+ * Exported so failure classifiers can recognize this host in pnpm's output
39
+ * (#926) without duplicating the URL.
40
+ */
41
+ export declare const CHINA_NPM_REGISTRY = "https://mirrors.cloud.tencent.com/npm";
42
+ /** GitHub transports that fail independently on filtered networks. */
43
+ export type GithubService = 'git' | 'raw' | 'avatar';
44
+ /** A prefix proxy, or null for the canonical GitHub address. */
45
+ export type GithubRoute = string | null;
46
+ /** Ordered candidates per GitHub service. */
47
+ export type GithubRoutes = Record<GithubService, GithubRoute[]>;
48
+ /**
49
+ * One place the catalog can be read from.
50
+ *
51
+ * Two kinds because the two routes are genuinely different transports, not
52
+ * two URLs. The npm route reads a published package — which is what lets the
53
+ * catalog ride the same mirror as everything else, and gives it a version
54
+ * number that can be rolled back when a bad build ships.
55
+ */
56
+ export type CatalogSource = {
57
+ kind: 'url';
58
+ url: string;
59
+ } | {
60
+ kind: 'npm';
61
+ registry: string;
62
+ pkg: string;
63
+ };
64
+ /** Where one region sends each kind of request. `null` means "go direct". */
65
+ export interface RegionRoutes {
66
+ /** npm registry base, no trailing slash. */
67
+ npmRegistry: string;
68
+ /** Prefix proxy for github.com-family URLs, or null to go direct. */
69
+ githubProxy: string | null;
70
+ /** Ordered routes per service; every list ends in a direct escape path. */
71
+ githubRoutes: GithubRoutes;
72
+ /**
73
+ * Where to look for the catalog, in order. Later entries are fallbacks.
74
+ *
75
+ * The catalog is the FIRST request the market makes, so a mirror that has
76
+ * gone down must mean a slow market rather than an empty one — every
77
+ * region ends its list at an address that has always worked.
78
+ */
79
+ catalog: CatalogSource[];
80
+ }
81
+ /**
82
+ * 一个地区要尝试的所有目录地址。
83
+ *
84
+ * crate 只有一份公开 JSON,没有可以退到的 npm 镜像包,所以这里的候选列表
85
+ * 就是该地区自己声明的顺序;保留这个函数是为了让「列表怎么来的」只在一个
86
+ * 地方回答,调用方不必知道地区表的结构。
87
+ */
88
+ export declare function catalogSourcesFor(region: Region): CatalogSource[];
89
+ /**
90
+ * Normalize a user-maintained prefix, or reject it.
91
+ *
92
+ * Public mirrors receive the complete destination URL in their path. Only
93
+ * HTTPS prefixes without embedded credentials or query fragments are safe to
94
+ * persist and show again in the settings UI.
95
+ */
96
+ export declare function normalizeGithubProxy(value: unknown): string | null;
97
+ /** Apply (or clear) the persisted UI escape route. */
98
+ export declare function setCustomGithubProxy(proxy: string | null): void;
99
+ /** Whether the operator-owned environment variable disables UI changes. */
100
+ export declare function githubProxyManaged(env?: NodeJS.ProcessEnv): boolean;
101
+ /** Remember one verified route without changing other GitHub services. */
102
+ export declare function rememberGithubRoute(service: GithubService, route: GithubRoute): void;
103
+ /** Forget learned winners after the configured candidates change. */
104
+ export declare function resetGithubRoutePreferences(): void;
105
+ /**
106
+ * The routes for a region, with environment overrides applied.
107
+ *
108
+ * Overrides win over the table because they are the user's statement about
109
+ * their own network, and they are the way out when a public proxy dies.
110
+ *
111
+ * `DSHM_REGISTRY_URL` keeps its existing meaning — the catalog URL — and
112
+ * when set it REPLACES the source list rather than heading it: someone
113
+ * pointing the market at their own catalog does not want it quietly
114
+ * reverting to ours.
115
+ */
116
+ export declare function routesFor(region: Region, env?: NodeJS.ProcessEnv): RegionRoutes;
117
+ /** Ordered candidates with this process's last verified winner first. */
118
+ export declare function githubRoutesFor(service: GithubService, region?: Region, env?: NodeJS.ProcessEnv): GithubRoute[];
119
+ /** The region in force. */
120
+ export declare function activeRegion(): Region;
121
+ /** Set the region in force. Callers are responsible for their own caches. */
122
+ export declare function setActiveRegion(region: Region): void;
123
+ /**
124
+ * Wrap a github.com-family URL in a prefix proxy.
125
+ *
126
+ * The proxy takes the full absolute URL as its path (`{proxy}/{url}`) rather
127
+ * than a rewritten hostname, so the same joining rule works for every
128
+ * GitHub service a caller has explicitly allowed through public routes.
129
+ *
130
+ * @param proxy - the prefix, or null to go direct.
131
+ * @param url - an absolute https URL on a github.com-family host.
132
+ * @returns the proxied URL, or `url` unchanged when there is no proxy.
133
+ */
134
+ export declare function throughProxy(proxy: string | null, url: string): string;