@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,755 @@
1
+ 'use strict';
2
+
3
+ var chunkPD72MVTX_cjs = require('./chunk-PD72MVTX.cjs');
4
+
5
+ // src/encoder/types.ts
6
+ var DEFAULT_ENCODE_OPTIONS = {
7
+ canonical: false,
8
+ allowIndefinite: true,
9
+ rejectDuplicateKeys: false,
10
+ maxDepth: 64,
11
+ maxOutputSize: 100 * 1024 * 1024
12
+ // 100 MB
13
+ };
14
+
15
+ // src/encoder/utils.ts
16
+ function bytesToHex(bytes) {
17
+ return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
18
+ }
19
+ function concatenateUint8Arrays(arrays) {
20
+ const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
21
+ const result = new Uint8Array(totalLength);
22
+ let offset = 0;
23
+ for (const arr of arrays) {
24
+ result.set(arr, offset);
25
+ offset += arr.length;
26
+ }
27
+ return result;
28
+ }
29
+ function compareBytes(a, b) {
30
+ if (a.length !== b.length) {
31
+ return a.length - b.length;
32
+ }
33
+ for (let i = 0; i < a.length; i++) {
34
+ const byteA = a[i];
35
+ const byteB = b[i];
36
+ if (byteA === void 0 || byteB === void 0) {
37
+ throw new Error(`Unexpected undefined byte at index ${i}`);
38
+ }
39
+ if (byteA !== byteB) {
40
+ return byteA - byteB;
41
+ }
42
+ }
43
+ return 0;
44
+ }
45
+ function writeUint(value, bytes) {
46
+ const result = new Uint8Array(bytes);
47
+ for (let i = bytes - 1; i >= 0; i--) {
48
+ result[i] = value & 255;
49
+ value = value >>> 8;
50
+ }
51
+ return result;
52
+ }
53
+ function writeBigUint(value, bytes) {
54
+ const result = new Uint8Array(bytes);
55
+ for (let i = bytes - 1; i >= 0; i--) {
56
+ result[i] = Number(value & 0xffn);
57
+ value = value >> 8n;
58
+ }
59
+ return result;
60
+ }
61
+
62
+ // src/encoder/composables/useCborIntegerEncoder.ts
63
+ function useCborIntegerEncoder() {
64
+ const encodeUnsignedInt = (value) => {
65
+ const bigValue = typeof value === "bigint" ? value : BigInt(value);
66
+ if (bigValue < 0n) {
67
+ throw new Error("Cannot encode negative value as unsigned integer");
68
+ }
69
+ const MAX_UINT64 = 18446744073709551615n;
70
+ if (bigValue > MAX_UINT64) {
71
+ throw new Error("Value exceeds maximum unsigned integer (2^64-1)");
72
+ }
73
+ let bytes;
74
+ if (bigValue <= 23n) {
75
+ bytes = new Uint8Array([Number(bigValue)]);
76
+ } else if (bigValue <= 255n) {
77
+ bytes = new Uint8Array([24, Number(bigValue)]);
78
+ } else if (bigValue <= 65535n) {
79
+ const valueBytes = writeUint(Number(bigValue), 2);
80
+ bytes = new Uint8Array([25, ...valueBytes]);
81
+ } else if (bigValue <= 4294967295n) {
82
+ const valueBytes = writeUint(Number(bigValue), 4);
83
+ bytes = new Uint8Array([26, ...valueBytes]);
84
+ } else {
85
+ const valueBytes = writeBigUint(bigValue, 8);
86
+ bytes = new Uint8Array([27, ...valueBytes]);
87
+ }
88
+ return {
89
+ bytes,
90
+ hex: bytesToHex(bytes)
91
+ };
92
+ };
93
+ const encodeNegativeInt = (value) => {
94
+ const bigValue = typeof value === "bigint" ? value : BigInt(value);
95
+ if (bigValue >= 0n) {
96
+ throw new Error("Cannot encode positive value as negative integer");
97
+ }
98
+ const MIN_INT64 = -18446744073709551616n;
99
+ if (bigValue < MIN_INT64) {
100
+ throw new Error("Value exceeds minimum negative integer (-2^64)");
101
+ }
102
+ const encoded = -1n - bigValue;
103
+ let bytes;
104
+ if (encoded <= 23n) {
105
+ bytes = new Uint8Array([32 + Number(encoded)]);
106
+ } else if (encoded <= 255n) {
107
+ bytes = new Uint8Array([56, Number(encoded)]);
108
+ } else if (encoded <= 65535n) {
109
+ const valueBytes = writeUint(Number(encoded), 2);
110
+ bytes = new Uint8Array([57, ...valueBytes]);
111
+ } else if (encoded <= 4294967295n) {
112
+ const valueBytes = writeUint(Number(encoded), 4);
113
+ bytes = new Uint8Array([58, ...valueBytes]);
114
+ } else {
115
+ const valueBytes = writeBigUint(encoded, 8);
116
+ bytes = new Uint8Array([59, ...valueBytes]);
117
+ }
118
+ return {
119
+ bytes,
120
+ hex: bytesToHex(bytes)
121
+ };
122
+ };
123
+ const encodeInteger = (value) => {
124
+ const bigValue = typeof value === "bigint" ? value : BigInt(value);
125
+ if (bigValue < 0n) {
126
+ return encodeNegativeInt(bigValue);
127
+ } else {
128
+ return encodeUnsignedInt(bigValue);
129
+ }
130
+ };
131
+ return {
132
+ encodeUnsignedInt,
133
+ encodeNegativeInt,
134
+ encodeInteger
135
+ };
136
+ }
137
+
138
+ // src/encoder/composables/useCborStringEncoder.ts
139
+ function useCborStringEncoder(globalOptions) {
140
+ const options = { ...DEFAULT_ENCODE_OPTIONS, ...globalOptions };
141
+ const { isCborByteString } = chunkPD72MVTX_cjs.useCborByteString();
142
+ const { isCborTextString } = chunkPD72MVTX_cjs.useCborTextString();
143
+ const encodeLengthHeader = (majorType, length) => {
144
+ const baseValue = majorType << 5;
145
+ if (length <= 23) {
146
+ return new Uint8Array([baseValue | length]);
147
+ } else if (length <= 255) {
148
+ return new Uint8Array([baseValue | 24, length]);
149
+ } else if (length <= 65535) {
150
+ const lengthBytes = writeUint(length, 2);
151
+ return new Uint8Array([baseValue | 25, ...lengthBytes]);
152
+ } else if (length <= 4294967295) {
153
+ const lengthBytes = writeUint(length, 4);
154
+ return new Uint8Array([baseValue | 26, ...lengthBytes]);
155
+ } else {
156
+ const lengthBytes = length > Number.MAX_SAFE_INTEGER ? writeBigUint(BigInt(length), 8) : writeUint(length, 8);
157
+ return new Uint8Array([baseValue | 27, ...lengthBytes]);
158
+ }
159
+ };
160
+ const encodeByteString = (data, encodeOptions) => {
161
+ const isIndefinite = isCborByteString(data) && data[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
162
+ if (encodeOptions?.indefinite || Array.isArray(data) || isIndefinite) {
163
+ if (options.canonical) {
164
+ throw new Error("Indefinite-length encoding not allowed in canonical mode");
165
+ }
166
+ if (isCborByteString(data) && data.chunks) {
167
+ return encodeByteStringIndefinite(data.chunks);
168
+ }
169
+ const bytes2 = isCborByteString(data) ? data.bytes : Array.isArray(data) ? data : [data];
170
+ return encodeByteStringIndefinite(Array.isArray(bytes2) ? bytes2 : [bytes2]);
171
+ }
172
+ const bytes = isCborByteString(data) ? data.bytes : data;
173
+ const header = encodeLengthHeader(2, bytes.length);
174
+ const result = concatenateUint8Arrays([header, bytes]);
175
+ if (result.length > options.maxOutputSize) {
176
+ throw new Error("Encoded output exceeds maximum size");
177
+ }
178
+ return {
179
+ bytes: result,
180
+ hex: bytesToHex(result)
181
+ };
182
+ };
183
+ const encodeByteStringIndefinite = (chunks) => {
184
+ if (options.canonical) {
185
+ throw new Error("Indefinite-length encoding not allowed in canonical mode");
186
+ }
187
+ const parts = [new Uint8Array([95])];
188
+ for (const chunk of chunks) {
189
+ const header = encodeLengthHeader(2, chunk.length);
190
+ parts.push(header);
191
+ parts.push(chunk);
192
+ }
193
+ parts.push(new Uint8Array([255]));
194
+ const result = concatenateUint8Arrays(parts);
195
+ return {
196
+ bytes: result,
197
+ hex: bytesToHex(result)
198
+ };
199
+ };
200
+ const encodeTextString = (text, encodeOptions) => {
201
+ const isIndefinite = isCborTextString(text) && text[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
202
+ if (encodeOptions?.indefinite || isIndefinite) {
203
+ if (options.canonical) {
204
+ throw new Error("Indefinite-length encoding not allowed in canonical mode");
205
+ }
206
+ if (isCborTextString(text) && text.chunks) {
207
+ return encodeTextStringIndefinite(text.chunks);
208
+ }
209
+ const textStr2 = isCborTextString(text) ? text.text : text;
210
+ return encodeTextStringIndefinite([textStr2]);
211
+ }
212
+ const textStr = isCborTextString(text) ? text.text : text;
213
+ const encoder = new TextEncoder();
214
+ const utf8Bytes = encoder.encode(textStr);
215
+ const header = encodeLengthHeader(3, utf8Bytes.length);
216
+ const result = concatenateUint8Arrays([header, utf8Bytes]);
217
+ if (result.length > options.maxOutputSize) {
218
+ throw new Error("Encoded output exceeds maximum size");
219
+ }
220
+ return {
221
+ bytes: result,
222
+ hex: bytesToHex(result)
223
+ };
224
+ };
225
+ const encodeTextStringIndefinite = (chunks) => {
226
+ if (options.canonical) {
227
+ throw new Error("Indefinite-length encoding not allowed in canonical mode");
228
+ }
229
+ const parts = [new Uint8Array([127])];
230
+ const encoder = new TextEncoder();
231
+ for (const chunk of chunks) {
232
+ const utf8Bytes = encoder.encode(chunk);
233
+ const header = encodeLengthHeader(3, utf8Bytes.length);
234
+ parts.push(header);
235
+ parts.push(utf8Bytes);
236
+ }
237
+ parts.push(new Uint8Array([255]));
238
+ const result = concatenateUint8Arrays(parts);
239
+ return {
240
+ bytes: result,
241
+ hex: bytesToHex(result)
242
+ };
243
+ };
244
+ return {
245
+ encodeByteString,
246
+ encodeByteStringIndefinite,
247
+ encodeTextString,
248
+ encodeTextStringIndefinite
249
+ };
250
+ }
251
+
252
+ // src/encoder/composables/useCborCollectionEncoder.ts
253
+ function useCborCollectionEncoder(globalOptions) {
254
+ const options = { ...DEFAULT_ENCODE_OPTIONS, ...globalOptions };
255
+ const { encodeInteger } = useCborIntegerEncoder();
256
+ const { encodeTextString, encodeByteString } = useCborStringEncoder(globalOptions);
257
+ const { isCborByteString } = chunkPD72MVTX_cjs.useCborByteString();
258
+ const { isCborTextString } = chunkPD72MVTX_cjs.useCborTextString();
259
+ let mainEncode = null;
260
+ const setMainEncode = (encodeFn) => {
261
+ mainEncode = encodeFn;
262
+ };
263
+ const encodeValue = (value, ctx) => {
264
+ if (ctx.depth > ctx.options.maxDepth) {
265
+ throw new Error("Maximum nesting depth exceeded");
266
+ }
267
+ if (value === null || value === void 0) {
268
+ return new Uint8Array([246]);
269
+ } else if (typeof value === "boolean") {
270
+ return new Uint8Array([value ? 245 : 244]);
271
+ } else if (typeof value === "number" || typeof value === "bigint") {
272
+ return encodeInteger(value).bytes;
273
+ } else if (typeof value === "string" || isCborTextString(value)) {
274
+ return encodeTextString(value).bytes;
275
+ } else if (value instanceof Uint8Array || isCborByteString(value)) {
276
+ return encodeByteString(value).bytes;
277
+ } else if (Array.isArray(value)) {
278
+ const isIndefinite = value[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
279
+ const newCtx = { ...ctx, depth: ctx.depth + 1 };
280
+ if (isIndefinite) {
281
+ const parts = [new Uint8Array([159])];
282
+ for (const item of value) {
283
+ const encoded = encodeValue(item, newCtx);
284
+ parts.push(encoded);
285
+ }
286
+ parts.push(new Uint8Array([255]));
287
+ return concatenateUint8Arrays(parts);
288
+ } else {
289
+ return encodeArrayInternal(value, newCtx).bytes;
290
+ }
291
+ } else if (typeof value === "object" && value !== null && "tag" in value && "value" in value) {
292
+ if (mainEncode) {
293
+ return mainEncode(value).bytes;
294
+ }
295
+ throw new Error("Tagged value encoding requires main encoder to be set");
296
+ } else if (value instanceof Map || typeof value === "object" && value !== null) {
297
+ const isIndefinite = value[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
298
+ const newCtx = { ...ctx, depth: ctx.depth + 1 };
299
+ if (isIndefinite) {
300
+ const entries = value instanceof Map ? Array.from(value.entries()) : Object.entries(value);
301
+ const parts = [new Uint8Array([191])];
302
+ for (const [key, val] of entries) {
303
+ const encodedKey = encodeValue(key, newCtx);
304
+ const encodedValue = encodeValue(val, newCtx);
305
+ parts.push(encodedKey);
306
+ parts.push(encodedValue);
307
+ }
308
+ parts.push(new Uint8Array([255]));
309
+ return concatenateUint8Arrays(parts);
310
+ } else {
311
+ return encodeMapInternal(value, newCtx).bytes;
312
+ }
313
+ } else {
314
+ throw new Error(`Unsupported value type: ${typeof value}`);
315
+ }
316
+ };
317
+ const encodeLengthHeader = (majorType, length) => {
318
+ const baseValue = majorType << 5;
319
+ if (length <= 23) {
320
+ return new Uint8Array([baseValue | length]);
321
+ } else if (length <= 255) {
322
+ return new Uint8Array([baseValue | 24, length]);
323
+ } else if (length <= 65535) {
324
+ return new Uint8Array([baseValue | 25, length >> 8, length & 255]);
325
+ } else if (length <= 4294967295) {
326
+ return new Uint8Array([
327
+ baseValue | 26,
328
+ length >> 24 & 255,
329
+ length >> 16 & 255,
330
+ length >> 8 & 255,
331
+ length & 255
332
+ ]);
333
+ } else {
334
+ throw new Error("Collection too large to encode");
335
+ }
336
+ };
337
+ const encodeArrayInternal = (array, ctx) => {
338
+ const header = encodeLengthHeader(4, array.length);
339
+ const parts = [header];
340
+ for (const item of array) {
341
+ const encoded = encodeValue(item, ctx);
342
+ parts.push(encoded);
343
+ }
344
+ const result = concatenateUint8Arrays(parts);
345
+ ctx.bytesWritten += result.length;
346
+ if (ctx.bytesWritten > ctx.options.maxOutputSize) {
347
+ throw new Error("Encoded output exceeds maximum size");
348
+ }
349
+ return {
350
+ bytes: result,
351
+ hex: bytesToHex(result)
352
+ };
353
+ };
354
+ const encodeArray = (array, encodeOptions) => {
355
+ const isIndefinite = array[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
356
+ if (encodeOptions?.indefinite || isIndefinite) {
357
+ if (options.canonical) {
358
+ throw new Error("Indefinite-length encoding not allowed in canonical mode");
359
+ }
360
+ return encodeArrayIndefinite(array);
361
+ }
362
+ const ctx = {
363
+ depth: 0,
364
+ bytesWritten: 0,
365
+ options
366
+ };
367
+ return encodeArrayInternal(array, ctx);
368
+ };
369
+ const encodeArrayIndefinite = (array) => {
370
+ if (options.canonical) {
371
+ throw new Error("Indefinite-length encoding not allowed in canonical mode");
372
+ }
373
+ const parts = [new Uint8Array([159])];
374
+ const ctx = {
375
+ depth: 0,
376
+ bytesWritten: 0,
377
+ options
378
+ };
379
+ for (const item of array) {
380
+ const encoded = encodeValue(item, ctx);
381
+ parts.push(encoded);
382
+ }
383
+ parts.push(new Uint8Array([255]));
384
+ const result = concatenateUint8Arrays(parts);
385
+ return {
386
+ bytes: result,
387
+ hex: bytesToHex(result)
388
+ };
389
+ };
390
+ const encodeMapInternal = (map, ctx) => {
391
+ let entries;
392
+ if (map[chunkPD72MVTX_cjs.ALL_ENTRIES_SYMBOL]) {
393
+ entries = map[chunkPD72MVTX_cjs.ALL_ENTRIES_SYMBOL];
394
+ } else {
395
+ entries = map instanceof Map ? Array.from(map.entries()) : Object.entries(map);
396
+ }
397
+ if (ctx.options.canonical && !map[chunkPD72MVTX_cjs.ALL_ENTRIES_SYMBOL]) {
398
+ entries.sort((a, b) => {
399
+ const keyA = encodeValue(a[0], { ...ctx, depth: ctx.depth + 1 });
400
+ const keyB = encodeValue(b[0], { ...ctx, depth: ctx.depth + 1 });
401
+ return compareBytes(keyA, keyB);
402
+ });
403
+ }
404
+ const header = encodeLengthHeader(5, entries.length);
405
+ const parts = [header];
406
+ for (const [key, value] of entries) {
407
+ const encodedKey = encodeValue(key, ctx);
408
+ const encodedValue = encodeValue(value, ctx);
409
+ parts.push(encodedKey);
410
+ parts.push(encodedValue);
411
+ }
412
+ const result = concatenateUint8Arrays(parts);
413
+ ctx.bytesWritten += result.length;
414
+ if (ctx.bytesWritten > ctx.options.maxOutputSize) {
415
+ throw new Error("Encoded output exceeds maximum size");
416
+ }
417
+ return {
418
+ bytes: result,
419
+ hex: bytesToHex(result)
420
+ };
421
+ };
422
+ const encodeMap = (map, encodeOptions) => {
423
+ const isIndefinite = map[chunkPD72MVTX_cjs.INDEFINITE_SYMBOL] === true;
424
+ if (encodeOptions?.indefinite || isIndefinite) {
425
+ if (options.canonical) {
426
+ throw new Error("Indefinite-length encoding not allowed in canonical mode");
427
+ }
428
+ return encodeMapIndefinite(map);
429
+ }
430
+ const ctx = {
431
+ depth: 0,
432
+ bytesWritten: 0,
433
+ options
434
+ };
435
+ return encodeMapInternal(map, ctx);
436
+ };
437
+ const encodeMapIndefinite = (map) => {
438
+ if (options.canonical) {
439
+ throw new Error("Indefinite-length encoding not allowed in canonical mode");
440
+ }
441
+ const entries = map instanceof Map ? Array.from(map.entries()) : Object.entries(map);
442
+ const parts = [new Uint8Array([191])];
443
+ const ctx = {
444
+ depth: 0,
445
+ bytesWritten: 0,
446
+ options
447
+ };
448
+ for (const [key, value] of entries) {
449
+ const encodedKey = encodeValue(key, ctx);
450
+ const encodedValue = encodeValue(value, ctx);
451
+ parts.push(encodedKey);
452
+ parts.push(encodedValue);
453
+ }
454
+ parts.push(new Uint8Array([255]));
455
+ const result = concatenateUint8Arrays(parts);
456
+ return {
457
+ bytes: result,
458
+ hex: bytesToHex(result)
459
+ };
460
+ };
461
+ return {
462
+ encodeArray,
463
+ encodeArrayIndefinite,
464
+ encodeMap,
465
+ encodeMapIndefinite,
466
+ setMainEncode
467
+ };
468
+ }
469
+
470
+ // src/encoder/composables/useCborSimpleEncoder.ts
471
+ function useCborSimpleEncoder(_globalOptions) {
472
+ const { encodeInteger } = useCborIntegerEncoder();
473
+ const encodeSimple = (value) => {
474
+ let byte;
475
+ if (value === false) {
476
+ byte = 244;
477
+ } else if (value === true) {
478
+ byte = 245;
479
+ } else if (value === null) {
480
+ byte = 246;
481
+ } else if (value === void 0) {
482
+ byte = 247;
483
+ } else {
484
+ throw new Error(`Unsupported simple value: ${value}`);
485
+ }
486
+ return {
487
+ bytes: new Uint8Array([byte]),
488
+ hex: bytesToHex(new Uint8Array([byte]))
489
+ };
490
+ };
491
+ const encodeFloat16Bytes = (value) => {
492
+ if (value === 0) return new Uint8Array([0, 0]);
493
+ if (value === -0) return new Uint8Array([128, 0]);
494
+ if (Number.isNaN(value)) return new Uint8Array([126, 0]);
495
+ if (value === Infinity) return new Uint8Array([124, 0]);
496
+ if (value === -Infinity) return new Uint8Array([252, 0]);
497
+ const sign = value < 0 ? 1 : 0;
498
+ const absValue = Math.abs(value);
499
+ const buffer = new ArrayBuffer(8);
500
+ const view = new DataView(buffer);
501
+ view.setFloat64(0, absValue, false);
502
+ const bits = view.getBigUint64(0, false);
503
+ const exp64 = Number(bits >> 52n & 0x7ffn) - 1023;
504
+ const mant64 = Number(bits & 0xfffffffffffffn);
505
+ let exp16;
506
+ let mant16;
507
+ if (exp64 < -14) {
508
+ exp16 = 0;
509
+ mant16 = 0;
510
+ } else if (exp64 > 15) {
511
+ exp16 = 31;
512
+ mant16 = 0;
513
+ } else {
514
+ exp16 = exp64 + 15;
515
+ mant16 = mant64 >> 42;
516
+ }
517
+ const float16 = sign << 15 | exp16 << 10 | mant16;
518
+ return new Uint8Array([
519
+ float16 >> 8 & 255,
520
+ float16 & 255
521
+ ]);
522
+ };
523
+ const encodeFloat32Bytes = (value) => {
524
+ const buffer = new ArrayBuffer(4);
525
+ const view = new DataView(buffer);
526
+ view.setFloat32(0, value, false);
527
+ return new Uint8Array(buffer);
528
+ };
529
+ const encodeFloat64Bytes = (value) => {
530
+ const buffer = new ArrayBuffer(8);
531
+ const view = new DataView(buffer);
532
+ view.setFloat64(0, value, false);
533
+ return new Uint8Array(buffer);
534
+ };
535
+ const canBeFloat16 = (value) => {
536
+ if (!Number.isFinite(value) || value === 0 || value === -0) {
537
+ return true;
538
+ }
539
+ const absValue = Math.abs(value);
540
+ if (absValue < 6103515625e-14 || absValue > 65504) {
541
+ return false;
542
+ }
543
+ const bytes = encodeFloat16Bytes(value);
544
+ const view = new DataView(bytes.buffer);
545
+ const bits = view.getUint16(0, false);
546
+ const sign = bits >> 15 & 1;
547
+ const exp = bits >> 10 & 31;
548
+ const mant = bits & 1023;
549
+ let decoded;
550
+ if (exp === 0) {
551
+ decoded = (sign ? -1 : 1) * Math.pow(2, -14) * (mant / 1024);
552
+ } else if (exp === 31) {
553
+ decoded = mant === 0 ? sign ? -Infinity : Infinity : NaN;
554
+ } else {
555
+ decoded = (sign ? -1 : 1) * Math.pow(2, exp - 15) * (1 + mant / 1024);
556
+ }
557
+ return decoded === value;
558
+ };
559
+ const canBeFloat32 = (value) => {
560
+ if (!Number.isFinite(value) || value === 0 || value === -0) {
561
+ return true;
562
+ }
563
+ const buffer = new ArrayBuffer(4);
564
+ const view = new DataView(buffer);
565
+ view.setFloat32(0, value, false);
566
+ const decoded = view.getFloat32(0, false);
567
+ return decoded === value;
568
+ };
569
+ const isInteger = (value) => {
570
+ return Number.isInteger(value) && Number.isSafeInteger(value);
571
+ };
572
+ const encodeFloat = (value, precision) => {
573
+ let bytes;
574
+ if (isInteger(value) && !precision) {
575
+ return encodeInteger(value);
576
+ }
577
+ if (precision === 16) {
578
+ const floatBytes = encodeFloat16Bytes(value);
579
+ bytes = new Uint8Array([249, ...floatBytes]);
580
+ } else if (precision === 32) {
581
+ const floatBytes = encodeFloat32Bytes(value);
582
+ bytes = new Uint8Array([250, ...floatBytes]);
583
+ } else if (precision === 64) {
584
+ const floatBytes = encodeFloat64Bytes(value);
585
+ bytes = new Uint8Array([251, ...floatBytes]);
586
+ } else {
587
+ if (canBeFloat16(value)) {
588
+ const floatBytes = encodeFloat16Bytes(value);
589
+ bytes = new Uint8Array([249, ...floatBytes]);
590
+ } else if (canBeFloat32(value)) {
591
+ const floatBytes = encodeFloat32Bytes(value);
592
+ bytes = new Uint8Array([250, ...floatBytes]);
593
+ } else {
594
+ const floatBytes = encodeFloat64Bytes(value);
595
+ bytes = new Uint8Array([251, ...floatBytes]);
596
+ }
597
+ }
598
+ return {
599
+ bytes,
600
+ hex: bytesToHex(bytes)
601
+ };
602
+ };
603
+ return {
604
+ encodeSimple,
605
+ encodeFloat
606
+ };
607
+ }
608
+
609
+ // src/encoder/composables/useCborTagEncoder.ts
610
+ function useCborTagEncoder() {
611
+ const encodeTagNumber = (tagNumber) => {
612
+ const bigTag = typeof tagNumber === "bigint" ? tagNumber : BigInt(tagNumber);
613
+ if (bigTag < 0n) {
614
+ throw new Error("Tag number cannot be negative");
615
+ }
616
+ const MAX_UINT64 = 18446744073709551615n;
617
+ if (bigTag > MAX_UINT64) {
618
+ throw new Error("Tag number exceeds maximum (2^64-1)");
619
+ }
620
+ let bytes;
621
+ if (bigTag <= 23n) {
622
+ bytes = new Uint8Array([192 + Number(bigTag)]);
623
+ } else if (bigTag <= 255n) {
624
+ bytes = new Uint8Array([216, Number(bigTag)]);
625
+ } else if (bigTag <= 65535n) {
626
+ const valueBytes = writeUint(Number(bigTag), 2);
627
+ bytes = new Uint8Array([217, ...valueBytes]);
628
+ } else if (bigTag <= 4294967295n) {
629
+ const valueBytes = writeUint(Number(bigTag), 4);
630
+ bytes = new Uint8Array([218, ...valueBytes]);
631
+ } else {
632
+ const valueBytes = writeBigUint(bigTag, 8);
633
+ bytes = new Uint8Array([219, ...valueBytes]);
634
+ }
635
+ return bytes;
636
+ };
637
+ const encodeTag = (tagNumber, value, encode) => {
638
+ const tagBytes = encodeTagNumber(tagNumber);
639
+ const valueResult = encode(value);
640
+ const totalLength = tagBytes.length + valueResult.bytes.length;
641
+ const bytes = new Uint8Array(totalLength);
642
+ bytes.set(tagBytes, 0);
643
+ bytes.set(valueResult.bytes, tagBytes.length);
644
+ return {
645
+ bytes,
646
+ hex: bytesToHex(bytes)
647
+ };
648
+ };
649
+ const encodeTaggedValue = (taggedValue, encode) => {
650
+ return encodeTag(taggedValue.tag, taggedValue.value, encode);
651
+ };
652
+ return {
653
+ encodeTagNumber,
654
+ encodeTag,
655
+ encodeTaggedValue
656
+ };
657
+ }
658
+
659
+ // src/encoder/composables/useCborEncoder.ts
660
+ function useCborEncoder(globalOptions) {
661
+ const options = { ...DEFAULT_ENCODE_OPTIONS, ...globalOptions };
662
+ const { encodeInteger } = useCborIntegerEncoder();
663
+ const { encodeTextString, encodeByteString } = useCborStringEncoder(options);
664
+ const { encodeArray, encodeMap, setMainEncode } = useCborCollectionEncoder(options);
665
+ const { encodeSimple, encodeFloat } = useCborSimpleEncoder();
666
+ const { encodeTaggedValue } = useCborTagEncoder();
667
+ const { isCborByteString } = chunkPD72MVTX_cjs.useCborByteString();
668
+ const { isCborTextString } = chunkPD72MVTX_cjs.useCborTextString();
669
+ const encode = (value) => {
670
+ if (value === null || value === void 0 || typeof value === "boolean") {
671
+ return encodeSimple(value);
672
+ }
673
+ if (typeof value === "number") {
674
+ if (Number.isInteger(value) && Number.isSafeInteger(value)) {
675
+ return encodeInteger(value);
676
+ }
677
+ return encodeFloat(value);
678
+ }
679
+ if (typeof value === "bigint") {
680
+ return encodeInteger(value);
681
+ }
682
+ if (isCborTextString(value)) {
683
+ return encodeTextString(value);
684
+ }
685
+ if (typeof value === "string") {
686
+ return encodeTextString(value);
687
+ }
688
+ if (isCborByteString(value)) {
689
+ return encodeByteString(value);
690
+ }
691
+ if (value instanceof Uint8Array) {
692
+ return encodeByteString(value);
693
+ }
694
+ if (Array.isArray(value)) {
695
+ return encodeArray(value);
696
+ }
697
+ if (value instanceof Map) {
698
+ return encodeMap(value);
699
+ }
700
+ if (typeof value === "object" && value !== null && "tag" in value && "value" in value && typeof value.tag === "number") {
701
+ return encodeTaggedValue(value, encode);
702
+ }
703
+ if (typeof value === "object" && value !== null) {
704
+ return encodeMap(value);
705
+ }
706
+ throw new Error(`Unsupported value type: ${typeof value}`);
707
+ };
708
+ setMainEncode(encode);
709
+ const encodeToHex = (value) => {
710
+ return encode(value).hex;
711
+ };
712
+ const encodeToBytes = (value) => {
713
+ return encode(value).bytes;
714
+ };
715
+ const encodeSequence = (values) => {
716
+ const allBytes = [];
717
+ for (const value of values) {
718
+ const result = encode(value);
719
+ allBytes.push(result.bytes);
720
+ }
721
+ const totalLength = allBytes.reduce((sum, arr) => sum + arr.length, 0);
722
+ const concatenated = new Uint8Array(totalLength);
723
+ let offset = 0;
724
+ for (const bytes of allBytes) {
725
+ concatenated.set(bytes, offset);
726
+ offset += bytes.length;
727
+ }
728
+ const hex = Array.from(concatenated).map((b) => b.toString(16).padStart(2, "0")).join("");
729
+ return {
730
+ bytes: concatenated,
731
+ hex
732
+ };
733
+ };
734
+ return {
735
+ encode,
736
+ encodeToHex,
737
+ encodeToBytes,
738
+ encodeSequence
739
+ };
740
+ }
741
+
742
+ exports.DEFAULT_ENCODE_OPTIONS = DEFAULT_ENCODE_OPTIONS;
743
+ exports.bytesToHex = bytesToHex;
744
+ exports.compareBytes = compareBytes;
745
+ exports.concatenateUint8Arrays = concatenateUint8Arrays;
746
+ exports.useCborCollectionEncoder = useCborCollectionEncoder;
747
+ exports.useCborEncoder = useCborEncoder;
748
+ exports.useCborIntegerEncoder = useCborIntegerEncoder;
749
+ exports.useCborSimpleEncoder = useCborSimpleEncoder;
750
+ exports.useCborStringEncoder = useCborStringEncoder;
751
+ exports.useCborTagEncoder = useCborTagEncoder;
752
+ exports.writeBigUint = writeBigUint;
753
+ exports.writeUint = writeUint;
754
+ //# sourceMappingURL=chunk-2FUTHZQQ.cjs.map
755
+ //# sourceMappingURL=chunk-2FUTHZQQ.cjs.map