godprotocol 1.2.19 → 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.
- package/Objects/Manager.js +362 -296
- package/Objects/Oracle.js +246 -284
- package/Objects/Repository.js +33 -16
- package/package.json +1 -4
- package/repos/fs.js +83 -75
- package/utils/create_server.js +62 -45
package/repos/fs.js
CHANGED
|
@@ -1,114 +1,122 @@
|
|
|
1
|
-
import fs from
|
|
2
|
-
import { copy_object } from
|
|
3
|
-
import Repository from
|
|
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 =
|
|
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
|
|
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
|
|
31
|
-
result = await this.write(payload.path, payload.data)
|
|
32
|
-
break
|
|
33
|
-
case
|
|
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
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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:
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
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;
|
package/utils/create_server.js
CHANGED
|
@@ -1,116 +1,133 @@
|
|
|
1
|
-
import { set_page } from
|
|
2
|
-
import { generate_random_string } from
|
|
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,
|
|
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(
|
|
10
|
-
res.setHeader(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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 ===
|
|
20
|
-
if (req.url ===
|
|
21
|
-
res.writeHead(200, {
|
|
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 (
|
|
26
|
-
|
|
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(
|
|
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 (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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, {
|
|
42
|
-
return res.end(
|
|
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
|
|
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:
|
|
68
|
+
if (!(await acc.verify(data.password))) {
|
|
69
|
+
result = { message: "Incorrect password." };
|
|
54
70
|
} else {
|
|
55
|
-
const uid = generate_random_string(16,
|
|
71
|
+
const uid = generate_random_string(16, "alnum");
|
|
56
72
|
account_uids[uid] = data.name;
|
|
57
|
-
result = { uid, message:
|
|
73
|
+
result = { uid, message: "Account signed-in successfully." };
|
|
58
74
|
}
|
|
59
75
|
} else {
|
|
60
|
-
let
|
|
61
|
-
|
|
62
|
-
|
|
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:
|
|
82
|
+
result = { uid, message: "Account created successfully." };
|
|
65
83
|
} else {
|
|
66
|
-
result = { message:
|
|
84
|
+
result = { message: "Account creation failed." };
|
|
67
85
|
}
|
|
68
86
|
}
|
|
69
87
|
break;
|
|
70
88
|
}
|
|
71
89
|
|
|
72
|
-
case
|
|
90
|
+
case "/oracle":
|
|
73
91
|
result = await manager.oracle.call(data);
|
|
74
92
|
break;
|
|
75
93
|
|
|
76
|
-
case
|
|
77
|
-
case
|
|
78
|
-
case
|
|
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 ===
|
|
85
|
-
if (req.url ===
|
|
86
|
-
if (req.url ===
|
|
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, {
|
|
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(
|
|
98
|
-
|
|
115
|
+
return app ? app(req, res) : res.writeHead(404).end("Not Found");
|
|
99
116
|
} catch (err) {
|
|
100
|
-
console.error(
|
|
101
|
-
res.writeHead(500, {
|
|
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) =>
|
|
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
|
};
|