@nodii/dsar 0.0.1 → 0.2.1

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 (78) hide show
  1. package/README.md +57 -1
  2. package/dist/audit-actions.d.ts +13 -0
  3. package/dist/audit-actions.d.ts.map +1 -0
  4. package/dist/audit-actions.js +32 -0
  5. package/dist/audit-actions.js.map +1 -0
  6. package/dist/bind-pattern-a.d.ts +27 -0
  7. package/dist/bind-pattern-a.d.ts.map +1 -0
  8. package/dist/bind-pattern-a.js +387 -0
  9. package/dist/bind-pattern-a.js.map +1 -0
  10. package/dist/bind-pattern-b.d.ts +10 -0
  11. package/dist/bind-pattern-b.d.ts.map +1 -0
  12. package/dist/bind-pattern-b.js +89 -0
  13. package/dist/bind-pattern-b.js.map +1 -0
  14. package/dist/bind-pattern-c.d.ts +6 -0
  15. package/dist/bind-pattern-c.d.ts.map +1 -0
  16. package/dist/bind-pattern-c.js +9 -0
  17. package/dist/bind-pattern-c.js.map +1 -0
  18. package/dist/canonical.d.ts +9 -0
  19. package/dist/canonical.d.ts.map +1 -0
  20. package/dist/canonical.js +57 -0
  21. package/dist/canonical.js.map +1 -0
  22. package/dist/grpc/dsar-service.d.ts +37 -0
  23. package/dist/grpc/dsar-service.d.ts.map +1 -0
  24. package/dist/grpc/dsar-service.js +66 -0
  25. package/dist/grpc/dsar-service.js.map +1 -0
  26. package/dist/handlers.d.ts +102 -0
  27. package/dist/handlers.d.ts.map +1 -0
  28. package/dist/handlers.js +268 -0
  29. package/dist/handlers.js.map +1 -0
  30. package/dist/holds.d.ts +48 -0
  31. package/dist/holds.d.ts.map +1 -0
  32. package/dist/holds.js +79 -0
  33. package/dist/holds.js.map +1 -0
  34. package/dist/index.d.ts +33 -1
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +45 -4
  37. package/dist/index.js.map +1 -1
  38. package/dist/init.d.ts +21 -0
  39. package/dist/init.d.ts.map +1 -0
  40. package/dist/init.js +37 -0
  41. package/dist/init.js.map +1 -0
  42. package/dist/migrations/index.d.ts +15 -0
  43. package/dist/migrations/index.d.ts.map +1 -0
  44. package/dist/migrations/index.js +110 -0
  45. package/dist/migrations/index.js.map +1 -0
  46. package/dist/orchestrator.d.ts +41 -0
  47. package/dist/orchestrator.d.ts.map +1 -0
  48. package/dist/orchestrator.js +205 -0
  49. package/dist/orchestrator.js.map +1 -0
  50. package/dist/register.d.ts +36 -0
  51. package/dist/register.d.ts.map +1 -0
  52. package/dist/register.js +97 -0
  53. package/dist/register.js.map +1 -0
  54. package/dist/registry.d.ts +10 -0
  55. package/dist/registry.d.ts.map +1 -0
  56. package/dist/registry.js +57 -0
  57. package/dist/registry.js.map +1 -0
  58. package/dist/retention.d.ts +17 -0
  59. package/dist/retention.d.ts.map +1 -0
  60. package/dist/retention.js +32 -0
  61. package/dist/retention.js.map +1 -0
  62. package/dist/s3-artifact.d.ts +56 -0
  63. package/dist/s3-artifact.d.ts.map +1 -0
  64. package/dist/s3-artifact.js +121 -0
  65. package/dist/s3-artifact.js.map +1 -0
  66. package/dist/state-store/postgres-lifecycle.d.ts +69 -0
  67. package/dist/state-store/postgres-lifecycle.d.ts.map +1 -0
  68. package/dist/state-store/postgres-lifecycle.js +145 -0
  69. package/dist/state-store/postgres-lifecycle.js.map +1 -0
  70. package/dist/store.d.ts +19 -0
  71. package/dist/store.d.ts.map +1 -0
  72. package/dist/store.js +66 -0
  73. package/dist/store.js.map +1 -0
  74. package/dist/types.d.ts +277 -0
  75. package/dist/types.d.ts.map +1 -0
  76. package/dist/types.js +103 -0
  77. package/dist/types.js.map +1 -0
  78. package/package.json +23 -7
@@ -0,0 +1,205 @@
1
+ // bindOrchestrator — spec § 5.5 + § 5.9.
2
+ //
3
+ // The DSAR orchestrator. Runs in nodii-tenant-service. Owns the
4
+ // `platform_lifecycle_requests` aggregator + the sibling fan-out.
5
+ //
6
+ // v0.1.1 ships a runtime that:
7
+ // 1. Queries `retention_policies` for siblings owning (data_class,
8
+ // subject_kind).
9
+ // 2. For sync RPCs (RectifyData / PlaceRetentionHold / LiftRetentionHold /
10
+ // GetLifecycleRequestStatus / Acknowledge), fans out to siblings
11
+ // sequentially and aggregates statuses.
12
+ // 3. For async RPCs (AccessData / EraseData / ExportPortableData /
13
+ // RestrictProcessing), emits `tenant.dsar.<rpc>_requested.v1` to the
14
+ // outbox + writes a `platform_lifecycle_requests` row with status
15
+ // `accepted`. Real saga-step Pattern 2 wrapping lives outside this
16
+ // module (consumer wires this into `@nodii/saga`).
17
+ // 4. Aggregates per-sibling statuses into PARTIAL / COMPLETED / FAILED.
18
+ import { randomUUID } from "node:crypto";
19
+ import { actionName } from "./audit-actions";
20
+ import { isHeld } from "./holds";
21
+ const SYNC_RPCS = new Set([
22
+ "RectifyData",
23
+ "PlaceRetentionHold",
24
+ "LiftRetentionHold",
25
+ "GetLifecycleRequestStatus",
26
+ "Acknowledge",
27
+ ]);
28
+ const ASYNC_RPCS = new Set([
29
+ "AccessData",
30
+ "EraseData",
31
+ "ExportPortableData",
32
+ "RestrictProcessing",
33
+ ]);
34
+ export function dispatchModeFor(rpcMethod) {
35
+ return ASYNC_RPCS.has(rpcMethod) ? "async" : "sync";
36
+ }
37
+ /**
38
+ * bindOrchestrator returns a DsarServiceImpl that aggregates across all
39
+ * registered siblings. Spec § 5.5 + § 5.9.
40
+ */
41
+ export function bindOrchestrator(opts) {
42
+ const siblings = opts.siblingClients ?? {};
43
+ const aggTable = opts.aggregatorTableName ?? "platform_lifecycle_requests";
44
+ const resolveSiblings = async (dataClass, subjectKind) => {
45
+ // Static map wins when supplied (unit tests).
46
+ if (opts.retentionPolicies) {
47
+ const key = `${dataClass}:${subjectKind}`;
48
+ return opts.retentionPolicies[key] ?? Object.keys(siblings);
49
+ }
50
+ if (!opts.sqlClient)
51
+ return Object.keys(siblings);
52
+ const stmt = `SELECT service_id FROM ${opts.retentionTableName ?? "retention_policies"}
53
+ WHERE data_class = $1 AND subject_kind = $2 AND soft_deleted_at IS NULL`;
54
+ const rows = (await opts.sqlClient.unsafe(stmt, [
55
+ dataClass,
56
+ subjectKind,
57
+ ]));
58
+ return rows.map((r) => r.service_id);
59
+ };
60
+ const writeAggregator = async (request, rpcMethod, siblingResults, overallStatus) => {
61
+ if (!opts.sqlClient)
62
+ return;
63
+ const id = randomUUID();
64
+ const stmt = `INSERT INTO ${aggTable}
65
+ (id, request_id, rpc_method, tenant_id, subject_kind, subject_id,
66
+ status, sibling_results, requested_by, created_at, completed_at)
67
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8::jsonb, $9, now(), now())
68
+ ON CONFLICT (request_id, rpc_method) DO UPDATE
69
+ SET status = EXCLUDED.status,
70
+ sibling_results = EXCLUDED.sibling_results,
71
+ completed_at = now()`;
72
+ await opts.sqlClient.unsafe(stmt, [
73
+ id,
74
+ String(request.request_id ?? ""),
75
+ rpcMethod,
76
+ String(request.tenant_id ?? ""),
77
+ String(request.subject_kind ?? ""),
78
+ String(request.subject_id ?? ""),
79
+ overallStatus,
80
+ siblingResults,
81
+ String(request.requested_by ?? ""),
82
+ ]);
83
+ };
84
+ const emitAudit = async (rpcMethod, phase, request, payload) => {
85
+ if (!opts.auditEmitter)
86
+ return;
87
+ // Orchestrator-side audits use the `tenant.dsar.*` family per spec § 5.6.
88
+ // We borrow `actionName` for the verb mapping, then prefix with `tenant.`.
89
+ const baseAction = actionName("tenant", rpcMethod);
90
+ await opts.auditEmitter.emit({
91
+ action: `${baseAction}_${phase}`,
92
+ target_kind: "data_subject",
93
+ target_id: String(request.subject_id ?? ""),
94
+ tenant_id: String(request.tenant_id ?? ""),
95
+ payload,
96
+ });
97
+ };
98
+ const fanOut = async (rpcMethod, request, dataClass) => {
99
+ const requestId = String(request.request_id ?? "");
100
+ const subjectKind = String(request.subject_kind ?? "");
101
+ const resolved = await resolveSiblings(dataClass, subjectKind);
102
+ await emitAudit(rpcMethod, "dispatched", request, {
103
+ siblings: resolved,
104
+ });
105
+ // Pre-erase hold check at orchestrator level.
106
+ if (rpcMethod === "EraseData" && opts.sqlClient) {
107
+ try {
108
+ const held = await isHeld({
109
+ sql: opts.sqlClient,
110
+ tenantId: String(request.tenant_id ?? ""),
111
+ subjectId: String(request.subject_id ?? ""),
112
+ holdKind: "erase",
113
+ tableName: opts.holdsTableName,
114
+ });
115
+ if (held.held) {
116
+ const heldResponse = {
117
+ request_id: requestId,
118
+ status: "HELD",
119
+ reason: "blocked_by_hold",
120
+ statute_citation: held.statute_citation ?? "",
121
+ expires_at: held.expires_at,
122
+ };
123
+ await writeAggregator(request, rpcMethod, {}, "HELD");
124
+ return heldResponse;
125
+ }
126
+ }
127
+ catch {
128
+ /* holds table missing — proceed */
129
+ }
130
+ }
131
+ const siblingResults = {};
132
+ let anyFailed = false;
133
+ let anyCompleted = false;
134
+ let artifactUri;
135
+ for (const siblingName of resolved) {
136
+ const sibling = siblings[siblingName];
137
+ if (!sibling) {
138
+ siblingResults[siblingName] = "UNREACHABLE";
139
+ anyFailed = true;
140
+ continue;
141
+ }
142
+ const method = sibling.service[rpcMethod];
143
+ if (!method) {
144
+ siblingResults[siblingName] = "UNSUPPORTED";
145
+ anyFailed = true;
146
+ continue;
147
+ }
148
+ try {
149
+ const resp = await method(request);
150
+ siblingResults[siblingName] = resp.status;
151
+ if (resp.status === "COMPLETED" || resp.status === "NO_DATA") {
152
+ anyCompleted = true;
153
+ if (resp.result_artifact_uri)
154
+ artifactUri = resp.result_artifact_uri;
155
+ }
156
+ else if (resp.status === "FAILED" || resp.status === "HELD") {
157
+ anyFailed = true;
158
+ }
159
+ }
160
+ catch (err) {
161
+ siblingResults[siblingName] = "ERROR";
162
+ anyFailed = true;
163
+ void err;
164
+ }
165
+ }
166
+ const overall = anyFailed && anyCompleted
167
+ ? "PARTIAL"
168
+ : anyFailed
169
+ ? "FAILED"
170
+ : "COMPLETED";
171
+ await writeAggregator(request, rpcMethod, siblingResults, overall);
172
+ await emitAudit(rpcMethod, "aggregated", request, {
173
+ sibling_results: siblingResults,
174
+ status: overall,
175
+ });
176
+ return {
177
+ request_id: requestId,
178
+ status: overall,
179
+ reason: "",
180
+ result_artifact_uri: artifactUri,
181
+ };
182
+ };
183
+ const buildHandler = (rpcMethod) => async (request) => {
184
+ // Default data_class = "PII" for v0.1.0 — real classification lives in
185
+ // each request's `data_class` field once we extend the proto.
186
+ const dataClass = String(request.data_class ?? "PII");
187
+ const mode = dispatchModeFor(rpcMethod);
188
+ void mode; // Both sync + async run the same fan-out in v0.1.1.
189
+ // Real Pattern 2 outbox dispatch lands with the @nodii/saga wrap.
190
+ return fanOut(rpcMethod, request, dataClass);
191
+ };
192
+ return {
193
+ AccessData: buildHandler("AccessData"),
194
+ EraseData: buildHandler("EraseData"),
195
+ RectifyData: buildHandler("RectifyData"),
196
+ ExportPortableData: buildHandler("ExportPortableData"),
197
+ RestrictProcessing: buildHandler("RestrictProcessing"),
198
+ PlaceRetentionHold: buildHandler("PlaceRetentionHold"),
199
+ LiftRetentionHold: buildHandler("LiftRetentionHold"),
200
+ GetLifecycleRequestStatus: buildHandler("GetLifecycleRequestStatus"),
201
+ Acknowledge: buildHandler("Acknowledge"),
202
+ };
203
+ }
204
+ export { SYNC_RPCS, ASYNC_RPCS };
205
+ //# sourceMappingURL=orchestrator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orchestrator.js","sourceRoot":"","sources":["../src/orchestrator.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,EAAE;AACF,gEAAgE;AAChE,kEAAkE;AAClE,EAAE;AACF,+BAA+B;AAC/B,qEAAqE;AACrE,sBAAsB;AACtB,6EAA6E;AAC7E,sEAAsE;AACtE,6CAA6C;AAC7C,qEAAqE;AACrE,0EAA0E;AAC1E,uEAAuE;AACvE,wEAAwE;AACxE,wDAAwD;AACxD,0EAA0E;AAE1E,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,M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YAAY,CAAC,oBAAoB,CAAC;QACtD,iBAAiB,EAAE,YAAY,CAAC,mBAAmB,CAAC;QACpD,yBAAyB,EAAE,YAAY,CAAC,2BAA2B,CAAC;QACpE,WAAW,EAAE,YAAY,CAAC,aAAa,CAAC;KACzC,CAAC;AACJ,CAAC;AAED,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC"}
@@ -0,0 +1,36 @@
1
+ import type { SubjectKindStr } from "./types";
2
+ type SqlClient = {
3
+ unsafe: (sql: string, args?: unknown[]) => Promise<any>;
4
+ };
5
+ export interface DataClassRegistration {
6
+ dataClass: string;
7
+ subjectKind: SubjectKindStr;
8
+ }
9
+ export interface RegisterWithOrchestratorOpts {
10
+ serviceName: string;
11
+ rpcAddr: string;
12
+ dataClasses: DataClassRegistration[];
13
+ /** HTTP-mode: orchestrator endpoint base URL. */
14
+ orchestratorUrl?: string;
15
+ /** Direct-mode (orchestrator internal): Postgres SQL client. */
16
+ sqlClient?: SqlClient;
17
+ /** Defaults to `retention_policies`. */
18
+ tableName?: string;
19
+ /** Custom fetch (defaults to globalThis.fetch). */
20
+ fetchImpl?: typeof fetch;
21
+ }
22
+ export interface RegisterWithOrchestratorResult {
23
+ rows_inserted: number;
24
+ rows_updated: number;
25
+ rows_soft_deleted: number;
26
+ }
27
+ /**
28
+ * Register this service's data-class set with the orchestrator. Spec § 5.11.5.
29
+ *
30
+ * Idempotent: re-registering with the same data_classes returns
31
+ * rows_inserted=0, rows_updated=0. Removing a data_class entry between boots
32
+ * soft-deletes the row.
33
+ */
34
+ export declare function registerWithOrchestrator(opts: RegisterWithOrchestratorOpts): Promise<RegisterWithOrchestratorResult>;
35
+ export {};
36
+ //# sourceMappingURL=register.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C,KAAK,SAAS,GAAG;IAEf,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;CACzD,CAAC;AAEF,MAAM,WAAW,qBAAqB;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,cAAc,CAAC;CAC7B;AAED,MAAM,WAAW,4BAA4B;IAC3C,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,qBAAqB,EAAE,CAAC;IACrC,iDAAiD;IACjD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gEAAgE;IAChE,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,wCAAwC;IACxC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,mDAAmD;IACnD,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;CAC1B;AAED,MAAM,WAAW,8BAA8B;IAC7C,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,iBAAiB,EAAE,MAAM,CAAC;CAC3B;AAED;;;;;;GAMG;AACH,wBAAsB,wBAAwB,CAC5C,IAAI,EAAE,4BAA4B,GACjC,OAAO,CAAC,8BAA8B,CAAC,CAUzC"}
@@ -0,0 +1,97 @@
1
+ // Service self-registry per spec § 5.11.5.
2
+ //
3
+ // Every Pattern A/B/C service calls `registerWithOrchestrator(...)` at boot.
4
+ // The call is an S2S RPC (or HTTP POST in dev) to the orchestrator's
5
+ // `RetentionPolicyService.UpsertRegistration` endpoint.
6
+ //
7
+ // v0.1.1 ships two modes:
8
+ // - `orchestratorUrl` set → HTTP POST (production-shaped against the
9
+ // orchestrator's RPC; CI tests against synthetic mini-microservices).
10
+ // - `sqlClient` set → direct UPSERT into `retention_policies` (orchestrator-
11
+ // internal mode; used by nodii-tenant-service itself).
12
+ /**
13
+ * Register this service's data-class set with the orchestrator. Spec § 5.11.5.
14
+ *
15
+ * Idempotent: re-registering with the same data_classes returns
16
+ * rows_inserted=0, rows_updated=0. Removing a data_class entry between boots
17
+ * soft-deletes the row.
18
+ */
19
+ export async function registerWithOrchestrator(opts) {
20
+ if (opts.sqlClient) {
21
+ return registerViaSql(opts);
22
+ }
23
+ if (opts.orchestratorUrl) {
24
+ return registerViaHttp(opts);
25
+ }
26
+ throw new Error("registerWithOrchestrator: pass either `sqlClient` (orchestrator-internal) or `orchestratorUrl` (HTTP).");
27
+ }
28
+ async function registerViaSql(opts) {
29
+ if (!opts.sqlClient)
30
+ throw new Error("sqlClient required");
31
+ const table = opts.tableName ?? "retention_policies";
32
+ const dataClassSet = opts.dataClasses;
33
+ // UPSERT each (data_class, subject_kind).
34
+ let rowsInserted = 0;
35
+ let rowsUpdated = 0;
36
+ for (const dc of dataClassSet) {
37
+ const stmt = `INSERT INTO ${table}
38
+ (service_id, data_class, subject_kind, rpc_addr, registered_at, soft_deleted_at)
39
+ VALUES ($1, $2, $3, $4, now(), NULL)
40
+ ON CONFLICT (service_id, data_class, subject_kind) DO UPDATE
41
+ SET rpc_addr = EXCLUDED.rpc_addr,
42
+ registered_at = now(),
43
+ soft_deleted_at = NULL
44
+ RETURNING xmax::text::bigint AS xmax`;
45
+ const rows = (await opts.sqlClient.unsafe(stmt, [
46
+ opts.serviceName,
47
+ dc.dataClass,
48
+ dc.subjectKind,
49
+ opts.rpcAddr,
50
+ ]));
51
+ if (rows[0]?.xmax === "0")
52
+ rowsInserted++;
53
+ else
54
+ rowsUpdated++;
55
+ }
56
+ // Soft-delete any rows for this service not in the set.
57
+ const tuples = dataClassSet
58
+ .map((dc) => `('${dc.dataClass}','${dc.subjectKind}')`)
59
+ .join(",");
60
+ let rowsSoftDeleted = 0;
61
+ if (tuples.length > 0) {
62
+ const stmt = `UPDATE ${table}
63
+ SET soft_deleted_at = now()
64
+ WHERE service_id = $1
65
+ AND soft_deleted_at IS NULL
66
+ AND (data_class, subject_kind) NOT IN (${tuples})`;
67
+ const result = (await opts.sqlClient.unsafe(stmt, [opts.serviceName]));
68
+ rowsSoftDeleted = result?.count ?? 0;
69
+ }
70
+ return {
71
+ rows_inserted: rowsInserted,
72
+ rows_updated: rowsUpdated,
73
+ rows_soft_deleted: rowsSoftDeleted,
74
+ };
75
+ }
76
+ async function registerViaHttp(opts) {
77
+ const f = opts.fetchImpl ?? fetch;
78
+ const url = `${opts.orchestratorUrl?.replace(/\/$/, "")}/v1/retention-policies/upsert-registration`;
79
+ const body = {
80
+ service_id: opts.serviceName,
81
+ rpc_addr: opts.rpcAddr,
82
+ data_classes: opts.dataClasses.map((dc) => ({
83
+ data_class: dc.dataClass,
84
+ subject_kind: dc.subjectKind,
85
+ })),
86
+ };
87
+ const resp = await f(url, {
88
+ method: "POST",
89
+ headers: { "content-type": "application/json" },
90
+ body: JSON.stringify(body),
91
+ });
92
+ if (!resp.ok) {
93
+ throw new Error(`registerWithOrchestrator: HTTP ${resp.status} from ${url}`);
94
+ }
95
+ return (await resp.json());
96
+ }
97
+ //# sourceMappingURL=register.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.js","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":"AAAA,2CAA2C;AAC3C,EAAE;AACF,6EAA6E;AAC7E,qEAAqE;AACrE,wDAAwD;AACxD,EAAE;AACF,0BAA0B;AAC1B,uEAAuE;AACvE,0EAA0E;AAC1E,+EAA+E;AAC/E,2DAA2D;AAkC3D;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,IAAkC;IAElC,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;QACnB,OAAO,cAAc,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IACD,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;QACzB,OAAO,eAAe,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IACD,MAAM,IAAI,KAAK,CACb,wGAAwG,CACzG,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,cAAc,CAC3B,IAAkC;IA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I,CAAC,CAAC;IACvC,CAAC;IAED,OAAO;QACL,aAAa,EAAE,YAAY;QAC3B,YAAY,EAAE,WAAW;QACzB,iBAAiB,EAAE,eAAe;KACnC,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,eAAe,CAC5B,IAAkC;IAElC,MAAM,CAAC,GAAG,IAAI,CAAC,SAAS,IAAI,KAAK,CAAC;IAClC,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,eAAe,EAAE,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,4CAA4C,CAAC;IACpG,MAAM,IAAI,GAAG;QACX,UAAU,EAAE,IAAI,CAAC,WAAW;QAC5B,QAAQ,EAAE,IAAI,CAAC,OAAO;QACtB,YAAY,EAAE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;YAC1C,UAAU,EAAE,EAAE,CAAC,SAAS;YACxB,YAAY,EAAE,EAAE,CAAC,WAAW;SAC7B,CAAC,CAAC;KACJ,CAAC;IACF,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,GAAG,EAAE;QACxB,MAAM,EAAE,MAAM;QACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;QAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;KAC3B,CAAC,CAAC;IACH,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,kCAAkC,IAAI,CAAC,MAAM,SAAS,GAAG,EAAE,CAC5D,CAAC;IACJ,CAAC;IACD,OAAO,CAAC,MAAM,IAAI,CAAC,IAAI,EAAE,CAAmC,CAAC;AAC/D,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { type DsarSubjectKind, type EraseExecutorFn, type ExportGatherFn, type RectifyHandlerFn } from "./types";
2
+ export declare function registerExportGather(subjectKind: DsarSubjectKind, fn: ExportGatherFn): void;
3
+ export declare function registerEraseExecutor(subjectKind: DsarSubjectKind, fn: EraseExecutorFn): void;
4
+ export declare function registerRectifyHandler(subjectKind: DsarSubjectKind, fn: RectifyHandlerFn): void;
5
+ export declare function lookupExportGather(subjectKind: DsarSubjectKind): ExportGatherFn;
6
+ export declare function lookupEraseExecutor(subjectKind: DsarSubjectKind): EraseExecutorFn;
7
+ export declare function lookupRectifyHandler(subjectKind: DsarSubjectKind): RectifyHandlerFn;
8
+ /** Test-only: wipe the registry. */
9
+ export declare function _resetRegistryForTests(): void;
10
+ //# sourceMappingURL=registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,cAAc,EAEnB,KAAK,gBAAgB,EAEtB,MAAM,SAAS,CAAC;AAcjB,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,eAAe,EAC5B,EAAE,EAAE,cAAc,GACjB,IAAI,CAKN;AAED,wBAAgB,qBAAqB,CACnC,WAAW,EAAE,eAAe,EAC5B,EAAE,EAAE,eAAe,GAClB,IAAI,CAKN;AAED,wBAAgB,sBAAsB,CACpC,WAAW,EAAE,eAAe,EAC5B,EAAE,EAAE,gBAAgB,GACnB,IAAI,CAKN;AAED,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,eAAe,GAC3B,cAAc,CAMhB;AAED,wBAAgB,mBAAmB,CACjC,WAAW,EAAE,eAAe,GAC3B,eAAe,CAMjB;AAED,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,eAAe,GAC3B,gBAAgB,CAMlB;AAED,oCAAoC;AACpC,wBAAgB,sBAAsB,IAAI,IAAI,CAI7C"}
@@ -0,0 +1,57 @@
1
+ // Per-subjectKind handler registry — process-local.
2
+ //
3
+ // Spec § 5.1. Each Pattern A consumer registers one handler per
4
+ // (verb, subjectKind) tuple at boot. Lookups raise
5
+ // `SubjectKindNotRegistered` when no handler is bound.
6
+ import { HandlerAlreadyRegistered, SubjectKindNotRegistered, } from "./types";
7
+ const registry = {
8
+ exportGather: new Map(),
9
+ eraseExecutor: new Map(),
10
+ rectifyHandler: new Map(),
11
+ };
12
+ export function registerExportGather(subjectKind, fn) {
13
+ if (registry.exportGather.has(subjectKind)) {
14
+ throw new HandlerAlreadyRegistered("export_gather", subjectKind);
15
+ }
16
+ registry.exportGather.set(subjectKind, fn);
17
+ }
18
+ export function registerEraseExecutor(subjectKind, fn) {
19
+ if (registry.eraseExecutor.has(subjectKind)) {
20
+ throw new HandlerAlreadyRegistered("erase_executor", subjectKind);
21
+ }
22
+ registry.eraseExecutor.set(subjectKind, fn);
23
+ }
24
+ export function registerRectifyHandler(subjectKind, fn) {
25
+ if (registry.rectifyHandler.has(subjectKind)) {
26
+ throw new HandlerAlreadyRegistered("rectify_handler", subjectKind);
27
+ }
28
+ registry.rectifyHandler.set(subjectKind, fn);
29
+ }
30
+ export function lookupExportGather(subjectKind) {
31
+ const fn = registry.exportGather.get(subjectKind);
32
+ if (!fn) {
33
+ throw new SubjectKindNotRegistered("export_gather", subjectKind);
34
+ }
35
+ return fn;
36
+ }
37
+ export function lookupEraseExecutor(subjectKind) {
38
+ const fn = registry.eraseExecutor.get(subjectKind);
39
+ if (!fn) {
40
+ throw new SubjectKindNotRegistered("erase_executor", subjectKind);
41
+ }
42
+ return fn;
43
+ }
44
+ export function lookupRectifyHandler(subjectKind) {
45
+ const fn = registry.rectifyHandler.get(subjectKind);
46
+ if (!fn) {
47
+ throw new SubjectKindNotRegistered("rectify_handler", subjectKind);
48
+ }
49
+ return fn;
50
+ }
51
+ /** Test-only: wipe the registry. */
52
+ export function _resetRegistryForTests() {
53
+ registry.exportGather.clear();
54
+ registry.eraseExecutor.clear();
55
+ registry.rectifyHandler.clear();
56
+ }
57
+ //# sourceMappingURL=registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,E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,WAAW,CAAC,CAAC;IAClD,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,MAAM,IAAI,wBAAwB,CAAC,eAAe,EAAE,WAAW,CAAC,CAAC;IACnE,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,MAAM,UAAU,mBAAmB,CACjC,WAA4B;IAE5B,MAAM,EAAE,GAAG,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IACnD,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,MAAM,IAAI,wBAAwB,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAC;IACpE,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,MAAM,UAAU,oBAAoB,CAClC,WAA4B;IAE5B,MAAM,EAAE,GAAG,QAAQ,CAAC,cAAc,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IACpD,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,MAAM,IAAI,wBAAwB,CAAC,iBAAiB,EAAE,WAAW,CAAC,CAAC;IACrE,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,oCAAoC;AACpC,MAAM,UAAU,sBAAsB;IACpC,QAAQ,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;IAC9B,QAAQ,CAAC,aAAa,CAAC,KAAK,EAAE,CAAC;IAC/B,QAAQ,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC;AAClC,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { DsarRow, RetentionPolicy } from "./types";
2
+ export interface RetentionMatch {
3
+ policy: RetentionPolicy;
4
+ retainUntil: string;
5
+ }
6
+ /**
7
+ * Match a DSAR row against a list of retention policies. Returns the most
8
+ * conservative policy that matches the row's `(data_class, subject_kind)`
9
+ * tuple plus the absolute `retainUntil` timestamp computed from the row's
10
+ * `created_at`.
11
+ *
12
+ * `dataClass` is supplied per row because the lib doesn't know the data
13
+ * classes a row touches — callers (the orchestrator) pass it from their
14
+ * `lifecycle_requests` table.
15
+ */
16
+ export declare function applyRetention(policies: RetentionPolicy[], dsarRow: DsarRow, dataClass: string): RetentionMatch | null;
17
+ //# sourceMappingURL=retention.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retention.d.ts","sourceRoot":"","sources":["../src/retention.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAExD,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,eAAe,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,eAAe,EAAE,EAC3B,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,MAAM,GAChB,cAAc,GAAG,IAAI,CAoBvB"}
@@ -0,0 +1,32 @@
1
+ // Retention policy helpers. v0.1.0 ships only the types + a pure
2
+ // `applyRetention` matcher; orchestrator-side `RetentionPolicyService` lands
3
+ // in v0.1.x.
4
+ /**
5
+ * Match a DSAR row against a list of retention policies. Returns the most
6
+ * conservative policy that matches the row's `(data_class, subject_kind)`
7
+ * tuple plus the absolute `retainUntil` timestamp computed from the row's
8
+ * `created_at`.
9
+ *
10
+ * `dataClass` is supplied per row because the lib doesn't know the data
11
+ * classes a row touches — callers (the orchestrator) pass it from their
12
+ * `lifecycle_requests` table.
13
+ */
14
+ export function applyRetention(policies, dsarRow, dataClass) {
15
+ const matches = policies.filter((p) => p.data_class === dataClass && p.subject_kind === dsarRow.subject_kind);
16
+ if (matches.length === 0)
17
+ return null;
18
+ // Most conservative = shortest retention.
19
+ matches.sort((a, b) => a.retention_days - b.retention_days);
20
+ const winner = matches[0];
21
+ if (!winner)
22
+ return null;
23
+ const createdAt = new Date(dsarRow.created_at);
24
+ if (Number.isNaN(createdAt.getTime()))
25
+ return null;
26
+ const retainUntilDate = new Date(createdAt.getTime() + winner.retention_days * 24 * 3600 * 1000);
27
+ return {
28
+ policy: winner,
29
+ retainUntil: retainUntilDate.toISOString(),
30
+ };
31
+ }
32
+ //# sourceMappingURL=retention.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retention.js","sourceRoot":"","sources":["../src/retention.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,6EAA6E;AAC7E,aAAa;AASb;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAC5B,QAA2B,EAC3B,OAAgB,EAChB,SAAiB;IAEjB,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAC7B,CAAC,CAAC,EAAE,EAAE,CACJ,CAAC,CAAC,UAAU,KAAK,SAAS,IAAI,CAAC,CAAC,YAAY,KAAK,OAAO,CAAC,YAAY,CACxE,CAAC;IACF,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACtC,0CAA0C;IAC1C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,cAAc,GAAG,CAAC,CAAC,cAAc,CAAC,CAAC;IAC5D,MAAM,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAC1B,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IAEzB,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAC/C,IAAI,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;QAAE,OAAO,IAAI,CAAC;IACnD,MAAM,eAAe,GAAG,IAAI,IAAI,CAC9B,SAAS,CAAC,OAAO,EAAE,GAAG,MAAM,CAAC,cAAc,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAC/D,CAAC;IACF,OAAO;QACL,MAAM,EAAE,MAAM;QACd,WAAW,EAAE,eAAe,CAAC,WAAW,EAAE;KAC3C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,56 @@
1
+ import { S3Client } from "@aws-sdk/client-s3";
2
+ import type { AuditEmitter, SubjectKindStr } from "./types";
3
+ export interface CreateArtifactOpts {
4
+ s3Client: S3Client;
5
+ tenantId: string;
6
+ subjectId: string;
7
+ subjectKind: SubjectKindStr;
8
+ kind: "access" | "portability";
9
+ payload: Record<string, unknown>;
10
+ region: string;
11
+ /** SSE-KMS key id (production); omit for minio dev/test. */
12
+ kmsKeyId?: string;
13
+ /** Optional bucket-name override (production: `nodii-dsar-artifacts-<region>`). */
14
+ bucketName?: string;
15
+ /** 7-day expiry default per spec § 5.7. */
16
+ expirySeconds?: number;
17
+ /** Service name for audit auto-naming. */
18
+ serviceName?: string;
19
+ /** Audit emitter — captures bucket+key+size+expiresAt, never the URL. */
20
+ auditEmitter?: AuditEmitter;
21
+ }
22
+ export interface CreateArtifactResult {
23
+ bucket: string;
24
+ key: string;
25
+ size: number;
26
+ expiresAt: string;
27
+ presignedUrl: string;
28
+ }
29
+ /**
30
+ * Build the canonical bucket name for a region. Spec § 5.7.
31
+ */
32
+ export declare function bucketNameForRegion(region: string): string;
33
+ /**
34
+ * Ensure the bucket exists. In production, infra/Terraform owns this; in
35
+ * dev/test we create it on-demand against minio.
36
+ */
37
+ export declare function ensureBucket(client: S3Client, bucket: string): Promise<void>;
38
+ /**
39
+ * Create an S3 artifact for DSAR access/portability per spec § 5.7.
40
+ */
41
+ export declare function createArtifact(opts: CreateArtifactOpts): Promise<CreateArtifactResult>;
42
+ /**
43
+ * Compose a default S3Client for minio (dev/test) or AWS (production).
44
+ *
45
+ * Production: pass no opts; SDK reads AWS_REGION + credential chain.
46
+ * Dev/test: pass `endpoint=http://localhost:9001 region=us-east-1
47
+ * accessKeyId=minio secretAccessKey=minio12345 forcePathStyle=true`.
48
+ */
49
+ export declare function makeS3Client(opts?: {
50
+ endpoint?: string;
51
+ region?: string;
52
+ accessKeyId?: string;
53
+ secretAccessKey?: string;
54
+ forcePathStyle?: boolean;
55
+ }): S3Client;
56
+ //# sourceMappingURL=s3-artifact.d.ts.map
@@ -0,0 +1 @@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