godprotocol 2.2.98 → 2.3.0
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/package.json +1 -1
- package/server/Routable/Header.js +56 -33
- package/server/Routable/Services.js +2 -1
package/Godprotocol.js
CHANGED
|
@@ -47,6 +47,8 @@ class GodProtocol {
|
|
|
47
47
|
};
|
|
48
48
|
|
|
49
49
|
add_router = async (version, routes, opts = {}) => {
|
|
50
|
+
if (typeof routes === "function") routes = await routes();
|
|
51
|
+
|
|
50
52
|
if (opts.is_old) {
|
|
51
53
|
await this.route_table.init_version(version, { is_old: true });
|
|
52
54
|
this.route_table.versions[version].routes = routes;
|
package/package.json
CHANGED
|
@@ -16,18 +16,28 @@ class DB {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
read_cache = async (query, category) => {
|
|
19
|
-
let
|
|
19
|
+
let folder_name = `$CACHE-${category}`;
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
let Cache = await this.gp.route_table.get_services("cache");
|
|
22
|
+
let res = await Cache.call("get", {
|
|
23
|
+
key: `${folder_name}:${JSON.stringify(query)}`,
|
|
24
|
+
});
|
|
24
25
|
|
|
25
|
-
if (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
if (!res?.ok && res.status_code === "service_unauthorised") {
|
|
27
|
+
let coll = await this.folder(folder_name);
|
|
28
|
+
|
|
29
|
+
const doc = await coll.findOne(query);
|
|
30
|
+
if (!doc) return null;
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
if (doc.expiry && new Date(doc.expiry) < new Date()) {
|
|
33
|
+
await coll.deleteOne({ _id: doc._id });
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return JSON.parse(doc?.payload);
|
|
38
|
+
} else {
|
|
39
|
+
return res?.ok ? JSON.parse(res.data) : null;
|
|
40
|
+
}
|
|
31
41
|
};
|
|
32
42
|
|
|
33
43
|
save_cache = async (
|
|
@@ -36,36 +46,49 @@ class DB {
|
|
|
36
46
|
category,
|
|
37
47
|
ttlMs = 7 * 24 * 60 * 60 * 1000,
|
|
38
48
|
) => {
|
|
39
|
-
let
|
|
40
|
-
|
|
49
|
+
let folder_name = `$CACHE-${category}`;
|
|
50
|
+
let Cache = await this.gp.route_table.get_service("cache");
|
|
41
51
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
52
|
+
let res = await Cache.call("get", {
|
|
53
|
+
key: folder_name,
|
|
54
|
+
value: JSON.stringify(payload),
|
|
55
|
+
no_db: true,
|
|
56
|
+
});
|
|
45
57
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
58
|
+
if (!res?.ok && res.status_code === "service_unauthorised") {
|
|
59
|
+
let coll = await this.folder(folder_name);
|
|
60
|
+
const now = new Date();
|
|
50
61
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
62
|
+
const toSet = {
|
|
63
|
+
payload: JSON.stringify(payload),
|
|
64
|
+
};
|
|
55
65
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
// 🔥 set expiry if TTL provided
|
|
67
|
+
if (ttlMs) {
|
|
68
|
+
toSet.expiry = new Date(now.getTime() + ttlMs);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// OR use payload expiry if present
|
|
72
|
+
else if (payload?.expiry) {
|
|
73
|
+
toSet.expiry = new Date(payload.expiry);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
res = await coll.updateOne(
|
|
77
|
+
query,
|
|
78
|
+
{
|
|
79
|
+
$set: toSet,
|
|
80
|
+
$setOnInsert: {
|
|
81
|
+
_id: crypto.randomUUID(),
|
|
82
|
+
created: now,
|
|
83
|
+
},
|
|
63
84
|
},
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
);
|
|
85
|
+
{ upsert: true },
|
|
86
|
+
);
|
|
67
87
|
|
|
68
|
-
|
|
88
|
+
return res;
|
|
89
|
+
} else {
|
|
90
|
+
return res;
|
|
91
|
+
}
|
|
69
92
|
};
|
|
70
93
|
|
|
71
94
|
folder = async (name) => {
|