-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
234 lines (190 loc) · 5.2 KB
/
index.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
222
223
224
225
226
227
228
229
230
231
232
233
234
import CID from 'multiformats/cid'
import decodeNode from './pb-decode.js'
import encodeNode from './pb-encode.js'
const code = 0x70
const name = 'dag-pb'
const pbNodeProperties = ['Data', 'Links']
const pbLinkProperties = ['Hash', 'Name', 'Tsize']
const textEncoder = new TextEncoder()
function linkComparator (a, b) {
if (a === b) {
return 0
}
const abuf = a.Name ? textEncoder.encode(a.Name) : []
const bbuf = b.Name ? textEncoder.encode(b.Name) : []
let x = abuf.length
let y = bbuf.length
for (let i = 0, len = Math.min(x, y); i < len; ++i) {
if (abuf[i] !== bbuf[i]) {
x = abuf[i]
y = bbuf[i]
break
}
}
return x < y ? -1 : y < x ? 1 : 0
}
function hasOnlyProperties (node, properties) {
return !Object.keys(node).some((p) => !properties.includes(p))
}
function asLink (link) {
if (typeof link.asCID === 'object') {
const Hash = CID.asCID(link)
if (!Hash) {
throw new TypeError('Invalid DAG-PB form')
}
return { Hash }
}
if (typeof link !== 'object' || Array.isArray(link)) {
throw new TypeError('Invalid DAG-PB form')
}
const pbl = {}
if (link.Hash) {
let cid = CID.asCID(link.Hash)
try {
if (!cid) {
if (typeof link.Hash === 'string') {
cid = CID.parse(link.Hash)
} else if (link.Hash instanceof Uint8Array) {
cid = CID.decode(link.Hash)
}
}
} catch (e) {
throw new TypeError(`Invalid DAG-PB form: ${e.message}`)
}
if (cid) {
pbl.Hash = cid
}
}
if (!pbl.Hash) {
throw new TypeError('Invalid DAG-PB form')
}
if (typeof link.Name === 'string') {
pbl.Name = link.Name
}
if (typeof link.Tsize === 'number') {
pbl.Tsize = link.Tsize
}
return pbl
}
function prepare (node) {
if (node instanceof Uint8Array || typeof node === 'string') {
node = { Data: node }
}
if (typeof node !== 'object' || Array.isArray(node)) {
throw new TypeError('Invalid DAG-PB form')
}
const pbn = {}
if (node.Data) {
if (typeof node.Data === 'string') {
pbn.Data = textEncoder.encode(node.Data)
} else if (node.Data instanceof Uint8Array) {
pbn.Data = node.Data
}
}
if (node.Links && Array.isArray(node.Links) && node.Links.length) {
pbn.Links = node.Links.map(asLink)
pbn.Links.sort(linkComparator)
} else {
pbn.Links = []
}
return pbn
}
function validate (node) {
/*
type PBLink struct {
Hash optional Link
Name optional String
Tsize optional Int
}
type PBNode struct {
Links [PBLink]
Data optional Bytes
}
*/
if (!node || typeof node !== 'object' || Array.isArray(node)) {
throw new TypeError('Invalid DAG-PB form')
}
if (!hasOnlyProperties(node, pbNodeProperties)) {
throw new TypeError('Invalid DAG-PB form (extraneous properties)')
}
if (node.Data !== undefined && !(node.Data instanceof Uint8Array)) {
throw new TypeError('Invalid DAG-PB form (Data must be a Uint8Array)')
}
if (!Array.isArray(node.Links)) {
throw new TypeError('Invalid DAG-PB form (Links must be an array)')
}
for (let i = 0; i < node.Links.length; i++) {
const link = node.Links[i]
if (!link || typeof link !== 'object' || Array.isArray(link)) {
throw new TypeError('Invalid DAG-PB form (bad link object)')
}
if (!hasOnlyProperties(link, pbLinkProperties)) {
throw new TypeError('Invalid DAG-PB form (extraneous properties on link object)')
}
if (!link.Hash) {
throw new TypeError('Invalid DAG-PB form (link must have a Hash)')
}
if (link.Hash.asCID !== link.Hash) {
throw new TypeError('Invalid DAG-PB form (link Hash must be a CID)')
}
if (link.Name !== undefined && typeof link.Name !== 'string') {
throw new TypeError('Invalid DAG-PB form (link Name must be a string)')
}
if (link.Tsize !== undefined && (typeof link.Tsize !== 'number' || link.Tsize % 1 !== 0)) {
throw new TypeError('Invalid DAG-PB form (link Tsize must be an integer)')
}
if (i > 0 && linkComparator(link, node.Links[i - 1]) === -1) {
throw new TypeError('Invalid DAG-PB form (links must be sorted by Name bytes)')
}
}
}
function encode (node) {
validate(node)
const pbn = {}
if (node.Links) {
pbn.Links = node.Links.map((l) => {
const link = {}
if (l.Hash) {
link.Hash = l.Hash.bytes // cid -> bytes
}
if (l.Name !== undefined) {
link.Name = l.Name
}
if (l.Tsize !== undefined) {
link.Tsize = l.Tsize
}
return link
})
}
if (node.Data) {
pbn.Data = node.Data
}
return encodeNode(pbn)
}
function decode (bytes) {
const pbn = decodeNode(bytes)
const node = {}
if (pbn.Data) {
node.Data = pbn.Data
}
if (pbn.Links) {
node.Links = pbn.Links.map((l) => {
const link = {}
try {
link.Hash = CID.decode(l.Hash)
} catch (e) {}
if (!link.Hash) {
throw new Error('Invalid Hash field found in link, expected CID')
}
if (l.Name !== undefined) {
link.Name = l.Name
}
if (l.Tsize !== undefined) {
link.Tsize = l.Tsize
}
return link
})
}
return node
}
export { name, code, encode, decode, prepare, validate }