pathchain 1.0.60 → 1.1.1

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 +251 -296
  2. package/package.json +1 -1
  3. package/updater.js +95 -1
package/maker.js CHANGED
@@ -1,422 +1,377 @@
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/")){
171
- 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'))
137
+ if (checker.checkEmptyDir("files/entities/")) {
138
+ return "A pioneer's secret is needed to create the first entity. The pioneer can be found at 'files/pioneer/'" + pioneer();
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;
146
+ console.log("MAKER :: ENTITY :: ancestor_entity_hash : ", ancestor_entity_hash);
184
147
 
185
- var entity_hash = sha256(register_moment_hash + "_" + ancestor_entity_hash);
148
+ const entity_hash = sha256(`${register_moment_hash}_${ancestor_entity_hash}`);
186
149
 
150
+ const buffer = entity.entity.encode({
151
+ register: `moments/${register_moment_hash}`,
152
+ ancestor: `entities/${ancestor_entity_hash}`,
153
+ tag: `entities/${entity_hash}`,
154
+ });
187
155
 
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);
156
+ const updated_secret = updater.useSecret(xsecret, entity_hash);
195
157
  console.log("Updated secret: ", updated_secret);
196
158
 
197
- checker.checkDir("files/entities/") // checking
198
- fs.writeFileSync("files/entities/" + entity_hash, buffer);
159
+ checker.checkDir("files/entities/");
160
+ fs.writeFileSync(`files/entities/${entity_hash}`, buffer);
199
161
 
200
162
  return entity_hash;
201
- }
202
- else{
203
- return "Secret has been already used or it was not found"
163
+ } else {
164
+ return "Secret has been already used or it was not found";
204
165
  }
205
166
  }
206
167
  }
207
168
 
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
169
+ /**
170
+ * Creates a node Protocol Buffer
171
+ * @param {string} text - Node content (required)
172
+ * @param {string} author - Author of the node (optional, default=pioneer_hash)
173
+ * @param {string} format - Date format (optional)
174
+ * @returns {string} node_hash - Hash of the created node
218
175
  */
219
176
  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'))
177
+ const node_pb = pb(fs.readFileSync('node_modules/pathchain/proto/node.proto'));
221
178
 
222
- var author_folder = author;
223
- if(author == ""){
224
- author = author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
179
+ let author_folder = author;
180
+ if (author == "") {
181
+ author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
225
182
  author_folder = "";
226
- }else{
227
- author_folder = author_folder + '/';
183
+ } else {
184
+ author_folder = `${author_folder}/`;
228
185
  }
229
186
 
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)
187
+ const register = dt.format(new Date(), dt.compile(format, true));
188
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
234
189
 
235
- var node_hash = sha256(register_moment_hash + "_" + author);
190
+ const node_hash = sha256(`${register_moment_hash}_${author}`);
236
191
 
237
- var buffer = node_pb.node.encode({
238
- register: "moments/" + register_moment_hash,
239
- author: "etities/" + author,
192
+ const buffer = node_pb.node.encode({
193
+ register: `moments/${register_moment_hash}`,
194
+ author: `entities/${author}`,
240
195
  text: text,
241
- tag: "nodes/" + node_hash
242
- })
196
+ tag: `nodes/${node_hash}`
197
+ });
243
198
 
244
- checker.checkDir("files/" + author_folder + "nodes/") // checking
245
- fs.writeFileSync("files/" + author_folder + "nodes/" + node_hash, buffer);
199
+ checker.checkDir(`files/${author_folder}nodes/`);
200
+ fs.writeFileSync(`files/${author_folder}nodes/${node_hash}`, buffer);
246
201
 
247
202
  return node_hash;
248
203
  }
249
204
 
205
+ /**
206
+ * Creates a path Protocol Buffer
207
+ * @param {string} text - Path description (optional, default=path_hash)
208
+ * @param {Array} elements - Array of elements in the path (required)
209
+ * @param {string} author - Author of the path (optional, default=pioneer_hash)
210
+ * @param {string} ancestor - Ancestor path (optional, default=path_hash)
211
+ * @param {string} format - Date format (optional)
212
+ * @returns {string} path_hash - Hash of the created path
213
+ */
214
+ function path(text, elements, author, ancestor, format = 'MM DD YYYY HH:mm:SSS [GMT]Z') {
215
+ const path_pb = pb(fs.readFileSync('node_modules/pathchain/proto/path.proto'));
250
216
 
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');
217
+ let author_folder = author;
218
+ if (author == "") {
219
+ author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
268
220
  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));
221
+ } else {
222
+ author_folder = `${author_folder}/`;
223
+ }
275
224
 
276
- var register_moment_hash = moment(register, 0, 0, 0, 0, 0, format)
225
+ const register = dt.format(new Date(), dt.compile(format, true));
226
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
277
227
 
278
- var path_hash = sha256(register_moment_hash + "_" + author);
228
+ const path_hash = sha256(`${register_moment_hash}_${author}`);
279
229
 
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);
230
+ if (text == "") { text = path_hash; }
231
+
232
+ let prev_link = "";
233
+ // Building target chain
234
+ for (let i = 0; i < elements.length; i++) {
235
+ const current_link = link(elements[i], "", "", author, "");
236
+ if (i > 0) {
237
+ updater.setLinkNext(prev_link, current_link);
238
+ updater.setPrevLink(current_link, prev_link);
239
+ }
240
+ prev_link = current_link;
241
+ }
242
+
243
+ if (ancestor == "") { ancestor = path_hash; }
244
+
245
+ const buffer = path_pb.path.encode({
246
+ register: `moments/${register_moment_hash}`,
247
+ author: author,
248
+ text: text,
249
+ head: `links/${prev_link}`,
250
+ ancestor: `${author_folder}paths/${ancestor}`,
251
+ tag: `${author_folder}paths/${path_hash}`,
252
+ });
253
+
254
+ checker.checkDir(`files/${author_folder}paths/`);
255
+ fs.writeFileSync(`files/${author_folder}paths/${path_hash}`, buffer);
295
256
 
296
- return path_hash;
257
+ return path_hash;
297
258
  }
298
259
 
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
260
+ /**
261
+ * Creates a label Protocol Buffer
262
+ * @param {string} text - Label text (required)
263
+ * @param {string} author - Author of the label (optional, default=pioneer_hash)
264
+ * @param {string} ancestor - Ancestor label (optional, default=label_hash)
265
+ * @param {string} format - Date format (optional)
266
+ * @returns {string} label_hash - Hash of the created label
309
267
  */
310
268
  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'))
269
+ const label_pb = pb(fs.readFileSync('node_modules/pathchain/proto/label.proto'));
312
270
 
313
- var author_folder = author;
314
- if(author == ""){
315
- author = author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
271
+ let author_folder = author;
272
+ if (author == "") {
273
+ author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
316
274
  author_folder = "";
317
- }else{
318
- author_folder = author_folder + '/';
275
+ } else {
276
+ author_folder = `${author_folder}/`;
319
277
  }
320
278
 
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)
279
+ const register = dt.format(new Date(), dt.compile(format, true));
280
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
325
281
 
326
- var label_hash = sha256(register_moment_hash + "_" + author);
282
+ const label_hash = sha256(`${register_moment_hash}_${author}`);
327
283
 
328
- if(ancestor == ""){
284
+ if (ancestor == "") {
329
285
  ancestor = label_hash;
330
286
  }
331
287
 
332
- var buffer = label_pb.label.encode({
333
- register: "moments/" + register_moment_hash,
334
- author: "etities/" + author,
288
+ const buffer = label_pb.label.encode({
289
+ register: `moments/${register_moment_hash}`,
290
+ author: `entities/${author}`,
335
291
  text: text,
336
- ancestor: author_folder + "labels/" + ancestor,
337
- tag: author_folder + "labels/" + label_hash
338
- })
292
+ ancestor: `${author_folder}labels/${ancestor}`,
293
+ tag: `${author_folder}labels/${label_hash}`
294
+ });
339
295
 
340
- checker.checkDir("files/" + author_folder + "labels/") // checking
341
- fs.writeFileSync("files/" + author_folder + "labels/" + label_hash, buffer);
296
+ checker.checkDir(`files/${author_folder}labels/`);
297
+ fs.writeFileSync(`files/${author_folder}labels/${label_hash}`, buffer);
342
298
 
343
299
  return label_hash;
344
300
  }
345
301
 
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
302
+ /**
303
+ * Creates a link Protocol Buffer
304
+ * @param {string} target - Target of the link (required)
305
+ * @param {string} prev - Previous link in chain (optional, default=link_hash)
306
+ * @param {string} next - Next link in chain (optional, default=link_hash)
307
+ * @param {string} author - Author of the link (optional, default=pioneer_hash)
308
+ * @param {string} ancestor - Ancestor link (optional, default=link_hash)
309
+ * @param {string} format - Date format (optional)
310
+ * @returns {string} link_hash - Hash of the created link
358
311
  */
359
312
  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'))
313
+ // Load the link protocol buffer definition
314
+ const link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'));
361
315
 
362
- if(target == "") return "The link must link something. Target can't be null.";
316
+ // Validate target
317
+ if (target === "") {
318
+ return "The link must be linking something. The target cannot be null.";
319
+ }
363
320
 
364
- var author_folder = author;
365
- if(author == ""){
366
- author = author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
321
+ // Handle author and author folder
322
+ let author_folder = author;
323
+ if (author === "") {
324
+ author = pioneer(getter.getCurrentDate(), 'MM DD YYYY HH:mm:SSS [GMT]Z');
367
325
  author_folder = "";
368
- }else{
369
- author_folder = author_folder + '/';
326
+ } else {
327
+ author_folder = `${author_folder}/`;
370
328
  }
371
329
 
372
- var register = new Date()
373
- register = dt.format(register, dt.compile(format, true));
374
-
375
- var register_moment_hash = moment(register, 0, 0, 0, 0, 0, format)
330
+ // Create a moment for this link
331
+ const register = dt.format(new Date(), dt.compile(format, true));
332
+ const register_moment_hash = moment(register, 0, 0, 0, 0, 0, format);
376
333
 
377
- var link_hash = sha256(register_moment_hash + "_" + author + "_" + prev + "_" + next);
334
+ // Generate a unique hash for this link
335
+ const link_hash = sha256(`${register_moment_hash}_${author}_${prev}_${next}`);
378
336
 
379
- if(ancestor == ""){
380
- ancestor = link_hash;
381
- }
382
- if(prev == ""){
383
- prev = link_hash;
384
- }
385
- if(next == ""){
386
- next = link_hash;
387
- }
337
+ // Set default values if not provided
338
+ if (ancestor === "") { ancestor = link_hash; }
339
+ if (prev === "") { prev = link_hash; }
340
+ if (next === "") { next = link_hash; }
388
341
 
389
- // Find the target to figure out its nature (node, path or label)
390
- if(checker.checkFile("files/" + author_folder + "nodes/" + target) == true){
391
- target = 'nodes/' + target;
392
- console.log("Target is a node")
393
- }
394
- if(checker.checkFile("files/" + author_folder + "paths/" + target) == true){
395
- target = 'paths/' + target;
396
- console.log("Target is a path")
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";
397
350
  }
398
- if(checker.checkFile("files/" + author_folder + "labels/" + target) == true){
399
- target = 'labels/' + target;
400
- console.log("Target is a label")
401
- }
402
-
403
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");
357
+ }
404
358
 
405
- var buffer = link_pb.link.encode({
406
- register: "moments/" + register_moment_hash,
407
- author: "etities/" + author,
408
- prev: "links/" + prev,
409
- next: "links/" + next,
359
+ // Encode the link data into a protocol buffer
360
+ const buffer = link_pb.link.encode({
361
+ register: `moments/${register_moment_hash}`,
362
+ author: `entities/${author}`,
363
+ prev: `links/${prev}`,
364
+ next: `links/${next}`,
410
365
  target: target,
411
- ancestor: author_folder + "links/" + ancestor,
412
- tag: author_folder + "links/" + link_hash
413
- })
366
+ ancestor: `${author_folder}links/${ancestor}`,
367
+ tag: `${author_folder}links/${link_hash}`
368
+ });
414
369
 
415
- checker.checkDir("files/" + author_folder + "links/") // checking
416
- fs.writeFileSync("files/" + author_folder + "links/" + link_hash, buffer);
370
+ // Ensure the directory exists and write the link buffer to a file
371
+ checker.checkDir(`files/${author_folder}links/`);
372
+ fs.writeFileSync(`files/${author_folder}links/${link_hash}`, buffer);
417
373
 
418
374
  return link_hash;
419
375
  }
420
376
 
421
-
422
- module.exports = { moment, pioneer, secret, entity, node, path, label, link }
377
+ 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.60"
37
+ "version": "1.1.1"
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 }