@sublang/spex 4.1.0 → 5.0.0

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 (98) hide show
  1. package/README.md +438 -54
  2. package/dist/cli.js +81 -31
  3. package/dist/commands/add.d.ts +12 -0
  4. package/dist/commands/add.js +138 -0
  5. package/dist/commands/auth-target.d.ts +11 -0
  6. package/dist/commands/auth-target.js +50 -0
  7. package/dist/commands/environment.d.ts +123 -0
  8. package/dist/commands/environment.js +411 -0
  9. package/dist/commands/export.d.ts +21 -0
  10. package/dist/commands/export.js +663 -0
  11. package/dist/commands/home.d.ts +56 -0
  12. package/dist/commands/home.js +215 -0
  13. package/dist/commands/install.d.ts +2 -0
  14. package/dist/commands/install.js +48 -0
  15. package/dist/commands/list.d.ts +2 -0
  16. package/dist/commands/list.js +87 -0
  17. package/dist/commands/login.d.ts +2 -0
  18. package/dist/commands/login.js +211 -0
  19. package/dist/commands/logout.d.ts +2 -0
  20. package/dist/commands/logout.js +74 -0
  21. package/dist/commands/publish.d.ts +2 -0
  22. package/dist/commands/publish.js +120 -0
  23. package/dist/commands/remove.d.ts +2 -0
  24. package/dist/commands/remove.js +34 -0
  25. package/dist/commands/run.d.ts +50 -0
  26. package/dist/commands/run.js +309 -0
  27. package/dist/commands/search.d.ts +2 -0
  28. package/dist/commands/search.js +34 -0
  29. package/dist/commands/settle.d.ts +124 -0
  30. package/dist/commands/settle.js +475 -0
  31. package/dist/commands/update.d.ts +2 -0
  32. package/dist/commands/update.js +37 -0
  33. package/dist/commands/whoami.d.ts +2 -0
  34. package/dist/commands/whoami.js +75 -0
  35. package/dist/lib/environment/agent-folders.d.ts +14 -0
  36. package/dist/lib/environment/agent-folders.js +11 -0
  37. package/dist/lib/environment/archive.d.ts +26 -0
  38. package/dist/lib/environment/archive.js +91 -0
  39. package/dist/lib/environment/builtins.d.ts +112 -0
  40. package/dist/lib/environment/builtins.js +539 -0
  41. package/dist/lib/environment/citations.d.ts +75 -0
  42. package/dist/lib/environment/citations.js +267 -0
  43. package/dist/lib/environment/exports.d.ts +190 -0
  44. package/dist/lib/environment/exports.js +1029 -0
  45. package/dist/lib/environment/format.d.ts +133 -0
  46. package/dist/lib/environment/format.js +651 -0
  47. package/dist/lib/environment/git-source.d.ts +41 -0
  48. package/dist/lib/environment/git-source.js +159 -0
  49. package/dist/lib/environment/index.d.ts +20 -0
  50. package/dist/lib/environment/index.js +27 -0
  51. package/dist/lib/environment/install.d.ts +140 -0
  52. package/dist/lib/environment/install.js +694 -0
  53. package/dist/lib/environment/layout.d.ts +15 -0
  54. package/dist/lib/environment/layout.js +50 -0
  55. package/dist/lib/environment/lock.d.ts +87 -0
  56. package/dist/lib/environment/lock.js +305 -0
  57. package/dist/lib/environment/marker.d.ts +15 -0
  58. package/dist/lib/environment/marker.js +31 -0
  59. package/dist/lib/environment/own-paths.d.ts +59 -0
  60. package/dist/lib/environment/own-paths.js +174 -0
  61. package/dist/lib/environment/placed-record.d.ts +32 -0
  62. package/dist/lib/environment/placed-record.js +72 -0
  63. package/dist/lib/environment/registries.d.ts +25 -0
  64. package/dist/lib/environment/registries.js +75 -0
  65. package/dist/lib/environment/registry.d.ts +173 -0
  66. package/dist/lib/environment/registry.js +390 -0
  67. package/dist/lib/environment/requests.d.ts +108 -0
  68. package/dist/lib/environment/requests.js +403 -0
  69. package/dist/lib/environment/resolve.d.ts +70 -0
  70. package/dist/lib/environment/resolve.js +451 -0
  71. package/dist/lib/environment/semver.d.ts +30 -0
  72. package/dist/lib/environment/semver.js +130 -0
  73. package/dist/lib/environment/store.d.ts +23 -0
  74. package/dist/lib/environment/store.js +123 -0
  75. package/dist/lib/environment/tar.d.ts +27 -0
  76. package/dist/lib/environment/tar.js +219 -0
  77. package/dist/lib/files.d.ts +66 -0
  78. package/dist/lib/files.js +233 -0
  79. package/dist/lib/git-host.d.ts +579 -0
  80. package/dist/lib/git-host.js +1363 -0
  81. package/dist/lib/home-file.d.ts +124 -0
  82. package/dist/lib/home-file.js +288 -0
  83. package/dist/lib/instance.d.ts +63 -0
  84. package/dist/lib/instance.js +130 -0
  85. package/dist/lib/words.d.ts +32 -0
  86. package/dist/lib/words.js +27 -0
  87. package/dist/lint.js +8 -0
  88. package/dist/testing/index.d.ts +1 -0
  89. package/dist/testing/index.js +7 -0
  90. package/dist/testing/standin-registry.d.ts +94 -0
  91. package/dist/testing/standin-registry.js +449 -0
  92. package/package.json +37 -3
  93. package/dist/attached-cli.d.ts +0 -13
  94. package/dist/attached-cli.js +0 -247
  95. package/dist/attached-client.d.ts +0 -36
  96. package/dist/attached-client.js +0 -189
  97. package/dist/generated-protocol.d.ts +0 -2
  98. package/dist/generated-protocol.js +0 -6
@@ -0,0 +1,403 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
3
+ // Requests: an environment's `spex.yaml` (environments-2, DR-104,
4
+ // DR-165, DR-168). Exactly `{format: 1, language?, space?, registry?, packages}` — `space`
5
+ // the origin of the environment's instance — each package named
6
+ // `<org>/<pkg>` and requested from one source — a registry version
7
+ // requirement, a path inside the working folder, or a Git repository at
8
+ // a revision — with an optional `select` and `alias`. Edits go through
9
+ // yaml's Document API, so comments and key order survive, and each is a
10
+ // versioned write of the bytes it read (DR-111).
11
+ import { existsSync, readFileSync } from "node:fs";
12
+ import { readFile } from "node:fs/promises";
13
+ import { isAbsolute, posix } from "node:path";
14
+ import { Document, isMap, isSeq, parseDocument } from "yaml";
15
+ import { readVersioned, writeVersionedBytes } from "../files.js";
16
+ import { defaultInstanceOf } from "../home-file.js";
17
+ import { canonicalOrigin, OriginRefused } from "../instance.js";
18
+ import { isLanguageTag, isPackageName, isSkillName, sha256Hex } from "./format.js";
19
+ import { isRequirement } from "./semver.js";
20
+ export class RequestsError extends Error {
21
+ issues;
22
+ /** The format a `spex.yaml` from a newer Spex names, where it names one. */
23
+ newerFormat;
24
+ constructor(issues, newerFormat) {
25
+ super(newerFormat !== undefined
26
+ ? `spex.yaml is from a newer Spex (format ${newerFormat})`
27
+ : `spex.yaml cannot be read: ${issues.map((issue) => issue.message).join("; ")}`);
28
+ this.issues = issues;
29
+ this.name = "RequestsError";
30
+ if (newerFormat !== undefined)
31
+ this.newerFormat = newerFormat;
32
+ }
33
+ }
34
+ export function isRegistryRequest(request) {
35
+ return "version" in request && typeof request.version === "string";
36
+ }
37
+ export function isPathRequest(request) {
38
+ return !("git" in request) && "path" in request && typeof request.path === "string";
39
+ }
40
+ export function isGitRequest(request) {
41
+ return "git" in request && typeof request.git === "string";
42
+ }
43
+ function isPlainObject(value) {
44
+ return typeof value === "object" && value !== null && !Array.isArray(value);
45
+ }
46
+ /** Why a name is no package name, with the rule, one wording wherever a
47
+ * name is refused. */
48
+ export function packageNameRefusal(name) {
49
+ return `${JSON.stringify(name)} is not a package name — use <org>/<pkg>, each part lowercase letters, digits and single hyphens, at most 64 characters, like acme/tool`;
50
+ }
51
+ /** Whether a request path stays inside the working folder. */
52
+ export function isInsidePath(path) {
53
+ if (path === "" || isAbsolute(path) || path.includes("\\") || /^[a-zA-Z]:/.test(path))
54
+ return false;
55
+ const normalized = posix.normalize(path);
56
+ return normalized !== ".." && !normalized.startsWith("../") && !posix.isAbsolute(normalized);
57
+ }
58
+ const REQUEST_FIELDS = new Set(["version", "path", "git", "rev", "select", "alias"]);
59
+ /** Every reason a request for a name is refused (environments-2,
60
+ * environments-15): a bad name, no or several sources, a malformed
61
+ * requirement, a path that is not relative or leads out of the folder
62
+ * it is relative to, a bad select or alias. */
63
+ export function requestIssues(name, request) {
64
+ const at = `packages.${name}`;
65
+ const issues = [];
66
+ if (!isPackageName(name))
67
+ issues.push({ path: at, rule: "name", message: packageNameRefusal(name) });
68
+ if (!isPlainObject(request))
69
+ return [...issues, { path: at, rule: "request", message: `${name} is not a mapping` }];
70
+ for (const key of Object.keys(request)) {
71
+ if (!REQUEST_FIELDS.has(key) && !key.startsWith("x-"))
72
+ issues.push({ path: at, rule: "field", message: `${name} has an unknown field "${key}"` });
73
+ }
74
+ const sources = [
75
+ request.version !== undefined ? "registry" : undefined,
76
+ request.git !== undefined ? "git" : undefined,
77
+ request.path !== undefined && request.git === undefined ? "path" : undefined,
78
+ ].filter(Boolean);
79
+ if (sources.length !== 1) {
80
+ issues.push({ path: at, rule: "source", message: `${name} needs exactly one of version, path, or git with rev` });
81
+ }
82
+ else if (sources[0] === "registry") {
83
+ if (!isRequirement(request.version))
84
+ issues.push({ path: at, rule: "version", message: `${JSON.stringify(request.version)} is not a version — use 1.2.3, ^1.2.3 or ~1.2.3` });
85
+ if (request.rev !== undefined || request.path !== undefined)
86
+ issues.push({ path: at, rule: "source", message: `${name} needs exactly one of version, path, or git with rev` });
87
+ }
88
+ else if (sources[0] === "path") {
89
+ if (typeof request.path !== "string" || !isInsidePath(request.path)) {
90
+ issues.push({ path: at, rule: "path", message: `path ${JSON.stringify(request.path)} is not a relative folder inside the project folder or your Personal records` });
91
+ }
92
+ if (request.rev !== undefined)
93
+ issues.push({ path: at, rule: "source", message: `rev goes with git` });
94
+ }
95
+ else {
96
+ if (typeof request.git !== "string" || request.git.trim() === "")
97
+ issues.push({ path: at, rule: "git", message: `git names no repository` });
98
+ if (typeof request.rev !== "string" || request.rev.trim() === "" || request.rev.startsWith("-"))
99
+ issues.push({ path: at, rule: "git", message: `git needs a rev: a branch, tag or commit` });
100
+ if (request.path !== undefined && (typeof request.path !== "string" || !isInsidePath(request.path))) {
101
+ issues.push({ path: at, rule: "path", message: `path ${JSON.stringify(request.path)} is not a relative folder inside the repository` });
102
+ }
103
+ }
104
+ if (request.select !== undefined) {
105
+ if (!Array.isArray(request.select)) {
106
+ issues.push({ path: at, rule: "select", message: `select is not a list of {artifact, language}` });
107
+ }
108
+ else {
109
+ for (const entry of request.select) {
110
+ if (!isPlainObject(entry) || typeof entry.artifact !== "string" || entry.artifact === ""
111
+ || (entry.language !== undefined && !isLanguageTag(entry.language))
112
+ || Object.keys(entry).some((key) => key !== "artifact" && key !== "language")) {
113
+ issues.push({ path: at, rule: "select", message: `select entry ${JSON.stringify(entry)} is not {artifact, language}` });
114
+ }
115
+ }
116
+ }
117
+ }
118
+ if (request.alias !== undefined) {
119
+ if (!isPlainObject(request.alias)) {
120
+ issues.push({ path: at, rule: "alias", message: `alias is not a mapping of id to name` });
121
+ }
122
+ else {
123
+ for (const [id, alias] of Object.entries(request.alias)) {
124
+ if (!isSkillName(alias))
125
+ issues.push({ path: at, rule: "alias", message: `alias of ${id}, ${JSON.stringify(alias)}, is not an Agent Skills name` });
126
+ }
127
+ }
128
+ }
129
+ return issues;
130
+ }
131
+ function toRequest(value) {
132
+ const extras = {
133
+ ...(Array.isArray(value.select)
134
+ ? { select: value.select.map((entry) => ({
135
+ artifact: entry.artifact,
136
+ ...(typeof entry.language === "string" ? { language: entry.language } : {}),
137
+ })) }
138
+ : {}),
139
+ ...(isPlainObject(value.alias) ? { alias: { ...value.alias } } : {}),
140
+ };
141
+ if (value.version !== undefined)
142
+ return { version: value.version, ...extras };
143
+ if (value.git !== undefined) {
144
+ return { git: value.git, rev: value.rev, ...(typeof value.path === "string" ? { path: value.path } : {}), ...extras };
145
+ }
146
+ return { path: value.path, ...extras };
147
+ }
148
+ /** Read a parsed `spex.yaml` value as requests, or throw. */
149
+ export function parseRequestsValue(raw) {
150
+ const issues = [];
151
+ if (!isPlainObject(raw))
152
+ throw new RequestsError([{ rule: "requests", message: "it is not a mapping" }]);
153
+ if (typeof raw.format === "number" && Number.isInteger(raw.format) && raw.format > 1) {
154
+ throw new RequestsError([{ rule: "format", message: `spex.yaml is from a newer Spex (format ${raw.format})` }], raw.format);
155
+ }
156
+ if (raw.format !== 1)
157
+ throw new RequestsError([{ rule: "format", message: `format ${JSON.stringify(raw.format ?? null)} is not format 1` }]);
158
+ for (const key of Object.keys(raw)) {
159
+ if (key !== "format" && key !== "language" && key !== "space" && key !== "registry" && key !== "packages" && !key.startsWith("x-")) {
160
+ issues.push({ rule: "field", message: `unknown field "${key}"` });
161
+ }
162
+ }
163
+ if (raw.language !== undefined && !isLanguageTag(raw.language)) {
164
+ issues.push({ rule: "language", message: `language ${JSON.stringify(raw.language)} is not a BCP 47 tag` });
165
+ }
166
+ if (raw.space !== undefined) {
167
+ const refusal = spaceRefusal(raw.space);
168
+ if (refusal !== null)
169
+ issues.push({ rule: "space", message: refusal });
170
+ }
171
+ if (raw.registry !== undefined) {
172
+ const refusal = spaceRefusal(raw.registry);
173
+ if (refusal !== null)
174
+ issues.push({ rule: "registry", message: refusal.replace(/^space/, "registry").replace(/a Space address/, "a registry address") });
175
+ }
176
+ const packages = {};
177
+ const rawPackages = raw.packages ?? {};
178
+ if (!isPlainObject(rawPackages)) {
179
+ issues.push({ rule: "packages", message: "packages is not a mapping" });
180
+ }
181
+ else {
182
+ for (const [name, request] of Object.entries(rawPackages)) {
183
+ const found = requestIssues(name, request);
184
+ if (found.length > 0)
185
+ issues.push(...found);
186
+ else
187
+ packages[name] = toRequest(request);
188
+ }
189
+ }
190
+ if (issues.length > 0)
191
+ throw new RequestsError(issues);
192
+ return {
193
+ format: 1,
194
+ ...(typeof raw.language === "string" ? { language: raw.language } : {}),
195
+ ...(typeof raw.space === "string" ? { space: raw.space } : {}),
196
+ ...(typeof raw.registry === "string" ? { registry: canonicalOrigin(raw.registry) } : {}),
197
+ packages,
198
+ };
199
+ }
200
+ /** Why a `space` value is no instance origin by the home file's rule
201
+ * (storage-2), naming it; null where it is one. A value carrying a user
202
+ * is named by that reason alone, never repeated, as it may hold a
203
+ * secret. */
204
+ function spaceRefusal(value) {
205
+ if (typeof value !== "string")
206
+ return `space ${JSON.stringify(value)} is not a Space address`;
207
+ try {
208
+ canonicalOrigin(value);
209
+ return null;
210
+ }
211
+ catch (error) {
212
+ if (!(error instanceof OriginRefused))
213
+ throw error;
214
+ const named = error.reason === "user" ? "space" : `space ${JSON.stringify(value)}`;
215
+ return `${named} is not a Space address: ${error.message.replace(/^origin refused: /, "")}`;
216
+ }
217
+ }
218
+ /** The origin of an environment's instance (environments-2, DR-165): the
219
+ * `space` its `spex.yaml` names, else the home's default instance —
220
+ * the first it lists, or the origin it was created with while it lists
221
+ * none (storage-2) — canonical. The registry, the credential brokered
222
+ * for Git sources, the orgs a person may publish to and the publish
223
+ * destination all follow it. */
224
+ export function environmentInstance(requests, home) {
225
+ const space = requests?.space;
226
+ return canonicalOrigin(space ?? defaultInstanceOf(home).origin);
227
+ }
228
+ /** The public package service used explicitly by new environments. */
229
+ export const PUBLIC_REGISTRY_ORIGIN = "https://spex.sublang.ai";
230
+ /** Existing manifests without registry preserve their former source and
231
+ * credential. An absent manifest starts in independent registry mode. */
232
+ export function environmentRegistry(requests, home) {
233
+ if (requests !== null && requests.registry === undefined) {
234
+ return { origin: environmentInstance(requests, home), scope: "space", legacy: true };
235
+ }
236
+ return { origin: canonicalOrigin(requests?.registry ?? PUBLIC_REGISTRY_ORIGIN), scope: "registry", legacy: false };
237
+ }
238
+ /** Name a package registry without changing the Space binding. */
239
+ export function setRegistryIn(doc, origin) {
240
+ doc.set("registry", canonicalOrigin(origin));
241
+ }
242
+ /** Parse `spex.yaml` text, or throw. */
243
+ export function parseRequests(text) {
244
+ const doc = parseDocument(text, { prettyErrors: false, uniqueKeys: true });
245
+ if (doc.errors.length > 0)
246
+ throw new RequestsError(doc.errors.map((error) => ({ rule: "yaml", message: error.message })));
247
+ return parseRequestsValue(doc.toJS() ?? {});
248
+ }
249
+ /** The lock's `requests` field: the SHA-256 of the file's bytes. */
250
+ export function requestsDigest(text) {
251
+ return sha256Hex(text);
252
+ }
253
+ export async function readRequests(file) {
254
+ return parseRequests(await readFile(file, "utf8"));
255
+ }
256
+ /** Read `spex.yaml` with its text and digest; null where absent. */
257
+ export function readRequestsFile(file) {
258
+ if (!existsSync(file))
259
+ return null;
260
+ const text = readFileSync(file, "utf8");
261
+ return { requests: parseRequests(text), text, digest: requestsDigest(text) };
262
+ }
263
+ function requestValue(request) {
264
+ const value = {};
265
+ if (isRegistryRequest(request))
266
+ value.version = request.version;
267
+ else if (isGitRequest(request)) {
268
+ value.git = request.git;
269
+ value.rev = request.rev;
270
+ if (request.path !== undefined)
271
+ value.path = request.path;
272
+ }
273
+ else
274
+ value.path = request.path;
275
+ if (request.select !== undefined)
276
+ value.select = request.select.map((entry) => ({ ...entry }));
277
+ if (request.alias !== undefined)
278
+ value.alias = { ...request.alias };
279
+ return value;
280
+ }
281
+ function requestsValue(requests) {
282
+ const packages = {};
283
+ for (const [name, request] of Object.entries(requests.packages))
284
+ packages[name] = requestValue(request);
285
+ return {
286
+ format: 1,
287
+ ...(requests.language !== undefined ? { language: requests.language } : {}),
288
+ ...(requests.space !== undefined ? { space: requests.space } : {}),
289
+ ...(requests.registry !== undefined ? { registry: requests.registry } : {}),
290
+ packages,
291
+ };
292
+ }
293
+ /** Name the environment's instance in a `spex.yaml` document
294
+ * (environments-2): `space` set to `origin`, a new key placed before
295
+ * `packages` so the file reads format, language, space, packages; null
296
+ * removes it. Comments and the order of every other key stay. */
297
+ export function setSpaceIn(doc, origin) {
298
+ if (origin === null) {
299
+ if (doc.has("space"))
300
+ doc.delete("space");
301
+ return;
302
+ }
303
+ if (doc.has("space")) {
304
+ doc.set("space", origin);
305
+ return;
306
+ }
307
+ const map = doc.contents;
308
+ if (!isMap(map)) {
309
+ doc.set("space", origin);
310
+ return;
311
+ }
312
+ const pair = doc.createPair("space", origin);
313
+ const at = map.items.findIndex((item) => (item.key?.value ?? item.key) === "packages");
314
+ if (at < 0)
315
+ map.items.push(pair);
316
+ else
317
+ map.items.splice(at, 0, pair);
318
+ }
319
+ function sameValue(a, b) {
320
+ return JSON.stringify(a) === JSON.stringify(b);
321
+ }
322
+ /** Bring a document node to a value, touching only what differs, so
323
+ * comments and key order of what stays survive. */
324
+ function syncNode(doc, path, value) {
325
+ const node = path.length === 0 ? doc.contents : doc.getIn(path, true);
326
+ if (isPlainObject(value) && isMap(node)) {
327
+ for (const item of [...node.items]) {
328
+ const key = item.key?.value ?? item.key;
329
+ if (typeof key === "string" && !(key in value))
330
+ doc.deleteIn([...path, key]);
331
+ }
332
+ for (const [key, child] of Object.entries(value)) {
333
+ if (!node.has(key))
334
+ doc.setIn([...path, key], doc.createNode(child));
335
+ else
336
+ syncNode(doc, [...path, key], child);
337
+ }
338
+ return;
339
+ }
340
+ if (Array.isArray(value) && isSeq(node) && node.items.length === value.length) {
341
+ value.forEach((child, index) => syncNode(doc, [...path, index], child));
342
+ return;
343
+ }
344
+ const current = node === undefined || node === null ? undefined
345
+ : typeof node.toJSON === "function" ? node.toJSON() : node;
346
+ if (sameValue(current, value))
347
+ return;
348
+ if (path.length === 0)
349
+ doc.contents = doc.createNode(value);
350
+ else
351
+ doc.setIn(path, typeof value === "object" && value !== null ? doc.createNode(value) : value);
352
+ }
353
+ function documentOf(text) {
354
+ if (text !== null) {
355
+ const doc = parseDocument(text, { uniqueKeys: true });
356
+ if (doc.errors.length > 0)
357
+ throw new RequestsError(doc.errors.map((error) => ({ rule: "yaml", message: error.message })));
358
+ if (doc.contents !== null)
359
+ return doc;
360
+ }
361
+ return new Document({ format: 1, packages: {} });
362
+ }
363
+ /** Write requests, preserving the file's comments and key order. */
364
+ export async function writeRequests(file, requests) {
365
+ return (await editRequests(file, (doc) => syncNode(doc, [], requestsValue(requests)))).text;
366
+ }
367
+ /** Edit `spex.yaml` through the Document of the bytes read, then
368
+ * validate the candidate and write it under their version: a refused
369
+ * edit writes nothing, and a file changed meanwhile — or a clone gone —
370
+ * is refused as a conflict, no folder made (DR-111). */
371
+ export async function editRequests(file, edit) {
372
+ const { bytes, version } = readVersioned(file);
373
+ const doc = documentOf(bytes === null ? null : bytes.toString("utf8"));
374
+ edit(doc);
375
+ const text = doc.toString({ lineWidth: 0 });
376
+ const requests = parseRequests(text);
377
+ writeVersionedBytes(file, text, version);
378
+ return { requests, text, digest: requestsDigest(text) };
379
+ }
380
+ /** Set one package's request, or remove it with null (environments-15). */
381
+ export async function setRequest(file, name, request) {
382
+ if (request !== null) {
383
+ const issues = requestIssues(name, request);
384
+ if (issues.length > 0)
385
+ throw new RequestsError(issues);
386
+ }
387
+ return editRequests(file, (doc) => {
388
+ if (!doc.has("packages") || !isMap(doc.get("packages", true)))
389
+ doc.set("packages", doc.createNode({}));
390
+ // An empty map reads `{}`, which YAML can only write in flow style:
391
+ // its first request takes block style, as every other file's do.
392
+ const packages = doc.get("packages", true);
393
+ if (isMap(packages) && packages.items.length === 0)
394
+ packages.flow = false;
395
+ if (request === null)
396
+ doc.deleteIn(["packages", name]);
397
+ else if (doc.hasIn(["packages", name]))
398
+ syncNode(doc, ["packages", name], requestValue(request));
399
+ else
400
+ doc.setIn(["packages", name], doc.createNode(requestValue(request)));
401
+ });
402
+ }
403
+ //# sourceMappingURL=requests.js.map
@@ -0,0 +1,70 @@
1
+ import type { GitCredential, GitSource } from "./git-source.js";
2
+ import { type Lock } from "./lock.js";
3
+ import { type OwnPathIssue } from "./own-paths.js";
4
+ import { type RegistrySource } from "./registry.js";
5
+ import { type Requests } from "./requests.js";
6
+ export interface ConflictRequirement {
7
+ /** The spec package stating it, or `spex.yaml` for a request. */
8
+ by: string;
9
+ requirement: string;
10
+ }
11
+ export interface Conflict {
12
+ name: string;
13
+ requirements: ConflictRequirement[];
14
+ }
15
+ export type ResolveResult = {
16
+ ok: true;
17
+ lock: Lock;
18
+ } | {
19
+ ok: false;
20
+ conflicts: Conflict[];
21
+ };
22
+ export interface ResolveInput {
23
+ requests: Requests;
24
+ /** The `spex.yaml` text the requests were read from: the lock's
25
+ * `requests` digest is taken over these bytes. */
26
+ requestsText?: string;
27
+ registry: RegistrySource;
28
+ /** The folder a request's `path` is relative to (environments-2): the
29
+ * paired working folder's root, or your own group's clone's; null
30
+ * where this device has none. */
31
+ pathRoot: string | null;
32
+ /** Whether `pathRoot` is your own group's clone, whose requests by
33
+ * path name only a folder that syncs with it (environments-32). */
34
+ confined?: boolean;
35
+ git: GitSource;
36
+ /** The credential for a Git repository, where one applies. */
37
+ gitCredential?: (repo: string) => Promise<GitCredential | undefined>;
38
+ /** The lock being replaced. A resolve takes the highest solution
39
+ * whatever it holds (DR-104); a registry package resolving to the
40
+ * version and checksum it records keeps the registry it records
41
+ * (environments-5). */
42
+ previous?: Lock;
43
+ /** A bound on candidate trials before the search gives up. */
44
+ maxSteps?: number;
45
+ }
46
+ export type ResolveErrorKind = "no_working_folder" | "path_outside" | "path_confined" | "path_missing" | "not_a_package" | "name_mismatch" | "bound";
47
+ /** What a host needs to phrase a resolution's refusal in its own words:
48
+ * the request's folder or Git repository at its revision; whether the
49
+ * folder itself is missing; the package a folder holds in place of the
50
+ * one asked; the issues of a `meta.yaml` that does not read; and the
51
+ * reason your own group's clone refuses a folder (environments-32). */
52
+ export interface ResolveErrorDetail {
53
+ path?: string;
54
+ git?: string;
55
+ rev?: string;
56
+ folderMissing?: boolean;
57
+ found?: string;
58
+ issues?: string;
59
+ confined?: OwnPathIssue;
60
+ }
61
+ export declare class ResolveError extends Error {
62
+ readonly kind: ResolveErrorKind;
63
+ readonly packageName: string | undefined;
64
+ readonly detail: ResolveErrorDetail;
65
+ constructor(kind: ResolveErrorKind, packageName: string | undefined, message: string, detail?: ResolveErrorDetail);
66
+ }
67
+ /** Where the request file names spec packages in a conflict report. */
68
+ export declare const REQUESTS_FILE = "spex.yaml";
69
+ /** Resolve requests into a lock (environments-5, environments-6). */
70
+ export declare function resolve(input: ResolveInput): Promise<ResolveResult>;