@cloakapi/conformance 0.0.0-stage → 0.1.4

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.
@@ -0,0 +1,143 @@
1
+ const MAX_JSON_CODE_UNITS = 8 * 1024 * 1024;
2
+ const MAX_JSON_DEPTH = 256;
3
+
4
+ function decimalForm(value) {
5
+ const match = /^(-?)(\d+)(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/.exec(value);
6
+ if (!match) return null;
7
+ const fraction = match[3] || '';
8
+ let digits = (match[2] + fraction).replace(/^0+/, '');
9
+ if (!digits) return '0';
10
+ let exponent = Number(match[4] || 0) - fraction.length;
11
+ const trailingZeros = /0+$/.exec(digits)?.[0].length || 0;
12
+ if (trailingZeros > 0) {
13
+ digits = digits.slice(0, -trailingZeros);
14
+ exponent += trailingZeros;
15
+ }
16
+ return `${match[1]}${digits}e${exponent}`;
17
+ }
18
+
19
+ /** Parse JSON without duplicate members or values that JS would lose. */
20
+ export function parseStrictJson(raw) {
21
+ if (typeof raw !== 'string') throw new TypeError('JSON input must be text');
22
+ if (raw.length > MAX_JSON_CODE_UNITS) throw new SyntaxError('JSON input is too large');
23
+
24
+ let i = 0;
25
+ const fail = () => { throw new SyntaxError(`ambiguous or invalid JSON at offset ${i}`); };
26
+ const ws = () => {
27
+ while (raw[i] === '\u0009' || raw[i] === '\u000a' || raw[i] === '\u000d' || raw[i] === '\u0020') i++;
28
+ };
29
+ const string = () => {
30
+ const start = i++;
31
+ while (i < raw.length) {
32
+ const c = raw.charCodeAt(i++);
33
+ if (c === 0x22) {
34
+ let value;
35
+ try {
36
+ value = JSON.parse(raw.slice(start, i));
37
+ } catch {
38
+ fail();
39
+ }
40
+ for (let j = 0; j < value.length; j++) {
41
+ const unit = value.charCodeAt(j);
42
+ if (unit >= 0xd800 && unit <= 0xdbff) {
43
+ const next = value.charCodeAt(j + 1);
44
+ if (!(next >= 0xdc00 && next <= 0xdfff)) fail();
45
+ j++;
46
+ } else if (unit >= 0xdc00 && unit <= 0xdfff) {
47
+ fail();
48
+ }
49
+ }
50
+ return value;
51
+ }
52
+ if (c < 0x20) fail();
53
+ if (c === 0x5c) {
54
+ const escape = raw[i++];
55
+ if (escape === 'u') {
56
+ if (!/^[0-9a-fA-F]{4}$/.test(raw.slice(i, i + 4))) fail();
57
+ i += 4;
58
+ } else if (!'"\\/bfnrt'.includes(escape)) {
59
+ fail();
60
+ }
61
+ }
62
+ }
63
+ fail();
64
+ };
65
+ const value = (depth = 0) => {
66
+ ws();
67
+ if (raw[i] === '"') {
68
+ string();
69
+ return;
70
+ }
71
+ if (raw[i] === '{') {
72
+ if (depth >= MAX_JSON_DEPTH) fail();
73
+ i++;
74
+ ws();
75
+ const keys = new Set();
76
+ if (raw[i] === '}') {
77
+ i++;
78
+ return;
79
+ }
80
+ while (true) {
81
+ ws();
82
+ if (raw[i] !== '"') fail();
83
+ const key = string();
84
+ if (keys.has(key)) fail();
85
+ keys.add(key);
86
+ ws();
87
+ if (raw[i++] !== ':') fail();
88
+ value(depth + 1);
89
+ ws();
90
+ if (raw[i] === '}') {
91
+ i++;
92
+ return;
93
+ }
94
+ if (raw[i++] !== ',') fail();
95
+ }
96
+ }
97
+ if (raw[i] === '[') {
98
+ if (depth >= MAX_JSON_DEPTH) fail();
99
+ i++;
100
+ ws();
101
+ if (raw[i] === ']') {
102
+ i++;
103
+ return;
104
+ }
105
+ while (true) {
106
+ value(depth + 1);
107
+ ws();
108
+ if (raw[i] === ']') {
109
+ i++;
110
+ return;
111
+ }
112
+ if (raw[i++] !== ',') fail();
113
+ }
114
+ }
115
+
116
+ const number = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/.exec(raw.slice(i));
117
+ if (number) {
118
+ const token = number[0];
119
+ const parsed = Number(token);
120
+ const roundTrip = decimalForm(parsed.toString());
121
+ if (
122
+ roundTrip === null ||
123
+ decimalForm(token) !== roundTrip ||
124
+ Object.is(parsed, -0)
125
+ ) fail();
126
+ i += token.length;
127
+ return;
128
+ }
129
+ for (const literal of ['true', 'false', 'null']) {
130
+ if (raw.startsWith(literal, i)) {
131
+ i += literal.length;
132
+ return;
133
+ }
134
+ }
135
+ fail();
136
+ };
137
+
138
+ if (raw.charCodeAt(0) === 0xfeff) fail();
139
+ value();
140
+ ws();
141
+ if (i !== raw.length) fail();
142
+ return JSON.parse(raw);
143
+ }