You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BREAKING CHANGE!
* Will be much more strict with data types on encode and decode, only
IPLD Data Model values will be accepted, all others will be rejected
(including `undefined`). No other tags than 42 will be involved in encode
or decode.
* Will strictly reject decode of data where integers are encoded using more
bytes than are required, as per deterministic DAG-CBOR rules.
* Strictly only tag 42 and no others. Also no exotic types like Simple Values.
* Indefinite length items will not be accepted for decode.
* BigInt will be used for integers decoded outside of the safe integer range.
They are also accepted for encodes, within the 64-bit range. The exotic
bignumber type from borc is no longer supported.
* Floats are now always encoded as 64-bit, to match the updated DAG-CBOR spec
and the Go implementation.
* Uint8Arrays are native through the stack now, `Buffer` isn't required for
browser bundling.
thrownewError('The object passed has circular references')
12
+
// this will receive all Objects, we need to filter out anything that's not
13
+
// a CID and return `null` for that so it's encoded as normal
14
+
functioncidEncoder(obj){
15
+
if(obj.asCID!==obj){
16
+
returnnull// any other kind of object
29
17
}
30
-
31
-
functiontransform(obj){
32
-
if(bytes.isBinary(obj))returnbytes.coerce(obj)
33
-
if(!obj||typeofobj==='string'){
34
-
returnobj
35
-
}
36
-
37
-
if(Array.isArray(obj)){
38
-
returnobj.map(transform)
39
-
}
40
-
41
-
constcid=asCID(obj,config)
42
-
if(cid){
43
-
returntagCID(cid)
44
-
}
45
-
46
-
constkeys=Object.keys(obj)
47
-
48
-
if(keys.length>0){
49
-
// Recursive transform
50
-
constout={}
51
-
keys.forEach((key)=>{
52
-
if(typeofobj[key]==='object'){
53
-
out[key]=transform(obj[key])
54
-
}else{
55
-
out[key]=obj[key]
56
-
}
57
-
})
58
-
returnout
59
-
}else{
60
-
returnobj
61
-
}
18
+
constcid=CID.asCID(obj)
19
+
/* c8 ignore next 4 */
20
+
// very unlikely case, and it'll probably throw a recursion error in cborg
21
+
if(!cid){
22
+
returnnull
62
23
}
63
-
64
-
returntransform(dagNode)
24
+
constbytes=newUint8Array(cid.bytes.byteLength+1)
25
+
bytes.set(cid.bytes,1)// prefix is 0x00, for historical reasons
26
+
return[
27
+
newcborg.Token(cborg.Type.tag,CID_CBOR_TAG),
28
+
newcborg.Token(cborg.Type.bytes,bytes)
29
+
]
65
30
}
66
31
67
-
constdefaultTags={
68
-
[CID_CBOR_TAG]: (val)=>{
69
-
returndecodeCID(val.subarray(1),cidConfig)
70
-
}
32
+
functionundefinedEncoder(){
33
+
thrownewError('`undefined` is not supported by the IPLD Data Model and cannot be encoded')
71
34
}
72
35
73
-
constdefaultSize=64*1024// current decoder heap size, 64 Kb
74
-
constdefaultMaxSize=64*1024*1024// max heap size when auto-growing, 64 Mb
75
-
76
-
letcurrentSize=defaultSize
77
-
letmaxSize=defaultMaxSize
78
-
letdecoder=null
79
-
letcidConfig=null
80
-
81
-
/**
82
-
* Configure the underlying CBOR decoder.
83
-
*
84
-
* @param {Object} [options] - The options the decoder takes. The decoder will reset to the defaul values if no options are given.
85
-
* @param {number} [options.size=65536] - The current heap size used in CBOR parsing, this may grow automatically as larger blocks are encountered up to `maxSize`
86
-
* @param {number} [options.maxSize=67108864] - The maximum size the CBOR parsing heap is allowed to grow to before `dagCBOR.util.deserialize()` returns an error
87
-
* @param {Object} [options.tags] - An object whose keys are CBOR tag numbers and values are transform functions that accept a `value` and return a decoded representation of that `value`
88
-
*/
89
-
constconfigureDecoder=(options)=>{
90
-
consttags=defaultTags
91
-
92
-
if(options){
93
-
if(typeofoptions.size==='number'){
94
-
currentSize=options.size
95
-
}
96
-
if(typeofoptions.maxSize==='number'){
97
-
maxSize=options.maxSize
98
-
}
99
-
}else{
100
-
// no options, reset to defaults
101
-
currentSize=defaultSize
102
-
maxSize=defaultMaxSize
103
-
}
104
-
105
-
constdecoderOptions={
106
-
tags,
107
-
size: currentSize
36
+
constencodeOptions={
37
+
float64: true,
38
+
typeEncoders: {
39
+
Object: cidEncoder,
40
+
undefined: undefinedEncoder
108
41
}
109
-
110
-
decoder=newcbor.Decoder(decoderOptions)
111
-
// borc edits opts.size in-place so we can capture _actual_ size
0 commit comments