@pegasimed.com/pegasi-sail-connect-shared 0.1.0-dev.0 → 0.2.0-dev.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 +398 -125
- package/dist/authorization/index.d.ts +3 -0
- package/dist/authorization/index.d.ts.map +1 -0
- package/dist/authorization/index.js +3 -0
- package/dist/authorization/index.js.map +1 -0
- package/dist/authorization/internal-authorization.d.ts +105 -0
- package/dist/authorization/internal-authorization.d.ts.map +1 -0
- package/dist/authorization/internal-authorization.js +287 -0
- package/dist/authorization/internal-authorization.js.map +1 -0
- package/dist/capabilities/catalog-data.d.ts +3917 -0
- package/dist/capabilities/catalog-data.d.ts.map +1 -0
- package/dist/capabilities/catalog-data.js +4244 -0
- package/dist/capabilities/catalog-data.js.map +1 -0
- package/dist/capabilities/catalog.d.ts +38 -0
- package/dist/capabilities/catalog.d.ts.map +1 -0
- package/dist/capabilities/catalog.js +91 -0
- package/dist/capabilities/catalog.js.map +1 -0
- package/dist/capabilities/index.d.ts +8 -0
- package/dist/capabilities/index.d.ts.map +1 -0
- package/dist/capabilities/index.js +8 -0
- package/dist/capabilities/index.js.map +1 -0
- package/dist/capabilities/list.d.ts +46 -0
- package/dist/capabilities/list.d.ts.map +1 -0
- package/dist/capabilities/list.js +80 -0
- package/dist/capabilities/list.js.map +1 -0
- package/dist/capabilities/match.d.ts +42 -0
- package/dist/capabilities/match.d.ts.map +1 -0
- package/dist/capabilities/match.js +139 -0
- package/dist/capabilities/match.js.map +1 -0
- package/dist/capabilities/migration-data.d.ts +2424 -0
- package/dist/capabilities/migration-data.d.ts.map +1 -0
- package/dist/capabilities/migration-data.js +2646 -0
- package/dist/capabilities/migration-data.js.map +1 -0
- package/dist/capabilities/migration.d.ts +176 -0
- package/dist/capabilities/migration.d.ts.map +1 -0
- package/dist/capabilities/migration.js +209 -0
- package/dist/capabilities/migration.js.map +1 -0
- package/dist/capabilities/types.d.ts +124 -0
- package/dist/capabilities/types.d.ts.map +1 -0
- package/dist/capabilities/types.js +2 -0
- package/dist/capabilities/types.js.map +1 -0
- package/dist/capabilities/validate.d.ts +19 -0
- package/dist/capabilities/validate.d.ts.map +1 -0
- package/dist/capabilities/validate.js +235 -0
- package/dist/capabilities/validate.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +111 -102
- package/scripts/check-service-keys.mjs +250 -0
- package/scripts/lint-phi.mjs +266 -266
package/package.json
CHANGED
|
@@ -1,102 +1,111 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@pegasimed.com/pegasi-sail-connect-shared",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Versioned shared TypeScript helpers for PEGASI SAIL/CONNECT services.",
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "https://github.com/devops-pegasi/pegasi-sail-connect-shared"
|
|
8
|
-
},
|
|
9
|
-
"bugs": {
|
|
10
|
-
"url": "https://github.com/devops-pegasi/pegasi-sail-connect-shared/issues"
|
|
11
|
-
},
|
|
12
|
-
"homepage": "https://github.com/devops-pegasi/pegasi-sail-connect-shared#readme",
|
|
13
|
-
"type": "module",
|
|
14
|
-
"main": "./dist/index.js",
|
|
15
|
-
"types": "./dist/index.d.ts",
|
|
16
|
-
"exports": {
|
|
17
|
-
".": {
|
|
18
|
-
"types": "./dist/index.d.ts",
|
|
19
|
-
"import": "./dist/index.js"
|
|
20
|
-
},
|
|
21
|
-
"./config": {
|
|
22
|
-
"types": "./dist/config.d.ts",
|
|
23
|
-
"import": "./dist/config.js"
|
|
24
|
-
},
|
|
25
|
-
"./ids": {
|
|
26
|
-
"types": "./dist/ids.d.ts",
|
|
27
|
-
"import": "./dist/ids.js"
|
|
28
|
-
},
|
|
29
|
-
"./audit": {
|
|
30
|
-
"types": "./dist/audit.d.ts",
|
|
31
|
-
"import": "./dist/audit.js"
|
|
32
|
-
},
|
|
33
|
-
"./clinical-operations": {
|
|
34
|
-
"types": "./dist/clinical-operations.d.ts",
|
|
35
|
-
"import": "./dist/clinical-operations.js"
|
|
36
|
-
},
|
|
37
|
-
"./jwt": {
|
|
38
|
-
"types": "./dist/jwt.d.ts",
|
|
39
|
-
"import": "./dist/jwt.js"
|
|
40
|
-
},
|
|
41
|
-
"./phi-redaction": {
|
|
42
|
-
"types": "./dist/phi-redaction.d.ts",
|
|
43
|
-
"import": "./dist/phi-redaction.js"
|
|
44
|
-
},
|
|
45
|
-
"./stable-json": {
|
|
46
|
-
"types": "./dist/stable-json.d.ts",
|
|
47
|
-
"import": "./dist/stable-json.js"
|
|
48
|
-
},
|
|
49
|
-
"./worker-logger": {
|
|
50
|
-
"types": "./dist/worker-logger.d.ts",
|
|
51
|
-
"import": "./dist/worker-logger.js"
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
"
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
},
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
"
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
"pino":
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
"
|
|
101
|
-
|
|
102
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@pegasimed.com/pegasi-sail-connect-shared",
|
|
3
|
+
"version": "0.2.0-dev.0",
|
|
4
|
+
"description": "Versioned shared TypeScript helpers for PEGASI SAIL/CONNECT services.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/devops-pegasi/pegasi-sail-connect-shared"
|
|
8
|
+
},
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/devops-pegasi/pegasi-sail-connect-shared/issues"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/devops-pegasi/pegasi-sail-connect-shared#readme",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./config": {
|
|
22
|
+
"types": "./dist/config.d.ts",
|
|
23
|
+
"import": "./dist/config.js"
|
|
24
|
+
},
|
|
25
|
+
"./ids": {
|
|
26
|
+
"types": "./dist/ids.d.ts",
|
|
27
|
+
"import": "./dist/ids.js"
|
|
28
|
+
},
|
|
29
|
+
"./audit": {
|
|
30
|
+
"types": "./dist/audit.d.ts",
|
|
31
|
+
"import": "./dist/audit.js"
|
|
32
|
+
},
|
|
33
|
+
"./clinical-operations": {
|
|
34
|
+
"types": "./dist/clinical-operations.d.ts",
|
|
35
|
+
"import": "./dist/clinical-operations.js"
|
|
36
|
+
},
|
|
37
|
+
"./jwt": {
|
|
38
|
+
"types": "./dist/jwt.d.ts",
|
|
39
|
+
"import": "./dist/jwt.js"
|
|
40
|
+
},
|
|
41
|
+
"./phi-redaction": {
|
|
42
|
+
"types": "./dist/phi-redaction.d.ts",
|
|
43
|
+
"import": "./dist/phi-redaction.js"
|
|
44
|
+
},
|
|
45
|
+
"./stable-json": {
|
|
46
|
+
"types": "./dist/stable-json.d.ts",
|
|
47
|
+
"import": "./dist/stable-json.js"
|
|
48
|
+
},
|
|
49
|
+
"./worker-logger": {
|
|
50
|
+
"types": "./dist/worker-logger.d.ts",
|
|
51
|
+
"import": "./dist/worker-logger.js"
|
|
52
|
+
},
|
|
53
|
+
"./capabilities": {
|
|
54
|
+
"types": "./dist/capabilities/index.d.ts",
|
|
55
|
+
"import": "./dist/capabilities/index.js"
|
|
56
|
+
},
|
|
57
|
+
"./authorization": {
|
|
58
|
+
"types": "./dist/authorization/index.d.ts",
|
|
59
|
+
"import": "./dist/authorization/index.js"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"files": [
|
|
63
|
+
"dist",
|
|
64
|
+
"scripts/lint-phi.mjs",
|
|
65
|
+
"scripts/check-service-keys.mjs",
|
|
66
|
+
"README.md"
|
|
67
|
+
],
|
|
68
|
+
"bin": {
|
|
69
|
+
"lint-phi": "./scripts/lint-phi.mjs"
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "tsc -p tsconfig.json",
|
|
73
|
+
"check": "tsc -p tsconfig.json --noEmit && tsc -p test/capabilities/tsconfig.json && tsc -p test/authorization/tsconfig.json",
|
|
74
|
+
"prepare": "npm run build",
|
|
75
|
+
"lint": "eslint .",
|
|
76
|
+
"test": "vitest run --coverage",
|
|
77
|
+
"validate": "npm run check && npm run lint && npm test"
|
|
78
|
+
},
|
|
79
|
+
"engines": {
|
|
80
|
+
"node": ">=22"
|
|
81
|
+
},
|
|
82
|
+
"publishConfig": {
|
|
83
|
+
"access": "public",
|
|
84
|
+
"registry": "https://registry.npmjs.org/"
|
|
85
|
+
},
|
|
86
|
+
"peerDependencies": {
|
|
87
|
+
"fastify": "^5.0.0",
|
|
88
|
+
"pino": "^10.0.0"
|
|
89
|
+
},
|
|
90
|
+
"peerDependenciesMeta": {
|
|
91
|
+
"fastify": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"pino": {
|
|
95
|
+
"optional": false
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
"devDependencies": {
|
|
99
|
+
"@types/node": "^24.10.1",
|
|
100
|
+
"@vitest/coverage-v8": "^4.1.8",
|
|
101
|
+
"eslint": "^10.4.1",
|
|
102
|
+
"fastify": "^5.6.2",
|
|
103
|
+
"pino": "^10.3.1",
|
|
104
|
+
"typescript": "^5.9.3",
|
|
105
|
+
"typescript-eslint": "^8.61.0",
|
|
106
|
+
"vitest": "^4.0.14"
|
|
107
|
+
},
|
|
108
|
+
"overrides": {
|
|
109
|
+
"fast-uri": "3.1.2"
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// PEGASI SAIL/CONNECT — permission keys of a service against the capability catalog
|
|
3
|
+
//
|
|
4
|
+
// Runs as a CI gate so that a service never requires a key the catalog does
|
|
5
|
+
// not expose (drift between services and catalog). It parses every source
|
|
6
|
+
// file with the TypeScript parser (ts.createSourceFile, not regular
|
|
7
|
+
// expressions) and checks each string literal and template literal without
|
|
8
|
+
// substitutions:
|
|
9
|
+
//
|
|
10
|
+
// - "old": the literal is an old key of PERMISSION_MIGRATION (e.g.
|
|
11
|
+
// `patient:read`, `access-api:users:write`). The service still uses the
|
|
12
|
+
// old grammar; migrate it to its canonical key.
|
|
13
|
+
// - "drift": the literal has the shape `servicio:recurso:acción` (three
|
|
14
|
+
// segments of the catalog grammar) but is not a catalog key; or it is a
|
|
15
|
+
// key-shaped literal with `*` anywhere in a segment (`patient:dom*:read`,
|
|
16
|
+
// `patient:**:read`, `*:*:*`), a two-segment `servicio:*` of a catalog
|
|
17
|
+
// service, or an old key with `*` (`meetings:*`). A literal with `*` is
|
|
18
|
+
// always drift, before "old": there are no wildcard grants, every grant is
|
|
19
|
+
// an explicit catalog key.
|
|
20
|
+
//
|
|
21
|
+
// Keys built at run time (template literals with substitutions,
|
|
22
|
+
// concatenations) are not visible here: the authorization helper of the
|
|
23
|
+
// service must only accept `PermissionKey`, so the TypeScript compiler
|
|
24
|
+
// rejects them (see README, "Evitar el desfase entre servicios y catálogo").
|
|
25
|
+
//
|
|
26
|
+
// Override mechanism: a literal that has the shape of a key but is not one
|
|
27
|
+
// (a cache key, an event name) is silenced with a comment
|
|
28
|
+
// // allow-service-key: <human-readable reason>
|
|
29
|
+
// on the same line or directly above.
|
|
30
|
+
//
|
|
31
|
+
// Test files (*.test.*, *.spec.*, test/, tests/, __tests__/) are skipped
|
|
32
|
+
// unless --include-tests is given. Requires `typescript` (a devDependency of
|
|
33
|
+
// every SAIL/CONNECT service) and the built catalog of this package.
|
|
34
|
+
//
|
|
35
|
+
// Usage:
|
|
36
|
+
// node node_modules/@pegasimed.com/pegasi-sail-connect-shared/scripts/check-service-keys.mjs src/
|
|
37
|
+
// node scripts/check-service-keys.mjs src/ --json
|
|
38
|
+
// node scripts/check-service-keys.mjs --help
|
|
39
|
+
//
|
|
40
|
+
// Exit codes: 0 no findings, 1 findings, 2 usage error.
|
|
41
|
+
|
|
42
|
+
import { readdirSync, readFileSync, realpathSync } from "node:fs";
|
|
43
|
+
import { extname, join, relative, resolve } from "node:path";
|
|
44
|
+
import process, { argv, cwd, exit, stderr, stdout } from "node:process";
|
|
45
|
+
import { fileURLToPath } from "node:url";
|
|
46
|
+
import ts from "typescript";
|
|
47
|
+
|
|
48
|
+
const SOURCE_EXTENSIONS = new Set([".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]);
|
|
49
|
+
const SKIP_DIRS = new Set(["node_modules", "dist", "build", "coverage", ".git", ".next", ".turbo"]);
|
|
50
|
+
const TEST_FILE_PATTERNS = [/\.test\.[mc]?[jt]sx?$/i, /\.spec\.[mc]?[jt]sx?$/i, /(^|[\\/])(test|tests|__tests__)[\\/]/i];
|
|
51
|
+
const SEGMENT = "[a-z][a-z0-9]*(?:-[a-z0-9]+)*";
|
|
52
|
+
const SHAPED_SEGMENT = `(?:${SEGMENT}|\\*)`;
|
|
53
|
+
/** Three segments of the catalog grammar, or `*` in any of them (always drift: wildcards are forbidden). */
|
|
54
|
+
export const KEY_SHAPE = new RegExp(`^${SHAPED_SEGMENT}:${SHAPED_SEGMENT}:${SHAPED_SEGMENT}$`);
|
|
55
|
+
// A segment that may hold `*` anywhere: `dom*`, `pat*ient`, `**`, `*`.
|
|
56
|
+
const WILDCARD_SEGMENT = "[a-z*][a-z0-9*]*(?:-[a-z0-9*]+)*";
|
|
57
|
+
/** Three key-shaped segments with `*` allowed anywhere in them. */
|
|
58
|
+
export const WILDCARD_KEY_SHAPE = new RegExp(`^${WILDCARD_SEGMENT}:${WILDCARD_SEGMENT}:${WILDCARD_SEGMENT}$`);
|
|
59
|
+
const TWO_SEGMENT_WILDCARD = new RegExp(`^(${SEGMENT}):${WILDCARD_SEGMENT}$`);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* True for a literal with `*` that has the shape of a key: three segments
|
|
63
|
+
* with `*` anywhere, or two segments (`servicio:*`, `servicio:recu*`) whose
|
|
64
|
+
* first segment is a catalog service. Other text with `*` (a MIME type, a
|
|
65
|
+
* glob, `image:*` of no service) is not a permission.
|
|
66
|
+
*/
|
|
67
|
+
export function isWildcardKeyLiteral(text, services) {
|
|
68
|
+
if (!text.includes("*")) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
if (WILDCARD_KEY_SHAPE.test(text)) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
const match = TWO_SEGMENT_WILDCARD.exec(text);
|
|
75
|
+
return match !== null && services.has(match[1]);
|
|
76
|
+
}
|
|
77
|
+
const ALLOW_COMMENT = /\/\/\s*allow-service-key\s*:\s*\S/i;
|
|
78
|
+
const USAGE = "Usage: node scripts/check-service-keys.mjs <source-dir> [<source-dir>...] [--json] [--include-tests]\n";
|
|
79
|
+
|
|
80
|
+
function compare(left, right) {
|
|
81
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function isTestFile(path) {
|
|
85
|
+
return TEST_FILE_PATTERNS.some((pattern) => pattern.test(path));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function scriptKindOf(path) {
|
|
89
|
+
const extension = extname(path).toLowerCase();
|
|
90
|
+
if (extension === ".tsx") return ts.ScriptKind.TSX;
|
|
91
|
+
if (extension === ".jsx") return ts.ScriptKind.JSX;
|
|
92
|
+
if (extension === ".js" || extension === ".mjs" || extension === ".cjs") return ts.ScriptKind.JS;
|
|
93
|
+
return ts.ScriptKind.TS;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Source files under root, sorted, skipping build output, dependencies and (by default) tests. */
|
|
97
|
+
export function listSourceFiles(root, { includeTests = false } = {}) {
|
|
98
|
+
const files = [];
|
|
99
|
+
const visit = (dir) => {
|
|
100
|
+
const entries = readdirSync(dir, { withFileTypes: true }).sort((left, right) => compare(left.name, right.name));
|
|
101
|
+
for (const entry of entries) {
|
|
102
|
+
const path = join(dir, entry.name);
|
|
103
|
+
if (entry.isDirectory()) {
|
|
104
|
+
if (!SKIP_DIRS.has(entry.name)) visit(path);
|
|
105
|
+
} else if (entry.isFile() && SOURCE_EXTENSIONS.has(extname(entry.name).toLowerCase()) && !entry.name.endsWith(".d.ts")) {
|
|
106
|
+
if (includeTests || !isTestFile(relative(root, path))) files.push(path);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
visit(root);
|
|
111
|
+
return files;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** String literals and template literals without substitutions of one source text, with 1-based line and column. */
|
|
115
|
+
export function collectStringLiterals(sourceText, fileName) {
|
|
116
|
+
const source = ts.createSourceFile(fileName, sourceText, ts.ScriptTarget.Latest, true, scriptKindOf(fileName));
|
|
117
|
+
const lines = sourceText.split(/\r?\n/);
|
|
118
|
+
const literals = [];
|
|
119
|
+
const visit = (node) => {
|
|
120
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
121
|
+
const position = source.getLineAndCharacterOfPosition(node.getStart(source));
|
|
122
|
+
const line = position.line + 1;
|
|
123
|
+
const allowed = ALLOW_COMMENT.test(lines[line - 1] ?? "") || ALLOW_COMMENT.test(lines[line - 2] ?? "");
|
|
124
|
+
literals.push({ text: node.text, line, column: position.character + 1, allowed });
|
|
125
|
+
}
|
|
126
|
+
ts.forEachChild(node, visit);
|
|
127
|
+
};
|
|
128
|
+
visit(source);
|
|
129
|
+
return literals;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Findings of the literals of the given source files. `catalog` supplies
|
|
134
|
+
* `findUnknownPermissionKeys`, the old keys of PERMISSION_MIGRATION and the
|
|
135
|
+
* catalog services.
|
|
136
|
+
*/
|
|
137
|
+
export function scanFiles(files, catalog, { root = cwd() } = {}) {
|
|
138
|
+
const oldKeys = new Set(catalog.oldKeys);
|
|
139
|
+
const services = new Set(catalog.services ?? []);
|
|
140
|
+
const findings = [];
|
|
141
|
+
let allowed = 0;
|
|
142
|
+
for (const file of files) {
|
|
143
|
+
for (const literal of collectStringLiterals(readFileSync(file, "utf8"), file)) {
|
|
144
|
+
// A literal with `*` in key shape (or an old key with `*`) is always drift, before "old".
|
|
145
|
+
const wildcard = literal.text.includes("*") && (oldKeys.has(literal.text) || isWildcardKeyLiteral(literal.text, services));
|
|
146
|
+
// A catalog key is never a finding, even when it is also an old key kept as is.
|
|
147
|
+
if (!wildcard && (!(oldKeys.has(literal.text) || KEY_SHAPE.test(literal.text)) || catalog.findUnknownPermissionKeys([literal.text]).length === 0)) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const kind = !wildcard && oldKeys.has(literal.text) ? "old" : "drift";
|
|
151
|
+
if (literal.allowed) {
|
|
152
|
+
allowed += 1;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
findings.push({ kind, key: literal.text, file: relative(root, file).split("\\").join("/"), line: literal.line, column: literal.column });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return { files: files.length, allowed, findings };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Distinct keys per kind, sorted. */
|
|
162
|
+
export function summarize(findings) {
|
|
163
|
+
const keys = (kind) => [...new Set(findings.filter((finding) => finding.kind === kind).map((finding) => finding.key))].sort(compare);
|
|
164
|
+
return { old: keys("old"), drift: keys("drift") };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function parseArgs(args) {
|
|
168
|
+
const options = { roots: [], json: false, includeTests: false, help: false, error: undefined };
|
|
169
|
+
for (const arg of args) {
|
|
170
|
+
if (arg === "--json") options.json = true;
|
|
171
|
+
else if (arg === "--include-tests") options.includeTests = true;
|
|
172
|
+
else if (arg === "--help" || arg === "-h") options.help = true;
|
|
173
|
+
else if (arg.startsWith("-")) options.error = `unknown option: ${arg}`;
|
|
174
|
+
else options.roots.push(arg);
|
|
175
|
+
}
|
|
176
|
+
if (!options.help && options.error === undefined && options.roots.length === 0) {
|
|
177
|
+
options.error = "a source directory is required";
|
|
178
|
+
}
|
|
179
|
+
return options;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Runs the check; returns the exit code. `catalog` is injected so tests can use the sources of the catalog. */
|
|
183
|
+
export function run(args, { catalog, write = (text) => stdout.write(text), writeError = (text) => stderr.write(text), root = cwd() }) {
|
|
184
|
+
const options = parseArgs(args);
|
|
185
|
+
if (options.help) {
|
|
186
|
+
write(USAGE);
|
|
187
|
+
return 0;
|
|
188
|
+
}
|
|
189
|
+
if (options.error !== undefined) {
|
|
190
|
+
writeError(`check-service-keys: ${options.error}\n${USAGE}`);
|
|
191
|
+
return 2;
|
|
192
|
+
}
|
|
193
|
+
let files;
|
|
194
|
+
try {
|
|
195
|
+
files = options.roots.flatMap((dir) => listSourceFiles(resolve(root, dir), { includeTests: options.includeTests }));
|
|
196
|
+
} catch (error) {
|
|
197
|
+
writeError(`check-service-keys: cannot read ${options.roots.join(", ")}: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
198
|
+
return 2;
|
|
199
|
+
}
|
|
200
|
+
const result = scanFiles(files, catalog, { root });
|
|
201
|
+
const summary = summarize(result.findings);
|
|
202
|
+
if (options.json) {
|
|
203
|
+
write(`${JSON.stringify({ catalogVersion: catalog.version, ...result, ...summary }, null, 2)}\n`);
|
|
204
|
+
} else {
|
|
205
|
+
for (const finding of result.findings) {
|
|
206
|
+
const hint =
|
|
207
|
+
finding.kind === "old"
|
|
208
|
+
? "old key: use its canonical key (PERMISSION_MIGRATION)"
|
|
209
|
+
: finding.key.includes("*")
|
|
210
|
+
? "wildcards are forbidden: require and grant explicit catalog keys"
|
|
211
|
+
: "not in the capability catalog";
|
|
212
|
+
write(`${finding.file}:${finding.line}:${finding.column} ${finding.kind} ${finding.key} — ${hint}\n`);
|
|
213
|
+
}
|
|
214
|
+
write(
|
|
215
|
+
`check-service-keys: ${result.files} files, ${summary.old.length} old keys, ${summary.drift.length} keys not in the catalog` +
|
|
216
|
+
` (${result.findings.length} literals, ${result.allowed} allowed), catalog ${catalog.version}.\n`
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return result.findings.length > 0 ? 1 : 0;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function loadCatalog() {
|
|
223
|
+
const module = await import(new URL("../dist/capabilities/index.js", import.meta.url).href);
|
|
224
|
+
return {
|
|
225
|
+
version: module.CAPABILITY_CATALOG_VERSION,
|
|
226
|
+
findUnknownPermissionKeys: module.findUnknownPermissionKeys,
|
|
227
|
+
oldKeys: module.PERMISSION_MIGRATION.entries.map((entry) => entry.old),
|
|
228
|
+
services: module.CAPABILITY_SERVICES
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function isEntryPoint() {
|
|
233
|
+
try {
|
|
234
|
+
return argv[1] !== undefined && realpathSync.native(resolve(argv[1])) === realpathSync.native(fileURLToPath(import.meta.url));
|
|
235
|
+
} catch {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (isEntryPoint()) {
|
|
241
|
+
let catalog;
|
|
242
|
+
try {
|
|
243
|
+
catalog = await loadCatalog();
|
|
244
|
+
} catch (error) {
|
|
245
|
+
stderr.write(`check-service-keys: cannot load the built catalog (run npm run build): ${error instanceof Error ? error.message : String(error)}\n`);
|
|
246
|
+
exit(2);
|
|
247
|
+
}
|
|
248
|
+
// exitCode instead of exit(): lets a piped stdout flush.
|
|
249
|
+
process.exitCode = run(argv.slice(2), { catalog });
|
|
250
|
+
}
|