This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
/
util.js
221 lines (191 loc) · 5.89 KB
/
util.js
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
'use strict'
// @ts-ignore TODO: switch to cborg
const cbor = require('borc')
const multicodec = require('multicodec')
const multihashing = require('multihashing-async')
const { multihash } = multihashing
const CID = require('cids')
// @ts-ignore
const isCircular = require('is-circular')
const uint8ArrayConcat = require('uint8arrays/concat')
const uint8ArrayFromString = require('uint8arrays/from-string')
/**
* @typedef {import('cids').CIDVersion} CIDVersion
* @typedef {import('multihashing-async').multihash.HashCode} HashCode
*/
// https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
const CID_CBOR_TAG = 42
/**
* @param {CID | string} cid
*/
function tagCID (cid) {
let buf
if (typeof cid === 'string') {
buf = new CID(cid).bytes
} else if (CID.isCID(cid)) {
buf = cid.bytes
} else {
throw new Error('Could not tag CID - was not string or CID')
}
return new cbor.Tagged(CID_CBOR_TAG, uint8ArrayConcat([
uint8ArrayFromString('00', 'base16'), // thanks jdag
buf
], 1 + buf.length))
}
/**
* @param {any} dagNode
*/
function replaceCIDbyTAG (dagNode) {
let circular
try {
circular = isCircular(dagNode)
} catch (e) {
circular = false
}
if (circular) {
throw new Error('The object passed has circular references')
}
/**
* @param {any} obj
* @returns {any}
*/
function transform (obj) {
if (!obj || obj instanceof Uint8Array || typeof obj === 'string') {
return obj
}
if (Array.isArray(obj)) {
return obj.map(transform)
}
if (CID.isCID(obj)) {
return tagCID(obj)
}
const keys = Object.keys(obj)
if (keys.length > 0) {
// Recursive transform
/** @type {Record<string, any>} */
const out = {}
keys.forEach((key) => {
if (typeof obj[key] === 'object') {
out[key] = transform(obj[key])
} else {
out[key] = obj[key]
}
})
return out
} else {
return obj
}
}
return transform(dagNode)
}
const codec = multicodec.DAG_CBOR
const defaultHashAlg = multihash.names['sha2-256']
const defaultTags = {
/**
* @param {Uint8Array} val
*/
[CID_CBOR_TAG]: (val) => {
// remove that 0
val = val.slice(1)
return new CID(val)
}
}
const defaultSize = 64 * 1024 // current decoder heap size, 64 Kb
let currentSize = defaultSize
const defaultMaxSize = 64 * 1024 * 1024 // max heap size when auto-growing, 64 Mb
let maxSize = defaultMaxSize
/** @type {cbor.Decoder} */
let decoder
/**
* Configure the underlying CBOR decoder.
*
* @param {Object} [options] - The options the decoder takes. The decoder will reset to the defaul values if no options are given.
* @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`
* @param {number} [options.maxSize=67108864] - The maximum size the CBOR parsing heap is allowed to grow to before `dagCBOR.util.deserialize()` returns an error
* @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`
*/
function configureDecoder (options) {
let tags = defaultTags
if (options) {
if (typeof options.size === 'number') {
currentSize = options.size
}
if (typeof options.maxSize === 'number') {
maxSize = options.maxSize
}
if (options.tags) {
tags = Object.assign({}, defaultTags, options && options.tags)
}
} else {
// no options, reset to defaults
currentSize = defaultSize
maxSize = defaultMaxSize
}
const decoderOptions = {
tags,
size: currentSize
}
decoder = new cbor.Decoder(decoderOptions)
// borc edits opts.size in-place so we can capture _actual_ size
currentSize = decoderOptions.size
}
configureDecoder() // Setup default cbor.Decoder
/**
* Serialize internal representation into a binary CBOR block.
*
* @param {Object} node - Internal representation of a CBOR block
* @returns {Uint8Array} - The encoded binary representation
*/
function serialize (node) {
const nodeTagged = replaceCIDbyTAG(node)
const serialized = cbor.encode(nodeTagged)
return serialized
}
/**
* Deserialize CBOR block into the internal representation.
*
* @param {Uint8Array} data - Binary representation of a CBOR block
* @returns {any} - An object that conforms to the IPLD Data Model
*/
function deserialize (data) {
if (data.length > currentSize && data.length <= maxSize) {
configureDecoder({ size: data.length })
}
if (data.length > currentSize) {
throw new Error('Data is too large to deserialize with current decoder')
}
// borc will decode back-to-back objects into an implicit top-level array, we
// strictly want to only see a single explicit top-level object
const all = decoder.decodeAll(data)
if (all.length !== 1) {
throw new Error('Extraneous CBOR data found beyond initial top-level object')
}
return all[0]
}
/**
* Calculate the CID of the binary blob.
*
* @param {Uint8Array} binaryBlob - Encoded IPLD Node
* @param {Object} [userOptions] - Options to create the CID
* @param {CIDVersion} [userOptions.cidVersion=1] - CID version number
* @param {HashCode} [userOptions.hashAlg=multihash.names['sha2-256']] - Defaults to the defaultHashAlg of the format
*/
async function cid (binaryBlob, userOptions = {}) {
const options = {
cidVersion: userOptions.cidVersion == null ? 1 : userOptions.cidVersion,
hashAlg: userOptions.hashAlg == null ? module.exports.defaultHashAlg : userOptions.hashAlg
}
const hashName = multihash.codes[options.hashAlg]
const hash = await multihashing(binaryBlob, hashName)
const codecName = multicodec.getNameFromCode(module.exports.codec)
const cid = new CID(options.cidVersion, codecName, hash)
return cid
}
module.exports = {
codec,
defaultHashAlg,
configureDecoder,
serialize,
deserialize,
cid
}