passport-bridge 0.35.1 → 0.36.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/cliAutopilotPolicy.d.ts +2 -0
- package/dist/cliExec.d.ts +1 -2
- package/dist/cliExec.js +17 -38
- package/dist/cliGitCredential.js +28 -10
- package/dist/cliHookDecide.js +36 -16
- package/dist/cliPolicyV2.js +6 -4
- package/dist/connectorKit/cli.generated.d.ts +15 -0
- package/dist/connectorKit/cli.generated.js +244 -0
- package/dist/connectorKit/engine.generated.d.ts +50 -0
- package/dist/connectorKit/engine.generated.js +475 -0
- package/dist/connectorKit/manifest.generated.d.ts +317 -0
- package/dist/connectorKit/manifest.generated.js +18 -0
- package/dist/connectorKit/manifests/flagSpec.generated.d.ts +6 -0
- package/dist/connectorKit/manifests/flagSpec.generated.js +20 -0
- package/dist/connectorKit/manifests/github.generated.d.ts +17 -0
- package/dist/connectorKit/manifests/github.generated.js +82 -0
- package/dist/connectorKit/manifests/githubCapture.generated.d.ts +2 -0
- package/dist/connectorKit/manifests/githubCapture.generated.js +307 -0
- package/dist/connectorKit/manifests/githubHooks.generated.d.ts +23 -0
- package/dist/connectorKit/manifests/githubHooks.generated.js +401 -0
- package/dist/connectorKit/manifests/githubReads.generated.d.ts +6 -0
- package/dist/connectorKit/manifests/githubReads.generated.js +200 -0
- package/dist/connectorKit/manifests/railway.generated.d.ts +6 -0
- package/dist/connectorKit/manifests/railway.generated.js +121 -0
- package/dist/connectorKit/manifests/railwayCapture.generated.d.ts +3 -0
- package/dist/connectorKit/manifests/railwayCapture.generated.js +375 -0
- package/dist/connectorKit/manifests/railwayHooks.generated.d.ts +25 -0
- package/dist/connectorKit/manifests/railwayHooks.generated.js +487 -0
- package/dist/connectorKit/manifests/supabase.generated.d.ts +3 -0
- package/dist/connectorKit/manifests/supabase.generated.js +34 -0
- package/dist/connectorKit/manifests/supabaseApi.generated.d.ts +3 -0
- package/dist/connectorKit/manifests/supabaseApi.generated.js +632 -0
- package/dist/connectorKit/manifests/supabaseCapture.generated.d.ts +9 -0
- package/dist/connectorKit/manifests/supabaseCapture.generated.js +421 -0
- package/dist/connectorKit/manifests/supabaseCatalog.generated.d.ts +11 -0
- package/dist/connectorKit/manifests/supabaseCatalog.generated.js +70 -0
- package/dist/connectorKit/manifests/supabaseCli.generated.d.ts +10 -0
- package/dist/connectorKit/manifests/supabaseCli.generated.js +581 -0
- package/dist/connectorKit/manifests/supabasePlatform.generated.d.ts +7 -0
- package/dist/connectorKit/manifests/supabasePlatform.generated.js +283 -0
- package/dist/connectorKit/manifests/supabaseProbes.generated.d.ts +5 -0
- package/dist/connectorKit/manifests/supabaseProbes.generated.js +580 -0
- package/dist/connectorKit/params.generated.d.ts +25 -0
- package/dist/connectorKit/params.generated.js +193 -0
- package/dist/connectorKit/pgParser.d.ts +9 -0
- package/dist/connectorKit/pgParser.js +13 -0
- package/dist/connectorKit/shadow.generated.d.ts +58 -0
- package/dist/connectorKit/shadow.generated.js +184 -0
- package/dist/connectorKit/shell.generated.d.ts +73 -0
- package/dist/connectorKit/shell.generated.js +516 -0
- package/dist/connectorKit/sourcegen.generated.d.ts +50 -0
- package/dist/connectorKit/sourcegen.generated.js +208 -0
- package/dist/connectorKit/sources/supabase/api-operations.generated.d.ts +187 -0
- package/dist/connectorKit/sources/supabase/api-operations.generated.js +2 -0
- package/dist/connectorKit/sources/supabase/cli-flags.generated.d.ts +1236 -0
- package/dist/connectorKit/sources/supabase/cli-flags.generated.js +2 -0
- package/dist/connectorKit/sources/supabase/mcp-tools.generated.d.ts +35 -0
- package/dist/connectorKit/sources/supabase/mcp-tools.generated.js +2 -0
- package/dist/connectorKit/sql.generated.d.ts +29 -0
- package/dist/connectorKit/sql.generated.js +420 -0
- package/dist/connectorKit/sqlFingerprint.generated.d.ts +8 -0
- package/dist/connectorKit/sqlFingerprint.generated.js +105 -0
- package/dist/connectorKit/template.generated.d.ts +21 -0
- package/dist/connectorKit/template.generated.js +89 -0
- package/dist/connectorKit/types.generated.d.ts +81 -0
- package/dist/connectorKit/types.generated.js +6 -0
- package/dist/controlPlane.generated.d.ts +46 -2
- package/dist/controlPlane.generated.js +10 -1
- package/dist/controlPlaneExec.d.ts +21 -2
- package/dist/controlPlaneExec.js +83 -5
- package/dist/controlPlaneGithub.generated.js +4 -8
- package/dist/controlPlaneGuard.js +29 -4
- package/dist/controlPlaneIds.d.ts +39 -0
- package/dist/controlPlaneIds.js +175 -0
- package/dist/controlPlaneModel.generated.d.ts +67 -0
- package/dist/controlPlaneModel.generated.js +153 -0
- package/dist/controlPlaneRailwayGuard.js +13 -3
- package/dist/controlPlaneSupabase.d.ts +84 -0
- package/dist/controlPlaneSupabase.js +278 -0
- package/dist/execGithubRepo.d.ts +2 -0
- package/dist/execGithubRepo.js +30 -0
- package/dist/gitPush.d.ts +20 -0
- package/dist/gitPush.js +45 -7
- package/dist/kitShadow.d.ts +90 -0
- package/dist/kitShadow.js +261 -0
- package/dist/kitShadowBudget.d.ts +10 -0
- package/dist/kitShadowBudget.js +15 -0
- package/dist/pluginPin.d.ts +1 -1
- package/dist/pluginPin.js +1 -1
- package/dist/supabaseDecisions.d.ts +9 -0
- package/dist/supabaseDecisions.js +98 -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,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one Level 1 engine: any manifest, any request (a CLI line, an MCP tool
|
|
3
|
+
* call, an API call), to the governed Actions it takes. Pure and synchronous,
|
|
4
|
+
* like every connector's `classify` (spec Part 2): no network, no platform
|
|
5
|
+
* names. Summaries are templates filled with safe names only.
|
|
6
|
+
*
|
|
7
|
+
* Fails closed. These all become the manifest's Unclassified Action (kind
|
|
8
|
+
* delete, blocked by default): a command, tool or route the manifest doesn't
|
|
9
|
+
* name; a flag the vendor's spec doesn't list; a boolean the CLI framework
|
|
10
|
+
* wouldn't accept; a value flag with no value; an argument no rule names; a
|
|
11
|
+
* known operation whose conditions no rule matches; a SQL rule with no SQL; a
|
|
12
|
+
* field sent twice with two values; a list where a rule takes one value; a
|
|
13
|
+
* node whose parent can't be resolved from the request or the inventory; a
|
|
14
|
+
* hook that's missing; a problem the guard's resolver found (`shell.ts`).
|
|
15
|
+
* Only a reviewed read (a reads entry or a "read" rule), a local-only command,
|
|
16
|
+
* or SQL the parser shows only reads returns null.
|
|
17
|
+
*/
|
|
18
|
+
import { type Classification, type DiscoveredNode } from "../controlPlane.generated.js";
|
|
19
|
+
import { type Manifest } from "./manifest.generated.js";
|
|
20
|
+
import type { ClassifyContext, ConnectorHooks, Normalized, Request } from "./types.generated.js";
|
|
21
|
+
export { at, fill, parseTemplate, placeholders, render, safeName } from "./template.generated.js";
|
|
22
|
+
export type * from "./types.generated.js";
|
|
23
|
+
export declare function normalize(m: Manifest, request: Request): Normalized | null;
|
|
24
|
+
export type Chain = {
|
|
25
|
+
nodeId: string;
|
|
26
|
+
chain: DiscoveredNode[];
|
|
27
|
+
unresolved?: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* The node a request lands on and its chain from the service down. The
|
|
31
|
+
* requested type, or its nearest ancestor the params can name; every
|
|
32
|
+
* ancestor above it must be named by the params or found in the inventory
|
|
33
|
+
* (context.parents, hooks.resolveParent), or the chain is `unresolved` and
|
|
34
|
+
* the caller fails closed.
|
|
35
|
+
*/
|
|
36
|
+
export declare function nodeChain(m: Manifest, type: string, params: Readonly<Record<string, unknown>>, context?: ClassifyContext, hooks?: ConnectorHooks): Chain;
|
|
37
|
+
export declare function assertSchema(m: Manifest): void;
|
|
38
|
+
/**
|
|
39
|
+
* How a request was decided: `rule` (a capture rule or its hook named the Actions, which may include the
|
|
40
|
+
* Unclassified Action on purpose), `read` (a reviewed read or a local-only command), `failed` (nothing
|
|
41
|
+
* named it, or what named it couldn't be resolved: Unclassified), or `none` (not this platform's).
|
|
42
|
+
*/
|
|
43
|
+
export type Explained = {
|
|
44
|
+
found: Classification[];
|
|
45
|
+
via: "rule" | "read" | "failed" | "none";
|
|
46
|
+
};
|
|
47
|
+
/** The Actions a request takes: one, several for a bundle, or null when it only reads or isn't this platform's. */
|
|
48
|
+
export declare function classify(m: Manifest, request: Request, context?: ClassifyContext, hooks?: ConnectorHooks): Classification | Classification[] | null;
|
|
49
|
+
/** `classify`, with how it was decided. */
|
|
50
|
+
export declare function explain(m: Manifest, request: Request, context?: ClassifyContext, hooks?: ConnectorHooks): Explained;
|
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
// Generated by scripts/generate-control-plane-classifier.mjs. Do not edit.
|
|
2
|
+
/**
|
|
3
|
+
* The one Level 1 engine: any manifest, any request (a CLI line, an MCP tool
|
|
4
|
+
* call, an API call), to the governed Actions it takes. Pure and synchronous,
|
|
5
|
+
* like every connector's `classify` (spec Part 2): no network, no platform
|
|
6
|
+
* names. Summaries are templates filled with safe names only.
|
|
7
|
+
*
|
|
8
|
+
* Fails closed. These all become the manifest's Unclassified Action (kind
|
|
9
|
+
* delete, blocked by default): a command, tool or route the manifest doesn't
|
|
10
|
+
* name; a flag the vendor's spec doesn't list; a boolean the CLI framework
|
|
11
|
+
* wouldn't accept; a value flag with no value; an argument no rule names; a
|
|
12
|
+
* known operation whose conditions no rule matches; a SQL rule with no SQL; a
|
|
13
|
+
* field sent twice with two values; a list where a rule takes one value; a
|
|
14
|
+
* node whose parent can't be resolved from the request or the inventory; a
|
|
15
|
+
* hook that's missing; a problem the guard's resolver found (`shell.ts`).
|
|
16
|
+
* Only a reviewed read (a reads entry or a "read" rule), a local-only command,
|
|
17
|
+
* or SQL the parser shows only reads returns null.
|
|
18
|
+
*/
|
|
19
|
+
import { nodeId } from "../controlPlane.generated.js";
|
|
20
|
+
import { knownOps, normalizeCli } from "./cli.generated.js";
|
|
21
|
+
import { MANIFEST_SCHEMA } from "./manifest.generated.js";
|
|
22
|
+
import { canonical, holds } from "./params.generated.js";
|
|
23
|
+
import { classifySql, SQL_LABEL } from "./sql.generated.js";
|
|
24
|
+
import { fill, placeholders, render, safeName } from "./template.generated.js";
|
|
25
|
+
export { at, fill, parseTemplate, placeholders, render, safeName } from "./template.generated.js";
|
|
26
|
+
/* ------------------------------------------------------------------ */
|
|
27
|
+
/* Normalize */
|
|
28
|
+
/* ------------------------------------------------------------------ */
|
|
29
|
+
/** Top-level scalar and string-list fields as params; nested values only through a rule's dotted paths. */
|
|
30
|
+
function flat(args) {
|
|
31
|
+
const out = {};
|
|
32
|
+
if (!args || typeof args !== "object" || Array.isArray(args))
|
|
33
|
+
return out;
|
|
34
|
+
for (const [key, value] of Object.entries(args)) {
|
|
35
|
+
if (typeof value === "string")
|
|
36
|
+
out[key] = value;
|
|
37
|
+
else if (typeof value === "number" || typeof value === "boolean")
|
|
38
|
+
out[key] = String(value);
|
|
39
|
+
else if (Array.isArray(value) && value.every((item) => typeof item === "string"))
|
|
40
|
+
out[key] = value;
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
function normalizeMcp(m, tool, args) {
|
|
45
|
+
let op = tool;
|
|
46
|
+
for (const prefix of m.capture.mcp.prefixes)
|
|
47
|
+
if (op.startsWith(prefix))
|
|
48
|
+
op = op.slice(prefix.length);
|
|
49
|
+
const tools = m.capture.inputs?.mcp;
|
|
50
|
+
const inputs = tools && Object.hasOwn(tools, op) ? tools[op] : undefined;
|
|
51
|
+
const params = flat(args);
|
|
52
|
+
// A declared schema: an argument it doesn't list is dropped by the server, and never read here.
|
|
53
|
+
if (inputs)
|
|
54
|
+
for (const key of Object.keys(params))
|
|
55
|
+
if (!inputs.body?.includes(key))
|
|
56
|
+
delete params[key];
|
|
57
|
+
return { iface: "mcp", op, params, body: args, local: false, remote: true, ...(inputs ? { inputs } : {}) };
|
|
58
|
+
}
|
|
59
|
+
const routeTables = new WeakMap();
|
|
60
|
+
/** The API's routes as matchers, built once per manifest; `byLength` holds one per template, longest first. */
|
|
61
|
+
function routesOf(m) {
|
|
62
|
+
const api = m.capture.api;
|
|
63
|
+
const hit = routeTables.get(api);
|
|
64
|
+
if (hit)
|
|
65
|
+
return hit;
|
|
66
|
+
const routes = [...api.rules.map((r) => r.match), ...Object.keys(api.reads)].map((op) => {
|
|
67
|
+
const [verb, template] = op.split(" ");
|
|
68
|
+
const names = [];
|
|
69
|
+
const source = template
|
|
70
|
+
.replace(/(.)\/+$/, "$1")
|
|
71
|
+
.replace(/\{(\w+)\}/g, (_, name) => (names.push(name), "([^/]+)"));
|
|
72
|
+
return { op, verb, template, names, whole: new RegExp(`^${source}$`), prefix: new RegExp(`^${source}(?:/|$)`) };
|
|
73
|
+
});
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
const byLength = [...routes]
|
|
76
|
+
.sort((a, b) => b.template.length - a.template.length)
|
|
77
|
+
.filter((r) => !seen.has(r.template) && seen.add(r.template));
|
|
78
|
+
const built = { routes, byLength };
|
|
79
|
+
routeTables.set(api, built);
|
|
80
|
+
return built;
|
|
81
|
+
}
|
|
82
|
+
function normalizeApi(m, method, url, body) {
|
|
83
|
+
const parsed = new URL(url.replace(/^(https?:\/\/[^/]+)?\/+/, "$1/"), m.capture.api.base);
|
|
84
|
+
const path = parsed.pathname.replace(/\/{2,}/g, "/").replace(/(.)\/+$/, "$1");
|
|
85
|
+
const verb = method.toUpperCase();
|
|
86
|
+
const query = {};
|
|
87
|
+
let duplicate = false;
|
|
88
|
+
parsed.searchParams.forEach((value, key) => {
|
|
89
|
+
// The same key twice with different values: servers disagree on which wins, so neither can be trusted.
|
|
90
|
+
if (query[key] !== undefined && query[key] !== value)
|
|
91
|
+
duplicate = true;
|
|
92
|
+
query[key] = value;
|
|
93
|
+
});
|
|
94
|
+
const { routes, byLength } = routesOf(m);
|
|
95
|
+
const match = (route, whole) => {
|
|
96
|
+
const found = (whole ? route.whole : route.prefix).exec(path);
|
|
97
|
+
if (!found)
|
|
98
|
+
return null;
|
|
99
|
+
const names = route.names;
|
|
100
|
+
// An encoded "/" stays encoded: it would name another node than the one the server routes to.
|
|
101
|
+
// A malformed escape stays as written: the server can't route it to anything else.
|
|
102
|
+
const decode = (raw) => {
|
|
103
|
+
let decoded;
|
|
104
|
+
try {
|
|
105
|
+
decoded = decodeURIComponent(raw);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return raw;
|
|
109
|
+
}
|
|
110
|
+
return decoded.includes("/") ? raw : decoded;
|
|
111
|
+
};
|
|
112
|
+
return Object.fromEntries(names.map((name, i) => [name, decode(found[i + 1])]));
|
|
113
|
+
};
|
|
114
|
+
for (const route of routes) {
|
|
115
|
+
const found = route.verb === verb ? match(route, true) : null;
|
|
116
|
+
if (!found)
|
|
117
|
+
continue;
|
|
118
|
+
const op = route.op;
|
|
119
|
+
const declared = m.capture.inputs?.api?.[op];
|
|
120
|
+
// Query keys the vendor documents for every route (Vercel's teamId) count as declared on each.
|
|
121
|
+
const global = m.capture.api.query ?? [];
|
|
122
|
+
const inputs = declared && global.length
|
|
123
|
+
? { ...declared, query: [...new Set([...(declared.query ?? []), ...global])] }
|
|
124
|
+
: declared;
|
|
125
|
+
const slashed = Object.values(found).some((value) => /%2f/i.test(value));
|
|
126
|
+
const problem = duplicate ? "a query key given twice" : slashed ? "a path param holds a slash" : undefined;
|
|
127
|
+
return {
|
|
128
|
+
iface: "api",
|
|
129
|
+
op,
|
|
130
|
+
params: inputs ? { ...found } : { ...query, ...found },
|
|
131
|
+
query,
|
|
132
|
+
pathParams: Object.keys(found),
|
|
133
|
+
body,
|
|
134
|
+
local: false,
|
|
135
|
+
remote: true,
|
|
136
|
+
...(inputs ? { inputs } : {}),
|
|
137
|
+
...(problem ? { problem } : {})
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
// A route the manifest doesn't know: its params from the longest known route it starts with, and a
|
|
141
|
+
// summary route with that template and the rest masked.
|
|
142
|
+
const knownRoute = byLength.find((route) => match(route, false) !== null);
|
|
143
|
+
const known = knownRoute?.template;
|
|
144
|
+
const prefix = knownRoute ? match(knownRoute, false) : {};
|
|
145
|
+
const rest = path
|
|
146
|
+
.split("/")
|
|
147
|
+
.filter(Boolean)
|
|
148
|
+
.slice(known ? known.split("/").filter(Boolean).length : 0);
|
|
149
|
+
const route = `${known ?? ""}/${rest.map((part) => (/^[a-z][a-z_-]{0,40}$/.test(part) ? part : "…")).join("/")}`;
|
|
150
|
+
return {
|
|
151
|
+
iface: "api",
|
|
152
|
+
op: `${verb} ${path}`,
|
|
153
|
+
route: route.replace(/\/+$/, ""),
|
|
154
|
+
params: { ...query, ...prefix },
|
|
155
|
+
query,
|
|
156
|
+
pathParams: Object.keys(prefix),
|
|
157
|
+
body,
|
|
158
|
+
local: false,
|
|
159
|
+
remote: true
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
export function normalize(m, request) {
|
|
163
|
+
if (request.interface === "cli")
|
|
164
|
+
return normalizeCli(m, request.argv, request.problem);
|
|
165
|
+
if (request.interface === "mcp")
|
|
166
|
+
return normalizeMcp(m, request.tool, request.args);
|
|
167
|
+
return normalizeApi(m, request.method, request.url, request.body);
|
|
168
|
+
}
|
|
169
|
+
/* ------------------------------------------------------------------ */
|
|
170
|
+
/* Nodes */
|
|
171
|
+
/* ------------------------------------------------------------------ */
|
|
172
|
+
/** A node the inventory knows, and its ancestors up to the service, as discovered nodes (service first). */
|
|
173
|
+
function inventoryChain(id, context, serviceId) {
|
|
174
|
+
const out = [];
|
|
175
|
+
for (let current = id, guard = 0; current && current !== serviceId && guard < 8; guard++) {
|
|
176
|
+
const parentId = context.parents?.[current] ?? serviceId;
|
|
177
|
+
const tail = current.slice(current.lastIndexOf("/") + 1);
|
|
178
|
+
const type = tail.slice(0, tail.indexOf(":"));
|
|
179
|
+
const key = current.slice(current.indexOf(":") + 1);
|
|
180
|
+
out.unshift({ id: current, parentId, type, name: context.names?.[current] ?? key, externalKey: key });
|
|
181
|
+
current = parentId;
|
|
182
|
+
}
|
|
183
|
+
return out;
|
|
184
|
+
}
|
|
185
|
+
/** The node types from the service down to `type`. */
|
|
186
|
+
function typeChain(m, type) {
|
|
187
|
+
const specs = [];
|
|
188
|
+
for (let t = type, guard = 0; t && guard < 8; guard++) {
|
|
189
|
+
const spec = m.nodes[t];
|
|
190
|
+
if (!spec)
|
|
191
|
+
break;
|
|
192
|
+
specs.unshift(t);
|
|
193
|
+
t = spec.parent === "service" ? null : spec.parent.type;
|
|
194
|
+
}
|
|
195
|
+
return specs;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* The node a request lands on and its chain from the service down. The
|
|
199
|
+
* requested type, or its nearest ancestor the params can name; every
|
|
200
|
+
* ancestor above it must be named by the params or found in the inventory
|
|
201
|
+
* (context.parents, hooks.resolveParent), or the chain is `unresolved` and
|
|
202
|
+
* the caller fails closed.
|
|
203
|
+
*/
|
|
204
|
+
export function nodeChain(m, type, params, context = {}, hooks = {}) {
|
|
205
|
+
const specs = typeChain(m, type);
|
|
206
|
+
const service = m.types.find((def) => def.type === type)?.service ?? m.services[0].key;
|
|
207
|
+
const serviceId = nodeId(m.platform, service);
|
|
208
|
+
const aliases = context.aliases ?? {};
|
|
209
|
+
const idOf = (t, key) => {
|
|
210
|
+
const named = nodeId(m.platform, service, t, key);
|
|
211
|
+
return aliases[named] ?? named;
|
|
212
|
+
};
|
|
213
|
+
const keys = specs.map((t) => fill(m.nodes[t].key, params));
|
|
214
|
+
// The deepest type the params name.
|
|
215
|
+
let deepest = specs.length - 1;
|
|
216
|
+
while (deepest >= 0 && keys[deepest] === null)
|
|
217
|
+
deepest -= 1;
|
|
218
|
+
if (deepest < 0)
|
|
219
|
+
return { nodeId: serviceId, chain: [], unresolved: type };
|
|
220
|
+
const chain = [];
|
|
221
|
+
let parentId = serviceId;
|
|
222
|
+
for (let i = 0; i <= deepest; i++) {
|
|
223
|
+
const t = specs[i];
|
|
224
|
+
const key = keys[i] ?? null;
|
|
225
|
+
if (key === null) {
|
|
226
|
+
// An ancestor the request doesn't name: the inventory may know the child's parent.
|
|
227
|
+
const childIndex = keys.findIndex((k, index) => index > i && index <= deepest && k !== null);
|
|
228
|
+
const childKey = keys[childIndex];
|
|
229
|
+
const childType = specs[childIndex];
|
|
230
|
+
const childId = idOf(childType, childKey);
|
|
231
|
+
const found = context.parents?.[childId] ?? hooks.resolveParent?.(childType, childId, context) ?? null;
|
|
232
|
+
if (!found)
|
|
233
|
+
return { nodeId: serviceId, chain: [], unresolved: t };
|
|
234
|
+
chain.push(...inventoryChain(found, context, serviceId));
|
|
235
|
+
parentId = found;
|
|
236
|
+
// Skip the other ancestors the request doesn't name: the inventory chain covers them.
|
|
237
|
+
while (i + 1 <= deepest && keys[i + 1] === null)
|
|
238
|
+
i += 1;
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
const id = idOf(t, key);
|
|
242
|
+
// The inventory knows this node's parent: it wins over a parent the request claims.
|
|
243
|
+
const known = context.parents?.[id];
|
|
244
|
+
if (known && known !== parentId && i === deepest) {
|
|
245
|
+
chain.splice(0, chain.length, ...inventoryChain(known, context, serviceId));
|
|
246
|
+
parentId = known;
|
|
247
|
+
}
|
|
248
|
+
const spec = m.nodes[t];
|
|
249
|
+
const name = context.names?.[id] ?? (spec.name ? fill(spec.name, params) : null) ?? key.split("/").pop();
|
|
250
|
+
chain.push({ id, parentId, type: t, name, externalKey: id.slice(id.indexOf(":") + 1) });
|
|
251
|
+
parentId = id;
|
|
252
|
+
}
|
|
253
|
+
return { nodeId: parentId, chain };
|
|
254
|
+
}
|
|
255
|
+
/* ------------------------------------------------------------------ */
|
|
256
|
+
/* Build */
|
|
257
|
+
/* ------------------------------------------------------------------ */
|
|
258
|
+
const UNCLASSIFIED = Symbol("unclassified");
|
|
259
|
+
function target(m, spec, params) {
|
|
260
|
+
if (!spec)
|
|
261
|
+
return undefined;
|
|
262
|
+
let key = fill(spec.key, params);
|
|
263
|
+
if (key === null)
|
|
264
|
+
return undefined;
|
|
265
|
+
const def = m.types.find((t) => t.type === spec.type);
|
|
266
|
+
if (def?.targetName && typeof def.targetName === "object") {
|
|
267
|
+
const parts = key.split("/");
|
|
268
|
+
if (parts.length > def.targetName.prefix)
|
|
269
|
+
key = `${parts.slice(0, def.targetName.prefix).join("/")}/…`;
|
|
270
|
+
}
|
|
271
|
+
return { type: spec.type, key, name: (spec.name ? fill(spec.name, params) : null) ?? key };
|
|
272
|
+
}
|
|
273
|
+
/** A param this part names that holds several values, when the rule doesn't take them one at a time. */
|
|
274
|
+
function listWhereOne(m, part, params, each) {
|
|
275
|
+
const templates = [
|
|
276
|
+
...typeChain(m, part.on).flatMap((t) => [m.nodes[t].key].flat()),
|
|
277
|
+
part.target?.key,
|
|
278
|
+
part.target?.name,
|
|
279
|
+
part.summary
|
|
280
|
+
].filter((t) => typeof t === "string");
|
|
281
|
+
return templates
|
|
282
|
+
.flatMap(placeholders)
|
|
283
|
+
.some((name) => name !== each && Array.isArray(params[name]) && params[name].length > 1);
|
|
284
|
+
}
|
|
285
|
+
function buildSql(m, part, p, at, out) {
|
|
286
|
+
const sql = p[part.action.slice(4)];
|
|
287
|
+
if (typeof sql !== "string" || !sql.trim())
|
|
288
|
+
return false;
|
|
289
|
+
const seen = new Set();
|
|
290
|
+
for (const finding of classifySql(sql)) {
|
|
291
|
+
const actionKey = m.capture.sql[finding.category];
|
|
292
|
+
if (!actionKey)
|
|
293
|
+
return false;
|
|
294
|
+
if (seen.has(`${actionKey} ${finding.category} ${finding.table ?? ""}`))
|
|
295
|
+
continue;
|
|
296
|
+
seen.add(`${actionKey} ${finding.category} ${finding.table ?? ""}`);
|
|
297
|
+
const label = actionKey === m.capture.unclassified
|
|
298
|
+
? SQL_LABEL[finding.category]
|
|
299
|
+
: m.actions.find((a) => a.key === actionKey).label;
|
|
300
|
+
const t = finding.table ? { type: "table", key: finding.table, name: finding.table } : undefined;
|
|
301
|
+
const where = part.summary ? render(part.summary, p) : "";
|
|
302
|
+
out.push({
|
|
303
|
+
nodeId: at.nodeId,
|
|
304
|
+
actionKey,
|
|
305
|
+
// What the SQL does, then how it was sent, as an aside: "Drop a table public.users (run SQL on project x)".
|
|
306
|
+
summary: `${label}${t ? ` ${t.name}` : ""}${where ? ` (${where[0].toLowerCase()}${where.slice(1)})` : ""}`,
|
|
307
|
+
nodes: at.chain,
|
|
308
|
+
...(t ? { target: t } : {})
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
function build(m, part, params, context, hooks, each) {
|
|
314
|
+
const p = { ...params };
|
|
315
|
+
for (const [name, source] of Object.entries(part.from ?? {}))
|
|
316
|
+
if (p[source] !== undefined)
|
|
317
|
+
p[name] = p[source];
|
|
318
|
+
if (listWhereOne(m, part, p, each))
|
|
319
|
+
return UNCLASSIFIED;
|
|
320
|
+
const at = nodeChain(m, part.on, p, context, hooks);
|
|
321
|
+
if (at.unresolved)
|
|
322
|
+
return UNCLASSIFIED;
|
|
323
|
+
const out = [];
|
|
324
|
+
if (part.action.startsWith("sql:"))
|
|
325
|
+
return buildSql(m, part, p, at, out) ? out : UNCLASSIFIED;
|
|
326
|
+
const t = target(m, part.target, p);
|
|
327
|
+
out.push({
|
|
328
|
+
nodeId: at.nodeId,
|
|
329
|
+
actionKey: part.action,
|
|
330
|
+
summary: render(part.summary ?? m.actions.find((a) => a.key === part.action)?.label ?? part.action, p),
|
|
331
|
+
nodes: at.chain,
|
|
332
|
+
...(t ? { target: t } : {})
|
|
333
|
+
});
|
|
334
|
+
return out;
|
|
335
|
+
}
|
|
336
|
+
/** What a request that fails closed becomes: the Unclassified Action, on the most specific node it names. */
|
|
337
|
+
function unclassified(m, n, params, context, hooks, problem = n.problem) {
|
|
338
|
+
const action = m.actions.find((a) => a.key === m.capture.unclassified);
|
|
339
|
+
// Words only: an id or value in a path or command never reaches the summary.
|
|
340
|
+
const word = (part) => (/^(\{\w+\}|[a-z][a-z_-]{0,40})$/.test(part) ? part : "…");
|
|
341
|
+
const ops = n.iface === "cli" ? knownOps(m) : [];
|
|
342
|
+
const what = n.iface === "api"
|
|
343
|
+
? `${n.op.split(" ")[0]} ${n.route ?? `/${(n.op.split(" ")[1] ?? "").split("/").filter(Boolean).map(word).join("/")}`}`
|
|
344
|
+
: n.iface === "cli"
|
|
345
|
+
? // Only words the CLI itself uses: an unknown command's arguments may be values.
|
|
346
|
+
n.op
|
|
347
|
+
.split(" ")
|
|
348
|
+
.map((part, i) => (ops.some((op) => op.split(" ")[i] === part) ? part : "…"))
|
|
349
|
+
.join(" ")
|
|
350
|
+
: (safeName(n.op) ?? "…");
|
|
351
|
+
const at = nodeChain(m, action.appliesTo, params, context, hooks);
|
|
352
|
+
return {
|
|
353
|
+
nodeId: at.unresolved
|
|
354
|
+
? nodeId(m.platform, m.types.find((t) => t.type === action.appliesTo)?.service ?? m.services[0].key)
|
|
355
|
+
: at.nodeId,
|
|
356
|
+
actionKey: m.capture.unclassified,
|
|
357
|
+
summary: `${action.label}: ${what}${problem ? ` (${safeName(problem) ?? "invalid flags"})` : ""}`,
|
|
358
|
+
nodes: at.unresolved ? [] : at.chain
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
/* ------------------------------------------------------------------ */
|
|
362
|
+
/* Classify */
|
|
363
|
+
/* ------------------------------------------------------------------ */
|
|
364
|
+
export function assertSchema(m) {
|
|
365
|
+
if (m.schema !== MANIFEST_SCHEMA)
|
|
366
|
+
throw new Error(`${m.platform}: manifest schema ${m.schema}, engine reads ${MANIFEST_SCHEMA}`);
|
|
367
|
+
}
|
|
368
|
+
/** The Actions a request takes: one, several for a bundle, or null when it only reads or isn't this platform's. */
|
|
369
|
+
export function classify(m, request, context = {}, hooks = {}) {
|
|
370
|
+
const { found } = explain(m, request, context, hooks);
|
|
371
|
+
return found.length === 0 ? null : found.length === 1 ? found[0] : found;
|
|
372
|
+
}
|
|
373
|
+
/** `classify`, with how it was decided. */
|
|
374
|
+
export function explain(m, request, context = {}, hooks = {}) {
|
|
375
|
+
assertSchema(m);
|
|
376
|
+
const n = normalize(m, hooks.normalize ? hooks.normalize(request) : request);
|
|
377
|
+
if (!n)
|
|
378
|
+
return { found: [], via: "none" };
|
|
379
|
+
if (!n.op && !n.problem)
|
|
380
|
+
return { found: [], via: "read" }; // the program alone, `--help`, `--version`
|
|
381
|
+
const infoOnly = (m.capture.cli.infoFlags ?? []).some((flag) => n.params[flag] === true);
|
|
382
|
+
if (n.iface === "cli" && infoOnly && !n.problem)
|
|
383
|
+
return { found: [], via: "read" };
|
|
384
|
+
return decideRequest(m, n, context, hooks, {});
|
|
385
|
+
}
|
|
386
|
+
function decideRequest(m, n, context, hooks, override) {
|
|
387
|
+
const failed = (rule, problem) => ({
|
|
388
|
+
found: [unclassified(m, n, canonical(m, n, rule, context, override).params, context, hooks, problem ?? n.problem)],
|
|
389
|
+
via: "failed"
|
|
390
|
+
});
|
|
391
|
+
if (n.problem)
|
|
392
|
+
return failed();
|
|
393
|
+
const spec = m.capture[n.iface];
|
|
394
|
+
const candidates = spec.rules.filter((rule) => rule.match === n.op);
|
|
395
|
+
if (candidates.length === 0)
|
|
396
|
+
return Object.hasOwn(spec.reads, n.op) ? { found: [], via: "read" } : failed();
|
|
397
|
+
if (n.iface === "cli" && n.local)
|
|
398
|
+
return localHonoured(m, n, candidates) ? { found: [], via: "read" } : failed();
|
|
399
|
+
for (const rule of candidates) {
|
|
400
|
+
const { params, problem } = canonical(m, n, rule, context, override);
|
|
401
|
+
if (problem)
|
|
402
|
+
return failed(rule, problem);
|
|
403
|
+
// A list, one item at a time: each item is classified as if the request named only it.
|
|
404
|
+
const list = rule.each !== undefined && !(rule.each in override) ? params[rule.each] : undefined;
|
|
405
|
+
if (Array.isArray(list) && list.length > 1)
|
|
406
|
+
return eachItem(m, n, context, hooks, override, rule.each, list);
|
|
407
|
+
if (!holds(rule.when, params, declaredBody(n)))
|
|
408
|
+
continue;
|
|
409
|
+
if (n.iface === "cli" && unnamedArgs(n, rule))
|
|
410
|
+
return failed(rule);
|
|
411
|
+
if ((rule.localByDefault && !n.remote) || rule.action === "read")
|
|
412
|
+
return { found: [], via: "read" };
|
|
413
|
+
const answer = decide(m, n, rule, params, context, hooks);
|
|
414
|
+
if (answer === "read")
|
|
415
|
+
return { found: [], via: "read" };
|
|
416
|
+
return answer === "unclassified" ? failed(rule) : { found: answer, via: "rule" };
|
|
417
|
+
}
|
|
418
|
+
return failed();
|
|
419
|
+
}
|
|
420
|
+
function eachItem(m, n, context, hooks, override, name, list) {
|
|
421
|
+
const items = list.map((item) => decideRequest(m, n, context, hooks, { ...override, [name]: item }));
|
|
422
|
+
const vias = new Set(items.map((one) => one.via));
|
|
423
|
+
const via = vias.has("failed") ? "failed" : vias.has("rule") ? "rule" : "read";
|
|
424
|
+
return { found: dedupe(items.flatMap((one) => one.found)), via };
|
|
425
|
+
}
|
|
426
|
+
/** The body's top-level fields the operation declares (all of them when its schema isn't known). */
|
|
427
|
+
function declaredBody(n) {
|
|
428
|
+
if (!n.inputs || !n.body || typeof n.body !== "object" || Array.isArray(n.body))
|
|
429
|
+
return n.body;
|
|
430
|
+
const fields = new Set(n.inputs.body ?? []);
|
|
431
|
+
return Object.fromEntries(Object.entries(n.body).filter(([key]) => fields.has(key)));
|
|
432
|
+
}
|
|
433
|
+
/** Positional arguments beyond what the rule names (and no rest argument takes them): unread, so unsafe. */
|
|
434
|
+
function unnamedArgs(n, rule) {
|
|
435
|
+
if (!rule.args || rule.args.at(-1).startsWith("..."))
|
|
436
|
+
return false;
|
|
437
|
+
return Object.keys(n.params).filter((key) => /^\$\d+$/.test(key)).length > rule.args.length;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* A local flag counts only where the command declares it, or (a deprecated --local) where the rule acts
|
|
441
|
+
* locally by default or the vendor calls the command selectable. Elsewhere the CLI may ignore it.
|
|
442
|
+
*/
|
|
443
|
+
function localHonoured(m, n, candidates) {
|
|
444
|
+
const own = m.capture.cli.flags.commands[n.op] ?? [];
|
|
445
|
+
const legacyOnly = (n.localFlagsUsed ?? []).filter((flag) => !own.some((spec) => spec.long === flag));
|
|
446
|
+
if (legacyOnly.length === 0)
|
|
447
|
+
return true;
|
|
448
|
+
return candidates.some((rule) => rule.localByDefault) || (m.capture.cli.localCommands ?? []).includes(n.op);
|
|
449
|
+
}
|
|
450
|
+
/** The same Action on the same node and target, named twice (a rule and its `also`), counts once. */
|
|
451
|
+
function dedupe(found) {
|
|
452
|
+
const seen = new Set();
|
|
453
|
+
return found.filter((x) => {
|
|
454
|
+
const key = `${x.actionKey} ${x.nodeId} ${x.target?.key ?? ""}`;
|
|
455
|
+
if (seen.has(key))
|
|
456
|
+
return false;
|
|
457
|
+
seen.add(key);
|
|
458
|
+
return true;
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
/** One matched rule's answer: its hook's, or the Actions it and its `also` rules build. */
|
|
462
|
+
function decide(m, n, rule, params, context, hooks) {
|
|
463
|
+
if (rule.hook) {
|
|
464
|
+
const hook = hooks.classify?.[rule.hook];
|
|
465
|
+
return hook ? hook(n, params, context, rule) : "unclassified";
|
|
466
|
+
}
|
|
467
|
+
const found = [];
|
|
468
|
+
for (const part of [rule, ...(rule.also ?? []).filter((extra) => holds(extra.when, params, declaredBody(n)))]) {
|
|
469
|
+
const built = build(m, part, params, context, hooks, rule.each);
|
|
470
|
+
if (built === UNCLASSIFIED)
|
|
471
|
+
return "unclassified";
|
|
472
|
+
found.push(...built);
|
|
473
|
+
}
|
|
474
|
+
return dedupe(found);
|
|
475
|
+
}
|