-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathextended.js
More file actions
251 lines (230 loc) · 5.92 KB
/
Copy pathextended.js
File metadata and controls
251 lines (230 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* cborg/extended - Extended JavaScript type support for CBOR
*
* This module provides encode/decode functions that support extended JavaScript
* types: Date, RegExp, Map, Set, BigInt, and all TypedArray types.
*
* Similar to the browser's structured clone algorithm, this module prioritises
* JavaScript type preservation using standard CBOR tags. Unlike base cborg
* (designed for IPLD/content-addressed data), types round-trip with full fidelity.
*/
import { encode as _encode, decode as _decode } from '../../cborg.js'
import {
// BigInt
structBigIntEncoder,
bigIntDecoder,
bigNegIntDecoder,
// Date
dateEncoder,
dateDecoder,
// RegExp
regExpEncoder,
regExpDecoder,
// Set
setEncoder,
setDecoder,
// Map
mapEncoder,
mapDecoder,
// Error
errorEncoder,
errorDecoder,
// Negative zero
negativeZeroEncoder,
// TypedArrays
uint8ArrayEncoder,
uint8ArrayDecoder,
uint8ClampedArrayEncoder,
uint8ClampedArrayDecoder,
int8ArrayEncoder,
int8ArrayDecoder,
uint16ArrayEncoder,
uint16ArrayDecoder,
uint32ArrayEncoder,
uint32ArrayDecoder,
int16ArrayEncoder,
int16ArrayDecoder,
int32ArrayEncoder,
int32ArrayDecoder,
float32ArrayEncoder,
float32ArrayDecoder,
float64ArrayEncoder,
float64ArrayDecoder,
bigUint64ArrayEncoder,
bigUint64ArrayDecoder,
bigInt64ArrayEncoder,
bigInt64ArrayDecoder,
// Tag constants
TAG_DATE_EPOCH,
TAG_BIGINT_POS,
TAG_BIGINT_NEG,
TAG_OBJECT_CLASS,
TAG_UINT8_ARRAY,
TAG_UINT8_CLAMPED_ARRAY,
TAG_INT8_ARRAY,
TAG_UINT16_ARRAY_LE,
TAG_UINT32_ARRAY_LE,
TAG_BIGUINT64_ARRAY_LE,
TAG_INT16_ARRAY_LE,
TAG_INT32_ARRAY_LE,
TAG_BIGINT64_ARRAY_LE,
TAG_FLOAT32_ARRAY_LE,
TAG_FLOAT64_ARRAY_LE,
TAG_SET,
TAG_MAP,
TAG_REGEXP
} from '../taglib.js'
/**
* @typedef {import('../../interface.js').EncodeOptions} EncodeOptions
* @typedef {import('../../interface.js').DecodeOptions} DecodeOptions
* @typedef {import('../../interface.js').AllocatedByteView} AllocatedByteView
*/
/**
* Type encoders for all supported types
*/
const typeEncoders = {
number: negativeZeroEncoder,
bigint: structBigIntEncoder,
Date: dateEncoder,
RegExp: regExpEncoder,
Set: setEncoder,
Map: mapEncoder,
Error: errorEncoder,
EvalError: errorEncoder,
RangeError: errorEncoder,
ReferenceError: errorEncoder,
SyntaxError: errorEncoder,
TypeError: errorEncoder,
URIError: errorEncoder,
Uint8Array: uint8ArrayEncoder,
Uint8ClampedArray: uint8ClampedArrayEncoder,
Int8Array: int8ArrayEncoder,
Uint16Array: uint16ArrayEncoder,
Uint32Array: uint32ArrayEncoder,
Int16Array: int16ArrayEncoder,
Int32Array: int32ArrayEncoder,
Float32Array: float32ArrayEncoder,
Float64Array: float64ArrayEncoder,
BigUint64Array: bigUint64ArrayEncoder,
BigInt64Array: bigInt64ArrayEncoder
}
/**
* Tag decoders for all supported tags
*/
const tags = {
[TAG_DATE_EPOCH]: dateDecoder,
[TAG_BIGINT_POS]: bigIntDecoder,
[TAG_BIGINT_NEG]: bigNegIntDecoder,
[TAG_OBJECT_CLASS]: errorDecoder,
[TAG_REGEXP]: regExpDecoder,
[TAG_SET]: setDecoder,
[TAG_MAP]: mapDecoder,
[TAG_UINT8_ARRAY]: uint8ArrayDecoder,
[TAG_UINT8_CLAMPED_ARRAY]: uint8ClampedArrayDecoder,
[TAG_INT8_ARRAY]: int8ArrayDecoder,
[TAG_UINT16_ARRAY_LE]: uint16ArrayDecoder,
[TAG_UINT32_ARRAY_LE]: uint32ArrayDecoder,
[TAG_INT16_ARRAY_LE]: int16ArrayDecoder,
[TAG_INT32_ARRAY_LE]: int32ArrayDecoder,
[TAG_FLOAT32_ARRAY_LE]: float32ArrayDecoder,
[TAG_FLOAT64_ARRAY_LE]: float64ArrayDecoder,
[TAG_BIGUINT64_ARRAY_LE]: bigUint64ArrayDecoder,
[TAG_BIGINT64_ARRAY_LE]: bigInt64ArrayDecoder
}
/**
* Encode a value to CBOR with extended JavaScript type support.
*
* Supported types beyond standard cborg:
* - Date (Tag 1)
* - RegExp (Tag 21066)
* - Map (Tag 259)
* - Set (Tag 258)
* - BigInt (Tags 2/3, always tagged)
* - All TypedArrays (Tags 64-87)
*
* @param {any} obj - Value to encode
* @param {EncodeOptions} [options] - Additional options (merged with extended defaults)
* @returns {AllocatedByteView}
*/
export function encode (obj, options = {}) {
return _encode(obj, {
mapSorter: undefined, // Preserve insertion order for type fidelity (like structured clone)
...options,
typeEncoders: { ...typeEncoders, ...options.typeEncoders }
})
}
/**
* Decode CBOR to a value with extended JavaScript type support.
*
* @param {Uint8Array} data - CBOR data to decode
* @param {DecodeOptions} [options] - Additional options (merged with extended defaults)
* @returns {any}
*/
export function decode (data, options = {}) {
return _decode(data, {
...options,
tags: { ...tags, ...options.tags }
// useMaps defaults to false: plain objects decode as objects, Tag 259 Maps decode as Maps.
// The mapDecoder uses decode.entries() to preserve key types regardless of useMaps setting.
})
}
// Re-export all taglib components for users who want to customize
export {
// Tag constants
TAG_DATE_EPOCH,
TAG_BIGINT_POS,
TAG_BIGINT_NEG,
TAG_UINT8_ARRAY,
TAG_UINT8_CLAMPED_ARRAY,
TAG_INT8_ARRAY,
TAG_UINT16_ARRAY_LE,
TAG_UINT32_ARRAY_LE,
TAG_BIGUINT64_ARRAY_LE,
TAG_INT16_ARRAY_LE,
TAG_INT32_ARRAY_LE,
TAG_BIGINT64_ARRAY_LE,
TAG_FLOAT32_ARRAY_LE,
TAG_FLOAT64_ARRAY_LE,
TAG_SET,
TAG_MAP,
TAG_REGEXP,
// BigInt
structBigIntEncoder,
bigIntDecoder,
bigNegIntDecoder,
// Date
dateEncoder,
dateDecoder,
// RegExp
regExpEncoder,
regExpDecoder,
// Set
setEncoder,
setDecoder,
// Map
mapEncoder,
mapDecoder,
// TypedArrays
uint8ArrayEncoder,
uint8ArrayDecoder,
uint8ClampedArrayEncoder,
uint8ClampedArrayDecoder,
int8ArrayEncoder,
int8ArrayDecoder,
uint16ArrayEncoder,
uint16ArrayDecoder,
uint32ArrayEncoder,
uint32ArrayDecoder,
int16ArrayEncoder,
int16ArrayDecoder,
int32ArrayEncoder,
int32ArrayDecoder,
float32ArrayEncoder,
float32ArrayDecoder,
float64ArrayEncoder,
float64ArrayDecoder,
bigUint64ArrayEncoder,
bigUint64ArrayDecoder,
bigInt64ArrayEncoder,
bigInt64ArrayDecoder
}