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 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: `${prev_link}`,
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
@@ -34,5 +34,5 @@
34
34
  "deprecated": false,
35
35
  "description": "![image](https://user-images.githubusercontent.com/104391124/282268780-cbbc1ee6-8d97-4d80-b896-66ae3eb723dd.png)",
36
36
  "name": "pathchain",
37
- "version": "1.1.12"
37
+ "version": "1.1.14"
38
38
  }
package/updater.js CHANGED
@@ -1,149 +1,89 @@
1
- var fs = require("fs");
2
- var pb = require('protocol-buffers');
3
- const getter = require("./getter");
4
-
5
- /** useSecret
6
- * [Function that changes a secret state to used]
7
- *
8
- * @param {string} xsecret
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
- let author_folder = xauthor;
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
- // LOADING PB
22
- var secret_pb = pb(fs.readFileSync('node_modules/pathchain/proto/secret.proto'))
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
- // DECODING SECRET
37
- var secret_enc = fileContents
38
- var secret_obj = secret_pb.secret.decode(secret_enc)
19
+ try {
20
+ const fileContents = fs.readFileSync(filePath);
21
+ const secretObj = secretProto.secret.decode(fileContents);
39
22
 
40
- if(secret_obj.used == false){
41
- // USING INVITE SECRET
42
- secret_obj.used = true;
43
- secret_obj.user = "entities/"+xauthor;
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
- // Public handling only >:c
46
- fs.writeFileSync("files/secrets/" + xsecret, secret_pb.secret.encode(secret_obj));
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
- /** setLinkNext
53
- * [Function that updates the 'next' pointer of a link]
54
- *
55
- * @param {string} xlink
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 = "", xnextlink = "") {
61
- console.log("Setting link: ", xnextlink);
62
- console.log("As prev link for: ", xlink);
41
+ function setLinkNext(xlink = '', xnextlink = '') {
42
+ console.log('Setting link:', xnextlink);
43
+ console.log('As NEXT link for:', xlink);
63
44
 
64
- // LOADING PB
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
- fileContents = fs.readFileSync("files/" + xlink);
73
- } catch (err) {
74
- if (err.code === 'ENOENT') {
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
- // NOT FOUND EXCEPTION FOR 'xnextlink'
82
- try {
83
- fileContents = fs.readFileSync("files/" + xnextlink);
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
- if (err.code === 'ENOENT') {
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
- /** setLinkPrev
102
- * [Function that updates the 'previous' pointer of a link]
103
- *
104
- * @param {string} xlink
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 = "", xprevlink = "") {
110
- console.log("Setting link: ", xprevlink);
111
- console.log("As prev link for: ", xlink);
68
+ function setLinkPrev(xlink = '', xprevlink = '') {
69
+ console.log('Setting link:', xprevlink);
70
+ console.log('As PREV link for:', xlink);
112
71
 
113
- // LOADING PB
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
- fileContents = fs.readFileSync("files/" + xlink);
122
- } catch (err) {
123
- if (err.code === 'ENOENT') {
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
- // NOT FOUND EXCEPTION FOR 'xprevlink'
131
- try {
132
- fileContents = fs.readFileSync("files/" + xprevlink);
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
- if (err.code === 'ENOENT') {
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 };
@@ -1,10 +0,0 @@
1
- syntax = "proto3";
2
-
3
- message pathlink {
4
- required string register = 1;
5
- required string author = 2;
6
- required string ancestor = 3;
7
- required string first = 4;
8
- required string second = 5;
9
- required string tag = 6;
10
- }
package/proto/tree.proto DELETED
@@ -1,11 +0,0 @@
1
- syntax = "proto3";
2
-
3
- message tree {
4
- required string register = 1;
5
- required string author = 2;
6
- required string text = 3;
7
- required string head = 4;
8
- required string ancestor = 5;
9
- optional string chain = 6;
10
- required string tag = 7;
11
- }
@@ -1,10 +0,0 @@
1
- syntax = "proto3";
2
-
3
- message treelink {
4
- required string register = 1;
5
- required string author = 2;
6
- required string ancestor = 3;
7
- required string first = 4;
8
- required string second = 5;
9
- required string tag = 6;
10
- }