This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathobject.js
302 lines (240 loc) · 7.14 KB
/
object.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
'use strict'
const callbackify = require('callbackify')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const DAGLink = dagPB.DAGLink
const CID = require('cids')
const mh = require('multihashes')
const multicodec = require('multicodec')
const Unixfs = require('ipfs-unixfs')
const errCode = require('err-code')
function normalizeMultihash (multihash, enc) {
if (typeof multihash === 'string') {
if (enc === 'base58' || !enc) {
return multihash
}
return Buffer.from(multihash, enc)
} else if (Buffer.isBuffer(multihash)) {
return multihash
} else if (CID.isCID(multihash)) {
return multihash.buffer
} else {
throw new Error('unsupported multihash')
}
}
function parseBuffer (buf, encoding) {
switch (encoding) {
case 'json':
return parseJSONBuffer(buf)
case 'protobuf':
return parseProtoBuffer(buf)
default:
throw new Error(`unkown encoding: ${encoding}`)
}
}
function parseJSONBuffer (buf) {
let data
let links
try {
const parsed = JSON.parse(buf.toString())
links = (parsed.Links || []).map((link) => {
return new DAGLink(
link.Name || link.name,
link.Size || link.size,
mh.fromB58String(link.Hash || link.hash || link.multihash)
)
})
data = Buffer.from(parsed.Data)
} catch (err) {
throw new Error('failed to parse JSON: ' + err)
}
return new DAGNode(data, links)
}
function parseProtoBuffer (buf) {
return dagPB.util.deserialize(buf)
}
function findLinks (node, links = []) {
for (const key in node) {
const val = node[key]
if (key === '/' && Object.keys(node).length === 1) {
try {
links.push(new DAGLink('', 0, new CID(val)))
continue
} catch (_) {
// not a CID
}
}
if (CID.isCID(val)) {
links.push(new DAGLink('', 0, val))
continue
}
if (Array.isArray(val)) {
findLinks(val, links)
}
if (typeof val === 'object' && !(val instanceof String)) {
findLinks(val, links)
}
}
return links
}
module.exports = function object (self) {
async function editAndSave (multihash, edit, options) {
options = options || {}
const node = await self.object.get(multihash, options)
// edit applies the edit func passed to
// editAndSave
const cid = await self._ipld.put(edit(node), multicodec.DAG_PB, {
cidVersion: 0,
hashAlg: multicodec.SHA2_256
})
if (options.preload !== false) {
self._preload(cid)
}
return cid
}
return {
new: callbackify.variadic(async (template, options) => {
options = options || {}
// allow options in the template position
if (template && typeof template !== 'string') {
options = template
template = null
}
let data
if (template) {
if (template === 'unixfs-dir') {
data = (new Unixfs('directory')).marshal()
} else {
throw new Error('unknown template')
}
} else {
data = Buffer.alloc(0)
}
const node = new DAGNode(data)
const cid = await self._ipld.put(node, multicodec.DAG_PB, {
cidVersion: 0,
hashAlg: multicodec.SHA2_256
})
if (options.preload !== false) {
self._preload(cid)
}
return cid
}),
put: callbackify.variadic(async (obj, options) => {
options = options || {}
const encoding = options.enc
let node
if (Buffer.isBuffer(obj)) {
if (encoding) {
node = await parseBuffer(obj, encoding)
} else {
node = new DAGNode(obj)
}
} else if (DAGNode.isDAGNode(obj)) {
// already a dag node
node = obj
} else if (typeof obj === 'object') {
node = new DAGNode(obj.Data, obj.Links)
} else {
throw new Error('obj not recognized')
}
const release = await self._gcLock.readLock()
try {
const cid = await self._ipld.put(node, multicodec.DAG_PB, {
cidVersion: 0,
hashAlg: multicodec.SHA2_256
})
if (options.preload !== false) {
self._preload(cid)
}
return cid
} finally {
release()
}
}),
get: callbackify.variadic(async (multihash, options) => { // eslint-disable-line require-await
options = options || {}
let mh, cid
try {
mh = normalizeMultihash(multihash, options.enc)
} catch (err) {
throw errCode(err, 'ERR_INVALID_MULTIHASH')
}
try {
cid = new CID(mh)
} catch (err) {
throw errCode(err, 'ERR_INVALID_CID')
}
if (options.cidVersion === 1) {
cid = cid.toV1()
}
if (options.preload !== false) {
self._preload(cid)
}
return self._ipld.get(cid)
}),
data: callbackify.variadic(async (multihash, options) => {
options = options || {}
const node = await self.object.get(multihash, options)
return node.Data
}),
links: callbackify.variadic(async (multihash, options) => {
options = options || {}
const cid = new CID(multihash)
const result = await self.dag.get(cid, options)
if (cid.codec === 'raw') {
return []
}
if (cid.codec === 'dag-pb') {
return result.value.Links
}
if (cid.codec === 'dag-cbor') {
return findLinks(result)
}
throw new Error(`Cannot resolve links from codec ${cid.codec}`)
}),
stat: callbackify.variadic(async (multihash, options) => {
options = options || {}
const node = await self.object.get(multihash, options)
const serialized = dagPB.util.serialize(node)
const cid = await dagPB.util.cid(serialized, {
cidVersion: 0
})
const blockSize = serialized.length
const linkLength = node.Links.reduce((a, l) => a + l.Tsize, 0)
return {
Hash: cid.toBaseEncodedString(),
NumLinks: node.Links.length,
BlockSize: blockSize,
LinksSize: blockSize - node.Data.length,
DataSize: node.Data.length,
CumulativeSize: blockSize + linkLength
}
}),
patch: {
addLink: callbackify.variadic(async (multihash, link, options) => { // eslint-disable-line require-await
return editAndSave(multihash, (node) => {
node.addLink(link)
return node
}, options)
}),
rmLink: callbackify.variadic(async (multihash, linkRef, options) => { // eslint-disable-line require-await
return editAndSave(multihash, (node) => {
node.rmLink(linkRef.Name || linkRef.name)
return node
}, options)
}),
appendData: callbackify.variadic(async (multihash, data, options) => { // eslint-disable-line require-await
return editAndSave(multihash, (node) => {
const newData = Buffer.concat([node.Data, data])
return new DAGNode(newData, node.Links)
}, options)
}),
setData: callbackify.variadic(async (multihash, data, options) => { // eslint-disable-line require-await
return editAndSave(multihash, (node) => {
return new DAGNode(data, node.Links)
}, options)
})
}
}
}