@marcuspuchalla/nachos 0.1.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 (100) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/LICENSE +674 -0
  3. package/README.md +345 -0
  4. package/dist/chunk-2FUTHZQQ.cjs +755 -0
  5. package/dist/chunk-2FUTHZQQ.cjs.map +1 -0
  6. package/dist/chunk-2HBCILJS.cjs +2034 -0
  7. package/dist/chunk-2HBCILJS.cjs.map +1 -0
  8. package/dist/chunk-7CFYWHS6.js +742 -0
  9. package/dist/chunk-7CFYWHS6.js.map +1 -0
  10. package/dist/chunk-PD72MVTX.cjs +160 -0
  11. package/dist/chunk-PD72MVTX.cjs.map +1 -0
  12. package/dist/chunk-ZDZ2B5PE.js +149 -0
  13. package/dist/chunk-ZDZ2B5PE.js.map +1 -0
  14. package/dist/chunk-ZRPJUEIZ.js +2020 -0
  15. package/dist/chunk-ZRPJUEIZ.js.map +1 -0
  16. package/dist/encoder/index.cjs +57 -0
  17. package/dist/encoder/index.cjs.map +1 -0
  18. package/dist/encoder/index.d.cts +72 -0
  19. package/dist/encoder/index.d.ts +72 -0
  20. package/dist/encoder/index.js +4 -0
  21. package/dist/encoder/index.js.map +1 -0
  22. package/dist/index.cjs +606 -0
  23. package/dist/index.cjs.map +1 -0
  24. package/dist/index.d.cts +494 -0
  25. package/dist/index.d.ts +494 -0
  26. package/dist/index.js +523 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/metafile-cjs.json +1 -0
  29. package/dist/metafile-esm.json +1 -0
  30. package/dist/parser/index.cjs +85 -0
  31. package/dist/parser/index.cjs.map +1 -0
  32. package/dist/parser/index.d.cts +72 -0
  33. package/dist/parser/index.d.ts +72 -0
  34. package/dist/parser/index.js +4 -0
  35. package/dist/parser/index.js.map +1 -0
  36. package/dist/types-DvNlfbKB.d.cts +301 -0
  37. package/dist/types-DvNlfbKB.d.ts +301 -0
  38. package/dist/useCborSimpleEncoder-ButVU988.d.cts +268 -0
  39. package/dist/useCborSimpleEncoder-TVxzNJ_9.d.ts +268 -0
  40. package/dist/useCborTag-B_iaShG6.d.ts +142 -0
  41. package/dist/useCborTag-BfTIV8HM.d.cts +142 -0
  42. package/package.json +102 -0
  43. package/src/__tests__/public-api.test.ts +326 -0
  44. package/src/encoder/__tests__/cbor-collection-encoder.test.ts +331 -0
  45. package/src/encoder/__tests__/cbor-integer-encoder.test.ts +283 -0
  46. package/src/encoder/__tests__/cbor-simple-encoder.test.ts +224 -0
  47. package/src/encoder/__tests__/cbor-string-encoder.test.ts +345 -0
  48. package/src/encoder/__tests__/cbor-tag-encoder.test.ts +565 -0
  49. package/src/encoder/composables/#useCborTagEncoder.ts# +158 -0
  50. package/src/encoder/composables/useCborCollectionEncoder.ts +424 -0
  51. package/src/encoder/composables/useCborEncoder.ts +203 -0
  52. package/src/encoder/composables/useCborIntegerEncoder.ts +188 -0
  53. package/src/encoder/composables/useCborSimpleEncoder.ts +266 -0
  54. package/src/encoder/composables/useCborStringEncoder.ts +266 -0
  55. package/src/encoder/composables/useCborTagEncoder.ts +158 -0
  56. package/src/encoder/index.ts +35 -0
  57. package/src/encoder/types.ts +88 -0
  58. package/src/encoder/utils.ts +80 -0
  59. package/src/index.ts +434 -0
  60. package/src/parser/__tests__/ast-tree-structure.test.ts +311 -0
  61. package/src/parser/__tests__/cbor-collection-errors.test.ts +296 -0
  62. package/src/parser/__tests__/cbor-collection.test.ts +369 -0
  63. package/src/parser/__tests__/cbor-deterministic-encoding.test.ts +432 -0
  64. package/src/parser/__tests__/cbor-diagnostic.test.ts +333 -0
  65. package/src/parser/__tests__/cbor-duplicate-keys.test.ts +235 -0
  66. package/src/parser/__tests__/cbor-float-errors.test.ts +222 -0
  67. package/src/parser/__tests__/cbor-float.test.ts +502 -0
  68. package/src/parser/__tests__/cbor-integer-errors.test.ts +139 -0
  69. package/src/parser/__tests__/cbor-integer.test.ts +200 -0
  70. package/src/parser/__tests__/cbor-map-duplicate-keys.test.ts +403 -0
  71. package/src/parser/__tests__/cbor-parser-errors.test.ts +126 -0
  72. package/src/parser/__tests__/cbor-security-dos-protection.test.ts +503 -0
  73. package/src/parser/__tests__/cbor-sequences.test.ts +150 -0
  74. package/src/parser/__tests__/cbor-source-map.test.ts +321 -0
  75. package/src/parser/__tests__/cbor-standard-tags.test.ts +340 -0
  76. package/src/parser/__tests__/cbor-string-errors.test.ts +227 -0
  77. package/src/parser/__tests__/cbor-string.test.ts +224 -0
  78. package/src/parser/__tests__/cbor-tag-advanced.test.ts +500 -0
  79. package/src/parser/__tests__/cbor-tag-errors.test.ts +447 -0
  80. package/src/parser/__tests__/cbor-tag-source-map.test.ts +360 -0
  81. package/src/parser/__tests__/cbor-tag.test.ts +684 -0
  82. package/src/parser/__tests__/extreme-edge-cases.test.ts +146 -0
  83. package/src/parser/__tests__/pathBuilder.test.ts +256 -0
  84. package/src/parser/__tests__/rfc-test-vectors.test.ts +607 -0
  85. package/src/parser/__tests__/security-limits.test.ts +248 -0
  86. package/src/parser/__tests__/utils-errors.test.ts +127 -0
  87. package/src/parser/composables/useCborCollection.ts +509 -0
  88. package/src/parser/composables/useCborDiagnostic.ts +381 -0
  89. package/src/parser/composables/useCborFloat.ts +256 -0
  90. package/src/parser/composables/useCborInteger.ts +114 -0
  91. package/src/parser/composables/useCborParser.ts +951 -0
  92. package/src/parser/composables/useCborString.ts +330 -0
  93. package/src/parser/composables/useCborStringTypes.ts +129 -0
  94. package/src/parser/composables/useCborTag.ts +739 -0
  95. package/src/parser/index.ts +56 -0
  96. package/src/parser/types.ts +371 -0
  97. package/src/parser/utils/pathBuilder.ts +259 -0
  98. package/src/parser/utils.ts +398 -0
  99. package/src/utils/__tests__/logger.test.ts +186 -0
  100. package/src/utils/logger.ts +96 -0
@@ -0,0 +1,114 @@
1
+ /**
2
+ * CBOR Integer Parser Composable
3
+ * Handles Major Types 0 (Unsigned) and 1 (Negative)
4
+ * Supports BigInt for 64-bit values outside Number.MAX_SAFE_INTEGER
5
+ */
6
+
7
+ import type { ParseResult, ParseOptions } from '../types'
8
+ import { hexToBytes, readByte, readUint, readBigUint, extractCborHeader, validateCanonicalInteger } from '../utils'
9
+
10
+ /**
11
+ * Composable for parsing CBOR integers (Major Types 0 and 1)
12
+ *
13
+ * @returns Object with parseInteger function
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const { parseInteger } = useCborInteger()
18
+ * const result = parseInteger('1864') // 100
19
+ * ```
20
+ */
21
+ export function useCborInteger() {
22
+ /**
23
+ * Parses CBOR integer (Major Type 0 or 1)
24
+ *
25
+ * @param hexString - CBOR hex string
26
+ * @param options - Parser options (optional)
27
+ * @returns Parsed integer value and bytes read
28
+ */
29
+ const parseInteger = (hexString: string, options?: ParseOptions): ParseResult => {
30
+ const buffer = hexToBytes(hexString)
31
+ const initialByte = readByte(buffer, 0)
32
+
33
+ const { majorType, additionalInfo } = extractCborHeader(initialByte)
34
+
35
+ // Get the raw value based on additional info
36
+ let rawValue: number | bigint
37
+ let bytesRead: number
38
+
39
+ if (additionalInfo < 24) {
40
+ // Direct encoding (0-23)
41
+ rawValue = additionalInfo
42
+ bytesRead = 1
43
+ } else if (additionalInfo === 24) {
44
+ // 1 byte follows
45
+ rawValue = readByte(buffer, 1)
46
+ bytesRead = 2
47
+ } else if (additionalInfo === 25) {
48
+ // 2 bytes follow
49
+ rawValue = readUint(buffer, 1, 2)
50
+ bytesRead = 3
51
+ } else if (additionalInfo === 26) {
52
+ // 4 bytes follow
53
+ rawValue = readUint(buffer, 1, 4)
54
+ bytesRead = 5
55
+ } else if (additionalInfo === 27) {
56
+ // 8 bytes follow - use BigInt for large values
57
+ const bigValue = readBigUint(buffer, 1, 8)
58
+
59
+ // Check if value fits in Number.MAX_SAFE_INTEGER
60
+ if (bigValue <= BigInt(Number.MAX_SAFE_INTEGER)) {
61
+ rawValue = Number(bigValue)
62
+ } else {
63
+ rawValue = bigValue
64
+ }
65
+ bytesRead = 9
66
+ } else {
67
+ throw new Error(`Invalid additional info: ${additionalInfo}`)
68
+ }
69
+
70
+ // Validate canonical encoding if requested
71
+ if (options?.validateCanonical) {
72
+ validateCanonicalInteger(rawValue, additionalInfo)
73
+ }
74
+
75
+ // Calculate final value based on major type
76
+ let finalValue: number | bigint
77
+
78
+ if (majorType === 0) {
79
+ // Unsigned integer
80
+ finalValue = rawValue
81
+ } else if (majorType === 1) {
82
+ // Negative integer: -1 - N
83
+ if (typeof rawValue === 'bigint') {
84
+ const negValue = -1n - rawValue
85
+ // Check if result fits in safe integer range
86
+ if (negValue >= BigInt(Number.MIN_SAFE_INTEGER) && negValue <= BigInt(Number.MAX_SAFE_INTEGER)) {
87
+ finalValue = Number(negValue)
88
+ } else {
89
+ finalValue = negValue
90
+ }
91
+ } else {
92
+ const negValue = -1 - rawValue
93
+ // Check if the negative value is still within safe integer range
94
+ if (negValue >= Number.MIN_SAFE_INTEGER) {
95
+ finalValue = negValue
96
+ } else {
97
+ // Convert to BigInt if outside safe range
98
+ finalValue = BigInt(negValue)
99
+ }
100
+ }
101
+ } else {
102
+ throw new Error(`Expected major type 0 or 1, got ${majorType}`)
103
+ }
104
+
105
+ return {
106
+ value: finalValue,
107
+ bytesRead
108
+ }
109
+ }
110
+
111
+ return {
112
+ parseInteger
113
+ }
114
+ }