@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
@@ -0,0 +1,1258 @@
1
+ // src/device/final-card-backstop.ts
2
+ var MAX_GAP = 6;
3
+ var SALT_LETTERS = 12;
4
+ var MIN_LEN = 13;
5
+ var MAX_LEN = 19;
6
+ var DOUBLED = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
7
+ var DECIMAL = /\p{Nd}/u;
8
+ var OTHER_NUMBER = /\p{No}/u;
9
+ var SEPARATOR = /[\s\-./\u0085\u00AD\u00B7\u200B-\u200F\u2010-\u2015\u2027\u202A-\u202E\u2060\u2066-\u2069\u2212\u2219\uFEFF]/u;
10
+ var SHORT_FORM_SEPARATORS = /[,|_;:+*'\uFF0D\uFF0C\uFE50\uFE10\u3001\uFE51\uFE11\uFF64\u060C\u066C\uFF0F\u3002\uFF1B\uFF1A\uFF0B\uFF0A\uFF07\uFF3F\uFF5C\uFE63\u2044\u2215\u2022\u30FB]/u;
11
+ var SHORT_FORM_PUNCTUATION = "[,|_;:+*'\\uFF0D\\uFF0C\\uFE50\\uFE10\\u3001\\uFE51\\uFE11\\uFF64\\u060C\\u066C\\uFF0F\\u3002\\uFF1B\\uFF1A\\uFF0B\\uFF0A\\uFF07\\uFF3F\\uFF5C\\uFE63\\u2044\\u2215\\u2022\\u30FB]";
12
+ var SHORT_FORM_HORIZONTAL = "[ \\t\\u00A0\\u1680\\u2000-\\u200A\\u202F\\u205F\\u3000\\-./\\u00AD\\u00B7\\u200B-\\u200F\\u2010-\\u2015\\u2027\\u202A-\\u202E\\u2060\\u2066-\\u2069\\u2212\\u2219\\uFEFF]";
13
+ var SHORT_FORM_HORIZONTAL_CHAR = new RegExp(`^${SHORT_FORM_HORIZONTAL}$`, "u");
14
+ var SHORT_FORM_LINE_BREAK = "(?:\\r\\n|[\\n\\r\\u000B\\u000C\\u0085\\u2028\\u2029])";
15
+ var SHORT_FORM_GAP = `((?:${SHORT_FORM_PUNCTUATION}|${SHORT_FORM_HORIZONTAL}|${SHORT_FORM_LINE_BREAK}){1,9})`;
16
+ var SHORT_FORMS = [
17
+ // No trailing digit guard: `4111,1111,1111,11119` is still the 4-4-4-4 card. The 4-6-5 guard stays (AA-29).
18
+ new RegExp(`[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}`, "gu"),
19
+ new RegExp(`[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{6}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{5}(?![\\p{Nd}\\p{No}])`, "gu"),
20
+ new RegExp(`[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{3}(?![\\p{Nd}\\p{No}])`, "gu"),
21
+ new RegExp(`[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{3}(?![\\p{Nd}\\p{No}])`, "gu")
22
+ ];
23
+ function isShortFormGap(gap) {
24
+ let punctuation = 0;
25
+ let horizontal = 0;
26
+ let lineBreaks = 0;
27
+ for (let i = 0; i < gap.length; i++) {
28
+ if (gap.startsWith("\r\n", i)) {
29
+ horizontal++;
30
+ lineBreaks++;
31
+ i++;
32
+ continue;
33
+ }
34
+ const char = gap[i];
35
+ if (SHORT_FORM_SEPARATORS.test(char)) punctuation++;
36
+ else if (SHORT_FORM_HORIZONTAL_CHAR.test(char)) horizontal++;
37
+ else if (/[\n\r\u000B\u000C\u0085\u2028\u2029]/u.test(char)) {
38
+ horizontal++;
39
+ lineBreaks++;
40
+ } else return false;
41
+ }
42
+ return punctuation <= 3 && horizontal <= 6 && lineBreaks <= 2 && !lineBreakPipeIsTableJoin(gap, lineBreaks) && !(lineBreaks > 1 && !gap.includes(","));
43
+ }
44
+ function lineBreakPipeIsTableJoin(gap, lineBreaks) {
45
+ if (lineBreaks === 0 || !gap.includes("|")) return false;
46
+ for (let i = 0; i < gap.length; i++) {
47
+ if (gap[i] !== "|") continue;
48
+ const prev = i > 0 ? gap[i - 1] : "";
49
+ const next = i + 1 < gap.length ? gap[i + 1] : "";
50
+ if (prev === " " || prev === " " || prev === "\xA0" || next === " " || next === " " || next === "\xA0") return true;
51
+ }
52
+ return false;
53
+ }
54
+ var digitCache = /* @__PURE__ */ new Map();
55
+ function digitValue(cp) {
56
+ if (cp >= 48 && cp <= 57) return cp - 48;
57
+ if (cp < 128) return -1;
58
+ const cached = digitCache.get(cp);
59
+ if (cached !== void 0) return cached;
60
+ let value = -1;
61
+ if (DECIMAL.test(String.fromCodePoint(cp))) {
62
+ let start = cp;
63
+ while (start > 0 && DECIMAL.test(String.fromCodePoint(start - 1))) start--;
64
+ value = (cp - start) % 10;
65
+ } else if (OTHER_NUMBER.test(String.fromCodePoint(cp))) {
66
+ const normalized = String.fromCodePoint(cp).normalize("NFKC");
67
+ if ([...normalized].length === 1 && DECIMAL.test(normalized)) value = digitValue(normalized.codePointAt(0));
68
+ }
69
+ digitCache.set(cp, value);
70
+ return value;
71
+ }
72
+ function shortFormSeparatorOffsets(body) {
73
+ const offsets = /* @__PURE__ */ new Set();
74
+ addExactGroupedCardOffsets(body, offsets);
75
+ if (!SHORT_FORM_SEPARATORS.test(body)) return offsets;
76
+ for (const pattern of SHORT_FORMS) {
77
+ pattern.lastIndex = 0;
78
+ let match;
79
+ while (match = pattern.exec(body)) {
80
+ pattern.lastIndex = match.index + 1;
81
+ const gaps = match.slice(1);
82
+ if (gaps.some((gap) => !isShortFormGap(gap))) continue;
83
+ let cursor = match.index;
84
+ for (let group = 0; group < gaps.length; group++) {
85
+ cursor += group === 0 ? 4 : pattern === SHORT_FORMS[1] && group === 1 ? 6 : 4;
86
+ for (let i = 0; i < gaps[group].length; i++) {
87
+ offsets.add(cursor + i);
88
+ }
89
+ cursor += gaps[group].length;
90
+ }
91
+ }
92
+ }
93
+ return offsets;
94
+ }
95
+ function addExactGroupedCardOffsets(body, offsets) {
96
+ const n = body.length;
97
+ let i = 0;
98
+ while (i < n) {
99
+ const cp0 = body.codePointAt(i);
100
+ const width0 = cp0 > 65535 ? 2 : 1;
101
+ if (digitValue(cp0) < 0) {
102
+ i += width0;
103
+ continue;
104
+ }
105
+ const groups = [];
106
+ let p = i;
107
+ while (p < n) {
108
+ const from = p;
109
+ let digits2 = "";
110
+ while (p < n) {
111
+ const cp = body.codePointAt(p);
112
+ const width = cp > 65535 ? 2 : 1;
113
+ const value = digitValue(cp);
114
+ if (value < 0) break;
115
+ digits2 += String(value);
116
+ p += width;
117
+ }
118
+ if (digits2.length === 0) break;
119
+ groups.push({ from, to: p, digits: digits2 });
120
+ let q = p;
121
+ while (q < n && q - p < 9 && digitValue(body.codePointAt(q)) < 0) {
122
+ const cp = body.codePointAt(q);
123
+ q += cp > 65535 ? 2 : 1;
124
+ }
125
+ const gap = body.slice(p, q);
126
+ if (gap.length < 1 || gap.length > 9 || !isShortFormGap(gap)) break;
127
+ if (q >= n || digitValue(body.codePointAt(q)) < 0) break;
128
+ p = q;
129
+ }
130
+ const lengths = groups.map((group) => group.digits.length);
131
+ const digits = groups.map((group) => group.digits).join("");
132
+ if (groups.length >= 2 && EXACT_SHORT_JSON_SHAPES.some((shape) => sameShape(lengths, shape)) && digitsPassCard(digits)) {
133
+ const from = groups[0].from;
134
+ const to = groups[groups.length - 1].to;
135
+ for (let k = from; k < to; k++) {
136
+ if (digitValue(body.codePointAt(k)) < 0) offsets.add(k);
137
+ }
138
+ }
139
+ const last = groups.length > 0 ? groups[groups.length - 1].to : i + width0;
140
+ i = last > i ? last : i + width0;
141
+ }
142
+ }
143
+ function isSeparator(cp, offset, shortFormOffsets) {
144
+ if (cp === 32 || cp === 45 || cp === 46 || cp === 47 || cp === 10 || cp === 13 || cp === 9 || cp === 11 || cp === 12) return true;
145
+ if (shortFormOffsets.has(offset)) return true;
146
+ if (cp < 128) return false;
147
+ return SEPARATOR.test(String.fromCodePoint(cp));
148
+ }
149
+ function plausible(d, s, len) {
150
+ const p1 = d[s];
151
+ const p2 = p1 * 10 + d[s + 1];
152
+ const p3 = p2 * 10 + d[s + 2];
153
+ const p4 = p3 * 10 + d[s + 3];
154
+ const p6 = len >= 6 ? p4 * 100 + d[s + 4] * 10 + d[s + 5] : 0;
155
+ if (p1 === 4) return len === 13 || len === 16 || len === 19;
156
+ if (len === 16 && (p2 >= 51 && p2 <= 55 || p4 >= 2221 && p4 <= 2720)) return true;
157
+ if (len === 15 && (p2 === 34 || p2 === 37)) return true;
158
+ if (len >= 16 && (p4 === 6011 || p2 === 65 || p3 >= 644 && p3 <= 649)) return true;
159
+ if (len >= 16 && p2 === 62) return true;
160
+ if (len >= 16 && p6 >= 622126 && p6 <= 622925) return true;
161
+ if (len >= 14 && (p3 >= 300 && p3 <= 305 || p4 === 3095 || p2 === 36 || p2 === 38 || p2 === 39)) return true;
162
+ if (len >= 16 && p4 >= 3528 && p4 <= 3589) return true;
163
+ if (len >= 16 && p4 >= 2200 && p4 <= 2204) return true;
164
+ if (len === 16 && (p3 === 508 || p2 === 81 || p2 === 82 || p4 === 9792 || p4 === 5019)) return true;
165
+ if (len >= 13 && (p4 === 5018 || p4 === 5020 || p4 === 5038 || p4 === 5893 || p4 === 6304 || p4 === 6759 || p4 >= 6761 && p4 <= 6763)) return true;
166
+ return false;
167
+ }
168
+ var LETTERS = "abcdefghijklmnop";
169
+ function randomSalt() {
170
+ const bytes = new Uint8Array(SALT_LETTERS);
171
+ const c = globalThis.crypto;
172
+ if (c?.getRandomValues) c.getRandomValues(bytes);
173
+ else for (let i = 0; i < bytes.length; i++) bytes[i] = Math.floor(Math.random() * 256);
174
+ let salt = "";
175
+ for (let i = 0; i < SALT_LETTERS; i++) salt += LETTERS[bytes[i] & 15];
176
+ return salt;
177
+ }
178
+ function freshSalt(body, reserved) {
179
+ const keys = reserved ? [...reserved] : [];
180
+ for (let attempt = 0; ; attempt++) {
181
+ const salt = randomSalt();
182
+ const prefix = `[CLK-CARD-${salt}`;
183
+ if (attempt < 16 && (body.includes(prefix) || keys.some((k) => k.startsWith(prefix)))) continue;
184
+ return salt;
185
+ }
186
+ }
187
+ var PAIR = Array.from({ length: 256 }, (_, b) => LETTERS[b >> 4] + LETTERS[b & 15]);
188
+ function markerAt(salt, index) {
189
+ return `[CLK-CARD-${salt}${PAIR[index >>> 16 & 255]}${PAIR[index >>> 8 & 255]}${PAIR[index & 255]}]`;
190
+ }
191
+ function markerContext(body, reserved) {
192
+ return { salt: freshSalt(body, reserved), index: 0 };
193
+ }
194
+ function finalCardBackstop(body, reserved) {
195
+ const context = markerContext(body, reserved);
196
+ const encoded = /%[0-9a-fA-F]{2}/u.test(body) ? lockPercentEncodedDigits(body, reserved, context) : lockJsonBoundaryCardRuns(body, reserved, context);
197
+ const plain = finalCardBackstopWithContext(encoded.body, reserved, context);
198
+ if (encoded.locked === 0) return plain;
199
+ return { body: plain.body, locked: encoded.locked + plain.locked, map: { ...encoded.map, ...plain.map } };
200
+ }
201
+ function finalCardBackstopToFixedPoint(body, reserved, maxPasses = 5, firstPass = finalCardBackstop) {
202
+ const reservedKeys = [...reserved ?? []];
203
+ const passLimit = Math.max(1, Math.min(5, Math.floor(maxPasses)));
204
+ let current = firstPass(body, reservedKeys);
205
+ let total = current.locked;
206
+ const map = { ...current.map };
207
+ for (let pass = 1; current.locked > 0 && pass < passLimit; pass++) {
208
+ current = finalCardBackstopWithContext(current.body, [...reservedKeys, ...Object.keys(map)]);
209
+ Object.assign(map, current.map);
210
+ total += current.locked;
211
+ }
212
+ if (current.locked > 0) {
213
+ const verification = finalCardBackstopWithContext(current.body, [...reservedKeys, ...Object.keys(map)]);
214
+ if (verification.locked > 0) throw new Error("Card backstop did not reach a fixed point.");
215
+ }
216
+ return { body: current.body, locked: total, map };
217
+ }
218
+ function hasUnprotectedCardJson(serialized) {
219
+ return finalCardBackstopJson(serialized).locked > 0;
220
+ }
221
+ function finalCardBackstopJsonToFixedPoint(serialized, reserved, maxPasses = 5, firstPass = finalCardBackstopJson) {
222
+ const reservedKeys = [...reserved ?? []];
223
+ const passLimit = Math.max(1, Math.min(5, Math.floor(maxPasses)));
224
+ let current = firstPass(serialized, reservedKeys);
225
+ let total = current.locked;
226
+ const map = { ...current.map };
227
+ for (let pass = 1; current.locked > 0 && pass < passLimit; pass++) {
228
+ current = firstPass(current.body, [...reservedKeys, ...Object.keys(map)]);
229
+ Object.assign(map, current.map);
230
+ total += current.locked;
231
+ }
232
+ if (current.locked > 0 && firstPass(current.body, [...reservedKeys, ...Object.keys(map)]).locked > 0) {
233
+ throw new Error("Card backstop did not reach a fixed point.");
234
+ }
235
+ return { body: current.body, locked: total, map };
236
+ }
237
+ function finalCardBackstopWithContext(body, reserved, sharedContext, spanOffsets) {
238
+ const shortFormOffsets = shortFormSeparatorOffsets(body);
239
+ let cap = 1024;
240
+ let D = new Uint8Array(cap);
241
+ let S = new Int32Array(cap);
242
+ let E = new Int32Array(cap);
243
+ let n = 0;
244
+ const runs = [];
245
+ let runFrom = 0;
246
+ let gap = 0;
247
+ let shortGap = false;
248
+ const endRun = () => {
249
+ if (n - runFrom >= MIN_LEN) runs.push(runFrom, n);
250
+ runFrom = n;
251
+ gap = 0;
252
+ shortGap = false;
253
+ };
254
+ for (let i = 0; i < body.length; ) {
255
+ const cp = body.codePointAt(i);
256
+ const width = cp > 65535 ? 2 : 1;
257
+ const v = digitValue(cp);
258
+ if (v >= 0) {
259
+ if (n === cap) {
260
+ cap *= 2;
261
+ const D2 = new Uint8Array(cap);
262
+ D2.set(D);
263
+ D = D2;
264
+ const S2 = new Int32Array(cap);
265
+ S2.set(S);
266
+ S = S2;
267
+ const E2 = new Int32Array(cap);
268
+ E2.set(E);
269
+ E = E2;
270
+ }
271
+ D[n] = v;
272
+ S[n] = i;
273
+ E[n] = i + width;
274
+ n++;
275
+ gap = 0;
276
+ shortGap = false;
277
+ } else if (n > runFrom && isSeparator(cp, i, shortFormOffsets)) {
278
+ shortGap || (shortGap = shortFormOffsets.has(i));
279
+ if (gap < (shortGap ? 9 : MAX_GAP)) gap++;
280
+ else endRun();
281
+ } else if (n > runFrom) {
282
+ endRun();
283
+ }
284
+ i += width;
285
+ }
286
+ endRun();
287
+ if (runs.length === 0) return { body, locked: 0, map: {} };
288
+ const P0 = new Int32Array(n + 1);
289
+ const P1 = new Int32Array(n + 1);
290
+ for (let t = 0; t < n; t++) {
291
+ P0[t + 1] = P0[t] + (t % 2 === 0 ? DOUBLED[D[t]] : D[t]);
292
+ P1[t + 1] = P1[t] + (t % 2 === 1 ? DOUBLED[D[t]] : D[t]);
293
+ }
294
+ const valid = (start, end) => {
295
+ const P = end % 2 === 0 ? P0 : P1;
296
+ return (P[end] - P[start]) % 10 === 0 && plausible(D, start, end - start);
297
+ };
298
+ const covStart = new Int32Array(n + 1);
299
+ const covEnd = new Int32Array(n + 1);
300
+ const spans = [];
301
+ for (let r = 0; r < runs.length; r += 2) {
302
+ const from = runs[r];
303
+ const to = runs[r + 1];
304
+ let any = false;
305
+ for (let len = MIN_LEN; len <= MAX_LEN; len++) {
306
+ for (let end = from + len; end <= to; end++) {
307
+ if (!valid(end - len, end)) continue;
308
+ covStart[end - len]++;
309
+ covEnd[end]++;
310
+ any = true;
311
+ }
312
+ }
313
+ if (!any) continue;
314
+ let cover = 0;
315
+ let open = -1;
316
+ for (let t = from; t <= to; t++) {
317
+ if (t > from) cover -= covEnd[t];
318
+ if (cover <= 0 && open >= 0) {
319
+ pushTiled(spans, open, t, valid);
320
+ open = -1;
321
+ }
322
+ if (t < to) cover += covStart[t];
323
+ if (cover > 0 && open < 0) open = t;
324
+ }
325
+ }
326
+ if (spans.length === 0) return { body, locked: 0, map: {} };
327
+ const context = sharedContext ?? markerContext(body, reserved);
328
+ const map = {};
329
+ const out = [];
330
+ let cursor = 0;
331
+ for (const [a, b] of spans) {
332
+ const from = S[a];
333
+ const to = E[b - 1];
334
+ spanOffsets?.push([from, to]);
335
+ const marker = markerAt(context.salt, context.index++);
336
+ map[marker] = body.slice(from, to);
337
+ out.push(body.slice(cursor, from));
338
+ out.push(marker);
339
+ cursor = to;
340
+ }
341
+ out.push(body.slice(cursor));
342
+ return { body: out.join(""), locked: spans.length, map };
343
+ }
344
+ function pushTiled(spans, from, to, valid) {
345
+ const len = to - from;
346
+ if (len <= MAX_LEN) {
347
+ spans.push([from, to]);
348
+ return;
349
+ }
350
+ const pieces = new Int32Array(len + 1);
351
+ const via = new Int8Array(len + 1);
352
+ for (let t = MIN_LEN; t <= len; t++) {
353
+ for (let w = MIN_LEN; w <= MAX_LEN && w <= t; w++) {
354
+ const before = t - w;
355
+ if (before !== 0 && pieces[before] === 0) continue;
356
+ if (!valid(from + before, from + t)) continue;
357
+ const count = (before === 0 ? 0 : pieces[before]) + 1;
358
+ if (count > pieces[t]) {
359
+ pieces[t] = count;
360
+ via[t] = w;
361
+ }
362
+ }
363
+ }
364
+ if (pieces[len] <= 1) {
365
+ spans.push([from, to]);
366
+ return;
367
+ }
368
+ const cut = [];
369
+ for (let t = len; t > 0; t -= via[t]) cut.push([from + t - via[t], from + t]);
370
+ cut.reverse();
371
+ for (const c of cut) spans.push(c);
372
+ }
373
+ function scannedEscapeUnit(cp) {
374
+ if (cp >= 48 && cp <= 57) return String.fromCharCode(cp);
375
+ if (cp < 0 || cp > 1114111) return null;
376
+ const char = String.fromCodePoint(cp);
377
+ return SHORT_FORM_SEPARATORS.test(char) ? char : null;
378
+ }
379
+ function lockOutsideEscapes(serialized, reserved) {
380
+ let decoded = "";
381
+ const sourceStart = [];
382
+ const sourceEnd = [];
383
+ for (let i = 0; i < serialized.length; ) {
384
+ const escape = /^\\u([0-9a-fA-F]{4})/.exec(serialized.slice(i));
385
+ const decodedUnit = escape ? scannedEscapeUnit(Number.parseInt(escape[1], 16)) : null;
386
+ if (escape && decodedUnit !== null) {
387
+ decoded += decodedUnit;
388
+ sourceStart.push(i);
389
+ i += escape[0].length;
390
+ sourceEnd.push(i);
391
+ } else {
392
+ const escapeWidth = serialized[i] === "\\" ? /^\\(?:u[0-9a-fA-F]{4}|[\s\S])/.exec(serialized.slice(i))?.[0].length ?? 1 : 0;
393
+ const width = escapeWidth || 1;
394
+ decoded += escapeWidth ? "\0" : serialized[i];
395
+ sourceStart.push(i);
396
+ sourceEnd.push(i + width);
397
+ i += width;
398
+ }
399
+ }
400
+ const lockedSpans = [];
401
+ const result = finalCardBackstopWithContext(decoded, reserved, void 0, lockedSpans);
402
+ if (result.locked === 0) return { body: serialized, locked: 0, map: {} };
403
+ const out = [];
404
+ let sourceCursor = 0;
405
+ let index = 0;
406
+ for (const marker of Object.keys(result.map)) {
407
+ const [start, end] = lockedSpans[index++];
408
+ const rawStart = sourceStart[start];
409
+ const rawEnd = sourceEnd[end - 1];
410
+ out.push(serialized.slice(sourceCursor, rawStart), marker);
411
+ sourceCursor = rawEnd;
412
+ }
413
+ out.push(serialized.slice(sourceCursor));
414
+ return { body: out.join(""), locked: result.locked, map: result.map };
415
+ }
416
+ var JSON_NUMBER = /-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/y;
417
+ function jsonNumberEnd(serialized, start) {
418
+ JSON_NUMBER.lastIndex = start;
419
+ const match = JSON_NUMBER.exec(serialized);
420
+ if (!match || match.index !== start) return null;
421
+ return start + match[0].length;
422
+ }
423
+ function asciiDigitCount(literal) {
424
+ let count = 0;
425
+ for (let i = 0; i < literal.length; i++) {
426
+ const code = literal.charCodeAt(i);
427
+ if (code >= 48 && code <= 57) count++;
428
+ }
429
+ return count;
430
+ }
431
+ function isJsonWhitespace(code) {
432
+ return code === 32 || code === 9 || code === 10 || code === 13;
433
+ }
434
+ function shortJsonNumberRunEnd(serialized, start) {
435
+ const first = jsonNumberEnd(serialized, start);
436
+ if (first === null || asciiDigitCount(serialized.slice(start, first)) >= 13) return null;
437
+ let end = first;
438
+ let count = 1;
439
+ for (; ; ) {
440
+ let k = end;
441
+ while (k < serialized.length && isJsonWhitespace(serialized.charCodeAt(k))) k++;
442
+ if (serialized.charCodeAt(k) !== 44) break;
443
+ k++;
444
+ while (k < serialized.length && isJsonWhitespace(serialized.charCodeAt(k))) k++;
445
+ const next = jsonNumberEnd(serialized, k);
446
+ if (next === null || asciiDigitCount(serialized.slice(k, next)) >= 13) break;
447
+ end = next;
448
+ count++;
449
+ }
450
+ return count >= 2 ? end : null;
451
+ }
452
+ var EXACT_SHORT_JSON_SHAPES = [
453
+ [4, 4, 4, 4],
454
+ [4, 6, 5],
455
+ [4, 4, 4, 4, 3],
456
+ [4, 4, 4, 3],
457
+ [8, 8],
458
+ [4, 4, 8],
459
+ [4, 8, 4],
460
+ [8, 4, 4]
461
+ ];
462
+ function sameShape(lengths, shape) {
463
+ if (lengths.length !== shape.length) return false;
464
+ for (let i = 0; i < lengths.length; i++) if (lengths[i] !== shape[i]) return false;
465
+ return true;
466
+ }
467
+ function digitsPassCard(digits) {
468
+ const n = digits.length;
469
+ if (n < MIN_LEN || n > MAX_LEN) return false;
470
+ const D = new Uint8Array(n);
471
+ let sum = 0;
472
+ for (let t = 0; t < n; t++) {
473
+ const v = digits.charCodeAt(t) - 48;
474
+ if (v < 0 || v > 9) return false;
475
+ D[t] = v;
476
+ const doubled = n % 2 === 0 ? t % 2 === 0 : t % 2 === 1;
477
+ sum += doubled ? DOUBLED[v] : v;
478
+ }
479
+ return sum % 10 === 0 && plausible(D, 0, n);
480
+ }
481
+ function exactShortJsonCardLiteral(literal) {
482
+ const parts = literal.split(",");
483
+ if (parts.length < 2 || parts.length > 5) return false;
484
+ const lengths = [];
485
+ let digits = "";
486
+ for (const part of parts) {
487
+ const token = part.trim();
488
+ if (!/^\d+$/.test(token)) return false;
489
+ lengths.push(token.length);
490
+ digits += token;
491
+ }
492
+ if (!EXACT_SHORT_JSON_SHAPES.some((shape) => sameShape(lengths, shape))) return false;
493
+ return digitsPassCard(digits);
494
+ }
495
+ function shortJsonRunLock(literal, context) {
496
+ if (!exactShortJsonCardLiteral(literal)) return null;
497
+ const marker = markerAt(context.salt, context.index++);
498
+ return { body: JSON.stringify(marker), locked: 1, map: { [marker]: literal } };
499
+ }
500
+ function jsonStringTokens(serialized) {
501
+ const tokens = [];
502
+ let i = 0;
503
+ let tokenIndex = 0;
504
+ while (i < serialized.length) {
505
+ if (serialized.charCodeAt(i) !== 34) {
506
+ i++;
507
+ continue;
508
+ }
509
+ let j = i + 1;
510
+ while (j < serialized.length) {
511
+ const ch = serialized.charCodeAt(j);
512
+ if (ch === 92) j += 2;
513
+ else if (ch === 34) break;
514
+ else j++;
515
+ }
516
+ if (j >= serialized.length) return null;
517
+ const raw = serialized.slice(i + 1, j);
518
+ if (/[\p{Nd}\p{No}]/u.test(raw) || /\\u[0-9a-fA-F]{4}/u.test(raw)) {
519
+ try {
520
+ tokens.push({ tokenIndex, value: raw.includes("\\") ? JSON.parse(serialized.slice(i, j + 1)) : raw });
521
+ } catch {
522
+ return null;
523
+ }
524
+ }
525
+ tokenIndex++;
526
+ i = j + 1;
527
+ }
528
+ return tokens;
529
+ }
530
+ function hexNibble(code) {
531
+ if (code >= 48 && code <= 57) return code - 48;
532
+ if (code >= 65 && code <= 70) return code - 55;
533
+ if (code >= 97 && code <= 102) return code - 87;
534
+ return -1;
535
+ }
536
+ function percentByteAt(text, i) {
537
+ if (i + 2 >= text.length || text.charCodeAt(i) !== 37) return null;
538
+ const hi = hexNibble(text.charCodeAt(i + 1));
539
+ const lo = hexNibble(text.charCodeAt(i + 2));
540
+ if (hi < 0 || lo < 0) return null;
541
+ return { byte: hi << 4 | lo, next: i + 3 };
542
+ }
543
+ function takePercentChar(text, i) {
544
+ const lead = percentByteAt(text, i);
545
+ if (!lead) return null;
546
+ const b0 = lead.byte;
547
+ if (b0 === 9 || b0 === 10 || b0 === 11 || b0 === 12 || b0 === 13) {
548
+ return { char: String.fromCharCode(b0), next: lead.next };
549
+ }
550
+ if (b0 >= 32 && b0 <= 126) return { char: String.fromCharCode(b0), next: lead.next };
551
+ let need = 0;
552
+ if (b0 >= 194 && b0 <= 223) need = 2;
553
+ else if (b0 >= 224 && b0 <= 239) need = 3;
554
+ else if (b0 >= 240 && b0 <= 244) need = 4;
555
+ else return null;
556
+ const bytes = [b0];
557
+ let pos = lead.next;
558
+ for (let n = 1; n < need; n++) {
559
+ const cont = percentByteAt(text, pos);
560
+ if (!cont || cont.byte < 128 || cont.byte > 191) return null;
561
+ bytes.push(cont.byte);
562
+ pos = cont.next;
563
+ }
564
+ if (b0 === 224 && bytes[1] < 160) return null;
565
+ if (b0 === 237 && bytes[1] >= 160) return null;
566
+ if (b0 === 240 && bytes[1] < 144) return null;
567
+ if (b0 === 244 && bytes[1] > 143) return null;
568
+ let cp = 0;
569
+ if (need === 2) cp = (b0 & 31) << 6 | bytes[1] & 63;
570
+ else if (need === 3) cp = (b0 & 15) << 12 | (bytes[1] & 63) << 6 | bytes[2] & 63;
571
+ else cp = (b0 & 7) << 18 | (bytes[1] & 63) << 12 | (bytes[2] & 63) << 6 | bytes[3] & 63;
572
+ return { char: String.fromCodePoint(cp), next: pos };
573
+ }
574
+ function projectPercentEncoded(text) {
575
+ let projected = "";
576
+ const starts = [];
577
+ const ends = [];
578
+ let decoded = false;
579
+ for (let i = 0; i < text.length; ) {
580
+ const tok = takePercentChar(text, i);
581
+ if (tok) {
582
+ decoded = true;
583
+ for (let u = 0; u < tok.char.length; u++) {
584
+ projected += tok.char[u];
585
+ starts.push(i);
586
+ ends.push(tok.next);
587
+ }
588
+ i = tok.next;
589
+ } else if (text.charCodeAt(i) === 43) {
590
+ decoded = true;
591
+ projected += " ";
592
+ starts.push(i);
593
+ ends.push(i + 1);
594
+ i++;
595
+ } else {
596
+ projected += text[i];
597
+ starts.push(i);
598
+ ends.push(i + 1);
599
+ i++;
600
+ }
601
+ }
602
+ if (!decoded || projected === text) return null;
603
+ return { projected, starts, ends };
604
+ }
605
+ var PERCENT_DECODE_LIMIT = 32;
606
+ var JSON_BOUNDARY_BACKSLASHES = [7, 3, 1, 0];
607
+ var JSON_BOUNDARY_GAP = 6;
608
+ var JSON_BOUNDARY_INDENT = 100;
609
+ function isBoundaryQuote(code) {
610
+ return code === 34 || code === 8220 || code === 8221 || code === 65282;
611
+ }
612
+ function isJsonBoundaryHorizontal(code) {
613
+ if (code === 32 || code === 9 || code === 160 || code === 12288 || code === 5760 || code === 8239 || code === 8287) return true;
614
+ return code >= 8192 && code <= 8202;
615
+ }
616
+ function consumeJsonBoundaryGap(text, p) {
617
+ const n = text.length;
618
+ let inline = 0;
619
+ let broke = false;
620
+ let indent = 0;
621
+ while (p < n) {
622
+ const c = text.charCodeAt(p);
623
+ if (!broke && c === 13) {
624
+ broke = true;
625
+ p++;
626
+ if (p < n && text.charCodeAt(p) === 10) p++;
627
+ continue;
628
+ }
629
+ if (!broke && (c === 10 || c === 8232 || c === 8233)) {
630
+ broke = true;
631
+ p++;
632
+ continue;
633
+ }
634
+ if (isJsonBoundaryHorizontal(c)) {
635
+ if (!broke) {
636
+ if (inline >= JSON_BOUNDARY_GAP) break;
637
+ inline++;
638
+ } else if (indent >= JSON_BOUNDARY_INDENT) {
639
+ break;
640
+ } else {
641
+ indent++;
642
+ }
643
+ p++;
644
+ continue;
645
+ }
646
+ break;
647
+ }
648
+ return p;
649
+ }
650
+ function isDigitAt(text, i) {
651
+ if (i < 0 || i >= text.length) return false;
652
+ const cu = text.charCodeAt(i);
653
+ if (cu >= 56320 && cu <= 57343) return false;
654
+ return digitValue(text.codePointAt(i)) >= 0;
655
+ }
656
+ function jsonStringBoundaryLength(text, i) {
657
+ const n = text.length;
658
+ for (const backs of JSON_BOUNDARY_BACKSLASHES) {
659
+ let p = i;
660
+ let ok = true;
661
+ for (let k = 0; k < backs; k++) {
662
+ if (p >= n || text.charCodeAt(p) !== 92) {
663
+ ok = false;
664
+ break;
665
+ }
666
+ p++;
667
+ }
668
+ if (!ok || p >= n || !isBoundaryQuote(text.charCodeAt(p)) || text.charCodeAt(p) !== 34 && backs !== 0) continue;
669
+ p++;
670
+ p = consumeJsonBoundaryGap(text, p);
671
+ if (p >= n || text.charCodeAt(p) !== 44) continue;
672
+ p++;
673
+ p = consumeJsonBoundaryGap(text, p);
674
+ for (let k = 0; k < backs; k++) {
675
+ if (p >= n || text.charCodeAt(p) !== 92) {
676
+ ok = false;
677
+ break;
678
+ }
679
+ p++;
680
+ }
681
+ if (!ok || p >= n || !isBoundaryQuote(text.charCodeAt(p)) || text.charCodeAt(p) !== 34 && backs !== 0) continue;
682
+ return p + 1 - i;
683
+ }
684
+ return 0;
685
+ }
686
+ function bridgeJsonStringBoundaries(text) {
687
+ let bridged = "";
688
+ const src = [];
689
+ let crossed = false;
690
+ for (let i = 0; i < text.length; ) {
691
+ const boundary = jsonStringBoundaryLength(text, i);
692
+ if (boundary > 0 && isDigitAt(text, i - 1) && isDigitAt(text, i + boundary)) {
693
+ bridged += " ";
694
+ src.push(-1);
695
+ crossed = true;
696
+ i += boundary;
697
+ continue;
698
+ }
699
+ const cp = text.codePointAt(i);
700
+ const width = cp > 65535 ? 2 : 1;
701
+ bridged += text.slice(i, i + width);
702
+ for (let k = 0; k < width; k++) src.push(i + k);
703
+ i += width;
704
+ }
705
+ if (!crossed || src.length !== bridged.length) return null;
706
+ return { bridged, src };
707
+ }
708
+ function boundaryRunIsIdColumn(bridged, src, spanFrom, spanTo) {
709
+ const joins = (i) => {
710
+ if (i < 0 || i >= bridged.length) return false;
711
+ if (src[i] < 0) return true;
712
+ const cu = bridged.charCodeAt(i);
713
+ if (cu >= 56320 && cu <= 57343) return false;
714
+ const cp = bridged.codePointAt(i);
715
+ if (digitValue(cp) >= 0) return true;
716
+ return cp === 32 || cp === 9 || cp === 10 || cp === 13 || cp === 45 || cp === 46 || cp === 47;
717
+ };
718
+ let from = spanFrom;
719
+ while (from > 0 && joins(from - 1)) {
720
+ const prev = bridged.charCodeAt(from - 1);
721
+ from -= prev >= 56320 && prev <= 57343 ? 2 : 1;
722
+ }
723
+ let to = spanTo;
724
+ while (to < bridged.length && joins(to)) {
725
+ const cp = bridged.codePointAt(to);
726
+ to += cp > 65535 ? 2 : 1;
727
+ }
728
+ let digits = 0;
729
+ let bridges = 0;
730
+ for (let i = from; i < to; ) {
731
+ if (src[i] < 0) {
732
+ bridges++;
733
+ i++;
734
+ continue;
735
+ }
736
+ const cp = bridged.codePointAt(i);
737
+ const width = cp > 65535 ? 2 : 1;
738
+ if (digitValue(cp) >= 0) digits++;
739
+ i += width;
740
+ }
741
+ return digits > MAX_LEN && bridges + 1 > 5;
742
+ }
743
+ function boundarySpanGroupLengths(bridged, src, from, to) {
744
+ if (from > 0 && src[from - 1] >= 0 && isDigitAt(bridged, from - 1)) return null;
745
+ if (to < bridged.length && isDigitAt(bridged, to)) return null;
746
+ const lengths = [];
747
+ let count = 0;
748
+ for (let i = from; i < to; ) {
749
+ if (src[i] < 0) {
750
+ if (count === 0) return null;
751
+ lengths.push(count);
752
+ count = 0;
753
+ i++;
754
+ continue;
755
+ }
756
+ const cp = bridged.codePointAt(i);
757
+ const width = cp > 65535 ? 2 : 1;
758
+ if (digitValue(cp) >= 0) count++;
759
+ else if (count > 0) {
760
+ lengths.push(count);
761
+ count = 0;
762
+ }
763
+ i += width;
764
+ }
765
+ if (count > 0) lengths.push(count);
766
+ return lengths.length > 0 ? lengths : null;
767
+ }
768
+ function lengthsAreCardShape(lengths) {
769
+ return EXACT_SHORT_JSON_SHAPES.some((shape) => sameShape(lengths, shape));
770
+ }
771
+ function groupAlignedBridgeSpans(bridged, src) {
772
+ const groups = [];
773
+ let i = 0;
774
+ while (i < bridged.length) {
775
+ if (src[i] < 0 || !isDigitAt(bridged, i)) {
776
+ i++;
777
+ continue;
778
+ }
779
+ const from = i;
780
+ let digits = "";
781
+ while (i < bridged.length && src[i] >= 0 && isDigitAt(bridged, i)) {
782
+ const cp = bridged.codePointAt(i);
783
+ const width = cp > 65535 ? 2 : 1;
784
+ const value = digitValue(cp);
785
+ if (value >= 0) digits += String(value);
786
+ i += width;
787
+ }
788
+ if (digits.length > 0) groups.push({ from, to: i, digits });
789
+ }
790
+ const out = [];
791
+ let g = 0;
792
+ while (g < groups.length) {
793
+ let matched = 0;
794
+ for (const shape of EXACT_SHORT_JSON_SHAPES) {
795
+ if (g + shape.length > groups.length) continue;
796
+ const slice = groups.slice(g, g + shape.length);
797
+ if (!sameShape(slice.map((group) => group.digits.length), shape)) continue;
798
+ if (!digitsPassCard(slice.map((group) => group.digits).join(""))) continue;
799
+ out.push([slice[0].from, slice[slice.length - 1].to]);
800
+ matched = shape.length;
801
+ break;
802
+ }
803
+ g += matched > 0 ? matched : 1;
804
+ }
805
+ return out;
806
+ }
807
+ function jsonBoundarySourceSpans(text, reserved) {
808
+ if (!text.includes('"') && !text.includes("\u201C") && !text.includes("\u201D") && !text.includes("\uFF02")) return [];
809
+ const bridged = bridgeJsonStringBoundaries(text);
810
+ if (!bridged) return [];
811
+ const bridgedSpans = [];
812
+ const result = finalCardBackstopWithContext(bridged.bridged, reserved, void 0, bridgedSpans);
813
+ const aligned = groupAlignedBridgeSpans(bridged.bridged, bridged.src);
814
+ if (result.locked === 0 && aligned.length === 0) return [];
815
+ const out = [];
816
+ for (const [from, to] of [...bridgedSpans, ...aligned]) {
817
+ let crossed = false;
818
+ for (let i = from; i < to; i++) {
819
+ if (bridged.src[i] < 0) {
820
+ crossed = true;
821
+ break;
822
+ }
823
+ }
824
+ if (!crossed) continue;
825
+ if (boundaryRunIsIdColumn(bridged.bridged, bridged.src, from, to)) {
826
+ const lengths = boundarySpanGroupLengths(bridged.bridged, bridged.src, from, to);
827
+ if (!lengths || !lengthsAreCardShape(lengths)) continue;
828
+ }
829
+ let lo = -1;
830
+ let prev = -1;
831
+ for (let i = from; i < to; i++) {
832
+ const source = bridged.src[i];
833
+ if (source < 0) {
834
+ if (lo >= 0) out.push([lo, prev + 1]);
835
+ lo = -1;
836
+ continue;
837
+ }
838
+ if (lo < 0) lo = source;
839
+ prev = source;
840
+ }
841
+ if (lo >= 0) out.push([lo, prev + 1]);
842
+ }
843
+ return out;
844
+ }
845
+ function mapThroughLevels(levels, lo, hi) {
846
+ let start = lo;
847
+ let end = hi;
848
+ for (let d = levels.length - 1; d >= 0; d--) {
849
+ start = levels[d].starts[start];
850
+ end = levels[d].ends[end - 1];
851
+ }
852
+ return [start, end];
853
+ }
854
+ function spliceMappedSpans(original, levels, spans, context) {
855
+ const mapped = [];
856
+ for (const [lo, hi] of spans) {
857
+ if (hi <= lo) continue;
858
+ const [start, end] = mapThroughLevels(levels, lo, hi);
859
+ if (end <= start) continue;
860
+ mapped.push({ lo: start, hi: end });
861
+ }
862
+ mapped.sort((a, b) => a.lo - b.lo || a.hi - b.hi);
863
+ const kept = [];
864
+ let last = -1;
865
+ for (const item of mapped) {
866
+ if (item.lo < last) continue;
867
+ kept.push(item);
868
+ last = item.hi;
869
+ }
870
+ if (kept.length === 0) return { body: original, locked: 0, map: {} };
871
+ const out = [];
872
+ const map = {};
873
+ let cursor = 0;
874
+ for (const item of kept) {
875
+ const marker = markerAt(context.salt, context.index++);
876
+ out.push(original.slice(cursor, item.lo), marker);
877
+ map[marker] = original.slice(item.lo, item.hi);
878
+ cursor = item.hi;
879
+ }
880
+ out.push(original.slice(cursor));
881
+ return { body: out.join(""), locked: kept.length, map };
882
+ }
883
+ function lockJsonBoundaryCardRuns(text, reserved, context) {
884
+ return spliceMappedSpans(text, [], jsonBoundarySourceSpans(text, reserved), context);
885
+ }
886
+ function lockPercentEncodedDigits(text, reserved, context) {
887
+ if (!/%[0-9a-fA-F]{2}/u.test(text)) return lockJsonBoundaryCardRuns(text, reserved, context);
888
+ const levels = [];
889
+ let current = text;
890
+ for (let depth = 0; depth < PERCENT_DECODE_LIMIT; depth++) {
891
+ const proj = projectPercentEncoded(current);
892
+ if (!proj) break;
893
+ levels.push(proj);
894
+ current = proj.projected;
895
+ }
896
+ if (levels.length === 0) return lockJsonBoundaryCardRuns(text, reserved, context);
897
+ const boundarySpans = jsonBoundarySourceSpans(current, reserved);
898
+ const normalSpans = [];
899
+ finalCardBackstopWithContext(current, reserved, void 0, normalSpans);
900
+ const chosen = [...boundarySpans];
901
+ for (const span of normalSpans) {
902
+ const overlaps = boundarySpans.some((boundary) => span[0] < boundary[1] && boundary[0] < span[1]);
903
+ if (!overlaps) chosen.push(span);
904
+ }
905
+ if (chosen.length === 0) return { body: text, locked: 0, map: {} };
906
+ return spliceMappedSpans(text, levels, chosen, context);
907
+ }
908
+ function crossFieldCardTokens(tokens, reserved, context) {
909
+ const joined = [];
910
+ const ranges = [];
911
+ const tokenIndexes = [];
912
+ let offset = 0;
913
+ for (const token of tokens) {
914
+ if (joined.length > 0) {
915
+ joined.push(" ");
916
+ offset++;
917
+ }
918
+ const start = offset;
919
+ joined.push(token.value);
920
+ offset += token.value.length;
921
+ ranges.push([start, offset]);
922
+ tokenIndexes.push(token.tokenIndex);
923
+ }
924
+ const spans = [];
925
+ finalCardBackstopWithContext(joined.join(""), reserved, context, spans);
926
+ const involved = /* @__PURE__ */ new Set();
927
+ const digitCount = (value) => {
928
+ let n = 0;
929
+ for (let i = 0; i < value.length; ) {
930
+ const cp = value.codePointAt(i);
931
+ if (digitValue(cp) >= 0) n++;
932
+ i += cp > 65535 ? 2 : 1;
933
+ }
934
+ return n;
935
+ };
936
+ const digitsPerToken = tokens.map((token) => digitCount(token.value));
937
+ for (const [start, end] of spans) {
938
+ const touching = [];
939
+ for (let i = 0; i < ranges.length; i++) {
940
+ if (ranges[i][0] < end && ranges[i][1] > start) touching.push(i);
941
+ }
942
+ if (touching.length < 2) continue;
943
+ let lo = touching[0];
944
+ let hi = touching[touching.length - 1];
945
+ while (lo > 0 && digitsPerToken[lo - 1] > 0) lo--;
946
+ while (hi + 1 < tokens.length && digitsPerToken[hi + 1] > 0) hi++;
947
+ let digits = 0;
948
+ let groups = 0;
949
+ for (let i = lo; i <= hi; i++) {
950
+ if (digitsPerToken[i] > 0) {
951
+ digits += digitsPerToken[i];
952
+ groups++;
953
+ }
954
+ }
955
+ if (digits > MAX_LEN && groups > 5) {
956
+ const whole = touching.every((index) => ranges[index][0] >= start && ranges[index][1] <= end);
957
+ if (!whole) continue;
958
+ const lengths = touching.map((index) => digitsPerToken[index]);
959
+ const pure = touching.every((index) => digitsPerToken[index] > 0 && digitsPerToken[index] === [...tokens[index].value].length);
960
+ if (!pure || !lengthsAreCardShape(lengths)) continue;
961
+ }
962
+ for (const index of touching) involved.add(tokenIndexes[index]);
963
+ }
964
+ for (let g = 0; g < tokens.length; ) {
965
+ let matched = 0;
966
+ for (const shape of EXACT_SHORT_JSON_SHAPES) {
967
+ if (g + shape.length > tokens.length) continue;
968
+ const slice = tokens.slice(g, g + shape.length);
969
+ const parts = [];
970
+ let pure = true;
971
+ for (const token of slice) {
972
+ let ascii = "";
973
+ for (const ch of token.value) {
974
+ const value = digitValue(ch.codePointAt(0));
975
+ if (value < 0) {
976
+ pure = false;
977
+ break;
978
+ }
979
+ ascii += String(value);
980
+ }
981
+ if (!pure || ascii.length === 0) {
982
+ pure = false;
983
+ break;
984
+ }
985
+ parts.push(ascii);
986
+ }
987
+ if (!pure || !sameShape(parts.map((part) => part.length), shape)) continue;
988
+ if (!digitsPassCard(parts.join(""))) continue;
989
+ for (const token of slice) involved.add(token.tokenIndex);
990
+ matched = shape.length;
991
+ break;
992
+ }
993
+ g += matched > 0 ? matched : 1;
994
+ }
995
+ return involved;
996
+ }
997
+ function mixedDigitPieces(serialized) {
998
+ const pieces = [];
999
+ let i = 0;
1000
+ let tokenIndex = 0;
1001
+ while (i < serialized.length) {
1002
+ const code = serialized.charCodeAt(i);
1003
+ if (code === 34) {
1004
+ let j = i + 1;
1005
+ while (j < serialized.length) {
1006
+ const ch = serialized.charCodeAt(j);
1007
+ if (ch === 92) j += 2;
1008
+ else if (ch === 34) break;
1009
+ else j++;
1010
+ }
1011
+ if (j >= serialized.length) return pieces;
1012
+ let decoded = "";
1013
+ try {
1014
+ decoded = JSON.parse(serialized.slice(i, j + 1));
1015
+ } catch {
1016
+ return pieces;
1017
+ }
1018
+ if (/^\d{1,12}$/.test(decoded)) {
1019
+ pieces.push({ kind: "string", start: i, end: j + 1, digits: decoded, tokenIndex });
1020
+ }
1021
+ tokenIndex++;
1022
+ i = j + 1;
1023
+ continue;
1024
+ }
1025
+ if (code === 45 || code >= 48 && code <= 57) {
1026
+ const end = jsonNumberEnd(serialized, i);
1027
+ if (end !== null) {
1028
+ const literal = serialized.slice(i, end);
1029
+ if (/^\d{1,12}$/.test(literal)) pieces.push({ kind: "number", start: i, end, digits: literal });
1030
+ i = end;
1031
+ continue;
1032
+ }
1033
+ }
1034
+ i++;
1035
+ }
1036
+ return pieces;
1037
+ }
1038
+ function mixedNumberStringLocks(serialized) {
1039
+ const numbers = /* @__PURE__ */ new Map();
1040
+ const strings = /* @__PURE__ */ new Set();
1041
+ const pieces = mixedDigitPieces(serialized);
1042
+ let g = 0;
1043
+ while (g < pieces.length) {
1044
+ let matched = 0;
1045
+ for (const shape of EXACT_SHORT_JSON_SHAPES) {
1046
+ if (g + shape.length > pieces.length) continue;
1047
+ const slice = pieces.slice(g, g + shape.length);
1048
+ if (!sameShape(slice.map((piece) => piece.digits.length), shape)) continue;
1049
+ if (!slice.some((piece) => piece.kind === "number") || !slice.some((piece) => piece.kind === "string")) continue;
1050
+ if (!digitsPassCard(slice.map((piece) => piece.digits).join(""))) continue;
1051
+ for (const piece of slice) {
1052
+ if (piece.kind === "number") numbers.set(piece.start, piece.end);
1053
+ else if (piece.tokenIndex !== void 0) strings.add(piece.tokenIndex);
1054
+ }
1055
+ matched = shape.length;
1056
+ break;
1057
+ }
1058
+ g += matched > 0 ? matched : 1;
1059
+ }
1060
+ return { numbers, strings };
1061
+ }
1062
+ function decodeBase64Text(token) {
1063
+ if (token.length < 20 || token.length > 88 || token.length % 4 !== 0) return null;
1064
+ if (!/^[A-Za-z0-9+/]+={0,2}$/.test(token)) return null;
1065
+ const atobFn = globalThis.atob;
1066
+ if (!atobFn) return null;
1067
+ let bin;
1068
+ try {
1069
+ bin = atobFn(token);
1070
+ } catch {
1071
+ return null;
1072
+ }
1073
+ if (bin.length < MIN_LEN || bin.length > 64) return null;
1074
+ let out = "";
1075
+ for (let i = 0; i < bin.length; i++) {
1076
+ const c = bin.charCodeAt(i);
1077
+ if (c !== 9 && c !== 10 && c !== 13 && (c < 32 || c > 126)) return null;
1078
+ out += bin[i];
1079
+ }
1080
+ return out;
1081
+ }
1082
+ function lockBase64CardTokens(value, context) {
1083
+ const re = /[A-Za-z0-9+/]{20,}={0,2}/g;
1084
+ const map = {};
1085
+ let locked = 0;
1086
+ const body = value.replace(re, (token) => {
1087
+ if (token.length > 88 || token.length % 4 !== 0) return token;
1088
+ const decoded = decodeBase64Text(token);
1089
+ if (decoded == null || finalCardBackstop(decoded).locked === 0) return token;
1090
+ const marker = markerAt(context.salt, context.index++);
1091
+ map[marker] = token;
1092
+ locked++;
1093
+ return marker;
1094
+ });
1095
+ return { body, locked, map };
1096
+ }
1097
+ function finalCardBackstopJson(serialized, reserved) {
1098
+ try {
1099
+ JSON.parse(serialized);
1100
+ } catch {
1101
+ return lockOutsideEscapes(serialized, reserved);
1102
+ }
1103
+ const out = [];
1104
+ const map = {};
1105
+ const context = markerContext(serialized, reserved);
1106
+ const tokens = jsonStringTokens(serialized);
1107
+ const splitTokens = tokens ? crossFieldCardTokens(tokens, reserved, context) : /* @__PURE__ */ new Set();
1108
+ const mixed = mixedNumberStringLocks(serialized);
1109
+ for (const index of mixed.strings) splitTokens.add(index);
1110
+ let locked = 0;
1111
+ let cursor = 0;
1112
+ let i = 0;
1113
+ let tokenIndex = 0;
1114
+ while (i < serialized.length) {
1115
+ const ch0 = serialized.charCodeAt(i);
1116
+ if (ch0 !== 34) {
1117
+ if (ch0 === 45 || ch0 >= 48 && ch0 <= 57) {
1118
+ const mixedEnd = mixed.numbers.get(i);
1119
+ if (mixedEnd !== void 0) {
1120
+ const literal = serialized.slice(i, mixedEnd);
1121
+ const marker = markerAt(context.salt, context.index++);
1122
+ map[marker] = literal;
1123
+ out.push(serialized.slice(cursor, i), JSON.stringify(marker));
1124
+ cursor = mixedEnd;
1125
+ locked++;
1126
+ i = mixedEnd;
1127
+ continue;
1128
+ }
1129
+ const runEnd = shortJsonNumberRunEnd(serialized, i);
1130
+ if (runEnd !== null) {
1131
+ let touchesMixed = false;
1132
+ for (const start of mixed.numbers.keys()) {
1133
+ if (start >= i && start < runEnd) {
1134
+ touchesMixed = true;
1135
+ break;
1136
+ }
1137
+ }
1138
+ if (!touchesMixed) {
1139
+ const run = serialized.slice(i, runEnd);
1140
+ const lockedRun = shortJsonRunLock(run, context);
1141
+ if (lockedRun) {
1142
+ out.push(serialized.slice(cursor, i), lockedRun.body);
1143
+ cursor = runEnd;
1144
+ locked += lockedRun.locked;
1145
+ Object.assign(map, lockedRun.map);
1146
+ }
1147
+ i = runEnd;
1148
+ continue;
1149
+ }
1150
+ JSON_NUMBER.lastIndex = i;
1151
+ const single = JSON_NUMBER.exec(serialized);
1152
+ if (single) {
1153
+ i += single[0].length;
1154
+ continue;
1155
+ }
1156
+ }
1157
+ JSON_NUMBER.lastIndex = i;
1158
+ const m = JSON_NUMBER.exec(serialized);
1159
+ if (m) {
1160
+ const token2 = m[0];
1161
+ if (token2.length >= 13) {
1162
+ const r2 = finalCardBackstopWithContext(token2, reserved, context);
1163
+ if (r2.locked > 0) {
1164
+ out.push(serialized.slice(cursor, i), JSON.stringify(r2.body));
1165
+ cursor = i + token2.length;
1166
+ locked += r2.locked;
1167
+ Object.assign(map, r2.map);
1168
+ }
1169
+ }
1170
+ i += token2.length;
1171
+ continue;
1172
+ }
1173
+ }
1174
+ i++;
1175
+ continue;
1176
+ }
1177
+ let j = i + 1;
1178
+ while (j < serialized.length) {
1179
+ const ch = serialized.charCodeAt(j);
1180
+ if (ch === 92) j += 2;
1181
+ else if (ch === 34) break;
1182
+ else j++;
1183
+ }
1184
+ if (j >= serialized.length) return lockOutsideEscapes(serialized, reserved);
1185
+ const token = serialized.slice(i, j + 1);
1186
+ let decoded;
1187
+ try {
1188
+ decoded = JSON.parse(token);
1189
+ } catch {
1190
+ return lockOutsideEscapes(serialized, reserved);
1191
+ }
1192
+ let value = decoded;
1193
+ let valueLocked = 0;
1194
+ const urlLocked = lockPercentEncodedDigits(value, reserved, context);
1195
+ if (urlLocked.locked > 0) {
1196
+ value = urlLocked.body;
1197
+ valueLocked += urlLocked.locked;
1198
+ Object.assign(map, urlLocked.map);
1199
+ }
1200
+ if (valueLocked === 0) {
1201
+ let nested = value;
1202
+ for (let depth = 0; depth < PERCENT_DECODE_LIMIT; depth++) {
1203
+ try {
1204
+ const next = decodeURIComponent(nested.replace(/\+/g, "%20"));
1205
+ if (next === nested) break;
1206
+ nested = next;
1207
+ } catch {
1208
+ break;
1209
+ }
1210
+ if (finalCardBackstop(nested).locked > 0) {
1211
+ const marker = markerAt(context.salt, context.index++);
1212
+ map[marker] = decoded;
1213
+ value = marker;
1214
+ valueLocked++;
1215
+ break;
1216
+ }
1217
+ }
1218
+ }
1219
+ if (valueLocked === 0) {
1220
+ const b64 = lockBase64CardTokens(value, context);
1221
+ if (b64.locked > 0) {
1222
+ value = b64.body;
1223
+ valueLocked += b64.locked;
1224
+ Object.assign(map, b64.map);
1225
+ }
1226
+ }
1227
+ const r = finalCardBackstopWithContext(value, reserved, context);
1228
+ if (r.locked > 0) {
1229
+ value = r.body;
1230
+ valueLocked += r.locked;
1231
+ Object.assign(map, r.map);
1232
+ }
1233
+ if (splitTokens.has(tokenIndex)) {
1234
+ const marker = markerAt(context.salt, context.index++);
1235
+ map[marker] = decoded;
1236
+ value = marker;
1237
+ valueLocked++;
1238
+ }
1239
+ if (valueLocked > 0) {
1240
+ out.push(serialized.slice(cursor, i), JSON.stringify(value));
1241
+ cursor = j + 1;
1242
+ locked += valueLocked;
1243
+ }
1244
+ tokenIndex++;
1245
+ i = j + 1;
1246
+ }
1247
+ if (locked === 0) return { body: serialized, locked: 0, map: {} };
1248
+ out.push(serialized.slice(cursor));
1249
+ return { body: out.join(""), locked, map };
1250
+ }
1251
+
1252
+ export {
1253
+ finalCardBackstop,
1254
+ finalCardBackstopToFixedPoint,
1255
+ hasUnprotectedCardJson,
1256
+ finalCardBackstopJsonToFixedPoint,
1257
+ finalCardBackstopJson
1258
+ };