dpdp-kit 0.1.1 → 0.2.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 +138 -11
- package/dist/audit.d.ts +2 -2
- package/dist/chunk-KVTUMHXO.js +491 -0
- package/dist/cli.js +61 -9
- package/dist/config.d.ts +15 -0
- package/dist/data-access.d.ts +19 -0
- package/dist/dpdp.d.ts +60 -3
- package/dist/drizzle.d.ts +505 -4
- package/dist/drizzle.js +268 -3
- package/dist/erasure.d.ts +2 -1
- package/dist/errors.d.ts +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.js +375 -24
- package/dist/personal.d.ts +17 -0
- package/dist/records.d.ts +124 -0
- package/dist/retention.d.ts +47 -0
- package/dist/rights.d.ts +49 -0
- package/dist/run-audit.d.ts +12 -0
- package/dist/schema-check.d.ts +22 -0
- package/dist/store.d.ts +9 -2
- package/package.json +1 -1
- package/dist/chunk-HRKHRKV4.js +0 -185
package/dist/drizzle.js
CHANGED
|
@@ -3,8 +3,8 @@ import {
|
|
|
3
3
|
} from "./chunk-67Q5GFD6.js";
|
|
4
4
|
|
|
5
5
|
// src/drizzle.ts
|
|
6
|
-
import { asc, desc, eq } from "drizzle-orm";
|
|
7
|
-
import { integer, jsonb, pgTable, primaryKey, text, timestamp } from "drizzle-orm/pg-core";
|
|
6
|
+
import { and, asc, desc, eq, getTableColumns, getTableName, is, lt, lte, or, sql, Table } from "drizzle-orm";
|
|
7
|
+
import { index, integer, jsonb, pgTable, primaryKey, text, timestamp } from "drizzle-orm/pg-core";
|
|
8
8
|
var consentEvents = pgTable(
|
|
9
9
|
"dpdp_consent_events",
|
|
10
10
|
{
|
|
@@ -36,6 +36,81 @@ var CONSENT_EVENTS_DDL = `CREATE TABLE IF NOT EXISTS dpdp_consent_events (
|
|
|
36
36
|
hash text NOT NULL,
|
|
37
37
|
PRIMARY KEY (subject_id, seq)
|
|
38
38
|
);`;
|
|
39
|
+
var ts = (name) => timestamp(name, { withTimezone: true, mode: "date" });
|
|
40
|
+
var rightsRequests = pgTable(
|
|
41
|
+
"dpdp_requests",
|
|
42
|
+
{
|
|
43
|
+
id: text("id").primaryKey(),
|
|
44
|
+
subjectId: text("subject_id").notNull(),
|
|
45
|
+
type: text("type").$type().notNull(),
|
|
46
|
+
status: text("status").$type().notNull(),
|
|
47
|
+
openedAt: ts("opened_at").notNull(),
|
|
48
|
+
dueAt: ts("due_at").notNull(),
|
|
49
|
+
closedAt: ts("closed_at"),
|
|
50
|
+
details: jsonb("details").$type(),
|
|
51
|
+
history: jsonb("history").$type().notNull()
|
|
52
|
+
},
|
|
53
|
+
(t) => [index("dpdp_requests_subject_idx").on(t.subjectId), index("dpdp_requests_status_due_idx").on(t.status, t.dueAt)]
|
|
54
|
+
);
|
|
55
|
+
var erasureTasks = pgTable(
|
|
56
|
+
"dpdp_erasure_tasks",
|
|
57
|
+
{
|
|
58
|
+
id: text("id").primaryKey(),
|
|
59
|
+
subjectId: text("subject_id").notNull(),
|
|
60
|
+
reason: text("reason").notNull(),
|
|
61
|
+
kind: text("kind").$type().notNull(),
|
|
62
|
+
tableKey: text("table_key"),
|
|
63
|
+
fields: jsonb("fields").$type(),
|
|
64
|
+
processor: text("processor"),
|
|
65
|
+
purposes: jsonb("purposes").$type(),
|
|
66
|
+
createdAt: ts("created_at").notNull(),
|
|
67
|
+
dueAt: ts("due_at").notNull(),
|
|
68
|
+
status: text("status").$type().notNull(),
|
|
69
|
+
attempts: integer("attempts").notNull(),
|
|
70
|
+
claimedAt: ts("claimed_at"),
|
|
71
|
+
doneAt: ts("done_at"),
|
|
72
|
+
affected: integer("affected"),
|
|
73
|
+
lastError: text("last_error"),
|
|
74
|
+
note: text("note")
|
|
75
|
+
},
|
|
76
|
+
(t) => [index("dpdp_erasure_tasks_subject_idx").on(t.subjectId), index("dpdp_erasure_tasks_status_due_idx").on(t.status, t.dueAt)]
|
|
77
|
+
);
|
|
78
|
+
var RECORDS_DDL = `CREATE TABLE IF NOT EXISTS dpdp_requests (
|
|
79
|
+
id text PRIMARY KEY,
|
|
80
|
+
subject_id text NOT NULL,
|
|
81
|
+
type text NOT NULL,
|
|
82
|
+
status text NOT NULL,
|
|
83
|
+
opened_at timestamptz NOT NULL,
|
|
84
|
+
due_at timestamptz NOT NULL,
|
|
85
|
+
closed_at timestamptz,
|
|
86
|
+
details jsonb,
|
|
87
|
+
history jsonb NOT NULL
|
|
88
|
+
);
|
|
89
|
+
CREATE INDEX IF NOT EXISTS dpdp_requests_subject_idx ON dpdp_requests (subject_id);
|
|
90
|
+
CREATE INDEX IF NOT EXISTS dpdp_requests_status_due_idx ON dpdp_requests (status, due_at);
|
|
91
|
+
CREATE TABLE IF NOT EXISTS dpdp_erasure_tasks (
|
|
92
|
+
id text PRIMARY KEY,
|
|
93
|
+
subject_id text NOT NULL,
|
|
94
|
+
reason text NOT NULL,
|
|
95
|
+
kind text NOT NULL,
|
|
96
|
+
table_key text,
|
|
97
|
+
fields jsonb,
|
|
98
|
+
processor text,
|
|
99
|
+
purposes jsonb,
|
|
100
|
+
created_at timestamptz NOT NULL,
|
|
101
|
+
due_at timestamptz NOT NULL,
|
|
102
|
+
status text NOT NULL,
|
|
103
|
+
attempts integer NOT NULL,
|
|
104
|
+
claimed_at timestamptz,
|
|
105
|
+
done_at timestamptz,
|
|
106
|
+
affected integer,
|
|
107
|
+
last_error text,
|
|
108
|
+
note text
|
|
109
|
+
);
|
|
110
|
+
CREATE INDEX IF NOT EXISTS dpdp_erasure_tasks_subject_idx ON dpdp_erasure_tasks (subject_id);
|
|
111
|
+
CREATE INDEX IF NOT EXISTS dpdp_erasure_tasks_status_due_idx ON dpdp_erasure_tasks (status, due_at);`;
|
|
112
|
+
var DPDP_DDL = `${CONSENT_EVENTS_DDL}
|
|
113
|
+
${RECORDS_DDL}`;
|
|
39
114
|
function fromRow(row) {
|
|
40
115
|
return {
|
|
41
116
|
subjectId: row.subjectId,
|
|
@@ -58,6 +133,89 @@ function isUniqueViolation(err) {
|
|
|
58
133
|
}
|
|
59
134
|
return false;
|
|
60
135
|
}
|
|
136
|
+
var iso = (d) => d ? d.toISOString() : void 0;
|
|
137
|
+
var date = (value) => value === void 0 ? null : new Date(value);
|
|
138
|
+
function requestFromRow(row) {
|
|
139
|
+
return {
|
|
140
|
+
id: row.id,
|
|
141
|
+
subjectId: row.subjectId,
|
|
142
|
+
type: row.type,
|
|
143
|
+
status: row.status,
|
|
144
|
+
openedAt: row.openedAt.toISOString(),
|
|
145
|
+
dueAt: row.dueAt.toISOString(),
|
|
146
|
+
...row.closedAt ? { closedAt: row.closedAt.toISOString() } : {},
|
|
147
|
+
...row.details !== null ? { details: row.details } : {},
|
|
148
|
+
history: row.history
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function requestToRow(r) {
|
|
152
|
+
return {
|
|
153
|
+
id: r.id,
|
|
154
|
+
subjectId: r.subjectId,
|
|
155
|
+
type: r.type,
|
|
156
|
+
status: r.status,
|
|
157
|
+
openedAt: new Date(r.openedAt),
|
|
158
|
+
dueAt: new Date(r.dueAt),
|
|
159
|
+
closedAt: date(r.closedAt),
|
|
160
|
+
details: r.details ?? null,
|
|
161
|
+
history: r.history
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function taskFromRow(row) {
|
|
165
|
+
const optional = {
|
|
166
|
+
table: row.tableKey ?? void 0,
|
|
167
|
+
fields: row.fields ?? void 0,
|
|
168
|
+
processor: row.processor ?? void 0,
|
|
169
|
+
purposes: row.purposes ?? void 0,
|
|
170
|
+
claimedAt: iso(row.claimedAt),
|
|
171
|
+
doneAt: iso(row.doneAt),
|
|
172
|
+
affected: row.affected ?? void 0,
|
|
173
|
+
lastError: row.lastError ?? void 0,
|
|
174
|
+
note: row.note ?? void 0
|
|
175
|
+
};
|
|
176
|
+
return {
|
|
177
|
+
id: row.id,
|
|
178
|
+
subjectId: row.subjectId,
|
|
179
|
+
reason: row.reason,
|
|
180
|
+
kind: row.kind,
|
|
181
|
+
createdAt: row.createdAt.toISOString(),
|
|
182
|
+
dueAt: row.dueAt.toISOString(),
|
|
183
|
+
status: row.status,
|
|
184
|
+
attempts: row.attempts,
|
|
185
|
+
...Object.fromEntries(Object.entries(optional).filter(([, v]) => v !== void 0))
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function taskToRow(t) {
|
|
189
|
+
return {
|
|
190
|
+
id: t.id,
|
|
191
|
+
subjectId: t.subjectId,
|
|
192
|
+
reason: t.reason,
|
|
193
|
+
kind: t.kind,
|
|
194
|
+
tableKey: t.table ?? null,
|
|
195
|
+
fields: t.fields ?? null,
|
|
196
|
+
processor: t.processor ?? null,
|
|
197
|
+
purposes: t.purposes ?? null,
|
|
198
|
+
createdAt: new Date(t.createdAt),
|
|
199
|
+
dueAt: new Date(t.dueAt),
|
|
200
|
+
status: t.status,
|
|
201
|
+
attempts: t.attempts,
|
|
202
|
+
claimedAt: date(t.claimedAt),
|
|
203
|
+
doneAt: date(t.doneAt),
|
|
204
|
+
affected: t.affected ?? null,
|
|
205
|
+
lastError: t.lastError ?? null,
|
|
206
|
+
note: t.note ?? null
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
function runnable(when) {
|
|
210
|
+
return and(
|
|
211
|
+
lte(erasureTasks.dueAt, new Date(when.now)),
|
|
212
|
+
or(
|
|
213
|
+
eq(erasureTasks.status, "pending"),
|
|
214
|
+
and(eq(erasureTasks.status, "failed"), lt(erasureTasks.attempts, when.maxAttempts)),
|
|
215
|
+
and(eq(erasureTasks.status, "running"), lt(erasureTasks.claimedAt, new Date(when.staleBefore)))
|
|
216
|
+
)
|
|
217
|
+
);
|
|
218
|
+
}
|
|
61
219
|
function drizzleStore(db) {
|
|
62
220
|
return {
|
|
63
221
|
async lastEvent(subjectId) {
|
|
@@ -87,11 +245,118 @@ function drizzleStore(db) {
|
|
|
87
245
|
async events(subjectId) {
|
|
88
246
|
const rows = await db.select().from(consentEvents).where(eq(consentEvents.subjectId, subjectId)).orderBy(asc(consentEvents.seq));
|
|
89
247
|
return rows.map(fromRow);
|
|
248
|
+
},
|
|
249
|
+
async insertRequest(request) {
|
|
250
|
+
await db.insert(rightsRequests).values(requestToRow(request));
|
|
251
|
+
},
|
|
252
|
+
async getRequest(id) {
|
|
253
|
+
const rows = await db.select().from(rightsRequests).where(eq(rightsRequests.id, id)).limit(1);
|
|
254
|
+
return rows[0] ? requestFromRow(rows[0]) : void 0;
|
|
255
|
+
},
|
|
256
|
+
async updateRequest(request) {
|
|
257
|
+
const { id, ...rest } = requestToRow(request);
|
|
258
|
+
await db.update(rightsRequests).set(rest).where(eq(rightsRequests.id, id));
|
|
259
|
+
},
|
|
260
|
+
async listRequests(filter) {
|
|
261
|
+
const where = and(
|
|
262
|
+
filter.subjectId ? eq(rightsRequests.subjectId, filter.subjectId) : void 0,
|
|
263
|
+
filter.status ? eq(rightsRequests.status, filter.status) : void 0
|
|
264
|
+
);
|
|
265
|
+
const rows = await db.select().from(rightsRequests).where(where).orderBy(asc(rightsRequests.openedAt));
|
|
266
|
+
return rows.map(requestFromRow);
|
|
267
|
+
},
|
|
268
|
+
async insertTasks(tasks) {
|
|
269
|
+
if (tasks.length > 0) await db.insert(erasureTasks).values(tasks.map(taskToRow));
|
|
270
|
+
},
|
|
271
|
+
async dueTasks({ limit, ...when }) {
|
|
272
|
+
const rows = await db.select().from(erasureTasks).where(runnable(when)).orderBy(asc(erasureTasks.dueAt)).limit(limit);
|
|
273
|
+
return rows.map(taskFromRow);
|
|
274
|
+
},
|
|
275
|
+
async claimTask({ id, ...when }) {
|
|
276
|
+
const claimed = await db.update(erasureTasks).set({ status: "running", claimedAt: new Date(when.now) }).where(and(eq(erasureTasks.id, id), runnable(when))).returning({ id: erasureTasks.id });
|
|
277
|
+
return claimed.length === 1;
|
|
278
|
+
},
|
|
279
|
+
async updateTask(task) {
|
|
280
|
+
const { id, ...rest } = taskToRow(task);
|
|
281
|
+
await db.update(erasureTasks).set(rest).where(eq(erasureTasks.id, id));
|
|
282
|
+
},
|
|
283
|
+
async getTask(id) {
|
|
284
|
+
const rows = await db.select().from(erasureTasks).where(eq(erasureTasks.id, id)).limit(1);
|
|
285
|
+
return rows[0] ? taskFromRow(rows[0]) : void 0;
|
|
286
|
+
},
|
|
287
|
+
async listTasks(filter) {
|
|
288
|
+
const where = and(
|
|
289
|
+
filter.subjectId ? eq(erasureTasks.subjectId, filter.subjectId) : void 0,
|
|
290
|
+
filter.status ? eq(erasureTasks.status, filter.status) : void 0
|
|
291
|
+
);
|
|
292
|
+
const rows = await db.select().from(erasureTasks).where(where).orderBy(asc(erasureTasks.dueAt));
|
|
293
|
+
return rows.map(taskFromRow);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
function drizzleSchema(module) {
|
|
298
|
+
const tables = /* @__PURE__ */ new Map();
|
|
299
|
+
for (const value of Object.values(module)) {
|
|
300
|
+
if (!is(value, Table)) continue;
|
|
301
|
+
const name = getTableName(value);
|
|
302
|
+
if (tables.has(name)) continue;
|
|
303
|
+
const columns = Object.entries(getTableColumns(value)).map(
|
|
304
|
+
([key, column]) => key === column.name ? { name: column.name } : { name: column.name, key }
|
|
305
|
+
);
|
|
306
|
+
tables.set(name, { name, columns });
|
|
307
|
+
}
|
|
308
|
+
return { tables: [...tables.values()] };
|
|
309
|
+
}
|
|
310
|
+
function rowsOf(result) {
|
|
311
|
+
if (Array.isArray(result)) return result;
|
|
312
|
+
return result.rows ?? [];
|
|
313
|
+
}
|
|
314
|
+
function drizzleData(db, options = {}) {
|
|
315
|
+
const columnsByTable = /* @__PURE__ */ new Map();
|
|
316
|
+
for (const table of options.schema ? drizzleSchema(options.schema).tables : []) {
|
|
317
|
+
columnsByTable.set(table.name, new Map(table.columns.map((c) => [c.key ?? c.name, c.name])));
|
|
318
|
+
}
|
|
319
|
+
const col = (table, name) => sql.identifier(columnsByTable.get(table)?.get(name) ?? name);
|
|
320
|
+
const where = (table, subjectColumn, subjectId) => sql`${col(table, subjectColumn)} = ${subjectId}`;
|
|
321
|
+
return {
|
|
322
|
+
async read(table, subjectColumn, subjectId, columns) {
|
|
323
|
+
if (columns.length === 0) return [];
|
|
324
|
+
const select = sql.join(
|
|
325
|
+
columns.map((c) => sql`${col(table, c)} AS ${sql.identifier(c)}`),
|
|
326
|
+
sql`, `
|
|
327
|
+
);
|
|
328
|
+
const result = await db.execute(
|
|
329
|
+
sql`SELECT ${select} FROM ${sql.identifier(table)} WHERE ${where(table, subjectColumn, subjectId)}`
|
|
330
|
+
);
|
|
331
|
+
return rowsOf(result);
|
|
332
|
+
},
|
|
333
|
+
async eraseFields(table, subjectColumn, subjectId, columns) {
|
|
334
|
+
if (columns.length === 0) return 0;
|
|
335
|
+
const set = sql.join(
|
|
336
|
+
columns.map((c) => sql`${col(table, c)} = NULL`),
|
|
337
|
+
sql`, `
|
|
338
|
+
);
|
|
339
|
+
const result = await db.execute(
|
|
340
|
+
sql`UPDATE ${sql.identifier(table)} SET ${set} WHERE ${where(table, subjectColumn, subjectId)} RETURNING 1`
|
|
341
|
+
);
|
|
342
|
+
return rowsOf(result).length;
|
|
343
|
+
},
|
|
344
|
+
async deleteRows(table, subjectColumn, subjectId) {
|
|
345
|
+
const result = await db.execute(
|
|
346
|
+
sql`DELETE FROM ${sql.identifier(table)} WHERE ${where(table, subjectColumn, subjectId)} RETURNING 1`
|
|
347
|
+
);
|
|
348
|
+
return rowsOf(result).length;
|
|
90
349
|
}
|
|
91
350
|
};
|
|
92
351
|
}
|
|
93
352
|
export {
|
|
94
353
|
CONSENT_EVENTS_DDL,
|
|
354
|
+
DPDP_DDL,
|
|
355
|
+
RECORDS_DDL,
|
|
95
356
|
consentEvents,
|
|
96
|
-
|
|
357
|
+
drizzleData,
|
|
358
|
+
drizzleSchema,
|
|
359
|
+
drizzleStore,
|
|
360
|
+
erasureTasks,
|
|
361
|
+
rightsRequests
|
|
97
362
|
};
|
package/dist/erasure.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { DpdpConfig, LegalHold } from "./config.js";
|
|
2
2
|
import { type Duration } from "./duration.js";
|
|
3
|
-
|
|
3
|
+
/** Why erasure is happening. An `"erasure-request"` (Section 12(3)) is handled like an account deletion, minus any purposes still needed. */
|
|
4
|
+
export type ErasureReason = "withdrawal" | "account-deletion" | "erasure-request";
|
|
4
5
|
export interface FieldPlan {
|
|
5
6
|
field: string;
|
|
6
7
|
/**
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Issue } from "./audit.js";
|
|
2
|
-
export type DpdpErrorCode = "invalid-config" | "invalid-subject" | "unknown-purpose" | "not-a-consent-purpose" | "no-purposes" | "invalid-language" | "write-conflict";
|
|
2
|
+
export type DpdpErrorCode = "invalid-config" | "invalid-subject" | "unknown-purpose" | "not-a-consent-purpose" | "no-purposes" | "invalid-language" | "write-conflict" | "store-unsupported" | "no-data-access" | "invalid-request-type" | "request-not-found" | "request-closed" | "task-not-found" | "task-not-manual";
|
|
3
3
|
/** Thrown for mistakes in how the library is called. Messages are for developers, not end users. */
|
|
4
4
|
export declare class DpdpError extends Error {
|
|
5
5
|
readonly code: DpdpErrorCode;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
export { audit, hasErrors, type Issue, type IssueLevel } from "./audit.js";
|
|
2
|
-
export { type Basis, type Category, type DpdpConfig, defineConfig, type FiduciaryDef, type LegalHold, type PersonalField, type ProcessorDef, type PurposeDef, type PurposeOf, purposeNames, type Retention, type TableDef, } from "./config.js";
|
|
2
|
+
export { type Basis, type Category, type DpdpConfig, defineConfig, type FiduciaryDef, type LegalHold, type PersonalField, type ProcessorDef, type PurposeDef, type PurposeOf, type RightsConfig, purposeNames, type Retention, type TableDef, } from "./config.js";
|
|
3
3
|
export { type ConsentMeta, createDpdp, type Dpdp, type DpdpOptions, hashEvent, type VerifyResult, } from "./dpdp.js";
|
|
4
4
|
export { type Duration, durationMs, isDuration } from "./duration.js";
|
|
5
5
|
export { type ErasurePlan, type ErasureReason, type FieldPlan, type PlanInput, type ProcessorPlan, planErasure, type TablePlan, } from "./erasure.js";
|
|
6
6
|
export { DpdpError, type DpdpErrorCode, SeqConflictError } from "./errors.js";
|
|
7
7
|
export { CONSENT_REQUIRED_BODY, type GuardOptions } from "./guard.js";
|
|
8
8
|
export { isNoticeLanguage, NOTICE_LANGUAGES, type NoticeLanguage } from "./languages.js";
|
|
9
|
-
export { type
|
|
9
|
+
export { type Confidence, guessPersonal, type PersonalGuess, tokenize } from "./personal.js";
|
|
10
|
+
export { checkSchema, type Schema, type SchemaColumn, type SchemaTable } from "./schema-check.js";
|
|
11
|
+
export { type ConsentAction, type ConsentEvent, type ConsentStore, type DpdpStore, isDpdpStore, memoryStore, } from "./store.js";
|
|
12
|
+
export { type DataAccess, memoryData } from "./data-access.js";
|
|
13
|
+
export { type ErasureTask, REQUEST_TYPES, type RequestFilter, type RequestHistoryEntry, type RequestStatus, type RequestStore, type RequestType, type RightsRequest, type TaskFilter, type TaskKind, type TaskStatus, type TaskStore, } from "./records.js";
|
|
14
|
+
export { type ProcessorHookInput, type ProcessorHooks, type RunReport, tasksFromPlan, } from "./retention.js";
|
|
15
|
+
export { type AccessSummary, DEFAULT_RESPONSE_WITHIN } from "./rights.js";
|