@hop-top/kit 0.5.0-alpha.10 → 0.5.0-alpha.11
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/CHANGELOG.md +24 -0
- package/README.md +4 -4
- package/dist/alias.js +3 -3
- package/dist/{chunk-T46YPHKN.js → chunk-4FMALWYB.js} +45 -45
- package/dist/{chunk-WWUKGJB5.js → chunk-4GPCWBBA.js} +60 -7
- package/dist/{chunk-JZ22ZUFA.js → chunk-65VPWMPV.js} +4 -1
- package/dist/{chunk-YGYQUFGY.js → chunk-BXJ2J27B.js} +10 -10
- package/dist/chunk-CXBZTONY.js +65 -0
- package/dist/{chunk-K7VCKJ27.js → chunk-EE4M2UOF.js} +8 -4
- package/dist/{chunk-72XZWQX7.js → chunk-GFRAYYRD.js} +475 -162
- package/dist/{chunk-2JV3A7OP.js → chunk-IVQHLI56.js} +6 -10
- package/dist/{chunk-NLMS7HGP.js → chunk-IZ6Q4SE3.js} +3 -3
- package/dist/chunk-MFVUENZR.js +1549 -0
- package/dist/{chunk-BLS2OBIQ.js → chunk-ML7D5DWY.js} +2 -2
- package/dist/{chunk-GGWJRWWA.js → chunk-RYKLVOVK.js} +7 -7
- package/dist/{chunk-QPUCXQXT.js → chunk-T6OZMPPV.js} +2 -1
- package/dist/{chunk-MUIQVU6E.js → chunk-VGOA5Y7S.js} +11 -11
- package/dist/{chunk-ABQSE7AI.js → chunk-VWGP4NVJ.js} +38 -38
- package/dist/cli-RSRk1M36.d.ts +707 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +60 -5
- package/dist/completion.js +2 -2
- package/dist/{index-DCe1boBz.d.ts → index-CGE5yLBj.d.ts} +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +22 -22
- package/dist/llm-anthropic.js +5 -5
- package/dist/llm-openai.js +5 -5
- package/dist/llm.js +3 -3
- package/dist/log.js +3 -3
- package/dist/mcp/index.js +3 -2
- package/dist/output.js +2 -4
- package/dist/routellm.js +6 -6
- package/dist/toolspec.js +4 -4
- package/dist/tui/index.d.ts +2 -2
- package/dist/tui/index.js +3 -3
- package/dist/{upgrade-p_zh3pvV.d.ts → upgrade-C3RWGwRi.d.ts} +23 -0
- package/dist/upgrade.d.ts +1 -1
- package/dist/upgrade.js +2 -2
- package/package.json +1 -1
- package/src/cli.root.conformance.test.ts +166 -0
- package/src/cli.ts +39 -0
- package/src/completion.conformance.test.ts +3 -2
- package/src/completion.test.ts +12 -0
- package/src/completion.ts +8 -3
- package/src/delegation.test.ts +299 -0
- package/src/delegation.ts +807 -0
- package/src/mcp/bridge.test.ts +28 -0
- package/src/mcp/bridge.ts +5 -10
- package/src/plan.ts +89 -0
- package/src/policy.ts +472 -0
- package/src/sideeffect.ts +105 -0
- package/src/toolspec-manifest.ts +7 -2
- package/src/upgrade.test.ts +291 -0
- package/src/upgrade.ts +116 -7
- package/dist/chunk-GHPJJV5L.js +0 -613
- package/dist/chunk-IXVQ62NZ.js +0 -334
- package/dist/cli-C5B5tnpA.d.ts +0 -400
- package/dist/{chunk-PSFYJGDG.js → chunk-K7E34Q3A.js} +2 -2
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module sideeffect
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* The `kit/side-effect` vocabulary and the one name-inference table every
|
|
6
|
+
* kit gate and projection reads. Mirrors Go's `cli.SideEffect` tiers and
|
|
7
|
+
* `cmdmeta.InferredDestructive`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Annotation key a command declares its side-effect tier under. */
|
|
11
|
+
export const SIDE_EFFECT_ANNOTATION = 'kit/side-effect';
|
|
12
|
+
|
|
13
|
+
/** The closed set of declarable tiers (legacy 4-tier plus the 6-tier ladder). */
|
|
14
|
+
export const SIDE_EFFECT_TIERS = [
|
|
15
|
+
'read',
|
|
16
|
+
'write',
|
|
17
|
+
'write-local',
|
|
18
|
+
'write-shared',
|
|
19
|
+
'destructive',
|
|
20
|
+
'destructive-local',
|
|
21
|
+
'destructive-shared',
|
|
22
|
+
'interactive',
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
export type SideEffect = (typeof SIDE_EFFECT_TIERS)[number];
|
|
26
|
+
|
|
27
|
+
/** The tier an unannotated command with a destructive leaf name is gated as. */
|
|
28
|
+
export const INFERRED_TIER: SideEffect = 'destructive-shared';
|
|
29
|
+
|
|
30
|
+
const WRITE_LIKE = new Set<string>(['write', 'write-local', 'write-shared']);
|
|
31
|
+
const DESTRUCTIVE_LIKE = new Set<string>([
|
|
32
|
+
'destructive',
|
|
33
|
+
'destructive-local',
|
|
34
|
+
'destructive-shared',
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
/** True for any tier that mutates state without irreversible loss. */
|
|
38
|
+
export function isWriteLike(tier: string): boolean {
|
|
39
|
+
return WRITE_LIKE.has(tier);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** True for any tier that performs an irreversible mutation. */
|
|
43
|
+
export function isDestructiveLike(tier: string): boolean {
|
|
44
|
+
return DESTRUCTIVE_LIKE.has(tier);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** True when `tier` names a declarable side-effect tier. */
|
|
48
|
+
export function isValidSideEffect(tier: string): boolean {
|
|
49
|
+
return (SIDE_EFFECT_TIERS as readonly string[]).includes(tier);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The one table of leaf names kit treats as destructive when a command
|
|
54
|
+
* carries no `kit/side-effect` annotation. Every gate and projection in
|
|
55
|
+
* this SDK reads it through {@link isDestructiveName} or
|
|
56
|
+
* {@link inferredDestructive}; none keeps its own copy.
|
|
57
|
+
*/
|
|
58
|
+
const DESTRUCTIVE_NAMES: ReadonlySet<string> = new Set([
|
|
59
|
+
'delete',
|
|
60
|
+
'remove',
|
|
61
|
+
'rm',
|
|
62
|
+
'destroy',
|
|
63
|
+
'purge',
|
|
64
|
+
'drop',
|
|
65
|
+
]);
|
|
66
|
+
|
|
67
|
+
/** True when `name` is in the destructive-name table. */
|
|
68
|
+
export function isDestructiveName(name: string): boolean {
|
|
69
|
+
return DESTRUCTIVE_NAMES.has(name);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The table's names, sorted. */
|
|
73
|
+
export function destructiveNames(): string[] {
|
|
74
|
+
return [...DESTRUCTIVE_NAMES].sort();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* True when a command is destructive by name alone: its annotation is
|
|
79
|
+
* absent or empty and its leaf name is in the table. An explicit
|
|
80
|
+
* annotation, valid or not, always wins: a malformed value is a
|
|
81
|
+
* declaration defect, never inferred over.
|
|
82
|
+
*/
|
|
83
|
+
export function inferredDestructive(
|
|
84
|
+
name: string,
|
|
85
|
+
annotations: Readonly<Record<string, string>> | undefined,
|
|
86
|
+
): boolean {
|
|
87
|
+
const declared = annotations?.[SIDE_EFFECT_ANNOTATION];
|
|
88
|
+
if (declared !== undefined && declared !== '') return false;
|
|
89
|
+
return isDestructiveName(name);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The tier the delegation gates act on: the declared `kit/side-effect`,
|
|
94
|
+
* else {@link INFERRED_TIER} for a destructive-named command. Undefined
|
|
95
|
+
* when neither says anything. A declared value, valid or malformed, is
|
|
96
|
+
* never replaced.
|
|
97
|
+
*/
|
|
98
|
+
export function gateSideEffect(
|
|
99
|
+
name: string,
|
|
100
|
+
annotations: Readonly<Record<string, string>> | undefined,
|
|
101
|
+
): string | undefined {
|
|
102
|
+
if (inferredDestructive(name, annotations)) return INFERRED_TIER;
|
|
103
|
+
const declared = annotations?.[SIDE_EFFECT_ANNOTATION];
|
|
104
|
+
return declared === undefined ? undefined : declared;
|
|
105
|
+
}
|
package/src/toolspec-manifest.ts
CHANGED
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
walkCommands,
|
|
27
27
|
type Annotations,
|
|
28
28
|
} from "./toolspec-internal.js";
|
|
29
|
+
import { inferredDestructive } from "./sideeffect.js";
|
|
29
30
|
|
|
30
31
|
/** Env var a harness sets to request a manifest schema version. */
|
|
31
32
|
export const SCHEMA_VERSION_ENV = "KIT_TOOLSPEC_SCHEMA";
|
|
@@ -233,13 +234,17 @@ const DRY_RUN_SIDE_EFFECTS = new Set([
|
|
|
233
234
|
"destructive-shared",
|
|
234
235
|
]);
|
|
235
236
|
|
|
236
|
-
|
|
237
|
+
// Go cmdmeta.IsDryRunSupported: an explicit kit/dry-run wins, then an
|
|
238
|
+
// unannotated destructive-named leaf (inferred destructive) is supported,
|
|
239
|
+
// then the declared side-effect tier decides.
|
|
240
|
+
function dryRunSupported(name: string, ann: Annotations): boolean {
|
|
237
241
|
switch (ann["kit/dry-run"]) {
|
|
238
242
|
case "opted-out":
|
|
239
243
|
return false;
|
|
240
244
|
case "supported":
|
|
241
245
|
return true;
|
|
242
246
|
}
|
|
247
|
+
if (inferredDestructive(name, ann)) return true;
|
|
243
248
|
return DRY_RUN_SIDE_EFFECTS.has(ann["kit/side-effect"] ?? "");
|
|
244
249
|
}
|
|
245
250
|
|
|
@@ -362,7 +367,7 @@ function manifestCommand(
|
|
|
362
367
|
if (ann["kit/passthrough"] === "true") out.passthrough = true;
|
|
363
368
|
const token = ann["kit/destructive-token"];
|
|
364
369
|
if (token === "required" || token === "true") out.destructive_token_required = true;
|
|
365
|
-
out.dry_run_supported = dryRunSupported(ann);
|
|
370
|
+
out.dry_run_supported = dryRunSupported(cmd.name(), ann);
|
|
366
371
|
if (ann["kit/dry-run-rationale"]) out.dry_run_rationale = ann["kit/dry-run-rationale"];
|
|
367
372
|
if (reserved) out.reserved = true;
|
|
368
373
|
if (hidden) out.hidden = true;
|
package/src/upgrade.test.ts
CHANGED
|
@@ -296,3 +296,294 @@ describe('createChecker', () => {
|
|
|
296
296
|
expect(out.value()).toContain('v2.0.0');
|
|
297
297
|
});
|
|
298
298
|
});
|
|
299
|
+
|
|
300
|
+
// ─── component tags and prereleases ──────────────────────────────────────────
|
|
301
|
+
|
|
302
|
+
interface Rel {
|
|
303
|
+
tag_name: string;
|
|
304
|
+
prerelease?: boolean;
|
|
305
|
+
draft?: boolean;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const API = 'https://api.github.com/repos/acme/mono/releases';
|
|
309
|
+
|
|
310
|
+
/** Serve `pages[n-1]` for `?page=n` on the list endpoint; 404 elsewhere. */
|
|
311
|
+
function mockListing(pages: Rel[][]) {
|
|
312
|
+
return vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {
|
|
313
|
+
const url = new URL(String(input));
|
|
314
|
+
if (url.origin + url.pathname !== API) return jsonResponse({}, 404);
|
|
315
|
+
const page = Number(url.searchParams.get('page') ?? '1');
|
|
316
|
+
return jsonResponse(pages[page - 1] ?? []);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
describe('createChecker: default mode', () => {
|
|
321
|
+
let tmpDir: string;
|
|
322
|
+
beforeEach(() => {
|
|
323
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'upgrade-test-'));
|
|
324
|
+
vi.restoreAllMocks();
|
|
325
|
+
});
|
|
326
|
+
afterEach(() => {
|
|
327
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
328
|
+
vi.restoreAllMocks();
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('reads releases/latest only, with the latest-release link and bare cache', async () => {
|
|
332
|
+
const spy = vi
|
|
333
|
+
.spyOn(globalThis, 'fetch')
|
|
334
|
+
.mockResolvedValue(jsonResponse({ tag_name: 'v2.0.0' }));
|
|
335
|
+
const out = new StringStream();
|
|
336
|
+
await createChecker({
|
|
337
|
+
name: 'my-tool',
|
|
338
|
+
currentVersion: '1.0.0',
|
|
339
|
+
owner: 'acme',
|
|
340
|
+
repo: 'mono',
|
|
341
|
+
stateDir: tmpDir,
|
|
342
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
343
|
+
|
|
344
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
345
|
+
expect(String(spy.mock.calls[0][0])).toBe(`${API}/latest`);
|
|
346
|
+
expect(out.value()).toBe(
|
|
347
|
+
'\nA new release of my-tool is available: v1.0.0 → v2.0.0\n' +
|
|
348
|
+
'https://github.com/acme/mono/releases/latest\n',
|
|
349
|
+
);
|
|
350
|
+
const cache = JSON.parse(
|
|
351
|
+
fs.readFileSync(path.join(tmpDir, '.upgrade-my-tool-cache.json'), 'utf8'),
|
|
352
|
+
);
|
|
353
|
+
expect(Object.keys(cache)).toEqual(['version', 'checkedAt']);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('stays silent for a component tag when no tagPrefix is set', async () => {
|
|
357
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
|
358
|
+
jsonResponse({ tag_name: 'xray/v0.2.0' }),
|
|
359
|
+
);
|
|
360
|
+
const out = new StringStream();
|
|
361
|
+
await createChecker({
|
|
362
|
+
name: 'xray',
|
|
363
|
+
currentVersion: '0.1.0',
|
|
364
|
+
owner: 'acme',
|
|
365
|
+
repo: 'mono',
|
|
366
|
+
stateDir: tmpDir,
|
|
367
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
368
|
+
expect(out.value()).toBe('');
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
describe('createChecker: tagPrefix and includePrereleases', () => {
|
|
373
|
+
let tmpDir: string;
|
|
374
|
+
beforeEach(() => {
|
|
375
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'upgrade-test-'));
|
|
376
|
+
vi.restoreAllMocks();
|
|
377
|
+
});
|
|
378
|
+
afterEach(() => {
|
|
379
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
380
|
+
vi.restoreAllMocks();
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
async function run(
|
|
384
|
+
currentVersion: string,
|
|
385
|
+
extra: { tagPrefix?: string; includePrereleases?: boolean },
|
|
386
|
+
): Promise<string> {
|
|
387
|
+
const out = new StringStream();
|
|
388
|
+
await createChecker({
|
|
389
|
+
name: 'xray',
|
|
390
|
+
currentVersion,
|
|
391
|
+
owner: 'acme',
|
|
392
|
+
repo: 'mono',
|
|
393
|
+
stateDir: tmpDir,
|
|
394
|
+
...extra,
|
|
395
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
396
|
+
return out.value();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
it('notifies for a prefixed tag and links the exact release', async () => {
|
|
400
|
+
mockListing([[{ tag_name: 'xray/v0.2.0' }]]);
|
|
401
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe(
|
|
402
|
+
'\nA new release of xray is available: v0.1.0 → v0.2.0\n' +
|
|
403
|
+
'https://github.com/acme/mono/releases/tag/xray/v0.2.0\n',
|
|
404
|
+
);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it('ignores other components even when their version is higher', async () => {
|
|
408
|
+
mockListing([[{ tag_name: 'kit/v9.0.0' }, { tag_name: 'xray/v0.2.0' }]]);
|
|
409
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('v0.1.0 → v0.2.0');
|
|
410
|
+
|
|
411
|
+
fs.rmSync(path.join(tmpDir, '.upgrade-xray-cache.json'));
|
|
412
|
+
mockListing([[{ tag_name: 'kit/v9.0.0' }, { tag_name: 'xray-cli/v9.0.0' }]]);
|
|
413
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('picks the highest semver, not the first listed', async () => {
|
|
417
|
+
mockListing([
|
|
418
|
+
[{ tag_name: 'xray/v0.2.1' }, { tag_name: 'xray/v0.10.0' }, { tag_name: 'xray/v0.9.0' }],
|
|
419
|
+
]);
|
|
420
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('→ v0.10.0');
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it('skips drafts', async () => {
|
|
424
|
+
mockListing([[{ tag_name: 'xray/v0.3.0', draft: true }, { tag_name: 'xray/v0.2.0' }]]);
|
|
425
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('→ v0.2.0');
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it('does not nudge a stable user onto a prerelease without includePrereleases', async () => {
|
|
429
|
+
mockListing([
|
|
430
|
+
[
|
|
431
|
+
{ tag_name: 'xray/v0.3.0-rc.1', prerelease: true },
|
|
432
|
+
{ tag_name: 'xray/v0.3.0-rc.0' }, // not flagged by GitHub; semver says prerelease
|
|
433
|
+
],
|
|
434
|
+
]);
|
|
435
|
+
expect(await run('0.2.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it('offers a newer prerelease to a stable user when includePrereleases is on', async () => {
|
|
439
|
+
mockListing([[{ tag_name: 'xray/v0.3.0-rc.1', prerelease: true }]]);
|
|
440
|
+
expect(await run('0.2.0', { tagPrefix: 'xray/v', includePrereleases: true })).toContain(
|
|
441
|
+
'v0.2.0 → v0.3.0-rc.1',
|
|
442
|
+
);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it('shows a prerelease user the newer prerelease', async () => {
|
|
446
|
+
mockListing([
|
|
447
|
+
[
|
|
448
|
+
{ tag_name: 'xray/v0.1.0-rc.1', prerelease: true },
|
|
449
|
+
{ tag_name: 'xray/v0.1.0-rc.0', prerelease: true },
|
|
450
|
+
],
|
|
451
|
+
]);
|
|
452
|
+
const o = { tagPrefix: 'xray/v', includePrereleases: true };
|
|
453
|
+
expect(await run('0.1.0-rc.0', o)).toContain('v0.1.0-rc.0 → v0.1.0-rc.1');
|
|
454
|
+
fs.rmSync(path.join(tmpDir, '.upgrade-xray-cache.json'));
|
|
455
|
+
expect(await run('0.1.0-rc.1', o)).toBe('');
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it('shows a prerelease user the final stable over its own prereleases', async () => {
|
|
459
|
+
mockListing([
|
|
460
|
+
[
|
|
461
|
+
{ tag_name: 'xray/v0.1.0-rc.1', prerelease: true },
|
|
462
|
+
{ tag_name: 'xray/v0.1.0' },
|
|
463
|
+
],
|
|
464
|
+
]);
|
|
465
|
+
expect(
|
|
466
|
+
await run('0.1.0-rc.0', { tagPrefix: 'xray/v', includePrereleases: true }),
|
|
467
|
+
).toContain('v0.1.0-rc.0 → v0.1.0\n');
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('shows a prerelease user the final stable without includePrereleases', async () => {
|
|
471
|
+
mockListing([
|
|
472
|
+
[
|
|
473
|
+
{ tag_name: 'xray/v0.1.0-rc.1', prerelease: true },
|
|
474
|
+
{ tag_name: 'xray/v0.1.0' },
|
|
475
|
+
],
|
|
476
|
+
]);
|
|
477
|
+
expect(await run('0.1.0-rc.0', { tagPrefix: 'xray/v' })).toContain('v0.1.0-rc.0 → v0.1.0\n');
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it('includePrereleases alone lists releases and links the tag as published', async () => {
|
|
481
|
+
mockListing([[{ tag_name: 'v1.0.0-rc.1', prerelease: true }, { tag_name: 'v0.9.0' }]]);
|
|
482
|
+
expect(await run('0.9.0', { includePrereleases: true })).toBe(
|
|
483
|
+
'\nA new release of xray is available: v0.9.0 → v1.0.0-rc.1\n' +
|
|
484
|
+
'https://github.com/acme/mono/releases/tag/v1.0.0-rc.1\n',
|
|
485
|
+
);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it('never calls releases/latest in list mode', async () => {
|
|
489
|
+
const spy = mockListing([[{ tag_name: 'xray/v0.2.0' }]]);
|
|
490
|
+
await run('0.1.0', { tagPrefix: 'xray/v' });
|
|
491
|
+
for (const call of spy.mock.calls) {
|
|
492
|
+
expect(String(call[0])).not.toContain('/releases/latest');
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
it('pages until a short page', async () => {
|
|
497
|
+
const filler = (n: number): Rel[] =>
|
|
498
|
+
Array.from({ length: 100 }, (_, i) => ({ tag_name: `kit/v1.${n}.${i}` }));
|
|
499
|
+
const spy = mockListing([filler(0), [{ tag_name: 'xray/v0.2.0' }]]);
|
|
500
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('→ v0.2.0');
|
|
501
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
it('fetches at most 3 pages', async () => {
|
|
505
|
+
const filler = (n: number): Rel[] =>
|
|
506
|
+
Array.from({ length: 100 }, (_, i) => ({ tag_name: `kit/v1.${n}.${i}` }));
|
|
507
|
+
const spy = mockListing([filler(0), filler(1), filler(2), [{ tag_name: 'xray/v0.2.0' }]]);
|
|
508
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
509
|
+
expect(spy).toHaveBeenCalledTimes(3);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it('keeps earlier pages when a later page fails', async () => {
|
|
513
|
+
const full = Array.from({ length: 100 }, (_, i) => ({ tag_name: `xray/v0.2.${i}` }));
|
|
514
|
+
let n = 0;
|
|
515
|
+
vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
|
|
516
|
+
++n === 1 ? jsonResponse(full) : jsonResponse({}, 500),
|
|
517
|
+
);
|
|
518
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toContain('→ v0.2.99');
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it('is silent when listing fails, returns junk, or throws', async () => {
|
|
522
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse({}, 403));
|
|
523
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
524
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse({ message: 'nope' }));
|
|
525
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
526
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
|
527
|
+
jsonResponse([null, { tag_name: 7 }, { tag_name: 'xray/vbogus' }]),
|
|
528
|
+
);
|
|
529
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
530
|
+
vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('offline'));
|
|
531
|
+
expect(await run('0.1.0', { tagPrefix: 'xray/v' })).toBe('');
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
it('keeps the tag link when the result comes from a fresh cache', async () => {
|
|
535
|
+
const spy = mockListing([[{ tag_name: 'xray/v0.2.0' }]]);
|
|
536
|
+
await run('0.1.0', { tagPrefix: 'xray/v' });
|
|
537
|
+
const second = await run('0.1.0', { tagPrefix: 'xray/v' });
|
|
538
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
539
|
+
expect(second).toContain('releases/tag/xray/v0.2.0');
|
|
540
|
+
});
|
|
541
|
+
it.each([
|
|
542
|
+
['path traversal', 'xray/v/../../../../evil/repo/releases/tag/x'],
|
|
543
|
+
['another version', 'xray/v9.9.9'],
|
|
544
|
+
['another component', 'kit/v2.0.0'],
|
|
545
|
+
['not a string', 42],
|
|
546
|
+
['missing', undefined],
|
|
547
|
+
])('treats a cached tag that does not name the version as a miss: %s', async (_label, tag) => {
|
|
548
|
+
fs.writeFileSync(
|
|
549
|
+
path.join(tmpDir, '.upgrade-xray-cache.json'),
|
|
550
|
+
JSON.stringify({ version: '2.0.0', checkedAt: Date.now(), tag }),
|
|
551
|
+
);
|
|
552
|
+
const spy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
|
553
|
+
jsonResponse([{ tag_name: 'xray/v1.5.0', draft: false, prerelease: false }]),
|
|
554
|
+
);
|
|
555
|
+
const out = new StringStream();
|
|
556
|
+
await createChecker({
|
|
557
|
+
name: 'xray',
|
|
558
|
+
currentVersion: '1.0.0',
|
|
559
|
+
owner: 'acme',
|
|
560
|
+
repo: 'mono',
|
|
561
|
+
stateDir: tmpDir,
|
|
562
|
+
tagPrefix: 'xray/v',
|
|
563
|
+
cacheTTL: 60_000,
|
|
564
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
565
|
+
expect(spy).toHaveBeenCalled();
|
|
566
|
+
expect(out.value()).toContain('https://github.com/acme/mono/releases/tag/xray/v1.5.0');
|
|
567
|
+
expect(out.value()).not.toContain('evil');
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
it('uses a cached tag that names the version, without fetching', async () => {
|
|
571
|
+
fs.writeFileSync(
|
|
572
|
+
path.join(tmpDir, '.upgrade-xray-cache.json'),
|
|
573
|
+
JSON.stringify({ version: '2.0.0', checkedAt: Date.now(), tag: 'xray/v2.0.0' }),
|
|
574
|
+
);
|
|
575
|
+
const spy = vi.spyOn(globalThis, 'fetch');
|
|
576
|
+
const out = new StringStream();
|
|
577
|
+
await createChecker({
|
|
578
|
+
name: 'xray',
|
|
579
|
+
currentVersion: '1.0.0',
|
|
580
|
+
owner: 'acme',
|
|
581
|
+
repo: 'mono',
|
|
582
|
+
stateDir: tmpDir,
|
|
583
|
+
tagPrefix: 'xray/v',
|
|
584
|
+
cacheTTL: 60_000,
|
|
585
|
+
}).notifyIfAvailable(out as unknown as NodeJS.WritableStream);
|
|
586
|
+
expect(spy).not.toHaveBeenCalled();
|
|
587
|
+
expect(out.value()).toContain('https://github.com/acme/mono/releases/tag/xray/v2.0.0');
|
|
588
|
+
});
|
|
589
|
+
});
|
package/src/upgrade.ts
CHANGED
|
@@ -25,6 +25,15 @@
|
|
|
25
25
|
* \nA new release of {name} is available: v{current} → v{latest}\nhttps://github.com/{owner}/{repo}/releases/latest\n
|
|
26
26
|
* ```
|
|
27
27
|
*
|
|
28
|
+
* ### Component tags and prereleases
|
|
29
|
+
* By default the check reads `releases/latest` and strips a leading `v`.
|
|
30
|
+
* Set `tagPrefix` (e.g. `xray/v`) for a monorepo whose tags carry a
|
|
31
|
+
* component: releases are listed, tags without the prefix are ignored, and
|
|
32
|
+
* the notice links to the matching release. Set `includePrereleases` to
|
|
33
|
+
* consider prereleases; without it, prereleases are never offered. Both
|
|
34
|
+
* options list at most 3 pages of 100 releases, newest first, drafts
|
|
35
|
+
* skipped; the highest semver wins.
|
|
36
|
+
*
|
|
28
37
|
* ### Usage example
|
|
29
38
|
* ```ts
|
|
30
39
|
* import { createChecker } from '@hop-top/kit/upgrade';
|
|
@@ -75,6 +84,20 @@ export interface CheckerOptions {
|
|
|
75
84
|
* @defaultValue 10_000
|
|
76
85
|
*/
|
|
77
86
|
timeout?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Tag prefix identifying this tool's releases, stripped before the semver
|
|
89
|
+
* comparison (e.g. `'xray/v'` for tags like `xray/v0.2.0`). Tags without
|
|
90
|
+
* the prefix are not candidates. Setting it lists releases instead of
|
|
91
|
+
* reading `releases/latest`, which may belong to another component.
|
|
92
|
+
* @defaultValue `''` (no prefix; `releases/latest`)
|
|
93
|
+
*/
|
|
94
|
+
tagPrefix?: string;
|
|
95
|
+
/**
|
|
96
|
+
* Offer prereleases (e.g. `0.1.0-rc.1`) as upgrades. Setting it lists
|
|
97
|
+
* releases instead of reading `releases/latest`, which skips prereleases.
|
|
98
|
+
* @defaultValue false
|
|
99
|
+
*/
|
|
100
|
+
includePrereleases?: boolean;
|
|
78
101
|
}
|
|
79
102
|
|
|
80
103
|
/**
|
|
@@ -96,8 +119,21 @@ export interface Checker {
|
|
|
96
119
|
interface CacheEntry {
|
|
97
120
|
version: string;
|
|
98
121
|
checkedAt: number;
|
|
122
|
+
/** Tag of the cached release; written only when releases are listed. */
|
|
123
|
+
tag?: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
interface GitHubRelease {
|
|
127
|
+
tag_name?: unknown;
|
|
128
|
+
draft?: unknown;
|
|
129
|
+
prerelease?: unknown;
|
|
99
130
|
}
|
|
100
131
|
|
|
132
|
+
/** Releases requested per page when listing (GitHub's maximum). */
|
|
133
|
+
const PAGE_SIZE = 100;
|
|
134
|
+
/** Pages fetched at most when listing: bounds the check to 3 requests. */
|
|
135
|
+
const MAX_PAGES = 3;
|
|
136
|
+
|
|
101
137
|
// ─── Implementation ───────────────────────────────────────────────────────────
|
|
102
138
|
|
|
103
139
|
/**
|
|
@@ -118,19 +154,45 @@ export function createChecker(opts: CheckerOptions): Checker {
|
|
|
118
154
|
cacheTTL = 24 * 60 * 60 * 1000,
|
|
119
155
|
stateDir = os.tmpdir(),
|
|
120
156
|
timeout = 10_000,
|
|
157
|
+
tagPrefix = '',
|
|
158
|
+
includePrereleases = false,
|
|
121
159
|
} = opts;
|
|
122
160
|
|
|
161
|
+
// Default mode (no prefix, no prereleases) keeps the original
|
|
162
|
+
// `releases/latest` lookup untouched.
|
|
163
|
+
const listMode = tagPrefix !== '' || includePrereleases;
|
|
164
|
+
|
|
123
165
|
const cacheFile = path.join(stateDir, `.upgrade-${name}-cache.json`);
|
|
124
166
|
const releaseURL = `https://api.github.com/repos/${owner}/${repo}/releases/latest`;
|
|
167
|
+
const listURL = `https://api.github.com/repos/${owner}/${repo}/releases`;
|
|
125
168
|
const releasesPageURL = `https://github.com/${owner}/${repo}/releases/latest`;
|
|
126
169
|
|
|
170
|
+
// `releases/latest` can be another component's release or 404 while only
|
|
171
|
+
// prereleases exist, so in list mode link the exact release, else the list.
|
|
172
|
+
function noticeURL(tag: string | undefined): string {
|
|
173
|
+
if (!listMode) return releasesPageURL;
|
|
174
|
+
const base = `https://github.com/${owner}/${repo}/releases`;
|
|
175
|
+
if (tag === undefined) return base;
|
|
176
|
+
return `${base}/tag/${tag.split('/').map(encodeURIComponent).join('/')}`;
|
|
177
|
+
}
|
|
178
|
+
|
|
127
179
|
// ── cache helpers ──────────────────────────────────────────────────────────
|
|
128
180
|
|
|
181
|
+
function tagNamesVersion(tag: unknown, version: string): boolean {
|
|
182
|
+
return typeof tag === 'string' && tag.startsWith(tagPrefix) &&
|
|
183
|
+
semver.valid(tag.slice(tagPrefix.length).replace(/^v/, '')) === version;
|
|
184
|
+
}
|
|
185
|
+
|
|
129
186
|
function readCache(): CacheEntry | null {
|
|
130
187
|
try {
|
|
131
188
|
const raw = fs.readFileSync(cacheFile, 'utf8');
|
|
132
189
|
const parsed = JSON.parse(raw) as CacheEntry;
|
|
133
190
|
if (typeof parsed.version === 'string' && typeof parsed.checkedAt === 'number') {
|
|
191
|
+
// The tag becomes part of a link, and the cache file may sit in a
|
|
192
|
+
// shared directory: trust it only if it names this cached version.
|
|
193
|
+
if (listMode && !tagNamesVersion(parsed.tag, parsed.version)) {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
134
196
|
return parsed;
|
|
135
197
|
}
|
|
136
198
|
} catch {
|
|
@@ -139,9 +201,10 @@ export function createChecker(opts: CheckerOptions): Checker {
|
|
|
139
201
|
return null;
|
|
140
202
|
}
|
|
141
203
|
|
|
142
|
-
function writeCache(version: string): void {
|
|
204
|
+
function writeCache(version: string, tag?: string): void {
|
|
143
205
|
try {
|
|
144
206
|
const entry: CacheEntry = { version, checkedAt: Date.now() };
|
|
207
|
+
if (tag !== undefined) entry.tag = tag;
|
|
145
208
|
fs.writeFileSync(cacheFile, JSON.stringify(entry), 'utf8');
|
|
146
209
|
} catch {
|
|
147
210
|
// Cache write failure is non-fatal.
|
|
@@ -150,10 +213,16 @@ export function createChecker(opts: CheckerOptions): Checker {
|
|
|
150
213
|
|
|
151
214
|
// ── fetch latest version ───────────────────────────────────────────────────
|
|
152
215
|
|
|
153
|
-
|
|
216
|
+
interface Latest {
|
|
217
|
+
version: string;
|
|
218
|
+
tag?: string;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function fetchLatestVersion(): Promise<Latest | null> {
|
|
154
222
|
const controller = new AbortController();
|
|
155
223
|
const timer = setTimeout(() => controller.abort(), timeout);
|
|
156
224
|
try {
|
|
225
|
+
if (listMode) return await fetchHighestRelease(controller.signal);
|
|
157
226
|
const res = await fetch(releaseURL, {
|
|
158
227
|
signal: controller.signal,
|
|
159
228
|
headers: { Accept: 'application/vnd.github+json' },
|
|
@@ -163,26 +232,66 @@ export function createChecker(opts: CheckerOptions): Checker {
|
|
|
163
232
|
const tag = body.tag_name;
|
|
164
233
|
if (typeof tag !== 'string') return null;
|
|
165
234
|
// Strip leading 'v' so semver.gt works with or without prefix.
|
|
166
|
-
return tag.replace(/^v/, '');
|
|
235
|
+
return { version: tag.replace(/^v/, '') };
|
|
167
236
|
} finally {
|
|
168
237
|
clearTimeout(timer);
|
|
169
238
|
}
|
|
170
239
|
}
|
|
171
240
|
|
|
241
|
+
/**
|
|
242
|
+
* Lists releases (newest first, bounded) and returns the highest semver
|
|
243
|
+
* among non-draft releases whose tag starts with `tagPrefix`; prereleases
|
|
244
|
+
* only when `includePrereleases` is set.
|
|
245
|
+
*/
|
|
246
|
+
async function fetchHighestRelease(signal: AbortSignal): Promise<Latest | null> {
|
|
247
|
+
let best: Latest | null = null;
|
|
248
|
+
for (let page = 1; page <= MAX_PAGES; page++) {
|
|
249
|
+
const res = await fetch(`${listURL}?per_page=${PAGE_SIZE}&page=${page}`, {
|
|
250
|
+
signal,
|
|
251
|
+
headers: { Accept: 'application/vnd.github+json' },
|
|
252
|
+
});
|
|
253
|
+
if (!res.ok) {
|
|
254
|
+
// Page 1 failing is a failed check; a later page keeps what we have.
|
|
255
|
+
if (page === 1) return null;
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
const body = (await res.json()) as unknown;
|
|
259
|
+
if (!Array.isArray(body)) return page === 1 ? null : best;
|
|
260
|
+
for (const rel of body as GitHubRelease[]) {
|
|
261
|
+
if (rel === null || typeof rel !== 'object') continue;
|
|
262
|
+
if (rel.draft === true || typeof rel.tag_name !== 'string') continue;
|
|
263
|
+
const tag = rel.tag_name;
|
|
264
|
+
if (!tag.startsWith(tagPrefix)) continue;
|
|
265
|
+
const version = semver.valid(tag.slice(tagPrefix.length).replace(/^v/, ''));
|
|
266
|
+
if (version === null) continue;
|
|
267
|
+
if (!includePrereleases && (rel.prerelease === true || semver.prerelease(version) !== null)) {
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (best === null || semver.gt(version, best.version)) best = { version, tag };
|
|
271
|
+
}
|
|
272
|
+
if (body.length < PAGE_SIZE) break;
|
|
273
|
+
}
|
|
274
|
+
return best;
|
|
275
|
+
}
|
|
276
|
+
|
|
172
277
|
// ── notifyIfAvailable ──────────────────────────────────────────────────────
|
|
173
278
|
|
|
174
279
|
async function notifyIfAvailable(out: NodeJS.WritableStream): Promise<void> {
|
|
175
280
|
try {
|
|
176
281
|
let latestVersion: string | null = null;
|
|
282
|
+
let latestTag: string | undefined;
|
|
177
283
|
|
|
178
284
|
// Check cache first.
|
|
179
285
|
const cached = readCache();
|
|
180
286
|
if (cached !== null && Date.now() - cached.checkedAt < cacheTTL) {
|
|
181
287
|
latestVersion = cached.version;
|
|
288
|
+
latestTag = cached.tag;
|
|
182
289
|
} else {
|
|
183
|
-
|
|
184
|
-
if (
|
|
185
|
-
|
|
290
|
+
const latest = await fetchLatestVersion();
|
|
291
|
+
if (latest !== null) {
|
|
292
|
+
latestVersion = latest.version;
|
|
293
|
+
latestTag = latest.tag;
|
|
294
|
+
writeCache(latest.version, latest.tag);
|
|
186
295
|
}
|
|
187
296
|
}
|
|
188
297
|
|
|
@@ -193,7 +302,7 @@ export function createChecker(opts: CheckerOptions): Checker {
|
|
|
193
302
|
|
|
194
303
|
const notice =
|
|
195
304
|
`\nA new release of ${name} is available: v${currentVersion} → v${latestVersion}\n` +
|
|
196
|
-
`${
|
|
305
|
+
`${noticeURL(latestTag)}\n`;
|
|
197
306
|
|
|
198
307
|
out.write(notice);
|
|
199
308
|
} catch {
|