godprotocol 1.2.15 → 1.2.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.2.15",
3
+ "version": "1.2.16",
4
4
  "description": "A distributed computing environment",
5
5
  "main": "Index.js",
6
6
  "scripts": {
@@ -3,11 +3,9 @@ import { generate_random_string } from 'generalised-datastore/utils/functions.js
3
3
 
4
4
  let PORT = Number(process.env.PORT) || 1408, account_uids = {};
5
5
 
6
- export const handle_routes = async (req, res, manager, app) => {
7
- let body = '';
8
-
6
+ export const handle_routes = async (req, res, manager, app) => {
9
7
  try {
10
- // Handle CORS preflight
8
+ // CORS
11
9
  res.setHeader('Access-Control-Allow-Origin', '*');
12
10
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
13
11
  res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
@@ -17,7 +15,7 @@ export const handle_routes = async (req, res, manager, app) => {
17
15
  return res.end();
18
16
  }
19
17
 
20
- // GET requests for pages
18
+ // GET pages
21
19
  if (req.method === 'GET') {
22
20
  if (req.url === '/') {
23
21
  res.writeHead(200, { 'Content-Type': 'text/html' });
@@ -29,70 +27,74 @@ export const handle_routes = async (req, res, manager, app) => {
29
27
  return res.end(set_page(req.url.slice(1), manager));
30
28
  }
31
29
 
32
- return app ? app(req,res) : res.writeHead(404).end('Not Found');
30
+ // hand over untouched
31
+ return app ? app(req, res) : res.writeHead(404).end('Not Found');
33
32
  }
34
33
 
35
- // Accumulate POST body
36
- req.on('data', chunk => { body += chunk; });
37
- await new Promise(resolve => req.on('end', resolve));
38
-
39
- let data = body ? JSON.parse(body) : null;
34
+ // 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));
40
39
 
41
- if (!data) {
42
- res.writeHead(400, { 'Content-Type': 'application/json' });
43
- return res.end(JSON.stringify({ error: 'Request body cannot be empty' }));
44
- }
40
+ if (!body) {
41
+ res.writeHead(400, { 'Content-Type': 'application/json' });
42
+ return res.end(JSON.stringify({ error: 'Request body cannot be empty' }));
43
+ }
45
44
 
46
- // POST route handling
47
- let result;
48
- switch (req.url) {
49
- case '/create_account':
50
- let acc = await manager.get_account(data.name);
51
- if (acc) {
52
- if (!await acc.verify(data.password)) {
53
- result = { message: 'Incorrect password.' };
54
- } else {
55
- const uid = generate_random_string(16, 'alnum');
56
- account_uids[uid] = data.name;
57
- result = { uid, message: 'Account signed-in successfully.' };
58
- }
59
- } else {
60
- let newAcc = await manager.add_account(data.name, { password: data.password });
61
- if (newAcc) {
62
- const uid = generate_random_string(16, 'alnum');
63
- account_uids[uid] = data.name;
64
- result = { uid, message: 'Account created successfully.' };
45
+ const data = JSON.parse(body);
46
+ let result;
47
+
48
+ switch (req.url) {
49
+ case '/create_account': {
50
+ let acc = await manager.get_account(data.name);
51
+ if (acc) {
52
+ if (!await acc.verify(data.password)) {
53
+ result = { message: 'Incorrect password.' };
54
+ } else {
55
+ const uid = generate_random_string(16, 'alnum');
56
+ account_uids[uid] = data.name;
57
+ result = { uid, message: 'Account signed-in successfully.' };
58
+ }
65
59
  } else {
66
- result = { message: 'Account creation failed.' };
60
+ let newAcc = await manager.add_account(data.name, { password: data.password });
61
+ if (newAcc) {
62
+ const uid = generate_random_string(16, 'alnum');
63
+ account_uids[uid] = data.name;
64
+ result = { uid, message: 'Account created successfully.' };
65
+ } else {
66
+ result = { message: 'Account creation failed.' };
67
+ }
67
68
  }
69
+ break;
68
70
  }
69
- break;
70
-
71
- case '/oracle':
72
- result = await manager.oracle.call(data);
73
- break;
74
- default:
75
- let account_name = account_uids[data.account], account_obj;
76
- if (!account_name){
77
- if (['load', 'run', 'parse'].includes(req.url.slice(1))){
78
- return res.end(JSON.stringify({ message: `Account ${data.account} not found` }));
71
+
72
+ case '/oracle':
73
+ result = await manager.oracle.call(data);
74
+ break;
75
+
76
+ case '/load':
77
+ case '/run':
78
+ case '/parse': {
79
+ const accountName = account_uids[data.account];
80
+ if (!accountName) {
81
+ result = { message: `Account ${data.account} not found` };
82
+ } else {
83
+ 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);
79
87
  }
80
- }else account_obj = await manager.get_account(account_name);
81
-
82
- if (req.url === '/load'){
83
- result = await account_obj.load(data);
84
- }else if (req.url === '/run'){
85
- result = await account_obj.run(data);
86
- }else if (req.url === '/parse'){
87
- result = await account_obj.parse(data);
88
- }else {
89
- return app? app(req,res): res.writeHead(404).end('Not Found');
88
+ break;
90
89
  }
91
- break;
90
+ }
91
+
92
+ res.writeHead(200, { 'Content-Type': 'application/json' });
93
+ return res.end(JSON.stringify(result));
92
94
  }
93
95
 
94
- res.writeHead(200, { 'Content-Type': 'application/json' });
95
- return res.end(JSON.stringify(result));
96
+ // Everything else forward to express app
97
+ return app ? app(req, res) : res.writeHead(404).end('Not Found');
96
98
 
97
99
  } catch (err) {
98
100
  console.error('Handler error:', err);
@@ -108,10 +110,10 @@ const create_server = (settings) => {
108
110
 
109
111
  port = port || PORT;
110
112
 
111
- let handler = async (req, res) => await handle_routes(req, res, manager, manager.options.app)
113
+ let handler = async (req, res) => await handle_routes(req, res, manager, manager.options.app);
112
114
 
113
115
  return handler;
114
116
  };
115
117
 
116
118
  export default create_server;
117
- export { PORT };
119
+ export { PORT };