godprotocol 2.2.43 → 2.2.47

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "2.2.43",
3
+ "version": "2.2.47",
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",
@@ -119,6 +119,159 @@ class Services {
119
119
  return new DB(this.db_config, this);
120
120
  };
121
121
 
122
+ handle_webhook_prior = async (req) => {
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.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.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.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.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;
203
+ };
204
+
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
+ };
274
+
122
275
  resolve_db = async (req) => {
123
276
  const { platform } = req.headers;
124
277
 
@@ -138,81 +291,24 @@ class Services {
138
291
  };
139
292
  }
140
293
 
141
- const baseDB = await this.platform_db();
142
-
143
294
  const query = {
144
295
  type: "db_config",
145
296
  key: platform.uri,
146
297
  platform: this.platform_uri,
147
298
  };
148
299
 
149
- // 🔹 1. Try cache
150
- const cached = await baseDB.read_cache(query, "config");
151
-
152
- let config;
153
-
154
- if (cached?.payload) {
155
- try {
156
- config = JSON.parse(cached.payload);
157
- } catch {
158
- config = null; // corrupted cache → ignore
159
- }
160
- }
161
-
162
300
  // 🔹 2. Fetch remote if no valid cache
163
- if (!config) {
164
- const tokenRes = await this.get_token(
165
- { uri: "settings.savvyaisolution.com" },
166
- { platform: platform._id },
167
- );
168
-
169
- if (!tokenRes.token) {
170
- return {
171
- ok: false,
172
- message: "Failed to retrieve platform token",
173
- status: 403,
174
- };
175
- }
176
301
 
177
- let ftch = await fetch(`${gp_services.settings}/get_settings`, {
178
- method: "POST",
179
- headers: {
180
- Authorization: `Bearer ${tokenRes.token}`,
181
- "x-platform": this.platform_uri,
182
- "x-api-version": "v1",
183
- Accept: "application/json",
184
- "Content-Type": "application/json",
185
- },
186
- body: JSON.stringify({
187
- category: "db",
188
- key: [this.platform_uri, "general"],
189
- }),
190
- });
191
- let dbRes = await ftch.json();
192
-
193
- if (!dbRes.ok) {
194
- return {
195
- ok: false,
196
- message: "Failed to retrieve database configuration",
197
- status: 500,
198
- };
199
- }
200
-
201
- config =
202
- dbRes.data?.db?.[`${this.platform_uri}`] || dbRes.data?.db?.["general"];
203
-
204
- if (!config) {
205
- return {
206
- ok: false,
207
- message: "Database configuration not found",
208
- status: 404,
209
- };
210
- }
211
-
212
- // 🔹 3. Save cache
213
- await baseDB.save_cache(query, config, "config");
214
- }
302
+ let config = await this.get_setting(
303
+ platform,
304
+ {
305
+ category: "db",
306
+ key: ["general", this.platform_uri],
307
+ },
308
+ query,
309
+ );
215
310
 
311
+ if (config?.ok === false) return config;
216
312
  // 🔹 4. Instantiate scoped DB
217
313
  const db = new DB(config, this);
218
314
 
@@ -43,6 +43,16 @@ const version_middleware = async (req, res, routers) => {
43
43
  }
44
44
 
45
45
  let db = await router.resolve_db(req);
46
+ let is_webhook = req.headers["is-webhook"];
47
+
48
+ if (!is_webhook) {
49
+ let web_prior = await router.handle_webhook_prior(req);
50
+ if (!web_prior?.ok) {
51
+ return await respond(web_prior, res);
52
+ } else {
53
+ req.headers.webhook_prior = web_prior;
54
+ }
55
+ }
46
56
 
47
57
  try {
48
58
  result = await router.execute(name, {
@@ -52,6 +62,8 @@ const version_middleware = async (req, res, routers) => {
52
62
  services: router.get_service,
53
63
  });
54
64
 
65
+ !is_webhook && (await router.handle_webhook_after(req, result));
66
+
55
67
  await respond(result, res);
56
68
  } catch (err) {
57
69
  console.error(err);