dsh-crate 0.6.0 → 0.6.2
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/client/client.js +1105 -138
- package/lib/catalog-npm.js +3 -1
- package/lib/catalogs.js +567 -0
- package/lib/changelog.js +3 -2
- package/lib/discovery-compatibility.js +3 -2
- package/lib/dsh-cli.js +1 -1
- package/lib/hot.js +40 -5
- package/lib/http.js +13 -0
- package/lib/net.js +10 -2
- package/lib/npmrc.js +267 -0
- package/lib/offline.js +183 -0
- package/lib/policy.js +326 -0
- package/lib/regions.js +15 -4
- package/lib/registry.js +89 -5
- package/lib/routes.js +374 -25
- package/lib/self-reload.js +296 -0
- package/lib/sources.js +20 -2
- package/lib/telemetry.js +4 -0
- package/lib/types/catalog-npm.d.ts +1 -1
- package/lib/types/catalogs.d.ts +156 -0
- package/lib/types/hot.d.ts +26 -5
- package/lib/types/http.d.ts +2 -0
- package/lib/types/net.d.ts +2 -2
- package/lib/types/npmrc.d.ts +79 -0
- package/lib/types/offline.d.ts +54 -0
- package/lib/types/policy.d.ts +116 -0
- package/lib/types/regions.d.ts +2 -0
- package/lib/types/registry.d.ts +45 -21
- package/lib/types/routes.d.ts +10 -0
- package/lib/types/self-reload.d.ts +174 -0
- package/lib/types/sources.d.ts +3 -0
- package/lib/types/updates.d.ts +2 -0
- package/lib/updates.js +26 -5
- package/package.json +1 -1
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reload the market's OWN server half inside the running DSH process.
|
|
3
|
+
*
|
|
4
|
+
* Why this exists: the market updates itself by replacing its files, and the
|
|
5
|
+
* Node module it already imported keeps running. The browser, meanwhile, loads
|
|
6
|
+
* the NEW client bundle, which calls routes the old process does not have (the
|
|
7
|
+
* host answers 405 with an empty body). The only cure used to be a restart,
|
|
8
|
+
* and on DSH Desktop even that is not ours to do (`allowRestart: false`).
|
|
9
|
+
*
|
|
10
|
+
* How: not by re-importing ourselves — Node never re-evaluates a module at a
|
|
11
|
+
* URL it has already loaded, and a cache-busted URL only renews the entry
|
|
12
|
+
* file while every sibling it imports stays the old one. The host already
|
|
13
|
+
* solves exactly this for every plugin: `hmr.reconcileProfile()` (the service
|
|
14
|
+
* `@deepseek-ai/dsh-hmr` publishes as `ctx.hmr`, present from DSH 0.2.1)
|
|
15
|
+
* compares the files of every loaded module with the disk, evicts the ones
|
|
16
|
+
* that changed together with their importers, stops the loader entries that
|
|
17
|
+
* ran them and starts those entries again from the new files. The market is a
|
|
18
|
+
* loader entry like any other, so it is replaced the same way. This module is
|
|
19
|
+
* the orchestration around that one call.
|
|
20
|
+
*
|
|
21
|
+
* The part that is ours, and the reason this is more than one call:
|
|
22
|
+
*
|
|
23
|
+
* - The request that asks for the swap is served by the instance about to be
|
|
24
|
+
* replaced. Its closure outlives its own disposal (the HTTP response object
|
|
25
|
+
* belongs to the web server, not to the plugin), so the SAME request can
|
|
26
|
+
* still report how the swap went.
|
|
27
|
+
* - The host does not restore the old code when the new code cannot start: it
|
|
28
|
+
* evicts the old modules without a backup. So the new build is imported
|
|
29
|
+
* first, from a private copy at a path Node has never seen, and the swap is
|
|
30
|
+
* only asked for when that worked. A build that cannot even load never
|
|
31
|
+
* replaces a market that is running fine.
|
|
32
|
+
* - Which instance is serving is a fact held outside every module — on
|
|
33
|
+
* `globalThis` under a registered symbol — because the module that would
|
|
34
|
+
* hold it is exactly what gets replaced.
|
|
35
|
+
*/
|
|
36
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
37
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
38
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
39
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return path;
|
|
43
|
+
};
|
|
44
|
+
import { cpSync, existsSync, mkdirSync, readFileSync, realpathSync, rmSync } from 'node:fs';
|
|
45
|
+
import { basename, dirname, join } from 'node:path';
|
|
46
|
+
import { pathToFileURL } from 'node:url';
|
|
47
|
+
/**
|
|
48
|
+
* The host's HMR service when it can replace modules, otherwise undefined.
|
|
49
|
+
* Checked by shape: DSH 0.2.0 and earlier publish an `hmr` service without
|
|
50
|
+
* `reconcileProfile`, and a call into it would fail after we had decided to
|
|
51
|
+
* go ahead.
|
|
52
|
+
*/
|
|
53
|
+
export function asHmr(value) {
|
|
54
|
+
if (value === null || typeof value !== 'object')
|
|
55
|
+
return undefined;
|
|
56
|
+
const candidate = value;
|
|
57
|
+
return typeof candidate.runExclusive === 'function' && typeof candidate.reconcileProfile === 'function'
|
|
58
|
+
? value
|
|
59
|
+
: undefined;
|
|
60
|
+
}
|
|
61
|
+
// ── Which instance is serving ─────────────────────────────────────────────
|
|
62
|
+
const INSTANCE_KEY = Symbol.for('dsh-crate.serving-instance');
|
|
63
|
+
let instanceSequence = 0;
|
|
64
|
+
/**
|
|
65
|
+
* Announce this instance as the one serving. Returns the id to compare
|
|
66
|
+
* against, and a release that only clears the record when it is still this
|
|
67
|
+
* instance's own — a late release from a replaced instance must not erase the
|
|
68
|
+
* record its replacement has already written.
|
|
69
|
+
*/
|
|
70
|
+
export function claimInstance(version) {
|
|
71
|
+
instanceSequence += 1;
|
|
72
|
+
const id = `${String(process.pid)}-${String(Date.now())}-${String(instanceSequence)}-${Math.random().toString(16).slice(2, 8)}`;
|
|
73
|
+
const holder = globalThis;
|
|
74
|
+
holder[INSTANCE_KEY] = { id, version };
|
|
75
|
+
return {
|
|
76
|
+
id,
|
|
77
|
+
release: () => {
|
|
78
|
+
if (holder[INSTANCE_KEY]?.id === id)
|
|
79
|
+
delete holder[INSTANCE_KEY];
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** The instance serving right now, or undefined while none is. */
|
|
84
|
+
export function currentInstance() {
|
|
85
|
+
return globalThis[INSTANCE_KEY];
|
|
86
|
+
}
|
|
87
|
+
let preflightSequence = 0;
|
|
88
|
+
/**
|
|
89
|
+
* Whether the package now on disk can be loaded at all.
|
|
90
|
+
*
|
|
91
|
+
* Imports a private copy of its `lib/` from a path nobody has imported, so
|
|
92
|
+
* the whole module graph is evaluated fresh and any failure — a syntax error,
|
|
93
|
+
* a dependency that is missing, a top-level throw — surfaces here, while the
|
|
94
|
+
* running market is still untouched. The copy sits NEXT TO the package so
|
|
95
|
+
* bare imports resolve through the same `node_modules` chain as the real one,
|
|
96
|
+
* and is deleted again afterwards; the host forgets files that disappeared.
|
|
97
|
+
*
|
|
98
|
+
* What it cannot see: a build that loads fine and then fails inside `apply`.
|
|
99
|
+
* That is only learnable by running it.
|
|
100
|
+
*
|
|
101
|
+
* `checked: false` means the check could not be run (the directory is not
|
|
102
|
+
* writable); the caller proceeds, because refusing every reload on a
|
|
103
|
+
* read-only profile would make the feature useless there.
|
|
104
|
+
*/
|
|
105
|
+
export async function preflightBuild(packageDir) {
|
|
106
|
+
let real;
|
|
107
|
+
let main;
|
|
108
|
+
try {
|
|
109
|
+
real = realpathSync(packageDir);
|
|
110
|
+
const manifest = JSON.parse(readFileSync(join(real, 'package.json'), 'utf8'));
|
|
111
|
+
main = typeof manifest.main === 'string' ? manifest.main : 'index.js';
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
return { ok: false, detail: `the installed package cannot be read — ${messageOf(error)}` };
|
|
115
|
+
}
|
|
116
|
+
if (!existsSync(join(real, main)))
|
|
117
|
+
return { ok: false, detail: `the entry file ${main} is missing` };
|
|
118
|
+
preflightSequence += 1;
|
|
119
|
+
const copy = join(dirname(real), `.dsh-crate-reload-${String(process.pid)}-${String(preflightSequence)}`);
|
|
120
|
+
try {
|
|
121
|
+
mkdirSync(copy, { recursive: true });
|
|
122
|
+
cpSync(join(real, 'package.json'), join(copy, 'package.json'));
|
|
123
|
+
cpSync(join(real, dirname(main)), join(copy, dirname(main)), {
|
|
124
|
+
recursive: true,
|
|
125
|
+
// Declarations are not loaded and a second copy of them is dead weight.
|
|
126
|
+
filter: source => {
|
|
127
|
+
const base = basename(source);
|
|
128
|
+
return base !== 'types' && base !== 'node_modules' && !base.endsWith('.d.ts');
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
rmSync(copy, { recursive: true, force: true });
|
|
134
|
+
return { ok: true, checked: false };
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const loaded = await import(__rewriteRelativeImportExtension(/* @vite-ignore */ pathToFileURL(join(copy, main)).href));
|
|
138
|
+
if (typeof loaded.apply !== 'function')
|
|
139
|
+
return { ok: false, detail: 'the new build does not export a plugin entry (apply)' };
|
|
140
|
+
return { ok: true, checked: true };
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
return { ok: false, detail: messageOf(error) };
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
rmSync(copy, { recursive: true, force: true });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function messageOf(error) {
|
|
150
|
+
const text = error instanceof Error ? error.message : String(error);
|
|
151
|
+
// The copy's own path means nothing to the reader; the file name does.
|
|
152
|
+
return text.replace(/\.dsh-crate-reload-\d+-\d+/g, 'the new build').slice(0, 400);
|
|
153
|
+
}
|
|
154
|
+
const DEFAULT_QUEUE_WAIT_MS = 60_000;
|
|
155
|
+
const DEFAULT_SETTLE_MS = 5_000;
|
|
156
|
+
const sleep = (ms) => new Promise(resolve => { setTimeout(resolve, ms); });
|
|
157
|
+
/**
|
|
158
|
+
* Ask the host to replace this market with the version on disk.
|
|
159
|
+
*
|
|
160
|
+
* The caller has already refused when an operation is running: a swap ends
|
|
161
|
+
* the instance that would have finished it. Nothing here writes a file.
|
|
162
|
+
*/
|
|
163
|
+
export async function reloadSelf(deps) {
|
|
164
|
+
const log = deps.log ?? (() => { });
|
|
165
|
+
const serving = deps.serving ?? currentInstance;
|
|
166
|
+
const hmr = deps.hmr();
|
|
167
|
+
if (hmr === undefined)
|
|
168
|
+
return { state: 'unsupported', reason: 'no-hmr' };
|
|
169
|
+
const target = deps.installedVersion();
|
|
170
|
+
if (target === null)
|
|
171
|
+
return { state: 'unsupported', reason: 'not-installed' };
|
|
172
|
+
if (target === deps.runningVersion)
|
|
173
|
+
return { state: 'current', version: target };
|
|
174
|
+
const verdict = await (deps.preflight ?? preflightBuild)(deps.packageDir());
|
|
175
|
+
if (!verdict.ok) {
|
|
176
|
+
log('warn', `reload to ${target} refused, the new build does not load: ${verdict.detail}`);
|
|
177
|
+
return { state: 'refused', detail: verdict.detail };
|
|
178
|
+
}
|
|
179
|
+
if (!verdict.checked)
|
|
180
|
+
log('warn', `reload to ${target}: the new build could not be test-loaded first`);
|
|
181
|
+
log('info', `reloading ${deps.runningVersion} -> ${target}`);
|
|
182
|
+
const queueWaitMs = deps.queueWaitMs ?? DEFAULT_QUEUE_WAIT_MS;
|
|
183
|
+
const swap = hmr.runExclusive(() => hmr.reconcileProfile());
|
|
184
|
+
// The swap itself is never abandoned: it was asked for, and running it a
|
|
185
|
+
// little late is what the user wants. Only the WAITING for it has a limit.
|
|
186
|
+
let timer;
|
|
187
|
+
const waited = await Promise.race([
|
|
188
|
+
swap.then(report => ({ report }), (error) => ({ error })),
|
|
189
|
+
new Promise(resolve => { timer = setTimeout(() => { resolve('queued'); }, queueWaitMs); }),
|
|
190
|
+
]).finally(() => { clearTimeout(timer); });
|
|
191
|
+
if (waited === 'queued') {
|
|
192
|
+
void swap.catch((error) => { log('warn', `queued reload failed: ${messageOf(error)}`); });
|
|
193
|
+
return { state: 'queued' };
|
|
194
|
+
}
|
|
195
|
+
if ('error' in waited) {
|
|
196
|
+
const detail = messageOf(waited.error);
|
|
197
|
+
const stillOurs = serving()?.id === deps.instanceId;
|
|
198
|
+
log('warn', `reload to ${target} failed${stillOurs ? ' (the running market was not touched)' : ''}: ${detail}`);
|
|
199
|
+
return stillOurs
|
|
200
|
+
? { state: 'restart-needed', detail }
|
|
201
|
+
: { state: 'failed', detail, serving: serving() !== undefined };
|
|
202
|
+
}
|
|
203
|
+
// The host says it is done. Believe the instance record, not the report: the
|
|
204
|
+
// new instance announces itself when its routes are up, which is the fact
|
|
205
|
+
// that matters.
|
|
206
|
+
const deadline = Date.now() + (deps.settleMs ?? DEFAULT_SETTLE_MS);
|
|
207
|
+
for (;;) {
|
|
208
|
+
const now = serving();
|
|
209
|
+
if (now !== undefined && now.id !== deps.instanceId) {
|
|
210
|
+
log('info', `reloaded to ${now.version}`);
|
|
211
|
+
return { state: 'reloaded', from: deps.runningVersion, to: now.version };
|
|
212
|
+
}
|
|
213
|
+
if (Date.now() >= deadline)
|
|
214
|
+
break;
|
|
215
|
+
await sleep(100);
|
|
216
|
+
}
|
|
217
|
+
const now = serving();
|
|
218
|
+
if (now?.id === deps.instanceId) {
|
|
219
|
+
// Nothing was replaced: the host could not compare or evict the modules
|
|
220
|
+
// (a loader without access to Node's module cache, a host-owned module).
|
|
221
|
+
const why = waited.report.restartReason !== undefined
|
|
222
|
+
? `the host could not replace modules (${waited.report.restartReason})`
|
|
223
|
+
: waited.report.pending.length > 0
|
|
224
|
+
? `the host keeps ${String(waited.report.pending.length)} changed module(s) until a restart (${waited.report.pending[0].reason})`
|
|
225
|
+
: 'the host reported done but the old code is still running';
|
|
226
|
+
log('warn', `reload to ${target}: ${why}`);
|
|
227
|
+
return { state: 'restart-needed', detail: why };
|
|
228
|
+
}
|
|
229
|
+
const detail = 'the new version did not start serving';
|
|
230
|
+
log('warn', `reload to ${target}: ${detail}`);
|
|
231
|
+
return { state: 'failed', detail, serving: false };
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* The HTTP answer for an outcome. Every failure says what happened, why and
|
|
235
|
+
* what to do now, in the project's two-language style; the state code is for
|
|
236
|
+
* the page, which decides what to offer next.
|
|
237
|
+
*/
|
|
238
|
+
export function selfReloadReply(outcome) {
|
|
239
|
+
switch (outcome.state) {
|
|
240
|
+
case 'reloaded':
|
|
241
|
+
return { status: 200, body: { ok: true, outcome: 'reloaded', from: outcome.from, to: outcome.to } };
|
|
242
|
+
case 'current':
|
|
243
|
+
return { status: 200, body: { ok: true, outcome: 'current', version: outcome.version } };
|
|
244
|
+
case 'queued':
|
|
245
|
+
return {
|
|
246
|
+
status: 202,
|
|
247
|
+
body: {
|
|
248
|
+
ok: true,
|
|
249
|
+
outcome: 'queued',
|
|
250
|
+
message: 'DSH 正在处理另一项变更,市场会在它结束后自动换成新版。 / DSH is busy with another change. The market will switch to the new version when it finishes.',
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
case 'unsupported':
|
|
254
|
+
return {
|
|
255
|
+
status: 501,
|
|
256
|
+
body: {
|
|
257
|
+
ok: false,
|
|
258
|
+
outcome: 'unsupported',
|
|
259
|
+
error: outcome.reason === 'no-hmr'
|
|
260
|
+
? '这个 DSH 版本不能原地换上新版市场。重启 DSH 就会用上新版。 / This DSH version cannot switch to the new market in place. Restart DSH to use it.'
|
|
261
|
+
: '市场不是作为插件装在这里的,没有新版文件可换。要用磁盘上的文件,请重启 DSH。 / The market is not installed as a plugin here, so there is no new version to switch to. Restart DSH to run what is on disk.',
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
case 'refused':
|
|
265
|
+
return {
|
|
266
|
+
status: 422,
|
|
267
|
+
body: {
|
|
268
|
+
ok: false,
|
|
269
|
+
outcome: 'refused',
|
|
270
|
+
detail: outcome.detail,
|
|
271
|
+
error: '新版本没能加载,已保留正在运行的版本,它照常可用。请再更新一次;重启 DSH 能看到具体原因。 / The new version could not load, so the running one was kept and still works. Update again, or restart DSH to see why.',
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
case 'restart-needed':
|
|
275
|
+
return {
|
|
276
|
+
status: 502,
|
|
277
|
+
body: {
|
|
278
|
+
ok: false,
|
|
279
|
+
outcome: 'restart-needed',
|
|
280
|
+
detail: outcome.detail,
|
|
281
|
+
error: 'DSH 这次没能换上新版,当前仍是旧版,功能照常。重启 DSH 就会用上新版。 / DSH could not switch to the new version this time. The old one keeps working. Restart DSH to use the new one.',
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
case 'failed':
|
|
285
|
+
return {
|
|
286
|
+
status: 502,
|
|
287
|
+
body: {
|
|
288
|
+
ok: false,
|
|
289
|
+
outcome: 'failed',
|
|
290
|
+
serving: outcome.serving,
|
|
291
|
+
detail: outcome.detail,
|
|
292
|
+
error: '切换到新版时出了问题,市场暂时不可用。重启 DSH 即可恢复。 / Switching to the new version went wrong and the market is unavailable. Restart DSH to recover.',
|
|
293
|
+
},
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
}
|
package/lib/sources.js
CHANGED
|
@@ -504,9 +504,27 @@ export function pinnedAllowBuildsKeys(name, spec, sha) {
|
|
|
504
504
|
* @returns the target spec, or null when the source url is unsupported.
|
|
505
505
|
*/
|
|
506
506
|
export function installTargetFor(entry) {
|
|
507
|
+
// An entry from an offline bundle installs from the package file that came
|
|
508
|
+
// with it. The path was resolved and checked when the catalog was read.
|
|
509
|
+
if (entry.source !== undefined && typeof entry.offlineTarball === 'string' && entry.offlineTarball !== '')
|
|
510
|
+
return `file:${entry.offlineTarball}`;
|
|
511
|
+
const hasNpm = typeof entry.npm === 'string' && NPM_NAME_RE.test(entry.npm);
|
|
512
|
+
if (entry.source !== undefined && !hasNpm && typeof entry.git === 'string' && /^git\+(?:https?|ssh):\/\//u.test(entry.git))
|
|
513
|
+
return entry.git;
|
|
507
514
|
const source = parseSourceUrl(entry.url);
|
|
508
|
-
if (source === null)
|
|
509
|
-
|
|
515
|
+
if (source === null) {
|
|
516
|
+
// An entry from a catalog the organization or the user added may link to
|
|
517
|
+
// anywhere (an internal repository host, a docs page). `source` is stamped
|
|
518
|
+
// by the market after merging and never read from a catalog's own text, so
|
|
519
|
+
// its presence means "a configured catalog listed this"; the install is
|
|
520
|
+
// then an ordinary registry install of that package.
|
|
521
|
+
if (entry.source === undefined)
|
|
522
|
+
return null;
|
|
523
|
+
if (typeof entry.npm === 'string' && NPM_NAME_RE.test(entry.npm))
|
|
524
|
+
return entry.npm;
|
|
525
|
+
// No registry name: an internal repository, as normalized when the catalog was read.
|
|
526
|
+
return typeof entry.git === 'string' && /^git\+(?:https?|ssh):\/\//u.test(entry.git) ? entry.git : null;
|
|
527
|
+
}
|
|
510
528
|
// The market manages its own installation and is published to npm. Keep
|
|
511
529
|
// legacy catalog snapshots and self-update requests on that prebuilt
|
|
512
530
|
// registry package even when the entry has not yet gained its generated
|
package/lib/telemetry.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getPolicy } from './policy.js';
|
|
1
2
|
import { randomBytes } from 'node:crypto';
|
|
2
3
|
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
3
4
|
import { dirname, join } from 'node:path';
|
|
@@ -49,6 +50,9 @@ export function setTelemetryEnabled(enabled, file = telemetryFile()) {
|
|
|
49
50
|
const utcDay = (now) => new Date(now).toISOString().slice(0, 10);
|
|
50
51
|
/** Why nothing may be sent right now, or null. Explicit opt-outs always win. */
|
|
51
52
|
export function suppressedBy(env) {
|
|
53
|
+
// An organization that forbids the count wins over the user's own switch.
|
|
54
|
+
if (getPolicy().telemetry === false)
|
|
55
|
+
return 'organization policy';
|
|
52
56
|
if (env.DSH_CRATE_TELEMETRY === '0')
|
|
53
57
|
return 'DSH_CRATE_TELEMETRY=0';
|
|
54
58
|
if (env.DO_NOT_TRACK === '1')
|
|
@@ -74,4 +74,4 @@ file?: string): Promise<{
|
|
|
74
74
|
* @param pkg - package name.
|
|
75
75
|
* @param file - exact entry name inside the tarball (`package/cordis.patch.yml`).
|
|
76
76
|
*/
|
|
77
|
-
export declare function packageFileFromRegistry(
|
|
77
|
+
export declare function packageFileFromRegistry(base: string, pkg: string, file: string): Promise<Buffer | null>;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalog sources beyond the public one: an organization's own list of
|
|
3
|
+
* plugins, in the same shape as the public `plugins.json`.
|
|
4
|
+
*
|
|
5
|
+
* Where they come from:
|
|
6
|
+
* - the policy file's `catalogs` (locked; the user cannot remove them), and
|
|
7
|
+
* - the user's own, added in the market's settings (stored in the profile's
|
|
8
|
+
* market state) unless the policy says `"userCatalogs": false`.
|
|
9
|
+
*
|
|
10
|
+
* This module holds everything about them EXCEPT the public catalog, whose
|
|
11
|
+
* fetch path (and its 304 validator) stays in src/registry.ts exactly as it
|
|
12
|
+
* was. `loadRegistry` there calls in here only when something is configured;
|
|
13
|
+
* with nothing configured it never gets here, which is what makes "no policy,
|
|
14
|
+
* no user catalogs" the same requests as before.
|
|
15
|
+
*
|
|
16
|
+
* ## Failure
|
|
17
|
+
*
|
|
18
|
+
* A source that cannot be read costs the user that source, not the market:
|
|
19
|
+
* every source is fetched on its own, what loaded is merged, and each failure
|
|
20
|
+
* is reported per source as a code plus detail (`SourceError`) for the UI to
|
|
21
|
+
* say in the user's language. Secrets: header values and `.npmrc` tokens are
|
|
22
|
+
* never put into a message — {@link scrub} removes any configured header value
|
|
23
|
+
* and URL credentials from every failure text before it leaves this module.
|
|
24
|
+
*/
|
|
25
|
+
import { type LocalizedLabel, type Policy } from './policy.ts';
|
|
26
|
+
import type { Registry, RegistryPlugin } from './registry.ts';
|
|
27
|
+
/** A saved user catalog: a URL or an absolute file path, nothing secret. */
|
|
28
|
+
export interface UserCatalog {
|
|
29
|
+
id: string;
|
|
30
|
+
url?: string;
|
|
31
|
+
file?: string;
|
|
32
|
+
}
|
|
33
|
+
export declare const MAX_USER_CATALOGS = 8;
|
|
34
|
+
export type SourceErrorCode = 'network' | 'timeout' | 'auth' | 'http' | 'invalid' | 'empty' | 'file' | 'env' | 'policy';
|
|
35
|
+
/** What went wrong with one source: a code the UI words, plus a short detail. */
|
|
36
|
+
export interface SourceError {
|
|
37
|
+
code: SourceErrorCode;
|
|
38
|
+
detail: string;
|
|
39
|
+
}
|
|
40
|
+
/** What the settings list and the Discover banner know about one source. No secrets, ever. */
|
|
41
|
+
export interface SourceInfo {
|
|
42
|
+
id: string;
|
|
43
|
+
kind: 'public' | 'policy' | 'user';
|
|
44
|
+
label: LocalizedLabel;
|
|
45
|
+
/** Host and path (or file path) for display; no credentials, no query. */
|
|
46
|
+
where: string;
|
|
47
|
+
/** `off`: the policy turned the public catalog off. */
|
|
48
|
+
status: 'ok' | 'failed' | 'off';
|
|
49
|
+
count: number;
|
|
50
|
+
/** Entries skipped because they were not usable (no name, no link, no category). */
|
|
51
|
+
skipped: number;
|
|
52
|
+
error?: SourceError;
|
|
53
|
+
/** Set by the organization: the user cannot remove or switch it. */
|
|
54
|
+
locked: boolean;
|
|
55
|
+
/** Its entries install from package files shipped with it, not from a registry. */
|
|
56
|
+
offline?: true;
|
|
57
|
+
}
|
|
58
|
+
/** The public catalog's label, as plugin cards and the source list show it. */
|
|
59
|
+
export declare const PUBLIC_LABEL: LocalizedLabel;
|
|
60
|
+
/** A source ready to fetch: what to read, and the headers it needs. */
|
|
61
|
+
export interface ExtraSource {
|
|
62
|
+
id: string;
|
|
63
|
+
kind: 'policy' | 'user';
|
|
64
|
+
label: LocalizedLabel;
|
|
65
|
+
url?: string;
|
|
66
|
+
file?: string;
|
|
67
|
+
/** Unexpanded; `${ENV}` is expanded per request. */
|
|
68
|
+
headers: Record<string, string>;
|
|
69
|
+
}
|
|
70
|
+
/** Replace the user's saved catalogs (read from the profile's market state). */
|
|
71
|
+
export declare function setUserCatalogs(list: readonly UserCatalog[]): void;
|
|
72
|
+
export declare function currentUserCatalogs(): readonly UserCatalog[];
|
|
73
|
+
export interface CatalogConfig {
|
|
74
|
+
policy: Policy;
|
|
75
|
+
publicEnabled: boolean;
|
|
76
|
+
extras: ExtraSource[];
|
|
77
|
+
/** Anything beyond "the public catalog and nothing else". */
|
|
78
|
+
active: boolean;
|
|
79
|
+
/** Changes whenever what would be fetched (or filtered) changes. */
|
|
80
|
+
signature: string;
|
|
81
|
+
}
|
|
82
|
+
/** Host name or file name: a label for a catalog that did not name itself. */
|
|
83
|
+
export declare function fallbackLabel(entry: Pick<UserCatalog, 'url' | 'file'>): LocalizedLabel;
|
|
84
|
+
/** The sources and filters in force right now. Cheap: the policy is cached. */
|
|
85
|
+
export declare function catalogConfig(): CatalogConfig;
|
|
86
|
+
/** Remove URL credentials, query strings, and any configured header value from a text. */
|
|
87
|
+
export declare function scrub(text: string, secrets?: readonly string[]): string;
|
|
88
|
+
/** The plain location of a source for display: host and path, or the file path. */
|
|
89
|
+
export declare function displayWhere(source: Pick<ExtraSource, 'url' | 'file'>): string;
|
|
90
|
+
/** Drop every validator, so the next read of each source is unconditional. */
|
|
91
|
+
export declare function forgetExtraCatalogs(): void;
|
|
92
|
+
/**
|
|
93
|
+
* A git source an entry may install from, as pnpm's spec, or null.
|
|
94
|
+
*
|
|
95
|
+
* https and ssh remotes (plus plain http for hosts that only speak it), an
|
|
96
|
+
* optional `#ref`. A password in the address is refused: a catalog is shared
|
|
97
|
+
* text, and a secret does not belong in it. ssh keys are the machine's own.
|
|
98
|
+
*/
|
|
99
|
+
export declare function normalizeGitSpec(value: unknown): string | null;
|
|
100
|
+
export interface ExtraResult {
|
|
101
|
+
info: SourceInfo;
|
|
102
|
+
registry: Registry | null;
|
|
103
|
+
}
|
|
104
|
+
/** Read one source and report it; never throws. */
|
|
105
|
+
export declare function loadExtra(source: ExtraSource, options?: {
|
|
106
|
+
fresh?: boolean;
|
|
107
|
+
env?: NodeJS.ProcessEnv;
|
|
108
|
+
}): Promise<ExtraResult>;
|
|
109
|
+
/** Whether the policy's `allow` list lets this entry be shown and installed. */
|
|
110
|
+
export declare function entryAllowed(entry: Pick<RegistryPlugin, 'name' | 'npm'>, allow: readonly string[] | null): boolean;
|
|
111
|
+
export interface MergeInput {
|
|
112
|
+
/** The public catalog, or null when it is off or failed. */
|
|
113
|
+
publicRegistry: Registry | null;
|
|
114
|
+
publicInfo: SourceInfo | null;
|
|
115
|
+
/** In priority order: policy catalogs, then the user's. */
|
|
116
|
+
extras: readonly {
|
|
117
|
+
info: SourceInfo;
|
|
118
|
+
registry: Registry | null;
|
|
119
|
+
}[];
|
|
120
|
+
allow: readonly string[] | null;
|
|
121
|
+
/** The administrator's exact-version pins by npm package name. */
|
|
122
|
+
pins?: Readonly<Record<string, string>>;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Combine the sources. Pure.
|
|
126
|
+
*
|
|
127
|
+
* - The same repository URL, or the same npm package, in two sources: the
|
|
128
|
+
* private one wins (earlier private sources before later ones). One plugin
|
|
129
|
+
* must not appear as two cards because two catalogs both list it.
|
|
130
|
+
* - Private entries come first: they are why the organization set this up.
|
|
131
|
+
* - Categories merge; a category id a private entry uses that nothing
|
|
132
|
+
* describes shows under its own id.
|
|
133
|
+
* - `source` is stamped here on every private entry, and removed from public
|
|
134
|
+
* ones, so a catalog cannot pass itself off as another source.
|
|
135
|
+
* - `allow` filters the union; `pins` fix the version an entry shows and installs.
|
|
136
|
+
*/
|
|
137
|
+
export declare function mergeSources(input: MergeInput): Registry;
|
|
138
|
+
/** What a person typed into the add box, as a source, or why not. */
|
|
139
|
+
export declare function parseCatalogInput(text: string): {
|
|
140
|
+
url?: string;
|
|
141
|
+
file?: string;
|
|
142
|
+
} | {
|
|
143
|
+
error: 'empty' | 'credentials' | 'unrecognized';
|
|
144
|
+
};
|
|
145
|
+
/** A stable id for a user catalog, so adding the same thing twice is one entry. */
|
|
146
|
+
export declare function userCatalogId(entry: {
|
|
147
|
+
url?: string;
|
|
148
|
+
file?: string;
|
|
149
|
+
}): string;
|
|
150
|
+
/** Read a candidate once, for the add box: the count on success, the reason on failure. */
|
|
151
|
+
export declare function probeCatalog(entry: {
|
|
152
|
+
url?: string;
|
|
153
|
+
file?: string;
|
|
154
|
+
}, env?: NodeJS.ProcessEnv): Promise<ExtraResult>;
|
|
155
|
+
/** The shape the id of a saved catalog must have; stored state is untrusted. */
|
|
156
|
+
export declare function validUserCatalog(value: unknown): UserCatalog | null;
|
package/lib/types/hot.d.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
import { type UpdateCheckIntervalHours } from './update-notification-settings.ts';
|
|
19
19
|
import { type Channel } from './channels.ts';
|
|
20
20
|
import { type Region } from './regions.ts';
|
|
21
|
+
import { type UserCatalog } from './catalogs.ts';
|
|
21
22
|
interface HotRow {
|
|
22
23
|
id: string;
|
|
23
24
|
name: string;
|
|
@@ -191,6 +192,14 @@ export interface MarketState {
|
|
|
191
192
|
* pre-install list, unlike groups/notes which target installed packages.
|
|
192
193
|
*/
|
|
193
194
|
favorites?: string[];
|
|
195
|
+
/**
|
|
196
|
+
* Extra catalogs the user added in the settings card: a URL or an absolute
|
|
197
|
+
* file path each, nothing secret. Optional on the way in like `favorites`
|
|
198
|
+
* (omitted means "the caller has nothing to say"); an explicit empty list
|
|
199
|
+
* is how the last one is removed. Ignored while the policy sets
|
|
200
|
+
* `userCatalogs: false`.
|
|
201
|
+
*/
|
|
202
|
+
userCatalogs?: UserCatalog[];
|
|
194
203
|
/**
|
|
195
204
|
* Package names the user hid from Discover and Themes (#657).
|
|
196
205
|
*
|
|
@@ -241,11 +250,10 @@ export interface MarketState {
|
|
|
241
250
|
/**
|
|
242
251
|
* Whether the market still lists its own page in Settings (#1015).
|
|
243
252
|
*
|
|
244
|
-
* Absent means
|
|
245
|
-
*
|
|
246
|
-
* 0.2.1
|
|
247
|
-
*
|
|
248
|
-
* field so a later default change still reaches users who never touched it.
|
|
253
|
+
* Absent means "not chosen": hidden wherever the Plugins page tab or the
|
|
254
|
+
* sidebar row is a way in, shown where it is the only one (a host before
|
|
255
|
+
* 0.2.1). Both answers are stored, like `sidebarEntry`, so turning it on
|
|
256
|
+
* keeps it on.
|
|
249
257
|
*/
|
|
250
258
|
settingsSection?: boolean;
|
|
251
259
|
/**
|
|
@@ -421,6 +429,19 @@ export declare function recordInstalled(profileDir: string, names: readonly stri
|
|
|
421
429
|
export declare function recordUpdated(profileDir: string, names: readonly string[], at?: Date): void;
|
|
422
430
|
/** The package is gone: its stamps must not outlive it. */
|
|
423
431
|
export declare function forgetInstalled(profileDir: string, name: string): void;
|
|
432
|
+
/**
|
|
433
|
+
* Forget every hot mount. Called when the market's routes unload.
|
|
434
|
+
*
|
|
435
|
+
* The trees these tables point at hang off the market's own instance, so
|
|
436
|
+
* they go down with it. The tables do not: they are module state, and a
|
|
437
|
+
* module the host did not replace — its file did not change — is still
|
|
438
|
+
* loaded when the next instance starts (src/self-reload.ts). Left as they
|
|
439
|
+
* are, the new instance would believe a plugin is still mounted that its
|
|
440
|
+
* predecessor took down: its shim would never be mounted again, and
|
|
441
|
+
* "switch off" would dispose a handle that is already gone while the plugin,
|
|
442
|
+
* brought back by the host's own composition, kept running.
|
|
443
|
+
*/
|
|
444
|
+
export declare function resetHotState(): void;
|
|
424
445
|
/** Package names whose hot-mount trees are still attached and can be removed. */
|
|
425
446
|
export declare function listHotMounts(): string[];
|
|
426
447
|
/** Report a failed child only when it belongs to this package's hot-mount tree. */
|
package/lib/types/http.d.ts
CHANGED
|
@@ -110,5 +110,7 @@ export declare function trustedAuthority(host: string, trustedHosts: readonly st
|
|
|
110
110
|
*/
|
|
111
111
|
export declare function loopbackAuthority(host: string | undefined): boolean;
|
|
112
112
|
export declare function sameOrigin(request: IncomingMessage): boolean;
|
|
113
|
+
/** Read a request body as bytes, refusing anything over `maxBytes` (stops reading as soon as it is over). */
|
|
114
|
+
export declare function readRawBody(request: IncomingMessage, maxBytes: number): Promise<Buffer>;
|
|
113
115
|
/** Read and parse a JSON request body, rejecting anything over 4 KiB. */
|
|
114
116
|
export declare function readJsonBody(request: IncomingMessage, maxBytes?: number): Promise<unknown>;
|
package/lib/types/net.d.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* - On Node 25, `setGlobalDispatcher` from the undici package does not
|
|
17
17
|
* steer global fetch. With a dispatcher installed that way, a global
|
|
18
18
|
* fetch produced no CONNECT at a local proxy, while undici's own fetch
|
|
19
|
-
* produced `CONNECT
|
|
19
|
+
* produced `CONNECT dshcrate.com:443`.
|
|
20
20
|
* - On Node 22 the two stacks share one symbol (#742). Global fetch reads
|
|
21
21
|
* `Symbol.for('undici.globalDispatcher.1')`. The host's first import of
|
|
22
22
|
* undici 8 (`web_fetch`) finds `.2` empty, installs its dispatcher, and
|
|
@@ -86,7 +86,7 @@ export declare function configuredProxy(): string | null;
|
|
|
86
86
|
* through this module's own agent when it has none. Both paths pass the
|
|
87
87
|
* dispatcher described on this module.
|
|
88
88
|
*/
|
|
89
|
-
export declare function marketFetch(url: string,
|
|
89
|
+
export declare function marketFetch(url: string, given?: {
|
|
90
90
|
signal?: AbortSignal;
|
|
91
91
|
headers?: Record<string, string>;
|
|
92
92
|
method?: 'GET' | 'HEAD' | 'POST';
|