godprotocol 2.2.41 → 2.2.45
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/Godprotocol.js +2 -0
- package/Index.js +1 -0
- package/package.json +3 -2
- package/server/Routable/Header.js +14 -9
- package/server/Routable/Route_table.js +8 -1
- package/server/Routable/Services.js +64 -33
package/Godprotocol.js
CHANGED
|
@@ -5,6 +5,7 @@ class GodProtocol {
|
|
|
5
5
|
constructor(config) {
|
|
6
6
|
this.db_config = config.db_config;
|
|
7
7
|
|
|
8
|
+
console.log(config, "uhh");
|
|
8
9
|
this.api_key = config.api_key;
|
|
9
10
|
this.platform_uri = config.platform_uri;
|
|
10
11
|
this.route_table = new Route_table(this);
|
|
@@ -18,6 +19,7 @@ class GodProtocol {
|
|
|
18
19
|
if (opts.is_old) {
|
|
19
20
|
await this.route_table.init_version("v1", { is_old: true });
|
|
20
21
|
this.route_table.versions[version].routes = routes;
|
|
22
|
+
return;
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
await this.route_table.init_version(version);
|
package/Index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "godprotocol",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.45",
|
|
4
4
|
"description": "A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.",
|
|
5
5
|
"main": "Index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"homepage": "https://github.com/immanuel-savvy/GodProtocol#readme",
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"aircode4": "latest",
|
|
36
|
-
"generalised-datastore": "latest"
|
|
36
|
+
"generalised-datastore": "latest",
|
|
37
|
+
"@godprotocol/repositories": "^1.1.27"
|
|
37
38
|
},
|
|
38
39
|
"engines": {
|
|
39
40
|
"node": ">=18"
|
|
@@ -4,16 +4,21 @@ import Services, { gp_services } from "./Services.js";
|
|
|
4
4
|
class DB {
|
|
5
5
|
constructor(config, router) {
|
|
6
6
|
this.router = router;
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
let conf = {
|
|
8
9
|
db_url: config.db_url,
|
|
9
10
|
db_name: router?.gp?.platform_uri.replace(/\./g, "-"),
|
|
10
|
-
}
|
|
11
|
+
};
|
|
12
|
+
console.log(conf);
|
|
13
|
+
this.db = new Mongo(conf);
|
|
11
14
|
|
|
12
15
|
this.folders = {};
|
|
13
16
|
}
|
|
14
17
|
|
|
15
18
|
read_cache = async (query, category) => {
|
|
16
|
-
let coll = this.
|
|
19
|
+
let coll = await this.folder(`$CACHE-${category}`);
|
|
20
|
+
|
|
21
|
+
// await coll.deleteMany()
|
|
17
22
|
const doc = await coll.findOne(query);
|
|
18
23
|
if (!doc) return null;
|
|
19
24
|
|
|
@@ -22,7 +27,7 @@ class DB {
|
|
|
22
27
|
return null;
|
|
23
28
|
}
|
|
24
29
|
|
|
25
|
-
return
|
|
30
|
+
return JSON.parse(doc?.payload);
|
|
26
31
|
};
|
|
27
32
|
|
|
28
33
|
save_cache = async (
|
|
@@ -31,7 +36,7 @@ class DB {
|
|
|
31
36
|
category,
|
|
32
37
|
ttlMs = 7 * 24 * 60 * 60 * 1000,
|
|
33
38
|
) => {
|
|
34
|
-
let coll = this.
|
|
39
|
+
let coll = await this.folder(`$CACHE-${category}`);
|
|
35
40
|
const now = new Date();
|
|
36
41
|
|
|
37
42
|
const toSet = {
|
|
@@ -68,7 +73,7 @@ class DB {
|
|
|
68
73
|
|
|
69
74
|
if (folder) return folder;
|
|
70
75
|
|
|
71
|
-
folder = this.db.collection(name);
|
|
76
|
+
folder = await this.db.collection(name);
|
|
72
77
|
this.folders[name] = folder;
|
|
73
78
|
|
|
74
79
|
return folder;
|
|
@@ -114,7 +119,7 @@ class Headers extends Services {
|
|
|
114
119
|
headers["authorization"] = `Bearer ${authorization}`;
|
|
115
120
|
}
|
|
116
121
|
|
|
117
|
-
|
|
122
|
+
let res = await fetch(`${gp_services.profile}/validate`, {
|
|
118
123
|
method: "post",
|
|
119
124
|
headers: {
|
|
120
125
|
"Content-Type": "application/json",
|
|
@@ -168,7 +173,7 @@ class Headers extends Services {
|
|
|
168
173
|
headers["authorization"] = `Bearer ${authorization}`;
|
|
169
174
|
}
|
|
170
175
|
|
|
171
|
-
|
|
176
|
+
let res = await fetch(`${gp_services.profile}/validate_third_party`, {
|
|
172
177
|
method: "post",
|
|
173
178
|
headers: {
|
|
174
179
|
"Content-Type": "application/json",
|
|
@@ -205,7 +210,7 @@ class Headers extends Services {
|
|
|
205
210
|
handle_security = async (name, request) => {
|
|
206
211
|
let route = await this.get_route(name);
|
|
207
212
|
|
|
208
|
-
if (!route
|
|
213
|
+
if (!route?.config?.security?.length) return true;
|
|
209
214
|
|
|
210
215
|
if (!this.check_security(route.config.security, request)) {
|
|
211
216
|
console.log("Unauthorized access attempt to route:", name);
|
|
@@ -14,6 +14,7 @@ class Route_table extends Headers {
|
|
|
14
14
|
|
|
15
15
|
this.gp = gp;
|
|
16
16
|
this.db_config = gp.db_config;
|
|
17
|
+
this.api_key = gp.api_key;
|
|
17
18
|
this.versions = {};
|
|
18
19
|
this.validators = {};
|
|
19
20
|
}
|
|
@@ -52,7 +53,13 @@ class Route_table extends Headers {
|
|
|
52
53
|
this.versions[this.active_version].routes[name] = { handler, config };
|
|
53
54
|
};
|
|
54
55
|
|
|
55
|
-
get_route = async (name) =>
|
|
56
|
+
get_route = async (name) => {
|
|
57
|
+
let version = this.versions[this.active_version];
|
|
58
|
+
|
|
59
|
+
let route = version.routes[name];
|
|
60
|
+
|
|
61
|
+
return route;
|
|
62
|
+
};
|
|
56
63
|
|
|
57
64
|
check_validation = async (rule, data) => {
|
|
58
65
|
let { prop, type, required, default: is_default } = rule;
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { DB } from "./Header.js";
|
|
2
2
|
|
|
3
|
+
let DEV = true;
|
|
3
4
|
let gp_services = {
|
|
4
|
-
profile:
|
|
5
|
-
|
|
5
|
+
profile: DEV
|
|
6
|
+
? "http://localhost:4000"
|
|
7
|
+
: "https://profile-api.savvyaisolution.com",
|
|
8
|
+
settings: DEV
|
|
9
|
+
? "http://localhost:4005"
|
|
10
|
+
: "https://settings-api.savvyaisolution.com",
|
|
6
11
|
};
|
|
7
12
|
|
|
8
13
|
export { gp_services };
|
|
@@ -19,37 +24,50 @@ class Services {
|
|
|
19
24
|
get_token = async (service, id) => {
|
|
20
25
|
let token;
|
|
21
26
|
|
|
27
|
+
console.log(service, id, "gettok");
|
|
22
28
|
let db = await this.platform_db();
|
|
23
29
|
token = await db.read_cache(
|
|
24
30
|
{ platform_uri: service.uri, id: id.profile || id.platform },
|
|
25
31
|
"token",
|
|
26
32
|
);
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
console.log(token, "in-cache");
|
|
35
|
+
if (token) return token.token;
|
|
36
|
+
try {
|
|
37
|
+
console.log(this.api_key, gp_services.profile);
|
|
29
38
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
token = await ftch.json();
|
|
39
|
+
let ftch = await fetch(`${gp_services.profile}/get_token`, {
|
|
40
|
+
method: "POST",
|
|
41
|
+
headers: {
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
Accept: "application/json",
|
|
44
|
+
"x-api-version": "v2",
|
|
45
|
+
"x-api-key": this.api_key,
|
|
46
|
+
},
|
|
47
|
+
body: JSON.stringify({ platform_uri: service.uri, ...id }),
|
|
48
|
+
});
|
|
42
49
|
|
|
43
|
-
|
|
44
|
-
await
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
console.log("whats it like??");
|
|
51
|
+
token = await ftch.json();
|
|
52
|
+
console.log(token, "HOOWWW");
|
|
53
|
+
|
|
54
|
+
if (token?.ok) {
|
|
55
|
+
await db.save_cache(
|
|
56
|
+
{ id: id.profile || id.platform, platform_uri: service.uri },
|
|
57
|
+
{ token: token.token },
|
|
58
|
+
"token",
|
|
59
|
+
);
|
|
60
|
+
return token.token;
|
|
61
|
+
}
|
|
62
|
+
} catch (e) {
|
|
63
|
+
return {
|
|
64
|
+
ok: false,
|
|
65
|
+
status: 500,
|
|
66
|
+
message: e.message,
|
|
67
|
+
};
|
|
50
68
|
}
|
|
51
69
|
|
|
52
|
-
return
|
|
70
|
+
return token;
|
|
53
71
|
};
|
|
54
72
|
|
|
55
73
|
get_service = async (service) => {
|
|
@@ -69,18 +87,30 @@ class Services {
|
|
|
69
87
|
token = header.token;
|
|
70
88
|
} else if (header.profile) {
|
|
71
89
|
token = await this.get_token(servic, header);
|
|
90
|
+
|
|
91
|
+
if (typeof token !== "string") {
|
|
92
|
+
if (token?.ok === false) return token;
|
|
93
|
+
}
|
|
72
94
|
}
|
|
73
95
|
}
|
|
74
|
-
headers["Authorization"] = `Bearer ${token}`;
|
|
96
|
+
if (!token) headers["Authorization"] = `Bearer ${token}`;
|
|
75
97
|
headers["x-platform"] = this.platform_uri;
|
|
76
98
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
99
|
+
try {
|
|
100
|
+
let response = await fetch(`${servic.url}/${path}`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers,
|
|
103
|
+
body: JSON.stringify(body || {}),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return await response.json();
|
|
107
|
+
} catch (e) {
|
|
108
|
+
return {
|
|
109
|
+
ok: false,
|
|
110
|
+
message: e.message,
|
|
111
|
+
status: 500,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
84
114
|
},
|
|
85
115
|
};
|
|
86
116
|
};
|
|
@@ -92,11 +122,12 @@ class Services {
|
|
|
92
122
|
resolve_db = async (req) => {
|
|
93
123
|
const { platform } = req.headers;
|
|
94
124
|
|
|
125
|
+
console.log(req.headers["x-api-key"], process.env.API_KEY);
|
|
95
126
|
// 🔹 Internal system call → use base DB
|
|
96
127
|
if (req.headers["x-api-key"] === process.env.API_KEY) {
|
|
97
128
|
const db = await this.platform_db();
|
|
98
129
|
req.db = db;
|
|
99
|
-
return db;
|
|
130
|
+
return { ok: true, db };
|
|
100
131
|
}
|
|
101
132
|
|
|
102
133
|
if (!platform?.uri || !platform?._id) {
|
|
@@ -187,7 +218,7 @@ class Services {
|
|
|
187
218
|
|
|
188
219
|
req.db = db;
|
|
189
220
|
|
|
190
|
-
return db;
|
|
221
|
+
return { ok: true, db };
|
|
191
222
|
};
|
|
192
223
|
}
|
|
193
224
|
|