godprotocol 2.2.66 → 2.2.68

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "2.2.66",
3
+ "version": "2.2.68",
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",
@@ -136,11 +136,15 @@ class Services {
136
136
 
137
137
  handle_webhook_after = async (req, result) => {
138
138
  let { headers, body, route } = req;
139
- let { xplatform, platform } = headers;
139
+ let { xplatform, third_party, platform } = headers;
140
140
 
141
141
  if (xplatform && xplatform.uri !== platform.uri) {
142
- if (xplatform.webhook?.enabled) {
143
- let { webhook } = xplatform;
142
+ let webhook = third_party?.webhook || xplatform.webhook;
143
+
144
+ if (webhook?.enabled) {
145
+ if (webhook?.routes?.length && !webhook?.routes?.includes(route))
146
+ return;
147
+
144
148
  await fetch(webhook?.url, {
145
149
  method: "POST",
146
150
  headers: {
@@ -69,64 +69,76 @@ const version_middleware = async (req, res, routers) => {
69
69
  const version = req.headers["x-api-version"] || "v1";
70
70
 
71
71
  const versionRouter = routers.get_version(version);
72
- // ----------------------------
73
- // BODY (STREAM PARSE)
74
- // ----------------------------
75
- const body = await getBody(req);
76
72
 
77
73
  if (!versionRouter) {
78
- res.statusCode = 400;
79
- res.setHeader("Content-Type", "application/json");
80
- return res.end(
81
- JSON.stringify({
74
+ return respond(
75
+ {
82
76
  ok: false,
83
77
  message: "Invalid API version",
84
- }),
78
+ status: 400,
79
+ },
80
+ res,
85
81
  );
86
82
  }
87
83
 
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
+ let body = {};
97
+
98
+ if (!["GET", "HEAD"].includes(req.method)) {
99
+ body = await getBody(req);
100
+ }
101
+
102
+ // unified payload
103
+ body = req.method === "GET" ? query : body;
104
+
88
105
  if (versionRouter.config?.is_old) {
89
106
  req.body = body;
90
107
  return versionRouter.routes(req, res);
91
108
  }
92
109
 
93
110
  const router = routers;
94
- const name = req.url.replace(/^\//, "");
111
+
112
+ // remove query string from route
113
+ const name = url.pathname.replace(/^\//, "");
95
114
 
96
115
  const isWebhook = req.headers["is-webhook"];
97
116
 
98
- // ----------------------------
99
117
  // SECURITY
100
- // ----------------------------
101
118
  const securityResult = await router.handle_security(name, req);
102
119
 
103
120
  if (securityResult !== true) {
104
121
  return respond(securityResult, res);
105
122
  }
106
123
 
107
- // ----------------------------
108
124
  // DB
109
- // ----------------------------
110
125
  const db = await router.resolve_db(req);
111
126
 
112
- // ----------------------------
113
127
  // WEBHOOK PRIOR
114
- // ----------------------------
115
128
  if (!isWebhook) {
116
- const webPrior = await router.handle_webhook_prior({ ...req, body });
129
+ const webPrior = await router.handle_webhook_prior({
130
+ ...req,
131
+ body,
132
+ });
117
133
 
118
134
  if (!webPrior?.ok) {
119
135
  return respond(webPrior, res);
120
136
  }
121
137
 
122
- if (webPrior?.ok) {
123
- req.headers.webhook_prior = webPrior;
124
- }
138
+ req.headers.webhook_prior = webPrior;
125
139
  }
126
140
 
127
- // ----------------------------
128
141
  // EXECUTE
129
- // ----------------------------
130
142
  const result = await router.execute(name, {
131
143
  body,
132
144
  route: name,
@@ -134,11 +146,10 @@ const version_middleware = async (req, res, routers) => {
134
146
  db: db.db,
135
147
  gp: router.gp,
136
148
  services: router.get_service,
149
+ method: req.method,
137
150
  });
138
151
 
139
- // ----------------------------
140
152
  // WEBHOOK AFTER
141
- // ----------------------------
142
153
  if (!isWebhook) {
143
154
  await router.handle_webhook_after(req, result);
144
155
  }
@@ -147,13 +158,13 @@ const version_middleware = async (req, res, routers) => {
147
158
  } catch (err) {
148
159
  console.error("Version Middleware Error:", err);
149
160
 
150
- res.statusCode = 500;
151
- res.setHeader("Content-Type", "application/json");
152
- res.end(
153
- JSON.stringify({
161
+ return respond(
162
+ {
154
163
  ok: false,
155
164
  message: "Internal server error",
156
- }),
165
+ status: 500,
166
+ },
167
+ res,
157
168
  );
158
169
  }
159
170
  };