godprotocol 1.2.20 → 1.2.21

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.
@@ -5,421 +5,487 @@ import { hash } from "godprotocol/utils/hash.js";
5
5
  import { copy_object } from "generalised-datastore/utils/functions.js";
6
6
 
7
7
  class Manager {
8
- constructor(name, options){
9
- this.name = name
10
- this.options = options
8
+ constructor(name, options) {
9
+ this.name = name;
10
+ this.options = options;
11
11
  this.trinity = options.trinity;
12
12
 
13
- this.accounts = new Object()
13
+ this.accounts = new Object();
14
14
 
15
- this.clock_speed = 0
16
- this.accounts_pairs = new Object()
15
+ this.clock_speed = 0;
16
+ this.accounts_pairs = new Object();
17
17
  }
18
18
 
19
- swap_result = async (id, thread)=>{
20
- if (typeof id === 'number'){
21
- id = thread.results[id]
19
+ swap_result = async (id, thread) => {
20
+ if (typeof id === "number") {
21
+ id = thread.results[id];
22
22
  }
23
-
24
- return id
25
- }
26
23
 
27
- set_bullet = async (bullet, thread, )=>{
28
- switch(bullet.type){
29
- case 'assignment':
30
- bullet.value = await this.swap_result(bullet.value, thread, )
24
+ return id;
25
+ };
26
+
27
+ set_bullet = async (bullet, thread) => {
28
+ switch (bullet.type) {
29
+ case "assignment":
30
+ bullet.value = await this.swap_result(bullet.value, thread);
31
31
  break;
32
- case 'call':
33
- bullet.identifier = await this.swap_result(bullet.identifier, thread, )
34
-
35
- for (let a = 0; a< bullet.arguments.length; a++){
36
- bullet.arguments[a] = await this.swap_result(bullet.arguments[a], thread, )
32
+ case "call":
33
+ bullet.identifier = await this.swap_result(bullet.identifier, thread);
34
+
35
+ for (let a = 0; a < bullet.arguments.length; a++) {
36
+ bullet.arguments[a] = await this.swap_result(
37
+ bullet.arguments[a],
38
+ thread
39
+ );
37
40
  }
38
41
  break;
39
- case 'return':
40
- bullet.output = await this.swap_result(bullet.output, thread)
42
+ case "return":
43
+ bullet.output = await this.swap_result(bullet.output, thread);
44
+ break;
45
+ case "module":
46
+ for (let b = 0; b < bullet.body.length; b++) {
47
+ bullet.body[b] = await this.swap_result(bullet.body[b], thread);
48
+ }
41
49
  break;
42
- case 'module':
43
- for(let b=0; b< bullet.body.length; b++){
44
- bullet.body[b] = await this.swap_result(bullet.body[b], thread)
50
+ case "nest":
51
+ for (let n = 0; n < bullet.nests.length; n++) {
52
+ bullet.nests[n] = await this.swap_result(bullet.nests[n], thread);
45
53
  }
46
- break
47
- case 'nest':
48
- for (let n=0; n< bullet.nests.length; n++){
49
- bullet.nests[n] = await this.swap_result(bullet.nests[n], thread, )
54
+ if (bullet.assignment) {
55
+ bullet.assignment = await this.swap_result(bullet.assignment, thread);
50
56
  }
51
- if (bullet.assignment){
52
- bullet.assignment = await this.swap_result(bullet.assignment, thread, )
53
- }
54
57
  break;
55
- case 'twain':
56
- for (let key in bullet.value){
57
- let [prop, value] = bullet.value[key]
58
- bullet.value[key][0] = await this.swap_result(prop, thread, )
59
- bullet.value[key][1] = await this.swap_result(value, thread, )
58
+ case "twain":
59
+ for (let key in bullet.value) {
60
+ let [prop, value] = bullet.value[key];
61
+ bullet.value[key][0] = await this.swap_result(prop, thread);
62
+ bullet.value[key][1] = await this.swap_result(value, thread);
60
63
  }
61
- break
62
- case 'array':
63
- for (let i=0; i< bullet.value.length; i++){
64
- let val = bullet.value[i]
65
- bullet.value[i] = await this.swap_result(val, thread, )
64
+ break;
65
+ case "array":
66
+ for (let i = 0; i < bullet.value.length; i++) {
67
+ let val = bullet.value[i];
68
+ bullet.value[i] = await this.swap_result(val, thread);
66
69
  }
67
- break
68
- case 'variable':
69
70
  break;
70
- case 'try':
71
- }
71
+ case "variable":
72
+ break;
73
+ case "try":
74
+ }
72
75
 
73
- return bullet
74
- }
76
+ return bullet;
77
+ };
75
78
 
76
- _stack = new Array()
79
+ _stack = new Array();
77
80
 
78
- break_count = 0
79
-
80
- start_clock = async()=>{
81
- this.clock = setInterval(async() => {
81
+ break_count = 0;
82
+
83
+ start_clock = async () => {
84
+ this.clock = setInterval(async () => {
82
85
  this.clock_running = true;
83
- for (let account in this.accounts_pairs){
84
- let account_pair = this.accounts_pairs[account]
85
- let {threads, threadlist, state, current_thread} = account_pair
86
-
87
- if (state === 'running')
88
- continue;
89
-
90
- let total_loop = 0
91
- while(threadlist.length && (total_loop < threadlist.length)){
92
- let thread_id = threadlist[account_pair.current_thread]
93
- let thread = threads[thread_id]
94
- if (!thread){
95
- account_pair.current_thread = 0
96
- continue
86
+ for (let account in this.accounts_pairs) {
87
+ let account_pair = this.accounts_pairs[account];
88
+ let { threads, threadlist, state, current_thread } = account_pair;
89
+
90
+ if (state === "running") continue;
91
+
92
+ let total_loop = 0;
93
+ while (threadlist.length && total_loop < threadlist.length) {
94
+ let thread_id = threadlist[account_pair.current_thread];
95
+ let thread = threads[thread_id];
96
+ if (!thread) {
97
+ account_pair.current_thread = 0;
98
+ continue;
97
99
  }
98
- let {bullets, pointer, callback, results, chain, spawn, status, _stack, on_pointer} = thread
99
-
100
- if (status && status.start){
101
- if (Date.now() - status.start > status.duration){
102
- thread.status = 'active'
100
+ let {
101
+ bullets,
102
+ pointer,
103
+ callback,
104
+ results,
105
+ chain,
106
+ spawn,
107
+ status,
108
+ _stack,
109
+ on_pointer,
110
+ } = thread;
111
+
112
+ if (status && status.start) {
113
+ if (Date.now() - status.start > status.duration) {
114
+ thread.status = "active";
103
115
  }
104
- }
105
- if (status === 'active'){
106
- account_pair.current_thread++
107
- account_pair.state = 'running'
116
+ }
117
+ if (status === "active") {
118
+ account_pair.current_thread++;
119
+ account_pair.state = "running";
108
120
  } else {
109
- total_loop++
110
- account_pair.current_thread++
111
- continue
121
+ total_loop++;
122
+ account_pair.current_thread++;
123
+ continue;
112
124
  }
113
-
114
- total_loop = 0
115
- let current_bullet = bullets[pointer], res;
116
-
117
- if (current_bullet){
118
- if (current_bullet.type === 'loop'){
119
- _stack.push(current_bullet)
120
- } else if (current_bullet.type === 'try'){
121
- _stack.push(current_bullet)
122
- } else if(current_bullet.type === 'catch'){
123
- let try_conf = _stack.pop()
124
- thread.pointer += try_conf.catch_offset
125
- } else if(current_bullet.type === 'endloop'){
126
- let loop_conf = _stack.pop()
127
- if (!loop_conf || loop_conf.type !== 'loop'){
128
- console.log(`ErrorLog: Corrupt stacking`)
129
- thread.pointer = bullets.length
130
- }else thread.pointer = loop_conf.conditional_jmp
125
+
126
+ total_loop = 0;
127
+ let current_bullet = bullets[pointer],
128
+ res;
129
+
130
+ if (current_bullet) {
131
+ if (current_bullet.type === "loop") {
132
+ _stack.push(current_bullet);
133
+ } else if (current_bullet.type === "try") {
134
+ _stack.push(current_bullet);
135
+ } else if (current_bullet.type === "catch") {
136
+ let try_conf = _stack.pop();
137
+ thread.pointer += try_conf.catch_offset;
138
+ } else if (current_bullet.type === "endloop") {
139
+ let loop_conf = _stack.pop();
140
+ if (!loop_conf || loop_conf.type !== "loop") {
141
+ console.log(`ErrorLog: Corrupt stacking`);
142
+ thread.pointer = bullets.length;
143
+ } else thread.pointer = loop_conf.conditional_jmp;
131
144
  } else {
132
- let account_obj = this.accounts[account]
133
-
134
- if (Object.keys(current_bullet).includes('jmp')){}
135
- else
136
- res = await account_obj.vm.execute(this.set_bullet(copy_object(current_bullet), thread), {chain, manager: this, thread})
137
-
138
- if (res && res.__interrupt__){
139
- if (res.type === 'return'){
140
- if (spawn){
141
- let thrd = threads[spawn.thread_id]
142
- thrd.results[spawn.pointer] = res.output
143
- thread.pointer = bullets.length-1
144
- }else res = res.output
145
- } else if (['break', 'continue'].includes(res.__interrupt__)){
145
+ let account_obj = this.accounts[account];
146
+
147
+ if (Object.keys(current_bullet).includes("jmp")) {
148
+ } else
149
+ res = await account_obj.vm.execute(
150
+ this.set_bullet(copy_object(current_bullet), thread),
151
+ { chain, manager: this, thread }
152
+ );
153
+
154
+ if (res && res.__interrupt__) {
155
+ if (res.type === "return") {
156
+ if (spawn) {
157
+ let thrd = threads[spawn.thread_id];
158
+ thrd.results[spawn.pointer] = res.output;
159
+ thread.pointer = bullets.length - 1;
160
+ } else res = res.output;
161
+ } else if (["break", "continue"].includes(res.__interrupt__)) {
146
162
  let caught;
147
- while(_stack.length){
148
- let loop = _stack.slice(-1)[0]
149
-
150
- if (loop && loop.type === 'loop'){
151
- if (res.__interrupt__ === 'break'){
152
- thread.pointer = loop.conditional_jmp -1
153
- }else if(res.__interrupt__ === 'continue'){
154
- thread.pointer = loop.update_jmp -1
163
+ while (_stack.length) {
164
+ let loop = _stack.slice(-1)[0];
165
+
166
+ if (loop && loop.type === "loop") {
167
+ if (res.__interrupt__ === "break") {
168
+ thread.pointer = loop.conditional_jmp - 1;
169
+ } else if (res.__interrupt__ === "continue") {
170
+ thread.pointer = loop.update_jmp - 1;
155
171
  }
156
- caught = true
157
- break
172
+ caught = true;
173
+ break;
158
174
  }
159
- _stack.pop()
175
+ _stack.pop();
160
176
  }
161
- if (!caught){
162
- console.log(`ErrorLog: Loop keywords can only be used inside a loop`)
163
- thread.pointer = bullets.length
177
+ if (!caught) {
178
+ console.log(
179
+ `ErrorLog: Loop keywords can only be used inside a loop`
180
+ );
181
+ thread.pointer = bullets.length;
164
182
  }
165
- }else {
166
- let i = _stack.length - 1, caught;
167
- while (_stack.length){
168
- let tstack = _stack[i]
169
- i--
170
- let t_catch = bullets[tstack.catch_index]
171
-
172
- if (!t_catch.objects.length || t_catch.objects.includes(res.payload.type)){
173
- thread.pointer = tstack.catch_index
183
+ } else {
184
+ let i = _stack.length - 1,
185
+ caught;
186
+ while (_stack.length) {
187
+ let tstack = _stack[i];
188
+ i--;
189
+ let t_catch = bullets[tstack.catch_index];
190
+
191
+ if (
192
+ !t_catch.objects.length ||
193
+ t_catch.objects.includes(res.payload.type)
194
+ ) {
195
+ thread.pointer = tstack.catch_index;
174
196
 
175
197
  if (tstack.alias)
176
- await account_obj.vm.error_instance({location: tstack.location, payload: res.payload}, tstack.alias)
177
-
178
- _stack.pop()
179
- caught = true
180
- break
198
+ await account_obj.vm.error_instance(
199
+ { location: tstack.location, payload: res.payload },
200
+ tstack.alias
201
+ );
202
+
203
+ _stack.pop();
204
+ caught = true;
205
+ break;
181
206
  }
182
- _stack.pop()
207
+ _stack.pop();
183
208
  }
184
- if (!caught){
185
- console.log(`ErrorLog: ${res.payload.message}`)
186
- if (spawn){
187
- let thrd = threads[spawn.thread_id]
188
- thrd.results[spawn.pointer] = res
209
+ if (!caught) {
210
+ console.log(`ErrorLog: ${res.payload.message}`);
211
+ if (spawn) {
212
+ let thrd = threads[spawn.thread_id];
213
+ thrd.results[spawn.pointer] = res;
189
214
  }
190
- thread.pointer = bullets.length
215
+ thread.pointer = bullets.length;
191
216
  }
192
217
  }
193
- }else {
194
- if (current_bullet.type === 'assignment'){
195
- bullets.map(bull=>{
196
- if (bull.type === 'variable' && bull.location === current_bullet.location){
197
- bull._id = res._id
218
+ } else {
219
+ if (current_bullet.type === "assignment") {
220
+ bullets.map((bull) => {
221
+ if (
222
+ bull.type === "variable" &&
223
+ bull.location === current_bullet.location
224
+ ) {
225
+ bull._id = res._id;
198
226
  }
199
- })
227
+ });
200
228
  }
201
- if (current_bullet.type === 'nest' && current_bullet.assignment){
202
- if (current_bullet.assignment_location){
203
- bullets.map(bull=>{
204
- if (bull.type === 'variable' && bull.location === current_bullet.assignment_location){
205
- bull._id = res._id
206
- let trd = thread
207
- while (trd.spawn){
208
- let tid = trd.spawn.thread_id
209
- trd = threads[tid]
210
- trd.bullets.map(bl=>{
211
- if (bl.type === 'variable' && bl.location === current_bullet.assignment_location){
212
- bl._id = res._id
229
+ if (
230
+ current_bullet.type === "nest" &&
231
+ current_bullet.assignment
232
+ ) {
233
+ if (current_bullet.assignment_location) {
234
+ bullets.map((bull) => {
235
+ if (
236
+ bull.type === "variable" &&
237
+ bull.location === current_bullet.assignment_location
238
+ ) {
239
+ bull._id = res._id;
240
+ let trd = thread;
241
+ while (trd.spawn) {
242
+ let tid = trd.spawn.thread_id;
243
+ trd = threads[tid];
244
+ trd.bullets.map((bl) => {
245
+ if (
246
+ bl.type === "variable" &&
247
+ bl.location === current_bullet.assignment_location
248
+ ) {
249
+ bl._id = res._id;
213
250
  }
214
- })
251
+ });
215
252
  }
216
253
  }
217
- })
254
+ });
218
255
  }
219
256
  }
220
- if (current_bullet.conditional_jmp){
221
- if (!res.literal){
222
- console.log("Manager.js::SHOUT!")
257
+ if (current_bullet.conditional_jmp) {
258
+ if (!res.literal) {
259
+ console.log("Manager.js::SHOUT!");
223
260
  }
224
- let pass = await res.literal()
225
-
226
- if (current_bullet.invert){
227
- if(!pass){
228
- thread.pointer = current_bullet.conditional_jmp - 1
261
+ let pass = await res.literal();
262
+
263
+ if (current_bullet.invert) {
264
+ if (!pass) {
265
+ thread.pointer = current_bullet.conditional_jmp - 1;
229
266
  }
230
- }else{
231
- if (!pass){
232
- thread.pointer = current_bullet.conditional_jmp - 1
267
+ } else {
268
+ if (!pass) {
269
+ thread.pointer = current_bullet.conditional_jmp - 1;
233
270
  }
234
271
  }
235
272
  }
236
- if (current_bullet.jmp){
237
- thread.pointer = current_bullet.jmp - 1
273
+ if (current_bullet.jmp) {
274
+ thread.pointer = current_bullet.jmp - 1;
238
275
  }
239
276
  }
240
277
 
241
- results[pointer] = res
278
+ results[pointer] = res;
242
279
 
243
- on_pointer && await on_pointer(pointer, res)
280
+ on_pointer && (await on_pointer(pointer, res));
244
281
  }
245
282
  }
246
-
247
- thread.pointer++
248
- if (thread.pointer >= bullets.length){
249
- callback && await callback(res, thread)
250
- delete threads[thread_id]
251
- delete this.accounts[account].vm.envs[thread_id]
252
- account_pair.current_thread --
253
- threadlist.splice(account_pair.current_thread, 1)
254
- if (spawn){
255
- let thrd = threads[spawn.thread_id]
256
- if(thrd){
257
- thrd.status = 'active'
283
+
284
+ thread.pointer++;
285
+ if (thread.pointer >= bullets.length) {
286
+ callback && (await callback(res, thread));
287
+ delete threads[thread_id];
288
+ delete this.accounts[account].vm.envs[thread_id];
289
+ account_pair.current_thread--;
290
+ threadlist.splice(account_pair.current_thread, 1);
291
+ if (spawn) {
292
+ let thrd = threads[spawn.thread_id];
293
+ if (thrd) {
294
+ thrd.status = "active";
258
295
  }
259
296
  }
260
297
  }
261
- if (account_pair.current_thread === threadlist.length){
262
- account_pair.current_thread = 0
298
+ if (account_pair.current_thread === threadlist.length) {
299
+ account_pair.current_thread = 0;
263
300
  }
264
-
265
- account_pair.state = 'active'
266
- break
301
+
302
+ account_pair.state = "active";
303
+ break;
267
304
  }
268
- if (!threadlist.length){
269
- delete this.accounts_pairs[account]
270
- if (!Object.keys(this.accounts_pairs).length){
271
- this.clock_running = false
272
- clearInterval(this.clock)
305
+ if (!threadlist.length) {
306
+ delete this.accounts_pairs[account];
307
+ if (!Object.keys(this.accounts_pairs).length) {
308
+ this.clock_running = false;
309
+ clearInterval(this.clock);
273
310
  }
274
311
  }
275
312
  }
276
313
  }, this.clock_speed);
277
- }
314
+ };
278
315
 
279
- load = async(thread, account, options={})=>{
280
- let {callback, on_pointer, spawn, addr, pointer} = options
281
- let tracker = this.accounts_pairs[account.name]
316
+ load = async (thread, account, options = {}) => {
317
+ let { callback, on_pointer, spawn, addr, pointer } = options;
318
+ let tracker = this.accounts_pairs[account.name];
282
319
 
283
- if (!tracker){
320
+ if (!tracker) {
284
321
  tracker = {
285
322
  threadlist: [],
286
323
  threads: {},
287
324
  current_thread: 0,
288
- }
289
- this.accounts_pairs[account.name] = tracker
325
+ };
326
+ this.accounts_pairs[account.name] = tracker;
290
327
  }
291
-
292
- let thread_id = Math.random().toString()
293
- tracker.threadlist.push(thread_id)
328
+
329
+ let thread_id = Math.random().toString();
330
+ tracker.threadlist.push(thread_id);
294
331
  tracker.threads[thread_id] = {
295
- bullets: thread,
296
- _id: thread_id,
332
+ bullets: thread,
333
+ _id: thread_id,
297
334
  on_pointer,
298
- pointer: 0,
335
+ pointer: 0,
299
336
  results: {},
300
- _stack: new Array(),
301
- status: 'active',
337
+ _stack: new Array(),
338
+ status: "active",
302
339
  addr,
303
- chain: account.chain,
304
- spawn: spawn? { thread_id: spawn, pointer } : null,
305
- callback
340
+ chain: account.chain,
341
+ spawn: spawn ? { thread_id: spawn, pointer } : null,
342
+ callback,
306
343
  };
307
- if (spawn){
308
- let spawn_thread = tracker.threads[spawn]
309
-
310
- spawn_thread.status = thread_id
344
+ if (spawn) {
345
+ let spawn_thread = tracker.threads[spawn];
346
+
347
+ spawn_thread.status = thread_id;
311
348
  }
312
349
 
313
- if (!this.clock_running){
314
- await this.start_clock()
350
+ if (!this.clock_running) {
351
+ await this.start_clock();
315
352
  }
316
353
 
317
- return thread_id
318
- }
354
+ return thread_id;
355
+ };
319
356
 
320
- sync = async()=>{
321
- this.path = `${this.trinity.game}/${this.name}`
322
- this.hash = hash
357
+ sync = async () => {
358
+ this.path = `${this.trinity.game}/${this.name}`;
359
+ this.hash = hash;
323
360
 
324
- this.ds = new GDS(this)
325
- await this.ds.sync()
361
+ this.ds = new GDS(this);
362
+ await this.ds.sync();
326
363
  let create_serv;
327
- if (this.options.server){
328
- this.server = this.options.server
364
+ if (this.options.server) {
365
+ this.server = this.options.server;
329
366
  if (this.options.server.port)
330
- this.options.server_domain = `${this.options.server.hostname}:${this.options.server.port}`
331
- else this.options.server_domain = this.options.server.hostname
332
- let handle = create_server({manager: this, ...this.options.server, app: this.options.app })
333
-
334
- this.handler = (req, res, cb)=>{
335
-
336
- handle(req,res)
337
-
338
- if (this.options.oracle && !this.served){
339
- this.served = true
367
+ this.options.server_domain = `${this.options.server.hostname}:${this.options.server.port}`;
368
+ else this.options.server_domain = this.options.server.hostname;
369
+ let handle = create_server({
370
+ manager: this,
371
+ ...this.options.server,
372
+ app: this.options.app,
373
+ });
374
+
375
+ this.handler = (req, res, cb) => {
376
+ handle(req, res);
377
+
378
+ if (this.options.oracle && !this.served) {
379
+ this.served = true;
340
380
  this.oracle = this.options.oracle;
341
- this.oracle.set_manager(this)
342
- .then(async()=>{
343
- if (this.options.init_account){
344
- await this.add_account(this.options.init_account.name, {password: this.options.init_account.password, init: true})
345
-
346
- cb && cb()
347
- }
348
- })
381
+ this.oracle.sync(this).then(async () => {
382
+ if (this.options.init_account) {
383
+ await this.add_account(this.options.init_account.name, {
384
+ password: this.options.init_account.password,
385
+ init: true,
386
+ });
387
+
388
+ cb && cb();
389
+ }
390
+ });
349
391
  }
350
- }
392
+ };
351
393
  }
352
394
 
353
395
  return create_serv;
354
- }
396
+ };
355
397
 
356
- get_init = async()=>{
398
+ get_init = async () => {
357
399
  let acc = this.init_account;
358
400
 
359
401
  if (!acc && this.options.init_account) {
360
- acc = await this.add_account(this.options.init_account.name, {password: this.options.init_account.password, init: true})
402
+ acc = await this.add_account(this.options.init_account.name, {
403
+ password: this.options.init_account.password,
404
+ init: true,
405
+ });
361
406
  }
362
407
 
363
408
  return acc;
364
- }
409
+ };
365
410
 
366
- get_from_repo = async(address, options={}) =>{
367
- let acc = this.get_acc(options)
411
+ get_from_repo = async (address, options = {}) => {
412
+ let acc = this.get_acc(options);
368
413
 
369
- address = `.storage/${acc}/${address}/__literal__`
370
- let content = await this.oracle.get_from_repos(address, {count: 1, args: options})
414
+ address = `.storage/${acc}/${address}/__literal__`;
415
+ let content = await this.oracle.get_from_repos(address, {
416
+ count: 1,
417
+ args: options,
418
+ });
371
419
 
372
- return content && JSON.parse(content)
373
- }
420
+ return content && JSON.parse(content);
421
+ };
374
422
 
375
- get_acc = options=>{
376
- let acc = options.account
377
- if (!acc){
378
- acc = this.options.init_account
379
- if (!acc) return
380
- acc = `Accounts/${acc.name}`
423
+ get_acc = (options) => {
424
+ let acc = options.account;
425
+ if (!acc) {
426
+ acc = this.options.init_account;
427
+ if (!acc) return;
428
+ acc = `Accounts/${acc.name}`;
381
429
  }
382
430
 
383
431
  return acc;
384
- }
432
+ };
385
433
 
386
- set_to_repo = async(address, content, options={}) =>{
387
- let acc = this.get_acc(options)
434
+ set_to_repo = async (address, content, options = {}) => {
435
+ let acc = this.get_acc(options);
388
436
 
389
- address = `.storage/${acc}/${address}/__literal__`
390
- let response = await this.oracle.set_to_repos(address, JSON.stringify(content), {count: 1, args: options})
437
+ address = `.storage/${acc}/${address}/__literal__`;
438
+ let response = await this.oracle.set_to_repos(
439
+ address,
440
+ JSON.stringify(content),
441
+ { count: 1, args: options }
442
+ );
391
443
 
392
- return response
393
- }
444
+ return response;
445
+ };
394
446
 
395
- get_account = async(name) => {
396
- let acc = this.accounts[name]
397
- if (!acc){
398
- let servers = await this.oracle.get_servers(`Accounts/${name}`)
399
- if(servers.length){
400
- acc = await this.add_account(name, {servers})
447
+ get_account = async (name) => {
448
+ let acc = this.accounts[name];
449
+ if (!acc) {
450
+ let servers = await this.oracle.get_servers(`Accounts/${name}`);
451
+ if (servers.length) {
452
+ acc = await this.add_account(name, { servers });
401
453
  }
402
454
  }
403
455
 
404
456
  return acc;
405
- }
457
+ };
406
458
 
407
- add_account = async(name, options={}) => {
408
- let acc = this.accounts[name]
459
+ add_account = async (name, options = {}) => {
460
+ let acc = this.accounts[name];
409
461
  if (!acc) {
410
- acc = new Account(name, {...options, manager: this})
411
- let res = await acc.sync()
412
- if (res)
413
- this.accounts[name] = acc
414
- else return
462
+ acc = new Account(name, { ...options, manager: this });
463
+ let res = await acc.sync();
464
+ if (res) this.accounts[name] = acc;
465
+ else return;
415
466
  }
416
467
 
417
- if (options.init){
418
- this.init_account = acc
468
+ if (options.init) {
469
+ this.init_account = acc;
419
470
  }
420
471
 
421
472
  return acc;
422
- }
473
+ };
474
+
475
+ folder = async (address, options = {}) => {
476
+ let acc;
477
+ if (options.account) {
478
+ acc = `Accounts/${options.account}`;
479
+ } else if (this.options.init_account) {
480
+ acc = `Accounts/${this.options.init_account.name}`;
481
+ }
482
+
483
+ address = `${acc}/${address}`;
484
+
485
+ let folder = await this.ds.folder(address, { account: acc });
486
+
487
+ return folder;
488
+ };
423
489
  }
424
490
 
425
491
  export default Manager;
package/Objects/Oracle.js CHANGED
@@ -13,6 +13,7 @@ class Oracle extends Handlers {
13
13
  this.remotes = new Array();
14
14
 
15
15
  this.account_repos = new Object();
16
+ this.configs = new Object();
16
17
  }
17
18
 
18
19
  fetch = async (payload, server) => {
@@ -34,7 +35,7 @@ class Oracle extends Handlers {
34
35
  let { method, args } = payload;
35
36
 
36
37
  switch (method) {
37
- case "update_server":
38
+ case "update_servers":
38
39
  let { repos, account, server } = args;
39
40
 
40
41
  let servers = await this.get_servers(account);
@@ -88,9 +89,10 @@ class Oracle extends Handlers {
88
89
 
89
90
  let fnd =
90
91
  this.repos.find((r) => r.repo.get_id() === repo) ||
91
- acc_repos.find((r) => r.repo._id === repo);
92
+ acc_repos.find((r) => (r.repo._id || r.repo.get_id()) === repo);
92
93
  if (fnd) {
93
94
  if (fnd.repo.get_id) {
95
+ fnd = { ...fnd };
94
96
  fnd.repo = fnd.repo.objectify();
95
97
  }
96
98
  repos[r] = fnd;
@@ -135,11 +137,12 @@ class Oracle extends Handlers {
135
137
  parse_option_repos = async (options) => {
136
138
  let repos = [];
137
139
  if (options.repos) {
138
- for (let r = 0; r < options.repos[r]; r++) {
140
+ for (let r = 0; r < options.repos.length; r++) {
139
141
  let rep = options.repos[r];
142
+
140
143
  repos.push({
141
144
  filter: rep.filter,
142
- repo: await this.cloth_repo(rep.repo),
145
+ repo: rep.repo.get_id ? rep.repo : await this.cloth_repo(rep.repo),
143
146
  });
144
147
  }
145
148
  } else repos = this.repos;
@@ -150,7 +153,6 @@ class Oracle extends Handlers {
150
153
  set_to_repos = async (path, content, options = {}) => {
151
154
  let { exclude } = options;
152
155
  let repos = await this.parse_option_repos(options),
153
- content,
154
156
  reps = [];
155
157
 
156
158
  for (let r = 0; r < repos.length; r++) {
@@ -193,22 +195,25 @@ class Oracle extends Handlers {
193
195
  dot_config = ".config";
194
196
 
195
197
  write = async (path, content, options = {}) => {
196
- let { account, address } = options;
198
+ let { account, address } = options,
199
+ conf;
197
200
 
198
201
  if (path.endsWith(this.dot_config)) {
199
- conf = await this.get_config(
200
- path.slice(0, (this.dot_config.length + 1) * -1)
201
- );
202
+ let sli = path.slice(0, (this.dot_config.length + 1) * -1);
203
+ conf = await this.get_config(sli);
202
204
 
203
205
  let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
204
- let reps = this.merge_repos(conf_repos, await this.filter_repos(path));
206
+ let reps = await this.merge_repos(
207
+ conf_repos,
208
+ await this.filter_repos(path)
209
+ );
205
210
 
206
211
  await this.set_to_repos(
207
212
  `.oracle/${path}`,
208
213
  JSON.stringify({
209
214
  ...conf,
210
215
  content,
211
- repos: reps.map((r) => r.repo._id),
216
+ repositories: reps.map((r) => r.repo._id || r.repo.get_id()),
212
217
  }),
213
218
  { repos: reps }
214
219
  );
@@ -216,8 +221,10 @@ class Oracle extends Handlers {
216
221
  conf = await this.get_config(address, true);
217
222
 
218
223
  let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
219
- let reps = this.merge_repos(conf_repos, await this.filter_repos(path));
220
-
224
+ let reps = await this.merge_repos(
225
+ conf_repos,
226
+ await this.filter_repos(path)
227
+ );
221
228
  await this.set_to_repos(`.storage/${path}`, content, { repos: reps });
222
229
  }
223
230
  };
@@ -226,7 +233,7 @@ class Oracle extends Handlers {
226
233
  let { account, address } = options,
227
234
  content;
228
235
 
229
- if (path.endsWith(this.dot_config.length)) {
236
+ if (path.endsWith(this.dot_config)) {
230
237
  content = await this.get_config(
231
238
  path.slice(0, (this.dot_config.length + 1) * -1)
232
239
  );
@@ -234,7 +241,10 @@ class Oracle extends Handlers {
234
241
  } else {
235
242
  let conf = await this.get_config(address, true);
236
243
  let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
237
- let reps = this.merge_repos(conf_repos, await this.filter_repos(path));
244
+ let reps = await this.merge_repos(
245
+ conf_repos,
246
+ await this.filter_repos(path)
247
+ );
238
248
 
239
249
  content = await this.get_from_repos(`.storage/${path}`, { repos: reps });
240
250
  }
@@ -242,7 +252,7 @@ class Oracle extends Handlers {
242
252
  return content;
243
253
  };
244
254
 
245
- update_server = async (account) => {
255
+ update_servers = async (account) => {
246
256
  let servers = await this.get_servers(account);
247
257
  if (servers.find((serv) => this.server_match(serv, this.server))) {
248
258
  return;
@@ -260,7 +270,7 @@ class Oracle extends Handlers {
260
270
 
261
271
  let reps = await this.fetch(
262
272
  {
263
- method: "update_server",
273
+ method: "update_servers",
264
274
  args: { account, repos, server: this.server },
265
275
  },
266
276
  server
@@ -285,15 +295,26 @@ class Oracle extends Handlers {
285
295
  return filtered_repos;
286
296
  };
287
297
 
298
+ get_repo = async (repo) => {
299
+ for (let r = 0; r < this.repos.length; r++) {
300
+ let rep = this.repos[r];
301
+
302
+ if (rep.repo.get_id() === repo.get_id()) return rep.repo;
303
+ }
304
+ };
305
+
288
306
  add_repo = async (repository) => {
289
307
  let { filter, repo } = repository;
290
- if (await this.get_repo(repo.type, repo.options.name)) return;
291
308
 
292
309
  repo = await this.cloth_repo(repo);
310
+ if (await this.get_repo(repo)) return;
311
+
293
312
  this.repos.push({ filter, repo });
294
313
  };
295
314
 
296
- sync = async () => {};
315
+ sync = async (mgr) => {
316
+ this.manager = mgr;
317
+ };
297
318
  }
298
319
 
299
320
  export default Oracle;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.2.20",
3
+ "version": "1.2.21",
4
4
  "description": "A distributed computing environment",
5
5
  "main": "Index.js",
6
6
  "scripts": {
@@ -28,12 +28,9 @@
28
28
  "type": "module",
29
29
  "homepage": "https://github.com/immanuel-savvy/GodProtocol#readme",
30
30
  "dependencies": {
31
- "@octokit/rest": "^21.0.2",
32
31
  "crypto": "^1.0.1",
33
- "elliptic": "^6.6.0",
34
32
  "fs": "^0.0.1-security",
35
33
  "generalised-datastore": "latest",
36
- "js-base64": "^3.7.7",
37
34
  "aircode4": "latest"
38
35
  },
39
36
  "devDependencies": {
package/repos/fs.js CHANGED
@@ -1,114 +1,122 @@
1
- import fs from 'fs'
2
- import { copy_object } from 'generalised-datastore/utils/functions.js'
3
- import Repository from 'godprotocol/Objects/Repository.js';
1
+ import fs from "fs";
2
+ import { copy_object } from "generalised-datastore/utils/functions.js";
3
+ import Repository from "godprotocol/Objects/Repository.js";
4
4
  import { post_request } from "godprotocol/utils/services.js";
5
5
 
6
- class Fs extends Repository{
7
- constructor(options={}){
8
- super()
9
-
6
+ class Fs extends Repository {
7
+ constructor(options = {}) {
8
+ super();
9
+
10
10
  this._fs = fs;
11
11
  this.__dirname = options.__dirname;
12
12
  this.remote = options.remote;
13
13
  this.name = options.name;
14
14
 
15
- this.type = 'fs'
15
+ this.type = "fs";
16
16
  this.cache = new Object();
17
17
  }
18
18
 
19
- parse_path = async(path)=>{
20
- path = path.split('/')
21
- let addr = path.slice(0,-1).join('/')
22
- if (!this._fs.existsSync(addr)){
23
- this._fs.mkdirSync(addr, {recursive: true})
19
+ parse_path = async (path) => {
20
+ path = path.split("/");
21
+ let addr = path.slice(0, -1).join("/");
22
+ if (!this._fs.existsSync(addr)) {
23
+ this._fs.mkdirSync(addr, { recursive: true });
24
24
  }
25
- }
25
+ };
26
26
 
27
- call = async payload=>{
27
+ call = async (payload) => {
28
28
  let result;
29
- switch(payload.op){
30
- case 'write':
31
- result = await this.write(payload.path, payload.data)
32
- break
33
- case 'read':
34
- result = await this.read(payload.path)
29
+ switch (payload.op) {
30
+ case "write":
31
+ result = await this.write(payload.path, payload.data);
32
+ break;
33
+ case "read":
34
+ result = await this.read(payload.path);
35
35
  break;
36
36
  }
37
37
  return result;
38
- }
39
-
40
- fetch = async payload=>{
41
- let res = await post_request({
42
- options: {
43
- ...this.remote,
44
- path: '/oracle'
45
- },
46
- data: JSON.stringify({method: 'fs', arg: {payload, name: this.name}})
47
- })
38
+ };
39
+
40
+ fetch = async (payload) => {
41
+ let res;
42
+ if (this.remote) {
43
+ res = await post_request({
44
+ options: {
45
+ ...this.remote,
46
+ path: "/oracle",
47
+ },
48
+ data: JSON.stringify({
49
+ method: "fs",
50
+ arg: { payload, name: this.name },
51
+ }),
52
+ });
53
+ } else {
54
+ res = await this.call(payload);
55
+ }
48
56
 
49
57
  return res;
50
- }
58
+ };
51
59
 
52
- write_file = async(path, data)=>{
53
- path = this.path(path)
60
+ write_file = async (path, data) => {
61
+ path = this.path(path);
62
+ console.log(path, data);
54
63
 
55
- await this.fetch({path, data, op: 'write'})
56
- }
64
+ await this.fetch({ path, data, op: "write" });
65
+ };
57
66
 
58
- write = async(path, data) => {
59
- await this.parse_path(path)
60
- this._fs.writeFileSync(path, data)
61
-
62
- this.cache[path] = data
63
- }
67
+ write = async (path, data) => {
68
+ await this.parse_path(path);
69
+ this._fs.writeFileSync(path, data);
64
70
 
65
- read = async(path)=>{
71
+ this.cache[path] = data;
72
+ };
73
+
74
+ read = async (path) => {
66
75
  let data;
67
- try{
68
- data = this.cache[path]
69
- if (!data){
70
- await this.parse_path(path)
71
- data = this._fs.readFileSync(path, {encoding: 'utf-8'})
72
- }else {
73
- data = copy_object(data)
76
+ try {
77
+ data = this.cache[path];
78
+ if (!data) {
79
+ await this.parse_path(path);
80
+ data = this._fs.readFileSync(path, { encoding: "utf-8" });
81
+ } else {
82
+ data = copy_object(data);
74
83
  }
75
-
76
- }catch(e){
77
- console.log(e.message)
84
+ } catch (e) {
85
+ console.log(e.message);
78
86
  }
79
87
 
80
88
  return data;
81
- }
89
+ };
82
90
 
83
- read_file = async(path)=>{
84
- path = this.path(path)
91
+ read_file = async (path) => {
92
+ path = this.path(path);
93
+
94
+ let data = await this.fetch({ path, op: "read" });
85
95
 
86
- let data = await this.fetch({path, op: 'read'})
87
-
88
96
  return data;
89
- }
97
+ };
90
98
 
91
- path = pth=>{
92
- return `${this.__dirname}/${pth}`
93
- }
99
+ path = (pth) => {
100
+ return `${this.__dirname}/${pth}`;
101
+ };
94
102
 
95
- mkdir = async(path)=>{
103
+ mkdir = async (path) => {
96
104
  let done;
97
- try{
98
- this._fs.mkdirSync(this.path(path), {recursive: true})
99
- done = true
100
- }catch(e){
101
- done = false
105
+ try {
106
+ this._fs.mkdirSync(this.path(path), { recursive: true });
107
+ done = true;
108
+ } catch (e) {
109
+ done = false;
102
110
  }
103
111
 
104
112
  return done;
105
- }
113
+ };
106
114
 
107
- exists = async(path)=>{
108
- path = this.path(path)
109
-
110
- return !!this.cache[path] || this._fs.existsSync(path)
111
- }
115
+ exists = async (path) => {
116
+ path = this.path(path);
117
+
118
+ return !!this.cache[path] || this._fs.existsSync(path);
119
+ };
112
120
  }
113
121
 
114
- export default Fs
122
+ export default Fs;