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 pathdag.js
170 lines (136 loc) · 4.17 KB
/
dag.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
'use strict'
const callbackify = require('callbackify')
const CID = require('cids')
const all = require('async-iterator-all')
const errCode = require('err-code')
const multicodec = require('multicodec')
function parseArgs (cid, path, options) {
options = options || {}
// Allow options in path position
if (path !== undefined && typeof path !== 'string') {
options = path
path = undefined
}
if (typeof cid === 'string') {
if (cid.startsWith('/ipfs/')) {
cid = cid.substring(6)
}
const split = cid.split('/')
try {
cid = new CID(split[0])
} catch (err) {
throw errCode(err, 'ERR_INVALID_CID')
}
split.shift()
if (split.length > 0) {
path = split.join('/')
} else {
path = path || '/'
}
} else if (Buffer.isBuffer(cid)) {
try {
cid = new CID(cid)
} catch (err) {
throw errCode(err, 'ERR_INVALID_CID')
}
}
return [
cid,
path,
options
]
}
module.exports = function dag (self) {
return {
put: callbackify.variadic(async (dagNode, options) => {
options = options || {}
if (options.cid && (options.format || options.hashAlg)) {
throw new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hashAlg` options.')
} else if (((options.format && !options.hashAlg) || (!options.format && options.hashAlg))) {
throw new Error('Can\'t put dag node. Please provide `format` AND `hashAlg` options.')
}
const optionDefaults = {
format: multicodec.DAG_CBOR,
hashAlg: multicodec.SHA2_256
}
// The IPLD expects the format and hashAlg as constants
if (options.format && typeof options.format === 'string') {
const constantName = options.format.toUpperCase().replace(/-/g, '_')
options.format = multicodec[constantName]
}
if (options.hashAlg && typeof options.hashAlg === 'string') {
const constantName = options.hashAlg.toUpperCase().replace(/-/g, '_')
options.hashAlg = multicodec[constantName]
}
options = options.cid ? options : Object.assign({}, optionDefaults, options)
// js-ipld defaults to verion 1 CIDs. Hence set version 0 explicitly for
// dag-pb nodes
if (options.version === undefined) {
if (options.format === multicodec.DAG_PB && options.hashAlg === multicodec.SHA2_256) {
options.version = 0
} else {
options.version = 1
}
}
let release
if (options.pin) {
release = await self._gcLock.readLock()
}
try {
const cid = await self._ipld.put(dagNode, options.format, {
hashAlg: options.hashAlg,
cidVersion: options.version
})
if (options.pin) {
await self.pin.add(cid, {
lock: false
})
}
if (options.preload !== false) {
self._preload(cid)
}
return cid
} finally {
if (release) {
release()
}
}
}),
get: callbackify.variadic(async (cid, path, options) => {
[cid, path, options] = parseArgs(cid, path, options)
if (options.preload !== false) {
self._preload(cid)
}
if (path == null || path === '/') {
const value = await self._ipld.get(cid)
return {
value,
remainderPath: ''
}
} else {
let result
for await (const entry of self._ipld.resolve(cid, path)) {
if (options.localResolve) {
return entry
}
result = entry
}
return result
}
}),
tree: callbackify.variadic(async (cid, path, options) => { // eslint-disable-line require-await
[cid, path, options] = parseArgs(cid, path, options)
if (options.preload !== false) {
self._preload(cid)
}
return all(self._ipld.tree(cid, path, options))
}),
resolve: callbackify.variadic(async (cid, path, options) => { // eslint-disable-line require-await
[cid, path, options] = parseArgs(cid, path, options)
if (options.preload !== false) {
self._preload(cid)
}
return all(self._ipld.resolve(cid, path))
})
}
}