godprotocol 2.3.23 → 2.3.25
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 +42 -1
- package/package.json +1 -1
- package/server/Routable/Header.js +29 -17
- package/server/Routable/Services.js +23 -33
- package/server/Utils_.js +21 -11
package/Godprotocol.js
CHANGED
|
@@ -3,6 +3,23 @@ import Route_table from "./server/Routable/Route_table.js";
|
|
|
3
3
|
import Utils from "./server/Utils_.js";
|
|
4
4
|
import version_middleware from "./server/version_control.js";
|
|
5
5
|
|
|
6
|
+
let gp_services = async (services_, gp) => {
|
|
7
|
+
if (!gp.utils.validateService(["identity"], services_)) return;
|
|
8
|
+
|
|
9
|
+
let { identity } = services_;
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
identity: {
|
|
13
|
+
api_version: "v3",
|
|
14
|
+
uri: identity.uri,
|
|
15
|
+
local: identity.local,
|
|
16
|
+
url: identity.url,
|
|
17
|
+
profile_key: identity.profile_key,
|
|
18
|
+
api_key: identity.api_key,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
6
23
|
class GodProtocol {
|
|
7
24
|
constructor(config) {
|
|
8
25
|
this.db_config = config.db_config;
|
|
@@ -17,9 +34,23 @@ class GodProtocol {
|
|
|
17
34
|
|
|
18
35
|
this.memory = new Map();
|
|
19
36
|
|
|
37
|
+
this.capabilities = config.capabilities;
|
|
38
|
+
|
|
20
39
|
this.utils = new Utils();
|
|
40
|
+
this.load_gp_services();
|
|
21
41
|
}
|
|
22
42
|
|
|
43
|
+
__gp_version = "__GP__";
|
|
44
|
+
|
|
45
|
+
load_gp_services = async () => {
|
|
46
|
+
if (!this.capabilities) return;
|
|
47
|
+
|
|
48
|
+
await this.route_table.load_services(
|
|
49
|
+
await gp_services(this.capabilities, this),
|
|
50
|
+
this.__gp_version,
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
|
|
23
54
|
on_start(fn) {
|
|
24
55
|
this.startup_fn = fn;
|
|
25
56
|
}
|
|
@@ -51,12 +82,22 @@ class GodProtocol {
|
|
|
51
82
|
};
|
|
52
83
|
|
|
53
84
|
add_router = async (version, routes, opts = {}) => {
|
|
85
|
+
if (opts.alias) {
|
|
86
|
+
for (let o of opts.alias) {
|
|
87
|
+
await this.add_router(version, routes, {
|
|
88
|
+
...opts,
|
|
89
|
+
alias: null,
|
|
90
|
+
prefix: o,
|
|
91
|
+
is_alias: true,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
54
95
|
if (opts?.prefix) {
|
|
55
96
|
if (opts.prefix === "..") {
|
|
56
97
|
opts.prefix = "";
|
|
57
98
|
} else {
|
|
58
99
|
version = `${opts.prefix}:${version}`;
|
|
59
|
-
if (!opts.db_name) opts.db_name = opts.prefix;
|
|
100
|
+
if (!opts.is_alias && !opts.db_name) opts.db_name = opts.prefix;
|
|
60
101
|
}
|
|
61
102
|
}
|
|
62
103
|
|
package/package.json
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import { Mongo } from "@godprotocol/repositories";
|
|
2
|
-
import Services
|
|
2
|
+
import Services from "./Services.js";
|
|
3
3
|
|
|
4
4
|
class DB {
|
|
5
5
|
constructor(config, router) {
|
|
6
6
|
this.router = router;
|
|
7
|
-
|
|
8
|
-
this.db_name=config.db_name;
|
|
7
|
+
|
|
8
|
+
this.db_name = config.db_name;
|
|
9
9
|
|
|
10
10
|
let conf = {
|
|
11
11
|
db_url: config.db_url,
|
|
12
12
|
db_name: config.db_name || router?.gp?.platform_uri.replace(/\./g, "-"),
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
|
|
16
15
|
this.db = new Mongo(conf);
|
|
17
16
|
|
|
18
17
|
this.folders = {};
|
|
@@ -135,12 +134,19 @@ class Headers extends Services {
|
|
|
135
134
|
}
|
|
136
135
|
}
|
|
137
136
|
|
|
138
|
-
let gp_profile = await this.get_service(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
137
|
+
let gp_profile = await this.get_service("identity", {
|
|
138
|
+
version: this.gp.__gp_version,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
let res = await gp_profile.call(
|
|
142
|
+
"validate",
|
|
143
|
+
{},
|
|
144
|
+
{
|
|
145
|
+
api_key,
|
|
146
|
+
token: authorization,
|
|
147
|
+
api_version: "v3",
|
|
148
|
+
},
|
|
149
|
+
);
|
|
144
150
|
|
|
145
151
|
// 🔹 3. Save cache if valid
|
|
146
152
|
if (res.ok && res.data) {
|
|
@@ -191,19 +197,25 @@ class Headers extends Services {
|
|
|
191
197
|
hst = hst.replace("10.0.2.2", "localhost");
|
|
192
198
|
}
|
|
193
199
|
|
|
194
|
-
let gp_profile = await this.get_service("identity"
|
|
200
|
+
let gp_profile = await this.get_service("identity", {
|
|
201
|
+
version: this.gp.__gp_version,
|
|
202
|
+
});
|
|
195
203
|
|
|
196
|
-
let third_party =
|
|
197
|
-
xplatform !== this.platform_uri //&& !gp_profile.url.endsWith(hst);
|
|
204
|
+
let third_party = xplatform !== this.platform_uri; //&& !gp_profile.url.endsWith(hst);
|
|
198
205
|
|
|
199
206
|
// if (gp_profile.url.endsWith(hst)) {
|
|
200
207
|
// headers["x-platform"] = xplatform;
|
|
201
208
|
// }
|
|
202
209
|
|
|
203
|
-
let res = await gp_profile.call(
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
210
|
+
let res = await gp_profile.call(
|
|
211
|
+
`${third_party ? "third_party_me" : "me"}`,
|
|
212
|
+
third_party ? { from: xplatform } : {},
|
|
213
|
+
{
|
|
214
|
+
token: authorization,
|
|
215
|
+
xplatform,
|
|
216
|
+
},
|
|
217
|
+
);
|
|
218
|
+
|
|
207
219
|
// 🔹 3. Save cache
|
|
208
220
|
if (res.ok && res.data) {
|
|
209
221
|
await db.save_cache(query, res.data, "auth");
|
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
import { DB } from "./Header.js";
|
|
2
2
|
|
|
3
|
-
let gp_services = () => {
|
|
4
|
-
return {};
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export { gp_services };
|
|
8
|
-
|
|
9
3
|
class Services {
|
|
10
4
|
constructor() {
|
|
11
5
|
this.services = new Object();
|
|
12
6
|
|
|
13
7
|
this.dbs = new Object();
|
|
14
|
-
this.load_services(gp_services);
|
|
15
8
|
this.version_services = {};
|
|
16
9
|
}
|
|
17
10
|
|
|
@@ -42,16 +35,13 @@ class Services {
|
|
|
42
35
|
|
|
43
36
|
if (token) return token.token;
|
|
44
37
|
try {
|
|
45
|
-
let gp_profile =
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"x-api-key": this.api_key,
|
|
53
|
-
},
|
|
54
|
-
body: JSON.stringify({ platform_uri: service.uri, ...id }),
|
|
38
|
+
let gp_profile = await this.get_service("identity", {
|
|
39
|
+
version: this.gp.__gp_version,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
let ftch = await gp_profile.call(`get_token`, {
|
|
43
|
+
platform_uri: service.uri,
|
|
44
|
+
...id,
|
|
55
45
|
});
|
|
56
46
|
|
|
57
47
|
token = await ftch.json();
|
|
@@ -77,10 +67,10 @@ class Services {
|
|
|
77
67
|
return token;
|
|
78
68
|
};
|
|
79
69
|
|
|
80
|
-
get_service = async (service) => {
|
|
70
|
+
get_service = async (service, opts = {}) => {
|
|
71
|
+
let ver = opts?.version || this.active_version;
|
|
81
72
|
let servic =
|
|
82
|
-
this.version_services[
|
|
83
|
-
this.services[service];
|
|
73
|
+
this.version_services[ver]?.[service] || this.services[service];
|
|
84
74
|
|
|
85
75
|
if (!servic) return;
|
|
86
76
|
|
|
@@ -95,15 +85,15 @@ class Services {
|
|
|
95
85
|
? `${servic.local}:${servic.api_version || header.api_version}`
|
|
96
86
|
: servic.api_version || header.api_version || "v1",
|
|
97
87
|
};
|
|
98
|
-
|
|
99
|
-
if (header?.api_key){
|
|
100
|
-
headers[
|
|
88
|
+
|
|
89
|
+
if (header?.api_key) {
|
|
90
|
+
headers["x-api-key"] = header.api_key;
|
|
101
91
|
} else if (servic.api_key) {
|
|
102
92
|
headers["x-api-key"] = servic.api_key;
|
|
103
93
|
}
|
|
104
94
|
|
|
105
|
-
if (header?.token){
|
|
106
|
-
headers[
|
|
95
|
+
if (header?.token) {
|
|
96
|
+
headers["Authorization"] = `Bearer ${header.token}`;
|
|
107
97
|
headers["x-platform"] = this.platform_uri;
|
|
108
98
|
} else if (servic.profile_key) {
|
|
109
99
|
headers["Authorization"] = `${servic.profile_key}`;
|
|
@@ -113,10 +103,10 @@ class Services {
|
|
|
113
103
|
if (typeof token !== "string") {
|
|
114
104
|
if (token?.ok === false) return token;
|
|
115
105
|
}
|
|
116
|
-
|
|
106
|
+
|
|
117
107
|
if (token) headers["Authorization"] = `Bearer ${token}`;
|
|
118
108
|
headers["x-platform"] = this.platform_uri;
|
|
119
|
-
}
|
|
109
|
+
}
|
|
120
110
|
|
|
121
111
|
if (
|
|
122
112
|
!headers["x-api-key"] &&
|
|
@@ -131,7 +121,7 @@ class Services {
|
|
|
131
121
|
}
|
|
132
122
|
|
|
133
123
|
try {
|
|
134
|
-
|
|
124
|
+
let prev_version = this.active_version;
|
|
135
125
|
|
|
136
126
|
let response = await fetch(
|
|
137
127
|
`${servic.local ? this.gp.server.domain : servic.url}/${path}`,
|
|
@@ -142,7 +132,7 @@ class Services {
|
|
|
142
132
|
},
|
|
143
133
|
);
|
|
144
134
|
|
|
145
|
-
this.get_version(prev_version)
|
|
135
|
+
this.get_version(prev_version);
|
|
146
136
|
|
|
147
137
|
return await response.json();
|
|
148
138
|
} catch (e) {
|
|
@@ -174,9 +164,9 @@ class Services {
|
|
|
174
164
|
|
|
175
165
|
let db = this.dbs[name];
|
|
176
166
|
if (!db) {
|
|
177
|
-
let conf = {...this.db_config};
|
|
178
|
-
if (name){
|
|
179
|
-
conf = {...this.db_config, db_name: name}
|
|
167
|
+
let conf = { ...this.db_config };
|
|
168
|
+
if (name) {
|
|
169
|
+
conf = { ...this.db_config, db_name: name };
|
|
180
170
|
}
|
|
181
171
|
db = new DB(conf, this);
|
|
182
172
|
|
|
@@ -194,7 +184,7 @@ class Services {
|
|
|
194
184
|
return res && Object.keys(res).hasOwnProperty("ok") ? res : { ok: false };
|
|
195
185
|
}
|
|
196
186
|
return {
|
|
197
|
-
ok: true,
|
|
187
|
+
ok: true,
|
|
198
188
|
};
|
|
199
189
|
};
|
|
200
190
|
|
package/server/Utils_.js
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
class Utils {
|
|
2
|
-
validateService = (
|
|
3
|
-
|
|
4
|
-
console.error(`Service "${name}" is missing.`);
|
|
5
|
-
return;
|
|
6
|
-
}
|
|
2
|
+
validateService = (names, services) => {
|
|
3
|
+
let hasErrors = false;
|
|
7
4
|
|
|
8
|
-
const
|
|
9
|
-
|
|
5
|
+
for (const name of names) {
|
|
6
|
+
const service = services[name];
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
if (!service) {
|
|
9
|
+
console.error(`Service "${name}" is missing.`);
|
|
10
|
+
hasErrors = true;
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const hasUrlOrLocal = Boolean(service.url || service.local);
|
|
15
|
+
const hasUri = Boolean(service.uri);
|
|
16
|
+
|
|
17
|
+
if (!hasUrlOrLocal || !hasUri) {
|
|
18
|
+
console.error(
|
|
19
|
+
`Service "${name}" must provide both (url or local) and uri properties.`,
|
|
20
|
+
);
|
|
21
|
+
hasErrors = true;
|
|
22
|
+
}
|
|
15
23
|
}
|
|
24
|
+
|
|
25
|
+
return !hasErrors;
|
|
16
26
|
};
|
|
17
27
|
|
|
18
28
|
validateRouter = (name, services_config) => {
|