@vllnt/convex-consent 0.1.0-canary.591da41

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 (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +143 -0
  3. package/dist/client/index.d.ts +156 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +142 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/types.d.ts +79 -0
  8. package/dist/client/types.d.ts.map +1 -0
  9. package/dist/client/types.js +3 -0
  10. package/dist/client/types.js.map +1 -0
  11. package/dist/component/_generated/api.d.ts +40 -0
  12. package/dist/component/_generated/api.d.ts.map +1 -0
  13. package/dist/component/_generated/api.js +31 -0
  14. package/dist/component/_generated/api.js.map +1 -0
  15. package/dist/component/_generated/component.d.ts +102 -0
  16. package/dist/component/_generated/component.d.ts.map +1 -0
  17. package/dist/component/_generated/component.js +11 -0
  18. package/dist/component/_generated/component.js.map +1 -0
  19. package/dist/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/component/_generated/dataModel.js +11 -0
  22. package/dist/component/_generated/dataModel.js.map +1 -0
  23. package/dist/component/_generated/server.d.ts +133 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +80 -0
  26. package/dist/component/_generated/server.js.map +1 -0
  27. package/dist/component/convex.config.d.ts +3 -0
  28. package/dist/component/convex.config.d.ts.map +1 -0
  29. package/dist/component/convex.config.js +7 -0
  30. package/dist/component/convex.config.js.map +1 -0
  31. package/dist/component/crons.d.ts +16 -0
  32. package/dist/component/crons.d.ts.map +1 -0
  33. package/dist/component/crons.js +19 -0
  34. package/dist/component/crons.js.map +1 -0
  35. package/dist/component/mutations.d.ts +60 -0
  36. package/dist/component/mutations.d.ts.map +1 -0
  37. package/dist/component/mutations.js +149 -0
  38. package/dist/component/mutations.js.map +1 -0
  39. package/dist/component/queries.d.ts +78 -0
  40. package/dist/component/queries.d.ts.map +1 -0
  41. package/dist/component/queries.js +119 -0
  42. package/dist/component/queries.js.map +1 -0
  43. package/dist/component/schema.d.ts +53 -0
  44. package/dist/component/schema.d.ts.map +1 -0
  45. package/dist/component/schema.js +40 -0
  46. package/dist/component/schema.js.map +1 -0
  47. package/dist/component/validators.d.ts +80 -0
  48. package/dist/component/validators.d.ts.map +1 -0
  49. package/dist/component/validators.js +62 -0
  50. package/dist/component/validators.js.map +1 -0
  51. package/dist/shared.d.ts +31 -0
  52. package/dist/shared.d.ts.map +1 -0
  53. package/dist/shared.js +29 -0
  54. package/dist/shared.js.map +1 -0
  55. package/package.json +101 -0
  56. package/src/client/index.ts +298 -0
  57. package/src/client/types.ts +85 -0
  58. package/src/component/_generated/api.ts +56 -0
  59. package/src/component/_generated/component.ts +127 -0
  60. package/src/component/_generated/dataModel.ts +60 -0
  61. package/src/component/_generated/server.ts +169 -0
  62. package/src/component/convex.config.ts +9 -0
  63. package/src/component/crons.ts +23 -0
  64. package/src/component/mutations.ts +158 -0
  65. package/src/component/queries.ts +142 -0
  66. package/src/component/schema.ts +41 -0
  67. package/src/component/validators.ts +70 -0
  68. package/src/shared.ts +36 -0
  69. package/src/test.ts +15 -0
@@ -0,0 +1,41 @@
1
+ import { defineSchema, defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ import { consentDecision, jsonValue } from "./validators";
4
+
5
+ /**
6
+ * Two sandboxed tables — the consent ledger's own concern.
7
+ *
8
+ * `consentEvents` is the **append-only** legal record (GDPR Art. 7(1) proof): a
9
+ * row is inserted on every grant/deny/withdraw and is NEVER mutated, only ever
10
+ * deleted by the retention prune. `subjectRef` and `purpose` are opaque host
11
+ * strings; `proof` is opaque host evidence (policy hash, IP, UA). The
12
+ * `by_subject_purpose_at` index reads one pair's history newest-first; the
13
+ * `by_at` index drives the oldest-first retention sweep.
14
+ *
15
+ * `consentState` is the derived **current-decision projection** — exactly one
16
+ * row per `(subjectRef, purpose)`, overwritten on each new event. It is the O(1)
17
+ * runtime gate the host queries before processing; `by_subject_purpose` resolves
18
+ * a single pair, `by_subject` lists every purpose for one subject.
19
+ */
20
+ export default defineSchema({
21
+ consentEvents: defineTable({
22
+ subjectRef: v.string(),
23
+ purpose: v.string(),
24
+ decision: consentDecision,
25
+ version: v.string(),
26
+ proof: v.optional(jsonValue),
27
+ at: v.number(),
28
+ })
29
+ .index("by_subject_purpose_at", ["subjectRef", "purpose", "at"])
30
+ .index("by_at", ["at"]),
31
+
32
+ consentState: defineTable({
33
+ subjectRef: v.string(),
34
+ purpose: v.string(),
35
+ decision: consentDecision,
36
+ version: v.string(),
37
+ at: v.number(),
38
+ })
39
+ .index("by_subject_purpose", ["subjectRef", "purpose"])
40
+ .index("by_subject", ["subjectRef"]),
41
+ });
@@ -0,0 +1,70 @@
1
+ import { v } from "convex/values";
2
+
3
+ /**
4
+ * Opaque host-owned proof recorded alongside a consent decision — the evidence
5
+ * that the subject made the choice (a policy hash, an IP, a user agent, a signed
6
+ * token). The component never inspects it; it is last-resort arbitrary data,
7
+ * aliased here rather than left bare in function signatures. The host narrows it
8
+ * at the {@link Consent} client boundary via an optional `proofValidator` parser.
9
+ *
10
+ * This is the single documented `v.any()` escape hatch in the component; the lint
11
+ * rule `convex-rules/no-bare-v-any` is satisfied by routing every arbitrary host
12
+ * proof through this alias instead of a bare `v.any()`.
13
+ */
14
+ export const jsonValue = v.any();
15
+
16
+ /**
17
+ * The three consent decisions a subject can hold for a purpose. `granted` and
18
+ * `denied` are explicit answers to the consent prompt; `withdrawn` revokes a
19
+ * prior grant. Every decision is appended to the immutable ledger — the GDPR
20
+ * proof — and projected onto the current-state row.
21
+ */
22
+ export const consentDecision = v.union(
23
+ v.literal("granted"),
24
+ v.literal("denied"),
25
+ v.literal("withdrawn"),
26
+ );
27
+
28
+ /**
29
+ * One immutable row in the consent ledger. `subjectRef` and `purpose` are opaque
30
+ * host strings; `decision` is the recorded answer; `version` is the policy
31
+ * version the subject consented to; `proof` is the opaque host evidence; `at` is
32
+ * the server timestamp. The ledger is append-only — a row is never mutated.
33
+ */
34
+ export const consentEventView = v.object({
35
+ subjectRef: v.string(),
36
+ purpose: v.string(),
37
+ decision: consentDecision,
38
+ version: v.string(),
39
+ proof: v.optional(jsonValue),
40
+ at: v.number(),
41
+ });
42
+
43
+ /**
44
+ * The current decision for one `(subjectRef, purpose)` pair — the O(1) runtime
45
+ * gate, projected from the latest ledger event. `null` is returned by
46
+ * {@link check}/{@link getState} when no event was ever recorded.
47
+ */
48
+ export const consentStateView = v.object({
49
+ subjectRef: v.string(),
50
+ purpose: v.string(),
51
+ decision: consentDecision,
52
+ version: v.string(),
53
+ at: v.number(),
54
+ });
55
+
56
+ /**
57
+ * The answer to "may I process `<purpose>` for `<subject>`?". `granted` is the
58
+ * gate: `true` only when the latest decision is `granted` AND its `version`
59
+ * matches the host-supplied `requiredVersion` (or no version was required).
60
+ * `stale` is `true` when the subject granted an older policy version than the one
61
+ * now required — the host re-prompts. `decision`/`version`/`at` mirror the
62
+ * current state (all `null` when no event was ever recorded).
63
+ */
64
+ export const consentCheckView = v.object({
65
+ granted: v.boolean(),
66
+ stale: v.boolean(),
67
+ decision: v.union(consentDecision, v.null()),
68
+ version: v.union(v.string(), v.null()),
69
+ at: v.union(v.number(), v.null()),
70
+ });
package/src/shared.ts ADDED
@@ -0,0 +1,36 @@
1
+ /** Shared constants used by both `client/` and `component/`. */
2
+
3
+ export const COMPONENT_NAME = "consent";
4
+
5
+ /**
6
+ * The consent decisions a subject can hold for a purpose. `granted` and `denied`
7
+ * are explicit answers to the consent prompt; `withdrawn` revokes a prior grant.
8
+ * Every decision is appended to the immutable ledger and projected onto the
9
+ * current-state row.
10
+ */
11
+ export const CONSENT_DECISIONS = ["granted", "denied", "withdrawn"] as const;
12
+
13
+ /** A single consent decision. */
14
+ export type ConsentDecision = (typeof CONSENT_DECISIONS)[number];
15
+
16
+ /**
17
+ * The default policy version stamped on a decision when the host supplies none.
18
+ * Consent is always versioned so a later policy change can invalidate a stale
19
+ * grant; `"1"` is the sensible zero-config default for a host that hasn't started
20
+ * versioning its policy yet.
21
+ */
22
+ export const DEFAULT_VERSION = "1";
23
+
24
+ /**
25
+ * Default retention (ms) for ledger events before the prune cron sweeps them:
26
+ * 365 days. Consent proof must survive long enough to answer an audit or a data
27
+ * subject request, so the default window is deliberately long. The current-state
28
+ * projection is NEVER pruned — only superseded historical ledger events past the
29
+ * cutoff are removed. A host with a stricter or looser legal window drives
30
+ * `prune` from its own scheduler with an explicit `before` cutoff.
31
+ */
32
+ export const DEFAULT_RETENTION_MS = 31_536_000_000;
33
+
34
+ /** Default page size for a `prune` pass before the sweep self-reschedules. */
35
+ export const DEFAULT_PRUNE_BATCH = 200;
36
+ export const MAX_PRUNE_BATCH = 500;
package/src/test.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { TestConvex } from "convex-test";
2
+ import schema from "./component/schema";
3
+
4
+ const modules = import.meta.glob("./component/**/*.ts");
5
+
6
+ /**
7
+ * Register this component with a `convex-test` instance so consuming apps can
8
+ * test integration: `import { register } from "@vllnt/convex-consent/test"`.
9
+ */
10
+ export function register(
11
+ t: TestConvex<typeof schema>,
12
+ name = "consent",
13
+ ): void {
14
+ t.registerComponent(name, schema, modules);
15
+ }