dpdp-kit 0.2.0 → 0.4.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/README.md +240 -17
  3. package/dist/access.d.ts +111 -0
  4. package/dist/audit.d.ts +3 -1
  5. package/dist/breach.d.ts +170 -0
  6. package/dist/chunk-2ELTZSW3.js +94 -0
  7. package/dist/chunk-5TVA2TKG.js +37 -0
  8. package/dist/chunk-HXVP446N.js +1051 -0
  9. package/dist/chunk-KLMR7S7I.js +438 -0
  10. package/dist/cli.js +53 -17
  11. package/dist/config.d.ts +69 -0
  12. package/dist/data-access.d.ts +6 -2
  13. package/dist/ddl.d.ts +10 -0
  14. package/dist/dpdp.d.ts +210 -0
  15. package/dist/drizzle.d.ts +403 -11
  16. package/dist/drizzle.js +209 -56
  17. package/dist/errors.d.ts +1 -1
  18. package/dist/express.d.ts +17 -1
  19. package/dist/express.js +10 -2
  20. package/dist/fastify.d.ts +41 -0
  21. package/dist/fastify.js +25 -0
  22. package/dist/guard.d.ts +19 -0
  23. package/dist/hono.d.ts +16 -1
  24. package/dist/hono.js +11 -2
  25. package/dist/index.d.ts +7 -4
  26. package/dist/index.js +420 -100
  27. package/dist/mongodb.d.ts +87 -0
  28. package/dist/mongodb.js +344 -0
  29. package/dist/nestjs.d.ts +65 -0
  30. package/dist/nestjs.js +41 -0
  31. package/dist/postgres.d.ts +45 -0
  32. package/dist/postgres.js +27 -0
  33. package/dist/prisma.d.ts +43 -0
  34. package/dist/prisma.js +281 -0
  35. package/dist/protect.d.ts +74 -0
  36. package/dist/protect.js +114 -0
  37. package/dist/retention.d.ts +1 -1
  38. package/dist/run-audit.d.ts +13 -1
  39. package/dist/safeguards.d.ts +23 -0
  40. package/dist/schedule.d.ts +37 -0
  41. package/dist/schedule.js +66 -0
  42. package/dist/schema-check.d.ts +25 -8
  43. package/dist/store.d.ts +11 -1
  44. package/package.json +58 -5
  45. package/dist/chunk-FGZAH2NF.js +0 -16
  46. package/dist/chunk-KVTUMHXO.js +0 -491
@@ -0,0 +1,170 @@
1
+ import type { DpdpConfig } from "./config.js";
2
+ /** Rule 7(2)(b): the detailed report is due within seventy-two hours of becoming aware. */
3
+ export declare const BOARD_REPORT_WITHIN_HOURS = 72;
4
+ export interface BreachInput {
5
+ /** When you became aware of the breach (ISO 8601). Rule 7's clock starts here. Default: now. */
6
+ awareAt?: string;
7
+ /** When it happened, as far as you know (ISO 8601). */
8
+ occurredAt?: string;
9
+ /** What happened: its nature (Rule 7(1)(a), 7(2)(a)). */
10
+ description: string;
11
+ /** How much data and how many people (Rule 7(1)(a), 7(2)(a)). */
12
+ extent?: string;
13
+ /** Where it happened: system, region or vendor (Rule 7(2)(a)). */
14
+ location?: string;
15
+ /** The likely impact, for the Board (Rule 7(2)(a)). */
16
+ likelyImpact?: string;
17
+ /** The consequences likely for the people affected (Rule 7(1)(b)). */
18
+ consequences?: string;
19
+ /** What you've done and are doing to reduce the risk (Rule 7(1)(c)). */
20
+ measures?: string;
21
+ /** What people can do to protect themselves (Rule 7(1)(d)). */
22
+ safetySteps?: string;
23
+ /** The personal data exposed, as data map keys: `"users.phone"`, or `"users"` for a whole table. */
24
+ fields?: readonly string[];
25
+ }
26
+ /** The detailed report's parts, Rule 7(2)(b)(i)–(v). Part (vi) is built from the notice records. */
27
+ export interface BoardReportInput {
28
+ /** (i) Updated and detailed information about the breach. */
29
+ details?: string;
30
+ /** (ii) The broad facts: events, circumstances and reasons leading to the breach. */
31
+ facts?: string;
32
+ /** (iii) Measures implemented or proposed to mitigate the risk. */
33
+ mitigation?: string;
34
+ /** (iv) Findings about the person who caused the breach. */
35
+ findings?: string;
36
+ /** (v) Remedial measures taken to prevent it happening again. */
37
+ remedial?: string;
38
+ }
39
+ export interface Breach extends Omit<BreachInput, "awareAt" | "fields"> {
40
+ id: string;
41
+ awareAt: string;
42
+ fields: string[];
43
+ /** When the detailed report to the Board is due: 72 hours after `awareAt`, or the extension. */
44
+ reportDueAt: string;
45
+ /** A longer period the Board allowed on a written request (Rule 7(2)(b)). */
46
+ extension?: {
47
+ until: string;
48
+ reference: string;
49
+ recordedAt: string;
50
+ };
51
+ /** When you told the Board, without delay (Rule 7(2)(a)). */
52
+ boardNotifiedAt?: string;
53
+ /** The detailed report's parts so far. */
54
+ report: BoardReportInput;
55
+ /** When you sent the detailed report (Rule 7(2)(b)). */
56
+ reportSentAt?: string;
57
+ status: "open" | "closed";
58
+ closedAt?: string;
59
+ history: Array<{
60
+ at: string;
61
+ event: string;
62
+ note?: string;
63
+ }>;
64
+ }
65
+ /** One affected person, and whether they've been told (Rule 7(1)). */
66
+ export interface BreachNotice {
67
+ breachId: string;
68
+ subjectId: string;
69
+ status: "pending" | "sent";
70
+ addedAt: string;
71
+ sentAt?: string;
72
+ /** How they were told, e.g. `"email"`, `"sms"`, `"in-app"`. */
73
+ channel?: string;
74
+ }
75
+ /** Storage for breach records and notices. Both built-in stores implement it. */
76
+ export interface BreachStore {
77
+ insertBreach(breach: Breach): Promise<void>;
78
+ getBreach(id: string): Promise<Breach | undefined>;
79
+ /**
80
+ * Replace a stored breach, but only if no one changed it since it was read. Each change adds one
81
+ * history entry, so save only when the stored history is one entry shorter than `breach.history`.
82
+ * Returns false when it isn't; dpdp-kit then reads it again and retries.
83
+ */
84
+ updateBreach(breach: Breach): Promise<boolean>;
85
+ /** Breaches, newest first. */
86
+ listBreaches(filter: {
87
+ status?: Breach["status"];
88
+ }): Promise<Breach[]>;
89
+ /** Add affected people; people already on the breach are left as they are. Returns how many were new. */
90
+ addNotices(notices: readonly BreachNotice[]): Promise<number>;
91
+ /** Mark these people as told. Returns how many changed. */
92
+ markNotices(breachId: string, subjectIds: readonly string[], sentAt: string, channel?: string): Promise<number>;
93
+ listNotices(breachId: string, filter: {
94
+ status?: BreachNotice["status"];
95
+ limit?: number;
96
+ }): Promise<BreachNotice[]>;
97
+ /** How many people are on the breach and how many were told, with the first and last time anyone was. */
98
+ countNotices(breachId: string): Promise<NoticeCounts>;
99
+ }
100
+ export interface NoticeCounts {
101
+ total: number;
102
+ sent: number;
103
+ firstSentAt?: string;
104
+ lastSentAt?: string;
105
+ }
106
+ export declare function isBreachStore(store: object): store is BreachStore;
107
+ export interface BreachStatus {
108
+ id: string;
109
+ status: Breach["status"];
110
+ awareAt: string;
111
+ /** Rule 7(2)(a): tell the Board without delay. */
112
+ boardNotified: boolean;
113
+ /** Rule 7(2)(b). */
114
+ report: {
115
+ dueAt: string;
116
+ sent: boolean;
117
+ overdue: boolean;
118
+ hoursLeft: number;
119
+ missing: Array<keyof BoardReportInput>;
120
+ };
121
+ /** Rule 7(1): each affected person, without delay. */
122
+ people: {
123
+ total: number;
124
+ sent: number;
125
+ pending: number;
126
+ };
127
+ /** What still needs doing, in plain words. */
128
+ todo: string[];
129
+ }
130
+ export declare function breachStatus(breach: Breach, counts: NoticeCounts, now: string): BreachStatus;
131
+ /** The notice for an affected person, Rule 7(1)(a)–(e). Translate it for their language before sending. */
132
+ export interface PersonNotice {
133
+ /** (a) What happened, how much, and when. */
134
+ description: string;
135
+ /** (b) The consequences likely for them. */
136
+ consequences: string;
137
+ /** (c) What you're doing about it. */
138
+ measures: string;
139
+ /** (d) What they can do to protect themselves. */
140
+ safetySteps: string;
141
+ /** (e) Who can answer their questions. */
142
+ contact: string;
143
+ /** All of the above as plain text. */
144
+ text: string;
145
+ /** Parts not filled in yet: send only when this is empty. */
146
+ missing: Array<"consequences" | "measures" | "safetySteps" | "contact">;
147
+ }
148
+ export declare function personNotice(config: DpdpConfig<string>, breach: Breach): PersonNotice;
149
+ /** The detailed report for the Board, Rule 7(2)(b)(i)–(vi). */
150
+ export interface BoardReport {
151
+ breachId: string;
152
+ awareAt: string;
153
+ dueAt: string;
154
+ /** (i)–(v), as recorded. */
155
+ details: string;
156
+ facts: string;
157
+ mitigation: string;
158
+ findings: string;
159
+ remedial: string;
160
+ /** (vi) The notices given to affected people. */
161
+ intimations: {
162
+ total: number;
163
+ sent: number;
164
+ pending: number;
165
+ firstSentAt?: string;
166
+ lastSentAt?: string;
167
+ };
168
+ /** Parts (i)–(v) not filled in yet. */
169
+ missing: Array<keyof BoardReportInput>;
170
+ }
@@ -0,0 +1,94 @@
1
+ // src/ddl.ts
2
+ var CONSENT_EVENTS_DDL = `CREATE TABLE IF NOT EXISTS dpdp_consent_events (
3
+ subject_id text NOT NULL,
4
+ seq integer NOT NULL,
5
+ action text NOT NULL CHECK (action IN ('grant', 'withdraw')),
6
+ purposes jsonb NOT NULL,
7
+ notice_version text,
8
+ language text,
9
+ channel text,
10
+ evidence jsonb,
11
+ at timestamptz NOT NULL,
12
+ prev_hash text,
13
+ hash text NOT NULL,
14
+ PRIMARY KEY (subject_id, seq)
15
+ );`;
16
+ var RECORDS_DDL = `CREATE TABLE IF NOT EXISTS dpdp_requests (
17
+ id text PRIMARY KEY,
18
+ subject_id text NOT NULL,
19
+ type text NOT NULL,
20
+ status text NOT NULL,
21
+ opened_at timestamptz NOT NULL,
22
+ due_at timestamptz NOT NULL,
23
+ closed_at timestamptz,
24
+ details jsonb,
25
+ history jsonb NOT NULL
26
+ );
27
+ CREATE INDEX IF NOT EXISTS dpdp_requests_subject_idx ON dpdp_requests (subject_id);
28
+ CREATE INDEX IF NOT EXISTS dpdp_requests_status_due_idx ON dpdp_requests (status, due_at);
29
+ CREATE TABLE IF NOT EXISTS dpdp_erasure_tasks (
30
+ id text PRIMARY KEY,
31
+ subject_id text NOT NULL,
32
+ reason text NOT NULL,
33
+ kind text NOT NULL,
34
+ table_key text,
35
+ fields jsonb,
36
+ processor text,
37
+ purposes jsonb,
38
+ created_at timestamptz NOT NULL,
39
+ due_at timestamptz NOT NULL,
40
+ status text NOT NULL,
41
+ attempts integer NOT NULL,
42
+ claimed_at timestamptz,
43
+ done_at timestamptz,
44
+ affected integer,
45
+ last_error text,
46
+ note text
47
+ );
48
+ CREATE INDEX IF NOT EXISTS dpdp_erasure_tasks_subject_idx ON dpdp_erasure_tasks (subject_id);
49
+ CREATE INDEX IF NOT EXISTS dpdp_erasure_tasks_status_due_idx ON dpdp_erasure_tasks (status, due_at);`;
50
+ var ACCESS_LOG_DDL = `CREATE TABLE IF NOT EXISTS dpdp_access_log (
51
+ id text PRIMARY KEY,
52
+ at timestamptz NOT NULL,
53
+ actor text NOT NULL,
54
+ subject_id text NOT NULL,
55
+ action text NOT NULL,
56
+ purpose text NOT NULL,
57
+ fields jsonb,
58
+ out_of_purpose jsonb,
59
+ reason text,
60
+ evidence jsonb
61
+ );
62
+ CREATE INDEX IF NOT EXISTS dpdp_access_log_at_idx ON dpdp_access_log (at);
63
+ CREATE INDEX IF NOT EXISTS dpdp_access_log_subject_idx ON dpdp_access_log (subject_id, at);
64
+ CREATE INDEX IF NOT EXISTS dpdp_access_log_actor_idx ON dpdp_access_log (actor, at);`;
65
+ var BREACH_DDL = `CREATE TABLE IF NOT EXISTS dpdp_breaches (
66
+ id text PRIMARY KEY,
67
+ status text NOT NULL,
68
+ aware_at timestamptz NOT NULL,
69
+ report_due_at timestamptz NOT NULL,
70
+ data jsonb NOT NULL
71
+ );
72
+ CREATE INDEX IF NOT EXISTS dpdp_breaches_status_idx ON dpdp_breaches (status, aware_at);
73
+ CREATE TABLE IF NOT EXISTS dpdp_breach_notices (
74
+ breach_id text NOT NULL,
75
+ subject_id text NOT NULL,
76
+ status text NOT NULL,
77
+ added_at timestamptz NOT NULL,
78
+ sent_at timestamptz,
79
+ channel text,
80
+ PRIMARY KEY (breach_id, subject_id)
81
+ );
82
+ CREATE INDEX IF NOT EXISTS dpdp_breach_notices_status_idx ON dpdp_breach_notices (breach_id, status, added_at);`;
83
+ var DPDP_DDL = `${CONSENT_EVENTS_DDL}
84
+ ${RECORDS_DDL}
85
+ ${ACCESS_LOG_DDL}
86
+ ${BREACH_DDL}`;
87
+
88
+ export {
89
+ CONSENT_EVENTS_DDL,
90
+ RECORDS_DDL,
91
+ ACCESS_LOG_DDL,
92
+ BREACH_DDL,
93
+ DPDP_DDL
94
+ };
@@ -0,0 +1,37 @@
1
+ import {
2
+ DpdpError
3
+ } from "./chunk-67Q5GFD6.js";
4
+
5
+ // src/guard.ts
6
+ var CONSENT_REQUIRED_BODY = (purpose) => ({
7
+ error: "consent_required",
8
+ purpose,
9
+ message: "You haven't agreed to this yet. You can turn it on in your privacy settings."
10
+ });
11
+ async function checkConsent(dpdp, purpose, options, ctx) {
12
+ const subjectId = await options.subject(ctx);
13
+ if (!subjectId) return false;
14
+ return dpdp.consent.has(subjectId, purpose);
15
+ }
16
+ async function recordAccess(dpdp, options, ctx) {
17
+ const subject = await options.subject(ctx);
18
+ const subjectIds = typeof subject === "string" ? [subject] : subject ? [...subject] : [];
19
+ if (subjectIds.length === 0) return;
20
+ const actor = await options.actor(ctx);
21
+ if (!actor) throw new DpdpError("invalid-actor", "logAccess: actor() returned nothing; every access to personal data needs a known actor");
22
+ const evidence = options.evidence?.(ctx) ?? void 0;
23
+ await dpdp.access.log({
24
+ actor,
25
+ subjectIds,
26
+ purpose: options.purpose,
27
+ ...options.fields ? { fields: options.fields } : {},
28
+ ...options.action ? { action: options.action } : {},
29
+ ...evidence ? { evidence } : {}
30
+ });
31
+ }
32
+
33
+ export {
34
+ CONSENT_REQUIRED_BODY,
35
+ checkConsent,
36
+ recordAccess
37
+ };