godprotocol 2.2.97 → 2.2.99

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
@@ -14,15 +14,17 @@ class GodProtocol {
14
14
  this.route_table = new Route_table(this);
15
15
 
16
16
  this.memory = new Map();
17
-
18
- if (typeof this.startup_fn === "function") {
19
- this.startup_fn(this);
20
- }
21
17
  }
22
18
 
23
- on_start = async (fn) => {
19
+ on_start(fn) {
24
20
  this.startup_fn = fn;
25
- };
21
+ }
22
+
23
+ async boot() {
24
+ if (this.startup_fn) {
25
+ await this.startup_fn(this);
26
+ }
27
+ }
26
28
 
27
29
  uri_default_domain = (uri) => {
28
30
  if (!uri.includes(".")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "2.2.97",
3
+ "version": "2.2.99",
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",
@@ -16,18 +16,28 @@ class DB {
16
16
  }
17
17
 
18
18
  read_cache = async (query, category) => {
19
- let coll = await this.folder(`$CACHE-${category}`);
19
+ let folder_name = `$CACHE-${category}`;
20
20
 
21
- // await coll.deleteMany()
22
- const doc = await coll.findOne(query);
23
- if (!doc) return null;
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 (doc.expiry && new Date(doc.expiry) < new Date()) {
26
- await coll.deleteOne({ _id: doc._id });
27
- return null;
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
- return JSON.parse(doc?.payload);
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 coll = await this.folder(`$CACHE-${category}`);
40
- const now = new Date();
49
+ let folder_name = `$CACHE-${category}`;
50
+ let Cache = await this.gp.route_table.get_service("cache");
41
51
 
42
- const toSet = {
43
- payload: JSON.stringify(payload),
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
- // 🔥 set expiry if TTL provided
47
- if (ttlMs) {
48
- toSet.expiry = new Date(now.getTime() + ttlMs);
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
- // OR use payload expiry if present
52
- else if (payload?.expiry) {
53
- toSet.expiry = new Date(payload.expiry);
54
- }
62
+ const toSet = {
63
+ payload: JSON.stringify(payload),
64
+ };
55
65
 
56
- const res = await coll.updateOne(
57
- query,
58
- {
59
- $set: toSet,
60
- $setOnInsert: {
61
- _id: crypto.randomUUID(),
62
- created: now,
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
- { upsert: true },
66
- );
85
+ { upsert: true },
86
+ );
67
87
 
68
- return res;
88
+ return res;
89
+ } else {
90
+ return res;
91
+ }
69
92
  };
70
93
 
71
94
  folder = async (name) => {
@@ -131,7 +131,8 @@ class Services {
131
131
  ) {
132
132
  return {
133
133
  ok: false,
134
- meesage: "Service unauthorized",
134
+ status_code: "service_unauthorised",
135
+ meesage: "Service unauthorised",
135
136
  };
136
137
  }
137
138
  try {