+18
-9
src/pds.js
+18
-9
src/pds.js
···
1
+
// === CONSTANTS ===
2
+
// CBOR primitive markers (RFC 8949)
3
+
const CBOR_FALSE = 0xf4
4
+
const CBOR_TRUE = 0xf5
5
+
const CBOR_NULL = 0xf6
6
+
7
+
// DAG-CBOR CID link tag
8
+
const CBOR_TAG_CID = 42
9
+
1
10
// === CID WRAPPER ===
2
11
// Explicit CID type for DAG-CBOR encoding (avoids fragile heuristic detection)
3
12
···
18
27
19
28
function encode(val) {
20
29
if (val === null) {
21
-
parts.push(0xf6) // null
30
+
parts.push(CBOR_NULL)
22
31
} else if (val === true) {
23
-
parts.push(0xf5) // true
32
+
parts.push(CBOR_TRUE)
24
33
} else if (val === false) {
25
-
parts.push(0xf4) // false
34
+
parts.push(CBOR_FALSE)
26
35
} else if (typeof val === 'number') {
27
36
encodeInteger(val)
28
37
} else if (typeof val === 'string') {
···
82
91
83
92
function encode(val) {
84
93
if (val === null) {
85
-
parts.push(0xf6) // null
94
+
parts.push(CBOR_NULL)
86
95
} else if (val === true) {
87
-
parts.push(0xf5) // true
96
+
parts.push(CBOR_TRUE)
88
97
} else if (val === false) {
89
-
parts.push(0xf4) // false
98
+
parts.push(CBOR_FALSE)
90
99
} else if (typeof val === 'number') {
91
100
if (Number.isInteger(val) && val >= 0) {
92
101
encodeHead(0, val)
···
99
108
parts.push(...bytes)
100
109
} else if (val instanceof CID) {
101
110
// CID - encode with CBOR tag 42 + 0x00 prefix
102
-
parts.push(0xd8, 42) // tag(42)
111
+
parts.push(0xd8, CBOR_TAG_CID)
103
112
encodeHead(2, val.bytes.length + 1) // +1 for 0x00 prefix
104
113
parts.push(0x00) // multibase identity prefix
105
114
parts.push(...val.bytes)
···
569
578
570
579
function encode(val) {
571
580
if (val === null || val === undefined) {
572
-
parts.push(0xf6) // null
581
+
parts.push(CBOR_NULL)
573
582
} else if (typeof val === 'number') {
574
583
encodeHead(0, val) // unsigned int
575
584
} else if (val instanceof CID) {
576
585
// CID - encode with CBOR tag 42 + 0x00 prefix (DAG-CBOR CID link)
577
-
parts.push(0xd8, 42) // tag 42
586
+
parts.push(0xd8, CBOR_TAG_CID)
578
587
encodeHead(2, val.bytes.length + 1) // +1 for 0x00 prefix
579
588
parts.push(0x00) // multibase identity prefix
580
589
parts.push(...val.bytes)