godprotocol 1.2.14 → 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/Objects/Manager.js +3 -1
- package/Objects/Oracle.js +4 -2
- package/Objects/Repository.js +6 -1
- package/Objects/Utils.js +10 -3
- package/package.json +1 -1
- package/repos/github.js +28 -17
- package/utils/create_server.js +62 -60
package/Objects/Manager.js
CHANGED
|
@@ -331,7 +331,7 @@ class Manager {
|
|
|
331
331
|
else this.options.server_domain = this.options.server.hostname
|
|
332
332
|
let handle = create_server({manager: this, ...this.options.server, app: this.options.app })
|
|
333
333
|
|
|
334
|
-
this.handler = (req, res)=>{
|
|
334
|
+
this.handler = (req, res, cb)=>{
|
|
335
335
|
|
|
336
336
|
handle(req,res)
|
|
337
337
|
|
|
@@ -342,6 +342,8 @@ class Manager {
|
|
|
342
342
|
.then(async()=>{
|
|
343
343
|
if (this.options.init_account){
|
|
344
344
|
await this.add_account(this.options.init_account.name, {password: this.options.init_account.password, init: true})
|
|
345
|
+
|
|
346
|
+
cb && cb()
|
|
345
347
|
}
|
|
346
348
|
})
|
|
347
349
|
}
|
package/Objects/Oracle.js
CHANGED
|
@@ -153,8 +153,10 @@ class Oracle extends Handlers{
|
|
|
153
153
|
let repo = this.repos[r]
|
|
154
154
|
|
|
155
155
|
if (!repo.filter || address.match(repo.filter)){
|
|
156
|
-
await repo.repo.write_file(address, content, args)
|
|
157
|
-
|
|
156
|
+
res = await repo.repo.write_file(address, content, args)
|
|
157
|
+
if (!args.just_exists){
|
|
158
|
+
res = await repo.repo.objectify()
|
|
159
|
+
}
|
|
158
160
|
if (counted && !count) break;
|
|
159
161
|
count --
|
|
160
162
|
}
|
package/Objects/Repository.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
class Repository{
|
|
2
|
-
constructor(){
|
|
2
|
+
constructor(type){
|
|
3
|
+
this.type = type;
|
|
3
4
|
}
|
|
4
5
|
|
|
5
6
|
objectify = ()=>{
|
|
@@ -16,6 +17,10 @@ class Repository{
|
|
|
16
17
|
|
|
17
18
|
return obj
|
|
18
19
|
}
|
|
20
|
+
|
|
21
|
+
match = async()=>{
|
|
22
|
+
return false
|
|
23
|
+
}
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
export default Repository;
|
package/Objects/Utils.js
CHANGED
|
@@ -11,13 +11,20 @@ class Utils{
|
|
|
11
11
|
await this.load({address: address.slice(this.physical_address.length+1), sequence: res, compiler: true, from_reload: true})
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
get_from_repo = async(address) =>{
|
|
15
|
-
address =
|
|
16
|
-
let content = await this.manager.oracle.
|
|
14
|
+
get_from_repo = async(address, options) =>{
|
|
15
|
+
address = `./storage/${this.physical_address}/${address}/__literal__`
|
|
16
|
+
let content = await this.manager.oracle.get_from_repos(address, {count: 1, args: options})
|
|
17
17
|
|
|
18
18
|
return content && JSON.parse(content)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
set_to_repo = async(address, content, options) =>{
|
|
22
|
+
address = `./storage/${this.physical_address}/${address}/__literal__`
|
|
23
|
+
let response = await this.manager.oracle.set_to_repos(address, JSON.stringify(content), {count: 1, args: options})
|
|
24
|
+
|
|
25
|
+
return response
|
|
26
|
+
}
|
|
27
|
+
|
|
21
28
|
read_file = async(address) =>{
|
|
22
29
|
let content = await this.manager.oracle.read(address, {account: this.physical_address})
|
|
23
30
|
|
package/package.json
CHANGED
package/repos/github.js
CHANGED
|
@@ -17,13 +17,14 @@ class Github extends Repository{
|
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
// Write (create or update) a file
|
|
20
|
-
write_file = async (file_path, content) => {
|
|
20
|
+
write_file = async (file_path, content, options={}) => {
|
|
21
21
|
const url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
|
|
22
|
-
let sha = null;
|
|
22
|
+
let sha = null, exists;
|
|
23
23
|
|
|
24
24
|
// Check if file exists, get its SHA
|
|
25
25
|
try{
|
|
26
|
-
|
|
26
|
+
exists = await this.file_exists(file_path);
|
|
27
|
+
|
|
27
28
|
if (exists) {
|
|
28
29
|
const res = await fetch(url, { headers: this.get_headers() });
|
|
29
30
|
if (res.ok) {
|
|
@@ -31,26 +32,36 @@ class Github extends Repository{
|
|
|
31
32
|
sha = data.sha;
|
|
32
33
|
}
|
|
33
34
|
}
|
|
35
|
+
if (options.just_exists) return exists
|
|
34
36
|
}catch(e){
|
|
35
37
|
console.log(e.message)
|
|
36
38
|
return
|
|
37
39
|
}
|
|
38
|
-
|
|
39
40
|
|
|
40
41
|
// Build payload
|
|
41
|
-
|
|
42
|
+
let body;
|
|
43
|
+
try{
|
|
44
|
+
body = {
|
|
42
45
|
message: exists ? 'Update file' : 'Create file',
|
|
43
46
|
content: Buffer.from(content).toString('base64'),
|
|
44
47
|
...(sha && { sha }),
|
|
45
48
|
};
|
|
49
|
+
}catch(e){
|
|
46
50
|
|
|
47
|
-
|
|
48
|
-
method: 'PUT',
|
|
49
|
-
headers: this.get_headers(),
|
|
50
|
-
body: JSON.stringify(body),
|
|
51
|
-
});
|
|
51
|
+
}
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
try{
|
|
54
|
+
const response = await fetch(url, {
|
|
55
|
+
method: 'PUT',
|
|
56
|
+
headers: this.get_headers(),
|
|
57
|
+
body: JSON.stringify(body),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return response.json();
|
|
61
|
+
}catch(e){
|
|
62
|
+
console.log(e.message)
|
|
63
|
+
}
|
|
64
|
+
|
|
54
65
|
};
|
|
55
66
|
|
|
56
67
|
// Read a file
|
|
@@ -60,15 +71,15 @@ class Github extends Repository{
|
|
|
60
71
|
let response;
|
|
61
72
|
try{
|
|
62
73
|
response = await fetch(url, { headers: this.get_headers() })
|
|
74
|
+
|
|
75
|
+
if (!response.ok) return null;
|
|
76
|
+
|
|
77
|
+
const fileData = await response.json();
|
|
78
|
+
return Buffer.from(fileData.content, 'base64').toString('utf-8');
|
|
63
79
|
}catch(e){
|
|
64
|
-
console.log(e)
|
|
80
|
+
console.log(e.message)
|
|
65
81
|
return;
|
|
66
82
|
}
|
|
67
|
-
;
|
|
68
|
-
if (!response.ok) return null;
|
|
69
|
-
|
|
70
|
-
const fileData = await response.json();
|
|
71
|
-
return Buffer.from(fileData.content, 'base64').toString('utf-8');
|
|
72
83
|
};
|
|
73
84
|
|
|
74
85
|
// Check if file exists
|
package/utils/create_server.js
CHANGED
|
@@ -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 =
|
|
7
|
-
let body = '';
|
|
8
|
-
|
|
6
|
+
export const handle_routes = async (req, res, manager, app) => {
|
|
9
7
|
try {
|
|
10
|
-
//
|
|
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
|
|
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
|
-
|
|
30
|
+
// hand over untouched
|
|
31
|
+
return app ? app(req, res) : res.writeHead(404).end('Not Found');
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
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
|
-
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
93
|
+
return res.end(JSON.stringify(result));
|
|
92
94
|
}
|
|
93
95
|
|
|
94
|
-
|
|
95
|
-
return res.
|
|
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 };
|