@marcuspuchalla/nachos 0.1.3 → 0.2.0

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 (68) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/dist/{chunk-PTWN7K3Y.cjs → chunk-3Z45RBZP.cjs} +469 -244
  3. package/dist/chunk-3Z45RBZP.cjs.map +1 -0
  4. package/dist/{chunk-2MTLSQ7E.js → chunk-EDXZTSIA.js} +224 -166
  5. package/dist/chunk-EDXZTSIA.js.map +1 -0
  6. package/dist/{chunk-R62CQQNI.cjs → chunk-HMUA5KLG.cjs} +239 -181
  7. package/dist/chunk-HMUA5KLG.cjs.map +1 -0
  8. package/dist/{chunk-ZDZ2B5PE.js → chunk-JESIF5IF.js} +7 -3
  9. package/dist/chunk-JESIF5IF.js.map +1 -0
  10. package/dist/{chunk-5A5T56JB.js → chunk-LWNWC2O7.js} +442 -217
  11. package/dist/chunk-LWNWC2O7.js.map +1 -0
  12. package/dist/{chunk-PD72MVTX.cjs → chunk-P6A2OOIY.cjs} +7 -3
  13. package/dist/chunk-P6A2OOIY.cjs.map +1 -0
  14. package/dist/encoder/index.cjs +14 -14
  15. package/dist/encoder/index.d.cts +5 -4
  16. package/dist/encoder/index.d.ts +5 -4
  17. package/dist/encoder/index.js +2 -2
  18. package/dist/index.cjs +58 -39
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +40 -21
  21. package/dist/index.d.ts +40 -21
  22. package/dist/index.js +37 -17
  23. package/dist/index.js.map +1 -1
  24. package/dist/metafile-cjs.json +1 -1
  25. package/dist/metafile-esm.json +1 -1
  26. package/dist/parser/index.cjs +21 -21
  27. package/dist/parser/index.d.cts +4 -2
  28. package/dist/parser/index.d.ts +4 -2
  29. package/dist/parser/index.js +2 -2
  30. package/dist/{types-DvNlfbKB.d.cts → types-eG2qalpr.d.cts} +27 -1
  31. package/dist/{types-DvNlfbKB.d.ts → types-eG2qalpr.d.ts} +27 -1
  32. package/dist/{useCborSimpleEncoder-TVxzNJ_9.d.ts → useCborSimpleEncoder-CamvS-_N.d.ts} +7 -3
  33. package/dist/{useCborSimpleEncoder-ButVU988.d.cts → useCborSimpleEncoder-DXgPx62d.d.cts} +7 -3
  34. package/dist/{useCborTag-xV2Pz2VY.d.ts → useCborTag-D4d7xG3-.d.cts} +9 -4
  35. package/dist/{useCborTag-Cs1CZuXZ.d.cts → useCborTag-TYst1KR6.d.ts} +9 -4
  36. package/package.json +1 -1
  37. package/src/__tests__/audit-fixes.test.ts +141 -0
  38. package/src/__tests__/public-api.test.ts +153 -0
  39. package/src/__tests__/roundtrip.test.ts +5 -6
  40. package/src/encoder/__tests__/cbor-collection-encoder.test.ts +103 -5
  41. package/src/encoder/__tests__/cbor-encoder-errors.test.ts +40 -5
  42. package/src/encoder/__tests__/cbor-simple-encoder.test.ts +126 -0
  43. package/src/encoder/composables/useCborCollectionEncoder.ts +30 -26
  44. package/src/encoder/composables/useCborEncoder.ts +40 -0
  45. package/src/encoder/composables/useCborSimpleEncoder.ts +40 -9
  46. package/src/encoder/types.ts +9 -4
  47. package/src/encoder/utils.ts +33 -1
  48. package/src/index.ts +39 -20
  49. package/src/parser/__tests__/buffer-native-parsing.test.ts +338 -0
  50. package/src/parser/__tests__/cbor-map-duplicate-keys.test.ts +97 -7
  51. package/src/parser/__tests__/cbor-security-dos-protection.test.ts +164 -31
  52. package/src/parser/__tests__/cbor-standard-tags.test.ts +75 -7
  53. package/src/parser/__tests__/cbor-tag-reparse-fix.test.ts +268 -0
  54. package/src/parser/__tests__/utils-errors.test.ts +11 -3
  55. package/src/parser/composables/useCborCollection.ts +51 -45
  56. package/src/parser/composables/useCborDiagnostic.ts +28 -0
  57. package/src/parser/composables/useCborFloat.ts +2 -1
  58. package/src/parser/composables/useCborInteger.ts +24 -10
  59. package/src/parser/composables/useCborParser.ts +448 -208
  60. package/src/parser/composables/useCborTag.ts +53 -38
  61. package/src/parser/types.ts +32 -1
  62. package/src/parser/utils.ts +52 -0
  63. package/dist/chunk-2MTLSQ7E.js.map +0 -1
  64. package/dist/chunk-5A5T56JB.js.map +0 -1
  65. package/dist/chunk-PD72MVTX.cjs.map +0 -1
  66. package/dist/chunk-PTWN7K3Y.cjs.map +0 -1
  67. package/dist/chunk-R62CQQNI.cjs.map +0 -1
  68. package/dist/chunk-ZDZ2B5PE.js.map +0 -1
@@ -1,12 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var chunkPD72MVTX_cjs = require('./chunk-PD72MVTX.cjs');
3
+ var chunkP6A2OOIY_cjs = require('./chunk-P6A2OOIY.cjs');
4
4
 
5
5
  // src/encoder/types.ts
6
6
  var DEFAULT_ENCODE_OPTIONS = {
7
7
  canonical: false,
8
8
  allowIndefinite: true,
9
9
  rejectDuplicateKeys: false,
10
+ mapKeyOrder: "length-first",
10
11
  maxDepth: 64,
11
12
  maxOutputSize: 100 * 1024 * 1024
12
13
  // 100 MB
@@ -42,6 +43,20 @@ function compareBytes(a, b) {
42
43
  }
43
44
  return 0;
44
45
  }
46
+ function compareBytesLexicographic(a, b) {
47
+ const min = Math.min(a.length, b.length);
48
+ for (let i = 0; i < min; i++) {
49
+ const byteA = a[i];
50
+ const byteB = b[i];
51
+ if (byteA !== byteB) {
52
+ return byteA - byteB;
53
+ }
54
+ }
55
+ return a.length - b.length;
56
+ }
57
+ function compareMapKeys(a, b, order = "length-first") {
58
+ return order === "bytewise" ? compareBytesLexicographic(a, b) : compareBytes(a, b);
59
+ }
45
60
  function writeUint(value, bytes) {
46
61
  const result = new Uint8Array(bytes);
47
62
  for (let i = bytes - 1; i >= 0; i--) {
@@ -138,8 +153,8 @@ function useCborIntegerEncoder() {
138
153
  // src/encoder/composables/useCborStringEncoder.ts
139
154
  function useCborStringEncoder(globalOptions) {
140
155
  const options = { ...DEFAULT_ENCODE_OPTIONS, ...globalOptions };
141
- const { isCborByteString } = chunkPD72MVTX_cjs.useCborByteString();
142
- const { isCborTextString } = chunkPD72MVTX_cjs.useCborTextString();
156
+ const { isCborByteString } = chunkP6A2OOIY_cjs.useCborByteString();
157
+ const { isCborTextString } = chunkP6A2OOIY_cjs.useCborTextString();
143
158
  const encodeLengthHeader = (majorType, length) => {
144
159
  const baseValue = majorType << 5;
145
160
  if (length <= 23) {
@@ -158,7 +173,7 @@ function useCborStringEncoder(globalOptions) {
158
173
  }
159
174
  };
160
175
  const encodeByteString = (data, encodeOptions) => {
161
- const isIndefinite = isCborByteString(data) && data[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
176
+ const isIndefinite = isCborByteString(data) && data[chunkP6A2OOIY_cjs.INDEFINITE_SYMBOL] === true;
162
177
  if (encodeOptions?.indefinite || Array.isArray(data) || isIndefinite) {
163
178
  if (options.allowIndefinite === false) {
164
179
  throw new Error("Indefinite-length encoding is not allowed");
@@ -204,7 +219,7 @@ function useCborStringEncoder(globalOptions) {
204
219
  };
205
220
  };
206
221
  const encodeTextString = (text, encodeOptions) => {
207
- const isIndefinite = isCborTextString(text) && text[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
222
+ const isIndefinite = isCborTextString(text) && text[chunkP6A2OOIY_cjs.INDEFINITE_SYMBOL] === true;
208
223
  if (encodeOptions?.indefinite || isIndefinite) {
209
224
  if (options.allowIndefinite === false) {
210
225
  throw new Error("Indefinite-length encoding is not allowed");
@@ -261,13 +276,174 @@ function useCborStringEncoder(globalOptions) {
261
276
  };
262
277
  }
263
278
 
279
+ // src/encoder/composables/useCborSimpleEncoder.ts
280
+ function useCborSimpleEncoder(_globalOptions) {
281
+ const { encodeInteger } = useCborIntegerEncoder();
282
+ const encodeSimple = (value) => {
283
+ let byte;
284
+ if (value === false) {
285
+ byte = 244;
286
+ } else if (value === true) {
287
+ byte = 245;
288
+ } else if (value === null) {
289
+ byte = 246;
290
+ } else if (value === void 0) {
291
+ byte = 247;
292
+ } else {
293
+ throw new Error(`Unsupported simple value: ${value}`);
294
+ }
295
+ return {
296
+ bytes: new Uint8Array([byte]),
297
+ hex: bytesToHex(new Uint8Array([byte]))
298
+ };
299
+ };
300
+ const encodeFloat16Bytes = (value) => {
301
+ if (Object.is(value, -0)) return new Uint8Array([128, 0]);
302
+ if (value === 0) return new Uint8Array([0, 0]);
303
+ if (Number.isNaN(value)) return new Uint8Array([126, 0]);
304
+ if (value === Infinity) return new Uint8Array([124, 0]);
305
+ if (value === -Infinity) return new Uint8Array([252, 0]);
306
+ const sign = value < 0 ? 1 : 0;
307
+ const absValue = Math.abs(value);
308
+ const buffer = new ArrayBuffer(8);
309
+ const view = new DataView(buffer);
310
+ view.setFloat64(0, absValue, false);
311
+ const bits = view.getBigUint64(0, false);
312
+ const exp64 = Number(bits >> 52n & 0x7ffn) - 1023;
313
+ const mant64 = Number(bits & 0xfffffffffffffn);
314
+ const mant64n = BigInt(mant64);
315
+ let exp16;
316
+ let mant16;
317
+ if (exp64 < -24) {
318
+ exp16 = 0;
319
+ mant16 = 0;
320
+ } else if (exp64 < -14) {
321
+ exp16 = 0;
322
+ const shift = -14 - exp64;
323
+ const fullMant = 1n << 10n | mant64n >> 42n;
324
+ const lostFromExtract = mant64n & (1n << 42n) - 1n;
325
+ const shiftN = BigInt(shift);
326
+ const guardBit = Number(fullMant >> shiftN - 1n & 1n);
327
+ const stickyBitsBelow = shiftN > 1n ? fullMant & (1n << shiftN - 1n) - 1n : 0n;
328
+ const sticky = stickyBitsBelow !== 0n || lostFromExtract !== 0n ? 1 : 0;
329
+ const truncated = Number(fullMant >> shiftN);
330
+ const lsb = truncated & 1;
331
+ mant16 = truncated + (guardBit & (sticky | lsb));
332
+ } else if (exp64 > 15) {
333
+ exp16 = 31;
334
+ mant16 = 0;
335
+ } else {
336
+ exp16 = exp64 + 15;
337
+ const truncated = Number(mant64n >> 42n);
338
+ const guardBit = Number(mant64n >> 41n & 1n);
339
+ const stickyBits = (mant64n & (1n << 41n) - 1n) !== 0n ? 1 : 0;
340
+ const lsb = truncated & 1;
341
+ mant16 = truncated + (guardBit & (stickyBits | lsb));
342
+ if (mant16 > 1023) {
343
+ mant16 = 0;
344
+ exp16 += 1;
345
+ }
346
+ }
347
+ const float16 = sign << 15 | exp16 << 10 | mant16;
348
+ return new Uint8Array([
349
+ float16 >> 8 & 255,
350
+ float16 & 255
351
+ ]);
352
+ };
353
+ const encodeFloat32Bytes = (value) => {
354
+ const buffer = new ArrayBuffer(4);
355
+ const view = new DataView(buffer);
356
+ view.setFloat32(0, value, false);
357
+ return new Uint8Array(buffer);
358
+ };
359
+ const encodeFloat64Bytes = (value) => {
360
+ const buffer = new ArrayBuffer(8);
361
+ const view = new DataView(buffer);
362
+ view.setFloat64(0, value, false);
363
+ return new Uint8Array(buffer);
364
+ };
365
+ const canBeFloat16 = (value) => {
366
+ if (!Number.isFinite(value) || Object.is(value, 0) || Object.is(value, -0)) {
367
+ return true;
368
+ }
369
+ const absValue = Math.abs(value);
370
+ if (absValue < 5960464477539063e-23 || absValue > 65504) {
371
+ return false;
372
+ }
373
+ const bytes = encodeFloat16Bytes(value);
374
+ const view = new DataView(bytes.buffer);
375
+ const bits = view.getUint16(0, false);
376
+ const sign = bits >> 15 & 1;
377
+ const exp = bits >> 10 & 31;
378
+ const mant = bits & 1023;
379
+ let decoded;
380
+ if (exp === 0) {
381
+ decoded = (sign ? -1 : 1) * Math.pow(2, -14) * (mant / 1024);
382
+ } else if (exp === 31) {
383
+ decoded = mant === 0 ? sign ? -Infinity : Infinity : NaN;
384
+ } else {
385
+ decoded = (sign ? -1 : 1) * Math.pow(2, exp - 15) * (1 + mant / 1024);
386
+ }
387
+ return decoded === value;
388
+ };
389
+ const canBeFloat32 = (value) => {
390
+ if (!Number.isFinite(value) || Object.is(value, 0) || Object.is(value, -0)) {
391
+ return true;
392
+ }
393
+ const buffer = new ArrayBuffer(4);
394
+ const view = new DataView(buffer);
395
+ view.setFloat32(0, value, false);
396
+ const decoded = view.getFloat32(0, false);
397
+ return decoded === value;
398
+ };
399
+ const isInteger = (value) => {
400
+ return Number.isInteger(value) && Number.isSafeInteger(value);
401
+ };
402
+ const encodeFloat = (value, precision) => {
403
+ let bytes;
404
+ if (isInteger(value) && !precision) {
405
+ return encodeInteger(value);
406
+ }
407
+ if (precision === 16) {
408
+ const floatBytes = encodeFloat16Bytes(value);
409
+ bytes = new Uint8Array([249, ...floatBytes]);
410
+ } else if (precision === 32) {
411
+ const floatBytes = encodeFloat32Bytes(value);
412
+ bytes = new Uint8Array([250, ...floatBytes]);
413
+ } else if (precision === 64) {
414
+ const floatBytes = encodeFloat64Bytes(value);
415
+ bytes = new Uint8Array([251, ...floatBytes]);
416
+ } else {
417
+ if (canBeFloat16(value)) {
418
+ const floatBytes = encodeFloat16Bytes(value);
419
+ bytes = new Uint8Array([249, ...floatBytes]);
420
+ } else if (canBeFloat32(value)) {
421
+ const floatBytes = encodeFloat32Bytes(value);
422
+ bytes = new Uint8Array([250, ...floatBytes]);
423
+ } else {
424
+ const floatBytes = encodeFloat64Bytes(value);
425
+ bytes = new Uint8Array([251, ...floatBytes]);
426
+ }
427
+ }
428
+ return {
429
+ bytes,
430
+ hex: bytesToHex(bytes)
431
+ };
432
+ };
433
+ return {
434
+ encodeSimple,
435
+ encodeFloat
436
+ };
437
+ }
438
+
264
439
  // src/encoder/composables/useCborCollectionEncoder.ts
265
440
  function useCborCollectionEncoder(globalOptions) {
266
441
  const options = { ...DEFAULT_ENCODE_OPTIONS, ...globalOptions };
267
442
  const { encodeInteger } = useCborIntegerEncoder();
268
443
  const { encodeTextString, encodeByteString } = useCborStringEncoder(globalOptions);
269
- const { isCborByteString } = chunkPD72MVTX_cjs.useCborByteString();
270
- const { isCborTextString } = chunkPD72MVTX_cjs.useCborTextString();
444
+ const { encodeFloat } = useCborSimpleEncoder();
445
+ const { isCborByteString } = chunkP6A2OOIY_cjs.useCborByteString();
446
+ const { isCborTextString } = chunkP6A2OOIY_cjs.useCborTextString();
271
447
  let mainEncode = null;
272
448
  const setMainEncode = (encodeFn) => {
273
449
  mainEncode = encodeFn;
@@ -280,14 +456,22 @@ function useCborCollectionEncoder(globalOptions) {
280
456
  return new Uint8Array([246]);
281
457
  } else if (typeof value === "boolean") {
282
458
  return new Uint8Array([value ? 245 : 244]);
283
- } else if (typeof value === "number" || typeof value === "bigint") {
459
+ } else if (typeof value === "number") {
460
+ if (Object.is(value, -0)) {
461
+ return encodeFloat(value, 16).bytes;
462
+ }
463
+ if (Number.isInteger(value) && Number.isSafeInteger(value)) {
464
+ return encodeInteger(value).bytes;
465
+ }
466
+ return encodeFloat(value).bytes;
467
+ } else if (typeof value === "bigint") {
284
468
  return encodeInteger(value).bytes;
285
469
  } else if (typeof value === "string" || isCborTextString(value)) {
286
470
  return encodeTextString(value).bytes;
287
471
  } else if (value instanceof Uint8Array || isCborByteString(value)) {
288
472
  return encodeByteString(value).bytes;
289
473
  } else if (Array.isArray(value)) {
290
- const isIndefinite = value[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
474
+ const isIndefinite = value[chunkP6A2OOIY_cjs.INDEFINITE_SYMBOL] === true;
291
475
  const newCtx = { ...ctx, depth: ctx.depth + 1 };
292
476
  if (isIndefinite) {
293
477
  if (ctx.options.allowIndefinite === false) {
@@ -309,7 +493,7 @@ function useCborCollectionEncoder(globalOptions) {
309
493
  }
310
494
  throw new Error("Tagged value encoding requires main encoder to be set");
311
495
  } else if (value instanceof Map || typeof value === "object" && value !== null) {
312
- const isIndefinite = value[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
496
+ const isIndefinite = value[chunkP6A2OOIY_cjs.INDEFINITE_SYMBOL] === true;
313
497
  const newCtx = { ...ctx, depth: ctx.depth + 1 };
314
498
  if (isIndefinite) {
315
499
  if (ctx.options.allowIndefinite === false) {
@@ -360,17 +544,13 @@ function useCborCollectionEncoder(globalOptions) {
360
544
  parts.push(encoded);
361
545
  }
362
546
  const result = concatenateUint8Arrays(parts);
363
- ctx.bytesWritten += result.length;
364
- if (ctx.bytesWritten > ctx.options.maxOutputSize) {
365
- throw new Error("Encoded output exceeds maximum size");
366
- }
367
547
  return {
368
548
  bytes: result,
369
549
  hex: bytesToHex(result)
370
550
  };
371
551
  };
372
552
  const encodeArray = (array, encodeOptions) => {
373
- const isIndefinite = array[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
553
+ const isIndefinite = array[chunkP6A2OOIY_cjs.INDEFINITE_SYMBOL] === true;
374
554
  if (encodeOptions?.indefinite || isIndefinite) {
375
555
  if (options.allowIndefinite === false) {
376
556
  throw new Error("Indefinite-length encoding is not allowed");
@@ -382,7 +562,6 @@ function useCborCollectionEncoder(globalOptions) {
382
562
  }
383
563
  const ctx = {
384
564
  depth: 0,
385
- bytesWritten: 0,
386
565
  options
387
566
  };
388
567
  return encodeArrayInternal(array, ctx);
@@ -397,7 +576,6 @@ function useCborCollectionEncoder(globalOptions) {
397
576
  const parts = [new Uint8Array([159])];
398
577
  const ctx = {
399
578
  depth: 0,
400
- bytesWritten: 0,
401
579
  options
402
580
  };
403
581
  for (const item of array) {
@@ -413,22 +591,27 @@ function useCborCollectionEncoder(globalOptions) {
413
591
  };
414
592
  const encodeMapInternal = (map, ctx) => {
415
593
  let entries;
416
- if (map[chunkPD72MVTX_cjs.ALL_ENTRIES_SYMBOL]) {
417
- entries = map[chunkPD72MVTX_cjs.ALL_ENTRIES_SYMBOL];
594
+ if (map[chunkP6A2OOIY_cjs.ALL_ENTRIES_SYMBOL]) {
595
+ entries = map[chunkP6A2OOIY_cjs.ALL_ENTRIES_SYMBOL];
418
596
  } else {
419
597
  entries = map instanceof Map ? Array.from(map.entries()) : Object.entries(map);
420
598
  }
421
- if (ctx.options.canonical && !map[chunkPD72MVTX_cjs.ALL_ENTRIES_SYMBOL]) {
422
- entries.sort((a, b) => {
423
- const keyA = encodeValue(a[0], { ...ctx, depth: ctx.depth + 1 });
424
- const keyB = encodeValue(b[0], { ...ctx, depth: ctx.depth + 1 });
425
- return compareBytes(keyA, keyB);
426
- });
599
+ let preEncodedKeys = null;
600
+ if (ctx.options.canonical && !map[chunkP6A2OOIY_cjs.ALL_ENTRIES_SYMBOL]) {
601
+ const childCtx = { ...ctx, depth: ctx.depth + 1 };
602
+ const withEncodedKeys = entries.map((entry) => ({
603
+ encodedKey: encodeValue(entry[0], childCtx),
604
+ entry
605
+ }));
606
+ const keyOrder = ctx.options.mapKeyOrder ?? "length-first";
607
+ withEncodedKeys.sort((a, b) => compareMapKeys(a.encodedKey, b.encodedKey, keyOrder));
608
+ entries = withEncodedKeys.map((t) => t.entry);
609
+ preEncodedKeys = withEncodedKeys.map((t) => t.encodedKey);
427
610
  }
428
611
  if (ctx.options.rejectDuplicateKeys) {
429
612
  const seen = /* @__PURE__ */ new Set();
430
- for (const [key] of entries) {
431
- const keyBytes = encodeValue(key, { ...ctx, depth: ctx.depth + 1, bytesWritten: 0 });
613
+ const keysForDupCheck = preEncodedKeys ?? entries.map((e) => encodeValue(e[0], { ...ctx, depth: ctx.depth + 1 }));
614
+ for (const keyBytes of keysForDupCheck) {
432
615
  const keyHex = bytesToHex(keyBytes);
433
616
  if (seen.has(keyHex)) {
434
617
  throw new Error("Duplicate map key detected");
@@ -445,17 +628,13 @@ function useCborCollectionEncoder(globalOptions) {
445
628
  parts.push(encodedValue);
446
629
  }
447
630
  const result = concatenateUint8Arrays(parts);
448
- ctx.bytesWritten += result.length;
449
- if (ctx.bytesWritten > ctx.options.maxOutputSize) {
450
- throw new Error("Encoded output exceeds maximum size");
451
- }
452
631
  return {
453
632
  bytes: result,
454
633
  hex: bytesToHex(result)
455
634
  };
456
635
  };
457
636
  const encodeMap = (map, encodeOptions) => {
458
- const isIndefinite = map[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
637
+ const isIndefinite = map[chunkP6A2OOIY_cjs.INDEFINITE_SYMBOL] === true;
459
638
  if (encodeOptions?.indefinite || isIndefinite) {
460
639
  if (options.allowIndefinite === false) {
461
640
  throw new Error("Indefinite-length encoding is not allowed");
@@ -467,7 +646,6 @@ function useCborCollectionEncoder(globalOptions) {
467
646
  }
468
647
  const ctx = {
469
648
  depth: 0,
470
- bytesWritten: 0,
471
649
  options
472
650
  };
473
651
  return encodeMapInternal(map, ctx);
@@ -483,7 +661,6 @@ function useCborCollectionEncoder(globalOptions) {
483
661
  const parts = [new Uint8Array([191])];
484
662
  const ctx = {
485
663
  depth: 0,
486
- bytesWritten: 0,
487
664
  options
488
665
  };
489
666
  for (const [key, value] of entries) {
@@ -508,149 +685,6 @@ function useCborCollectionEncoder(globalOptions) {
508
685
  };
509
686
  }
510
687
 
511
- // src/encoder/composables/useCborSimpleEncoder.ts
512
- function useCborSimpleEncoder(_globalOptions) {
513
- const { encodeInteger } = useCborIntegerEncoder();
514
- const encodeSimple = (value) => {
515
- let byte;
516
- if (value === false) {
517
- byte = 244;
518
- } else if (value === true) {
519
- byte = 245;
520
- } else if (value === null) {
521
- byte = 246;
522
- } else if (value === void 0) {
523
- byte = 247;
524
- } else {
525
- throw new Error(`Unsupported simple value: ${value}`);
526
- }
527
- return {
528
- bytes: new Uint8Array([byte]),
529
- hex: bytesToHex(new Uint8Array([byte]))
530
- };
531
- };
532
- const encodeFloat16Bytes = (value) => {
533
- if (value === 0) return new Uint8Array([0, 0]);
534
- if (value === -0) return new Uint8Array([128, 0]);
535
- if (Number.isNaN(value)) return new Uint8Array([126, 0]);
536
- if (value === Infinity) return new Uint8Array([124, 0]);
537
- if (value === -Infinity) return new Uint8Array([252, 0]);
538
- const sign = value < 0 ? 1 : 0;
539
- const absValue = Math.abs(value);
540
- const buffer = new ArrayBuffer(8);
541
- const view = new DataView(buffer);
542
- view.setFloat64(0, absValue, false);
543
- const bits = view.getBigUint64(0, false);
544
- const exp64 = Number(bits >> 52n & 0x7ffn) - 1023;
545
- const mant64 = Number(bits & 0xfffffffffffffn);
546
- let exp16;
547
- let mant16;
548
- if (exp64 < -24) {
549
- exp16 = 0;
550
- mant16 = 0;
551
- } else if (exp64 < -14) {
552
- exp16 = 0;
553
- const shift = -14 - exp64;
554
- mant16 = (1 << 10 | mant64 >> 42) + ((1 << shift - 1) - 1) >> shift;
555
- } else if (exp64 > 15) {
556
- exp16 = 31;
557
- mant16 = 0;
558
- } else {
559
- exp16 = exp64 + 15;
560
- mant16 = mant64 >> 42;
561
- }
562
- const float16 = sign << 15 | exp16 << 10 | mant16;
563
- return new Uint8Array([
564
- float16 >> 8 & 255,
565
- float16 & 255
566
- ]);
567
- };
568
- const encodeFloat32Bytes = (value) => {
569
- const buffer = new ArrayBuffer(4);
570
- const view = new DataView(buffer);
571
- view.setFloat32(0, value, false);
572
- return new Uint8Array(buffer);
573
- };
574
- const encodeFloat64Bytes = (value) => {
575
- const buffer = new ArrayBuffer(8);
576
- const view = new DataView(buffer);
577
- view.setFloat64(0, value, false);
578
- return new Uint8Array(buffer);
579
- };
580
- const canBeFloat16 = (value) => {
581
- if (!Number.isFinite(value) || value === 0 || value === -0) {
582
- return true;
583
- }
584
- const absValue = Math.abs(value);
585
- if (absValue < 6103515625e-14 || absValue > 65504) {
586
- return false;
587
- }
588
- const bytes = encodeFloat16Bytes(value);
589
- const view = new DataView(bytes.buffer);
590
- const bits = view.getUint16(0, false);
591
- const sign = bits >> 15 & 1;
592
- const exp = bits >> 10 & 31;
593
- const mant = bits & 1023;
594
- let decoded;
595
- if (exp === 0) {
596
- decoded = (sign ? -1 : 1) * Math.pow(2, -14) * (mant / 1024);
597
- } else if (exp === 31) {
598
- decoded = mant === 0 ? sign ? -Infinity : Infinity : NaN;
599
- } else {
600
- decoded = (sign ? -1 : 1) * Math.pow(2, exp - 15) * (1 + mant / 1024);
601
- }
602
- return decoded === value;
603
- };
604
- const canBeFloat32 = (value) => {
605
- if (!Number.isFinite(value) || value === 0 || value === -0) {
606
- return true;
607
- }
608
- const buffer = new ArrayBuffer(4);
609
- const view = new DataView(buffer);
610
- view.setFloat32(0, value, false);
611
- const decoded = view.getFloat32(0, false);
612
- return decoded === value;
613
- };
614
- const isInteger = (value) => {
615
- return Number.isInteger(value) && Number.isSafeInteger(value);
616
- };
617
- const encodeFloat = (value, precision) => {
618
- let bytes;
619
- if (isInteger(value) && !precision) {
620
- return encodeInteger(value);
621
- }
622
- if (precision === 16) {
623
- const floatBytes = encodeFloat16Bytes(value);
624
- bytes = new Uint8Array([249, ...floatBytes]);
625
- } else if (precision === 32) {
626
- const floatBytes = encodeFloat32Bytes(value);
627
- bytes = new Uint8Array([250, ...floatBytes]);
628
- } else if (precision === 64) {
629
- const floatBytes = encodeFloat64Bytes(value);
630
- bytes = new Uint8Array([251, ...floatBytes]);
631
- } else {
632
- if (canBeFloat16(value)) {
633
- const floatBytes = encodeFloat16Bytes(value);
634
- bytes = new Uint8Array([249, ...floatBytes]);
635
- } else if (canBeFloat32(value)) {
636
- const floatBytes = encodeFloat32Bytes(value);
637
- bytes = new Uint8Array([250, ...floatBytes]);
638
- } else {
639
- const floatBytes = encodeFloat64Bytes(value);
640
- bytes = new Uint8Array([251, ...floatBytes]);
641
- }
642
- }
643
- return {
644
- bytes,
645
- hex: bytesToHex(bytes)
646
- };
647
- };
648
- return {
649
- encodeSimple,
650
- encodeFloat
651
- };
652
- }
653
-
654
688
  // src/encoder/composables/useCborTagEncoder.ts
655
689
  function useCborTagEncoder() {
656
690
  const encodeTagNumber = (tagNumber) => {
@@ -712,13 +746,37 @@ function useCborEncoder(globalOptions) {
712
746
  const { encodeArray, encodeMap, setMainEncode } = useCborCollectionEncoder(options);
713
747
  const { encodeSimple, encodeFloat } = useCborSimpleEncoder();
714
748
  const { encodeTaggedValue } = useCborTagEncoder();
715
- const { isCborByteString } = chunkPD72MVTX_cjs.useCborByteString();
716
- const { isCborTextString } = chunkPD72MVTX_cjs.useCborTextString();
749
+ const { isCborByteString } = chunkP6A2OOIY_cjs.useCborByteString();
750
+ const { isCborTextString } = chunkP6A2OOIY_cjs.useCborTextString();
717
751
  const encode = (value) => {
752
+ const result = encodeValue(value);
753
+ if (options.maxOutputSize && result.bytes.length > options.maxOutputSize) {
754
+ throw new Error(
755
+ `Encoded output size ${result.bytes.length} bytes exceeds limit of ${options.maxOutputSize} bytes`
756
+ );
757
+ }
758
+ return result;
759
+ };
760
+ let currentDepth = 0;
761
+ const encodeValue = (value) => {
762
+ if (currentDepth > options.maxDepth) {
763
+ throw new Error(`Maximum nesting depth ${options.maxDepth} exceeded`);
764
+ }
765
+ currentDepth++;
766
+ try {
767
+ return encodeValueInner(value);
768
+ } finally {
769
+ currentDepth--;
770
+ }
771
+ };
772
+ const encodeValueInner = (value) => {
718
773
  if (value === null || value === void 0 || typeof value === "boolean") {
719
774
  return encodeSimple(value);
720
775
  }
721
776
  if (typeof value === "number") {
777
+ if (Object.is(value, -0)) {
778
+ return encodeFloat(value, 16);
779
+ }
722
780
  if (Number.isSafeInteger(value)) {
723
781
  return encodeInteger(value);
724
782
  }
@@ -799,5 +857,5 @@ exports.useCborStringEncoder = useCborStringEncoder;
799
857
  exports.useCborTagEncoder = useCborTagEncoder;
800
858
  exports.writeBigUint = writeBigUint;
801
859
  exports.writeUint = writeUint;
802
- //# sourceMappingURL=chunk-R62CQQNI.cjs.map
803
- //# sourceMappingURL=chunk-R62CQQNI.cjs.map
860
+ //# sourceMappingURL=chunk-HMUA5KLG.cjs.map
861
+ //# sourceMappingURL=chunk-HMUA5KLG.cjs.map