@eum-agent/extension-catalog 1.0.1 → 1.0.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/dist/index.js +55 -75
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -180,7 +180,6 @@ const CatalogSchema = z.object({ releases: z.array(ReleaseSchema).max(2e3) }).st
|
|
|
180
180
|
const SnapshotSchema = z.object({
|
|
181
181
|
repository: z.literal(CATALOG_REPOSITORY),
|
|
182
182
|
commit: GitShaSchema,
|
|
183
|
-
treeSha: GitShaSchema,
|
|
184
183
|
contentDigest: z.string().regex(/^[0-9a-f]{64}$/),
|
|
185
184
|
fetchedAt: z.iso.datetime({ offset: true }),
|
|
186
185
|
revision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
|
|
@@ -544,7 +543,6 @@ function createDefaultsService(options) {
|
|
|
544
543
|
const AcceptedSnapshotSchema = z.object({
|
|
545
544
|
repository: z.literal(CATALOG_REPOSITORY),
|
|
546
545
|
commit: GitShaSchema,
|
|
547
|
-
treeSha: GitShaSchema,
|
|
548
546
|
contentDigest: z.string().regex(/^[0-9a-f]{64}$/)
|
|
549
547
|
}).strict();
|
|
550
548
|
z.object({
|
|
@@ -596,24 +594,49 @@ function projectInstalled(observations, snapshot, host) {
|
|
|
596
594
|
}
|
|
597
595
|
//#endregion
|
|
598
596
|
//#region src/extension-catalog/sync.ts
|
|
599
|
-
|
|
600
|
-
const
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
const
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
const
|
|
615
|
-
|
|
616
|
-
|
|
597
|
+
/** Git smart HTTP ref advertisement for the catalog repository. */
|
|
598
|
+
const CATALOG_REFS_URL = "https://github.com/yanxuan-lc/yaoguang-presets.git/info/refs?service=git-upload-pack";
|
|
599
|
+
/** Raw content URL of the extension index pinned to one commit. */
|
|
600
|
+
const catalogIndexUrl = (commit) => `https://raw.githubusercontent.com/yanxuan-lc/yaoguang-presets/${commit}/extensions/index.json`;
|
|
601
|
+
const REFS_MAX_BYTES = 1048576;
|
|
602
|
+
const headers = { "User-Agent": "YaoGuang-extensions" };
|
|
603
|
+
/**
|
|
604
|
+
* Returns the commit of `refs/heads/main` from a Git smart HTTP upload-pack advertisement.
|
|
605
|
+
* Requires the leading service announcement and well-formed pkt-lines; any other shape,
|
|
606
|
+
* including a missing main branch, is rejected as an invalid catalog.
|
|
607
|
+
*/
|
|
608
|
+
function parseMainCommit(bytes) {
|
|
609
|
+
const text = Buffer.from(bytes).toString("latin1");
|
|
610
|
+
const lines = [];
|
|
611
|
+
for (let at = 0; at < text.length;) {
|
|
612
|
+
const prefix = text.slice(at, at + 4);
|
|
613
|
+
if (!/^[0-9a-f]{4}$/.test(prefix)) throw new CatalogError("CATALOG_INVALID", "Malformed ref advertisement");
|
|
614
|
+
const length = Number.parseInt(prefix, 16);
|
|
615
|
+
if (length === 0) {
|
|
616
|
+
lines.push(null);
|
|
617
|
+
at += 4;
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
if (length < 4 || at + length > text.length) throw new CatalogError("CATALOG_INVALID", "Malformed ref advertisement");
|
|
621
|
+
lines.push(text.slice(at + 4, at + length).replace(/\n$/, ""));
|
|
622
|
+
at += length;
|
|
623
|
+
}
|
|
624
|
+
if (lines[0] !== "# service=git-upload-pack" || lines[1] !== null) throw new CatalogError("CATALOG_INVALID", "Missing upload-pack service announcement");
|
|
625
|
+
for (const line of lines.slice(2)) {
|
|
626
|
+
if (line === null) continue;
|
|
627
|
+
const match = /^([0-9a-f]{40}) ([^\0]+)(?:\0.*)?$/s.exec(line);
|
|
628
|
+
if (!match) throw new CatalogError("CATALOG_INVALID", "Malformed ref advertisement");
|
|
629
|
+
if (match[2] === "refs/heads/main") return match[1];
|
|
630
|
+
}
|
|
631
|
+
throw new CatalogError("CATALOG_INVALID", "Catalog repository has no main branch");
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Reads a bounded response without credentials, redirects, or remote-body diagnostics.
|
|
635
|
+
* A failed status becomes `SYNC_FAILED`, with a retry delay (at most one day) taken from
|
|
636
|
+
* `retry-after`, or from `x-ratelimit-reset` for 429 and exhausted-quota 403 responses.
|
|
637
|
+
* A 404 throws the error built by `notFound` when it is supplied.
|
|
638
|
+
*/
|
|
639
|
+
async function readBoundedResponse(network, url, signal, maxBytes, headers = {}, notFound) {
|
|
617
640
|
signal.throwIfAborted();
|
|
618
641
|
const response = await network.fetch(url, {
|
|
619
642
|
signal,
|
|
@@ -622,7 +645,9 @@ async function readBoundedResponse(network, url, signal, maxBytes, headers = {})
|
|
|
622
645
|
});
|
|
623
646
|
if (!response.ok) {
|
|
624
647
|
await response.body?.cancel().catch(() => {});
|
|
625
|
-
|
|
648
|
+
if (response.status === 404 && notFound) throw notFound();
|
|
649
|
+
let retry = Number(response.headers.get("retry-after"));
|
|
650
|
+
if (!(Number.isFinite(retry) && retry > 0) && (response.status === 429 || response.status === 403 && response.headers.get("x-ratelimit-remaining") === "0")) retry = Number(response.headers.get("x-ratelimit-reset")) - Math.floor(Date.now() / 1e3);
|
|
626
651
|
throw new CatalogError("SYNC_FAILED", `Request failed (HTTP ${response.status})`, Number.isFinite(retry) && retry > 0 ? Math.min(86400, retry) : void 0);
|
|
627
652
|
}
|
|
628
653
|
if (Number(response.headers.get("content-length")) > maxBytes) {
|
|
@@ -649,69 +674,25 @@ async function readBoundedResponse(network, url, signal, maxBytes, headers = {})
|
|
|
649
674
|
reader.releaseLock();
|
|
650
675
|
}
|
|
651
676
|
}
|
|
652
|
-
/**
|
|
677
|
+
/**
|
|
678
|
+
* Resolves the catalog's main commit with one ref advertisement request, then reads the complete
|
|
679
|
+
* extension index pinned to that commit. An unchanged commit reuses `previous` with a new fetch
|
|
680
|
+
* time. Requests carry no credentials, refuse redirects and share an 8 MiB budget and 30 s limit.
|
|
681
|
+
*/
|
|
653
682
|
async function synchronize(network, ownerSignal, previous) {
|
|
654
683
|
const controller = new AbortController();
|
|
655
684
|
const signal = AbortSignal.any([ownerSignal, controller.signal]);
|
|
656
685
|
const timer = setTimeout(() => controller.abort(new CatalogError("SYNC_FAILED", "Catalog synchronization exceeded 30 seconds")), 3e4);
|
|
657
|
-
let remaining = CATALOG_MAX_BYTES;
|
|
658
|
-
async function read(path, raw = false) {
|
|
659
|
-
const bytes = await readBoundedResponse(network, `${api}/${path}`, signal, remaining, {
|
|
660
|
-
Accept: raw ? "application/vnd.github.raw+json" : "application/vnd.github+json",
|
|
661
|
-
"X-GitHub-Api-Version": "2026-03-10",
|
|
662
|
-
"User-Agent": "YaoGuang-extensions"
|
|
663
|
-
});
|
|
664
|
-
remaining -= bytes.length;
|
|
665
|
-
if (remaining < 0) throw new CatalogError("LIMIT_EXCEEDED", "Whole catalog exceeds 8 MiB");
|
|
666
|
-
return bytes;
|
|
667
|
-
}
|
|
668
686
|
try {
|
|
669
|
-
const commit =
|
|
670
|
-
if (previous?.commit === commit
|
|
687
|
+
const commit = parseMainCommit(await readBoundedResponse(network, CATALOG_REFS_URL, signal, REFS_MAX_BYTES, headers));
|
|
688
|
+
if (previous?.commit === commit) return {
|
|
671
689
|
...previous,
|
|
672
690
|
fetchedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
673
691
|
};
|
|
674
|
-
const
|
|
675
|
-
if (tree.sha !== commit.commit.tree.sha) throw new CatalogError("CATALOG_INVALID", "Commit tree mismatch");
|
|
676
|
-
const paths = /* @__PURE__ */ new Set();
|
|
677
|
-
for (const item of tree.tree) {
|
|
678
|
-
if (paths.has(item.path)) throw new CatalogError("CATALOG_INVALID", "Duplicate tree path");
|
|
679
|
-
paths.add(item.path);
|
|
680
|
-
if (item.path === "extensions" || item.path.startsWith("extensions/")) {
|
|
681
|
-
if (item.path.split("/").some((p) => !p || p === "." || p === "..") || item.path.includes("\\") || !(item.mode === "100644" && item.type === "blob" || item.mode === "040000" && item.type === "tree")) throw new CatalogError("CATALOG_INVALID", "Catalog contains unsafe tree entry");
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
const index = tree.tree.find((item) => item.path === "extensions/index.json");
|
|
685
|
-
if (!index || index.mode !== "100644") throw new CatalogError("CATALOG_UNAVAILABLE", "No extension index is available");
|
|
686
|
-
const bytes = await read(`git/blobs/${index.sha}`, true);
|
|
687
|
-
if (blobSha(bytes) !== index.sha) throw new CatalogError("CATALOG_INVALID", "Index Git blob digest mismatch");
|
|
688
|
-
const releases = decodeReleases(parseJson(decodeUtf8(bytes)));
|
|
689
|
-
const files = tree.tree.filter((item) => item.type === "blob" && item.path.startsWith("extensions/releases/"));
|
|
690
|
-
if (files.length !== releases.length || files.some((item) => !/^extensions\/releases\/[^/]+\.json$/.test(item.path))) throw new CatalogError("CATALOG_INVALID", "Release file coverage mismatch");
|
|
691
|
-
let next = 0;
|
|
692
|
-
const records = /* @__PURE__ */ new Map();
|
|
693
|
-
const failed = (await Promise.allSettled(Array.from({ length: Math.min(4, files.length) }, async () => {
|
|
694
|
-
try {
|
|
695
|
-
for (;;) {
|
|
696
|
-
const item = files[next++];
|
|
697
|
-
if (!item) return;
|
|
698
|
-
const content = await read(`git/blobs/${item.sha}`, true);
|
|
699
|
-
if (blobSha(content) !== item.sha) throw new CatalogError("CATALOG_INVALID", "Release Git blob digest mismatch");
|
|
700
|
-
const release = ReleaseSchema.parse(parseJson(decodeUtf8(content)));
|
|
701
|
-
if (records.has(release.releaseId)) throw new CatalogError("CATALOG_INVALID", "Duplicate release file");
|
|
702
|
-
records.set(release.releaseId, recordDigest(release));
|
|
703
|
-
}
|
|
704
|
-
} catch (error) {
|
|
705
|
-
controller.abort(error);
|
|
706
|
-
throw error;
|
|
707
|
-
}
|
|
708
|
-
}))).find((result) => result.status === "rejected");
|
|
709
|
-
if (failed?.status === "rejected") throw failed.reason;
|
|
710
|
-
if (releases.some((release) => records.get(release.releaseId) !== recordDigest(release))) throw new CatalogError("CATALOG_INVALID", "Index and release contents differ");
|
|
692
|
+
const releases = decodeReleases(parseJson(decodeUtf8(await readBoundedResponse(network, catalogIndexUrl(commit), signal, CATALOG_MAX_BYTES, headers, () => new CatalogError("CATALOG_UNAVAILABLE", "No extension index is available")))));
|
|
711
693
|
return {
|
|
712
694
|
repository: CATALOG_REPOSITORY,
|
|
713
|
-
commit
|
|
714
|
-
treeSha: tree.sha,
|
|
695
|
+
commit,
|
|
715
696
|
contentDigest: recordDigest({ releases }),
|
|
716
697
|
fetchedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
717
698
|
revision: previous?.revision ?? 0,
|
|
@@ -903,7 +884,6 @@ function createCatalogService(options) {
|
|
|
903
884
|
snapshot: {
|
|
904
885
|
repository: accepted.repository,
|
|
905
886
|
commit: accepted.commit,
|
|
906
|
-
treeSha: accepted.treeSha,
|
|
907
887
|
contentDigest: accepted.contentDigest
|
|
908
888
|
},
|
|
909
889
|
release: candidate,
|