pathchain 1.1.5 → 1.1.7

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.
Files changed (4) hide show
  1. package/checker.js +47 -76
  2. package/getter.js +107 -240
  3. package/maker.js +14 -16
  4. package/package.json +1 -1
package/checker.js CHANGED
@@ -1,101 +1,72 @@
1
- var fs = require("fs");
2
- var emptyDir = require('empty-dir');
3
- var pb = require('protocol-buffers');
1
+ const fs = require('fs');
2
+ const emptyDir = require('empty-dir');
3
+ const pb = require('protocol-buffers');
4
4
 
5
- // "./files/users"
6
-
7
- /** checkDir
8
- * [Function that creates directory if it does not exist]
9
- *
10
- * @param {string} dir (required)
11
- *
12
- * @return void
5
+ /**
6
+ * Creates a directory if it does not exist.
7
+ * @param {string} dir - The directory path to check and create.
13
8
  */
14
- function checkDir(dir){
15
- if (!fs.existsSync(dir)){
9
+ function checkDir(dir) {
10
+ if (!fs.existsSync(dir)) {
16
11
  fs.mkdirSync(dir, { recursive: true });
17
12
  }
18
13
  }
19
14
 
20
- /** checkFiles
21
- * [Function that returns files in directory]
22
- *
23
- * @param {string} dir (required)
24
- *
25
- * @return void
15
+ /**
16
+ * Returns an array of files in the specified directory.
17
+ * @param {string} dir - The directory path to check.
18
+ * @returns {string[]} An array of file names in the directory.
26
19
  */
27
- function checkFiles(dir){
28
- var files = fs.readdirSync(dir);
29
- return files;
20
+ function checkFiles(dir) {
21
+ return fs.readdirSync(dir);
30
22
  }
31
23
 
32
- /** checkEmptyDir
33
- * [Function that returns TRUE if dir is empty, FALSE if not]
34
- *
35
- * @param {string} dir (required)
36
- *
37
- * @return {bool} empty_dir
24
+ /**
25
+ * Checks if the specified directory is empty.
26
+ * @param {string} dir - The directory path to check.
27
+ * @returns {boolean} True if the directory is empty, false otherwise.
38
28
  */
39
29
  function checkEmptyDir(dir) {
40
- var empty_dir = emptyDir.sync(dir);
41
- return empty_dir
30
+ return emptyDir.sync(dir);
42
31
  }
43
32
 
44
- /** checkFile
45
- * [Function that returns TRUE if file exists, FALSE if not]
46
- *
47
- * @param {string} dir (required)
48
- *
49
- * @return {bool} empty_dir
33
+ /**
34
+ * Checks if a file exists at the specified path.
35
+ * @param {string} dir - The file path to check.
36
+ * @returns {boolean} True if the file exists, false otherwise.
50
37
  */
51
- function checkFile(dir){
38
+ function checkFile(dir) {
52
39
  try {
53
- if (fs.existsSync(dir)) {
54
- return true;
55
- }
56
- } catch(err) {
57
- console.error(err)
40
+ return fs.existsSync(dir);
41
+ } catch (err) {
42
+ console.error(err);
43
+ return false;
58
44
  }
59
- return false;
60
45
  }
61
46
 
62
-
63
- /** isSecretUsed
64
- * [Function that returns TRUE if secret has been used, FALSE if not]
65
- *
66
- * @param {string} xsecret (required)
67
- *
68
- * @return {bool} used_secret
47
+ /**
48
+ * Checks if a secret has been used.
49
+ * @param {string} xsecret - The secret hash.
50
+ * @param {string} [xauthor=''] - The author of the secret (optional).
51
+ * @returns {boolean|string} True if the secret has been used, false if not, or an error message.
69
52
  */
70
- function isSecretUsed(xsecret, xauthor = "") {
71
- if(xauthor != ""){
72
- xauthor = xauthor + '/';
73
- }
53
+ function isSecretUsed(xsecret, xauthor = '') {
54
+ const secretProto = pb(fs.readFileSync('node_modules/pathchain/proto/secret.proto'));
55
+ const filePath = `files/${xauthor ? xauthor + '/' : ''}secrets/${xsecret}`;
74
56
 
75
- // LOADING PB
76
- var secret_pb = pb(fs.readFileSync('node_modules/pathchain/proto/secret.proto'))
77
-
78
- // NOT FOUND EXCEPTION
79
- var fileContents;
80
57
  try {
81
- fileContents = fs.readFileSync("files/" + xauthor + "secrets/" + xsecret);
58
+ const fileContents = fs.readFileSync(filePath);
59
+ const secretObj = secretProto.secret.decode(fileContents);
60
+ return secretObj.used;
82
61
  } catch (err) {
83
- if (err.code === 'ENOENT') {
84
- return "Secret not found";
85
- } else {
86
- throw err;
87
- }
62
+ return err.code === 'ENOENT' ? 'Secret not found' : err;
88
63
  }
89
-
90
- // DECODING SECRET
91
- var secret_enc = fileContents
92
- var secret_obj = secret_pb.secret.decode(secret_enc)
93
-
94
- return secret_obj.used;
95
64
  }
96
65
 
97
- // CHECKING THE SECRET CHECKS THE SECRET
98
-
99
- module.exports = { checkDir, checkFiles, checkFile, checkEmptyDir, isSecretUsed };
100
-
101
-
66
+ module.exports = {
67
+ checkDir,
68
+ checkFiles,
69
+ checkFile,
70
+ checkEmptyDir,
71
+ isSecretUsed
72
+ };
package/getter.js CHANGED
@@ -1,301 +1,168 @@
1
- var fs = require("fs");
2
- var pb = require('protocol-buffers');
3
- var dt = require('date-and-time');
4
- // const { getLinkObj } = require(".");
5
-
6
- /** getCurrentDate
7
- * [Function that returns current datetime string on "MMM DD YYYY HH:mm:ss [GMT]Z" format]
8
- *
9
- * @return {string} current_datetime
1
+ const fs = require('fs');
2
+ const pb = require('protocol-buffers');
3
+ const dt = require('date-and-time');
4
+
5
+ /**
6
+ * Returns the current datetime string in the specified format.
7
+ * @param {string} [format='MM DD YYYY HH:mm:SSS [GMT]Z'] - The desired date format.
8
+ * @returns {string} The formatted current datetime.
10
9
  */
11
- function getCurrentDate(format = 'MM DD YYYY HH:mm:SSS [GMT]Z'){
12
- var current_datetime = new Date()
13
- current_datetime = dt.format(current_datetime, dt.compile(format, true));
14
-
15
- return current_datetime;
10
+ function getCurrentDate(format = 'MM DD YYYY HH:mm:SSS [GMT]Z') {
11
+ const currentDatetime = new Date();
12
+ return dt.format(currentDatetime, dt.compile(format, true));
16
13
  }
17
14
 
18
- /** getMomentObj
19
- * [Function that recieves a moment hash and returns the moment object]
20
- *
21
- * @param {string} xmoment (required)
22
- *
23
- * @return void
15
+ /**
16
+ * Retrieves and decodes a moment object from a file.
17
+ * @param {string} xmoment - The moment hash or path.
18
+ * @returns {Object|string} The decoded moment object or an error message.
24
19
  */
25
20
  function getMomentObj(xmoment) {
26
- // "moments/hash" FORMAT EXCEPTION
27
- if(xmoment.split("/").length > 1){
28
- xmoment = xmoment.split("/")[1]
29
- }
30
-
31
- // LOADING PB
32
- var moment_pb = pb(fs.readFileSync('node_modules/pathchain/proto/moment.proto'))
21
+ // Handle "moments/hash" format
22
+ const momentHash = xmoment.includes('/') ? xmoment.split('/')[1] : xmoment;
23
+
24
+ const momentProto = pb(fs.readFileSync('node_modules/pathchain/proto/moment.proto'));
33
25
 
34
- // NOT FOUND EXCEPTION
35
- var fileContents;
36
26
  try {
37
- fileContents = fs.readFileSync("files/moments/"+xmoment);
27
+ const fileContents = fs.readFileSync(`files/moments/${momentHash}`);
28
+ return momentProto.moment.decode(fileContents);
38
29
  } catch (err) {
39
- if (err.code === 'ENOENT') {
40
- return "Moment buffer not found";
41
- } else {
42
- throw err;
43
- }
30
+ return err.code === 'ENOENT' ? "Moment buffer not found" : err;
44
31
  }
45
-
46
- // DECODING MOMENT
47
- var moment_enc = fileContents
48
- var moment_obj = moment_pb.moment.decode(moment_enc)
49
-
50
- return moment_obj;
51
32
  }
52
33
 
53
- /** getPioneerObj
54
- * [Function that recieves a pioneer hash and returns the pioneer object]
55
- *
56
- * @param {string} xpioneer (required)
57
- *
58
- * @return void
34
+ /**
35
+ * Retrieves and decodes a pioneer object from a file.
36
+ * @param {string} xpioneer - The pioneer hash.
37
+ * @returns {Object|string} The decoded pioneer object or an error message.
59
38
  */
60
39
  function getPioneerObj(xpioneer) {
61
-
62
- // LOADING PB
63
- var pioneer_pb = pb(fs.readFileSync('node_modules/pathchain/proto/entity.proto'))
40
+ const pioneerProto = pb(fs.readFileSync('node_modules/pathchain/proto/entity.proto'));
64
41
 
65
- // NOT FOUND EXCEPTION
66
- var fileContents;
67
42
  try {
68
- fileContents = fs.readFileSync("files/pioneer/"+xpioneer);
43
+ const fileContents = fs.readFileSync(`files/pioneer/${xpioneer}`);
44
+ return pioneerProto.entity.decode(fileContents);
69
45
  } catch (err) {
70
- if (err.code === 'ENOENT') {
71
- return "Pioneer not found";
72
- } else {
73
- throw err;
74
- }
46
+ return err.code === 'ENOENT' ? "Pioneer not found" : err;
75
47
  }
76
-
77
-
78
- // DECODING PIONEER
79
- var pioneer_enc = fileContents
80
- var pioneer_obj = pioneer_pb.entity.decode(pioneer_enc)
81
-
82
- return pioneer_obj;
83
48
  }
84
49
 
85
-
86
- /** getSecretObj
87
- * [Function that recieves a secret hash and returns the secret object]
88
- *
89
- * @param {string} xsecret (required)
90
- *
91
- * @return void
50
+ /**
51
+ * Retrieves and decodes a secret object from a file.
52
+ * @param {string} xsecret - The secret hash.
53
+ * @param {string} xauthor - The author of the secret (optional).
54
+ * @returns {Object|string} The decoded secret object or an error message.
92
55
  */
93
- function getSecretObj(xsecret, xauthor) {
94
-
95
- if(xauthor != "" && (xauthor !== null)){
96
- xauthor = xauthor + '/';
97
- }
98
-
99
- // LOADING PB
100
- var secret_pb = pb(fs.readFileSync('node_modules/pathchain/proto/secret.proto'))
56
+ function getSecretObj(xsecret, xauthor = '') {
57
+ const secretProto = pb(fs.readFileSync('node_modules/pathchain/proto/secret.proto'));
58
+ const filePath = `files/${xauthor ? xauthor + '/' : ''}secrets/${xsecret}`;
101
59
 
102
- // NOT FOUND EXCEPTION
103
- var fileContents;
104
60
  try {
105
- fileContents = fs.readFileSync("files/" + xauthor + "secrets/" + xsecret);
61
+ const fileContents = fs.readFileSync(filePath);
62
+ return secretProto.secret.decode(fileContents);
106
63
  } catch (err) {
107
- if (err.code === 'ENOENT') {
108
- return "Secret not found";
109
- } else {
110
- throw err;
111
- }
64
+ return err.code === 'ENOENT' ? "Secret not found" : err;
112
65
  }
113
-
114
- // DECODING SECRET
115
- var secret_enc = fileContents
116
- var secret_obj = secret_pb.secret.decode(secret_enc)
117
-
118
- return secret_obj;
119
66
  }
120
67
 
121
-
122
- /** getEntityObj
123
- * [Function that recieves a entity hash and returns the entity object]
124
- *
125
- * @param {string} xentity (required)
126
- *
127
- * @return void
68
+ /**
69
+ * Retrieves and decodes an entity object from a file.
70
+ * @param {string} xentity - The entity hash.
71
+ * @param {string} xauthor - The author of the entity (optional).
72
+ * @returns {Object|string} The decoded entity object or an error message.
128
73
  */
129
- function getEntityObj(xentity, xauthor) {
130
- if(xauthor != ""){
131
- xauthor = xauthor + '/';
132
- }
133
-
134
- // LOADING PB
135
- var entity_pb = pb(fs.readFileSync('node_modules/pathchain/proto/entity.proto'))
74
+ function getEntityObj(xentity, xauthor = '') {
75
+ const entityProto = pb(fs.readFileSync('node_modules/pathchain/proto/entity.proto'));
76
+ const filePath = `files/${xauthor ? xauthor + '/' : ''}entities/${xentity}`;
136
77
 
137
- // NOT FOUND EXCEPTION
138
- var fileContents;
139
78
  try {
140
- fileContents = fs.readFileSync("files/" + xauthor + "entities/" + xentity);
79
+ const fileContents = fs.readFileSync(filePath);
80
+ return entityProto.entity.decode(fileContents);
141
81
  } catch (err) {
142
- if (err.code === 'ENOENT') {
143
- return "Entity not found";
144
- } else {
145
- throw err;
146
- }
82
+ return err.code === 'ENOENT' ? "Entity not found" : err;
147
83
  }
148
-
149
- // DECODING ENTITY
150
- var entity_enc = fileContents
151
- var entity_obj = entity_pb.entity.decode(entity_enc)
152
-
153
- return entity_obj;
154
84
  }
155
85
 
156
- /** getNodeObj
157
- * [Function that recieves a node hash and returns the node object]
158
- *
159
- * @param {string} xnode (required)
160
- *
161
- * @return void
86
+ /**
87
+ * Retrieves and decodes a node object from a file.
88
+ * @param {string} xnode - The node hash.
89
+ * @param {string} xauthor - The author of the node (optional).
90
+ * @returns {Object|string} The decoded node object or an error message.
162
91
  */
163
- function getNodeObj(xnode, xauthor) {
164
-
165
- if(xauthor != ""){
166
- xauthor = xauthor + '/';
167
- }
168
-
169
- // LOADING PB
170
- var node_pb = pb(fs.readFileSync('node_modules/pathchain/proto/node.proto'))
92
+ function getNodeObj(xnode, xauthor = '') {
93
+ const nodeProto = pb(fs.readFileSync('node_modules/pathchain/proto/node.proto'));
94
+ const filePath = `files/${xauthor ? xauthor + '/' : ''}nodes/${xnode}`;
171
95
 
172
- // NOT FOUND EXCEPTION
173
- var fileContents;
174
96
  try {
175
- fileContents = fs.readFileSync("files/" + xauthor + "nodes/" + xnode);
97
+ const fileContents = fs.readFileSync(filePath);
98
+ return nodeProto.node.decode(fileContents);
176
99
  } catch (err) {
177
- if (err.code === 'ENOENT') {
178
- return "Node not found";
179
- } else {
180
- throw err;
181
- }
100
+ return err.code === 'ENOENT' ? "Node not found" : err;
182
101
  }
183
-
184
- // DECODING NODE
185
- var node_enc = fileContents
186
- var node_obj = node_pb.node.decode(node_enc)
187
-
188
- return node_obj;
189
102
  }
190
103
 
191
-
192
- /** getLinkObj
193
- * [Function that recieves a link hash and returns the link object]
194
- *
195
- * @param {string} xlink (required)
196
- *
197
- * @return void
104
+ /**
105
+ * Retrieves and decodes a link object from a file.
106
+ * @param {string} xlink - The link hash.
107
+ * @param {string} xauthor - The author of the link (optional).
108
+ * @returns {Object|string} The decoded link object or an error message.
198
109
  */
199
- function getLinkObj(xlink, xauthor) {
200
-
201
- if(xauthor != ""){
202
- xauthor = xauthor + '/';
203
- }
204
-
205
- // LOADING PB
206
- var link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'))
110
+ function getLinkObj(xlink, xauthor = '') {
111
+ const linkProto = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'));
112
+ const filePath = `files/${xauthor ? xauthor + '/' : ''}links/${xlink}`;
207
113
 
208
- // NOT FOUND EXCEPTION
209
- var fileContents;
210
114
  try {
211
- fileContents = fs.readFileSync("files/" + xauthor + "links/" + xlink);
115
+ const fileContents = fs.readFileSync(filePath);
116
+ return linkProto.link.decode(fileContents);
212
117
  } catch (err) {
213
- if (err.code === 'ENOENT') {
214
- return "Link not found";
215
- } else {
216
- throw err;
217
- }
118
+ return err.code === 'ENOENT' ? "Link not found" : err;
218
119
  }
219
-
220
- // DECODING NODE
221
- var link_enc = fileContents
222
- var link_obj = link_pb.link.decode(link_enc)
223
-
224
- return link_obj;
225
120
  }
226
121
 
227
-
228
- /** getPathObj
229
- * [Function that recieves a path hash and returns the path object]
230
- *
231
- * @param {string} xpath (required)
232
- *
233
- * @return void
122
+ /**
123
+ * Retrieves and decodes a path object from a file.
124
+ * @param {string} xpath - The path hash.
125
+ * @param {string} xauthor - The author of the path (optional).
126
+ * @returns {Object|string} The decoded path object or an error message.
234
127
  */
235
- function getPathObj(xpath, xauthor) {
236
-
237
- if(xauthor != ""){
238
- xauthor = xauthor + '/';
239
- }
240
-
241
- // LOADING PB
242
- var path_pb = pb(fs.readFileSync('node_modules/pathchain/proto/path.proto'))
128
+ function getPathObj(xpath, xauthor = '') {
129
+ const pathProto = pb(fs.readFileSync('node_modules/pathchain/proto/path.proto'));
130
+ const filePath = `files/${xauthor ? xauthor + '/' : ''}paths/${xpath}`;
243
131
 
244
- // NOT FOUND EXCEPTION
245
- var fileContents;
246
132
  try {
247
- fileContents = fs.readFileSync("files/" + xauthor + "paths/" + xpath);
133
+ const fileContents = fs.readFileSync(filePath);
134
+ return pathProto.path.decode(fileContents);
248
135
  } catch (err) {
249
- if (err.code === 'ENOENT') {
250
- return "Path not found";
251
- } else {
252
- throw err;
253
- }
136
+ return err.code === 'ENOENT' ? "Path not found" : err;
254
137
  }
255
-
256
- // DECODING PATH
257
- var path_enc = fileContents
258
- var path_obj = path_pb.path.decode(path_enc)
259
-
260
- return path_obj;
261
138
  }
262
139
 
263
-
264
-
265
- /** getLabelObj
266
- * [Function that recieves a label hash and returns the label object]
267
- *
268
- * @param {string} xlabel (required)
269
- *
270
- * @return void
140
+ /**
141
+ * Retrieves and decodes a label object from a file.
142
+ * @param {string} xlabel - The label hash.
143
+ * @param {string} xauthor - The author of the label (optional).
144
+ * @returns {Object|string} The decoded label object or an error message.
271
145
  */
272
- function getLabelObj(xlabel, xauthor) {
273
-
274
- if(xauthor != ""){
275
- xauthor = xauthor + '/';
276
- }
277
-
278
- // LOADING PB
279
- var label_pb = pb(fs.readFileSync('node_modules/pathchain/proto/label.proto'))
146
+ function getLabelObj(xlabel, xauthor = '') {
147
+ const labelProto = pb(fs.readFileSync('node_modules/pathchain/proto/label.proto'));
148
+ const filePath = `files/${xauthor ? xauthor + '/' : ''}labels/${xlabel}`;
280
149
 
281
- // NOT FOUND EXCEPTION
282
- var fileContents;
283
150
  try {
284
- fileContents = fs.readFileSync("files/" + xauthor + "labels/" + xlabel);
151
+ const fileContents = fs.readFileSync(filePath);
152
+ return labelProto.label.decode(fileContents);
285
153
  } catch (err) {
286
- if (err.code === 'ENOENT') {
287
- return "Label not found";
288
- } else {
289
- throw err;
290
- }
154
+ return err.code === 'ENOENT' ? "Label not found" : err;
291
155
  }
292
-
293
- // DECODING LABEL
294
- var label_enc = fileContents
295
- var label_obj = label_pb.label.decode(label_enc)
296
-
297
- return label_obj;
298
156
  }
299
157
 
300
-
301
- module.exports = { getCurrentDate, getMomentObj, getPioneerObj, getSecretObj, getEntityObj, getNodeObj, getLinkObj, getPathObj, getLabelObj }
158
+ module.exports = {
159
+ getCurrentDate,
160
+ getMomentObj,
161
+ getPioneerObj,
162
+ getSecretObj,
163
+ getEntityObj,
164
+ getNodeObj,
165
+ getLinkObj,
166
+ getPathObj,
167
+ getLabelObj
168
+ };
package/maker.js CHANGED
@@ -143,7 +143,6 @@ function entity(xsecret, format) {
143
143
  const register = dt.format(new Date(), dt.compile(format, true));
144
144
  const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
145
145
  const ancestor_entity_hash = getter.getSecretObj(xsecret, "").author;
146
- console.log("MAKER :: ENTITY :: xsecret obj : ", getter.getSecretObj(xsecret));
147
146
 
148
147
  const entity_hash = sha256(`${register_moment_hash}_${ancestor_entity_hash}`);
149
148
 
@@ -312,11 +311,23 @@ function label(text, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [GMT]Z') {
312
311
  function link(target, prev, next, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [GMT]Z') {
313
312
  // Load the link protocol buffer definition
314
313
  const link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'));
314
+ var target_type = "";
315
315
 
316
316
  // Validate target
317
317
  if (target === "") {
318
318
  return "The link must be linking something. The target cannot be null.";
319
319
  }
320
+ else{
321
+ var splitted_target = target.split("/");
322
+ console.log("Splitted target: ", splitted_target);
323
+ target_type = splitted_target[splitted_target.length - 2];
324
+ console.log("Target type: ", target_type);
325
+ }
326
+
327
+ // Homogenize the author.
328
+ // Break down the target address
329
+ // Make a copy of it inside *author* address if it is not public or if the original author is different than *author*
330
+ // What the fuck
320
331
 
321
332
  // Handle author and author folder
322
333
  let author_folder = author;
@@ -339,21 +350,8 @@ function link(target, prev, next, author, ancestor, format = 'MM DD YYYY HH:mm:S
339
350
  if (prev === "") { prev = link_hash; }
340
351
  if (next === "") { next = link_hash; }
341
352
 
342
- // Determine the nature of the target (node, path, or label)
343
- let target_type = "";
344
- if (checker.checkFile(`files/${author_folder}nodes/${target}`)) {
345
- target_type = "nodes";
346
- } else if (checker.checkFile(`files/${author_folder}paths/${target}`)) {
347
- target_type = "paths";
348
- } else if (checker.checkFile(`files/${author_folder}labels/${target}`)) {
349
- target_type = "labels";
350
- }
351
-
352
- if (target_type) {
353
- target = `${target_type}/${target}`;
354
- console.log(`Target is a ${target_type.slice(0, -1)}`);
355
- } else {
356
- console.log("Warning: Target type not recognized");
353
+ if (!checker.checkFile(`files/${target}`)) {
354
+ return "Target was not found!";
357
355
  }
358
356
 
359
357
  // Encode the link data into a protocol buffer
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.5"
37
+ "version": "1.1.7"
38
38
  }