pathchain 1.0.59 → 1.1.0

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 (3) hide show
  1. package/maker.js +248 -301
  2. package/package.json +1 -1
  3. package/updater.js +95 -1
package/maker.js CHANGED
@@ -1,429 +1,376 @@
1
- var fs = require("fs");
2
- var pb = require('protocol-buffers');
3
- var dt = require('date-and-time');
4
- var sha256 = require("js-sha256");
1
+ // Required modules
2
+ const fs = require("fs");
3
+ const pb = require('protocol-buffers');
4
+ const dt = require('date-and-time');
5
+ const sha256 = require("js-sha256");
5
6
  const getter = require("./getter");
6
7
  const checker = require("./checker");
7
8
  const updater = require("./updater");
8
9
 
9
- /** moment
10
- * [Moment Protocol Buffer Creation]
11
- *
12
- * @param {string} datetime (optional)
13
- * @param {float} lat (optional)
14
- * @param {float} lon (optional)
15
- * @param {float} x (optional)
16
- * @param {float} y (optional)
17
- * @param {float} z (optional)
18
- * @param {string} format (optional)
19
- *
20
- * @return {string} moment_buffer
10
+ /**
11
+ * Creates a moment Protocol Buffer
12
+ * @param {string} datetime - Timestamp (optional)
13
+ * @param {number} lat - Latitude (optional)
14
+ * @param {number} lon - Longitude (optional)
15
+ * @param {number} x - X coordinate (optional)
16
+ * @param {number} y - Y coordinate (optional)
17
+ * @param {number} z - Z coordinate (optional)
18
+ * @param {string} format - Date format (optional)
19
+ * @returns {string} moment_hash - Hash of the created moment
21
20
  */
22
21
  function moment(datetime, lat, lon, x, y, z, format) {
23
- var moment_pb = pb(fs.readFileSync('node_modules/pathchain/proto/moment.proto'));
22
+ const moment_pb = pb(fs.readFileSync('node_modules/pathchain/proto/moment.proto'));
24
23
 
25
- var date_obj = dt.preparse(datetime, format);
26
- var moment_obj = {
24
+ const date_obj = dt.preparse(datetime, format);
25
+ const moment_obj = {
27
26
  coordinates: {
28
27
  lat: lat,
29
28
  lon: lon,
30
29
  xyz: { x: x, y: y, z: z }
31
30
  },
32
31
  datetime: {
33
- Y: date_obj.Y,
34
- M: date_obj.M,
35
- D: date_obj.D,
36
- H: date_obj.H,
37
- A: date_obj.A,
38
- h: date_obj.h,
39
- m: date_obj.m,
40
- s: date_obj.s,
41
- S: date_obj.S,
42
- Z: date_obj.Z,
43
- _index: date_obj._index,
44
- _length: date_obj._length,
45
- _match: date_obj._match
32
+ Y: date_obj.Y, M: date_obj.M, D: date_obj.D,
33
+ H: date_obj.H, A: date_obj.A, h: date_obj.h,
34
+ m: date_obj.m, s: date_obj.s, S: date_obj.S,
35
+ Z: date_obj.Z, _index: date_obj._index,
36
+ _length: date_obj._length, _match: date_obj._match
46
37
  }
47
- }
38
+ };
48
39
 
49
- var buffer = moment_pb.moment.encode(moment_obj);
50
-
51
- var moment_hash = sha256(JSON.stringify(moment_pb.moment.decode(buffer)));
40
+ const buffer = moment_pb.moment.encode(moment_obj);
41
+ const moment_hash = sha256(JSON.stringify(moment_pb.moment.decode(buffer)));
52
42
 
53
43
  checker.checkDir("files/moments/");
54
- fs.writeFileSync("files/moments/" + moment_hash, buffer);
44
+ fs.writeFileSync(`files/moments/${moment_hash}`, buffer);
55
45
 
56
46
  return moment_hash;
57
47
  }
58
48
 
59
- /** secret
60
- * [Pioneer Protocol Buffer Creation]
61
- *
62
- * @param {string} author (optional, default=pioneer_hash)
63
- * @param {string} format (required)
64
- *
65
- * @return {string} secret_hash
49
+ /**
50
+ * Creates a secret Protocol Buffer
51
+ * @param {string} author - Author of the secret (optional, default=pioneer_hash)
52
+ * @param {string} format - Date format (required)
53
+ * @returns {string} secret_hash - Hash of the created secret
66
54
  */
67
55
  function secret(author, format) {
68
- checker.checkDir("files/entities/")
69
-
70
- // There is no pioneer
71
- if(checker.checkEmptyDir("files/entities/")){
72
- var secret_pb = pb(fs.readFileSync('node_modules/pathchain/proto/secret.proto'))
56
+ checker.checkDir("files/entities/");
73
57
 
74
- var register = new Date()
75
- register = dt.format(register, dt.compile(format, true));
58
+ // Create secret only if there's no pioneer yet
59
+ if (checker.checkEmptyDir("files/entities/")) {
60
+ const secret_pb = pb(fs.readFileSync('node_modules/pathchain/proto/secret.proto'));
76
61
 
77
- var register_moment_hash = moment(register, 0, 0, 0, 0, 0, format)
62
+ const register = dt.format(new Date(), dt.compile(format, true));
63
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
78
64
 
79
- var secret_hash = sha256(register_moment_hash + "_" + author);
65
+ const secret_hash = sha256(`${register_moment_hash}_${author}`);
80
66
 
81
- var buffer = secret_pb.secret.encode({
82
- register: "moments/"+register_moment_hash,
83
- author: "entities/"+author,
84
- user: "entities/"+author,
67
+ const buffer = secret_pb.secret.encode({
68
+ register: `moments/${register_moment_hash}`,
69
+ author: `entities/${author}`,
70
+ user: `entities/${author}`,
85
71
  used: false,
86
- tag: "secrets/"+secret_hash,
87
- })
72
+ tag: `secrets/${secret_hash}`,
73
+ });
88
74
 
89
- checker.checkDir("files/secrets/") // checking
90
- checker.checkDir("files/"+ author +"/secrets/") // checking
75
+ checker.checkDir("files/secrets/");
76
+ checker.checkDir(`files/${author}/secrets/`);
91
77
 
92
- fs.writeFileSync("files/secrets/" + secret_hash, buffer);
93
- fs.writeFileSync("files/"+ author +"/secrets/" + secret_hash, buffer);
78
+ fs.writeFileSync(`files/secrets/${secret_hash}`, buffer);
79
+ fs.writeFileSync(`files/${author}/secrets/${secret_hash}`, buffer);
94
80
 
95
81
  return secret_hash;
96
- }
97
- // The pioneer already exists
98
- else{
82
+ } else {
99
83
  return "The pioneer has only one secret";
100
84
  }
101
85
  }
102
86
 
103
- /** pioneer
104
- * [Pioneer Protocol Buffer Creation]
105
- *
106
- * @param {string} xbigbang (required) // The creation moment is the bigbang of the system
107
- * @param {string} format (required)
108
- *
109
- * @return {string} pioneer_buffer
87
+ /**
88
+ * Creates the pioneer (first entity) Protocol Buffer
89
+ * @param {string} xbigbang - The creation moment (required)
90
+ * @param {string} format - Date format (required)
91
+ * @returns {string} pioneer_hash - Hash of the created pioneer
110
92
  */
111
93
  function pioneer(xbigbang, format) {
112
- checker.checkDir("files/entities/")
94
+ checker.checkDir("files/entities/");
113
95
 
114
- // There is no pioneer
115
- if(checker.checkEmptyDir("files/entities/")){
116
- // CREATING PIONEER
117
- var entity = pb(fs.readFileSync('node_modules/pathchain/proto/entity.proto'))
96
+ // Create pioneer only if there's no pioneer yet
97
+ if (checker.checkEmptyDir("files/entities/")) {
98
+ const entity = pb(fs.readFileSync('node_modules/pathchain/proto/entity.proto'));
118
99
 
119
- var register = new Date()
120
- register = dt.format(register, dt.compile(format, true));
121
-
122
- var register_moment_hash = moment(register, 0, 0, 0, 0, 0, format)
123
-
124
- var birthday = dt.parse(xbigbang, format, true);
125
- birthday = dt.format(birthday, format, true);
100
+ const register = dt.format(new Date(), dt.compile(format, true));
101
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
126
102
 
127
- var birthday_moment = moment(birthday, 0, 0, 0, 0, 0, format)
103
+ const birthday = dt.format(dt.parse(xbigbang, format, true), format, true);
104
+ const birthday_moment = moment(birthday, 0, 0, 0, 0, 0, format);
128
105
 
129
- // console.log("PIONEER REGISTER MOMENT: ", register_moment_hash)
130
- // console.log("PIONEER BIRTHDAY MOMENT: ", birthday_moment)
106
+ const pioneer_hash = sha256(`${register_moment_hash}_${birthday_moment}`);
131
107
 
132
- var pioneer_hash = sha256(register_moment_hash + "_" + birthday_moment);
133
-
134
- var buffer = entity.entity.encode({
135
- register: "moments/"+register_moment_hash,
136
- ancestor: "entities/"+pioneer_hash, // points to itself as ancestor entity in the chain
137
- tag: "entities/"+pioneer_hash,
138
- })
108
+ const buffer = entity.entity.encode({
109
+ register: `moments/${register_moment_hash}`,
110
+ ancestor: `entities/${pioneer_hash}`, // points to itself as ancestor
111
+ tag: `entities/${pioneer_hash}`,
112
+ });
139
113
 
140
114
  const the_secret = secret(pioneer_hash, format);
141
- console.log("The pioneer secret buffer is: ", the_secret)
115
+ console.log("The pioneer secret buffer is: ", the_secret);
142
116
 
143
- // Save as active entity
144
- checker.checkDir("files/entities/")
145
- fs.writeFileSync("files/entities/" + pioneer_hash, buffer);
117
+ checker.checkDir("files/entities/");
118
+ fs.writeFileSync(`files/entities/${pioneer_hash}`, buffer);
146
119
 
147
- // Save pioneer to look for it later
148
- checker.checkDir("files/pioneer/")
149
- fs.writeFileSync("files/pioneer/" + pioneer_hash, buffer);
120
+ checker.checkDir("files/pioneer/");
121
+ fs.writeFileSync(`files/pioneer/${pioneer_hash}`, buffer);
150
122
 
151
123
  return pioneer_hash;
152
- }
153
- // There is a pioneer
154
- else{
124
+ } else {
155
125
  return checker.checkFiles('files/pioneer/')[0];
156
126
  }
157
127
  }
158
128
 
159
- /** entity
160
- * [Entity Protocol Buffer Creation]
161
- *
162
- * @param {string} xsecret (required)
163
- * @param {string} format (optional)
164
- *
165
- * @return {string} secret_hash
129
+ /**
130
+ * Creates an entity Protocol Buffer
131
+ * @param {string} xsecret - Secret used for entity creation (required)
132
+ * @param {string} format - Date format (optional)
133
+ * @returns {string} entity_hash - Hash of the created entity
166
134
  */
167
135
  function entity(xsecret, format) {
168
-
169
136
  checker.checkDir("files/entities/");
170
- if(checker.checkEmptyDir("files/entities/")){
137
+ if (checker.checkEmptyDir("files/entities/")) {
171
138
  return "A pioneer's secret is needed to create the first entity. There was no pioneer, but here is the one at 'files/pioneer/'" + pioneer();
172
- }
173
- else{
174
- if(checker.isSecretUsed(xsecret) == false){
175
- // CREATING entity
176
- var entity = pb(fs.readFileSync('node_modules/pathchain/proto/entity.proto'))
139
+ } else {
140
+ if (checker.isSecretUsed(xsecret) == false) {
141
+ const entity = pb(fs.readFileSync('node_modules/pathchain/proto/entity.proto'));
177
142
 
178
- var register = new Date()
179
- register = dt.format(register, dt.compile(format, true));
180
-
181
- var register_moment_hash = moment(register, 0, 0, 0, 0, 0, format)
182
- var ancestor_entity_hash = getter.getSecretObj(xsecret).author;
183
- // console.log("-> ancestor ENTITY HASH: ", ancestor_entity_hash);
143
+ const register = dt.format(new Date(), dt.compile(format, true));
144
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
145
+ const ancestor_entity_hash = getter.getSecretObj(xsecret).author;
184
146
 
185
- var entity_hash = sha256(register_moment_hash + "_" + ancestor_entity_hash);
147
+ const entity_hash = sha256(`${register_moment_hash}_${ancestor_entity_hash}`);
186
148
 
149
+ const buffer = entity.entity.encode({
150
+ register: `moments/${register_moment_hash}`,
151
+ ancestor: `entities/${ancestor_entity_hash}`,
152
+ tag: `entities/${entity_hash}`,
153
+ });
187
154
 
188
- var buffer = entity.entity.encode({
189
- register: "moments/"+register_moment_hash,
190
- ancestor: "entities/"+ancestor_entity_hash,
191
- tag: "entities/"+entity_hash,
192
- })
193
-
194
- var updated_secret = updater.useSecret(xsecret, entity_hash);
155
+ const updated_secret = updater.useSecret(xsecret, entity_hash);
195
156
  console.log("Updated secret: ", updated_secret);
196
157
 
197
- checker.checkDir("files/entities/") // checking
198
- fs.writeFileSync("files/entities/" + entity_hash, buffer);
158
+ checker.checkDir("files/entities/");
159
+ fs.writeFileSync(`files/entities/${entity_hash}`, buffer);
199
160
 
200
161
  return entity_hash;
201
- }
202
- else{
203
- return "Secret has been already used or it was not found"
162
+ } else {
163
+ return "Secret has been already used or it was not found";
204
164
  }
205
165
  }
206
166
  }
207
167
 
208
-
209
- /** node
210
- * [Node Protocol Buffer Creation]
211
- *
212
- * @param {string} author (optional, default=pioneer_hash)
213
- * @param {string} file (optional)
214
- * @param {string} str (optional)
215
- * @param {string} format (required)
216
- *
217
- * @return {string} node_hash
168
+ /**
169
+ * Creates a node Protocol Buffer
170
+ * @param {string} text - Node content (required)
171
+ * @param {string} author - Author of the node (optional, default=pioneer_hash)
172
+ * @param {string} format - Date format (optional)
173
+ * @returns {string} node_hash - Hash of the created node
218
174
  */
219
175
  function node(text, author, format = 'MM DD YYYY HH:mm:SSS [GMT]Z') {
220
- var node_pb = pb(fs.readFileSync('node_modules/pathchain/proto/node.proto'))
176
+ const node_pb = pb(fs.readFileSync('node_modules/pathchain/proto/node.proto'));
221
177
 
222
- var author_folder = author;
223
- if(author == ""){
224
- author = author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
178
+ let author_folder = author;
179
+ if (author == "") {
180
+ author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
225
181
  author_folder = "";
226
- }else{
227
- author_folder = author_folder + '/';
182
+ } else {
183
+ author_folder = `${author_folder}/`;
228
184
  }
229
185
 
230
- var register = new Date()
231
- register = dt.format(register, dt.compile(format, true));
232
-
233
- var register_moment_hash = moment(register, 0, 0, 0, 0, 0, format)
186
+ const register = dt.format(new Date(), dt.compile(format, true));
187
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
234
188
 
235
- var node_hash = sha256(register_moment_hash + "_" + author);
189
+ const node_hash = sha256(`${register_moment_hash}_${author}`);
236
190
 
237
- var buffer = node_pb.node.encode({
238
- register: "moments/" + register_moment_hash,
239
- author: "etities/" + author,
191
+ const buffer = node_pb.node.encode({
192
+ register: `moments/${register_moment_hash}`,
193
+ author: `entities/${author}`,
240
194
  text: text,
241
- tag: "nodes/" + node_hash
242
- })
195
+ tag: `nodes/${node_hash}`
196
+ });
243
197
 
244
- checker.checkDir("files/" + author_folder + "nodes/") // checking
245
- fs.writeFileSync("files/" + author_folder + "nodes/" + node_hash, buffer);
198
+ checker.checkDir(`files/${author_folder}nodes/`);
199
+ fs.writeFileSync(`files/${author_folder}nodes/${node_hash}`, buffer);
246
200
 
247
201
  return node_hash;
248
202
  }
249
203
 
204
+ /**
205
+ * Creates a path Protocol Buffer
206
+ * @param {string} text - Path description (optional, default=path_hash)
207
+ * @param {Array} elements - Array of elements in the path (required)
208
+ * @param {string} author - Author of the path (optional, default=pioneer_hash)
209
+ * @param {string} ancestor - Ancestor path (optional, default=path_hash)
210
+ * @param {string} format - Date format (optional)
211
+ * @returns {string} path_hash - Hash of the created path
212
+ */
213
+ function path(text, elements, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [GMT]Z') {
214
+ const path_pb = pb(fs.readFileSync('node_modules/pathchain/proto/path.proto'));
250
215
 
251
- /** path
252
- * [Pioneer Protocol Buffer Creation]
253
- *
254
- * @param {string} author (optional, default=pioneer_hash)
255
- * @param {string} text (optional, default=path_hash)
256
- * @param {string} head (required)
257
- * @param {string} ancestor (optional, default=path_hash)
258
- * @param {string} format (required)
259
- *
260
- * @return {string} path_hash
261
- */
262
- function path(text, head, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [GMT]Z') {
263
- var path_pb = pb(fs.readFileSync('node_modules/pathchain/proto/path.proto'))
264
-
265
- var author_folder = author;
266
- if(author == ""){
267
- author = author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
216
+ let author_folder = author;
217
+ if (author == "") {
218
+ author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
268
219
  author_folder = "";
269
- }else{
270
- author_folder = author_folder + '/';
271
- }
272
-
273
- var register = new Date()
274
- register = dt.format(register, dt.compile(format, true));
220
+ } else {
221
+ author_folder = `${author_folder}/`;
222
+ }
275
223
 
276
- var register_moment_hash = moment(register, 0, 0, 0, 0, 0, format)
224
+ const register = dt.format(new Date(), dt.compile(format, true));
225
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
277
226
 
278
- var path_hash = sha256(register_moment_hash + "_" + author);
227
+ const path_hash = sha256(`${register_moment_hash}_${author}`);
279
228
 
280
- if(text == ""){ text = path_hash; }
281
- if(head == ""){ return "Head node_link is required to create a path!" }
282
- if(ancestor == ""){ ancestor = path_hash; }
283
-
284
- var buffer = path_pb.path.encode({
285
- register: "moments/"+register_moment_hash,
286
- author: author,
287
- text: text,
288
- head: "links/" + head,
289
- ancestor: author_folder + "paths/" + ancestor,
290
- tag: author_folder + "paths/" + path_hash,
291
- })
292
-
293
- checker.checkDir("files/" + author_folder + "paths/") // checking
294
- fs.writeFileSync("files/" + author_folder + "paths/" + path_hash, buffer);
229
+ if (text == "") { text = path_hash; }
230
+
231
+ let prev_link = "";
232
+ // Building target chain
233
+ for (let i = 0; i < elements.length; i++) {
234
+ const current_link = link(elements[i], "", "", author, "");
235
+ if (i > 0) {
236
+ updater.setLinkNext(prev_link, current_link);
237
+ updater.setPrevLink(current_link, prev_link);
238
+ }
239
+ prev_link = current_link;
240
+ }
241
+
242
+ if (ancestor == "") { ancestor = path_hash; }
243
+
244
+ const buffer = path_pb.path.encode({
245
+ register: `moments/${register_moment_hash}`,
246
+ author: author,
247
+ text: text,
248
+ head: `links/${prev_link}`,
249
+ ancestor: `${author_folder}paths/${ancestor}`,
250
+ tag: `${author_folder}paths/${path_hash}`,
251
+ });
252
+
253
+ checker.checkDir(`files/${author_folder}paths/`);
254
+ fs.writeFileSync(`files/${author_folder}paths/${path_hash}`, buffer);
295
255
 
296
- return path_hash;
256
+ return path_hash;
297
257
  }
298
258
 
299
-
300
- /** label
301
- * [Label Protocol Buffer Creation]
302
- *
303
- * @param {string} author (optional, default=pioneer_hash)
304
- * @param {string} file (optional)
305
- * @param {string} str (optional)
306
- * @param {string} format (required)
307
- *
308
- * @return {string} label_hash
259
+ /**
260
+ * Creates a label Protocol Buffer
261
+ * @param {string} text - Label text (required)
262
+ * @param {string} author - Author of the label (optional, default=pioneer_hash)
263
+ * @param {string} ancestor - Ancestor label (optional, default=label_hash)
264
+ * @param {string} format - Date format (optional)
265
+ * @returns {string} label_hash - Hash of the created label
309
266
  */
310
267
  function label(text, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [GMT]Z') {
311
- var label_pb = pb(fs.readFileSync('node_modules/pathchain/proto/label.proto'))
268
+ const label_pb = pb(fs.readFileSync('node_modules/pathchain/proto/label.proto'));
312
269
 
313
- var author_folder = author;
314
- if(author == ""){
315
- author = author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
270
+ let author_folder = author;
271
+ if (author == "") {
272
+ author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
316
273
  author_folder = "";
317
- }else{
318
- author_folder = author_folder + '/';
274
+ } else {
275
+ author_folder = `${author_folder}/`;
319
276
  }
320
277
 
321
- var register = new Date()
322
- register = dt.format(register, dt.compile(format, true));
323
-
324
- var register_moment_hash = moment(register, 0, 0, 0, 0, 0, format)
278
+ const register = dt.format(new Date(), dt.compile(format, true));
279
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
325
280
 
326
- var label_hash = sha256(register_moment_hash + "_" + author);
281
+ const label_hash = sha256(`${register_moment_hash}_${author}`);
327
282
 
328
- if(ancestor == ""){
283
+ if (ancestor == "") {
329
284
  ancestor = label_hash;
330
285
  }
331
286
 
332
- var buffer = label_pb.label.encode({
333
- register: "moments/" + register_moment_hash,
334
- author: "etities/" + author,
287
+ const buffer = label_pb.label.encode({
288
+ register: `moments/${register_moment_hash}`,
289
+ author: `entities/${author}`,
335
290
  text: text,
336
- ancestor: author_folder + "labels/" + ancestor,
337
- tag: author_folder + "labels/" + label_hash
338
- })
291
+ ancestor: `${author_folder}labels/${ancestor}`,
292
+ tag: `${author_folder}labels/${label_hash}`
293
+ });
339
294
 
340
- checker.checkDir("files/" + author_folder + "labels/") // checking
341
- fs.writeFileSync("files/" + author_folder + "labels/" + label_hash, buffer);
295
+ checker.checkDir(`files/${author_folder}labels/`);
296
+ fs.writeFileSync(`files/${author_folder}labels/${label_hash}`, buffer);
342
297
 
343
298
  return label_hash;
344
299
  }
345
300
 
346
-
347
- /** link
348
- * [Link Protocol Buffer Creation]
349
- *
350
- * @param {string} target (optional)
351
- * @param {string} prev (optional)
352
- * @param {string} next (optional)
353
- * @param {string} author (optional, default=pioneer_hash)
354
- * @param {string} ancestor (optional)
355
- * @param {string} format (required)
356
- *
357
- * @return {string} link_hash
301
+ /**
302
+ * Creates a link Protocol Buffer
303
+ * @param {string} target - Target of the link (required)
304
+ * @param {string} prev - Previous link in chain (optional, default=link_hash)
305
+ * @param {string} next - Next link in chain (optional, default=link_hash)
306
+ * @param {string} author - Author of the link (optional, default=pioneer_hash)
307
+ * @param {string} ancestor - Ancestor link (optional, default=link_hash)
308
+ * @param {string} format - Date format (optional)
309
+ * @returns {string} link_hash - Hash of the created link
358
310
  */
359
311
  function link(target, prev, next, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [GMT]Z') {
360
- var link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'))
361
-
362
- if(target == "") return "The link must link something. Target can't be null.";
312
+ // Load the link protocol buffer definition
313
+ const link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'));
363
314
 
364
- if(prev == ""){
365
- if(next == "") return "The link should be connected to something. Prev and Next cannot be both null at the same time.";
366
- }
367
- if(next == ""){
368
- if(prev == "") return "The link should be connected to something. Prev and Next cannot be both null at the same time.";
315
+ // Validate target
316
+ if (target === "") {
317
+ return "The link must link something. Target can't be null.";
369
318
  }
370
319
 
371
- var author_folder = author;
372
- if(author == ""){
373
- author = author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
320
+ // Handle author and author folder
321
+ let author_folder = author;
322
+ if (author === "") {
323
+ author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
374
324
  author_folder = "";
375
- }else{
376
- author_folder = author_folder + '/';
325
+ } else {
326
+ author_folder = `${author_folder}/`;
377
327
  }
378
328
 
379
- var register = new Date()
380
- register = dt.format(register, dt.compile(format, true));
381
-
382
- var register_moment_hash = moment(register, 0, 0, 0, 0, 0, format)
329
+ // Create a moment for this link
330
+ const register = dt.format(new Date(), dt.compile(format, true));
331
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
383
332
 
384
- var link_hash = sha256(register_moment_hash + "_" + author + "_" + prev + "_" + next);
333
+ // Generate a unique hash for this link
334
+ const link_hash = sha256(`${register_moment_hash}_${author}_${prev}_${next}`);
385
335
 
386
- if(ancestor == ""){
387
- ancestor = link_hash;
388
- }
389
- if(prev == ""){
390
- prev = link_hash;
391
- }
392
- if(next == ""){
393
- next = link_hash;
394
- }
336
+ // Set default values if not provided
337
+ if (ancestor === "") { ancestor = link_hash; }
338
+ if (prev === "") { prev = link_hash; }
339
+ if (next === "") { next = link_hash; }
395
340
 
396
- // Find the target to figure out its nature (node, path or label)
397
- if(checker.checkFile("files/" + author_folder + "nodes/" + target) == true){
398
- target = 'nodes/' + target;
399
- console.log("Target is a node")
341
+ // Determine the nature of the target (node, path, or label)
342
+ let target_type = "";
343
+ if (checker.checkFile(`files/${author_folder}nodes/${target}`)) {
344
+ target_type = "nodes";
345
+ } else if (checker.checkFile(`files/${author_folder}paths/${target}`)) {
346
+ target_type = "paths";
347
+ } else if (checker.checkFile(`files/${author_folder}labels/${target}`)) {
348
+ target_type = "labels";
400
349
  }
401
- if(checker.checkFile("files/" + author_folder + "paths/" + target) == true){
402
- target = 'paths/' + target;
403
- console.log("Target is a path")
404
- }
405
- if(checker.checkFile("files/" + author_folder + "labels/" + target) == true){
406
- target = 'labels/' + target;
407
- console.log("Target is a label")
408
- }
409
-
410
350
 
351
+ if (target_type) {
352
+ target = `${target_type}/${target}`;
353
+ console.log(`Target is a ${target_type.slice(0, -1)}`);
354
+ } else {
355
+ console.log("Warning: Target type not recognized");
356
+ }
411
357
 
412
- var buffer = link_pb.link.encode({
413
- register: "moments/" + register_moment_hash,
414
- author: "etities/" + author,
415
- prev: "links/" + prev,
416
- next: "links/" + next,
358
+ // Encode the link data into a protocol buffer
359
+ const buffer = link_pb.link.encode({
360
+ register: `moments/${register_moment_hash}`,
361
+ author: `entities/${author}`,
362
+ prev: `links/${prev}`,
363
+ next: `links/${next}`,
417
364
  target: target,
418
- ancestor: author_folder + "links/" + ancestor,
419
- tag: author_folder + "links/" + link_hash
420
- })
365
+ ancestor: `${author_folder}links/${ancestor}`,
366
+ tag: `${author_folder}links/${link_hash}`
367
+ });
421
368
 
422
- checker.checkDir("files/" + author_folder + "links/") // checking
423
- fs.writeFileSync("files/" + author_folder + "links/" + link_hash, buffer);
369
+ // Ensure the directory exists and write the link buffer to a file
370
+ checker.checkDir(`files/${author_folder}links/`);
371
+ fs.writeFileSync(`files/${author_folder}links/${link_hash}`, buffer);
424
372
 
425
373
  return link_hash;
426
374
  }
427
375
 
428
-
429
- module.exports = { moment, pioneer, secret, entity, node, path, label, link }
376
+ module.exports = { moment, pioneer, secret, entity, node, path, label, link };
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.0.59"
37
+ "version": "1.1.0"
38
38
  }
package/updater.js CHANGED
@@ -43,4 +43,98 @@ function useSecret(xsecret, xauthor = "") {
43
43
  return secret_obj;
44
44
  }
45
45
 
46
- module.exports = { useSecret }
46
+
47
+ /** setLinkNext
48
+ * [Function that updates the 'next' pointer of a link]
49
+ *
50
+ * @param {string} xlink
51
+ * @param {string} xnextlink
52
+ *
53
+ * @return {string} xlink
54
+ */
55
+ function setLinkNext(xlink = "", xnextlink = "") {
56
+
57
+ // LOADING PB
58
+ var link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'))
59
+
60
+ // EXCEPTIONS FOR NOT FOUND LINKS
61
+ var fileContents;
62
+
63
+ // NOT FOUND EXCEPTION FOR 'xlink'
64
+ try {
65
+ fileContents = fs.readFileSync("files/links/" + xlink);
66
+ } catch (err) {
67
+ if (err.code === 'ENOENT') {
68
+ return "Link '" + xlink + "' not found";
69
+ } else {
70
+ throw err;
71
+ }
72
+ }
73
+
74
+ // NOT FOUND EXCEPTION FOR 'xnextlink'
75
+ try {
76
+ fileContents = fs.readFileSync("files/links/" + xnextlink);
77
+ } catch (err) {
78
+ if (err.code === 'ENOENT') {
79
+ return "Link '" + xnextlink + "' not found";
80
+ } else {
81
+ throw err;
82
+ }
83
+ }
84
+
85
+ // DECODING LINK
86
+ var link_enc = fileContents
87
+ var link_obj = link_pb.link.decode(link_enc)
88
+
89
+ link_obj.next = xnextlink;
90
+ return link_obj;
91
+ }
92
+
93
+
94
+ /** setLinkPrev
95
+ * [Function that updates the 'previous' pointer of a link]
96
+ *
97
+ * @param {string} xlink
98
+ * @param {string} xprevlink
99
+ *
100
+ * @return {string} xlink
101
+ */
102
+ function setLinkPrev(xlink = "", xprevlink = "") {
103
+
104
+ // LOADING PB
105
+ var link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'))
106
+
107
+ // EXCEPTIONS FOR NOT FOUND LINKS
108
+ var fileContents;
109
+
110
+ // NOT FOUND EXCEPTION FOR 'xlink'
111
+ try {
112
+ fileContents = fs.readFileSync("files/links/" + xlink);
113
+ } catch (err) {
114
+ if (err.code === 'ENOENT') {
115
+ return "Link '" + xlink + "' not found";
116
+ } else {
117
+ throw err;
118
+ }
119
+ }
120
+
121
+ // NOT FOUND EXCEPTION FOR 'xprevlink'
122
+ try {
123
+ fileContents = fs.readFileSync("files/links/" + xprevlink);
124
+ } catch (err) {
125
+ if (err.code === 'ENOENT') {
126
+ return "Link '" + xprevlink + "' not found";
127
+ } else {
128
+ throw err;
129
+ }
130
+ }
131
+
132
+ // DECODING LINK
133
+ var link_enc = fileContents
134
+ var link_obj = link_pb.link.decode(link_enc)
135
+
136
+ link_obj.prev = xprevlink;
137
+ return link_obj;
138
+ }
139
+
140
+ module.exports = { useSecret, setLinkNext, setLinkPrev }