godprotocol 1.2.13 → 1.2.15
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 +8 -1
- package/Objects/Oracle.js +19 -6
- package/Objects/Repository.js +10 -1
- package/Objects/Utils.js +28 -0
- package/package.json +1 -1
- package/repos/github.js +43 -19
- package/utils/create_server.js +9 -7
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
|
|
|
@@ -339,6 +339,13 @@ class Manager {
|
|
|
339
339
|
this.served = true
|
|
340
340
|
this.oracle = this.options.oracle;
|
|
341
341
|
this.oracle.set_manager(this)
|
|
342
|
+
.then(async()=>{
|
|
343
|
+
if (this.options.init_account){
|
|
344
|
+
await this.add_account(this.options.init_account.name, {password: this.options.init_account.password, init: true})
|
|
345
|
+
|
|
346
|
+
cb && cb()
|
|
347
|
+
}
|
|
348
|
+
})
|
|
342
349
|
}
|
|
343
350
|
}
|
|
344
351
|
}
|
package/Objects/Oracle.js
CHANGED
|
@@ -147,25 +147,38 @@ class Oracle extends Handlers{
|
|
|
147
147
|
return res;
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
set_to_repos = async(address, content)=>{
|
|
150
|
+
set_to_repos = async(address, content, options)=>{
|
|
151
|
+
let {count, args} = options, counted = count != null, res;
|
|
151
152
|
for (let r=0; r< this.repos.length; r++){
|
|
152
153
|
let repo = this.repos[r]
|
|
153
154
|
|
|
154
155
|
if (!repo.filter || address.match(repo.filter)){
|
|
155
|
-
await repo.repo.write_file(address, content)
|
|
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 --
|
|
156
162
|
}
|
|
157
163
|
}
|
|
164
|
+
|
|
165
|
+
return res;
|
|
158
166
|
}
|
|
159
167
|
|
|
160
|
-
get_from_repos = async(address)=>{
|
|
168
|
+
get_from_repos = async(address, repo_)=>{
|
|
169
|
+
let content
|
|
161
170
|
for (let r=0; r< this.repos.length; r++){
|
|
162
171
|
let repo = this.repos[r]
|
|
163
172
|
|
|
164
|
-
if
|
|
165
|
-
|
|
166
|
-
|
|
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
|
|
167
179
|
}
|
|
168
180
|
}
|
|
181
|
+
return content
|
|
169
182
|
}
|
|
170
183
|
|
|
171
184
|
read = async(path, options={})=>{
|
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 = ()=>{
|
|
@@ -8,10 +9,18 @@ class Repository{
|
|
|
8
9
|
obj.name = this.name;
|
|
9
10
|
obj.remote = this.remote
|
|
10
11
|
obj.__dirname = this.__dirname
|
|
12
|
+
}else if (this.type === 'github'){
|
|
13
|
+
obj.key = this.auth_token
|
|
14
|
+
obj.username = this.owner
|
|
15
|
+
obj.repo = this.repo;
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
return obj
|
|
14
19
|
}
|
|
20
|
+
|
|
21
|
+
match = async()=>{
|
|
22
|
+
return false
|
|
23
|
+
}
|
|
15
24
|
}
|
|
16
25
|
|
|
17
26
|
export default Repository;
|
package/Objects/Utils.js
CHANGED
|
@@ -10,6 +10,34 @@ class Utils{
|
|
|
10
10
|
|
|
11
11
|
await this.load({address: address.slice(this.physical_address.length+1), sequence: res, compiler: true, from_reload: true})
|
|
12
12
|
}
|
|
13
|
+
|
|
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
|
+
|
|
18
|
+
return content && JSON.parse(content)
|
|
19
|
+
}
|
|
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
|
+
|
|
28
|
+
read_file = async(address) =>{
|
|
29
|
+
let content = await this.manager.oracle.read(address, {account: this.physical_address})
|
|
30
|
+
|
|
31
|
+
return content;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
write_file = async(address, content)=>{
|
|
35
|
+
let res = await this.manager.oracle.write(address, JSON.stringify(content), {account: this.physical_address})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
read = async(address, query)=>{
|
|
39
|
+
|
|
40
|
+
}
|
|
13
41
|
}
|
|
14
42
|
|
|
15
43
|
|
package/package.json
CHANGED
package/repos/github.js
CHANGED
|
@@ -17,45 +17,69 @@ 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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
const
|
|
30
|
-
|
|
25
|
+
try{
|
|
26
|
+
exists = await this.file_exists(file_path);
|
|
27
|
+
|
|
28
|
+
if (exists) {
|
|
29
|
+
const res = await fetch(url, { headers: this.get_headers() });
|
|
30
|
+
if (res.ok) {
|
|
31
|
+
const data = await res.json();
|
|
32
|
+
sha = data.sha;
|
|
33
|
+
}
|
|
31
34
|
}
|
|
35
|
+
if (options.just_exists) return exists
|
|
36
|
+
}catch(e){
|
|
37
|
+
console.log(e.message)
|
|
38
|
+
return
|
|
32
39
|
}
|
|
33
40
|
|
|
34
41
|
// Build payload
|
|
35
|
-
|
|
42
|
+
let body;
|
|
43
|
+
try{
|
|
44
|
+
body = {
|
|
36
45
|
message: exists ? 'Update file' : 'Create file',
|
|
37
46
|
content: Buffer.from(content).toString('base64'),
|
|
38
47
|
...(sha && { sha }),
|
|
39
48
|
};
|
|
49
|
+
}catch(e){
|
|
40
50
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
try{
|
|
54
|
+
const response = await fetch(url, {
|
|
55
|
+
method: 'PUT',
|
|
56
|
+
headers: this.get_headers(),
|
|
57
|
+
body: JSON.stringify(body),
|
|
58
|
+
});
|
|
46
59
|
|
|
47
|
-
|
|
60
|
+
return response.json();
|
|
61
|
+
}catch(e){
|
|
62
|
+
console.log(e.message)
|
|
63
|
+
}
|
|
64
|
+
|
|
48
65
|
};
|
|
49
66
|
|
|
50
67
|
// Read a file
|
|
51
68
|
read_file = async (file_path) => {
|
|
52
69
|
const url = `${this.base_url}/repos/${this.owner}/${this.repo}/contents/${file_path}`;
|
|
53
70
|
|
|
54
|
-
|
|
55
|
-
|
|
71
|
+
let response;
|
|
72
|
+
try{
|
|
73
|
+
response = await fetch(url, { headers: this.get_headers() })
|
|
74
|
+
|
|
75
|
+
if (!response.ok) return null;
|
|
56
76
|
|
|
57
|
-
|
|
58
|
-
|
|
77
|
+
const fileData = await response.json();
|
|
78
|
+
return Buffer.from(fileData.content, 'base64').toString('utf-8');
|
|
79
|
+
}catch(e){
|
|
80
|
+
console.log(e.message)
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
59
83
|
};
|
|
60
84
|
|
|
61
85
|
// Check if file exists
|
package/utils/create_server.js
CHANGED
|
@@ -72,17 +72,19 @@ export const handle_routes = async (req, res, manager, app) => {
|
|
|
72
72
|
result = await manager.oracle.call(data);
|
|
73
73
|
break;
|
|
74
74
|
default:
|
|
75
|
-
let
|
|
76
|
-
if (!
|
|
77
|
-
|
|
78
|
-
|
|
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` }));
|
|
79
|
+
}
|
|
80
|
+
}else account_obj = await manager.get_account(account_name);
|
|
79
81
|
|
|
80
82
|
if (req.url === '/load'){
|
|
81
|
-
result = await
|
|
83
|
+
result = await account_obj.load(data);
|
|
82
84
|
}else if (req.url === '/run'){
|
|
83
|
-
result = await
|
|
85
|
+
result = await account_obj.run(data);
|
|
84
86
|
}else if (req.url === '/parse'){
|
|
85
|
-
result = await
|
|
87
|
+
result = await account_obj.parse(data);
|
|
86
88
|
}else {
|
|
87
89
|
return app? app(req,res): res.writeHead(404).end('Not Found');
|
|
88
90
|
}
|