@pegasimed.com/pegasi-sail-connect-shared 0.1.0-dev.0 → 0.1.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +275 -125
  2. package/dist/capabilities/catalog-data.d.ts +4507 -0
  3. package/dist/capabilities/catalog-data.d.ts.map +1 -0
  4. package/dist/capabilities/catalog-data.js +4854 -0
  5. package/dist/capabilities/catalog-data.js.map +1 -0
  6. package/dist/capabilities/catalog.d.ts +41 -0
  7. package/dist/capabilities/catalog.d.ts.map +1 -0
  8. package/dist/capabilities/catalog.js +70 -0
  9. package/dist/capabilities/catalog.js.map +1 -0
  10. package/dist/capabilities/index.d.ts +8 -0
  11. package/dist/capabilities/index.d.ts.map +1 -0
  12. package/dist/capabilities/index.js +8 -0
  13. package/dist/capabilities/index.js.map +1 -0
  14. package/dist/capabilities/list.d.ts +24 -0
  15. package/dist/capabilities/list.d.ts.map +1 -0
  16. package/dist/capabilities/list.js +46 -0
  17. package/dist/capabilities/list.js.map +1 -0
  18. package/dist/capabilities/match.d.ts +43 -0
  19. package/dist/capabilities/match.d.ts.map +1 -0
  20. package/dist/capabilities/match.js +152 -0
  21. package/dist/capabilities/match.js.map +1 -0
  22. package/dist/capabilities/migration-data.d.ts +2364 -0
  23. package/dist/capabilities/migration-data.d.ts.map +1 -0
  24. package/dist/capabilities/migration-data.js +2579 -0
  25. package/dist/capabilities/migration-data.js.map +1 -0
  26. package/dist/capabilities/migration.d.ts +152 -0
  27. package/dist/capabilities/migration.d.ts.map +1 -0
  28. package/dist/capabilities/migration.js +243 -0
  29. package/dist/capabilities/migration.js.map +1 -0
  30. package/dist/capabilities/types.d.ts +108 -0
  31. package/dist/capabilities/types.d.ts.map +1 -0
  32. package/dist/capabilities/types.js +2 -0
  33. package/dist/capabilities/types.js.map +1 -0
  34. package/dist/capabilities/validate.d.ts +17 -0
  35. package/dist/capabilities/validate.d.ts.map +1 -0
  36. package/dist/capabilities/validate.js +281 -0
  37. package/dist/capabilities/validate.js.map +1 -0
  38. package/dist/index.d.ts +1 -0
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +1 -0
  41. package/dist/index.js.map +1 -1
  42. package/package.json +107 -102
  43. package/scripts/check-service-keys.mjs +213 -0
  44. package/scripts/lint-phi.mjs +266 -266
package/package.json CHANGED
@@ -1,102 +1,107 @@
1
- {
2
- "name": "@pegasimed.com/pegasi-sail-connect-shared",
3
- "version": "0.1.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
- },
54
- "files": [
55
- "dist",
56
- "scripts",
57
- "README.md"
58
- ],
59
- "bin": {
60
- "lint-phi": "./scripts/lint-phi.mjs"
61
- },
62
- "scripts": {
63
- "build": "tsc -p tsconfig.json",
64
- "check": "tsc -p tsconfig.json --noEmit",
65
- "prepare": "npm run build",
66
- "lint": "eslint .",
67
- "test": "vitest run --coverage",
68
- "validate": "npm run check && npm run lint && npm test"
69
- },
70
- "engines": {
71
- "node": ">=22"
72
- },
73
- "publishConfig": {
74
- "access": "public",
75
- "registry": "https://registry.npmjs.org/"
76
- },
77
- "peerDependencies": {
78
- "fastify": "^5.0.0",
79
- "pino": "^10.0.0"
80
- },
81
- "peerDependenciesMeta": {
82
- "fastify": {
83
- "optional": true
84
- },
85
- "pino": {
86
- "optional": false
87
- }
88
- },
89
- "devDependencies": {
90
- "@types/node": "^24.10.1",
91
- "@vitest/coverage-v8": "^4.1.8",
92
- "eslint": "^10.4.1",
93
- "fastify": "^5.6.2",
94
- "pino": "^10.3.1",
95
- "typescript": "^5.9.3",
96
- "typescript-eslint": "^8.61.0",
97
- "vitest": "^4.0.14"
98
- },
99
- "overrides": {
100
- "fast-uri": "3.1.2"
101
- }
102
- }
1
+ {
2
+ "name": "@pegasimed.com/pegasi-sail-connect-shared",
3
+ "version": "0.1.0-dev.2",
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
+ },
58
+ "files": [
59
+ "dist",
60
+ "scripts/lint-phi.mjs",
61
+ "scripts/check-service-keys.mjs",
62
+ "README.md"
63
+ ],
64
+ "bin": {
65
+ "lint-phi": "./scripts/lint-phi.mjs"
66
+ },
67
+ "scripts": {
68
+ "build": "tsc -p tsconfig.json",
69
+ "check": "tsc -p tsconfig.json --noEmit && tsc -p test/capabilities/tsconfig.json",
70
+ "prepare": "npm run build",
71
+ "lint": "eslint .",
72
+ "test": "vitest run --coverage",
73
+ "validate": "npm run check && npm run lint && npm test"
74
+ },
75
+ "engines": {
76
+ "node": ">=22"
77
+ },
78
+ "publishConfig": {
79
+ "access": "public",
80
+ "registry": "https://registry.npmjs.org/"
81
+ },
82
+ "peerDependencies": {
83
+ "fastify": "^5.0.0",
84
+ "pino": "^10.0.0"
85
+ },
86
+ "peerDependenciesMeta": {
87
+ "fastify": {
88
+ "optional": true
89
+ },
90
+ "pino": {
91
+ "optional": false
92
+ }
93
+ },
94
+ "devDependencies": {
95
+ "@types/node": "^24.10.1",
96
+ "@vitest/coverage-v8": "^4.1.8",
97
+ "eslint": "^10.4.1",
98
+ "fastify": "^5.6.2",
99
+ "pino": "^10.3.1",
100
+ "typescript": "^5.9.3",
101
+ "typescript-eslint": "^8.61.0",
102
+ "vitest": "^4.0.14"
103
+ },
104
+ "overrides": {
105
+ "fast-uri": "3.1.2"
106
+ }
107
+ }
@@ -0,0 +1,213 @@
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, `*` allowed in resource and action)
15
+ // but is neither a catalog operation nor a listed wildcard grant.
16
+ //
17
+ // Keys built at run time (template literals with substitutions,
18
+ // concatenations) are not visible here: the authorization helper of the
19
+ // service must only accept `PermissionKey`, so the TypeScript compiler
20
+ // rejects them (see README, "Evitar el desfase entre servicios y catálogo").
21
+ //
22
+ // Override mechanism: a literal that has the shape of a key but is not one
23
+ // (a cache key, an event name) is silenced with a comment
24
+ // // allow-service-key: <human-readable reason>
25
+ // on the same line or directly above.
26
+ //
27
+ // Test files (*.test.*, *.spec.*, test/, tests/, __tests__/) are skipped
28
+ // unless --include-tests is given. Requires `typescript` (a devDependency of
29
+ // every SAIL/CONNECT service) and the built catalog of this package.
30
+ //
31
+ // Usage:
32
+ // node node_modules/@pegasimed.com/pegasi-sail-connect-shared/scripts/check-service-keys.mjs src/
33
+ // node scripts/check-service-keys.mjs src/ --json
34
+ // node scripts/check-service-keys.mjs --help
35
+ //
36
+ // Exit codes: 0 no findings, 1 findings, 2 usage error.
37
+
38
+ import { readdirSync, readFileSync, realpathSync } from "node:fs";
39
+ import { extname, join, relative, resolve } from "node:path";
40
+ import process, { argv, cwd, exit, stderr, stdout } from "node:process";
41
+ import { fileURLToPath } from "node:url";
42
+ import ts from "typescript";
43
+
44
+ const SOURCE_EXTENSIONS = new Set([".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]);
45
+ const SKIP_DIRS = new Set(["node_modules", "dist", "build", "coverage", ".git", ".next", ".turbo"]);
46
+ const TEST_FILE_PATTERNS = [/\.test\.[mc]?[jt]sx?$/i, /\.spec\.[mc]?[jt]sx?$/i, /(^|[\\/])(test|tests|__tests__)[\\/]/i];
47
+ const SEGMENT = "[a-z][a-z0-9]*(?:-[a-z0-9]+)*";
48
+ /** Three segments of the catalog grammar; `*` only in resource and action. */
49
+ export const KEY_SHAPE = new RegExp(`^${SEGMENT}:(?:${SEGMENT}|\\*):(?:${SEGMENT}|\\*)$`);
50
+ const ALLOW_COMMENT = /\/\/\s*allow-service-key\s*:\s*\S/i;
51
+ const USAGE = "Usage: node scripts/check-service-keys.mjs <source-dir> [<source-dir>...] [--json] [--include-tests]\n";
52
+
53
+ function compare(left, right) {
54
+ return left < right ? -1 : left > right ? 1 : 0;
55
+ }
56
+
57
+ function isTestFile(path) {
58
+ return TEST_FILE_PATTERNS.some((pattern) => pattern.test(path));
59
+ }
60
+
61
+ function scriptKindOf(path) {
62
+ const extension = extname(path).toLowerCase();
63
+ if (extension === ".tsx") return ts.ScriptKind.TSX;
64
+ if (extension === ".jsx") return ts.ScriptKind.JSX;
65
+ if (extension === ".js" || extension === ".mjs" || extension === ".cjs") return ts.ScriptKind.JS;
66
+ return ts.ScriptKind.TS;
67
+ }
68
+
69
+ /** Source files under root, sorted, skipping build output, dependencies and (by default) tests. */
70
+ export function listSourceFiles(root, { includeTests = false } = {}) {
71
+ const files = [];
72
+ const visit = (dir) => {
73
+ const entries = readdirSync(dir, { withFileTypes: true }).sort((left, right) => compare(left.name, right.name));
74
+ for (const entry of entries) {
75
+ const path = join(dir, entry.name);
76
+ if (entry.isDirectory()) {
77
+ if (!SKIP_DIRS.has(entry.name)) visit(path);
78
+ } else if (entry.isFile() && SOURCE_EXTENSIONS.has(extname(entry.name).toLowerCase()) && !entry.name.endsWith(".d.ts")) {
79
+ if (includeTests || !isTestFile(relative(root, path))) files.push(path);
80
+ }
81
+ }
82
+ };
83
+ visit(root);
84
+ return files;
85
+ }
86
+
87
+ /** String literals and template literals without substitutions of one source text, with 1-based line and column. */
88
+ export function collectStringLiterals(sourceText, fileName) {
89
+ const source = ts.createSourceFile(fileName, sourceText, ts.ScriptTarget.Latest, true, scriptKindOf(fileName));
90
+ const lines = sourceText.split(/\r?\n/);
91
+ const literals = [];
92
+ const visit = (node) => {
93
+ if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
94
+ const position = source.getLineAndCharacterOfPosition(node.getStart(source));
95
+ const line = position.line + 1;
96
+ const allowed = ALLOW_COMMENT.test(lines[line - 1] ?? "") || ALLOW_COMMENT.test(lines[line - 2] ?? "");
97
+ literals.push({ text: node.text, line, column: position.character + 1, allowed });
98
+ }
99
+ ts.forEachChild(node, visit);
100
+ };
101
+ visit(source);
102
+ return literals;
103
+ }
104
+
105
+ /**
106
+ * Findings of the literals of the given source files. `catalog` supplies
107
+ * `findUnknownPermissionKeys` and the old keys of PERMISSION_MIGRATION.
108
+ */
109
+ export function scanFiles(files, catalog, { root = cwd() } = {}) {
110
+ const oldKeys = new Set(catalog.oldKeys);
111
+ const findings = [];
112
+ let allowed = 0;
113
+ for (const file of files) {
114
+ for (const literal of collectStringLiterals(readFileSync(file, "utf8"), file)) {
115
+ // A catalog key is never a finding, even when it is also an old key kept as is.
116
+ if (!(oldKeys.has(literal.text) || KEY_SHAPE.test(literal.text)) || catalog.findUnknownPermissionKeys([literal.text]).length === 0) {
117
+ continue;
118
+ }
119
+ const kind = oldKeys.has(literal.text) ? "old" : "drift";
120
+ if (literal.allowed) {
121
+ allowed += 1;
122
+ continue;
123
+ }
124
+ findings.push({ kind, key: literal.text, file: relative(root, file).split("\\").join("/"), line: literal.line, column: literal.column });
125
+ }
126
+ }
127
+ return { files: files.length, allowed, findings };
128
+ }
129
+
130
+ /** Distinct keys per kind, sorted. */
131
+ export function summarize(findings) {
132
+ const keys = (kind) => [...new Set(findings.filter((finding) => finding.kind === kind).map((finding) => finding.key))].sort(compare);
133
+ return { old: keys("old"), drift: keys("drift") };
134
+ }
135
+
136
+ function parseArgs(args) {
137
+ const options = { roots: [], json: false, includeTests: false, help: false, error: undefined };
138
+ for (const arg of args) {
139
+ if (arg === "--json") options.json = true;
140
+ else if (arg === "--include-tests") options.includeTests = true;
141
+ else if (arg === "--help" || arg === "-h") options.help = true;
142
+ else if (arg.startsWith("-")) options.error = `unknown option: ${arg}`;
143
+ else options.roots.push(arg);
144
+ }
145
+ if (!options.help && options.error === undefined && options.roots.length === 0) {
146
+ options.error = "a source directory is required";
147
+ }
148
+ return options;
149
+ }
150
+
151
+ /** Runs the check; returns the exit code. `catalog` is injected so tests can use the sources of the catalog. */
152
+ export function run(args, { catalog, write = (text) => stdout.write(text), writeError = (text) => stderr.write(text), root = cwd() }) {
153
+ const options = parseArgs(args);
154
+ if (options.help) {
155
+ write(USAGE);
156
+ return 0;
157
+ }
158
+ if (options.error !== undefined) {
159
+ writeError(`check-service-keys: ${options.error}\n${USAGE}`);
160
+ return 2;
161
+ }
162
+ let files;
163
+ try {
164
+ files = options.roots.flatMap((dir) => listSourceFiles(resolve(root, dir), { includeTests: options.includeTests }));
165
+ } catch (error) {
166
+ writeError(`check-service-keys: cannot read ${options.roots.join(", ")}: ${error instanceof Error ? error.message : String(error)}\n`);
167
+ return 2;
168
+ }
169
+ const result = scanFiles(files, catalog, { root });
170
+ const summary = summarize(result.findings);
171
+ if (options.json) {
172
+ write(`${JSON.stringify({ catalogVersion: catalog.version, ...result, ...summary }, null, 2)}\n`);
173
+ } else {
174
+ for (const finding of result.findings) {
175
+ const hint = finding.kind === "old" ? "old key: use its canonical key (PERMISSION_MIGRATION)" : "not in the capability catalog";
176
+ write(`${finding.file}:${finding.line}:${finding.column} ${finding.kind} ${finding.key} — ${hint}\n`);
177
+ }
178
+ write(
179
+ `check-service-keys: ${result.files} files, ${summary.old.length} old keys, ${summary.drift.length} keys not in the catalog` +
180
+ ` (${result.findings.length} literals, ${result.allowed} allowed), catalog ${catalog.version}.\n`
181
+ );
182
+ }
183
+ return result.findings.length > 0 ? 1 : 0;
184
+ }
185
+
186
+ async function loadCatalog() {
187
+ const module = await import(new URL("../dist/capabilities/index.js", import.meta.url).href);
188
+ return {
189
+ version: module.CAPABILITY_CATALOG_VERSION,
190
+ findUnknownPermissionKeys: module.findUnknownPermissionKeys,
191
+ oldKeys: module.PERMISSION_MIGRATION.entries.map((entry) => entry.old)
192
+ };
193
+ }
194
+
195
+ function isEntryPoint() {
196
+ try {
197
+ return argv[1] !== undefined && realpathSync.native(resolve(argv[1])) === realpathSync.native(fileURLToPath(import.meta.url));
198
+ } catch {
199
+ return false;
200
+ }
201
+ }
202
+
203
+ if (isEntryPoint()) {
204
+ let catalog;
205
+ try {
206
+ catalog = await loadCatalog();
207
+ } catch (error) {
208
+ stderr.write(`check-service-keys: cannot load the built catalog (run npm run build): ${error instanceof Error ? error.message : String(error)}\n`);
209
+ exit(2);
210
+ }
211
+ // exitCode instead of exit(): lets a piped stdout flush.
212
+ process.exitCode = run(argv.slice(2), { catalog });
213
+ }