godprotocol 1.2.18 → 1.2.20

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.
@@ -353,6 +353,45 @@ class Manager {
353
353
  return create_serv;
354
354
  }
355
355
 
356
+ get_init = async()=>{
357
+ let acc = this.init_account;
358
+
359
+ 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})
361
+ }
362
+
363
+ return acc;
364
+ }
365
+
366
+ get_from_repo = async(address, options={}) =>{
367
+ let acc = this.get_acc(options)
368
+
369
+ address = `.storage/${acc}/${address}/__literal__`
370
+ let content = await this.oracle.get_from_repos(address, {count: 1, args: options})
371
+
372
+ return content && JSON.parse(content)
373
+ }
374
+
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}`
381
+ }
382
+
383
+ return acc;
384
+ }
385
+
386
+ set_to_repo = async(address, content, options={}) =>{
387
+ let acc = this.get_acc(options)
388
+
389
+ address = `.storage/${acc}/${address}/__literal__`
390
+ let response = await this.oracle.set_to_repos(address, JSON.stringify(content), {count: 1, args: options})
391
+
392
+ return response
393
+ }
394
+
356
395
  get_account = async(name) => {
357
396
  let acc = this.accounts[name]
358
397
  if (!acc){
package/Objects/Oracle.js CHANGED
@@ -3,356 +3,297 @@ import Github from "godprotocol/repos/github.js";
3
3
  import Handlers from "godprotocol/utils/oracle_handlers.js";
4
4
  import { post_request } from "godprotocol/utils/services.js";
5
5
 
6
- class Oracle extends Handlers{
6
+ class Oracle extends Handlers {
7
7
  constructor(server, mirror) {
8
8
  super();
9
9
 
10
- this.server = server
10
+ this.server = server;
11
11
  this.mirror = mirror;
12
- this.repos = new Array()
13
- this.remotes = new Array()
12
+ this.repos = new Array();
13
+ this.remotes = new Array();
14
+
15
+ this.account_repos = new Object();
14
16
  }
15
17
 
16
- add_repo = async (repository)=>{
17
- let {filter, repo} = repository
18
- if (await this.get_repo(repo.type, repo.options.name))
19
- return;
18
+ fetch = async (payload, server) => {
19
+ let response;
20
+ try {
21
+ response = await post_request({
22
+ options: {
23
+ ...server,
24
+ path: "/oracle",
25
+ },
26
+ data: JSON.stringify(payload),
27
+ });
28
+ } catch (e) {}
20
29
 
21
- switch(repo.type){
22
- case 'fs':
23
- repo = new Fs(repo.options)
24
- break
25
- case 'github':
26
- repo = new Github(repo.options)
27
- break
28
- }
29
- this.repos.push({filter, repo})
30
+ return response;
31
+ };
30
32
 
31
- if(!this.manager) return;
32
- if (this.server_match(this.manager.server, this.server)){
33
- for (let r=0; r< this.remotes.length; r++){
34
- let remote = this.remotes[r]
33
+ call = async (payload) => {
34
+ let { method, args } = payload;
35
35
 
36
- await this.fetch({
37
- method: 'add_repo',
38
- arg: {...repo}
39
- }, remote)
40
- }
41
- } else {
42
- await this.fetch({
43
- method: 'add_repo',
44
- arg: {...repo}
45
- }, this.server)
46
- }
47
- }
36
+ switch (method) {
37
+ case "update_server":
38
+ let { repos, account, server } = args;
48
39
 
49
- call = async(payload)=>{
50
- let {method, arg} = payload, result = {};
51
-
52
- switch(method){
53
- case 'update_servers':
54
- let addr = `.oracle/${arg.account}/.configs`
55
- let servers = await this.get_from_repos(addr)
56
- if (servers){
57
- servers = JSON.parse(servers)
58
- if (servers.find(s=>this.server_match(s, arg.server))){
59
- return
60
- }
61
- servers.push(arg.server)
62
- }else servers = [arg.server]
63
-
64
- await this.set_to_repos(addr, JSON.stringify(servers))
65
- break;
66
- case 'update_accounts':
67
- let a_addr = `.oracle/${arg.address}/.configs`
68
- let accs = await this.get_from_repos(a_addr)
69
- if (accs){
70
- accs = JSON.parse(accs)
71
- if (accs.includes(arg.account)){
72
- return
73
- }
74
- accs.push(arg.account)
75
- }else accs = [arg.account]
76
-
77
- await this.set_to_repos(a_addr, JSON.stringify(accs))
78
- break
79
- case 'get_accounts':
80
- result = this.get_accounts(arg.address, true)
81
- break
82
- case 'get_servers':
83
- result = this.get_servers(arg.account, true)
84
- break
85
- case 'write':
86
- await this.set_to_repos(`${arg.prefix}/${arg.path}`, arg.content)
87
- break;
88
- case 'read':
89
- let res = await this.get_from_repos(`${arg.prefix}/${arg.path}`)
90
- result = {content: res}
40
+ let servers = await this.get_servers(account);
41
+ if (servers.find((serv) => this.server_match(serv, server))) return;
42
+ servers.push(server);
43
+
44
+ let reps = await this.set_to_repos(
45
+ `.oracle/${account}/.config`,
46
+ JSON.stringify(servers),
47
+ { exclude: repos }
48
+ );
49
+ await this.merge_repos([...repos, ...reps], account);
50
+
51
+ return reps;
91
52
  break;
92
- case 'fs':
93
- let repo = await this.get_repo('fs', arg.name)
94
- result = await repo.call(arg.payload)
95
- break
96
- case 'sync':
97
- let repos = this.string_repos()
98
- await this.push_remote(arg.manager_server)
99
- result = {repos}
100
- break
101
- case 'add_repo':
102
- await this.add_repo(arg)
103
- break
104
53
  }
54
+ };
105
55
 
106
- return result
107
- }
56
+ stringify_repos = async () => {
57
+ let rp = [];
58
+ for (let r = 0; r < this.repos.length; r++) {
59
+ let repo = this.repos[r];
108
60
 
109
- push_remote = async(remote)=>{
110
- if (this.remotes.find(rem=>this.server_match(rem, remote))) return;
61
+ rp.push({ repo: await repo.repo.objectify(), filter: repo.filter });
62
+ }
63
+ return rp;
64
+ };
111
65
 
112
- this.remotes.push(remote)
113
- }
66
+ server_match = (s1, s2) => {
67
+ return s1.port === s2.port && s1.hostname === s2.hostname;
68
+ };
114
69
 
115
- string_repos = ()=>{
116
- let repos = new Array();
117
- for(let r=0; r< this.repos.length; r++){
118
- let {filter, repo} = this.repos[r];
119
- repos.push({filter, repo:{type: repo.type, options: repo.objectify()}})
70
+ cloth_repo = async (repo) => {
71
+ switch (repo.type) {
72
+ case "fs":
73
+ repo = new Fs(repo.options || repo);
74
+ break;
75
+ case "github":
76
+ repo = new Github(repo.options || repo);
77
+ break;
120
78
  }
121
- return repos;
122
- }
123
79
 
124
- get_repo = async(type, name) => {
125
- for (let r=0; r< this.repos.length; r++){
126
- let {repo} = this.repos[r]
80
+ return repo;
81
+ };
127
82
 
128
- if (repo.type === type && repo.name === name)
129
- return repo
83
+ repo_object = (repos, account) => {
84
+ repos = [...repos];
85
+ let acc_repos = this.account_repos[account] || [];
86
+ for (let r = 0; r < repos.length; r++) {
87
+ let repo = repos[r];
88
+
89
+ let fnd =
90
+ this.repos.find((r) => r.repo.get_id() === repo) ||
91
+ acc_repos.find((r) => r.repo._id === repo);
92
+ if (fnd) {
93
+ if (fnd.repo.get_id) {
94
+ fnd.repo = fnd.repo.objectify();
95
+ }
96
+ repos[r] = fnd;
97
+ }
130
98
  }
131
- }
132
99
 
133
- fetch = async(payload, server)=>{
134
- let res;
135
- try{
136
- res = await post_request({
137
- options: {
138
- ...server,
139
- path: '/oracle'
140
- },
141
- data: JSON.stringify(payload)
142
- })
143
- }catch(e){
144
- console.log(e)
100
+ return repos.filter((r) => !!r.repo);
101
+ };
102
+
103
+ merge_repos = async (repos, account) => {
104
+ let reps = Array.isArray(account) ? account : this.account_repos[account];
105
+ if (!reps) {
106
+ reps = new Array();
107
+ this.account_repos[account] = reps;
145
108
  }
146
109
 
147
- return res;
148
- }
110
+ for (let r = 0; r < repos.length; r++) {
111
+ let repo = repos[r];
112
+ if (reps.find((rp) => rp.repo._id === repo.repo._id)) continue;
149
113
 
150
- set_to_repos = async(address, content, options)=>{
151
- let {count, args} = options, counted = count != null, res;
152
- for (let r=0; r< this.repos.length; r++){
153
- let repo = this.repos[r]
114
+ repo.repo = await this.cloth_repo(repo.repo);
154
115
 
155
- if (!repo.filter || address.match(repo.filter)){
156
- res = await repo.repo.write_file(address, content, args)
157
- if (!args.just_exists){
158
- res = await repo.repo.objectify()
159
- }
160
- if (counted && !count) break;
161
- count --
162
- }
116
+ reps.push(repo);
163
117
  }
118
+ return reps;
119
+ };
164
120
 
165
- return res;
166
- }
121
+ get_from_repos = async (path, options = {}) => {
122
+ let repos = await this.parse_option_repos(options),
123
+ content;
167
124
 
168
- get_from_repos = async(address, repo_)=>{
169
- let content
170
- for (let r=0; r< this.repos.length; r++){
171
- let repo = this.repos[r]
172
-
173
- if(repo_ && repo.repo.match(repo_)){
174
- content = await repo.repo.read_file(address)
175
- break
176
- } else if (!repo.filter || address.match(repo.filter)){
177
- content = await repo.repo.read_file(address)
178
- if (content) break
125
+ for (let r = 0; r < repos.length; r++) {
126
+ let repo = repos[r];
127
+ if (!repo.filter || path.match(repo.filter)) {
128
+ content = await repo.repo.read_file(path);
129
+ if (content) break;
179
130
  }
180
131
  }
181
- return content
182
- }
132
+ return content;
133
+ };
183
134
 
184
- read = async(path, options={})=>{
185
- let {address, account, prefix} = options;
186
- prefix = prefix || '.storage'
187
-
188
- let has = (await this.get_accounts(address)).includes(account)
189
-
190
- if(!has) return;
191
-
192
- let servers = await this.get_servers(account)
193
- for (let s=0; s< servers.length; s++){
194
- let server = servers[s]
195
- if (this.server_match(server, this.server)){
196
- let res = await this.fetch({
197
- method: 'read',
198
- arg: {path, prefix}
199
- }, server)
200
-
201
- if(res && res.content){
202
- return res.content
203
- }else break;
135
+ parse_option_repos = async (options) => {
136
+ let repos = [];
137
+ if (options.repos) {
138
+ for (let r = 0; r < options.repos[r]; r++) {
139
+ let rep = options.repos[r];
140
+ repos.push({
141
+ filter: rep.filter,
142
+ repo: await this.cloth_repo(rep.repo),
143
+ });
204
144
  }
205
- }
206
- for (let s=0; s< servers.length; s++){
207
- let server = servers[s]
208
- if (this.server_match(server, this.server)){
209
- continue
145
+ } else repos = this.repos;
146
+
147
+ return repos;
148
+ };
149
+
150
+ set_to_repos = async (path, content, options = {}) => {
151
+ let { exclude } = options;
152
+ let repos = await this.parse_option_repos(options),
153
+ content,
154
+ reps = [];
155
+
156
+ for (let r = 0; r < repos.length; r++) {
157
+ let repo = repos[r];
158
+ if (exclude && exclude.find((e) => e._id === repo.repo.get_id())) {
159
+ continue;
210
160
  }
161
+ if (!repo.filter || path.match(repo.filter)) {
162
+ await repo.repo.write_file(path, content);
211
163
 
212
- let res = await this.fetch({
213
- method: 'read',
214
- arg: {path, prefix}
215
- }, server)
216
- if(res && res.content){
217
- return res.content
164
+ reps.push({ filter: repo.filter, repo: repo.repo.objectify() });
218
165
  }
219
166
  }
220
- }
221
-
222
- write = async(path, content, options={})=>{
223
- let {account, address, prefix} = options
224
- prefix = prefix || '.storage'
167
+ return reps;
168
+ };
225
169
 
226
- await this.update_accounts(address, account)
227
- let servers = await this.get_servers(account)
170
+ get_servers = async (account) => {
171
+ let servers = await this.get_from_repos(`.oracle/${account}/.config`);
172
+ if (servers) servers = JSON.parse(servers);
173
+ else servers = [];
228
174
 
229
- for (let s=0; s< servers.length; s++){
230
- let server = servers[s]
175
+ return servers;
176
+ };
231
177
 
232
- await this.fetch({
233
- method: 'write',
234
- arg: {path, content, prefix}
235
- }, server)
178
+ get_config = async (address, cache) => {
179
+ let conf;
180
+ if (cache) {
181
+ conf = this.configs[address];
182
+ if (conf) return conf;
236
183
  }
237
- }
184
+ conf = await this.get_from_repos(`.oracle/${address}/.config`);
185
+ if (!conf) return;
238
186
 
239
- get_servers = async(account, mirroring)=>{
240
- let addr = `.oracle/${account}/.configs`
241
- let servers = await this.get_from_repos(addr)
242
- servers = servers ? JSON.parse(servers) : []
243
-
244
- if(!mirroring && !servers.length && this.mirror){
245
- servers = await this.fetch({
246
- method: 'get_servers',
247
- arg: {account}
248
- }, this.mirror)
249
- if (servers.length){
250
- await this.set_to_repos(addr, JSON.stringify(servers))
251
- await this.update_servers(account, [...servers])
252
- }
253
- }
254
- return servers
255
- }
187
+ conf = JSON.parse(conf);
188
+ this.configs[address] = conf;
256
189
 
257
- update_servers = async(account, servers)=>{
258
- servers = servers || await this.get_servers(account)
259
-
260
- if (servers.find(s=>this.server_match(s, this.server))){
261
- return
262
- }
190
+ return conf;
191
+ };
263
192
 
264
- servers.push(this.server)
265
- for (let s=0; s< servers.length; s++){
266
- let server = servers[s]
193
+ dot_config = ".config";
267
194
 
268
- await this.fetch({
269
- method: 'update_servers',
270
- arg: {account, server: this.server},
271
- }, server)
272
- }
195
+ write = async (path, content, options = {}) => {
196
+ let { account, address } = options;
273
197
 
274
- return servers
275
- }
198
+ if (path.endsWith(this.dot_config)) {
199
+ conf = await this.get_config(
200
+ path.slice(0, (this.dot_config.length + 1) * -1)
201
+ );
276
202
 
277
- get_accounts = async(address, mirroring)=>{
278
- let addr = `.oracle/${address}/.configs`
279
- let accounts = await this.get_from_repos(addr)
280
-
281
- accounts = accounts ? JSON.parse(accounts) : []
282
-
283
- if (!mirroring && !accounts.length && this.mirror){
284
- accounts = await this.fetch({
285
- method: 'get_accounts',
286
- arg: {address}
287
- }, this.mirror)
288
- }
203
+ let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
204
+ let reps = this.merge_repos(conf_repos, await this.filter_repos(path));
289
205
 
290
- return accounts
291
- }
206
+ await this.set_to_repos(
207
+ `.oracle/${path}`,
208
+ JSON.stringify({
209
+ ...conf,
210
+ content,
211
+ repos: reps.map((r) => r.repo._id),
212
+ }),
213
+ { repos: reps }
214
+ );
215
+ } else {
216
+ conf = await this.get_config(address, true);
292
217
 
293
- update_accounts = async(address, account)=>{
294
- let accounts = await this.get_accounts(address)
295
-
296
- if (accounts.includes(account)){
297
- return
298
- }
218
+ let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
219
+ let reps = this.merge_repos(conf_repos, await this.filter_repos(path));
299
220
 
300
- accounts.push(account)
221
+ await this.set_to_repos(`.storage/${path}`, content, { repos: reps });
222
+ }
223
+ };
301
224
 
302
- let servers = await this.get_servers_from_address(address, accounts)
225
+ read = async (path, options = {}) => {
226
+ let { account, address } = options,
227
+ content;
303
228
 
304
- for (let s=0; s< servers.length; s++){
305
- let server = servers[s]
229
+ if (path.endsWith(this.dot_config.length)) {
230
+ content = await this.get_config(
231
+ path.slice(0, (this.dot_config.length + 1) * -1)
232
+ );
233
+ content = content ? content.content : content;
234
+ } else {
235
+ let conf = await this.get_config(address, true);
236
+ let conf_repos = conf ? this.repo_object(conf.repositories, account) : [];
237
+ let reps = this.merge_repos(conf_repos, await this.filter_repos(path));
306
238
 
307
- await this.fetch({
308
- method: 'update_accounts',
309
- arg: {address, account},
310
- }, server)
239
+ content = await this.get_from_repos(`.storage/${path}`, { repos: reps });
311
240
  }
312
241
 
313
- return accounts
314
- }
242
+ return content;
243
+ };
315
244
 
316
- get_servers_from_address = async(address, accounts)=>{
317
- accounts = accounts || await this.get_accounts(address)
318
-
319
- let servers = []
320
- for (let a=0; a< accounts.length; a++){
321
- let acc = accounts[a]
322
- let servers_ = await this.get_servers(acc)
323
- servers_.map(serv=>{
324
- if(!servers.find(s=>this.server_match(s, serv)))
325
- servers.push(serv)
326
- })
245
+ update_server = async (account) => {
246
+ let servers = await this.get_servers(account);
247
+ if (servers.find((serv) => this.server_match(serv, this.server))) {
248
+ return;
327
249
  }
328
250
 
329
- return servers
330
- }
251
+ servers.push(this.server);
252
+ let repos = await this.set_to_repos(
253
+ `.oracle/${account}/.config`,
254
+ JSON.stringify(servers)
255
+ );
256
+ this.account_repos[account] = repos;
331
257
 
332
- server_match = (s1, s2)=>{
333
- return s1.hostname === s2.hostname && s1.port === s2.port
334
- }
258
+ for (let s = 0; s < servers.length - 1; s++) {
259
+ let server = servers[s];
260
+
261
+ let reps = await this.fetch(
262
+ {
263
+ method: "update_server",
264
+ args: { account, repos, server: this.server },
265
+ },
266
+ server
267
+ );
335
268
 
336
- set_manager = async(manager)=>{
337
- console.log(`Manager:${manager.name} Oracle running at ${this.server.hostname}:${this.server.port}`)
269
+ await this.merge_repos(reps, account);
270
+ }
271
+ };
338
272
 
339
- if (!this.server_match(this.server, manager.server)){
340
- let response = await this.fetch({
341
- method: 'sync',
342
- arg: {manager_server: manager.server}
343
- }, this.server)
273
+ filter_repos = async (address, repos) => {
274
+ repos = repos || this.repos;
344
275
 
345
- for (let r=0; r< response.repos.length; r++){
346
- let repository = response.repos[r]
276
+ let filtered_repos = new Array();
277
+ for (let r = 0; r < repos.length; r++) {
278
+ let repo = repos[r];
347
279
 
348
- await this.add_repo(repository)
280
+ if (!repo.filter || address.match(repo.filter)) {
281
+ filtered_repos.push(repo);
349
282
  }
350
283
  }
351
- }
352
284
 
353
- sync = async () => {
354
-
285
+ return filtered_repos;
355
286
  };
287
+
288
+ add_repo = async (repository) => {
289
+ let { filter, repo } = repository;
290
+ if (await this.get_repo(repo.type, repo.options.name)) return;
291
+
292
+ repo = await this.cloth_repo(repo);
293
+ this.repos.push({ filter, repo });
294
+ };
295
+
296
+ sync = async () => {};
356
297
  }
357
298
 
358
299
  export default Oracle;
@@ -1,26 +1,43 @@
1
- class Repository{
2
- constructor(type){
1
+ class Repository {
2
+ constructor(type) {
3
3
  this.type = type;
4
4
  }
5
5
 
6
- objectify = ()=>{
7
- let obj = {}
8
- if (this.type === 'fs'){
6
+ objectify = () => {
7
+ let obj = { type: this.type };
8
+ if (this.type === "fs") {
9
9
  obj.name = this.name;
10
- obj.remote = this.remote
11
- obj.__dirname = this.__dirname
12
- }else if (this.type === 'github'){
13
- obj.key = this.auth_token
14
- obj.username = this.owner
10
+ obj.remote = this.remote;
11
+ obj.__dirname = this.__dirname;
12
+ obj._id = `${this.__dirname}/${this.name}`;
13
+ } else if (this.type === "github") {
14
+ obj.key = this.auth_token;
15
+ obj.username = this.owner;
15
16
  obj.repo = this.repo;
17
+ obj._id = `${this.owner}/${this.repo}`;
16
18
  }
17
19
 
18
- return obj
19
- }
20
+ return obj;
21
+ };
20
22
 
21
- match = async()=>{
22
- return false
23
- }
23
+ get_id = () => {
24
+ if (this._id) return this._id;
25
+
26
+ switch (this.type) {
27
+ case "fs":
28
+ this._id = `${this.__dirname}/${this.name}`;
29
+ break;
30
+ case "github":
31
+ this._id = `${this.owner}/${this.repo}`;
32
+ break;
33
+ }
34
+
35
+ return this._id;
36
+ };
37
+
38
+ match = async () => {
39
+ return false;
40
+ };
24
41
  }
25
42
 
26
- export default Repository;
43
+ export default Repository;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.2.18",
3
+ "version": "1.2.20",
4
4
  "description": "A distributed computing environment",
5
5
  "main": "Index.js",
6
6
  "scripts": {
@@ -1,116 +1,133 @@
1
- import { set_page } from '../html/index.js';
2
- import { generate_random_string } from 'generalised-datastore/utils/functions.js';
1
+ import { set_page } from "../html/index.js";
2
+ import { generate_random_string } from "generalised-datastore/utils/functions.js";
3
3
 
4
- let PORT = Number(process.env.PORT) || 1408, account_uids = {};
4
+ let PORT = Number(process.env.PORT) || 1408,
5
+ account_uids = {};
5
6
 
6
7
  export const handle_routes = async (req, res, manager, app) => {
7
8
  try {
8
9
  // CORS
9
- res.setHeader('Access-Control-Allow-Origin', '*');
10
- res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
11
- res.setHeader('Access-Control-Allow-Headers', '*');
12
-
13
- if (req.method === 'OPTIONS') {
10
+ res.setHeader("Access-Control-Allow-Origin", "*");
11
+ res.setHeader(
12
+ "Access-Control-Allow-Methods",
13
+ "GET, POST, PUT, DELETE, OPTIONS"
14
+ );
15
+ res.setHeader("Access-Control-Allow-Headers", "*");
16
+
17
+ if (req.method === "OPTIONS") {
14
18
  res.writeHead(204);
15
19
  return res.end();
16
20
  }
17
21
 
18
22
  // GET pages
19
- if (req.method === 'GET') {
20
- if (req.url === '/') {
21
- res.writeHead(200, { 'Content-Type': 'text/html' });
23
+ if (req.method === "GET") {
24
+ if (req.url === "/") {
25
+ res.writeHead(200, { "Content-Type": "text/html" });
22
26
  return res.end(set_page(req.url, manager));
23
27
  }
24
28
 
25
- if (['/create_account', '/load', '/run', '/parse', '/oracle'].includes(req.url)) {
26
- res.writeHead(200, { 'Content-Type': 'text/html' });
29
+ if (
30
+ ["/create_account", "/load", "/run", "/parse", "/oracle"].includes(
31
+ req.url
32
+ )
33
+ ) {
34
+ res.writeHead(200, { "Content-Type": "text/html" });
27
35
  return res.end(set_page(req.url.slice(1), manager));
28
36
  }
29
37
 
30
38
  // hand over untouched
31
- return app ? app(req, res) : res.writeHead(404).end('Not Found');
39
+ return app ? app(req, res) : res.writeHead(404).end("Not Found");
32
40
  }
33
41
 
34
42
  // Only custom POST endpoints handled here
35
- if (['/create_account', '/load', '/run', '/parse', '/oracle'].includes(req.url)) {
36
- let body = '';
37
- req.on('data', chunk => { body += chunk; });
38
- await new Promise(resolve => req.on('end', resolve));
43
+ if (
44
+ ["/create_account", "/load", "/run", "/parse", "/oracle"].includes(
45
+ req.url
46
+ )
47
+ ) {
48
+ let body = "";
49
+ req.on("data", (chunk) => {
50
+ body += chunk;
51
+ });
52
+ await new Promise((resolve) => req.on("end", resolve));
39
53
 
40
54
  if (!body) {
41
- res.writeHead(400, { 'Content-Type': 'application/json' });
42
- return res.end(JSON.stringify({ error: 'Request body cannot be empty' }));
55
+ res.writeHead(400, { "Content-Type": "application/json" });
56
+ return res.end(
57
+ JSON.stringify({ error: "Request body cannot be empty" })
58
+ );
43
59
  }
44
60
 
45
61
  const data = JSON.parse(body);
46
62
  let result;
47
63
 
48
64
  switch (req.url) {
49
- case '/create_account': {
65
+ case "/create_account": {
50
66
  let acc = await manager.get_account(data.name);
51
67
  if (acc) {
52
- if (!await acc.verify(data.password)) {
53
- result = { message: 'Incorrect password.' };
68
+ if (!(await acc.verify(data.password))) {
69
+ result = { message: "Incorrect password." };
54
70
  } else {
55
- const uid = generate_random_string(16, 'alnum');
71
+ const uid = generate_random_string(16, "alnum");
56
72
  account_uids[uid] = data.name;
57
- result = { uid, message: 'Account signed-in successfully.' };
73
+ result = { uid, message: "Account signed-in successfully." };
58
74
  }
59
75
  } else {
60
- let newAcc = await manager.add_account(data.name, { password: data.password });
61
- if (newAcc) {
62
- const uid = generate_random_string(16, 'alnum');
76
+ let new_acc = await manager.add_account(data.name, {
77
+ password: data.password,
78
+ });
79
+ if (new_acc) {
80
+ const uid = generate_random_string(16, "alnum");
63
81
  account_uids[uid] = data.name;
64
- result = { uid, message: 'Account created successfully.' };
82
+ result = { uid, message: "Account created successfully." };
65
83
  } else {
66
- result = { message: 'Account creation failed.' };
84
+ result = { message: "Account creation failed." };
67
85
  }
68
86
  }
69
87
  break;
70
88
  }
71
89
 
72
- case '/oracle':
90
+ case "/oracle":
73
91
  result = await manager.oracle.call(data);
74
92
  break;
75
93
 
76
- case '/load':
77
- case '/run':
78
- case '/parse': {
94
+ case "/load":
95
+ case "/run":
96
+ case "/parse": {
79
97
  const accountName = account_uids[data.account];
80
98
  if (!accountName) {
81
99
  result = { message: `Account ${data.account} not found` };
82
100
  } else {
83
101
  const accountObj = await manager.get_account(accountName);
84
- if (req.url === '/load') result = await accountObj.load(data);
85
- if (req.url === '/run') result = await accountObj.run(data);
86
- if (req.url === '/parse') result = await accountObj.parse(data);
102
+ if (req.url === "/load") result = await accountObj.load(data);
103
+ if (req.url === "/run") result = await accountObj.run(data);
104
+ if (req.url === "/parse") result = await accountObj.parse(data);
87
105
  }
88
106
  break;
89
107
  }
90
108
  }
91
109
 
92
- res.writeHead(200, { 'Content-Type': 'application/json' });
110
+ res.writeHead(200, { "Content-Type": "application/json" });
93
111
  return res.end(JSON.stringify(result));
94
112
  }
95
113
 
96
114
  // Everything else → forward to express app
97
- return app ? app(req, res) : res.writeHead(404).end('Not Found');
98
-
115
+ return app ? app(req, res) : res.writeHead(404).end("Not Found");
99
116
  } catch (err) {
100
- console.error('Handler error:', err);
101
- res.writeHead(500, { 'Content-Type': 'application/json' });
117
+ console.error("Handler error:", err);
118
+ res.writeHead(500, { "Content-Type": "application/json" });
102
119
  return res.end(JSON.stringify({ error: err.message }));
103
120
  }
104
121
  };
105
122
 
106
-
107
123
  const create_server = (settings) => {
108
124
  settings = settings || {};
109
125
  let { port, manager } = settings;
110
126
 
111
127
  port = port || PORT;
112
-
113
- let handler = async (req, res) => await handle_routes(req, res, manager, manager.options.app);
128
+
129
+ let handler = async (req, res) =>
130
+ await handle_routes(req, res, manager, manager.options.app);
114
131
 
115
132
  return handler;
116
133
  };