godprotocol 2.2.50 → 2.2.55

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 CHANGED
@@ -5,12 +5,16 @@ class GodProtocol {
5
5
  constructor(config) {
6
6
  this.db_config = config.db_config;
7
7
 
8
- console.log(config, "uhh");
8
+ // debug(config, "uhh");
9
9
  this.api_key = config.api_key;
10
10
  this.platform_uri = config.platform_uri;
11
11
  this.route_table = new Route_table(this);
12
12
  }
13
13
 
14
+ platform_db = async () => {
15
+ return await this.route_table.platform_db();
16
+ };
17
+
14
18
  load_services = async (services) => {
15
19
  await this.route_table.load_services(services);
16
20
  };
@@ -27,6 +31,14 @@ class GodProtocol {
27
31
  await this.route_table.load_routes(routes);
28
32
  };
29
33
 
34
+ call = async (route, { headers, body }) => {
35
+ return await version_middleware(
36
+ { path: route, headers, body },
37
+ null,
38
+ this.route_table,
39
+ );
40
+ };
41
+
30
42
  on_request = async (req, res) => {
31
43
  await version_middleware(req, res, this.route_table);
32
44
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "2.2.50",
3
+ "version": "2.2.55",
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",
@@ -146,7 +146,7 @@ class Headers extends Services {
146
146
  type: "third_party",
147
147
  xplatform,
148
148
  authorization: authorization || null,
149
- platform: process.env.PLATFORM_URI,
149
+ platform: this.platform_uri,
150
150
  };
151
151
 
152
152
  // 🔹 1. Try cache
@@ -165,24 +165,28 @@ class Headers extends Services {
165
165
 
166
166
  // 🔹 2. Call remote
167
167
  const headers = {
168
- "x-api-version": "v2",
169
- "x-api-key": process.env.API_KEY,
168
+ "x-api-version": "v3",
169
+ "x-platform": this.platform_uri,
170
+ "x-api-key": this.api_key,
170
171
  };
171
172
 
172
173
  if (authorization) {
173
174
  headers["authorization"] = `Bearer ${authorization}`;
174
175
  }
175
176
 
176
- let res = await fetch(`${gp_services.profile}/validate_third_party`, {
177
- method: "post",
178
- headers: {
179
- "Content-Type": "application/json",
180
- Accept: "application/json",
181
- "x-api-version": "v2",
182
- ...headers,
177
+ let res = await fetch(
178
+ `${gp_services.profile}/${xplatform === this.platform_uri ? "me" : "third_party_me"}`,
179
+ {
180
+ method: "post",
181
+ headers: {
182
+ "Content-Type": "application/json",
183
+ Accept: "application/json",
184
+ "x-api-version": "v3",
185
+ ...headers,
186
+ },
187
+ body: JSON.stringify({ from: xplatform }),
183
188
  },
184
- body: JSON.stringify({ platform_uri: xplatform }),
185
- });
189
+ );
186
190
  res = await res.json();
187
191
 
188
192
  // 🔹 3. Save cache
@@ -210,10 +214,11 @@ class Headers extends Services {
210
214
  handle_security = async (name, request) => {
211
215
  let route = await this.get_route(name);
212
216
 
217
+ console.log(route?.config?.security, "SECURITY FOR ROUTE");
213
218
  if (!route?.config?.security?.length) return true;
214
219
 
215
220
  if (!this.check_security(route.config.security, request)) {
216
- console.log("Unauthorized access attempt to route:", name);
221
+ // debug("Unauthorized access attempt to route:", name);
217
222
  return {
218
223
  ok: false,
219
224
  status: 401,
@@ -230,12 +235,10 @@ class Headers extends Services {
230
235
  if (xplatform) {
231
236
  val = await this.validate_third_party(xplatform, authorisation);
232
237
  } else {
233
- console.log(api_key, authorisation);
234
238
  val = await this.validate_api_key(api_key, authorisation);
235
- console.log(val, "Uhh");
236
239
  }
237
240
 
238
- console.log(val);
241
+ // debug(val);
239
242
  if (!val?.ok) {
240
243
  return val;
241
244
  } else {
@@ -248,8 +251,9 @@ class Headers extends Services {
248
251
  };
249
252
  }
250
253
  request.headers.profile = val.profile;
251
- request.headers.xplatform = val.xplatform;
252
254
  request.headers.platform = val.platform;
255
+ if (val.xplatform) request.headers.xplatform = val.xplatform;
256
+ if (val.third_party) request.headers.third_party = val.third_party;
253
257
  }
254
258
 
255
259
  return true;
@@ -186,7 +186,7 @@ class Route_table extends Headers {
186
186
  if (config.schema) {
187
187
  let resp = await this.validate(config.schema, payload);
188
188
 
189
- console.log("Validation result for route", name, ":", resp);
189
+ // debug("Validation result for route", name, ":", resp);
190
190
 
191
191
  if (!resp.ok) return resp;
192
192
  }
@@ -24,17 +24,17 @@ class Services {
24
24
  get_token = async (service, id) => {
25
25
  let token;
26
26
 
27
- console.log(service, id, "gettok");
27
+ // debug(service, id, "gettok");
28
28
  let db = await this.platform_db();
29
29
  token = await db.read_cache(
30
- { platform_uri: service.uri, id: id.profile || id.platform },
30
+ { platform_uri: service.uri, id: id.profile },
31
31
  "token",
32
32
  );
33
33
 
34
- console.log(token, "in-cache");
34
+ // debug(token, "in-cache");
35
35
  if (token) return token.token;
36
36
  try {
37
- console.log(this.api_key, gp_services.profile);
37
+ // debug(this.api_key, gp_services.profile);
38
38
 
39
39
  let ftch = await fetch(`${gp_services.profile}/get_token`, {
40
40
  method: "POST",
@@ -47,13 +47,13 @@ class Services {
47
47
  body: JSON.stringify({ platform_uri: service.uri, ...id }),
48
48
  });
49
49
 
50
- console.log("whats it like??");
50
+ // debug("whats it like??");
51
51
  token = await ftch.json();
52
- console.log(token, "HOOWWW");
52
+ // debug(token, "HOOWWW");
53
53
 
54
54
  if (token?.ok) {
55
55
  await db.save_cache(
56
- { id: id.profile || id.platform, platform_uri: service.uri },
56
+ { id: id.profile, platform_uri: service.uri },
57
57
  { token: token.token },
58
58
  "token",
59
59
  );
@@ -93,7 +93,7 @@ class Services {
93
93
  }
94
94
  }
95
95
  }
96
- if (!token) headers["Authorization"] = `Bearer ${token}`;
96
+ if (token) headers["Authorization"] = `Bearer ${token}`;
97
97
  headers["x-platform"] = this.platform_uri;
98
98
 
99
99
  try {
@@ -121,200 +121,18 @@ class Services {
121
121
 
122
122
  handle_webhook_prior = async (req) => {
123
123
  let { platform } = req.headers;
124
-
125
- let conf = await this.get_setting(
126
- platform,
127
- {
128
- category: "webhooks",
129
- key: ["general", this.platform_uri],
130
- },
131
- {
132
- type: "webhooks",
133
- key: platform.uri,
134
- platform: this.platform_uri,
135
- },
136
- );
137
-
138
- if (conf.ok === false) return null;
139
-
140
- let response = await fetch(conf?.data?.url, {
141
- method: "POST",
142
- headers: {
143
- "Content-Type": "application/json",
144
- Accept: "application/json",
145
- "x-platform": req.headers["x-platform"],
146
- "is-webhook": true,
147
- authorization: req.headers["authorization"],
148
- "x-api-version": conf?.data?.api_version || "v1",
149
- },
150
- body: JSON.stringify({
151
- body: req.body,
152
- from_platform: this.platform_uri,
153
- endpoint: req.path,
154
- server: this.gp.server,
155
- }),
156
- });
157
-
158
- let res = await response.json();
159
-
160
- return res;
161
- };
162
-
163
- handle_webhook_after = async (req, result) => {
164
- let { platform } = req.headers;
165
-
166
- let conf = await this.get_setting(
167
- platform,
168
- {
169
- category: "webhooks",
170
- key: ["general", this.platform_uri],
171
- },
172
- {
173
- type: "webhooks",
174
- key: platform.uri,
175
- platform: this.platform_uri,
176
- },
177
- );
178
-
179
- if (conf.ok === false) return null;
180
-
181
- let response = await fetch(conf?.data?.url, {
182
- method: "POST",
183
- headers: {
184
- "Content-Type": "application/json",
185
- Accept: "application/json",
186
- "x-platform": req.headers["x-platform"],
187
- "is-webhook": true,
188
- authorization: req.headers["authorization"],
189
- "x-api-version": conf?.data?.api_version || "v1",
190
- },
191
- body: JSON.stringify({
192
- response: result,
193
- body: req.body,
194
- from_platform: this.platform_uri,
195
- endpoint: req.path,
196
- server: this.gp.server,
197
- }),
198
- });
199
-
200
- let res = await response.json();
201
-
202
- return res;
124
+ if (!platform) return false;
203
125
  };
204
126
 
205
- get_setting = async (platform, setting, query) => {
206
- const baseDB = await this.platform_db();
207
-
208
- // 🔹 1. Try cache
209
- const cached = await baseDB.read_cache(query, "config");
210
-
211
- let config;
212
-
213
- if (cached?.payload) {
214
- try {
215
- config = JSON.parse(cached.payload);
216
- } catch {
217
- config = null; // corrupted cache → ignore
218
- }
219
- }
220
-
221
- if (config) return config;
222
-
223
- const tokenRes = await this.get_token(
224
- { uri: "settings.savvyaisolution.com" },
225
- { platform: platform._id },
226
- );
227
-
228
- if (!tokenRes.token) {
229
- return {
230
- ok: false,
231
- message: "Failed to retrieve platform token",
232
- status: 403,
233
- };
234
- }
235
-
236
- let ftch = await fetch(`${gp_services.settings}/get_settings`, {
237
- method: "POST",
238
- headers: {
239
- Authorization: `Bearer ${tokenRes.token}`,
240
- "x-platform": this.platform_uri,
241
- "x-api-version": "v1",
242
- Accept: "application/json",
243
- "Content-Type": "application/json",
244
- },
245
- body: JSON.stringify(setting),
246
- });
247
- let Res = await ftch.json();
248
-
249
- if (!Res.ok) {
250
- return {
251
- ok: false,
252
- message: "Failed to retrieve setting",
253
- status: 500,
254
- };
255
- }
256
-
257
- config =
258
- Res.data?.[setting.category]?.[`${this.platform_uri}`] ||
259
- Res.data?.[setting.category]?.["general"];
260
-
261
- if (!config) {
262
- return {
263
- ok: false,
264
- message: "setting not found",
265
- status: 404,
266
- };
267
- }
268
-
269
- // 🔹 3. Save cache
270
- await baseDB.save_cache(query, config, "config");
271
-
272
- return config;
273
- };
127
+ handle_webhook_after = async (req, result) => {};
274
128
 
275
129
  resolve_db = async (req) => {
276
- const { platform } = req.headers;
277
-
278
- console.log(req.headers["x-api-key"], process.env.API_KEY);
279
- // 🔹 Internal system call → use base DB
280
- if (req.headers["x-api-key"] === process.env.API_KEY) {
281
- const db = await this.platform_db();
282
- req.db = db;
283
- return { ok: true, db };
284
- }
285
-
286
- if (!platform?.uri || !platform?._id) {
287
- return {
288
- ok: false,
289
- message: "Platform context missing",
290
- status: 400,
291
- };
292
- }
130
+ let db = await this.platform_db();
293
131
 
294
- const query = {
295
- type: "db_config",
296
- key: platform.uri,
297
- platform: this.platform_uri,
132
+ return {
133
+ ok: true,
134
+ db,
298
135
  };
299
-
300
- // 🔹 2. Fetch remote if no valid cache
301
-
302
- let config = await this.get_setting(
303
- platform,
304
- {
305
- category: "db",
306
- key: ["general", this.platform_uri],
307
- },
308
- query,
309
- );
310
-
311
- if (config?.ok === false) return config;
312
- // 🔹 4. Instantiate scoped DB
313
- const db = new DB(config, this);
314
-
315
- req.db = db;
316
-
317
- return { ok: true, db };
318
136
  };
319
137
  }
320
138
 
@@ -7,6 +7,8 @@ const respond = async (result, res) => {
7
7
  if (result.pagination) response.pagination = result.pagination;
8
8
  if (result.token) response.token = result.token;
9
9
 
10
+ if (!res) return response;
11
+
10
12
  res.status(result?.status || (result?.ok ? 200 : 403)).json(response);
11
13
  };
12
14
 
@@ -32,6 +34,7 @@ const version_middleware = async (req, res, routers) => {
32
34
 
33
35
  try {
34
36
  result = await router.handle_security(name, req);
37
+ // console.log("Security check result:", result);
35
38
 
36
39
  if (result !== true) {
37
40
  return await respond(result, res);
@@ -45,9 +48,12 @@ const version_middleware = async (req, res, routers) => {
45
48
  let db = await router.resolve_db(req);
46
49
  let is_webhook = req.headers["is-webhook"];
47
50
 
51
+ // console.log(req.headers);
48
52
  if (!is_webhook) {
49
53
  let web_prior = await router.handle_webhook_prior(req);
50
- if (!web_prior?.ok) {
54
+
55
+ if (web_prior === false) {
56
+ } else if (!web_prior?.ok) {
51
57
  return await respond(web_prior, res);
52
58
  } else {
53
59
  req.headers.webhook_prior = web_prior;
@@ -58,13 +64,14 @@ const version_middleware = async (req, res, routers) => {
58
64
  result = await router.execute(name, {
59
65
  body: req.body,
60
66
  headers: req.headers,
61
- db,
67
+ db: db.db,
68
+ gp: router.gp,
62
69
  services: router.get_service,
63
70
  });
64
71
 
65
72
  !is_webhook && (await router.handle_webhook_after(req, result));
66
73
 
67
- await respond(result, res);
74
+ return await respond(result, res);
68
75
  } catch (err) {
69
76
  console.error(err);
70
77
 
@@ -73,10 +80,13 @@ const version_middleware = async (req, res, routers) => {
73
80
  } catch (err) {
74
81
  console.error("Version Middleware Error:", err);
75
82
 
76
- return res.status(500).json({
77
- ok: false,
78
- message: "Internal server error",
79
- });
83
+ return (
84
+ res &&
85
+ res.status(500).json({
86
+ ok: false,
87
+ message: "Internal server error",
88
+ })
89
+ );
80
90
  }
81
91
  };
82
92