This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathresolver.js
144 lines (118 loc) · 3.46 KB
/
resolver.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
'use strict'
const waterfall = require('async/waterfall')
const CID = require('cids')
const util = require('./util')
exports = module.exports
exports.multicodec = 'dag-pb'
exports.defaultHashAlg = 'sha2-256'
/*
* resolve: receives a path and a binary blob and returns the value on path,
* throw if not possible. `binaryBlob` is the ProtocolBuffer encoded data.
*/
exports.resolve = (binaryBlob, path, callback) => {
waterfall([
(cb) => util.deserialize(binaryBlob, cb),
(node, cb) => {
// Return the deserialized block if no path is given
if (!path) {
return callback(null, {
value: node,
remainderPath: ''
})
}
const split = path.split('/')
if (split[0] === 'Links') {
let remainderPath = ''
// all links
if (!split[1]) {
return cb(null, {
value: node.links.map((l) => l.toJSON()),
remainderPath: ''
})
}
// select one link
const values = {}
// populate both index number and name to enable both cases
// for the resolver
node.links.forEach((l, i) => {
const link = l.toJSON()
values[i] = {
hash: link.multihash,
name: link.name,
size: link.size
}
// TODO by enabling something to resolve through link name, we are
// applying a transformation (a view) to the data, confirm if this
// is exactly what we want
values[link.name] = link.multihash
})
let value = values[split[1]]
// if remainderPath exists, value needs to be CID
if (split[2] === 'Hash') {
value = { '/': value.hash }
} else if (split[2] === 'Tsize') {
value = { '/': value.size }
} else if (split[2] === 'Name') {
value = { '/': value.name }
}
remainderPath = split.slice(3).join('/')
cb(null, { value: value, remainderPath: remainderPath })
} else if (split[0] === 'Data') {
cb(null, { value: node.data, remainderPath: '' })
} else {
cb(new Error('path not available'))
}
}
], callback)
}
/*
* tree: returns a flattened array with paths: values of the project. options
* is an object that can carry several options (i.e. nestness)
*/
exports.tree = (binaryBlob, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
util.deserialize(binaryBlob, (err, node) => {
if (err) {
return callback(err)
}
const paths = []
paths.push('Links')
node.links.forEach((link, i) => {
paths.push(`Links/${i}/Name`)
paths.push(`Links/${i}/Tsize`)
paths.push(`Links/${i}/Hash`)
})
paths.push('Data')
callback(null, paths)
})
}
/*
* isLink: returns the Link if a given path in a binary blob is a Link,
* false otherwise
*/
exports.isLink = (binaryBlob, path, callback) => {
exports.resolve(binaryBlob, path, (err, result) => {
if (err) {
return callback(err)
}
if (result.remainderPath.length > 0) {
return callback(new Error('path out of scope'))
}
if (typeof result.value === 'object' && result.value['/']) {
let valid
try {
valid = CID.isCID(new CID(result.value['/']))
} catch (err) {
valid = false
}
if (valid) {
return callback(null, result.value)
}
}
callback(null, false)
})
}