@sublang/spex 4.2.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.
- package/README.md +433 -89
- package/dist/cli.js +80 -42
- package/dist/commands/add.d.ts +12 -0
- package/dist/commands/add.js +138 -0
- package/dist/commands/auth-target.d.ts +11 -0
- package/dist/commands/auth-target.js +50 -0
- package/dist/commands/environment.d.ts +123 -0
- package/dist/commands/environment.js +411 -0
- package/dist/commands/export.d.ts +21 -0
- package/dist/commands/export.js +663 -0
- package/dist/commands/home.d.ts +56 -0
- package/dist/commands/home.js +215 -0
- package/dist/commands/install.d.ts +2 -0
- package/dist/commands/install.js +48 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +87 -0
- package/dist/commands/login.d.ts +2 -0
- package/dist/commands/login.js +211 -0
- package/dist/commands/logout.d.ts +2 -0
- package/dist/commands/logout.js +74 -0
- package/dist/commands/publish.d.ts +2 -0
- package/dist/commands/publish.js +120 -0
- package/dist/commands/remove.d.ts +2 -0
- package/dist/commands/remove.js +34 -0
- package/dist/commands/run.d.ts +50 -0
- package/dist/commands/run.js +309 -0
- package/dist/commands/search.d.ts +2 -0
- package/dist/commands/search.js +34 -0
- package/dist/commands/settle.d.ts +124 -0
- package/dist/commands/settle.js +475 -0
- package/dist/commands/update.d.ts +2 -0
- package/dist/commands/update.js +37 -0
- package/dist/commands/whoami.d.ts +2 -0
- package/dist/commands/whoami.js +75 -0
- package/dist/lib/environment/agent-folders.d.ts +14 -0
- package/dist/lib/environment/agent-folders.js +11 -0
- package/dist/lib/environment/archive.d.ts +26 -0
- package/dist/lib/environment/archive.js +91 -0
- package/dist/lib/environment/builtins.d.ts +112 -0
- package/dist/lib/environment/builtins.js +539 -0
- package/dist/lib/environment/citations.d.ts +75 -0
- package/dist/lib/environment/citations.js +267 -0
- package/dist/lib/environment/exports.d.ts +190 -0
- package/dist/lib/environment/exports.js +1029 -0
- package/dist/lib/environment/format.d.ts +133 -0
- package/dist/lib/environment/format.js +651 -0
- package/dist/lib/environment/git-source.d.ts +41 -0
- package/dist/lib/environment/git-source.js +159 -0
- package/dist/lib/environment/index.d.ts +20 -0
- package/dist/lib/environment/index.js +27 -0
- package/dist/lib/environment/install.d.ts +140 -0
- package/dist/lib/environment/install.js +694 -0
- package/dist/lib/environment/layout.d.ts +15 -0
- package/dist/lib/environment/layout.js +50 -0
- package/dist/lib/environment/lock.d.ts +87 -0
- package/dist/lib/environment/lock.js +305 -0
- package/dist/lib/environment/marker.d.ts +15 -0
- package/dist/lib/environment/marker.js +31 -0
- package/dist/lib/environment/own-paths.d.ts +59 -0
- package/dist/lib/environment/own-paths.js +174 -0
- package/dist/lib/environment/placed-record.d.ts +32 -0
- package/dist/lib/environment/placed-record.js +72 -0
- package/dist/lib/environment/registries.d.ts +25 -0
- package/dist/lib/environment/registries.js +75 -0
- package/dist/lib/environment/registry.d.ts +173 -0
- package/dist/lib/environment/registry.js +390 -0
- package/dist/lib/environment/requests.d.ts +108 -0
- package/dist/lib/environment/requests.js +403 -0
- package/dist/lib/environment/resolve.d.ts +70 -0
- package/dist/lib/environment/resolve.js +451 -0
- package/dist/lib/environment/semver.d.ts +30 -0
- package/dist/lib/environment/semver.js +130 -0
- package/dist/lib/environment/store.d.ts +23 -0
- package/dist/lib/environment/store.js +123 -0
- package/dist/lib/environment/tar.d.ts +27 -0
- package/dist/lib/environment/tar.js +219 -0
- package/dist/lib/files.d.ts +66 -0
- package/dist/lib/files.js +233 -0
- package/dist/lib/git-host.d.ts +579 -0
- package/dist/lib/git-host.js +1363 -0
- package/dist/lib/home-file.d.ts +124 -0
- package/dist/lib/home-file.js +288 -0
- package/dist/lib/instance.d.ts +63 -0
- package/dist/lib/instance.js +130 -0
- package/dist/lib/words.d.ts +32 -0
- package/dist/lib/words.js +27 -0
- package/dist/lint.js +8 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.js +7 -0
- package/dist/testing/standin-registry.d.ts +94 -0
- package/dist/testing/standin-registry.js +449 -0
- package/package.json +37 -3
- package/dist/attached-cli.d.ts +0 -13
- package/dist/attached-cli.js +0 -249
- package/dist/attached-client.d.ts +0 -53
- package/dist/attached-client.js +0 -237
- package/dist/generated-protocol.d.ts +0 -2
- package/dist/generated-protocol.js +0 -6
- package/dist/package-init.d.ts +0 -21
- package/dist/package-init.js +0 -442
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
// The registry source (environments-12): Spex Space's `/api/v1` for
|
|
4
|
+
// format 2 (Spex Space DR-016, DR-017). The version index carries every
|
|
5
|
+
// version with its dependencies and artifacts, so a dependency graph
|
|
6
|
+
// resolves from indexes alone; a version resource is read only for a
|
|
7
|
+
// version picked; files come by raw URL or the release archive, each
|
|
8
|
+
// verified by its digest elsewhere. The app token of the instance the
|
|
9
|
+
// registry answers for is presented as a bearer where the home is signed
|
|
10
|
+
// in there; an answer refusing it as the instance's sign-out is reported
|
|
11
|
+
// with the token it refused (environments-12, DR-127).
|
|
12
|
+
import { canonicalOrigin } from "../instance.js";
|
|
13
|
+
import { isPackageName } from "./format.js";
|
|
14
|
+
export class RegistryError extends Error {
|
|
15
|
+
kind;
|
|
16
|
+
packageName;
|
|
17
|
+
code;
|
|
18
|
+
details;
|
|
19
|
+
/** The HTTP status the registry answered, where it answered one. */
|
|
20
|
+
status;
|
|
21
|
+
/** The registry that answered, by its URL, where one did. */
|
|
22
|
+
registry;
|
|
23
|
+
constructor(kind,
|
|
24
|
+
/** The spec package the answer was about, where there is one. */
|
|
25
|
+
packageName, message,
|
|
26
|
+
/** The registry's error code and issues, for a refused publish. */
|
|
27
|
+
code, details, answer = {}) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.kind = kind;
|
|
30
|
+
this.packageName = packageName;
|
|
31
|
+
this.code = code;
|
|
32
|
+
this.details = details;
|
|
33
|
+
this.name = "RegistryError";
|
|
34
|
+
if (answer.status !== undefined)
|
|
35
|
+
this.status = answer.status;
|
|
36
|
+
if (answer.registry !== undefined)
|
|
37
|
+
this.registry = answer.registry;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** A registry's URL as a reader reads it: its host, with its port where
|
|
41
|
+
* it names one. */
|
|
42
|
+
export function registryHost(url) {
|
|
43
|
+
try {
|
|
44
|
+
return new URL(url).host || url;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return url;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** How long the core waits for the registry's orgs (environments-27). */
|
|
51
|
+
export const ORGS_TIMEOUT_MS = 2_000;
|
|
52
|
+
/** The only manifest format this client reads (DR-104 schema versions). */
|
|
53
|
+
export const KNOWN_FORMAT = 2;
|
|
54
|
+
function isPlainObject(value) {
|
|
55
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
56
|
+
}
|
|
57
|
+
function splitName(name) {
|
|
58
|
+
if (!isPackageName(name))
|
|
59
|
+
throw new RegistryError("not_found", name, `"${name}" is not a <org>/<pkg> name`);
|
|
60
|
+
return name.split("/");
|
|
61
|
+
}
|
|
62
|
+
function encodePath(path) {
|
|
63
|
+
return path.split("/").map(encodeURIComponent).join("/");
|
|
64
|
+
}
|
|
65
|
+
function readArtifacts(raw) {
|
|
66
|
+
if (raw === undefined)
|
|
67
|
+
return {};
|
|
68
|
+
if (!isPlainObject(raw))
|
|
69
|
+
return undefined;
|
|
70
|
+
const out = {};
|
|
71
|
+
for (const [id, value] of Object.entries(raw)) {
|
|
72
|
+
if (!isPlainObject(value) || typeof value.kind !== "string")
|
|
73
|
+
return undefined;
|
|
74
|
+
const languages = Array.isArray(value.languages) ? value.languages.filter((item) => typeof item === "string")
|
|
75
|
+
: typeof value.language === "string" ? [value.language] : [];
|
|
76
|
+
out[id] = {
|
|
77
|
+
...value,
|
|
78
|
+
kind: value.kind,
|
|
79
|
+
...(typeof value.language === "string" ? { language: value.language } : {}),
|
|
80
|
+
languages,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
function readDependencies(raw) {
|
|
86
|
+
if (raw === undefined || raw === null)
|
|
87
|
+
return {};
|
|
88
|
+
if (!isPlainObject(raw) || !Object.values(raw).every((value) => typeof value === "string"))
|
|
89
|
+
return undefined;
|
|
90
|
+
return raw;
|
|
91
|
+
}
|
|
92
|
+
/** Validate a version index answer, or throw `unreadable`. */
|
|
93
|
+
export function readVersionIndex(name, raw) {
|
|
94
|
+
const unreadable = (why) => new RegistryError("unreadable", name, `the registry's version index for ${name} is unreadable: ${why}`);
|
|
95
|
+
if (!isPlainObject(raw) || !Array.isArray(raw.versions))
|
|
96
|
+
throw unreadable("no versions");
|
|
97
|
+
const versions = [];
|
|
98
|
+
for (const entry of raw.versions) {
|
|
99
|
+
if (!isPlainObject(entry) || typeof entry.version !== "string" || typeof entry.checksum !== "string")
|
|
100
|
+
throw unreadable("an entry lacks version or checksum");
|
|
101
|
+
const dependencies = readDependencies(entry.dependencies);
|
|
102
|
+
const artifacts = readArtifacts(entry.artifacts);
|
|
103
|
+
if (!dependencies || !artifacts)
|
|
104
|
+
throw unreadable(`version ${entry.version} has unreadable dependencies or artifacts`);
|
|
105
|
+
versions.push({
|
|
106
|
+
version: entry.version,
|
|
107
|
+
format: typeof entry.format === "number" ? entry.format : -1,
|
|
108
|
+
...(typeof entry.published_at === "string" ? { published_at: entry.published_at } : {}),
|
|
109
|
+
checksum: entry.checksum,
|
|
110
|
+
yanked: entry.yanked === true,
|
|
111
|
+
suppressed: entry.suppressed === true,
|
|
112
|
+
dependencies,
|
|
113
|
+
artifacts,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const [org, pkg] = name.split("/");
|
|
117
|
+
return { org: typeof raw.org === "string" ? raw.org : org, name: typeof raw.name === "string" ? raw.name : pkg, latest: typeof raw.latest === "string" ? raw.latest : null, versions };
|
|
118
|
+
}
|
|
119
|
+
/** Validate a version resource answer: a format this client does not
|
|
120
|
+
* know is refused naming it (environments-12). */
|
|
121
|
+
export function readVersionResource(name, version, raw) {
|
|
122
|
+
const unreadable = (why) => new RegistryError("unreadable", name, `the registry's answer for ${name} ${version} is unreadable: ${why}`);
|
|
123
|
+
if (!isPlainObject(raw))
|
|
124
|
+
throw unreadable("not an object");
|
|
125
|
+
if (raw.format !== KNOWN_FORMAT) {
|
|
126
|
+
throw new RegistryError("format", name, `${name} ${version} is in format ${JSON.stringify(raw.format ?? null)}, which this Spex does not read`);
|
|
127
|
+
}
|
|
128
|
+
if (typeof raw.version !== "string" || typeof raw.checksum !== "string")
|
|
129
|
+
throw unreadable("no version or checksum");
|
|
130
|
+
const dependencies = readDependencies(raw.dependencies);
|
|
131
|
+
const artifacts = readArtifacts(raw.artifacts);
|
|
132
|
+
if (!dependencies || !artifacts)
|
|
133
|
+
throw unreadable("unreadable dependencies or artifacts");
|
|
134
|
+
let files;
|
|
135
|
+
if (raw.files !== undefined) {
|
|
136
|
+
if (!Array.isArray(raw.files))
|
|
137
|
+
throw unreadable("files is not a list");
|
|
138
|
+
files = raw.files.map((file) => {
|
|
139
|
+
if (!isPlainObject(file) || typeof file.path !== "string" || typeof file.sha256 !== "string" || !/^[0-9a-f]{64}$/.test(file.sha256)) {
|
|
140
|
+
throw unreadable("a file entry lacks path or sha256");
|
|
141
|
+
}
|
|
142
|
+
return { path: file.path, size: typeof file.size === "number" ? file.size : 0, sha256: file.sha256, executable: file.executable === true };
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
...raw,
|
|
147
|
+
org: String(raw.org ?? name.split("/")[0]),
|
|
148
|
+
name: String(raw.name ?? name.split("/")[1]),
|
|
149
|
+
version: raw.version,
|
|
150
|
+
format: KNOWN_FORMAT,
|
|
151
|
+
artifacts: artifacts,
|
|
152
|
+
dependencies,
|
|
153
|
+
checksum: raw.checksum,
|
|
154
|
+
yanked: raw.yanked === true,
|
|
155
|
+
suppressed: raw.suppressed === true,
|
|
156
|
+
...(files ? { files } : {}),
|
|
157
|
+
...(isPlainObject(raw.dist) && typeof raw.dist.tarball === "string" ? { dist: { tarball: raw.dist.tarball, sha256: String(raw.dist.sha256 ?? raw.checksum) } } : {}),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/** The codes a registry refuses an instance's token with that sign that
|
|
161
|
+
* instance out (environments-12, git-host-4); `token_expired` is not one
|
|
162
|
+
* of them, an expired access secret being a failure of its call. */
|
|
163
|
+
export const SIGN_OUT_CODES = ["invalid_token", "token_revoked", "reauth_required"];
|
|
164
|
+
/** Spex Space over HTTP (environments-12). */
|
|
165
|
+
export class RegistryClient {
|
|
166
|
+
url;
|
|
167
|
+
token;
|
|
168
|
+
refused;
|
|
169
|
+
fetcher;
|
|
170
|
+
constructor(options) {
|
|
171
|
+
this.url = options.url.replace(/\/+$/, "");
|
|
172
|
+
this.token = options.token;
|
|
173
|
+
this.refused = options.refused;
|
|
174
|
+
this.fetcher = options.fetch ?? fetch;
|
|
175
|
+
}
|
|
176
|
+
/** Public discovery never presents a stored credential. */
|
|
177
|
+
async discovery(path) {
|
|
178
|
+
const response = await this.fetcher(`${this.url}${path}`, { headers: { accept: "application/json" }, redirect: "manual", signal: AbortSignal.timeout(ORGS_TIMEOUT_MS) });
|
|
179
|
+
if (!response.ok)
|
|
180
|
+
throw new RegistryError("unavailable", undefined, `service discovery answered ${response.status}`);
|
|
181
|
+
const raw = await response.json();
|
|
182
|
+
if (!isPlainObject(raw))
|
|
183
|
+
throw new RegistryError("unreadable", undefined, "service discovery is unreadable");
|
|
184
|
+
return raw;
|
|
185
|
+
}
|
|
186
|
+
async describe() {
|
|
187
|
+
const raw = await this.discovery("/api/v1/registry");
|
|
188
|
+
if (typeof raw.origin !== "string" || typeof raw.display_name !== "string" || !Array.isArray(raw.providers) || !raw.providers.every((provider) => typeof provider === "string")) {
|
|
189
|
+
throw new RegistryError("unreadable", undefined, "registry descriptor is unreadable");
|
|
190
|
+
}
|
|
191
|
+
return { origin: canonicalOrigin(raw.origin), displayName: raw.display_name, providers: raw.providers };
|
|
192
|
+
}
|
|
193
|
+
async spaces() {
|
|
194
|
+
const raw = await this.discovery("/api/v1/spaces");
|
|
195
|
+
if (!Array.isArray(raw.spaces))
|
|
196
|
+
throw new RegistryError("unreadable", undefined, "Space directory is unreadable");
|
|
197
|
+
return raw.spaces.map((entry) => {
|
|
198
|
+
if (!isPlainObject(entry) || typeof entry.origin !== "string" || typeof entry.display_name !== "string" || !Array.isArray(entry.sign_in) || !entry.sign_in.every((provider) => typeof provider === "string")) {
|
|
199
|
+
throw new RegistryError("unreadable", undefined, "Space directory is unreadable");
|
|
200
|
+
}
|
|
201
|
+
return { origin: canonicalOrigin(entry.origin), displayName: entry.display_name, signIn: entry.sign_in };
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
async request(path, name, init = {}) {
|
|
205
|
+
const headers = new Headers(init.headers);
|
|
206
|
+
if (!headers.has("accept"))
|
|
207
|
+
headers.set("accept", "application/json");
|
|
208
|
+
const about = name ? `${name}: ` : "";
|
|
209
|
+
const host = registryHost(this.url);
|
|
210
|
+
const at = { registry: this.url };
|
|
211
|
+
let token = null;
|
|
212
|
+
try {
|
|
213
|
+
token = this.token ? await this.token() : null;
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
// No token to be had: the call fails as one the registry could
|
|
217
|
+
// not be asked, so a caller with another registry moves on.
|
|
218
|
+
throw new RegistryError("unavailable", name, `${about}${error instanceof Error ? error.message : String(error)}`, undefined, undefined, at);
|
|
219
|
+
}
|
|
220
|
+
if (token)
|
|
221
|
+
headers.set("authorization", `Bearer ${token}`);
|
|
222
|
+
let response;
|
|
223
|
+
try {
|
|
224
|
+
response = await this.fetcher(`${this.url}${path}`, { ...init, headers, redirect: "manual" });
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// The runtime's own words (`fetch failed`) name no cause a reader
|
|
228
|
+
// can act on.
|
|
229
|
+
throw new RegistryError("unavailable", name, `${about}${host} could not be reached`, "unreachable", undefined, at);
|
|
230
|
+
}
|
|
231
|
+
if (response.ok)
|
|
232
|
+
return response;
|
|
233
|
+
let code;
|
|
234
|
+
let message;
|
|
235
|
+
let details;
|
|
236
|
+
try {
|
|
237
|
+
const body = (await response.json());
|
|
238
|
+
code = body.error?.code;
|
|
239
|
+
message = body.error?.message;
|
|
240
|
+
details = body.error?.details;
|
|
241
|
+
}
|
|
242
|
+
catch { /* not the envelope */ }
|
|
243
|
+
const said = message ? ` (${message})` : "";
|
|
244
|
+
const answered = { status: response.status, registry: this.url };
|
|
245
|
+
// A private package answers a reader it does not share with as one
|
|
246
|
+
// that is not there.
|
|
247
|
+
if (response.status === 404)
|
|
248
|
+
throw new RegistryError("not_found", name, `${about}not at ${host}, or private`, code, undefined, answered);
|
|
249
|
+
if (response.status === 410)
|
|
250
|
+
throw new RegistryError("not_found", name, `${about}withdrawn at ${host}`, code ?? "suppressed", undefined, answered);
|
|
251
|
+
if (response.status === 401) {
|
|
252
|
+
// A refusal answers the token this call presented (git-host-4); a
|
|
253
|
+
// hook that throws says why the call fails instead.
|
|
254
|
+
if (token && code !== undefined && SIGN_OUT_CODES.includes(code) && this.refused) {
|
|
255
|
+
try {
|
|
256
|
+
await this.refused(token, code);
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
throw new RegistryError("unauthenticated", name, `${about}${error instanceof Error ? error.message : String(error)}`, code, undefined, answered);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
throw new RegistryError("unauthenticated", name, `${about}sign-in expired at ${host}`, code, undefined, answered);
|
|
263
|
+
}
|
|
264
|
+
if (response.status === 403)
|
|
265
|
+
throw new RegistryError("forbidden", name, `${about}not shared with this account at ${host}`, code, undefined, answered);
|
|
266
|
+
if (response.status === 409 || response.status === 422 || response.status === 413) {
|
|
267
|
+
throw new RegistryError("rejected", name, `${about}${host} refused it${said}`, code, details, answered);
|
|
268
|
+
}
|
|
269
|
+
if (response.status >= 500)
|
|
270
|
+
throw new RegistryError("unavailable", name, `${about}${host} is down (${response.status})`, code, undefined, answered);
|
|
271
|
+
throw new RegistryError("unavailable", name, `${about}${host} answered ${response.status}${said}`, code, undefined, answered);
|
|
272
|
+
}
|
|
273
|
+
async json(path, name, init = {}) {
|
|
274
|
+
const response = await this.request(path, name, init);
|
|
275
|
+
try {
|
|
276
|
+
return await response.json();
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
throw new RegistryError("unreadable", name, `${name ?? "the registry"}: the registry's answer is not JSON: ${error.message}`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
async index(name) {
|
|
283
|
+
const [org, pkg] = splitName(name);
|
|
284
|
+
return readVersionIndex(name, await this.json(`/api/v1/packages/${org}/${pkg}`, name));
|
|
285
|
+
}
|
|
286
|
+
async version(name, version) {
|
|
287
|
+
const [org, pkg] = splitName(name);
|
|
288
|
+
return readVersionResource(name, version, await this.json(`/api/v1/packages/${org}/${pkg}/${encodeURIComponent(version)}`, name));
|
|
289
|
+
}
|
|
290
|
+
async file(name, version, path) {
|
|
291
|
+
const [org, pkg] = splitName(name);
|
|
292
|
+
const response = await this.request(`/${org}/${pkg}/${encodeURIComponent(version)}/${encodePath(path)}`, name, { headers: { accept: "*/*" } });
|
|
293
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
294
|
+
}
|
|
295
|
+
async archive(name, version) {
|
|
296
|
+
const [org, pkg] = splitName(name);
|
|
297
|
+
const response = await this.request(`/api/v1/packages/${org}/${pkg}/${encodeURIComponent(version)}/download`, name, { headers: { accept: "application/gzip" } });
|
|
298
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
299
|
+
}
|
|
300
|
+
/** A publish's refusals name what the publisher can act on: an org
|
|
301
|
+
* not theirs, a version already there, or the registry's checks. */
|
|
302
|
+
async publish(name, version, tgz) {
|
|
303
|
+
const [org, pkg] = splitName(name);
|
|
304
|
+
const host = registryHost(this.url);
|
|
305
|
+
try {
|
|
306
|
+
await this.request(`/api/v1/packages/${org}/${pkg}/${encodeURIComponent(version)}`, name, {
|
|
307
|
+
method: "PUT",
|
|
308
|
+
headers: { "content-type": "application/gzip" },
|
|
309
|
+
body: tgz,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
if (!(error instanceof RegistryError))
|
|
314
|
+
throw error;
|
|
315
|
+
const answer = { ...(error.status !== undefined ? { status: error.status } : {}), registry: this.url };
|
|
316
|
+
if (error.kind === "forbidden")
|
|
317
|
+
throw new RegistryError("forbidden", name, `${org} is not yours at ${host}`, error.code, error.details, answer);
|
|
318
|
+
if (error.kind === "rejected" && error.status === 409)
|
|
319
|
+
throw new RegistryError("rejected", name, `${name} ${version} is already at ${host}`, error.code, error.details, answer);
|
|
320
|
+
if (error.kind === "rejected" && error.status === 413)
|
|
321
|
+
throw new RegistryError("rejected", name, `${name} ${version} is too large for ${host}`, error.code, error.details, answer);
|
|
322
|
+
if (error.kind === "rejected")
|
|
323
|
+
throw new RegistryError("rejected", name, `${host} refused ${name} ${version}`, error.code, error.details, answer);
|
|
324
|
+
throw error;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/** The orgs the signed-in person may publish to (environments-27):
|
|
328
|
+
* `GET /api/v1/account/orgs` with the device's app token, read within
|
|
329
|
+
* the timeout; null for no answer — a refusal, any other status, a
|
|
330
|
+
* body not in its shape, a network failure, or none in time. */
|
|
331
|
+
async orgs(timeoutMs = ORGS_TIMEOUT_MS) {
|
|
332
|
+
return this.readOrgs(timeoutMs).catch(() => null);
|
|
333
|
+
}
|
|
334
|
+
async readOrgs(timeoutMs) {
|
|
335
|
+
const signal = AbortSignal.timeout(timeoutMs);
|
|
336
|
+
const late = new Promise((resolve) => signal.addEventListener("abort", () => resolve(null), { once: true }));
|
|
337
|
+
const asked = (async () => {
|
|
338
|
+
const response = await this.request("/api/v1/account/orgs", undefined, { signal });
|
|
339
|
+
if (response.status !== 200)
|
|
340
|
+
return null;
|
|
341
|
+
const raw = await response.json();
|
|
342
|
+
if (!isPlainObject(raw) || !Array.isArray(raw.orgs))
|
|
343
|
+
return null;
|
|
344
|
+
const orgs = [];
|
|
345
|
+
for (const org of raw.orgs) {
|
|
346
|
+
if (!isPlainObject(org) || typeof org.name !== "string" || typeof org.path !== "string" || typeof org.role !== "string")
|
|
347
|
+
return null;
|
|
348
|
+
orgs.push({ name: org.name, path: org.path, role: org.role, ...(typeof org.provider === "string" ? { provider: org.provider } : {}) });
|
|
349
|
+
}
|
|
350
|
+
return orgs;
|
|
351
|
+
})();
|
|
352
|
+
return Promise.race([asked, late]);
|
|
353
|
+
}
|
|
354
|
+
/** Check publishing rights before a preview or upload. An unreadable
|
|
355
|
+
* answer is not an empty list: neither case authorizes publishing. */
|
|
356
|
+
async checkPublishOrg(name, timeoutMs = 30_000) {
|
|
357
|
+
const [org] = splitName(name);
|
|
358
|
+
const orgs = await this.readOrgs(timeoutMs).catch((error) => {
|
|
359
|
+
// Keep an expired/revoked sign-in distinct from an unreadable or
|
|
360
|
+
// unavailable list; request() already applied its refusal hook.
|
|
361
|
+
if (error instanceof RegistryError && error.kind === "unauthenticated")
|
|
362
|
+
throw error;
|
|
363
|
+
return null;
|
|
364
|
+
});
|
|
365
|
+
if (orgs === null) {
|
|
366
|
+
throw new RegistryError("unavailable", name, `Your orgs at ${registryHost(this.url)} could not be read`, "orgs_unavailable", undefined, { registry: this.url });
|
|
367
|
+
}
|
|
368
|
+
if (!orgs.some((entry) => entry.name === org)) {
|
|
369
|
+
throw new RegistryError("forbidden", name, `${org} is not yours at ${registryHost(this.url)}`, "org_not_owned", undefined, { registry: this.url });
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
async search(query) {
|
|
373
|
+
const raw = await this.json(`/api/v1/search?q=${encodeURIComponent(query)}`, undefined);
|
|
374
|
+
const results = isPlainObject(raw) && Array.isArray(raw.results) ? raw.results : [];
|
|
375
|
+
const out = [];
|
|
376
|
+
for (const result of results) {
|
|
377
|
+
if (!isPlainObject(result) || typeof result.org !== "string" || typeof result.name !== "string")
|
|
378
|
+
continue;
|
|
379
|
+
const name = `${result.org}/${result.name}`;
|
|
380
|
+
let versions = [];
|
|
381
|
+
try {
|
|
382
|
+
versions = (await this.index(name)).versions.filter((entry) => !entry.suppressed && !entry.yanked).map((entry) => entry.version);
|
|
383
|
+
}
|
|
384
|
+
catch { /* listed without versions */ }
|
|
385
|
+
out.push({ name, description: typeof result.description === "string" ? result.description : "", versions });
|
|
386
|
+
}
|
|
387
|
+
return out;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Document } from "yaml";
|
|
2
|
+
import type { AuthScope } from "../git-host.js";
|
|
3
|
+
import { type HomeFile } from "../home-file.js";
|
|
4
|
+
import { type Issue } from "./format.js";
|
|
5
|
+
export interface Selection {
|
|
6
|
+
artifact: string;
|
|
7
|
+
language?: string;
|
|
8
|
+
}
|
|
9
|
+
interface RequestExtras {
|
|
10
|
+
select?: Selection[];
|
|
11
|
+
alias?: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
export type RegistryRequest = {
|
|
14
|
+
version: string;
|
|
15
|
+
} & RequestExtras;
|
|
16
|
+
export type PathRequest = {
|
|
17
|
+
path: string;
|
|
18
|
+
} & RequestExtras;
|
|
19
|
+
export type GitRequest = {
|
|
20
|
+
git: string;
|
|
21
|
+
rev: string;
|
|
22
|
+
path?: string;
|
|
23
|
+
} & RequestExtras;
|
|
24
|
+
export type Request = RegistryRequest | PathRequest | GitRequest;
|
|
25
|
+
export interface Requests {
|
|
26
|
+
format: 1;
|
|
27
|
+
language?: string;
|
|
28
|
+
/** The origin of the environment's instance, as `spex.yaml` names it
|
|
29
|
+
* (environments-2). */
|
|
30
|
+
space?: string;
|
|
31
|
+
/** Package service selected independently from Space. */
|
|
32
|
+
registry?: string;
|
|
33
|
+
packages: Record<string, Request>;
|
|
34
|
+
}
|
|
35
|
+
export declare class RequestsError extends Error {
|
|
36
|
+
readonly issues: Issue[];
|
|
37
|
+
/** The format a `spex.yaml` from a newer Spex names, where it names one. */
|
|
38
|
+
readonly newerFormat?: number;
|
|
39
|
+
constructor(issues: Issue[], newerFormat?: number);
|
|
40
|
+
}
|
|
41
|
+
export declare function isRegistryRequest(request: Request): request is RegistryRequest;
|
|
42
|
+
export declare function isPathRequest(request: Request): request is PathRequest;
|
|
43
|
+
export declare function isGitRequest(request: Request): request is GitRequest;
|
|
44
|
+
/** Why a name is no package name, with the rule, one wording wherever a
|
|
45
|
+
* name is refused. */
|
|
46
|
+
export declare function packageNameRefusal(name: string): string;
|
|
47
|
+
/** Whether a request path stays inside the working folder. */
|
|
48
|
+
export declare function isInsidePath(path: string): boolean;
|
|
49
|
+
/** Every reason a request for a name is refused (environments-2,
|
|
50
|
+
* environments-15): a bad name, no or several sources, a malformed
|
|
51
|
+
* requirement, a path that is not relative or leads out of the folder
|
|
52
|
+
* it is relative to, a bad select or alias. */
|
|
53
|
+
export declare function requestIssues(name: string, request: unknown): Issue[];
|
|
54
|
+
/** Read a parsed `spex.yaml` value as requests, or throw. */
|
|
55
|
+
export declare function parseRequestsValue(raw: unknown): Requests;
|
|
56
|
+
/** The origin of an environment's instance (environments-2, DR-165): the
|
|
57
|
+
* `space` its `spex.yaml` names, else the home's default instance —
|
|
58
|
+
* the first it lists, or the origin it was created with while it lists
|
|
59
|
+
* none (storage-2) — canonical. The registry, the credential brokered
|
|
60
|
+
* for Git sources, the orgs a person may publish to and the publish
|
|
61
|
+
* destination all follow it. */
|
|
62
|
+
export declare function environmentInstance(requests: Pick<Requests, "space"> | null, home: Pick<HomeFile, "origin" | "instances">): string;
|
|
63
|
+
/** The public package service used explicitly by new environments. */
|
|
64
|
+
export declare const PUBLIC_REGISTRY_ORIGIN = "https://spex.sublang.ai";
|
|
65
|
+
export interface EnvironmentRegistry {
|
|
66
|
+
origin: string;
|
|
67
|
+
scope: AuthScope;
|
|
68
|
+
legacy: boolean;
|
|
69
|
+
}
|
|
70
|
+
/** Existing manifests without registry preserve their former source and
|
|
71
|
+
* credential. An absent manifest starts in independent registry mode. */
|
|
72
|
+
export declare function environmentRegistry(requests: Pick<Requests, "space" | "registry"> | null, home: Pick<HomeFile, "origin" | "instances">): EnvironmentRegistry;
|
|
73
|
+
/** Name a package registry without changing the Space binding. */
|
|
74
|
+
export declare function setRegistryIn(doc: Document, origin: string): void;
|
|
75
|
+
/** Parse `spex.yaml` text, or throw. */
|
|
76
|
+
export declare function parseRequests(text: string): Requests;
|
|
77
|
+
/** The lock's `requests` field: the SHA-256 of the file's bytes. */
|
|
78
|
+
export declare function requestsDigest(text: string | Uint8Array): string;
|
|
79
|
+
export declare function readRequests(file: string): Promise<Requests>;
|
|
80
|
+
/** Read `spex.yaml` with its text and digest; null where absent. */
|
|
81
|
+
export declare function readRequestsFile(file: string): {
|
|
82
|
+
requests: Requests;
|
|
83
|
+
text: string;
|
|
84
|
+
digest: string;
|
|
85
|
+
} | null;
|
|
86
|
+
/** Name the environment's instance in a `spex.yaml` document
|
|
87
|
+
* (environments-2): `space` set to `origin`, a new key placed before
|
|
88
|
+
* `packages` so the file reads format, language, space, packages; null
|
|
89
|
+
* removes it. Comments and the order of every other key stay. */
|
|
90
|
+
export declare function setSpaceIn(doc: Document, origin: string | null): void;
|
|
91
|
+
/** Write requests, preserving the file's comments and key order. */
|
|
92
|
+
export declare function writeRequests(file: string, requests: Requests): Promise<string>;
|
|
93
|
+
/** Edit `spex.yaml` through the Document of the bytes read, then
|
|
94
|
+
* validate the candidate and write it under their version: a refused
|
|
95
|
+
* edit writes nothing, and a file changed meanwhile — or a clone gone —
|
|
96
|
+
* is refused as a conflict, no folder made (DR-111). */
|
|
97
|
+
export declare function editRequests(file: string, edit: (doc: Document) => void): Promise<{
|
|
98
|
+
requests: Requests;
|
|
99
|
+
text: string;
|
|
100
|
+
digest: string;
|
|
101
|
+
}>;
|
|
102
|
+
/** Set one package's request, or remove it with null (environments-15). */
|
|
103
|
+
export declare function setRequest(file: string, name: string, request: Request | null): Promise<{
|
|
104
|
+
requests: Requests;
|
|
105
|
+
text: string;
|
|
106
|
+
digest: string;
|
|
107
|
+
}>;
|
|
108
|
+
export {};
|