@cloakapi/sdk 1.5.0 → 1.6.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 (38) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +24 -4
  3. package/dist/browser/index.d.mts +99 -89
  4. package/dist/browser/index.d.ts +99 -89
  5. package/dist/browser/index.js +1245 -123
  6. package/dist/browser/index.mjs +24 -8
  7. package/dist/browser/light.d.mts +3 -2
  8. package/dist/browser/light.d.ts +3 -2
  9. package/dist/browser/light.js +1 -1
  10. package/dist/browser/light.mjs +17 -2
  11. package/dist/browser/names/public.mjs +2 -2
  12. package/dist/browser/ner-pipeline-worker.mjs +3 -3
  13. package/dist/{chunk-6TMC2A6M.mjs → chunk-3MXGO6NA.mjs} +12 -8
  14. package/dist/{chunk-G6TZ6722.mjs → chunk-6E6T2YO5.mjs} +5 -915
  15. package/dist/{chunk-EY35G37A.mjs → chunk-746IWCJ4.mjs} +224 -1
  16. package/dist/chunk-L7RLVZMD.mjs +922 -0
  17. package/dist/{chunk-JV52VGVZ.mjs → chunk-TRWGRB2P.mjs} +1 -1
  18. package/dist/chunk-TWAQCP3Z.mjs +1258 -0
  19. package/dist/{chunk-36BC4GYL.mjs → chunk-VYGV643D.mjs} +262 -122
  20. package/dist/device/index.d.mts +1448 -0
  21. package/dist/device/index.d.ts +1448 -0
  22. package/dist/device/index.js +4539 -0
  23. package/dist/device/index.mjs +2462 -0
  24. package/dist/{disclosure-Cx6xS8Tj.d.mts → disclosure-BhM2Aqa2.d.mts} +2 -12
  25. package/dist/{disclosure-Cx6xS8Tj.d.ts → disclosure-BhM2Aqa2.d.ts} +2 -12
  26. package/dist/index.d.mts +3 -2
  27. package/dist/index.d.ts +3 -2
  28. package/dist/index.js +1 -1
  29. package/dist/index.mjs +14 -12
  30. package/dist/{local-pii-supplement-SUOJZO7X.mjs → local-pii-supplement-5KFFPDDN.mjs} +4 -4
  31. package/dist/node.d.mts +2 -1
  32. package/dist/node.d.ts +2 -1
  33. package/dist/node.js +311 -58
  34. package/dist/node.mjs +18 -12
  35. package/dist/request-body-slots-BMShK1YO.d.mts +56 -0
  36. package/dist/request-body-slots-BMShK1YO.d.ts +56 -0
  37. package/package.json +9 -2
  38. package/dist/{chunk-PEDVEDAW.mjs → chunk-66SEKCCN.mjs} +8 -8
@@ -1,3 +1,209 @@
1
+ import {
2
+ indexOfWholeToken,
3
+ isWholeTokenOccurrence
4
+ } from "./chunk-QIORVVUD.mjs";
5
+ import {
6
+ finalCardBackstop
7
+ } from "./chunk-TWAQCP3Z.mjs";
8
+
9
+ // src/browser/privacy-mode-apply.ts
10
+ var SEALED_SECRET = /^\[CLK-(?:CARD|GOVID|IBAN|SECRET|PASSPORT)-[a-p]+\]$/;
11
+ function isSealedSecretMarker(value) {
12
+ return typeof value === "string" && SEALED_SECRET.test(value);
13
+ }
14
+ function hasOwn(obj, key) {
15
+ return Object.prototype.hasOwnProperty.call(obj, key);
16
+ }
17
+ function escapeRe(s) {
18
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
19
+ }
20
+ function tagOf(type) {
21
+ const t = String(type ?? "").toUpperCase().replace(/[^A-Z_]/g, "");
22
+ return t || "PII";
23
+ }
24
+ function createModeLedger() {
25
+ return { perOriginal: /* @__PURE__ */ new Map(), counters: /* @__PURE__ */ new Map() };
26
+ }
27
+ var defaultLedger = createModeLedger();
28
+ function resetDefaultModeLedger() {
29
+ defaultLedger.perOriginal.clear();
30
+ defaultLedger.counters.clear();
31
+ }
32
+ var conversationLedgers = /* @__PURE__ */ new WeakMap();
33
+ function privacyLedgerFor(conversation) {
34
+ let ledger = conversationLedgers.get(conversation);
35
+ if (!ledger) {
36
+ ledger = createModeLedger();
37
+ conversationLedgers.set(conversation, ledger);
38
+ }
39
+ return ledger;
40
+ }
41
+ function copyPrivacyLedger(from, to) {
42
+ const ledger = conversationLedgers.get(from);
43
+ if (!ledger) return;
44
+ conversationLedgers.set(to, {
45
+ perOriginal: new Map(ledger.perOriginal),
46
+ counters: new Map(ledger.counters)
47
+ });
48
+ }
49
+ function repairIdentityStandIns(review, ledger) {
50
+ if (!review || typeof review.body !== "string" || !review.map) return review;
51
+ const spans = review.spans ?? [];
52
+ const repairs = /* @__PURE__ */ new Map();
53
+ const addRepair = (original, type, surrogate, invalid) => {
54
+ const existing = repairs.get(original) ?? { type, needsOpaque: false };
55
+ existing.type ?? (existing.type = type);
56
+ existing.needsOpaque || (existing.needsOpaque = invalid);
57
+ if (!existing.surrogate && typeof surrogate === "string" && surrogate.length > 0 && surrogate !== original) {
58
+ existing.surrogate = surrogate;
59
+ }
60
+ repairs.set(original, existing);
61
+ };
62
+ for (const span of spans) {
63
+ if (!span || typeof span.original !== "string" || !span.original || !/\S/.test(span.original)) continue;
64
+ if (isSealedSecretMarker(span.surrogate)) continue;
65
+ const hasSurrogate = typeof span.surrogate === "string" && span.surrogate.length > 0;
66
+ const invalid = !hasSurrogate || span.surrogate === span.original;
67
+ if (invalid || review.body.includes(span.original)) {
68
+ addRepair(span.original, span.type, span.surrogate, invalid);
69
+ }
70
+ }
71
+ for (const [surrogate, original] of Object.entries(review.map)) {
72
+ if (isSealedSecretMarker(surrogate)) continue;
73
+ if (surrogate && typeof original === "string" && original.length > 0 && /\S/.test(original)) {
74
+ const invalid = surrogate === original;
75
+ if (invalid || review.body.includes(original)) addRepair(original, void 0, surrogate, invalid);
76
+ }
77
+ }
78
+ if (repairs.size === 0) return review;
79
+ const used = /* @__PURE__ */ new Set([
80
+ ...Object.keys(review.map),
81
+ ...spans.map((span) => span?.surrogate).filter((value) => typeof value === "string")
82
+ ]);
83
+ const opaqueMarker = (type, original) => {
84
+ const tag = tagOf(type);
85
+ const available = (candidate) => typeof candidate === "string" && candidate.length > 0 && candidate !== original && !used.has(candidate) && !review.body.includes(candidate) && (review.map[candidate] === void 0 || review.map[candidate] === original);
86
+ const bound = ledger.perOriginal.get(original);
87
+ if (available(bound)) {
88
+ used.add(bound);
89
+ return bound;
90
+ }
91
+ let n = ledger.counters.get(tag) ?? 0;
92
+ let marker;
93
+ do {
94
+ n += 1;
95
+ marker = `<<CLOAK:${tag}:${n}>>`;
96
+ } while (!available(marker));
97
+ ledger.counters.set(tag, n);
98
+ ledger.perOriginal.set(original, marker);
99
+ used.add(marker);
100
+ return marker;
101
+ };
102
+ const replacementByOriginal = /* @__PURE__ */ new Map();
103
+ for (const [original, repair] of repairs) {
104
+ let candidate = repair.needsOpaque ? void 0 : repair.surrogate;
105
+ if (typeof candidate !== "string" || candidate.length === 0 || candidate === original || review.map[candidate] !== void 0 && review.map[candidate] !== original) {
106
+ candidate = opaqueMarker(repair.type, original);
107
+ }
108
+ replacementByOriginal.set(original, candidate);
109
+ }
110
+ const nextSpans = spans.map((span) => {
111
+ if (!span || typeof span.original !== "string") return span;
112
+ const replacement = replacementByOriginal.get(span.original);
113
+ return replacement ? { ...span, surrogate: replacement } : span;
114
+ });
115
+ const needles = /* @__PURE__ */ new Map();
116
+ for (const [original, replacement] of replacementByOriginal) needles.set(original, { replacement, original: true });
117
+ const addStandIn = (needle, original) => {
118
+ const replacement = typeof original === "string" ? replacementByOriginal.get(original) : void 0;
119
+ if (replacement === void 0 || typeof needle !== "string" || needle.length === 0 || needle === replacement || needles.has(needle)) return;
120
+ needles.set(needle, { replacement, original: false });
121
+ };
122
+ for (const span of spans) addStandIn(span?.surrogate, span?.original);
123
+ for (const [surrogate, original] of Object.entries(review.map)) addStandIn(surrogate, original);
124
+ const source = review.body;
125
+ const ordered = [...needles.keys()].sort((a, b) => b.length - a.length);
126
+ const hasWholeToken = new Map(ordered.map((needle) => [needle, indexOfWholeToken(source, needle) >= 0]));
127
+ const body = source.replace(new RegExp(ordered.map(escapeRe).join("|"), "g"), (found, offset) => {
128
+ const needle = needles.get(found);
129
+ const whole = isWholeTokenOccurrence(source, offset, offset + found.length);
130
+ return whole || !needle.original && !hasWholeToken.get(found) ? needle.replacement : found;
131
+ });
132
+ const map = { ...review.map };
133
+ for (const [original, replacement] of replacementByOriginal) {
134
+ for (const [surrogate, mappedOriginal] of Object.entries(map)) {
135
+ if (mappedOriginal === original) delete map[surrogate];
136
+ }
137
+ map[replacement] = original;
138
+ }
139
+ return { ...review, body, map, ...review.spans ? { spans: nextSpans } : {} };
140
+ }
141
+ function applyPrivacyModeInner(review, mode, ledger) {
142
+ review = repairIdentityStandIns(review, ledger);
143
+ if (mode !== "max") return review;
144
+ if (!review || typeof review.body !== "string" || !review.map) return review;
145
+ const typeOfSurrogate = /* @__PURE__ */ new Map();
146
+ for (const s of review.spans ?? []) {
147
+ if (s && typeof s.surrogate === "string") typeOfSurrogate.set(s.surrogate, tagOf(s.type));
148
+ }
149
+ const surrogates = Object.keys(review.map).filter((s) => s.length > 0).sort((a, b) => b.length - a.length);
150
+ if (surrogates.length === 0) return review;
151
+ const rewrite = /* @__PURE__ */ new Map();
152
+ const nextMap = {};
153
+ for (const sur of surrogates) {
154
+ const original = review.map[sur];
155
+ if (isSealedSecretMarker(sur)) {
156
+ nextMap[sur] = original;
157
+ continue;
158
+ }
159
+ let placeholder = ledger.perOriginal.get(original);
160
+ const tag = typeOfSurrogate.get(sur) ?? "PII";
161
+ let n = ledger.counters.get(tag) ?? 0;
162
+ if (placeholder === void 0) {
163
+ n += 1;
164
+ placeholder = `<<CLOAK:${tag}:${n}>>`;
165
+ }
166
+ while (review.body.includes(placeholder) || hasOwn(review.map, placeholder) || hasOwn(nextMap, placeholder) && nextMap[placeholder] !== original || isSealedSecretMarker(placeholder)) {
167
+ placeholder = `<<CLOAK:${tag}:${++n}>>`;
168
+ }
169
+ ledger.counters.set(tag, n);
170
+ ledger.perOriginal.set(original, placeholder);
171
+ rewrite.set(sur, placeholder);
172
+ nextMap[placeholder] = original;
173
+ }
174
+ if (rewrite.size === 0) return review;
175
+ const rewriteKeys = [...rewrite.keys()];
176
+ const ordered = [...rewriteKeys, ...surrogates.filter((s) => isSealedSecretMarker(s))].sort(
177
+ (a, b) => b.length - a.length
178
+ );
179
+ const hasWholeToken = new Map(
180
+ rewriteKeys.map((s) => [s, indexOfWholeToken(review.body, s) >= 0])
181
+ );
182
+ const source = review.body;
183
+ const re = new RegExp(ordered.map(escapeRe).join("|"), "g");
184
+ const body = source.replace(re, (m, off) => {
185
+ if (isSealedSecretMarker(m) || !rewrite.has(m)) return m;
186
+ return hasWholeToken.get(m) && !isWholeTokenOccurrence(source, off, off + m.length) ? m : rewrite.get(m);
187
+ });
188
+ const spans = (review.spans ?? []).map(
189
+ (s) => s && typeof s.surrogate === "string" && rewrite.has(s.surrogate) ? { ...s, surrogate: rewrite.get(s.surrogate) } : s
190
+ );
191
+ return { ...review, body, map: nextMap, ...review.spans ? { spans } : {} };
192
+ }
193
+ function lockUnspannedCards(review) {
194
+ if (!review || typeof review.body !== "string" || !review.map) return review;
195
+ const cardLock = finalCardBackstop(review.body, Object.keys(review.map));
196
+ if (cardLock.locked === 0) return review;
197
+ return { ...review, body: cardLock.body, map: { ...review.map, ...cardLock.map } };
198
+ }
199
+ function applyPrivacyModeRewrite(review, mode, ledger = defaultLedger) {
200
+ return applyPrivacyModeInner(review, mode, ledger);
201
+ }
202
+ function applyPrivacyMode(review, mode, ledger = defaultLedger) {
203
+ return lockUnspannedCards(applyPrivacyModeRewrite(review, mode, ledger));
204
+ }
205
+ var applyPrivacyModeToReview = applyPrivacyMode;
206
+
1
207
  // src/browser/verify/strict-json.js
2
208
  var MAX_JSON_CODE_UNITS = 8 * 1024 * 1024;
3
209
  var MAX_JSON_DEPTH = 256;
@@ -791,7 +997,16 @@ async function fetchReceiptByJti(jti) {
791
997
  }).catch(() => {
792
998
  throw new Error("receipt lookup failed");
793
999
  });
794
- if (!res.ok) throw new Error("No receipt found for that ID (HTTP " + res.status + ").");
1000
+ if (!res.ok) {
1001
+ const data2 = await res.json().catch(() => null);
1002
+ const code = typeof data2?.error?.code === "string" ? data2.error.code : "";
1003
+ const message = typeof data2?.error?.message === "string" ? data2.error.message : "";
1004
+ if (code || message) {
1005
+ const status = code ? res.status + ", " + code : String(res.status);
1006
+ throw new Error("Receipt lookup failed (HTTP " + status + ")." + (message ? " " + message : ""));
1007
+ }
1008
+ throw new Error("No receipt found for that ID (HTTP " + res.status + ").");
1009
+ }
795
1010
  const data = parseStrictJson(await res.text());
796
1011
  return data && data.receipt ? data.receipt : data;
797
1012
  }
@@ -1205,6 +1420,14 @@ function holdAbortUntilBound(id) {
1205
1420
  }
1206
1421
 
1207
1422
  export {
1423
+ isSealedSecretMarker,
1424
+ createModeLedger,
1425
+ resetDefaultModeLedger,
1426
+ privacyLedgerFor,
1427
+ copyPrivacyLedger,
1428
+ applyPrivacyModeRewrite,
1429
+ applyPrivacyMode,
1430
+ applyPrivacyModeToReview,
1208
1431
  canonicalise,
1209
1432
  receiptHash,
1210
1433
  isCountersignedResult,