godprotocol 1.2.9 → 1.2.10

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