@eum-agent/sandbox-presets 1.0.0 → 1.0.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.
- package/dist/index.js +51 -52
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -134,7 +134,6 @@ const StoredProfileSchema = z.object({
|
|
|
134
134
|
/** Complete verified snapshot used for new selections. */
|
|
135
135
|
const SnapshotSchema = z.object({
|
|
136
136
|
commit: ShaSchema,
|
|
137
|
-
treeSha: ShaSchema,
|
|
138
137
|
syncedAt: z.string(),
|
|
139
138
|
profiles: z.array(StoredProfileSchema).min(1).max(128)
|
|
140
139
|
}).strict();
|
|
@@ -239,22 +238,11 @@ function decodeUtf8(bytes) {
|
|
|
239
238
|
}
|
|
240
239
|
//#endregion
|
|
241
240
|
//#region src/sandbox-presets/catalog.ts
|
|
242
|
-
/**
|
|
241
|
+
/** Generated bundle verification for one pinned commit of the fixed public repository. */
|
|
243
242
|
/** Computes Git's SHA-1 blob identity over exact UTF-8 bytes. */
|
|
244
243
|
function blobSha(bytes) {
|
|
245
244
|
return createHash("sha1").update(`blob ${bytes.length}\0`).update(bytes).digest("hex");
|
|
246
245
|
}
|
|
247
|
-
/** Bounded Git tree fields; GitHub may add harmless transport metadata. */
|
|
248
|
-
const TreeSchema = z.object({
|
|
249
|
-
sha: ShaSchema,
|
|
250
|
-
truncated: z.literal(false),
|
|
251
|
-
tree: z.array(z.object({
|
|
252
|
-
path: z.string().max(4096),
|
|
253
|
-
mode: z.string(),
|
|
254
|
-
type: z.string(),
|
|
255
|
-
sha: ShaSchema
|
|
256
|
-
})).max(2e4)
|
|
257
|
-
});
|
|
258
246
|
const BundleSchema = z.object({ presets: z.array(z.object({
|
|
259
247
|
id: IdSchema,
|
|
260
248
|
path: z.string().max(160),
|
|
@@ -269,22 +257,20 @@ function verifyProfile(stored) {
|
|
|
269
257
|
const result = ProfileSchema.safeParse(parseJson(stored.content));
|
|
270
258
|
if (!result.success || result.data.id !== stored.id || JSON.stringify(result.data) !== JSON.stringify(stored.profile)) invalid("Profile content and identity disagree");
|
|
271
259
|
}
|
|
272
|
-
/**
|
|
273
|
-
|
|
260
|
+
/**
|
|
261
|
+
* Decodes the generated bundle read from one pinned commit into a complete snapshot.
|
|
262
|
+
* Every category must have a unique id, live at `sandbox/<id>/paths.json`, and carry content
|
|
263
|
+
* whose Git blob identity matches its declared digest. Throws `PresetError` with
|
|
264
|
+
* `CATALOG_INVALID` for malformed or inconsistent data and `LIMIT_EXCEEDED` for oversized input.
|
|
265
|
+
*/
|
|
266
|
+
function decodeCatalog(commit, bytes, syncedAt = (/* @__PURE__ */ new Date()).toISOString()) {
|
|
274
267
|
if (bytes.length > 8388608) throw new PresetError("LIMIT_EXCEEDED", "Catalog exceeds 8 MiB");
|
|
275
|
-
const
|
|
276
|
-
if (!ShaSchema.safeParse(commit).success || !
|
|
277
|
-
const files = new Map(tree.data.tree.map((item) => [item.path, item]));
|
|
278
|
-
if (files.size !== tree.data.tree.length) invalid("Duplicate tree path");
|
|
279
|
-
for (const item of tree.data.tree) if (item.path.startsWith("sandbox/") || item.path === "sandbox" || item.path === "schema" || item.path === "schema/sandbox.schema.json") {
|
|
280
|
-
if (item.path.split("/").some((part) => !part || part === "." || part === "..") || !(item.mode === "040000" && item.type === "tree" || item.mode === "100644" && item.type === "blob")) invalid("Catalog contains linked, executable, or invalid files");
|
|
281
|
-
}
|
|
282
|
-
for (const p of ["sandbox/index.json", "schema/sandbox.schema.json"]) if (files.get(p)?.mode !== "100644") invalid("Catalog index or schema is missing");
|
|
283
|
-
if (files.get("sandbox/index.json")?.sha !== blobSha(bytes)) invalid("Index blob digest differs from tree");
|
|
268
|
+
const bundle = BundleSchema.safeParse(parseJson(decodeUtf8(bytes)));
|
|
269
|
+
if (!ShaSchema.safeParse(commit).success || !bundle.success) invalid("Invalid catalog bundle");
|
|
284
270
|
const profiles = [], ids = /* @__PURE__ */ new Set();
|
|
285
271
|
let entries = 0;
|
|
286
272
|
for (const item of bundle.data.presets) {
|
|
287
|
-
if (ids.has(item.id) || item.path !== `sandbox/${item.id}/paths.json`
|
|
273
|
+
if (ids.has(item.id) || item.path !== `sandbox/${item.id}/paths.json`) invalid("Category identity or path mismatch");
|
|
288
274
|
if (Buffer.byteLength(item.content) > 131072) throw new PresetError("LIMIT_EXCEEDED", "Profile exceeds 128 KiB");
|
|
289
275
|
const parsed = ProfileSchema.safeParse(parseJson(item.content));
|
|
290
276
|
if (!parsed.success) invalid(`Invalid profile: ${item.id}`);
|
|
@@ -301,12 +287,8 @@ function decodeCatalog(commit, rawTree, bytes, syncedAt = (/* @__PURE__ */ new D
|
|
|
301
287
|
ids.add(item.id);
|
|
302
288
|
profiles.push(profile);
|
|
303
289
|
}
|
|
304
|
-
const paths = tree.data.tree.filter((item) => /^sandbox\/[^/]+\/paths\.json$/.test(item.path));
|
|
305
|
-
const dirs = tree.data.tree.filter((item) => /^sandbox\/[^/]+$/.test(item.path) && item.type === "tree");
|
|
306
|
-
if (paths.length !== profiles.length || dirs.length !== profiles.length || paths.some((item) => !ids.has(item.path.split("/")[1] ?? ""))) invalid("Bundle omits or adds a category");
|
|
307
290
|
return {
|
|
308
291
|
commit,
|
|
309
|
-
treeSha: tree.data.sha,
|
|
310
292
|
syncedAt,
|
|
311
293
|
profiles: profiles.sort((a, b) => a.id < b.id ? -1 : 1)
|
|
312
294
|
};
|
|
@@ -429,23 +411,17 @@ function resolveProfile(profile, commit, host, reads) {
|
|
|
429
411
|
}
|
|
430
412
|
//#endregion
|
|
431
413
|
//#region src/sandbox-presets/sync.ts
|
|
432
|
-
const
|
|
433
|
-
const
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
});
|
|
437
|
-
async function readResponse(network, path, signal, raw = false) {
|
|
414
|
+
const refsUrl = "https://github.com/yanxuan-lc/yaoguang-presets.git/info/refs?service=git-upload-pack";
|
|
415
|
+
const rawBase = "https://raw.githubusercontent.com/yanxuan-lc/yaoguang-presets";
|
|
416
|
+
/** Reads one bounded public response; failures map to stable preset error codes. */
|
|
417
|
+
async function readResponse(network, url, signal) {
|
|
438
418
|
signal.throwIfAborted();
|
|
439
419
|
let response;
|
|
440
420
|
try {
|
|
441
|
-
response = await network.fetch(
|
|
421
|
+
response = await network.fetch(url, {
|
|
442
422
|
signal,
|
|
443
423
|
redirect: "error",
|
|
444
|
-
headers: {
|
|
445
|
-
Accept: raw ? "application/vnd.github.raw+json" : "application/vnd.github+json",
|
|
446
|
-
"X-GitHub-Api-Version": "2026-03-10",
|
|
447
|
-
"User-Agent": "YaoGuang-presets"
|
|
448
|
-
}
|
|
424
|
+
headers: { "User-Agent": "YaoGuang-presets" }
|
|
449
425
|
});
|
|
450
426
|
} catch {
|
|
451
427
|
signal.throwIfAborted();
|
|
@@ -483,22 +459,45 @@ async function readResponse(network, path, signal, raw = false) {
|
|
|
483
459
|
reader.releaseLock();
|
|
484
460
|
}
|
|
485
461
|
}
|
|
486
|
-
/**
|
|
462
|
+
/**
|
|
463
|
+
* Extracts the `refs/heads/main` commit from a Git smart HTTP upload-pack ref advertisement.
|
|
464
|
+
* Requires the `# service=git-upload-pack` announcement and well-formed pkt-lines; throws
|
|
465
|
+
* `PresetError` with `CATALOG_INVALID` otherwise.
|
|
466
|
+
*/
|
|
467
|
+
function mainCommit(text) {
|
|
468
|
+
const lines = [];
|
|
469
|
+
for (let at = 0; at < text.length;) {
|
|
470
|
+
const header = text.slice(at, at + 4);
|
|
471
|
+
if (!/^[0-9a-f]{4}$/.test(header)) invalid("Malformed ref advertisement");
|
|
472
|
+
const length = Number.parseInt(header, 16);
|
|
473
|
+
if (length === 0) {
|
|
474
|
+
at += 4;
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
if (length < 4 || at + length > text.length) invalid("Malformed ref advertisement");
|
|
478
|
+
lines.push(text.slice(at + 4, at + length).replace(/\n$/, ""));
|
|
479
|
+
at += length;
|
|
480
|
+
}
|
|
481
|
+
if (lines[0] !== "# service=git-upload-pack") invalid("Missing upload-pack announcement");
|
|
482
|
+
for (const line of lines.slice(1)) {
|
|
483
|
+
const match = /^([0-9a-f]{40}) ([^\0]+)(?:\0.*)?$/s.exec(line);
|
|
484
|
+
if (match?.[2] === "refs/heads/main") return match[1];
|
|
485
|
+
}
|
|
486
|
+
return invalid("Branch main is not advertised");
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Resolves `main` with one ref-advertisement request, then reads `sandbox/index.json` pinned to
|
|
490
|
+
* that commit. An unchanged commit reuses `previous` with a fresh sync time and no second request.
|
|
491
|
+
*/
|
|
487
492
|
async function synchronize(network, signal, previous) {
|
|
488
|
-
const commit =
|
|
489
|
-
if (
|
|
490
|
-
if (previous?.commit === commit.data.sha && previous.treeSha === commit.data.commit.tree.sha) return {
|
|
493
|
+
const commit = mainCommit(decodeUtf8(await readResponse(network, refsUrl, signal)));
|
|
494
|
+
if (previous?.commit === commit) return {
|
|
491
495
|
...previous,
|
|
492
496
|
syncedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
493
497
|
};
|
|
494
|
-
const
|
|
495
|
-
if (!tree.success || tree.data.sha !== commit.data.commit.tree.sha) invalid("Invalid or incomplete commit tree");
|
|
496
|
-
const index = tree.data.tree.find((item) => item.path === "sandbox/index.json");
|
|
497
|
-
if (!index) throw new PresetError("CATALOG_EMPTY", "No sandbox catalog is available");
|
|
498
|
-
if (index.mode !== "100644" || index.type !== "blob") invalid("Index is not an ordinary file");
|
|
499
|
-
const bytes = await readResponse(network, `git/blobs/${index.sha}`, signal, true);
|
|
498
|
+
const bytes = await readResponse(network, `${rawBase}/${commit}/sandbox/index.json`, signal);
|
|
500
499
|
signal.throwIfAborted();
|
|
501
|
-
return decodeCatalog(commit
|
|
500
|
+
return decodeCatalog(commit, bytes);
|
|
502
501
|
}
|
|
503
502
|
//#endregion
|
|
504
503
|
//#region src/sandbox-presets/service.ts
|