godprotocol 2.3.21 → 2.3.23

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
@@ -1,5 +1,6 @@
1
1
  import Resource from "./server/Resource.js";
2
2
  import Route_table from "./server/Routable/Route_table.js";
3
+ import Utils from "./server/Utils_.js";
3
4
  import version_middleware from "./server/version_control.js";
4
5
 
5
6
  class GodProtocol {
@@ -15,6 +16,8 @@ class GodProtocol {
15
16
  this.route_table = new Route_table(this);
16
17
 
17
18
  this.memory = new Map();
19
+
20
+ this.utils = new Utils();
18
21
  }
19
22
 
20
23
  on_start(fn) {
@@ -48,7 +51,14 @@ class GodProtocol {
48
51
  };
49
52
 
50
53
  add_router = async (version, routes, opts = {}) => {
51
- if (opts?.prefix) version = `${opts.prefix}:${version}`;
54
+ if (opts?.prefix) {
55
+ if (opts.prefix === "..") {
56
+ opts.prefix = "";
57
+ } else {
58
+ version = `${opts.prefix}:${version}`;
59
+ if (!opts.db_name) opts.db_name = opts.prefix;
60
+ }
61
+ }
52
62
 
53
63
  if (typeof routes === "function" && !opts.is_old) routes = await routes();
54
64
 
@@ -58,7 +68,10 @@ class GodProtocol {
58
68
  return;
59
69
  }
60
70
 
61
- await this.route_table.init_version(version);
71
+ await this.route_table.init_version(version, {
72
+ db_name: opts.db_name,
73
+ db_prefix: opts.db_prefix,
74
+ });
62
75
 
63
76
  await this.route_table.load_routes(routes, opts);
64
77
  };
package/Index.js CHANGED
@@ -1,13 +1,3 @@
1
1
  import GodProtocol from "./Godprotocol.js";
2
2
 
3
- // let gp = new GodProtocol({
4
- // platform_uri: "profile.savvyaisolution.com",
5
- // api_key: process.env.API_KEY,
6
- // db_config: {},
7
- // });
8
-
9
- // gp.add_router("v2", {});
10
- // gp.load_services(services);
11
-
12
- // app.use((req, res) => gp.on_request(req, res));
13
3
  export default GodProtocol;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "2.3.21",
3
+ "version": "2.3.23",
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",
@@ -4,12 +4,15 @@ import Services, { gp_services } from "./Services.js";
4
4
  class DB {
5
5
  constructor(config, router) {
6
6
  this.router = router;
7
+
8
+ this.db_name=config.db_name;
7
9
 
8
10
  let conf = {
9
11
  db_url: config.db_url,
10
12
  db_name: config.db_name || router?.gp?.platform_uri.replace(/\./g, "-"),
11
13
  };
12
14
 
15
+
13
16
  this.db = new Mongo(conf);
14
17
 
15
18
  this.folders = {};
@@ -132,29 +135,13 @@ class Headers extends Services {
132
135
  }
133
136
  }
134
137
 
135
- // 🔹 2. Call remote
136
- const headers = {
137
- "x-api-version": "v3",
138
- "x-api-key": api_key,
139
- };
140
-
141
- if (authorization) {
142
- headers["authorization"] = `Bearer ${authorization}`;
143
- }
144
-
145
- let gp_profile = gp_services().identity;
146
- let res = await fetch(`${gp_profile.url}/validate`, {
147
- method: "post",
148
- headers: {
149
- "Content-Type": "application/json",
150
- Accept: "application/json",
151
- "x-api-version": gp_profile.api_version,
152
- ...headers,
153
- },
154
- body: JSON.stringify({}),
155
- });
138
+ let gp_profile = await this.get_service('identity');
139
+ let res = await gp_profile.call('validate', {}, {
140
+ api_key,
141
+ token: authorization,
142
+ api_version:'v3'
143
+ })
156
144
 
157
- res = await res.json();
158
145
  // 🔹 3. Save cache if valid
159
146
  if (res.ok && res.data) {
160
147
  await db.save_cache(query, res.data, "auth");
@@ -204,30 +191,19 @@ class Headers extends Services {
204
191
  hst = hst.replace("10.0.2.2", "localhost");
205
192
  }
206
193
 
207
- let gp_profile = gp_services().identity;
194
+ let gp_profile = await this.get_service("identity")
208
195
 
209
196
  let third_party =
210
- xplatform !== this.platform_uri && !gp_profile.url.endsWith(hst);
197
+ xplatform !== this.platform_uri //&& !gp_profile.url.endsWith(hst);
211
198
 
212
- if (gp_profile.url.endsWith(hst)) {
213
- headers["x-platform"] = xplatform;
214
- }
215
-
216
- let res = await fetch(
217
- `${gp_profile.url}/${third_party ? "third_party_me" : "me"}`,
218
- {
219
- method: "post",
220
- headers: {
221
- "Content-Type": "application/json",
222
- Accept: "application/json",
223
- "x-api-version": gp_profile.api_version,
224
- ...headers,
225
- },
226
- body: JSON.stringify(third_party ? { from: xplatform } : {}),
227
- },
228
- );
229
- res = await res.json();
199
+ // if (gp_profile.url.endsWith(hst)) {
200
+ // headers["x-platform"] = xplatform;
201
+ // }
230
202
 
203
+ let res = await gp_profile.call(`${third_party ? "third_party_me" : "me"}`, (third_party ? { from: xplatform } : {}), {
204
+ token: authorization, xplatform
205
+ })
206
+
231
207
  // 🔹 3. Save cache
232
208
  if (res.ok && res.data) {
233
209
  await db.save_cache(query, res.data, "auth");
@@ -296,7 +272,6 @@ class Headers extends Services {
296
272
  val = await this.validate_api_key(api_key, authorisation);
297
273
  }
298
274
 
299
- // debug(val);
300
275
  if (!val?.ok) {
301
276
  return val;
302
277
  } else {
@@ -21,8 +21,21 @@ class Route_table extends Headers {
21
21
  this.validators = {};
22
22
  }
23
23
 
24
- get_version = (version = this.active_version) => {
24
+ get_version = async (version = this.active_version) => {
25
25
  let ver = this.versions[version];
26
+
27
+ if (!ver) {
28
+ let versions = Object.keys(this.versions);
29
+ let verr = versions.find((v) => v.startsWith(`${version}:`));
30
+
31
+ if (verr) {
32
+ let serv = await this.get_service(version);
33
+ if (serv) {
34
+ version = `${version}:${serv.api_version}`;
35
+ ver = this.versions[version];
36
+ }
37
+ }
38
+ }
26
39
  if (!ver) return;
27
40
 
28
41
  this.active_version = version;
@@ -67,7 +80,7 @@ class Route_table extends Headers {
67
80
  }
68
81
 
69
82
  if (opts?.services) {
70
- let services = await opts.services(opts.services_config);
83
+ let services = await opts.services(opts.services_config, this.gp);
71
84
  await this.load_services(services, this.active_version);
72
85
  }
73
86
  };
@@ -85,34 +85,38 @@ class Services {
85
85
  if (!servic) return;
86
86
 
87
87
  return {
88
+ api_version: servic.api_version,
88
89
  uri: servic.uri,
89
90
  call: async (path, body, header) => {
90
91
  let headers = {
91
92
  "Content-Type": "application/json",
92
93
  Accept: "application/json",
93
94
  "x-api-version": servic.local
94
- ? `${servic.local}:${servic.api_version}`
95
- : servic.api_version || "v1",
95
+ ? `${servic.local}:${servic.api_version || header.api_version}`
96
+ : servic.api_version || header.api_version || "v1",
96
97
  };
97
- let token;
98
- if (servic.profile_key) {
98
+
99
+ if (header?.api_key){
100
+ headers['x-api-key'] = header.api_key;
101
+ } else if (servic.api_key) {
102
+ headers["x-api-key"] = servic.api_key;
103
+ }
104
+
105
+ if (header?.token){
106
+ headers['Authorization'] = `Bearer ${header.token}`
107
+ headers["x-platform"] = this.platform_uri;
108
+ } else if (servic.profile_key) {
99
109
  headers["Authorization"] = `${servic.profile_key}`;
100
- } else if (header?.token || header?.profile) {
101
- if (header.token) {
102
- token = header.token;
103
- } else if (header.profile) {
104
- token = await this.get_token(servic, header);
105
-
106
- if (typeof token !== "string") {
107
- if (token?.ok === false) return token;
108
- }
109
- }
110
+ } else if (header?.profile) {
111
+ let token = await this.get_token(servic, header);
110
112
 
113
+ if (typeof token !== "string") {
114
+ if (token?.ok === false) return token;
115
+ }
116
+
111
117
  if (token) headers["Authorization"] = `Bearer ${token}`;
112
118
  headers["x-platform"] = this.platform_uri;
113
- } else if (servic.api_key) {
114
- headers["x-api-key"] = servic.api_key;
115
- }
119
+ }
116
120
 
117
121
  if (
118
122
  !headers["x-api-key"] &&
@@ -125,7 +129,10 @@ class Services {
125
129
  meesage: "Service unauthorised",
126
130
  };
127
131
  }
132
+
128
133
  try {
134
+ let prev_version = this.active_version;
135
+
129
136
  let response = await fetch(
130
137
  `${servic.local ? this.gp.server.domain : servic.url}/${path}`,
131
138
  {
@@ -135,6 +142,8 @@ class Services {
135
142
  },
136
143
  );
137
144
 
145
+ this.get_version(prev_version)
146
+
138
147
  return await response.json();
139
148
  } catch (e) {
140
149
  return {
@@ -165,7 +174,11 @@ class Services {
165
174
 
166
175
  let db = this.dbs[name];
167
176
  if (!db) {
168
- db = new DB({ ...this.db_config, name }, this);
177
+ let conf = {...this.db_config};
178
+ if (name){
179
+ conf = {...this.db_config, db_name: name}
180
+ }
181
+ db = new DB(conf, this);
169
182
 
170
183
  this.dbs[name] = db;
171
184
  }
@@ -181,7 +194,7 @@ class Services {
181
194
  return res && Object.keys(res).hasOwnProperty("ok") ? res : { ok: false };
182
195
  }
183
196
  return {
184
- ok: true,
197
+ ok: true,
185
198
  };
186
199
  };
187
200
 
@@ -0,0 +1,35 @@
1
+ class Utils {
2
+ validateService = (name, service) => {
3
+ if (!service) {
4
+ console.error(`Service "${name}" is missing.`);
5
+ return;
6
+ }
7
+
8
+ const hasUrlOrLocal = Boolean(service.url || service.local);
9
+ const hasUri = Boolean(service.uri);
10
+
11
+ if (!hasUrlOrLocal || !hasUri) {
12
+ console.error(
13
+ `Service "${name}" must provide both (url or local) and uri properties.`,
14
+ );
15
+ }
16
+ };
17
+
18
+ validateRouter = (name, services_config) => {
19
+ if (!services_config?.[name]) {
20
+ console.info(
21
+ `Missing required "${name}" service configuration in services_config`,
22
+ );
23
+ return false;
24
+ }
25
+
26
+ const prefix = services_config?.[name].local;
27
+ if (!prefix) {
28
+ return false;
29
+ }
30
+
31
+ return prefix;
32
+ };
33
+ }
34
+
35
+ export default Utils;