@marcuspuchalla/nachos 0.1.1 → 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.
Files changed (62) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/dist/{chunk-ZRPJUEIZ.js → chunk-5IWW5H47.js} +546 -227
  3. package/dist/chunk-5IWW5H47.js.map +1 -0
  4. package/dist/{chunk-2HBCILJS.cjs → chunk-RVG2BY32.cjs} +545 -226
  5. package/dist/chunk-RVG2BY32.cjs.map +1 -0
  6. package/dist/{chunk-2FUTHZQQ.cjs → chunk-S4RXO6IB.cjs} +244 -166
  7. package/dist/chunk-S4RXO6IB.cjs.map +1 -0
  8. package/dist/{chunk-7CFYWHS6.js → chunk-UMAX5MX5.js} +244 -166
  9. package/dist/chunk-UMAX5MX5.js.map +1 -0
  10. package/dist/encoder/index.cjs +13 -13
  11. package/dist/encoder/index.d.cts +2 -2
  12. package/dist/encoder/index.d.ts +2 -2
  13. package/dist/encoder/index.js +1 -1
  14. package/dist/index.cjs +32 -32
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +28 -19
  17. package/dist/index.d.ts +28 -19
  18. package/dist/index.js +16 -16
  19. package/dist/index.js.map +1 -1
  20. package/dist/metafile-cjs.json +1 -1
  21. package/dist/metafile-esm.json +1 -1
  22. package/dist/parser/index.cjs +14 -14
  23. package/dist/parser/index.d.cts +3 -1
  24. package/dist/parser/index.d.ts +3 -1
  25. package/dist/parser/index.js +1 -1
  26. package/dist/{useCborSimpleEncoder-TVxzNJ_9.d.ts → useCborSimpleEncoder-BoKEmjP9.d.ts} +0 -2
  27. package/dist/{useCborSimpleEncoder-ButVU988.d.cts → useCborSimpleEncoder-C_OHxoB8.d.cts} +0 -2
  28. package/dist/{useCborTag-B_iaShG6.d.ts → useCborTag-BD6Sqp7p.d.ts} +11 -6
  29. package/dist/{useCborTag-BfTIV8HM.d.cts → useCborTag-QpZR-Er2.d.cts} +11 -6
  30. package/package.json +1 -1
  31. package/src/__tests__/public-api.test.ts +153 -0
  32. package/src/__tests__/roundtrip.test.ts +701 -0
  33. package/src/encoder/__tests__/cbor-collection-encoder.test.ts +129 -5
  34. package/src/encoder/__tests__/cbor-encoder-errors.test.ts +847 -0
  35. package/src/encoder/__tests__/cbor-simple-encoder.test.ts +126 -0
  36. package/src/encoder/__tests__/cbor-string-encoder.test.ts +14 -0
  37. package/src/encoder/composables/useCborCollectionEncoder.ts +56 -23
  38. package/src/encoder/composables/useCborEncoder.ts +27 -1
  39. package/src/encoder/composables/useCborSimpleEncoder.ts +40 -8
  40. package/src/encoder/composables/useCborStringEncoder.ts +23 -10
  41. package/src/encoder/types.ts +0 -2
  42. package/src/index.ts +29 -20
  43. package/src/parser/__tests__/buffer-native-parsing.test.ts +338 -0
  44. package/src/parser/__tests__/cbor-float-errors.test.ts +41 -0
  45. package/src/parser/__tests__/cbor-map-duplicate-keys.test.ts +97 -7
  46. package/src/parser/__tests__/cbor-security-dos-protection.test.ts +166 -33
  47. package/src/parser/__tests__/cbor-standard-tags.test.ts +104 -7
  48. package/src/parser/__tests__/cbor-string-errors.test.ts +4 -4
  49. package/src/parser/__tests__/cbor-tag-errors.test.ts +1 -1
  50. package/src/parser/__tests__/cbor-tag-reparse-fix.test.ts +268 -0
  51. package/src/parser/composables/useCborCollection.ts +45 -42
  52. package/src/parser/composables/useCborFloat.ts +95 -9
  53. package/src/parser/composables/useCborInteger.ts +24 -10
  54. package/src/parser/composables/useCborParser.ts +387 -216
  55. package/src/parser/composables/useCborString.ts +22 -4
  56. package/src/parser/composables/useCborTag.ts +149 -53
  57. package/src/parser/utils.ts +11 -0
  58. package/dist/chunk-2FUTHZQQ.cjs.map +0 -1
  59. package/dist/chunk-2HBCILJS.cjs.map +0 -1
  60. package/dist/chunk-7CFYWHS6.js.map +0 -1
  61. package/dist/chunk-ZRPJUEIZ.js.map +0 -1
  62. package/src/encoder/composables/#useCborTagEncoder.ts# +0 -158
@@ -1,158 +0,0 @@
1
- /**
2
- * CBOR Tag Encoder Composable
3
- * Handles Major Type 6 (Semantic Tags)
4
- * Following RFC 8949 specification
5
- */
6
-
7
- import type { EncodeResult, TaggedValue, EncodableValue } from '../types'
8
- import { bytesToHex, writeUint, writeBigUint } from '../utils'
9
-
10
- /**
11
- * CBOR Tag Encoder Composable
12
- *
13
- * Provides functions to encode tagged values to CBOR format:
14
- * - Major Type 6: Semantic tags (0 to 2^64-1)
15
- *
16
- * Tags provide semantic meaning to CBOR values:
17
- * - Tag 0: Date/time string (RFC 3339)
18
- * - Tag 1: Epoch timestamp
19
- * - Tag 2: Positive bignum
20
- * - Tag 3: Negative bignum
21
- * - Tag 258: Cardano set (CIP-0005)
22
- * - And many more...
23
- *
24
- * @example
25
- * ```ts
26
- * const { encodeTag } = useCborTagEncoder()
27
- *
28
- * // Encode date/time string (tag 0)
29
- * const result1 = encodeTag(0, '2013-03-21T20:04:00Z', encode)
30
- * // result1.hex: 'c074323031332d30332d32315432303a30343a30305a'
31
- *
32
- * // Encode positive bignum (tag 2)
33
- * const result2 = encodeTag(2, new Uint8Array([0x01, 0xff]), encode)
34
- * // result2.hex: 'c24201ff'
35
- *
36
- * // Encode Cardano set (tag 258)
37
- * const result3 = encodeTag(258, [1, 2, 3], encode)
38
- * // result3.hex: 'd9010283010203'
39
- * ```
40
- */
41
- export function useCborTagEncoder() {
42
- /**
43
- * Encode tag number (Major Type 6 header)
44
- *
45
- * Tag numbers use the same encoding rules as unsigned integers:
46
- * - 0-23: Direct encoding in initial byte (0xc0-0xd7)
47
- * - 24-255: 0xd8 + 1 byte
48
- * - 256-65535: 0xd9 + 2 bytes
49
- * - 65536-4294967295: 0xda + 4 bytes
50
- * - 4294967296-2^64-1: 0xdb + 8 bytes
51
- *
52
- * @param tagNumber - Tag number (0 to 2^64-1)
53
- * @returns Encoded tag header bytes
54
- * @throws Error if tag number is negative or >= 2^64
55
- */
56
- const encodeTagNumber = (tagNumber: number | bigint): Uint8Array => {
57
- // Convert to BigInt for consistent handling
58
- const bigTag = typeof tagNumber === 'bigint' ? tagNumber : BigInt(tagNumber)
59
-
60
- // Validate tag is non-negative
61
- if (bigTag < 0n) {
62
- throw new Error('Tag number cannot be negative')
63
- }
64
-
65
- // Validate tag doesn't exceed 2^64-1
66
- const MAX_UINT64 = 18446744073709551615n // 2^64 - 1
67
- if (bigTag > MAX_UINT64) {
68
- throw new Error('Tag number exceeds maximum (2^64-1)')
69
- }
70
-
71
- let bytes: Uint8Array
72
-
73
- // Direct encoding (0-23) - Major type 6 (0xc0) + tag number
74
- if (bigTag <= 23n) {
75
- bytes = new Uint8Array([0xc0 + Number(bigTag)])
76
- }
77
- // 1-byte encoding (24-255) - 0xd8 + 1 byte
78
- else if (bigTag <= 255n) {
79
- bytes = new Uint8Array([0xd8, Number(bigTag)])
80
- }
81
- // 2-byte encoding (256-65535) - 0xd9 + 2 bytes
82
- else if (bigTag <= 65535n) {
83
- const valueBytes = writeUint(Number(bigTag), 2)
84
- bytes = new Uint8Array([0xd9, ...valueBytes])
85
- }
86
- // 4-byte encoding (65536-4294967295) - 0xda + 4 bytes
87
- else if (bigTag <= 4294967295n) {
88
- const valueBytes = writeUint(Number(bigTag), 4)
89
- bytes = new Uint8Array([0xda, ...valueBytes])
90
- }
91
- // 8-byte encoding (> 4294967295) - 0xdb + 8 bytes
92
- else {
93
- const valueBytes = writeBigUint(bigTag, 8)
94
- bytes = new Uint8Array([0xdb, ...valueBytes])
95
- }
96
-
97
- return bytes
98
- }
99
-
100
- /**
101
- * Encode tagged value (tag + content)
102
- *
103
- * A tagged value consists of:
104
- * 1. Tag number (Major Type 6 header)
105
- * 2. Tagged content (recursively encoded value)
106
- *
107
- * The encode function is passed as a parameter to avoid circular dependencies.
108
- *
109
- * @param tagNumber - Tag number
110
- * @param value - Value to tag
111
- * @param encode - Encoder function for the tagged value
112
- * @returns Encoded CBOR bytes and hex string
113
- */
114
- const encodeTag = (
115
- tagNumber: number | bigint,
116
- value: EncodableValue,
117
- encode: (value: EncodableValue) => EncodeResult
118
- ): EncodeResult => {
119
- // Encode tag number
120
- const tagBytes = encodeTagNumber(tagNumber)
121
-
122
- // Recursively encode the tagged value
123
- const valueResult = encode(value)
124
-
125
- // Concatenate tag header + value bytes
126
- const totalLength = tagBytes.length + valueResult.bytes.length
127
- const bytes = new Uint8Array(totalLength)
128
- bytes.set(tagBytes, 0)
129
- bytes.set(valueResult.bytes, tagBytes.length)
130
-
131
- return {
132
- bytes,
133
- hex: bytesToHex(bytes)
134
- }
135
- }
136
-
137
- /**
138
- * Encode TaggedValue object
139
- *
140
- * Convenience function for encoding { tag, value } objects.
141
- *
142
- * @param taggedValue - Object with tag and value properties
143
- * @param encode - Encoder function for the tagged value
144
- * @returns Encoded CBOR bytes and hex string
145
- */
146
- const encodeTaggedValue = (
147
- taggedValue: TaggedValue,
148
- encode: (value: EncodableValue) => EncodeResult
149
- ): EncodeResult => {
150
- return encodeTag(taggedValue.tag, taggedValue.value, encode)
151
- }
152
-
153
- return {
154
- encodeTagNumber,
155
- encodeTag,
156
- encodeTaggedValue
157
- }
158
- }