passport-bridge 0.48.2 → 0.49.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/dist/cliHookAfter.js +10 -1
- package/dist/cliHookDecide.d.ts +9 -3
- package/dist/cliHookDecide.js +182 -28
- package/dist/cliHookSessionState.d.ts +4 -1
- package/dist/cliHookSessionState.js +1 -1
- package/dist/connectorKit/callEffects.generated.d.ts +36 -0
- package/dist/connectorKit/callEffects.generated.js +73 -0
- package/dist/connectorKit/cli.generated.js +5 -2
- package/dist/connectorKit/engine.generated.d.ts +1 -1
- package/dist/connectorKit/engine.generated.js +32 -13
- package/dist/connectorKit/githubIds.generated.d.ts +1 -1
- package/dist/connectorKit/githubIds.generated.js +3 -2
- package/dist/connectorKit/manifest.generated.d.ts +34 -14
- package/dist/connectorKit/manifests/cloudflare.generated.d.ts +2 -0
- package/dist/connectorKit/manifests/cloudflare.generated.js +186 -0
- package/dist/connectorKit/manifests/cloudflareApi.generated.d.ts +4 -0
- package/dist/connectorKit/manifests/cloudflareApi.generated.js +207 -0
- package/dist/connectorKit/manifests/cloudflareCatalog.generated.d.ts +18 -0
- package/dist/connectorKit/manifests/cloudflareCatalog.generated.js +83 -0
- package/dist/connectorKit/manifests/cloudflareCli.generated.d.ts +2 -0
- package/dist/connectorKit/manifests/cloudflareCli.generated.js +441 -0
- package/dist/connectorKit/manifests/githubCapture.generated.js +19 -1
- package/dist/connectorKit/manifests/githubReads.generated.js +0 -3
- package/dist/connectorKit/manifests/kubernetes.generated.d.ts +76 -0
- package/dist/connectorKit/manifests/kubernetes.generated.js +210 -0
- package/dist/connectorKit/manifests/kubernetesArgs.generated.d.ts +21 -0
- package/dist/connectorKit/manifests/kubernetesArgs.generated.js +82 -0
- package/dist/connectorKit/manifests/kubernetesCli.generated.d.ts +30 -0
- package/dist/connectorKit/manifests/kubernetesCli.generated.js +530 -0
- package/dist/connectorKit/manifests/kubernetesEffects.generated.d.ts +15 -0
- package/dist/connectorKit/manifests/kubernetesEffects.generated.js +111 -0
- package/dist/connectorKit/manifests/kubernetesObjects.generated.d.ts +24 -0
- package/dist/connectorKit/manifests/kubernetesObjects.generated.js +154 -0
- package/dist/connectorKit/manifests/railwayHooks.generated.js +11 -5
- package/dist/connectorKit/manifests/sentry.generated.d.ts +6 -0
- package/dist/connectorKit/manifests/sentry.generated.js +27 -0
- package/dist/connectorKit/manifests/sentryApi.generated.d.ts +6 -0
- package/dist/connectorKit/manifests/sentryApi.generated.js +282 -0
- package/dist/connectorKit/manifests/sentryCapture.generated.d.ts +2 -0
- package/dist/connectorKit/manifests/sentryCapture.generated.js +165 -0
- package/dist/connectorKit/manifests/sentryCatalog.generated.d.ts +5 -0
- package/dist/connectorKit/manifests/sentryCatalog.generated.js +70 -0
- package/dist/connectorKit/manifests/sentryCli.generated.d.ts +2 -0
- package/dist/connectorKit/manifests/sentryCli.generated.js +230 -0
- package/dist/connectorKit/manifests/sentryIssues.generated.d.ts +20 -0
- package/dist/connectorKit/manifests/sentryIssues.generated.js +204 -0
- package/dist/connectorKit/manifests/supabase.generated.js +3 -2
- package/dist/connectorKit/manifests/supabaseApi.generated.js +52 -52
- package/dist/connectorKit/manifests/supabaseCapture.generated.js +22 -21
- package/dist/connectorKit/manifests/supabaseCatalog.generated.d.ts +2 -2
- package/dist/connectorKit/manifests/supabaseCatalog.generated.js +8 -7
- package/dist/connectorKit/manifests/supabaseCli.generated.js +32 -32
- package/dist/connectorKit/manifests/supabaseProbes.generated.js +29 -29
- package/dist/connectorKit/manifests/terraform.generated.d.ts +5 -0
- package/dist/connectorKit/manifests/terraform.generated.js +214 -0
- package/dist/connectorKit/manifests/terraformLink.generated.d.ts +30 -0
- package/dist/connectorKit/manifests/terraformLink.generated.js +73 -0
- package/dist/connectorKit/manifests/terraformPlan.generated.d.ts +48 -0
- package/dist/connectorKit/manifests/terraformPlan.generated.js +192 -0
- package/dist/connectorKit/params.generated.js +2 -0
- package/dist/connectorKit/shell.generated.d.ts +5 -1
- package/dist/connectorKit/shell.generated.js +121 -20
- package/dist/connectorKit/sources/cloudflare/api-operations.generated.d.ts +37 -0
- package/dist/connectorKit/sources/cloudflare/api-operations.generated.js +2 -0
- package/dist/connectorKit/sources/cloudflare/cli-flags.generated.d.ts +617 -0
- package/dist/connectorKit/sources/cloudflare/cli-flags.generated.js +2 -0
- package/dist/connectorKit/sources/cloudflare/mcp-tools.generated.d.ts +16 -0
- package/dist/connectorKit/sources/cloudflare/mcp-tools.generated.js +2 -0
- package/dist/connectorKit/sources/sentry/api-operations.generated.d.ts +121 -0
- package/dist/connectorKit/sources/sentry/api-operations.generated.js +2 -0
- package/dist/connectorKit/sources/sentry/cli-commands.generated.d.ts +530 -0
- package/dist/connectorKit/sources/sentry/cli-commands.generated.js +2 -0
- package/dist/connectorKit/sources/sentry/mcp-tools.generated.d.ts +32 -0
- package/dist/connectorKit/sources/sentry/mcp-tools.generated.js +2 -0
- package/dist/connectorKit/template.generated.d.ts +8 -4
- package/dist/connectorKit/template.generated.js +19 -6
- package/dist/controlPlane.generated.d.ts +11 -2
- package/dist/controlPlaneActions.generated.d.ts +52 -12
- package/dist/controlPlaneActions.generated.js +40 -11
- package/dist/controlPlaneCloudflare.d.ts +70 -0
- package/dist/controlPlaneCloudflare.js +188 -0
- package/dist/controlPlaneKubernetes.d.ts +72 -0
- package/dist/controlPlaneKubernetes.js +260 -0
- package/dist/controlPlaneModel.generated.d.ts +6 -0
- package/dist/controlPlaneNpm.generated.d.ts +83 -0
- package/dist/controlPlaneNpm.generated.js +307 -0
- package/dist/controlPlaneNpmCommands.generated.d.ts +35 -0
- package/dist/controlPlaneNpmCommands.generated.js +567 -0
- package/dist/controlPlaneNpmGuard.d.ts +44 -0
- package/dist/controlPlaneNpmGuard.js +210 -0
- package/dist/controlPlaneOffline.d.ts +1 -1
- package/dist/controlPlaneRailway.generated.js +2 -2
- package/dist/controlPlaneRailwayCli.generated.js +13 -2
- package/dist/controlPlaneRailwayModel.generated.d.ts +5 -0
- package/dist/controlPlaneRailwayModel.generated.js +15 -1
- package/dist/controlPlaneSentry.d.ts +68 -0
- package/dist/controlPlaneSentry.js +205 -0
- package/dist/controlPlaneTerraform.d.ts +86 -0
- package/dist/controlPlaneTerraform.js +273 -0
- package/dist/controlPlaneUnnamed.generated.d.ts +41 -2
- package/dist/controlPlaneUnnamed.generated.js +148 -2
- package/dist/cpCli.d.ts +1 -1
- package/dist/cpCli.js +1 -1
- package/dist/pluginPin.d.ts +1 -1
- package/dist/pluginPin.js +1 -1
- package/dist/terraformPlans.d.ts +45 -0
- package/dist/terraformPlans.js +96 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The control plane for Kubernetes on this computer (`kubectl` and `helm`), through the shared classifier
|
|
3
|
+
* (`shared/connectorKit/manifests/kubernetesCli.ts`, copied to `connectorKit/` by
|
|
4
|
+
* scripts/generate-control-plane-classifier.mjs). The guard classifies each command in a line, in the place it runs:
|
|
5
|
+
* the kubeconfig context (`--context`, `--kubeconfig`, `KUBECONFIG`, else `~/.kube/config`; names only, read by
|
|
6
|
+
* `kubeconfig.ts`, which never copies a credential), the folder (`cd dir && …`) its `-f` files are read from, and
|
|
7
|
+
* Helm's `HELM_*` variables. It asks `POST /api/cp/decide` once per command: everything one command does is one
|
|
8
|
+
* bundle, decided as a whole. Nothing on a cluster is read, and no cluster is ever contacted.
|
|
9
|
+
*
|
|
10
|
+
* Only the classification leaves this computer: nodes named from the context and the command's resource names, the
|
|
11
|
+
* catalog actions, fixed summaries, and the command masked (every flag value, and everything after `--`, replaced
|
|
12
|
+
* with `…`). Never a manifest's contents, a Secret's value, a token or a literal.
|
|
13
|
+
*/
|
|
14
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
15
|
+
import { homedir } from "node:os";
|
|
16
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
17
|
+
import { normalizeArgv, splitCommands } from "./cliHookParse.js";
|
|
18
|
+
import { readBoundedJson } from "./boundedResponse.js";
|
|
19
|
+
import { classifyKube, isKubeProgram } from "./connectorKit/manifests/kubernetesCli.generated.js";
|
|
20
|
+
import { MAX_MANIFEST_OBJECTS, readManifest } from "./connectorKit/manifests/kubernetesObjects.generated.js";
|
|
21
|
+
import { CP_DECIDE_PATH, CP_DECIDE_TIMEOUT_MS, worstAnswer } from "./controlPlaneGuard.js";
|
|
22
|
+
import { maskCommandArgv } from "./controlPlaneUnnamed.generated.js";
|
|
23
|
+
import { newRequestId } from "./controlPlaneLate.js";
|
|
24
|
+
import { NO_ANSWER_STATUSES, noAnswer, refusalOf } from "./cpRefusal.js";
|
|
25
|
+
import { NODE_LINK_FS } from "./envLinkFs.js";
|
|
26
|
+
import { kubeconfigFiles, readKubeconfig } from "./kubeconfig.js";
|
|
27
|
+
/** At most this many kubectl or helm commands in one line are asked about; a line doing more is refused. */
|
|
28
|
+
export const MAX_KUBE_COMMANDS = 8;
|
|
29
|
+
/** A bundle holds at most this many actions (the server's bound). */
|
|
30
|
+
export const MAX_KUBE_BUNDLE = 32;
|
|
31
|
+
export const TOO_MANY_KUBE = `Blocked by Passport: this command changes more than ${MAX_KUBE_BUNDLE} things on Kubernetes at once. Split it up so each can be checked.`;
|
|
32
|
+
const MAX_SUMMARY = 200;
|
|
33
|
+
/** A manifest file bigger than this isn't read (what it declares is unknown). */
|
|
34
|
+
export const MAX_MANIFEST_BYTES = 1024 * 1024;
|
|
35
|
+
/** At most this many files of one command's `-f` paths (folders included) are read. */
|
|
36
|
+
export const MAX_MANIFEST_FILES = 32;
|
|
37
|
+
const MANIFEST_FILE = /\.(?:ya?ml|json)$/i;
|
|
38
|
+
/** The files one `-f` path names: itself, or a folder's manifests (`-R`: its subfolders too). */
|
|
39
|
+
function manifestFiles(path, recursive, depth = 0) {
|
|
40
|
+
let stat;
|
|
41
|
+
try {
|
|
42
|
+
stat = statSync(path);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
if (stat.isFile())
|
|
48
|
+
return [path];
|
|
49
|
+
if (!stat.isDirectory() || depth > 4)
|
|
50
|
+
return null;
|
|
51
|
+
const out = [];
|
|
52
|
+
for (const entry of readdirSync(path, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
53
|
+
const full = join(path, entry.name);
|
|
54
|
+
if (entry.isFile() && MANIFEST_FILE.test(entry.name))
|
|
55
|
+
out.push(full);
|
|
56
|
+
else if (recursive && entry.isDirectory())
|
|
57
|
+
out.push(...(manifestFiles(full, true, depth + 1) ?? []));
|
|
58
|
+
if (out.length > MAX_MANIFEST_FILES)
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
/** What the `-f` paths declare, read here: a URL, `-`, a relative path with no folder known, a file too big, or
|
|
64
|
+
* anything that can't be read is unread. */
|
|
65
|
+
export function readManifests(paths, recursive, cwd) {
|
|
66
|
+
const out = { objects: [], unread: false };
|
|
67
|
+
let files = 0;
|
|
68
|
+
for (const path of paths) {
|
|
69
|
+
if (!path || path === "-" || /^[a-z][a-z0-9+.-]*:\/\//i.test(path) || (!isAbsolute(path) && !cwd)) {
|
|
70
|
+
out.unread = true;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const found = manifestFiles(isAbsolute(path) ? path : resolve(cwd, path), recursive);
|
|
74
|
+
if (!found || !found.length) {
|
|
75
|
+
out.unread = true;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
for (const file of found) {
|
|
79
|
+
if (++files > MAX_MANIFEST_FILES)
|
|
80
|
+
return { ...out, unread: true };
|
|
81
|
+
try {
|
|
82
|
+
if (statSync(file).size > MAX_MANIFEST_BYTES) {
|
|
83
|
+
out.unread = true;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const read = readManifest(readFileSync(file, "utf8"));
|
|
87
|
+
out.objects.push(...read.objects);
|
|
88
|
+
out.unread ||= read.unread;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
out.unread = true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (out.objects.length > MAX_MANIFEST_OBJECTS)
|
|
96
|
+
return { objects: out.objects.slice(0, MAX_MANIFEST_OBJECTS), unread: true };
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
/** The context a command runs against, by name only: the one named, else the kubeconfig's current one. */
|
|
100
|
+
export function kubeContext(where, env, name, kubeconfig) {
|
|
101
|
+
const home = where.home ?? homedir();
|
|
102
|
+
const explicit = kubeconfig
|
|
103
|
+
? isAbsolute(kubeconfig)
|
|
104
|
+
? kubeconfig
|
|
105
|
+
: where.cwd
|
|
106
|
+
? resolve(where.cwd, kubeconfig)
|
|
107
|
+
: undefined
|
|
108
|
+
: undefined;
|
|
109
|
+
if (kubeconfig && !explicit)
|
|
110
|
+
return name ? { name } : null;
|
|
111
|
+
const config = readKubeconfig(where.fs ?? NODE_LINK_FS, kubeconfigFiles(home, env, explicit));
|
|
112
|
+
if (!config || config === "unreadable")
|
|
113
|
+
return name ? { name } : null;
|
|
114
|
+
const chosen = name ?? config.currentContext;
|
|
115
|
+
if (!chosen)
|
|
116
|
+
return null;
|
|
117
|
+
const context = config.contexts.get(chosen);
|
|
118
|
+
const cluster = context?.cluster ? (config.clusters.get(context.cluster)?.label ?? context.cluster) : undefined;
|
|
119
|
+
return {
|
|
120
|
+
name: chosen,
|
|
121
|
+
...(cluster ? { cluster } : {}),
|
|
122
|
+
...(context?.namespace ? { namespace: context.namespace } : {})
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** The command as the event shows it: masked, and nothing after `--` (a shell command may carry anything). */
|
|
126
|
+
export function maskKubeCommand(argv) {
|
|
127
|
+
const dash = argv.indexOf("--");
|
|
128
|
+
const head = maskCommandArgv(dash < 0 ? argv : argv.slice(0, dash));
|
|
129
|
+
return head && dash >= 0 ? `${head} -- …` : head;
|
|
130
|
+
}
|
|
131
|
+
function assignments(words) {
|
|
132
|
+
const out = {};
|
|
133
|
+
for (const word of words) {
|
|
134
|
+
const eq = word.indexOf("=");
|
|
135
|
+
if (eq > 0)
|
|
136
|
+
out[word.slice(0, eq)] = word.slice(eq + 1);
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
/** Every kubectl and helm command of a line, each with what it does; reads leave nothing. */
|
|
141
|
+
export function classifyKubeLine(command, where) {
|
|
142
|
+
let split;
|
|
143
|
+
try {
|
|
144
|
+
split = splitCommands(command);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
let cwd = where.cwd ?? null;
|
|
150
|
+
const bundles = [];
|
|
151
|
+
for (const words of split.commands) {
|
|
152
|
+
const { argv, envInjected } = normalizeArgv(words);
|
|
153
|
+
if (!argv.length)
|
|
154
|
+
continue;
|
|
155
|
+
if (argv[0] === "cd") {
|
|
156
|
+
const target = argv[1];
|
|
157
|
+
cwd =
|
|
158
|
+
argv.length === 2 && target && !target.startsWith("~") && !target.includes("$") && cwd !== null
|
|
159
|
+
? resolve(cwd, target)
|
|
160
|
+
: null;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (!isKubeProgram(argv[0]))
|
|
164
|
+
continue;
|
|
165
|
+
const env = { ...where.env, ...assignments(envInjected) };
|
|
166
|
+
const resolver = {
|
|
167
|
+
context: (name, kubeconfig) => kubeContext({ ...where, cwd }, env, name, kubeconfig),
|
|
168
|
+
manifests: (paths, recursive) => readManifests(paths, recursive, cwd),
|
|
169
|
+
env
|
|
170
|
+
};
|
|
171
|
+
const found = classifyKube(argv, resolver);
|
|
172
|
+
if (!found.length)
|
|
173
|
+
continue;
|
|
174
|
+
const masked = maskKubeCommand(argv);
|
|
175
|
+
bundles.push({ found, ...(masked ? { command: masked } : {}) });
|
|
176
|
+
}
|
|
177
|
+
return bundles;
|
|
178
|
+
}
|
|
179
|
+
/** What a line's Kubernetes commands do, classified here (for when Passport can't be reached). */
|
|
180
|
+
export function kubernetesFound(command, where) {
|
|
181
|
+
return classifyKubeLine(command, where).flatMap((bundle) => bundle.found.map((found) => ({
|
|
182
|
+
platform: "kubernetes",
|
|
183
|
+
nodeId: found.nodeId,
|
|
184
|
+
actionKey: found.actionKey,
|
|
185
|
+
summary: found.summary.slice(0, MAX_SUMMARY)
|
|
186
|
+
})));
|
|
187
|
+
}
|
|
188
|
+
const action = (found) => ({
|
|
189
|
+
nodeId: found.nodeId,
|
|
190
|
+
actionKey: found.actionKey,
|
|
191
|
+
summary: found.summary.slice(0, MAX_SUMMARY),
|
|
192
|
+
...(found.nodes?.length ? { nodes: found.nodes } : {}),
|
|
193
|
+
// What an unnamed action hit, coarse; the server makes it coarse again (`sanitizeUnnamed`).
|
|
194
|
+
...(found.unnamed ? { unnamed: found.unnamed } : {})
|
|
195
|
+
});
|
|
196
|
+
/** The decide request for one bundle. The person's own kubeconfig runs it: Passport issues no cluster credential. */
|
|
197
|
+
export function kubernetesDecideBody(bundle, ask) {
|
|
198
|
+
const [first, ...rest] = bundle.found;
|
|
199
|
+
return {
|
|
200
|
+
...action(first),
|
|
201
|
+
interface: "cli",
|
|
202
|
+
platform: "kubernetes",
|
|
203
|
+
credential: "own",
|
|
204
|
+
...(rest.length ? { more: rest.map(action) } : {}),
|
|
205
|
+
...(bundle.command ? { command: bundle.command } : {}),
|
|
206
|
+
...(ask.sessionId && /^[A-Za-z0-9._:-]{1,128}$/.test(ask.sessionId) ? { sessionId: ask.sessionId } : {})
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/** Ask about one bundle under a new request id. A server that doesn't decide Kubernetes answers 400: undecided, never
|
|
210
|
+
* a block. */
|
|
211
|
+
export async function askKubernetes(api, bundle, ask, signal) {
|
|
212
|
+
if (bundle.found.length > MAX_KUBE_BUNDLE)
|
|
213
|
+
return { kind: "block", message: TOO_MANY_KUBE };
|
|
214
|
+
const requestId = newRequestId();
|
|
215
|
+
const unreachable = { kind: "unreachable", late: [requestId] };
|
|
216
|
+
let response;
|
|
217
|
+
try {
|
|
218
|
+
response = await api(CP_DECIDE_PATH, {
|
|
219
|
+
method: "POST",
|
|
220
|
+
headers: { "content-type": "application/json" },
|
|
221
|
+
body: JSON.stringify({ ...kubernetesDecideBody(bundle, ask), requestId }),
|
|
222
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(CP_DECIDE_TIMEOUT_MS)])
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
return unreachable;
|
|
227
|
+
}
|
|
228
|
+
if (response.status >= 500)
|
|
229
|
+
return unreachable;
|
|
230
|
+
if (NO_ANSWER_STATUSES.has(response.status))
|
|
231
|
+
return noAnswer(response, unreachable);
|
|
232
|
+
if (!response.ok) {
|
|
233
|
+
await response.body?.cancel().catch(() => undefined);
|
|
234
|
+
return { kind: "undecided" };
|
|
235
|
+
}
|
|
236
|
+
let body;
|
|
237
|
+
try {
|
|
238
|
+
body = await readBoundedJson(response);
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
return unreachable;
|
|
242
|
+
}
|
|
243
|
+
if (body.allow === true)
|
|
244
|
+
return { kind: "allow", eventId: typeof body.eventId === "string" ? body.eventId : null };
|
|
245
|
+
return refusalOf(body) ?? { kind: "undecided" };
|
|
246
|
+
}
|
|
247
|
+
/** The guard's question for a line's kubectl and helm commands: the worst answer of its bundles. */
|
|
248
|
+
export async function kubernetesLinePlan(command, where, open, extra, signal) {
|
|
249
|
+
const bundles = classifyKubeLine(command, where);
|
|
250
|
+
if (!bundles.length)
|
|
251
|
+
return null;
|
|
252
|
+
const label = bundles[0].found[0].summary;
|
|
253
|
+
if (bundles.length > MAX_KUBE_COMMANDS)
|
|
254
|
+
return { answer: { kind: "block", message: TOO_MANY_KUBE }, label };
|
|
255
|
+
const api = await open();
|
|
256
|
+
if (!api)
|
|
257
|
+
return null;
|
|
258
|
+
const answers = await Promise.all(bundles.map((bundle) => askKubernetes(api, bundle, extra, signal)));
|
|
259
|
+
return { answer: worstAnswer(answers) ?? answers.at(-1) ?? { kind: "allow", eventId: null }, label };
|
|
260
|
+
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* and Managed coverage with Followed. Platform-free; part of the contract with `controlPlane.ts`,
|
|
5
5
|
* changed only through the orchestrating session.
|
|
6
6
|
*/
|
|
7
|
+
import type { ProductionDecision } from "./controlPlaneActions.generated.js";
|
|
7
8
|
import { type ActionDef, type ActionEvent, type ControlRule, type Coverage, type Kind, type PlatformId, type PlatformKey, type PlatformMode, type ResourceNode, type Surface } from "./controlPlane.generated.js";
|
|
8
9
|
/** Retired kinds: Spend (2026-11-14), and Grant, Expose and Disarm (2026-11-16). Stored events that name one read as
|
|
9
10
|
* `change` (`currentKind`). */
|
|
@@ -15,6 +16,11 @@ export type CpPlatformModeResponse = {
|
|
|
15
16
|
platform: PlatformKey;
|
|
16
17
|
mode: PlatformMode;
|
|
17
18
|
};
|
|
19
|
+
/** PATCH /api/cp/nodes/:id/production `{ production: true | false | null }` (admins; null clears the correction). */
|
|
20
|
+
export type CpNodeProductionResponse = {
|
|
21
|
+
node: ResourceNode;
|
|
22
|
+
production: ProductionDecision;
|
|
23
|
+
};
|
|
18
24
|
/**
|
|
19
25
|
* Account-scoped node ids. The account is the connection's scope (an organization's id); the key is
|
|
20
26
|
* the platform's own stable id, or for a name-keyed type the parent's key and the name (`nameKey`).
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { ActionDef, DiscoveredNode, ResourceTypeDef, ServiceDef } from "./controlPlane.generated.js";
|
|
2
|
+
/**
|
|
3
|
+
* npm, through the CLI guard (docs/design/control-plane.md, "Action model"): what an agent does to a package
|
|
4
|
+
* registry, never a local install. `npm`, `pnpm` and `yarn` commands are read into the registry actions they take,
|
|
5
|
+
* on the registry › scope › package they take them on (the command families: `controlPlaneNpmCommands.ts`). Pure:
|
|
6
|
+
* the server and the bridge (a generated copy) share it; the bridge passes the files it may read (`NpmFiles`).
|
|
7
|
+
*
|
|
8
|
+
* Only names leave the computer: the registry's host (never a path, a user name or a token), the package's name and
|
|
9
|
+
* version from its `package.json`, a dist-tag, a team, an owner's user name. `.npmrc` is read line by line for
|
|
10
|
+
* `registry` and `@scope:registry` only; every other line (an `_authToken`, a password) is never kept.
|
|
11
|
+
*
|
|
12
|
+
* Local work (`install`, `ci`, `run`, `test`, `exec`, `version`, `pack`…) is not an action. `npm install -g` and
|
|
13
|
+
* `npx <package>` run code from the registry on this computer; they stay local here (a candidate for later).
|
|
14
|
+
*/
|
|
15
|
+
export declare const NPM_PLATFORM = "npm";
|
|
16
|
+
export declare const NPM_SERVICE = "registry";
|
|
17
|
+
/** The public registry, as every spelling of it is recorded. */
|
|
18
|
+
export declare const PUBLIC_REGISTRY = "registry.npmjs.org";
|
|
19
|
+
/** A registry the guard couldn't read (`registry=${VAR}`): decided as if it were public. */
|
|
20
|
+
export declare const UNKNOWN_REGISTRY = "unknown";
|
|
21
|
+
export declare const NPM_SERVICES: readonly ServiceDef[];
|
|
22
|
+
export declare const NPM_TYPES: readonly ResourceTypeDef[];
|
|
23
|
+
/** npm's actions. Kinds are each action's default effects' (`npmEffectsOf`); a call's own effects decide its kind. */
|
|
24
|
+
export declare const NPM_ACTIONS: readonly Omit<ActionDef, "effects">[];
|
|
25
|
+
export declare const npmNodeIds: {
|
|
26
|
+
platform: string;
|
|
27
|
+
service: string;
|
|
28
|
+
registry: (host: string) => string;
|
|
29
|
+
scope: (host: string, scope: string) => string;
|
|
30
|
+
package: (host: string, name: string) => string;
|
|
31
|
+
};
|
|
32
|
+
/** A version or a range as people write them ("0.49.0", "1.x", "<0.48"). */
|
|
33
|
+
export declare const VERSION: RegExp;
|
|
34
|
+
export declare const EXACT_VERSION: RegExp;
|
|
35
|
+
export declare const TAG: RegExp;
|
|
36
|
+
export declare const USER: RegExp;
|
|
37
|
+
export declare const TEAM: RegExp;
|
|
38
|
+
export declare const SAFE_ID: RegExp;
|
|
39
|
+
export declare const isPackageName: (name: string) => boolean;
|
|
40
|
+
export declare const isPublicRegistry: (host: string) => host is "unknown" | "registry.npmjs.org";
|
|
41
|
+
/** The registry's host from a URL, never its user, password, path or query; null when it isn't a URL. */
|
|
42
|
+
export declare function registryHost(value: string | undefined): string | null;
|
|
43
|
+
/** The chain from the registry down to the package (scoped packages under their scope). */
|
|
44
|
+
export declare function npmChain(host: string, name?: string): DiscoveredNode[];
|
|
45
|
+
/** The chain a node id stands for, rebuilt from the id alone: what the server checks a guard's chain against. */
|
|
46
|
+
export declare function npmChainOf(nodeId: string): DiscoveredNode[] | null;
|
|
47
|
+
/** The registry host a node id is on. */
|
|
48
|
+
export declare function npmHostOf(nodeId: string): string | null;
|
|
49
|
+
export type NpmFiles = {
|
|
50
|
+
/** A file's text, or undefined (missing, too big, unreadable). */
|
|
51
|
+
read: (path: string) => string | undefined;
|
|
52
|
+
/** The person's home folder, for `~/.npmrc`. */
|
|
53
|
+
home?: string;
|
|
54
|
+
/** The command's environment: the agent's, with what the line sets in front of it. */
|
|
55
|
+
env?: Readonly<Record<string, string | undefined>>;
|
|
56
|
+
};
|
|
57
|
+
/** `a/b` + `../c` as a path, `/`-separated (the bridge runs on macOS and Linux). */
|
|
58
|
+
export declare function joinPath(base: string, rel: string): string;
|
|
59
|
+
export type PackageJson = {
|
|
60
|
+
name?: string;
|
|
61
|
+
version?: string;
|
|
62
|
+
workspaces?: string[];
|
|
63
|
+
publishConfig?: {
|
|
64
|
+
registry?: string;
|
|
65
|
+
tag?: string;
|
|
66
|
+
access?: string;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
/** The fields read from a `package.json`; nothing else is kept. */
|
|
70
|
+
export declare function packageJson(files: NpmFiles, folder: string): PackageJson | null;
|
|
71
|
+
/** The nearest folder at or above `folder` with a `package.json`, as npm finds a project. */
|
|
72
|
+
export declare function projectFolder(files: NpmFiles, folder: string): {
|
|
73
|
+
folder: string;
|
|
74
|
+
pkg: PackageJson;
|
|
75
|
+
} | null;
|
|
76
|
+
/**
|
|
77
|
+
* The registries an `.npmrc` names: `registry=` and `@scope:registry=`, hosts only. Every other line, an
|
|
78
|
+
* `_authToken`, `_auth` or `_password` among them, is skipped without being kept.
|
|
79
|
+
*/
|
|
80
|
+
export declare function npmrcRegistries(text: string | undefined): {
|
|
81
|
+
registry?: string;
|
|
82
|
+
scopes: Record<string, string>;
|
|
83
|
+
};
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* npm, through the CLI guard (docs/design/control-plane.md, "Action model"): what an agent does to a package
|
|
3
|
+
* registry, never a local install. `npm`, `pnpm` and `yarn` commands are read into the registry actions they take,
|
|
4
|
+
* on the registry › scope › package they take them on (the command families: `controlPlaneNpmCommands.ts`). Pure:
|
|
5
|
+
* the server and the bridge (a generated copy) share it; the bridge passes the files it may read (`NpmFiles`).
|
|
6
|
+
*
|
|
7
|
+
* Only names leave the computer: the registry's host (never a path, a user name or a token), the package's name and
|
|
8
|
+
* version from its `package.json`, a dist-tag, a team, an owner's user name. `.npmrc` is read line by line for
|
|
9
|
+
* `registry` and `@scope:registry` only; every other line (an `_authToken`, a password) is never kept.
|
|
10
|
+
*
|
|
11
|
+
* Local work (`install`, `ci`, `run`, `test`, `exec`, `version`, `pack`…) is not an action. `npm install -g` and
|
|
12
|
+
* `npx <package>` run code from the registry on this computer; they stay local here (a candidate for later).
|
|
13
|
+
*/
|
|
14
|
+
export const NPM_PLATFORM = "npm";
|
|
15
|
+
export const NPM_SERVICE = "registry";
|
|
16
|
+
/** The public registry, as every spelling of it is recorded. */
|
|
17
|
+
export const PUBLIC_REGISTRY = "registry.npmjs.org";
|
|
18
|
+
const PUBLIC_HOSTS = new Set(["registry.npmjs.org", "registry.npmjs.com", "registry.yarnpkg.com"]);
|
|
19
|
+
/** A registry the guard couldn't read (`registry=${VAR}`): decided as if it were public. */
|
|
20
|
+
export const UNKNOWN_REGISTRY = "unknown";
|
|
21
|
+
export const NPM_SERVICES = [{ key: NPM_SERVICE, label: "Registries" }];
|
|
22
|
+
export const NPM_TYPES = [
|
|
23
|
+
{ type: "registry", label: "Registry", service: NPM_SERVICE, key: "name" },
|
|
24
|
+
{ type: "scope", label: "Scope", service: NPM_SERVICE, key: "name" },
|
|
25
|
+
{ type: "package", label: "Package", service: NPM_SERVICE, key: "name" },
|
|
26
|
+
{ type: "version", label: "Version", service: NPM_SERVICE, depth: "target", targetOf: "package", targetName: "name" },
|
|
27
|
+
{
|
|
28
|
+
type: "dist_tag",
|
|
29
|
+
label: "Dist-tag",
|
|
30
|
+
service: NPM_SERVICE,
|
|
31
|
+
depth: "target",
|
|
32
|
+
targetOf: "package",
|
|
33
|
+
targetName: "name"
|
|
34
|
+
},
|
|
35
|
+
{ type: "team", label: "Team", service: NPM_SERVICE, depth: "target", targetOf: "scope", targetName: "name" }
|
|
36
|
+
];
|
|
37
|
+
/** npm's actions. Kinds are each action's default effects' (`npmEffectsOf`); a call's own effects decide its kind. */
|
|
38
|
+
export const NPM_ACTIONS = [
|
|
39
|
+
{
|
|
40
|
+
key: "npm.package.publish",
|
|
41
|
+
kind: "change",
|
|
42
|
+
appliesTo: "package",
|
|
43
|
+
label: "Publish a version",
|
|
44
|
+
icon: "PackageCheck",
|
|
45
|
+
common: true
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
key: "npm.package.create",
|
|
49
|
+
kind: "create",
|
|
50
|
+
appliesTo: "package",
|
|
51
|
+
label: "Publish a new package",
|
|
52
|
+
icon: "PackagePlus",
|
|
53
|
+
common: true
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
key: "npm.package.unpublish",
|
|
57
|
+
kind: "delete",
|
|
58
|
+
appliesTo: "package",
|
|
59
|
+
label: "Unpublish",
|
|
60
|
+
icon: "PackageX",
|
|
61
|
+
common: true
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
key: "npm.package.deprecate",
|
|
65
|
+
kind: "change",
|
|
66
|
+
appliesTo: "package",
|
|
67
|
+
label: "Deprecate",
|
|
68
|
+
icon: "TriangleAlert",
|
|
69
|
+
common: true
|
|
70
|
+
},
|
|
71
|
+
{ key: "npm.package.tag", kind: "change", appliesTo: "package", label: "Move a dist-tag", icon: "Tag", common: true },
|
|
72
|
+
{
|
|
73
|
+
key: "npm.package.owner",
|
|
74
|
+
kind: "create",
|
|
75
|
+
appliesTo: "package",
|
|
76
|
+
label: "Add or remove an owner",
|
|
77
|
+
icon: "UserPlus",
|
|
78
|
+
common: true
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
key: "npm.package.access",
|
|
82
|
+
kind: "change",
|
|
83
|
+
appliesTo: "package",
|
|
84
|
+
label: "Change who can see or publish",
|
|
85
|
+
icon: "LockKeyhole"
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
key: "npm.package.trust",
|
|
89
|
+
kind: "create",
|
|
90
|
+
appliesTo: "package",
|
|
91
|
+
label: "Trust a CI workflow to publish",
|
|
92
|
+
icon: "Workflow"
|
|
93
|
+
},
|
|
94
|
+
{ key: "npm.package.stage", kind: "create", appliesTo: "package", label: "Stage a release", icon: "PackageOpen" },
|
|
95
|
+
{ key: "npm.scope.team", kind: "change", appliesTo: "scope", label: "Change a team", icon: "Users" },
|
|
96
|
+
{ key: "npm.scope.org", kind: "change", appliesTo: "scope", label: "Change an org member", icon: "Building2" },
|
|
97
|
+
{
|
|
98
|
+
key: "npm.registry.token",
|
|
99
|
+
kind: "create",
|
|
100
|
+
appliesTo: "registry",
|
|
101
|
+
label: "Create or revoke a token",
|
|
102
|
+
icon: "KeyRound",
|
|
103
|
+
common: true
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
key: "npm.registry.account",
|
|
107
|
+
kind: "change",
|
|
108
|
+
appliesTo: "registry",
|
|
109
|
+
label: "Change account security",
|
|
110
|
+
icon: "ShieldCheck"
|
|
111
|
+
},
|
|
112
|
+
{ key: "npm.registry.read", kind: "read", appliesTo: "registry", label: "Read the registry", icon: "Eye" },
|
|
113
|
+
{
|
|
114
|
+
key: "npm.unclassified",
|
|
115
|
+
kind: "unclassified",
|
|
116
|
+
appliesTo: "registry",
|
|
117
|
+
label: "Unclassified action",
|
|
118
|
+
icon: "CircleHelp"
|
|
119
|
+
}
|
|
120
|
+
];
|
|
121
|
+
/* ------------------------------------------------------------------ */
|
|
122
|
+
/* Node ids: npm › Registries › registry › scope › package */
|
|
123
|
+
/* ------------------------------------------------------------------ */
|
|
124
|
+
const id = (type, key) => `${NPM_PLATFORM}/${NPM_SERVICE}/${type}:${key}`;
|
|
125
|
+
export const npmNodeIds = {
|
|
126
|
+
platform: NPM_PLATFORM,
|
|
127
|
+
service: `${NPM_PLATFORM}/${NPM_SERVICE}`,
|
|
128
|
+
registry: (host) => id("registry", host),
|
|
129
|
+
scope: (host, scope) => id("scope", `${host}/${scope}`),
|
|
130
|
+
package: (host, name) => id("package", `${host}/${name}`)
|
|
131
|
+
};
|
|
132
|
+
/** A package name npm accepts (lowercase, URL-safe; `@scope/name`). */
|
|
133
|
+
const PACKAGE = /^(?:@[a-z0-9][a-z0-9._~-]{0,213}\/)?[a-z0-9][a-z0-9._~-]{0,213}$/;
|
|
134
|
+
/** A version or a range as people write them ("0.49.0", "1.x", "<0.48"). */
|
|
135
|
+
export const VERSION = /^[0-9A-Za-z.*<>=~^|+ -]{1,40}$/;
|
|
136
|
+
export const EXACT_VERSION = /^v?\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]{1,40})?(?:\+[0-9A-Za-z.-]{1,40})?$/;
|
|
137
|
+
export const TAG = /^[A-Za-z0-9][A-Za-z0-9._-]{0,39}$/;
|
|
138
|
+
export const USER = /^[a-z0-9][a-z0-9._-]{0,63}$/i;
|
|
139
|
+
export const TEAM = /^@?([a-z0-9][a-z0-9._-]{0,213}):([a-z0-9][a-z0-9._-]{0,63})$/i;
|
|
140
|
+
const HOST = /^[a-z0-9.-]{1,253}(?::\d{1,5})?$/;
|
|
141
|
+
export const SAFE_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
142
|
+
export const isPackageName = (name) => PACKAGE.test(name);
|
|
143
|
+
export const isPublicRegistry = (host) => host === PUBLIC_REGISTRY || host === UNKNOWN_REGISTRY;
|
|
144
|
+
/** The registry's host from a URL, never its user, password, path or query; null when it isn't a URL. */
|
|
145
|
+
export function registryHost(value) {
|
|
146
|
+
if (!value)
|
|
147
|
+
return null;
|
|
148
|
+
const text = value.trim().replace(/^["']|["']$/g, "");
|
|
149
|
+
if (/\$\{|\$[A-Z_]/.test(text))
|
|
150
|
+
return UNKNOWN_REGISTRY;
|
|
151
|
+
let url;
|
|
152
|
+
try {
|
|
153
|
+
url = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(text) ? text : `https://${text}`);
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
if (url.protocol !== "https:" && url.protocol !== "http:")
|
|
159
|
+
return null;
|
|
160
|
+
const host = url.host.toLowerCase();
|
|
161
|
+
if (!HOST.test(host))
|
|
162
|
+
return null;
|
|
163
|
+
return PUBLIC_HOSTS.has(host) ? PUBLIC_REGISTRY : host;
|
|
164
|
+
}
|
|
165
|
+
/** The chain from the registry down to the package (scoped packages under their scope). */
|
|
166
|
+
export function npmChain(host, name) {
|
|
167
|
+
const service = npmNodeIds.service;
|
|
168
|
+
const registry = {
|
|
169
|
+
id: npmNodeIds.registry(host),
|
|
170
|
+
parentId: service,
|
|
171
|
+
type: "registry",
|
|
172
|
+
name: host === UNKNOWN_REGISTRY ? "Unknown registry" : host,
|
|
173
|
+
externalKey: host
|
|
174
|
+
};
|
|
175
|
+
if (!name)
|
|
176
|
+
return [registry];
|
|
177
|
+
const scope = name.startsWith("@") ? name.slice(0, name.indexOf("/")) : null;
|
|
178
|
+
const chain = [registry];
|
|
179
|
+
if (scope)
|
|
180
|
+
chain.push({
|
|
181
|
+
id: npmNodeIds.scope(host, scope),
|
|
182
|
+
parentId: registry.id,
|
|
183
|
+
type: "scope",
|
|
184
|
+
name: scope,
|
|
185
|
+
externalKey: `${host}/${scope}`
|
|
186
|
+
});
|
|
187
|
+
chain.push({
|
|
188
|
+
id: npmNodeIds.package(host, name),
|
|
189
|
+
parentId: chain[chain.length - 1].id,
|
|
190
|
+
type: "package",
|
|
191
|
+
name,
|
|
192
|
+
externalKey: `${host}/${name}`
|
|
193
|
+
});
|
|
194
|
+
return chain;
|
|
195
|
+
}
|
|
196
|
+
/** The chain a node id stands for, rebuilt from the id alone: what the server checks a guard's chain against. */
|
|
197
|
+
export function npmChainOf(nodeId) {
|
|
198
|
+
const match = /^npm\/registry\/(registry|scope|package):(.+)$/.exec(nodeId);
|
|
199
|
+
if (!match)
|
|
200
|
+
return null;
|
|
201
|
+
const [, type, key] = match;
|
|
202
|
+
const slash = key.indexOf("/");
|
|
203
|
+
const host = slash < 0 ? key : key.slice(0, slash);
|
|
204
|
+
if (!HOST.test(host) && host !== UNKNOWN_REGISTRY)
|
|
205
|
+
return null;
|
|
206
|
+
if (type === "registry")
|
|
207
|
+
return slash < 0 ? npmChain(host) : null;
|
|
208
|
+
const rest = key.slice(slash + 1);
|
|
209
|
+
if (type === "scope") {
|
|
210
|
+
if (!/^@[a-z0-9][a-z0-9._~-]{0,213}$/.test(rest))
|
|
211
|
+
return null;
|
|
212
|
+
return [...npmChain(host), npmChain(host, `${rest}/x`)[1]];
|
|
213
|
+
}
|
|
214
|
+
return isPackageName(rest) ? npmChain(host, rest) : null;
|
|
215
|
+
}
|
|
216
|
+
/** The registry host a node id is on. */
|
|
217
|
+
export function npmHostOf(nodeId) {
|
|
218
|
+
const chain = npmChainOf(nodeId);
|
|
219
|
+
return chain ? chain[0].externalKey : null;
|
|
220
|
+
}
|
|
221
|
+
/** `a/b` + `../c` as a path, `/`-separated (the bridge runs on macOS and Linux). */
|
|
222
|
+
export function joinPath(base, rel) {
|
|
223
|
+
const parts = (rel.startsWith("/") ? rel : `${base}/${rel}`).split("/");
|
|
224
|
+
const out = [];
|
|
225
|
+
for (const part of parts) {
|
|
226
|
+
if (!part || part === ".")
|
|
227
|
+
continue;
|
|
228
|
+
if (part === "..")
|
|
229
|
+
out.pop();
|
|
230
|
+
else
|
|
231
|
+
out.push(part);
|
|
232
|
+
}
|
|
233
|
+
return `/${out.join("/")}`;
|
|
234
|
+
}
|
|
235
|
+
/** The fields read from a `package.json`; nothing else is kept. */
|
|
236
|
+
export function packageJson(files, folder) {
|
|
237
|
+
const text = files.read(joinPath(folder, "package.json"));
|
|
238
|
+
if (text === undefined)
|
|
239
|
+
return null;
|
|
240
|
+
try {
|
|
241
|
+
const raw = JSON.parse(text);
|
|
242
|
+
const str = (value) => (typeof value === "string" ? value : undefined);
|
|
243
|
+
const config = raw.publishConfig && typeof raw.publishConfig === "object" ? raw.publishConfig : {};
|
|
244
|
+
const ws = Array.isArray(raw.workspaces)
|
|
245
|
+
? raw.workspaces
|
|
246
|
+
: raw.workspaces &&
|
|
247
|
+
typeof raw.workspaces === "object" &&
|
|
248
|
+
Array.isArray(raw.workspaces.packages)
|
|
249
|
+
? raw.workspaces.packages
|
|
250
|
+
: [];
|
|
251
|
+
return {
|
|
252
|
+
...(str(raw.name) && isPackageName(str(raw.name)) ? { name: str(raw.name) } : {}),
|
|
253
|
+
...(str(raw.version) && EXACT_VERSION.test(str(raw.version)) ? { version: str(raw.version) } : {}),
|
|
254
|
+
workspaces: ws.filter((entry) => typeof entry === "string"),
|
|
255
|
+
publishConfig: {
|
|
256
|
+
...(str(config.registry) ? { registry: str(config.registry) } : {}),
|
|
257
|
+
...(str(config.tag) && TAG.test(str(config.tag)) ? { tag: str(config.tag) } : {}),
|
|
258
|
+
...(str(config.access) ? { access: str(config.access) } : {})
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/** The nearest folder at or above `folder` with a `package.json`, as npm finds a project. */
|
|
267
|
+
export function projectFolder(files, folder) {
|
|
268
|
+
for (let at = folder, guard = 0; guard < 64; guard++) {
|
|
269
|
+
const pkg = packageJson(files, at);
|
|
270
|
+
if (pkg)
|
|
271
|
+
return { folder: at, pkg };
|
|
272
|
+
if (at === "/")
|
|
273
|
+
return null;
|
|
274
|
+
at = joinPath(at, "..");
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
const MAX_NPMRC = 64 * 1024;
|
|
279
|
+
/**
|
|
280
|
+
* The registries an `.npmrc` names: `registry=` and `@scope:registry=`, hosts only. Every other line, an
|
|
281
|
+
* `_authToken`, `_auth` or `_password` among them, is skipped without being kept.
|
|
282
|
+
*/
|
|
283
|
+
export function npmrcRegistries(text) {
|
|
284
|
+
const scopes = {};
|
|
285
|
+
let registry;
|
|
286
|
+
if (text === undefined || text.length > MAX_NPMRC)
|
|
287
|
+
return { scopes };
|
|
288
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
289
|
+
const line = raw.trim();
|
|
290
|
+
if (!line || line.startsWith("#") || line.startsWith(";"))
|
|
291
|
+
continue;
|
|
292
|
+
const eq = line.indexOf("=");
|
|
293
|
+
if (eq <= 0)
|
|
294
|
+
continue;
|
|
295
|
+
const key = line.slice(0, eq).trim().toLowerCase();
|
|
296
|
+
if (key !== "registry" && !/^@[a-z0-9][a-z0-9._~-]*:registry$/.test(key))
|
|
297
|
+
continue;
|
|
298
|
+
const host = registryHost(line.slice(eq + 1));
|
|
299
|
+
if (!host)
|
|
300
|
+
continue;
|
|
301
|
+
if (key === "registry")
|
|
302
|
+
registry = host;
|
|
303
|
+
else
|
|
304
|
+
scopes[key.slice(0, key.indexOf(":"))] = host;
|
|
305
|
+
}
|
|
306
|
+
return { ...(registry ? { registry } : {}), scopes };
|
|
307
|
+
}
|