godprotocol 2.2.68 → 2.2.80

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
@@ -5,7 +5,7 @@ class GodProtocol {
5
5
  constructor(config) {
6
6
  this.db_config = config.db_config;
7
7
 
8
- // debug(config, "uhh");
8
+ this.static_path = config.static_path;
9
9
  this.api_key = config.api_key;
10
10
  this.platform_uri = config.platform_uri;
11
11
  this.route_table = new Route_table(this);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "2.2.68",
3
+ "version": "2.2.80",
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",
@@ -13,6 +13,7 @@ class Route_table extends Headers {
13
13
  super();
14
14
 
15
15
  this.gp = gp;
16
+ this.static_path = gp.static_path;
16
17
  this.db_config = gp.db_config;
17
18
  this.api_key = gp.api_key;
18
19
  this.platform_uri = gp.platform_uri;
@@ -0,0 +1,59 @@
1
+ import path from "path";
2
+ import fs from "fs";
3
+
4
+ const serveStaticFile = (req, res) => {
5
+ try {
6
+ const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
7
+
8
+ let filePath = decodeURIComponent(parsedUrl.pathname);
9
+
10
+ // prevent path traversal attacks
11
+ filePath = filePath.replace(/\.\./g, "");
12
+
13
+ const fullPath = path.join(process.cwd(), filePath);
14
+ console.log(fullPath);
15
+ if (!fs.existsSync(fullPath)) {
16
+ res.statusCode = 404;
17
+ res.setHeader("Content-Type", "application/json");
18
+ return res.end(
19
+ JSON.stringify({
20
+ ok: false,
21
+ message: "File not found",
22
+ }),
23
+ );
24
+ }
25
+
26
+ const stream = fs.createReadStream(fullPath);
27
+
28
+ // basic content type handling
29
+ const ext = path.extname(fullPath);
30
+
31
+ const mimeTypes = {
32
+ ".pdf": "application/pdf",
33
+ ".docx":
34
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
35
+ ".json": "application/json",
36
+ ".txt": "text/plain",
37
+ };
38
+
39
+ res.statusCode = 200;
40
+ res.setHeader("Content-Type", mimeTypes[ext] || "application/octet-stream");
41
+
42
+ stream.pipe(res);
43
+ return true;
44
+ } catch (err) {
45
+ console.error("Static file error:", err);
46
+
47
+ res.statusCode = 500;
48
+ res.end(
49
+ JSON.stringify({
50
+ ok: false,
51
+ message: "Failed to serve file",
52
+ }),
53
+ );
54
+
55
+ return true;
56
+ }
57
+ };
58
+
59
+ export default serveStaticFile;
@@ -1,3 +1,5 @@
1
+ import serveStaticFile from "godprotocol/server/serve_static_file.js";
2
+
1
3
  const getBody = (req) => {
2
4
  return new Promise((resolve, reject) => {
3
5
  let data = "";
@@ -39,25 +41,52 @@ const applyCors = (req, res) => {
39
41
  };
40
42
 
41
43
  const respond = (result, res) => {
44
+ // ----------------------------
45
+ // REDIRECT
46
+ // ----------------------------
47
+
48
+ if (result?.redirect) {
49
+ const status = result?.status || 302;
50
+
51
+ res.statusCode = status;
52
+ res.setHeader("Location", result.redirect);
53
+
54
+ res.end();
55
+
56
+ return result;
57
+ }
58
+
59
+ // ----------------------------
60
+ // NORMAL JSON RESPONSE
61
+ // ----------------------------
62
+
42
63
  const response = {
43
64
  ok: result?.ok ?? false,
44
65
  message: result?.message ?? "",
45
66
  data: result?.data,
46
67
  };
47
68
 
48
- if (result?.pagination) response.pagination = result.pagination;
49
- if (result?.token) response.token = result.token;
69
+ if (result?.pagination) {
70
+ response.pagination = result.pagination;
71
+ }
72
+
73
+ if (result?.token) {
74
+ response.token = result.token;
75
+ }
50
76
 
51
77
  const status = result?.status || (result?.ok ? 200 : 403);
52
78
 
53
79
  res.statusCode = status;
80
+
54
81
  res.setHeader("Content-Type", "application/json");
82
+
55
83
  res.end(JSON.stringify(response));
56
84
 
57
85
  return response;
58
86
  };
59
87
 
60
88
  const version_middleware = async (req, res, routers) => {
89
+ let error_stage = "Version Middleware";
61
90
  try {
62
91
  applyCors(req, res);
63
92
 
@@ -66,9 +95,23 @@ const version_middleware = async (req, res, routers) => {
66
95
  return res.end();
67
96
  }
68
97
 
69
- const version = req.headers["x-api-version"] || "v1";
98
+ let version;
99
+ if (req.method === "GET") {
100
+ const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
101
+
102
+ const pathname = parsedUrl.pathname;
103
+
104
+ // STATIC ROUTE DETECTION
105
+ if (routers.static_path && pathname.startsWith(routers.static_path)) {
106
+ return serveStaticFile(req, res);
107
+ }
108
+
109
+ version = "__GET__";
110
+ } else {
111
+ version = req.headers["x-api-version"] || "v1";
112
+ }
70
113
 
71
- const versionRouter = routers.get_version(version);
114
+ const versionRouter = await routers.get_version(version);
72
115
 
73
116
  if (!versionRouter) {
74
117
  return respond(
@@ -81,27 +124,17 @@ const version_middleware = async (req, res, routers) => {
81
124
  );
82
125
  }
83
126
 
84
- // ----------------------------
85
- // PARSE URL + QUERY
86
- // ----------------------------
87
-
88
- const url = new URL(req.url, `http://${req.headers.host}`);
89
-
90
- const query = Object.fromEntries(url.searchParams.entries());
91
-
92
- // ----------------------------
93
- // PARSE BODY
94
- // ----------------------------
95
-
96
127
  let body = {};
97
128
 
98
- if (!["GET", "HEAD"].includes(req.method)) {
129
+ if (req.method === "GET") {
130
+ const url = new URL(req.url, `http://${req.headers.host}`);
131
+
132
+ body = Object.fromEntries(url.searchParams.entries());
133
+ } else {
134
+ error_stage = "Body Parser";
99
135
  body = await getBody(req);
100
136
  }
101
137
 
102
- // unified payload
103
- body = req.method === "GET" ? query : body;
104
-
105
138
  if (versionRouter.config?.is_old) {
106
139
  req.body = body;
107
140
  return versionRouter.routes(req, res);
@@ -110,11 +143,14 @@ const version_middleware = async (req, res, routers) => {
110
143
  const router = routers;
111
144
 
112
145
  // remove query string from route
113
- const name = url.pathname.replace(/^\//, "");
146
+ const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
147
+ req.url = parsedUrl.pathname;
148
+ const name = req.url.replace(/^\//, "");
114
149
 
115
150
  const isWebhook = req.headers["is-webhook"];
116
151
 
117
152
  // SECURITY
153
+ error_stage = "Header";
118
154
  const securityResult = await router.handle_security(name, req);
119
155
 
120
156
  if (securityResult !== true) {
@@ -126,6 +162,7 @@ const version_middleware = async (req, res, routers) => {
126
162
 
127
163
  // WEBHOOK PRIOR
128
164
  if (!isWebhook) {
165
+ error_stage = "Callback";
129
166
  const webPrior = await router.handle_webhook_prior({
130
167
  ...req,
131
168
  body,
@@ -139,6 +176,7 @@ const version_middleware = async (req, res, routers) => {
139
176
  }
140
177
 
141
178
  // EXECUTE
179
+ error_stage = "Handler";
142
180
  const result = await router.execute(name, {
143
181
  body,
144
182
  route: name,
@@ -151,12 +189,13 @@ const version_middleware = async (req, res, routers) => {
151
189
 
152
190
  // WEBHOOK AFTER
153
191
  if (!isWebhook) {
192
+ error_stage = "Webhook";
154
193
  await router.handle_webhook_after(req, result);
155
194
  }
156
195
 
157
196
  return respond(result, res);
158
197
  } catch (err) {
159
- console.error("Version Middleware Error:", err);
198
+ console.error(`${error_stage} Error:`, err);
160
199
 
161
200
  return respond(
162
201
  {