@peax/consent-core 0.9.2 → 0.9.3
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/dist/index.js +12 -18
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -145,19 +145,12 @@ function createConsentSubscription() {
|
|
|
145
145
|
}
|
|
146
146
|
//#endregion
|
|
147
147
|
//#region src/consent/evaluateConsent.ts
|
|
148
|
-
/**
|
|
149
|
-
|
|
150
|
-
* Inputs must be validated, with unique policy use IDs and at most one choice
|
|
151
|
-
* per use. Denials remain blocked regardless of their consent version.
|
|
152
|
-
* A version mismatch blocks only this use, not other unchanged uses.
|
|
153
|
-
*/
|
|
154
|
-
function evaluateConsent({ currentAppPolicy, state, useId }) {
|
|
155
|
-
const use = currentAppPolicy.uses.find((use) => use.id === useId);
|
|
148
|
+
/** Decides one use from its declaration and recorded choice, which callers look up their own way. */
|
|
149
|
+
function decideConsent(use, choice) {
|
|
156
150
|
if (!use) return {
|
|
157
151
|
allowed: false,
|
|
158
152
|
reason: "unknown-use"
|
|
159
153
|
};
|
|
160
|
-
const choice = state.choices.find((choice) => choice.useId === useId);
|
|
161
154
|
if (!choice) return {
|
|
162
155
|
allowed: false,
|
|
163
156
|
reason: "undecided"
|
|
@@ -180,12 +173,9 @@ function evaluateConsent({ currentAppPolicy, state, useId }) {
|
|
|
180
173
|
* input contract. An empty result is not blanket permission: denials are omitted.
|
|
181
174
|
*/
|
|
182
175
|
function getMismatchedConsent({ currentAppPolicy, state }) {
|
|
176
|
+
const choices = new Map(state.choices.map((choice) => [choice.useId, choice]));
|
|
183
177
|
return currentAppPolicy.uses.flatMap((use) => {
|
|
184
|
-
const evaluation =
|
|
185
|
-
currentAppPolicy,
|
|
186
|
-
state,
|
|
187
|
-
useId: use.id
|
|
188
|
-
});
|
|
178
|
+
const evaluation = decideConsent(use, choices.get(use.id));
|
|
189
179
|
if (evaluation.allowed) return [];
|
|
190
180
|
if (evaluation.reason !== "undecided" && evaluation.reason !== "consent-version-mismatch") return [];
|
|
191
181
|
return [{
|
|
@@ -357,6 +347,11 @@ function createSnapshot(currentAppPolicy, state) {
|
|
|
357
347
|
state
|
|
358
348
|
});
|
|
359
349
|
}
|
|
350
|
+
function indexSnapshot({ currentAppPolicy, state }) {
|
|
351
|
+
const uses = new Map(currentAppPolicy.uses.map((use) => [use.id, use]));
|
|
352
|
+
const choices = new Map(state.choices.map((choice) => [choice.useId, choice]));
|
|
353
|
+
return (useId) => decideConsent(uses.get(useId), choices.get(useId));
|
|
354
|
+
}
|
|
360
355
|
function snapshotRequirements(snapshot) {
|
|
361
356
|
return Object.freeze(getMismatchedConsent(snapshot).map((requirement) => Object.freeze(requirement)));
|
|
362
357
|
}
|
|
@@ -367,6 +362,7 @@ function createConsent(input) {
|
|
|
367
362
|
if (!result.success) return result;
|
|
368
363
|
let snapshot = createSnapshot(result.data.currentAppPolicy, result.data.initialState);
|
|
369
364
|
let requirements = snapshotRequirements(snapshot);
|
|
365
|
+
let evaluate = indexSnapshot(snapshot);
|
|
370
366
|
const subscriptions = createConsentSubscription();
|
|
371
367
|
let delivering = false;
|
|
372
368
|
let reentrantCommits = 0;
|
|
@@ -380,6 +376,7 @@ function createConsent(input) {
|
|
|
380
376
|
const nextRequirements = snapshotRequirements(next);
|
|
381
377
|
snapshot = next;
|
|
382
378
|
requirements = nextRequirements;
|
|
379
|
+
evaluate = indexSnapshot(next);
|
|
383
380
|
if (delivering) subscriptions.notify();
|
|
384
381
|
else {
|
|
385
382
|
delivering = true;
|
|
@@ -399,10 +396,7 @@ function createConsent(input) {
|
|
|
399
396
|
success: true,
|
|
400
397
|
data: Object.freeze({
|
|
401
398
|
getSnapshot: () => snapshot,
|
|
402
|
-
evaluate: (useId) =>
|
|
403
|
-
...snapshot,
|
|
404
|
-
useId
|
|
405
|
-
}),
|
|
399
|
+
evaluate: (useId) => evaluate(useId),
|
|
406
400
|
getMismatchedConsent: () => requirements,
|
|
407
401
|
restoreState(input) {
|
|
408
402
|
const parsed = validateWithSchema(consentStateSchema, input);
|