pathchain 1.0.60 → 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.
- package/maker.js +249 -295
- package/package.json +1 -1
- package/updater.js +95 -1
package/maker.js
CHANGED
|
@@ -1,422 +1,376 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @param {
|
|
13
|
-
* @param {
|
|
14
|
-
* @param {
|
|
15
|
-
* @param {
|
|
16
|
-
* @param {
|
|
17
|
-
* @param {
|
|
18
|
-
* @
|
|
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
|
-
|
|
22
|
+
const moment_pb = pb(fs.readFileSync('node_modules/pathchain/proto/moment.proto'));
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
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(
|
|
44
|
+
fs.writeFileSync(`files/moments/${moment_hash}`, buffer);
|
|
55
45
|
|
|
56
46
|
return moment_hash;
|
|
57
47
|
}
|
|
58
48
|
|
|
59
|
-
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @param {string}
|
|
63
|
-
* @
|
|
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
|
-
|
|
75
|
-
|
|
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
|
-
|
|
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
|
-
|
|
65
|
+
const secret_hash = sha256(`${register_moment_hash}_${author}`);
|
|
80
66
|
|
|
81
|
-
|
|
82
|
-
register:
|
|
83
|
-
author:
|
|
84
|
-
user:
|
|
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:
|
|
87
|
-
})
|
|
72
|
+
tag: `secrets/${secret_hash}`,
|
|
73
|
+
});
|
|
88
74
|
|
|
89
|
-
checker.checkDir("files/secrets/")
|
|
90
|
-
checker.checkDir(
|
|
75
|
+
checker.checkDir("files/secrets/");
|
|
76
|
+
checker.checkDir(`files/${author}/secrets/`);
|
|
91
77
|
|
|
92
|
-
fs.writeFileSync(
|
|
93
|
-
fs.writeFileSync(
|
|
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
|
-
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* @param {string}
|
|
107
|
-
* @
|
|
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
|
-
//
|
|
115
|
-
if(checker.checkEmptyDir("files/entities/")){
|
|
116
|
-
|
|
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
|
-
|
|
120
|
-
|
|
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
|
-
|
|
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
|
-
|
|
130
|
-
// console.log("PIONEER BIRTHDAY MOMENT: ", birthday_moment)
|
|
106
|
+
const pioneer_hash = sha256(`${register_moment_hash}_${birthday_moment}`);
|
|
131
107
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
144
|
-
|
|
145
|
-
fs.writeFileSync("files/entities/" + pioneer_hash, buffer);
|
|
117
|
+
checker.checkDir("files/entities/");
|
|
118
|
+
fs.writeFileSync(`files/entities/${pioneer_hash}`, buffer);
|
|
146
119
|
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* @param {string}
|
|
163
|
-
* @
|
|
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
|
-
|
|
174
|
-
|
|
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
|
-
|
|
179
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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/")
|
|
198
|
-
fs.writeFileSync(
|
|
158
|
+
checker.checkDir("files/entities/");
|
|
159
|
+
fs.writeFileSync(`files/entities/${entity_hash}`, buffer);
|
|
199
160
|
|
|
200
161
|
return entity_hash;
|
|
201
|
-
}
|
|
202
|
-
|
|
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
|
-
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
* @param {string}
|
|
213
|
-
* @
|
|
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
|
-
|
|
176
|
+
const node_pb = pb(fs.readFileSync('node_modules/pathchain/proto/node.proto'));
|
|
221
177
|
|
|
222
|
-
|
|
223
|
-
if(author == ""){
|
|
224
|
-
author =
|
|
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
|
-
|
|
231
|
-
|
|
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
|
-
|
|
189
|
+
const node_hash = sha256(`${register_moment_hash}_${author}`);
|
|
236
190
|
|
|
237
|
-
|
|
238
|
-
register:
|
|
239
|
-
author:
|
|
191
|
+
const buffer = node_pb.node.encode({
|
|
192
|
+
register: `moments/${register_moment_hash}`,
|
|
193
|
+
author: `entities/${author}`,
|
|
240
194
|
text: text,
|
|
241
|
-
tag:
|
|
242
|
-
})
|
|
195
|
+
tag: `nodes/${node_hash}`
|
|
196
|
+
});
|
|
243
197
|
|
|
244
|
-
checker.checkDir(
|
|
245
|
-
fs.writeFileSync(
|
|
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
|
-
|
|
252
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
227
|
+
const path_hash = sha256(`${register_moment_hash}_${author}`);
|
|
279
228
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
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
|
-
|
|
256
|
+
return path_hash;
|
|
297
257
|
}
|
|
298
258
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
* @param {string}
|
|
304
|
-
* @param {string}
|
|
305
|
-
* @
|
|
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
|
-
|
|
268
|
+
const label_pb = pb(fs.readFileSync('node_modules/pathchain/proto/label.proto'));
|
|
312
269
|
|
|
313
|
-
|
|
314
|
-
if(author == ""){
|
|
315
|
-
author =
|
|
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
|
-
|
|
322
|
-
|
|
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
|
-
|
|
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
|
-
|
|
333
|
-
register:
|
|
334
|
-
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
|
|
337
|
-
tag: author_folder
|
|
338
|
-
})
|
|
291
|
+
ancestor: `${author_folder}labels/${ancestor}`,
|
|
292
|
+
tag: `${author_folder}labels/${label_hash}`
|
|
293
|
+
});
|
|
339
294
|
|
|
340
|
-
checker.checkDir(
|
|
341
|
-
fs.writeFileSync(
|
|
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
|
-
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
* @param {string}
|
|
351
|
-
* @param {string}
|
|
352
|
-
* @param {string}
|
|
353
|
-
* @param {string}
|
|
354
|
-
* @
|
|
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
|
-
|
|
312
|
+
// Load the link protocol buffer definition
|
|
313
|
+
const link_pb = pb(fs.readFileSync('node_modules/pathchain/proto/link.proto'));
|
|
361
314
|
|
|
362
|
-
|
|
315
|
+
// Validate target
|
|
316
|
+
if (target === "") {
|
|
317
|
+
return "The link must link something. Target can't be null.";
|
|
318
|
+
}
|
|
363
319
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
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');
|
|
367
324
|
author_folder = "";
|
|
368
|
-
}else{
|
|
369
|
-
author_folder = author_folder
|
|
325
|
+
} else {
|
|
326
|
+
author_folder = `${author_folder}/`;
|
|
370
327
|
}
|
|
371
328
|
|
|
372
|
-
|
|
373
|
-
register = dt.format(
|
|
374
|
-
|
|
375
|
-
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);
|
|
376
332
|
|
|
377
|
-
|
|
333
|
+
// Generate a unique hash for this link
|
|
334
|
+
const link_hash = sha256(`${register_moment_hash}_${author}_${prev}_${next}`);
|
|
378
335
|
|
|
379
|
-
if
|
|
380
|
-
|
|
381
|
-
}
|
|
382
|
-
if(
|
|
383
|
-
prev = link_hash;
|
|
384
|
-
}
|
|
385
|
-
if(next == ""){
|
|
386
|
-
next = link_hash;
|
|
387
|
-
}
|
|
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; }
|
|
388
340
|
|
|
389
|
-
//
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
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";
|
|
397
349
|
}
|
|
398
|
-
if(checker.checkFile("files/" + author_folder + "labels/" + target) == true){
|
|
399
|
-
target = 'labels/' + target;
|
|
400
|
-
console.log("Target is a label")
|
|
401
|
-
}
|
|
402
|
-
|
|
403
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
|
+
}
|
|
404
357
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
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}`,
|
|
410
364
|
target: target,
|
|
411
|
-
ancestor: author_folder
|
|
412
|
-
tag: author_folder
|
|
413
|
-
})
|
|
365
|
+
ancestor: `${author_folder}links/${ancestor}`,
|
|
366
|
+
tag: `${author_folder}links/${link_hash}`
|
|
367
|
+
});
|
|
414
368
|
|
|
415
|
-
|
|
416
|
-
|
|
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);
|
|
417
372
|
|
|
418
373
|
return link_hash;
|
|
419
374
|
}
|
|
420
375
|
|
|
421
|
-
|
|
422
|
-
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
package/updater.js
CHANGED
|
@@ -43,4 +43,98 @@ function useSecret(xsecret, xauthor = "") {
|
|
|
43
43
|
return secret_obj;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
|
|
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 }
|