@pegasimed.com/pegasi-sail-connect-shared 0.2.0-dev.1 → 0.2.0-dev.11

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 (56) hide show
  1. package/README.md +355 -30
  2. package/dist/authorization/index.d.ts +1 -0
  3. package/dist/authorization/index.d.ts.map +1 -1
  4. package/dist/authorization/index.js +1 -0
  5. package/dist/authorization/index.js.map +1 -1
  6. package/dist/authorization/work-grants.d.ts +151 -0
  7. package/dist/authorization/work-grants.d.ts.map +1 -0
  8. package/dist/authorization/work-grants.js +306 -0
  9. package/dist/authorization/work-grants.js.map +1 -0
  10. package/dist/authorization-testing.d.ts +229 -0
  11. package/dist/authorization-testing.d.ts.map +1 -0
  12. package/dist/authorization-testing.js +744 -0
  13. package/dist/authorization-testing.js.map +1 -0
  14. package/dist/capabilities/canonical-json.d.ts +3 -0
  15. package/dist/capabilities/canonical-json.d.ts.map +1 -0
  16. package/dist/capabilities/canonical-json.js +83 -0
  17. package/dist/capabilities/canonical-json.js.map +1 -0
  18. package/dist/capabilities/catalog-data.d.ts +559 -281
  19. package/dist/capabilities/catalog-data.d.ts.map +1 -1
  20. package/dist/capabilities/catalog-data.js +582 -287
  21. package/dist/capabilities/catalog-data.js.map +1 -1
  22. package/dist/capabilities/catalog.d.ts.map +1 -1
  23. package/dist/capabilities/catalog.js +4 -4
  24. package/dist/capabilities/catalog.js.map +1 -1
  25. package/dist/capabilities/index.d.ts +4 -0
  26. package/dist/capabilities/index.d.ts.map +1 -1
  27. package/dist/capabilities/index.js +7 -0
  28. package/dist/capabilities/index.js.map +1 -1
  29. package/dist/capabilities/migration-data.d.ts +105 -190
  30. package/dist/capabilities/migration-data.d.ts.map +1 -1
  31. package/dist/capabilities/migration-data.js +83 -144
  32. package/dist/capabilities/migration-data.js.map +1 -1
  33. package/dist/capabilities/migration.d.ts +20 -20
  34. package/dist/capabilities/migration.d.ts.map +1 -1
  35. package/dist/capabilities/migration.js +25 -40
  36. package/dist/capabilities/migration.js.map +1 -1
  37. package/dist/capabilities/sha256.d.ts +18 -0
  38. package/dist/capabilities/sha256.d.ts.map +1 -0
  39. package/dist/capabilities/sha256.js +180 -0
  40. package/dist/capabilities/sha256.js.map +1 -0
  41. package/dist/capabilities/types.d.ts +3 -2
  42. package/dist/capabilities/types.d.ts.map +1 -1
  43. package/dist/index.d.ts +1 -0
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +1 -0
  46. package/dist/index.js.map +1 -1
  47. package/dist/organization/index.d.ts +135 -0
  48. package/dist/organization/index.d.ts.map +1 -0
  49. package/dist/organization/index.js +92 -0
  50. package/dist/organization/index.js.map +1 -0
  51. package/dist/worker-logger.d.ts +46 -4
  52. package/dist/worker-logger.d.ts.map +1 -1
  53. package/dist/worker-logger.js +33 -11
  54. package/dist/worker-logger.js.map +1 -1
  55. package/package.json +11 -3
  56. package/scripts/check-service-keys.mjs +30 -7
@@ -5,24 +5,46 @@
5
5
  // need the shared PHI_LOG_REDACT_PATHS applied so accidentally
6
6
  // logging a PHI field gets redacted just like in the HTTP path.
7
7
  //
8
- // Pino is bundled with Fastify (peerDependency); we declare it as
9
- // our own peerDependency too so consumers don't pull a duplicate copy.
8
+ // Pino is an OPTIONAL peer dependency (0.2.0-dev.2, PKG-06): importing the
9
+ // package (or any subpath) never loads it. createWorkerLogger loads it
10
+ // lazily on its first call, from the consumer's installation, or uses the
11
+ // factory passed in `options.pino`. Without either it throws
12
+ // WorkerLoggerUnavailableError. Only types are imported from pino here, and
13
+ // they are erased from the compiled JavaScript.
10
14
  //
11
15
  // Usage:
12
16
  // import { createWorkerLogger } from "@pegasimed.com/pegasi-sail-connect-shared";
13
17
  // const log = createWorkerLogger("commercial-core-api", "outbox-worker");
14
18
  // log.info({ result }, "cycle done");
15
- import pino from "pino";
19
+ import { createRequire } from "node:module";
16
20
  import { PHI_LOG_REDACT_CENSOR, PHI_LOG_REDACT_PATHS } from "./phi-redaction.js";
17
- /**
18
- * Build a Pino logger pre-configured with the shared PHI redact paths
19
- * and a `<service>/<component>` name.
20
- *
21
- * @param service e.g. "commercial-core-api" — the npm package name root
22
- * @param component e.g. "outbox-worker" — the worker's role
23
- */
21
+ /** Thrown by createWorkerLogger when pino is not installed and no factory was passed. */
22
+ export class WorkerLoggerUnavailableError extends Error {
23
+ code = "WORKER_LOGGER_UNAVAILABLE";
24
+ constructor(cause) {
25
+ super('createWorkerLogger needs the optional peer dependency "pino" (^10): install it or pass options.pino.', { cause });
26
+ this.name = "WorkerLoggerUnavailableError";
27
+ }
28
+ }
29
+ const requireFromPackage = createRequire(import.meta.url);
30
+ let loadedPino;
31
+ // Loads pino on first use only (never at import time) and keeps it.
32
+ function loadPino() {
33
+ if (loadedPino === undefined) {
34
+ try {
35
+ const loaded = requireFromPackage("pino");
36
+ loadedPino = (config) => loaded(config);
37
+ }
38
+ catch (error) {
39
+ throw new WorkerLoggerUnavailableError(error);
40
+ }
41
+ }
42
+ return loadedPino;
43
+ }
24
44
  export function createWorkerLogger(service, component, options = {}) {
25
- return pino({
45
+ // Without a defined factory the second overload applies: L is pino's own Logger.
46
+ const factory = options.pino ?? loadPino();
47
+ return factory({
26
48
  name: `${service}/${component}`,
27
49
  level: options.level ?? process.env.LOG_LEVEL ?? "info",
28
50
  redact: { paths: [...PHI_LOG_REDACT_PATHS], censor: PHI_LOG_REDACT_CENSOR }
@@ -1 +1 @@
1
- {"version":3,"file":"worker-logger.js","sourceRoot":"","sources":["../src/worker-logger.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAC3D,iEAAiE;AACjE,gEAAgE;AAChE,oEAAoE;AACpE,+DAA+D;AAC/D,gEAAgE;AAChE,EAAE;AACF,kEAAkE;AAClE,uEAAuE;AACvE,EAAE;AACF,SAAS;AACT,oFAAoF;AACpF,4EAA4E;AAC5E,wCAAwC;AAExC,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,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SAAS,EAAE,CAAC;QAC7B,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,kBAAkB,CAAC,MAAM,CAAuC,CAAC;YAChF,UAAU,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC1C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,4BAA4B,CAAC,KAAK,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AA0BD,MAAM,UAAU,kBAAkB,CAChC,OAAe,EACf,SAAiB,EACjB,UAAyG,EAAE;IAE3G,iFAAiF;IACjF,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,IAAK,QAAQ,EAAwC,CAAC;IAClF,OAAO,OAAO,CAAC;QACb,IAAI,EAAE,GAAG,OAAO,IAAI,SAAS,EAAE;QAC/B,KAAK,EAAE,OAAO,CAAC,KAAK,IAAK,OAAO,CAAC,GAAG,CAAC,SAAyC,IAAI,MAAM;QACxF,MAAM,EAAE,EAAE,KAAK,EAAE,CAAC,GAAG,oBAAoB,CAAC,EAAE,MAAM,EAAE,qBAAqB,EAAE;KAC5E,CAAC,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pegasimed.com/pegasi-sail-connect-shared",
3
- "version": "0.2.0-dev.1",
3
+ "version": "0.2.0-dev.11",
4
4
  "description": "Versioned shared TypeScript helpers for PEGASI SAIL/CONNECT services.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -57,6 +57,14 @@
57
57
  "./authorization": {
58
58
  "types": "./dist/authorization/index.d.ts",
59
59
  "import": "./dist/authorization/index.js"
60
+ },
61
+ "./authorization/testing": {
62
+ "types": "./dist/authorization-testing.d.ts",
63
+ "import": "./dist/authorization-testing.js"
64
+ },
65
+ "./organization": {
66
+ "types": "./dist/organization/index.d.ts",
67
+ "import": "./dist/organization/index.js"
60
68
  }
61
69
  },
62
70
  "files": [
@@ -70,7 +78,7 @@
70
78
  },
71
79
  "scripts": {
72
80
  "build": "tsc -p tsconfig.json",
73
- "check": "tsc -p tsconfig.json --noEmit && tsc -p test/capabilities/tsconfig.json && tsc -p test/authorization/tsconfig.json",
81
+ "check": "tsc -p tsconfig.json --noEmit && tsc -p test/tsconfig.json && tsc -p test/capabilities/tsconfig.json && tsc -p test/authorization/tsconfig.json",
74
82
  "prepare": "npm run build",
75
83
  "lint": "eslint .",
76
84
  "test": "vitest run --coverage",
@@ -92,7 +100,7 @@
92
100
  "optional": true
93
101
  },
94
102
  "pino": {
95
- "optional": false
103
+ "optional": true
96
104
  }
97
105
  },
98
106
  "devDependencies": {
@@ -13,10 +13,12 @@
13
13
  // - "drift": the literal has the shape `servicio:recurso:acción` (three
14
14
  // segments of the catalog grammar) but is not a catalog key; or it is a
15
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.
16
+ // `patient:**:read`, `*:*:*`), a two-segment `servicio:*` whose first
17
+ // segment is a catalog service or the namespace of an old key of
18
+ // PERMISSION_MIGRATION (`patient:*`, `meetings:*`, `access-api:*`,
19
+ // `users:*`), or an old key with `*`. A literal with `*` is always drift,
20
+ // before "old": there are no wildcard grants, every grant is an explicit
21
+ // catalog key, and since 0.2.0-dev.4 every stored wildcard is quarantined.
20
22
  //
21
23
  // Keys built at run time (template literals with substitutions,
22
24
  // concatenations) are not visible here: the authorization helper of the
@@ -57,12 +59,14 @@ const WILDCARD_SEGMENT = "[a-z*][a-z0-9*]*(?:-[a-z0-9*]+)*";
57
59
  /** Three key-shaped segments with `*` allowed anywhere in them. */
58
60
  export const WILDCARD_KEY_SHAPE = new RegExp(`^${WILDCARD_SEGMENT}:${WILDCARD_SEGMENT}:${WILDCARD_SEGMENT}$`);
59
61
  const TWO_SEGMENT_WILDCARD = new RegExp(`^(${SEGMENT}):${WILDCARD_SEGMENT}$`);
62
+ const NAMESPACE = new RegExp(`^${SEGMENT}$`);
60
63
 
61
64
  /**
62
65
  * True for a literal with `*` that has the shape of a key: three segments
63
66
  * 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.
67
+ * first segment is in `services` (scanFiles passes wildcardNamespaces: the
68
+ * catalog services and the old key namespaces). Other text with `*` (a MIME
69
+ * type, a glob, `image:*` of no service) is not a permission.
66
70
  */
67
71
  export function isWildcardKeyLiteral(text, services) {
68
72
  if (!text.includes("*")) {
@@ -129,6 +133,25 @@ export function collectStringLiterals(sourceText, fileName) {
129
133
  return literals;
130
134
  }
131
135
 
136
+ /**
137
+ * First segments whose two-segment `servicio:*` is a wildcard grant: the
138
+ * catalog services and the namespaces of the old keys of PERMISSION_MIGRATION.
139
+ * Since 0.2.0-dev.4 no wildcard is an old key (all are quarantined), so an old
140
+ * service name such as `access-api` (of `access-api:*`) comes from the old
141
+ * keys of its namespace (`access-api:write`...).
142
+ */
143
+ export function wildcardNamespaces(catalog) {
144
+ const namespaces = new Set(catalog.services ?? []);
145
+ for (const old of catalog.oldKeys) {
146
+ const separator = old.indexOf(":");
147
+ const namespace = separator > 0 ? old.slice(0, separator).toLowerCase() : "";
148
+ if (NAMESPACE.test(namespace)) {
149
+ namespaces.add(namespace);
150
+ }
151
+ }
152
+ return namespaces;
153
+ }
154
+
132
155
  /**
133
156
  * Findings of the literals of the given source files. `catalog` supplies
134
157
  * `findUnknownPermissionKeys`, the old keys of PERMISSION_MIGRATION and the
@@ -136,7 +159,7 @@ export function collectStringLiterals(sourceText, fileName) {
136
159
  */
137
160
  export function scanFiles(files, catalog, { root = cwd() } = {}) {
138
161
  const oldKeys = new Set(catalog.oldKeys);
139
- const services = new Set(catalog.services ?? []);
162
+ const services = wildcardNamespaces(catalog);
140
163
  const findings = [];
141
164
  let allowed = 0;
142
165
  for (const file of files) {