@omnibuf/core 0.0.7 → 0.1.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.
package/dist/blind.d.ts CHANGED
@@ -24,7 +24,7 @@ export declare function encodeField0SchemaMeta(meta: SchemaMeta | null | undefin
24
24
  export declare function mergeSchemaMeta(base: SchemaMeta | null | undefined, next: SchemaMeta | null | undefined): SchemaMeta | null;
25
25
  export declare function decodeSchemaMeta(data: Uint8Array, depth?: number, limit?: number): SchemaMeta;
26
26
  /**
27
- * Decodes and merges all Field #0 (`Tag = 0x0B`, `WireSubMsg = 5`) SchemaMeta blocks in `packet`.
27
+ * Decodes and merges all Field #0 (`Tag = 0x19`, `WireSubMsg = 12`) SchemaMeta blocks in `packet`.
28
28
  */
29
29
  export declare function readField0SchemaMeta(packet: Uint8Array): [meta: SchemaMeta | null, ok: boolean];
30
30
  export interface DecodeBlindOptions {
@@ -34,7 +34,7 @@ export interface DecodeBlindOptions {
34
34
  */
35
35
  bytesAsBase64?: boolean;
36
36
  /**
37
- * When true (default), `WireFixed32` values are rounded to their shortest 32-bit IEEE-754 decimal representation.
37
+ * When true (default), `WireFloat32` values are rounded to their shortest 32-bit IEEE-754 decimal representation.
38
38
  */
39
39
  roundFloat32?: boolean;
40
40
  /**
@@ -44,7 +44,7 @@ export interface DecodeBlindOptions {
44
44
  }
45
45
  /**
46
46
  * Decodes any OmniBuf packet without a compile-time schema.
47
- * If Field #0 (`Tag = 0x0B`, `WireSubMsg = 5`, `omnibuf.v1.SchemaMeta`) is present, its field names
47
+ * If Field #0 (`Tag = 0x19`, `WireSubMsg = 12`, `omnibuf.v1.SchemaMeta`) is present, its field names
48
48
  * (and any nested children schemas) are used to decode human-readable keys.
49
49
  * Otherwise, synthetic field names (`"field_1"`, `"field_2"`, ...) are used.
50
50
  */
@@ -1 +1 @@
1
- {"version":3,"file":"blind.d.ts","sourceRoot":"","sources":["../src/blind.ts"],"names":[],"mappings":"AAKA,OAA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AAe,CAC7B,IAAI,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,EACnC,IAAI,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,GAClC,UAAU,GAAG,IAAI,CAuCnB;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,SAAI,EAAE,KAAK,SAAkB,GAAG,UAAU,CAmHjG;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,UAAU,GAAG,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC,CAe/F;AAED,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;OAEG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;OAEG;IACH,eAAe,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;CACrC;AAsJD;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,UAAU,EAClB,OAAO,CAAC,EAAE,kBAAkB,GAC3B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAGzB;AAypBD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,EACd,MAAM,CAAC,EAAE,MAAM,EACf,mBAAmB,UAAQ,GAC1B,MAAM,CAmFR;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,UAAU,EAClB,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,mBAAmB,CAAC,EAAE,OAAO,CAAA;CAAE,GAC3D,MAAM,CAGR"}
package/dist/blind.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // ============================================================================
2
2
  // Field #0 (omnibuf.v1.SchemaMeta) & Schema-Less Inspection (DecodeBlind)
3
3
  // ============================================================================
4
- import { validateMapTypes, validateValue } from './validation.js';
4
+ import { validateMapTypes, validateMessage, validateValue } from './validation.js';
5
5
  import { Encoder } from './encoder.js';
6
6
  import { appendBoolArrBody, appendSintArrAutoBody, appendStringArrBody, appendUintArrAutoBody, countSubMsgArrElems, } from './readers.js';
7
7
  import { MaxNestingDepth, WireArray, WireBoolFalse, WireBoolTrue, WireBytes, WireExtended, WireFixed32, WireFixed64, WireFloat32, WireFloat64, WireMap, WireSfixed32, WireSfixed64, WireSint, WireString, WireSubMsg, WireUint, decodeUTF8, decodeVarintAuto, decodeVarintBigInt, decodeZigZagAuto, float32FromBits, float64FromBits, nextContainerField, parseArrayHeader, parseMapHeader, readLenSlice, roundFloat32Shortest, skipValue, } from './wire.js';
@@ -81,6 +81,11 @@ function writeSchemaMetaBody(enc, meta, deterministic) {
81
81
  if (meta.fields) {
82
82
  const keys = Object.keys(meta.fields).map(Number);
83
83
  if (keys.length > 0) {
84
+ for (const k of keys) {
85
+ if (!Number.isInteger(k) || k < 1 || k > 536870911) {
86
+ throw new Error(`omnibuf: invalid SchemaMeta field number ${k}`);
87
+ }
88
+ }
84
89
  if (deterministic) {
85
90
  keys.sort((a, b) => a - b);
86
91
  }
@@ -101,6 +106,11 @@ function writeSchemaMetaBody(enc, meta, deterministic) {
101
106
  if (meta.children) {
102
107
  const keys = Object.keys(meta.children).map(Number);
103
108
  if (keys.length > 0) {
109
+ for (const k of keys) {
110
+ if (!Number.isInteger(k) || k < 1 || k > 536870911) {
111
+ throw new Error(`omnibuf: invalid SchemaMeta child field number ${k}`);
112
+ }
113
+ }
104
114
  if (deterministic) {
105
115
  keys.sort((a, b) => a - b);
106
116
  }
@@ -141,20 +151,57 @@ export function encodeField0SchemaMeta(meta, deterministic = true) {
141
151
  writeField0SchemaMeta(enc, meta, deterministic);
142
152
  return enc.finish();
143
153
  }
154
+ function hasAnyOwnKey(obj) {
155
+ if (!obj)
156
+ return false;
157
+ for (const _ in obj) {
158
+ if (Object.hasOwn(obj, _))
159
+ return true;
160
+ }
161
+ return false;
162
+ }
163
+ function mergeSchemaMetaInPlace(target, next) {
164
+ if (next.typeName) {
165
+ target.typeName = next.typeName;
166
+ }
167
+ if (next.fields) {
168
+ if (!target.fields) {
169
+ target.fields = next.fields;
170
+ }
171
+ else {
172
+ Object.assign(target.fields, next.fields);
173
+ }
174
+ }
175
+ if (next.children) {
176
+ if (!target.children) {
177
+ target.children = next.children;
178
+ }
179
+ else {
180
+ for (const [k, c] of Object.entries(next.children)) {
181
+ const numK = Number(k);
182
+ const existing = target.children[numK];
183
+ target.children[numK] = existing ? mergeSchemaMetaInPlace(existing, c) : c;
184
+ }
185
+ }
186
+ }
187
+ return target;
188
+ }
144
189
  /**
145
190
  * Merges two `SchemaMeta` definitions (later fields and children override/extend earlier ones).
146
191
  */
147
192
  export function mergeSchemaMeta(base, next) {
148
193
  if (!next)
149
194
  return base ?? null;
195
+ const nextHasFields = hasAnyOwnKey(next.fields);
196
+ const nextHasChildren = hasAnyOwnKey(next.children);
150
197
  if (!base) {
151
198
  const cp = {};
152
199
  if (next.typeName)
153
200
  cp.typeName = next.typeName;
154
- if (next.fields && Object.keys(next.fields).length > 0) {
201
+ if (nextHasFields) {
155
202
  cp.fields = { ...next.fields };
156
203
  }
157
- if (next.children && Object.keys(next.children).length > 0) {
204
+ if (nextHasChildren) {
158
205
  cp.children = {};
159
206
  for (const [k, c] of Object.entries(next.children)) {
160
207
  const mergedChild = mergeSchemaMeta(null, c);
@@ -164,15 +211,18 @@ export function mergeSchemaMeta(base, next) {
164
211
  }
165
212
  return cp;
166
213
  }
214
+ if (!next.typeName && !nextHasFields && !nextHasChildren) {
215
+ return base;
216
+ }
167
217
  const out = {
168
218
  typeName: next.typeName || base.typeName,
169
219
  };
170
- if (base.fields || next.fields) {
220
+ if (base.fields || nextHasFields) {
171
221
  out.fields = { ...(base.fields ?? {}), ...(next.fields ?? {}) };
172
222
  }
173
- if (base.children || next.children) {
223
+ if (base.children || nextHasChildren) {
174
224
  out.children = { ...(base.children ?? {}) };
175
- if (next.children) {
225
+ if (nextHasChildren) {
176
226
  for (const [k, c] of Object.entries(next.children)) {
177
227
  const numK = Number(k);
178
228
  const mergedChild = mergeSchemaMeta(out.children[numK], c);
@@ -185,7 +235,7 @@ export function mergeSchemaMeta(base, next) {
185
235
  }
186
236
  export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
187
237
  if (depth > limit) {
188
- throw new Error(`omnibuf: SchemaMeta nesting depth ${depth} exceeds MaxNestingDepth (${MaxNestingDepth})`);
238
+ throw new Error(`omnibuf: SchemaMeta nesting depth ${depth} exceeds MaxNestingDepth (${limit})`);
189
239
  }
190
240
  const meta = {};
191
241
  let pos = 0;
@@ -212,6 +262,9 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
212
262
  if (wt !== WireMap) {
213
263
  throw new Error(`omnibuf: SchemaMeta field #2 (fields) must be WireMap, got ${wt}`);
214
264
  }
265
+ if (depth + 1 > limit) {
266
+ throw new Error(`omnibuf: SchemaMeta fields map nesting depth ${depth + 1} exceeds MaxNestingDepth (${limit})`);
267
+ }
215
268
  const [body, nextPos] = readLenSlice(data, pos, 3);
216
269
  pos = nextPos;
217
270
  const [keyWT, valWT, c, startP, ok] = parseMapHeader(body);
@@ -240,14 +293,16 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
240
293
  if (wt !== WireMap) {
241
294
  throw new Error(`omnibuf: SchemaMeta field #3 (children) must be WireMap, got ${wt}`);
242
295
  }
296
+ if (depth + 1 > limit) {
297
+ throw new Error(`omnibuf: SchemaMeta children map nesting depth ${depth + 1} exceeds MaxNestingDepth (${limit})`);
298
+ }
243
299
  const [body, nextPos] = readLenSlice(data, pos, 3);
244
300
  pos = nextPos;
245
301
  const [keyWT, valWT, c, startP, ok] = parseMapHeader(body);
246
302
  if (!ok || keyWT !== WireUint || valWT !== WireSubMsg) {
247
303
  throw new Error('omnibuf: invalid SchemaMeta children map header');
248
304
  }
249
- if (!meta.children)
250
- meta.children = {};
305
+ const mapChildren = {};
251
306
  let p = startP;
252
307
  for (let i = 0; i < c; i++) {
253
308
  const [k64, kn] = decodeVarintBigInt(body, p);
@@ -256,14 +311,21 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
256
311
  }
257
312
  p += kn;
258
313
  const [childBytes, nextP] = readLenSlice(body, p, 0);
259
- const childMeta = decodeSchemaMeta(childBytes, depth + 1, limit);
260
- const numK = Number(k64);
261
- meta.children[numK] = mergeSchemaMeta(meta.children[numK], childMeta) ?? childMeta;
314
+ const childMeta = decodeSchemaMeta(childBytes, depth + 2, limit);
315
+ // Duplicate key inside a single WireMap replaces prior entry (Spec 6.6).
316
+ mapChildren[Number(k64)] = childMeta;
262
317
  p = nextP;
263
318
  }
264
319
  if (p !== body.length) {
265
320
  throw new Error(`omnibuf: SchemaMeta children map trailing bytes (parsed ${p}, body ${body.length})`);
266
321
  }
322
+ // Merge across separate children WireMap fields (Spec 8.2).
323
+ if (!meta.children)
324
+ meta.children = {};
325
+ for (const [k, childMeta] of Object.entries(mapChildren)) {
326
+ const numK = Number(k);
327
+ meta.children[numK] = mergeSchemaMeta(meta.children[numK], childMeta) ?? childMeta;
328
+ }
267
329
  break;
268
330
  }
269
331
  default: {
@@ -278,7 +340,7 @@ export function decodeSchemaMeta(data, depth = 0, limit = MaxNestingDepth) {
278
340
  return meta;
279
341
  }
280
342
  /**
281
- * Decodes and merges all Field #0 (`Tag = 0x0B`, `WireSubMsg = 5`) SchemaMeta blocks in `packet`.
343
+ * Decodes and merges all Field #0 (`Tag = 0x19`, `WireSubMsg = 12`) SchemaMeta blocks in `packet`.
282
344
  */
283
345
  export function readField0SchemaMeta(packet) {
284
346
  let merged = null;
@@ -291,8 +353,8 @@ export function readField0SchemaMeta(packet) {
291
353
  return [merged, merged !== null];
292
354
  pos = nextPos;
293
355
  try {
294
- const m = decodeSchemaMeta(body, 0);
295
- merged = mergeSchemaMeta(merged, m);
356
+ const m = decodeSchemaMeta(body, 1);
357
+ merged = merged ? mergeSchemaMetaInPlace(merged, m) : m;
296
358
  }
297
359
  catch {
298
360
  return [null, false];
@@ -321,9 +383,106 @@ function blindChildSchema(schema, fieldNum) {
321
383
  : undefined;
322
384
  return wrapBlindSchema(child, parent);
323
385
  }
386
+ function unifyBlindShapes(a, b) {
387
+ if (!a)
388
+ return [b ?? null, true];
389
+ if (!b)
390
+ return [a, true];
391
+ if (a.wt !== b.wt)
392
+ return [null, false];
393
+ if (a.wt === WireMap) {
394
+ if (a.anyMap)
395
+ return [b, true];
396
+ if (b.anyMap)
397
+ return [a, true];
398
+ if (a.keyWT !== b.keyWT)
399
+ return [null, false];
400
+ }
401
+ if (a.wt !== WireArray && a.wt !== WireMap) {
402
+ return [a, true];
403
+ }
404
+ const [u, ok] = unifyBlindShapes(a.elem, b.elem);
405
+ if (!ok)
406
+ return [null, false];
407
+ return [{ wt: a.wt, keyWT: a.keyWT, elem: u }, true];
408
+ }
409
+ function parseBlindContainerShape(data, pos, wt, depth) {
410
+ if (depth + 1 > MaxNestingDepth) {
411
+ throw new Error(`omnibuf: container nesting depth ${depth + 1} exceeds MaxNestingDepth (${MaxNestingDepth})`);
412
+ }
413
+ if (wt === WireArray) {
414
+ const [body, nextPos] = readLenSlice(data, pos, 2);
415
+ const [elemWT, count, startP, ok] = parseArrayHeader(body);
416
+ if (!ok) {
417
+ throw new Error('omnibuf: invalid WireArray header');
418
+ }
419
+ if (elemWT !== WireArray && elemWT !== WireMap) {
420
+ return [{ wt: WireArray, elem: { wt: elemWT } }, nextPos];
421
+ }
422
+ if (count === 0) {
423
+ if (elemWT === WireArray) {
424
+ return [{ wt: WireArray, elem: { wt: WireArray } }, nextPos];
425
+ }
426
+ return [{ wt: WireArray, elem: { wt: WireMap, anyMap: true } }, nextPos];
427
+ }
428
+ let merged = null;
429
+ let p = startP;
430
+ for (let i = 0; i < count; i++) {
431
+ const [elemShape, nextP] = parseBlindContainerShape(body, p, elemWT, depth + 1);
432
+ const [u, compat] = unifyBlindShapes(merged, elemShape);
433
+ if (!compat) {
434
+ throw new Error('omnibuf: inconsistent nested container element shapes in WireArray');
435
+ }
436
+ merged = u;
437
+ p = nextP;
438
+ }
439
+ if (p !== body.length) {
440
+ throw new Error('omnibuf: WireArray count/length mismatch');
441
+ }
442
+ return [{ wt: WireArray, elem: merged }, nextPos];
443
+ }
444
+ const [body, nextPos] = readLenSlice(data, pos, 3);
445
+ const [keyWT, valWT, count, startP, ok] = parseMapHeader(body);
446
+ if (!ok) {
447
+ throw new Error(`omnibuf: invalid WireMap header at ${pos}`);
448
+ }
449
+ validateMapTypes(keyWT, valWT);
450
+ if (valWT !== WireArray && valWT !== WireMap) {
451
+ return [{ wt: WireMap, keyWT, elem: { wt: valWT } }, nextPos];
452
+ }
453
+ if (count === 0) {
454
+ if (valWT === WireArray) {
455
+ return [{ wt: WireMap, keyWT, elem: { wt: WireArray } }, nextPos];
456
+ }
457
+ return [{ wt: WireMap, keyWT, elem: { wt: WireMap, anyMap: true } }, nextPos];
458
+ }
459
+ let merged = null;
460
+ let p = startP;
461
+ for (let i = 0; i < count; i++) {
462
+ p = skipValue(body, p, keyWT);
463
+ const [valShape, nextP] = parseBlindContainerShape(body, p, valWT, depth + 1);
464
+ const [u, compat] = unifyBlindShapes(merged, valShape);
465
+ if (!compat) {
466
+ throw new Error('omnibuf: inconsistent nested container value shapes in WireMap');
467
+ }
468
+ merged = u;
469
+ p = nextP;
470
+ }
471
+ if (p !== body.length) {
472
+ throw new Error('omnibuf: WireMap count/length mismatch');
473
+ }
474
+ return [{ wt: WireMap, keyWT, elem: merged }, nextPos];
475
+ }
476
+ function parseBlindFieldShape(data, pos, wt, depth) {
477
+ const normWT = wt === WireBoolTrue ? WireBoolFalse : wt;
478
+ if (normWT !== WireArray && normWT !== WireMap) {
479
+ return { wt: normWT };
480
+ }
481
+ return parseBlindContainerShape(data, pos, normWT, depth)[0];
482
+ }
324
483
  /**
325
484
  * Decodes any OmniBuf packet without a compile-time schema.
326
- * If Field #0 (`Tag = 0x0B`, `WireSubMsg = 5`, `omnibuf.v1.SchemaMeta`) is present, its field names
485
+ * If Field #0 (`Tag = 0x19`, `WireSubMsg = 12`, `omnibuf.v1.SchemaMeta`) is present, its field names
327
486
  * (and any nested children schemas) are used to decode human-readable keys.
328
487
  * Otherwise, synthetic field names (`"field_1"`, `"field_2"`, ...) are used.
329
488
  */
@@ -359,7 +518,7 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
359
518
  const [f0Body, nextPos] = readLenSlice(data, scanPos, 0);
360
519
  scanPos = nextPos;
361
520
  const parsedMeta = decodeSchemaMeta(f0Body, depth + 1);
362
- localSchema = mergeSchemaMeta(localSchema, parsedMeta);
521
+ localSchema = localSchema ? mergeSchemaMetaInPlace(localSchema, parsedMeta) : parsedMeta;
363
522
  continue;
364
523
  }
365
524
  scanPos = skipValue(data, scanPos, wt);
@@ -385,18 +544,13 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
385
544
  throw new Error(`omnibuf: fields ${previousField} and ${fieldNum} share name ${name}`);
386
545
  }
387
546
  fieldNames.set(name, fieldNum);
388
- let shape = wt === WireBoolTrue ? WireBoolFalse : wt;
389
- if (wt === WireArray || wt === WireMap) {
390
- const [body] = readLenSlice(data, pos, wt === WireArray ? 2 : 3);
391
- shape |= body[0] << 8;
392
- if (wt === WireMap)
393
- shape |= body[1] << 16;
394
- }
547
+ const shape = parseBlindFieldShape(data, pos, wt, depth);
395
548
  const previousShape = fieldShapes.get(fieldNum);
396
- if (previousShape !== undefined && previousShape !== shape) {
549
+ const [unifiedShape, shapeOk] = unifyBlindShapes(previousShape, shape);
550
+ if (!shapeOk || !unifiedShape) {
397
551
  throw new Error(`omnibuf: field ${fieldNum} changes wire shape within one message`);
398
552
  }
399
- fieldShapes.set(fieldNum, shape);
553
+ fieldShapes.set(fieldNum, unifiedShape);
400
554
  if (!Object.hasOwn(out, name) && wt !== WireSubMsg)
401
555
  setOwn(out, name, undefined);
402
556
  switch (wt) {
@@ -438,6 +592,7 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
438
592
  }
439
593
  case WireSubMsg: {
440
594
  const [subBytes, nextPos] = readLenSlice(data, pos, 0);
595
+ validateMessage(subBytes, depth + 1);
441
596
  let chunks = subPackets.get(fieldNum);
442
597
  if (!chunks) {
443
598
  chunks = [];
@@ -455,7 +610,7 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
455
610
  }
456
611
  const childSchema = blindChildSchema(activeSchema, fieldNum);
457
612
  const prev = Array.isArray(out[name]) ? out[name] : [];
458
- out[name] = decodeArrayBodyBlind(prev, body, childSchema, depth, options);
613
+ out[name] = decodeArrayBodyBlind(prev, body, childSchema, depth + 1, options);
459
614
  pos = nextPos;
460
615
  break;
461
616
  }
@@ -470,7 +625,7 @@ function decodeBlindWithSchema(data, inherited, depth, options, target) {
470
625
  const m = prev && typeof prev === 'object' && !Array.isArray(prev) && !(prev instanceof Uint8Array)
471
626
  ? prev
472
627
  : {};
473
- out[name] = decodeMapBodyBlind(m, body, childSchema, depth, options);
628
+ out[name] = decodeMapBodyBlind(m, body, childSchema, depth + 1, options);
474
629
  pos = nextPos;
475
630
  break;
476
631
  }
@@ -1022,13 +1177,18 @@ export function stringifyBlindJSON(value, indent, fallbackOnNonFinite = false) {
1022
1177
  continue;
1023
1178
  parts.push(`${nextIndent}${JSON.stringify(k)}: ${serialize(v, nextIndent)}`);
1024
1179
  }
1180
+ if (parts.length === 0)
1181
+ return '{}';
1025
1182
  return `{\n${parts.join(',\n')}\n${currentIndent}}`;
1026
1183
  }
1027
1184
  return 'null';
1028
1185
  }
1029
1186
  const out = serialize(value, '');
1030
- if (hasNonFinite && fallbackOnNonFinite) {
1031
- return '{}';
1187
+ if (hasNonFinite) {
1188
+ if (fallbackOnNonFinite) {
1189
+ return '{}';
1190
+ }
1191
+ throw new Error('omnibuf: non-finite float (NaN or Infinity) cannot be represented in JSON');
1032
1192
  }
1033
1193
  return out;
1034
1194
  }
@@ -1037,7 +1197,7 @@ export function stringifyBlindJSON(value, indent, fallbackOnNonFinite = false) {
1037
1197
  */
1038
1198
  export function decodeBlindJSON(packet, options) {
1039
1199
  const m = decodeBlind(packet, { bytesAsBase64: true, roundFloat32: true });
1040
- return stringifyBlindJSON(m, options?.indent, options?.fallbackOnNonFinite ?? true);
1200
+ return stringifyBlindJSON(m, options?.indent, options?.fallbackOnNonFinite ?? false);
1041
1201
  }
1042
1202
  function setOwn(object, key, value) {
1043
1203
  Object.defineProperty(object, key, {