pathchain 1.1.12 → 1.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/getter.js +75 -1
- package/index.js +24 -0
- package/maker.js +3 -1
- package/package.json +1 -1
- package/updater.js +61 -121
- package/proto/pathlink.proto +0 -10
- package/proto/tree.proto +0 -11
- package/proto/treelink.proto +0 -10
package/getter.js
CHANGED
|
@@ -155,6 +155,78 @@ function getLabelObj(xlabel, xauthor = '') {
|
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Retrieves and decodes a address object from a file.
|
|
161
|
+
* @param {string} xaddress - The address string.
|
|
162
|
+
* @returns {Object|string} The decoded object or an error message.
|
|
163
|
+
*/
|
|
164
|
+
function getObj(xaddress) {
|
|
165
|
+
// const labelProto = pb(fs.readFileSync('node_modules/pathchain/proto/label.proto'));
|
|
166
|
+
const filePath = `files/${xaddress}`;
|
|
167
|
+
|
|
168
|
+
var splitted_address = target.split("/");
|
|
169
|
+
console.log("Splitted address: ", splitted_target);
|
|
170
|
+
obj_type = splitted_address[splitted_address.length - 2];
|
|
171
|
+
console.log("Object type: ", obj_type);
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
const fileContents = fs.readFileSync(filePath);
|
|
175
|
+
switch (obj_type) {
|
|
176
|
+
case "entities":
|
|
177
|
+
const entityProto = pb(fs.readFileSync('node_modules/pathchain/proto/entity.proto'));
|
|
178
|
+
return entityProto.entity.decode(fileContents);
|
|
179
|
+
case "labels":
|
|
180
|
+
const labelProto = pb(fs.readFileSync('node_modules/pathchain/proto/label.proto'));
|
|
181
|
+
return labelProto.label.decode(fileContents);
|
|
182
|
+
case "links":
|
|
183
|
+
const linkProto = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'));
|
|
184
|
+
return linkProto.link.decode(fileContents);
|
|
185
|
+
case "moments":
|
|
186
|
+
const momentProto = pb(fs.readFileSync('node_modules/pathchain/proto/moment.proto'));
|
|
187
|
+
return momentProto.moment.decode(fileContents);
|
|
188
|
+
case "nodes":
|
|
189
|
+
const nodeProto = pb(fs.readFileSync('node_modules/pathchain/proto/node.proto'));
|
|
190
|
+
return nodeProto.node.decode(fileContents);
|
|
191
|
+
case "paths":
|
|
192
|
+
const pathProto = pb(fs.readFileSync('node_modules/pathchain/proto/path.proto'));
|
|
193
|
+
return pathProto.path.decode(fileContents);
|
|
194
|
+
case "secrets":
|
|
195
|
+
const secretProto = pb(fs.readFileSync('node_modules/pathchain/proto/secret.proto'));
|
|
196
|
+
return secretProto.secret.decode(fileContents);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
return err.code === 'ENOENT' ? "Element not found" : err;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Retrieves and decodes a path object from a file.
|
|
206
|
+
* @param {string} xpath - The path hash.
|
|
207
|
+
* @param {string} xauthor - The author of the path (optional).
|
|
208
|
+
* @returns {Object|string} The decoded path object or an error message.
|
|
209
|
+
*/
|
|
210
|
+
function getPathChainObj(xpath, xauthor = '') {
|
|
211
|
+
const pathProto = pb(fs.readFileSync('node_modules/pathchain/proto/path.proto'));
|
|
212
|
+
const filePath = `files/${xauthor ? xauthor + '/' : ''}paths/${xpath}`;
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
const fileContents = fs.readFileSync(filePath);
|
|
216
|
+
var pathObj = pathProto.path.decode(fileContents);
|
|
217
|
+
|
|
218
|
+
var current_link = this.getObj(pathObj.head);
|
|
219
|
+
console.log("Current link", current_link);
|
|
220
|
+
var current_obj = this.getObj(current_link.target);
|
|
221
|
+
console.log("Current object", current_obj);
|
|
222
|
+
// Get link -> get link.target
|
|
223
|
+
return pathObj;
|
|
224
|
+
} catch (err) {
|
|
225
|
+
return err.code === 'ENOENT' ? "Path not found" : err;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
|
|
158
230
|
module.exports = {
|
|
159
231
|
getCurrentDate,
|
|
160
232
|
getMomentObj,
|
|
@@ -164,5 +236,7 @@ module.exports = {
|
|
|
164
236
|
getNodeObj,
|
|
165
237
|
getLinkObj,
|
|
166
238
|
getPathObj,
|
|
167
|
-
getLabelObj
|
|
239
|
+
getLabelObj,
|
|
240
|
+
getObj,
|
|
241
|
+
getPathChainObj
|
|
168
242
|
};
|
package/index.js
CHANGED
|
@@ -236,6 +236,18 @@ exports.getPathObj = (xpath, xauthor="") => {
|
|
|
236
236
|
return path_object;
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
+
/** getPathChainObj
|
|
240
|
+
* [Function that recieves a path hash and returns the path object]
|
|
241
|
+
*
|
|
242
|
+
* @param {string} xpath (required)
|
|
243
|
+
*
|
|
244
|
+
* @return {obj} path_object (path object)
|
|
245
|
+
*/
|
|
246
|
+
exports.getPathChainObj = (xpath, xauthor="") => {
|
|
247
|
+
const path_object = getter.getPathChainObj(xpath, xauthor);
|
|
248
|
+
return path_object;
|
|
249
|
+
}
|
|
250
|
+
|
|
239
251
|
////////////////////
|
|
240
252
|
/// LABEL ///
|
|
241
253
|
////////////////////
|
|
@@ -262,3 +274,15 @@ exports.getLabelObj = (xlabel, xauthor="") => {
|
|
|
262
274
|
return label_object;
|
|
263
275
|
}
|
|
264
276
|
|
|
277
|
+
/** getObj
|
|
278
|
+
* [Function that recieves a path hash and returns the path object]
|
|
279
|
+
*
|
|
280
|
+
|
|
281
|
+
* Retrieves and decodes a address object from a file.
|
|
282
|
+
* @param {string} xaddress - The address string.
|
|
283
|
+
* @returns {Object|string} The decoded object or an error message.
|
|
284
|
+
*/
|
|
285
|
+
exports.getObj = (xaddress) => {
|
|
286
|
+
const pathchain_object = getter.getObj(xaddress);
|
|
287
|
+
return pathchain_object;
|
|
288
|
+
}
|
package/maker.js
CHANGED
|
@@ -229,9 +229,11 @@ function path(text, elements, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [
|
|
|
229
229
|
if (text == "") { text = path_hash; }
|
|
230
230
|
|
|
231
231
|
let prev_link = "";
|
|
232
|
+
let head_link = "";
|
|
232
233
|
// Building target chain
|
|
233
234
|
for (let i = 0; i < elements.length; i++) {
|
|
234
235
|
const current_link = link(elements[i], "", "", author, "");
|
|
236
|
+
if(i==0) head_link = current_link;
|
|
235
237
|
if (i > 0) {
|
|
236
238
|
updater.setLinkNext(prev_link, current_link);
|
|
237
239
|
updater.setLinkPrev(current_link, prev_link);
|
|
@@ -245,7 +247,7 @@ function path(text, elements, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [
|
|
|
245
247
|
register: `moments/${register_moment_hash}`,
|
|
246
248
|
author: author,
|
|
247
249
|
text: text,
|
|
248
|
-
head: `${
|
|
250
|
+
head: `${head_link}`,
|
|
249
251
|
ancestor: `${author_folder}paths/${ancestor}`,
|
|
250
252
|
tag: `${author_folder}paths/${path_hash}`,
|
|
251
253
|
});
|
package/package.json
CHANGED
package/updater.js
CHANGED
|
@@ -1,149 +1,89 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const getter = require(
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @param {string}
|
|
9
|
-
*
|
|
10
|
-
* @return {string} xsecret
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const pb = require('protocol-buffers');
|
|
3
|
+
const getter = require('./getter');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Changes a secret's state to used and associates it with a user.
|
|
7
|
+
* @param {string} xsecret - The secret hash.
|
|
8
|
+
* @param {string} [xauthor=''] - The author of the secret (optional).
|
|
9
|
+
* @returns {Object|string} The updated secret object or an error message.
|
|
11
10
|
*/
|
|
12
|
-
function useSecret(xsecret, xauthor =
|
|
13
|
-
|
|
14
|
-
if (xauthor == "") {
|
|
11
|
+
function useSecret(xsecret, xauthor = '') {
|
|
12
|
+
if (!xauthor) {
|
|
15
13
|
xauthor = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
|
|
16
|
-
author_folder = "";
|
|
17
|
-
} else {
|
|
18
|
-
author_folder = `${author_folder}/`;
|
|
19
14
|
}
|
|
20
15
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// NOT FOUND EXCEPTION
|
|
25
|
-
var fileContents;
|
|
26
|
-
try {
|
|
27
|
-
fileContents = fs.readFileSync("files/secrets/" + xsecret);
|
|
28
|
-
} catch (err) {
|
|
29
|
-
if (err.code === 'ENOENT') {
|
|
30
|
-
return "Secret '" + xsecret + "' not found";
|
|
31
|
-
} else {
|
|
32
|
-
throw err;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
16
|
+
const secretProto = pb(fs.readFileSync('node_modules/pathchain/proto/secret.proto'));
|
|
17
|
+
const filePath = `files/secrets/${xsecret}`;
|
|
35
18
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
19
|
+
try {
|
|
20
|
+
const fileContents = fs.readFileSync(filePath);
|
|
21
|
+
const secretObj = secretProto.secret.decode(fileContents);
|
|
39
22
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
23
|
+
if (!secretObj.used) {
|
|
24
|
+
secretObj.used = true;
|
|
25
|
+
secretObj.user = `entities/${xauthor}`;
|
|
26
|
+
fs.writeFileSync(filePath, secretProto.secret.encode(secretObj));
|
|
27
|
+
}
|
|
44
28
|
|
|
45
|
-
|
|
46
|
-
|
|
29
|
+
return secretObj;
|
|
30
|
+
} catch (err) {
|
|
31
|
+
return err.code === 'ENOENT' ? `Secret '${xsecret}' not found` : err;
|
|
47
32
|
}
|
|
48
|
-
return secret_obj;
|
|
49
33
|
}
|
|
50
34
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @
|
|
56
|
-
* @param {string} xnextlink
|
|
57
|
-
*
|
|
58
|
-
* @return {string} xlink
|
|
35
|
+
/**
|
|
36
|
+
* Updates the 'next' pointer of a link.
|
|
37
|
+
* @param {string} xlink - The current link hash.
|
|
38
|
+
* @param {string} xnextlink - The next link hash.
|
|
39
|
+
* @returns {Object|string} The updated link object or an error message.
|
|
59
40
|
*/
|
|
60
|
-
function setLinkNext(xlink =
|
|
61
|
-
console.log(
|
|
62
|
-
console.log(
|
|
41
|
+
function setLinkNext(xlink = '', xnextlink = '') {
|
|
42
|
+
console.log('Setting link:', xnextlink);
|
|
43
|
+
console.log('As NEXT link for:', xlink);
|
|
63
44
|
|
|
64
|
-
|
|
65
|
-
var link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'))
|
|
66
|
-
|
|
67
|
-
// EXCEPTIONS FOR NOT FOUND LINKS
|
|
68
|
-
var fileContents;
|
|
45
|
+
const linkProto = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'));
|
|
69
46
|
|
|
70
|
-
// NOT FOUND EXCEPTION FOR 'xlink'
|
|
71
47
|
try {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return "Link '" + xlink + "' not found";
|
|
76
|
-
} else {
|
|
77
|
-
throw err;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
48
|
+
// Check if both links exist
|
|
49
|
+
fs.readFileSync(`files/${xlink}`);
|
|
50
|
+
fs.readFileSync(`files/${xnextlink}`);
|
|
80
51
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
52
|
+
const fileContents = fs.readFileSync(`files/${xlink}`);
|
|
53
|
+
const linkObj = linkProto.link.decode(fileContents);
|
|
54
|
+
linkObj.next = xnextlink;
|
|
55
|
+
|
|
56
|
+
return linkObj;
|
|
84
57
|
} catch (err) {
|
|
85
|
-
|
|
86
|
-
return "Link '" + xnextlink + "' not found";
|
|
87
|
-
} else {
|
|
88
|
-
throw err;
|
|
89
|
-
}
|
|
58
|
+
return err.code === 'ENOENT' ? `Link '${err.path.split('/').pop()}' not found` : err;
|
|
90
59
|
}
|
|
91
|
-
|
|
92
|
-
// DECODING LINK
|
|
93
|
-
var link_enc = fileContents
|
|
94
|
-
var link_obj = link_pb.link.decode(link_enc)
|
|
95
|
-
|
|
96
|
-
link_obj.next = xnextlink;
|
|
97
|
-
return link_obj;
|
|
98
60
|
}
|
|
99
61
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* @
|
|
105
|
-
* @param {string} xprevlink
|
|
106
|
-
*
|
|
107
|
-
* @return {string} xlink
|
|
62
|
+
/**
|
|
63
|
+
* Updates the 'previous' pointer of a link.
|
|
64
|
+
* @param {string} xlink - The current link hash.
|
|
65
|
+
* @param {string} xprevlink - The previous link hash.
|
|
66
|
+
* @returns {Object|string} The updated link object or an error message.
|
|
108
67
|
*/
|
|
109
|
-
function setLinkPrev(xlink =
|
|
110
|
-
console.log(
|
|
111
|
-
console.log(
|
|
68
|
+
function setLinkPrev(xlink = '', xprevlink = '') {
|
|
69
|
+
console.log('Setting link:', xprevlink);
|
|
70
|
+
console.log('As PREV link for:', xlink);
|
|
112
71
|
|
|
113
|
-
|
|
114
|
-
var link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'))
|
|
115
|
-
|
|
116
|
-
// EXCEPTIONS FOR NOT FOUND LINKS
|
|
117
|
-
var fileContents;
|
|
72
|
+
const linkProto = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'));
|
|
118
73
|
|
|
119
|
-
// NOT FOUND EXCEPTION FOR 'xlink'
|
|
120
74
|
try {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return "Link '" + xlink + "' not found";
|
|
125
|
-
} else {
|
|
126
|
-
throw err;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
75
|
+
// Check if both links exist
|
|
76
|
+
fs.readFileSync(`files/${xlink}`);
|
|
77
|
+
fs.readFileSync(`files/${xprevlink}`);
|
|
129
78
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
79
|
+
const fileContents = fs.readFileSync(`files/${xlink}`);
|
|
80
|
+
const linkObj = linkProto.link.decode(fileContents);
|
|
81
|
+
linkObj.prev = xprevlink;
|
|
82
|
+
|
|
83
|
+
return linkObj;
|
|
133
84
|
} catch (err) {
|
|
134
|
-
|
|
135
|
-
return "Link '" + xprevlink + "' not found";
|
|
136
|
-
} else {
|
|
137
|
-
throw err;
|
|
138
|
-
}
|
|
85
|
+
return err.code === 'ENOENT' ? `Link '${err.path.split('/').pop()}' not found` : err;
|
|
139
86
|
}
|
|
140
|
-
|
|
141
|
-
// DECODING LINK
|
|
142
|
-
var link_enc = fileContents
|
|
143
|
-
var link_obj = link_pb.link.decode(link_enc)
|
|
144
|
-
|
|
145
|
-
link_obj.prev = xprevlink;
|
|
146
|
-
return link_obj;
|
|
147
87
|
}
|
|
148
88
|
|
|
149
|
-
module.exports = { useSecret, setLinkNext, setLinkPrev }
|
|
89
|
+
module.exports = { useSecret, setLinkNext, setLinkPrev };
|
package/proto/pathlink.proto
DELETED
package/proto/tree.proto
DELETED
package/proto/treelink.proto
DELETED