godprotocol 2.3.53 → 2.3.55
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 +1 -1
- package/server/version_control.js +32 -291
package/package.json
CHANGED
|
@@ -1,46 +1,30 @@
|
|
|
1
1
|
import parseMultipart from "./parse_multipart_data.js";
|
|
2
2
|
import serveStaticFile from "./serve_static_file.js";
|
|
3
|
-
import { debug } from "./utils.js";
|
|
4
3
|
|
|
5
4
|
const getBody = (req) => {
|
|
6
|
-
console.log("[getBody] Starting body parser");
|
|
7
|
-
|
|
8
5
|
return new Promise((resolve, reject) => {
|
|
9
6
|
let data = "";
|
|
10
7
|
|
|
11
8
|
req.on("data", (chunk) => {
|
|
12
|
-
console.log("[getBody] Received chunk:", chunk.length);
|
|
13
9
|
data += chunk;
|
|
14
10
|
});
|
|
15
11
|
|
|
16
12
|
req.on("end", () => {
|
|
17
|
-
console.log("[getBody] Request body received");
|
|
18
|
-
console.log("[getBody] Raw body length:", data.length);
|
|
19
|
-
|
|
20
13
|
try {
|
|
21
14
|
const parsed = data ? JSON.parse(data) : {};
|
|
22
|
-
|
|
23
|
-
console.log("[getBody] Parsed body:", parsed);
|
|
24
|
-
|
|
25
15
|
resolve(parsed);
|
|
26
16
|
} catch (err) {
|
|
27
|
-
console.error("[getBody] JSON parse error:", err);
|
|
28
|
-
|
|
29
17
|
reject(new Error("Invalid JSON body"));
|
|
30
18
|
}
|
|
31
19
|
});
|
|
32
20
|
|
|
33
21
|
req.on("error", (err) => {
|
|
34
|
-
console.error("[getBody] Request stream error:", err);
|
|
35
|
-
|
|
36
22
|
reject(err);
|
|
37
23
|
});
|
|
38
24
|
});
|
|
39
25
|
};
|
|
40
26
|
|
|
41
27
|
const applyCors = (req, res) => {
|
|
42
|
-
console.log("[CORS] Applying CORS headers");
|
|
43
|
-
|
|
44
28
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
45
29
|
|
|
46
30
|
res.setHeader(
|
|
@@ -50,30 +34,34 @@ const applyCors = (req, res) => {
|
|
|
50
34
|
|
|
51
35
|
res.setHeader(
|
|
52
36
|
"Access-Control-Allow-Headers",
|
|
53
|
-
"Content-Type, Authorization, x-api-version, x-api-key, x-platform
|
|
37
|
+
"Content-Type, Authorization, x-api-version, x-api-key, x-platform",
|
|
38
|
+
"x-secret",
|
|
54
39
|
);
|
|
55
40
|
|
|
56
|
-
|
|
41
|
+
// IMPORTANT: do NOT enable credentials with "*"
|
|
42
|
+
// res.setHeader("Access-Control-Allow-Credentials", "true");
|
|
57
43
|
};
|
|
58
44
|
|
|
59
45
|
const respond = (result, res) => {
|
|
60
|
-
|
|
46
|
+
// ----------------------------
|
|
47
|
+
// REDIRECT
|
|
48
|
+
// ----------------------------
|
|
61
49
|
|
|
62
50
|
if (result?.redirect) {
|
|
63
51
|
const status = result?.status || 302;
|
|
64
52
|
|
|
65
|
-
console.log("[RESPOND] Redirect:", result.redirect, "Status:", status);
|
|
66
|
-
|
|
67
53
|
res.statusCode = status;
|
|
68
54
|
res.setHeader("Location", result.redirect);
|
|
69
55
|
|
|
70
56
|
res.end();
|
|
71
57
|
|
|
72
|
-
console.log("[RESPOND] Redirect response sent");
|
|
73
|
-
|
|
74
58
|
return result;
|
|
75
59
|
}
|
|
76
60
|
|
|
61
|
+
// ----------------------------
|
|
62
|
+
// NORMAL JSON RESPONSE
|
|
63
|
+
// ----------------------------
|
|
64
|
+
|
|
77
65
|
const response = {
|
|
78
66
|
ok: result?.ok ?? false,
|
|
79
67
|
};
|
|
@@ -102,17 +90,12 @@ const respond = (result, res) => {
|
|
|
102
90
|
|
|
103
91
|
const status = result?.status || (result?.ok ? 200 : 403);
|
|
104
92
|
|
|
105
|
-
console.log("[RESPOND] HTTP status:", status);
|
|
106
|
-
console.log("[RESPOND] Response:", response);
|
|
107
|
-
|
|
108
93
|
res.statusCode = status;
|
|
109
94
|
|
|
110
95
|
res.setHeader("Content-Type", "application/json");
|
|
111
96
|
|
|
112
97
|
res.end(JSON.stringify(response));
|
|
113
98
|
|
|
114
|
-
console.log("[RESPOND] Response sent");
|
|
115
|
-
|
|
116
99
|
res.gp?.resource?.compute_response(res);
|
|
117
100
|
|
|
118
101
|
return response;
|
|
@@ -121,156 +104,56 @@ const respond = (result, res) => {
|
|
|
121
104
|
const version_middleware = async (req, res, routers) => {
|
|
122
105
|
let error_stage = "Version Middleware";
|
|
123
106
|
|
|
124
|
-
console.log("==================================================");
|
|
125
|
-
console.log("[MIDDLEWARE] REQUEST START");
|
|
126
|
-
console.log("==================================================");
|
|
127
|
-
|
|
128
|
-
console.log("[MIDDLEWARE] Method:", req.method);
|
|
129
|
-
console.log("[MIDDLEWARE] URL:", req.url);
|
|
130
|
-
console.log("[MIDDLEWARE] Headers:", req.headers);
|
|
131
|
-
|
|
132
107
|
try {
|
|
133
|
-
// --------------------------------------------------
|
|
134
|
-
// CORS
|
|
135
|
-
// --------------------------------------------------
|
|
136
|
-
|
|
137
|
-
error_stage = "CORS";
|
|
138
|
-
|
|
139
|
-
console.log("[MIDDLEWARE] Applying CORS");
|
|
140
|
-
|
|
141
108
|
applyCors(req, res);
|
|
142
109
|
|
|
143
|
-
console.log("[MIDDLEWARE] CORS applied");
|
|
144
|
-
|
|
145
|
-
// --------------------------------------------------
|
|
146
|
-
// OPTIONS
|
|
147
|
-
// --------------------------------------------------
|
|
148
|
-
|
|
149
110
|
if (req.method === "OPTIONS") {
|
|
150
|
-
console.log("[MIDDLEWARE] OPTIONS request");
|
|
151
|
-
|
|
152
111
|
res.statusCode = 204;
|
|
153
|
-
|
|
154
|
-
console.log("[MIDDLEWARE] Returning 204");
|
|
155
|
-
|
|
156
112
|
return res.end();
|
|
157
113
|
}
|
|
158
114
|
|
|
159
|
-
// --------------------------------------------------
|
|
160
|
-
// URL
|
|
161
|
-
// --------------------------------------------------
|
|
162
|
-
|
|
163
|
-
error_stage = "URL Parsing";
|
|
164
|
-
|
|
165
|
-
console.log("[MIDDLEWARE] Parsing URL");
|
|
166
|
-
|
|
167
115
|
const parsed_url = new URL(req.url, `http://${req.headers.host}`);
|
|
168
|
-
|
|
169
116
|
const pathname = parsed_url.pathname;
|
|
170
117
|
|
|
171
|
-
|
|
172
|
-
|
|
118
|
+
// ⬇️ query params captured for every method now
|
|
173
119
|
const query = Object.fromEntries(parsed_url.searchParams.entries());
|
|
174
|
-
|
|
175
120
|
req.query = query;
|
|
176
121
|
|
|
177
|
-
console.log("[MIDDLEWARE] Query:", query);
|
|
178
|
-
|
|
179
|
-
// --------------------------------------------------
|
|
180
|
-
// REQUEST ID
|
|
181
|
-
// --------------------------------------------------
|
|
182
|
-
|
|
183
122
|
req.request_id = crypto.randomUUID();
|
|
184
|
-
|
|
185
|
-
console.log("[MIDDLEWARE] Request ID:", req.request_id);
|
|
186
|
-
|
|
187
|
-
console.log("[MIDDLEWARE] Sending incoming_request hook");
|
|
188
|
-
|
|
189
123
|
routers.gp?.resource?.incoming_request(req);
|
|
190
|
-
|
|
191
124
|
res.gp = routers.gp;
|
|
192
125
|
res.request_id = req.request_id;
|
|
193
126
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
// --------------------------------------------------
|
|
197
|
-
// STATIC / API VERSION
|
|
198
|
-
// --------------------------------------------------
|
|
199
|
-
|
|
200
|
-
error_stage = "Version Resolution";
|
|
201
|
-
|
|
127
|
+
// STATIC ROUTE DETECTION
|
|
202
128
|
let version;
|
|
203
129
|
|
|
204
130
|
const versionMatch = pathname.match(/^\/api\/([^\/]+)(\/|$)/i);
|
|
205
|
-
|
|
206
|
-
console.log("[MIDDLEWARE] Version match:", versionMatch);
|
|
207
|
-
|
|
208
131
|
if (req.method === "GET") {
|
|
209
|
-
console.log("[MIDDLEWARE] GET request");
|
|
210
|
-
|
|
211
132
|
if (routers.static_path && pathname.startsWith(routers.static_path)) {
|
|
212
|
-
console.log("[MIDDLEWARE] Static route detected:", pathname);
|
|
213
|
-
|
|
214
133
|
res.gp = routers.gp;
|
|
215
134
|
|
|
216
135
|
return serveStaticFile(req, res);
|
|
217
136
|
}
|
|
218
137
|
|
|
219
138
|
version = "__GET__";
|
|
220
|
-
|
|
221
|
-
console.log("[MIDDLEWARE] GET version:", version);
|
|
222
139
|
} else if (versionMatch) {
|
|
223
140
|
version = versionMatch[1];
|
|
224
|
-
|
|
225
|
-
console.log("[MIDDLEWARE] Version extracted from URL:", version);
|
|
226
141
|
} else {
|
|
227
142
|
version = req.headers["x-api-version"] || "v1";
|
|
228
|
-
|
|
229
|
-
console.log("[MIDDLEWARE] Version from header/default:", version);
|
|
230
143
|
}
|
|
231
144
|
|
|
232
|
-
|
|
233
|
-
// PLATFORM
|
|
234
|
-
// --------------------------------------------------
|
|
235
|
-
|
|
236
|
-
if (req.headers["x-platform"]) {
|
|
237
|
-
console.log(
|
|
238
|
-
"[MIDDLEWARE] Original x-platform:",
|
|
239
|
-
req.headers["x-platform"],
|
|
240
|
-
);
|
|
241
|
-
|
|
145
|
+
if (req.headers["x-platform"])
|
|
242
146
|
req.headers["x-platform"] = routers.gp.uri_default_domain(
|
|
243
147
|
req.headers["x-platform"],
|
|
244
148
|
);
|
|
245
149
|
|
|
246
|
-
console.log(
|
|
247
|
-
"[MIDDLEWARE] Normalized x-platform:",
|
|
248
|
-
req.headers["x-platform"],
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// --------------------------------------------------
|
|
253
|
-
// VERSION ROUTER
|
|
254
|
-
// --------------------------------------------------
|
|
255
|
-
|
|
256
|
-
console.log("[MIDDLEWARE] Getting version router:", version);
|
|
257
|
-
|
|
258
150
|
let versionRouter = await routers.get_version(version);
|
|
259
151
|
|
|
260
|
-
console.log("[MIDDLEWARE] Version router found:", !!versionRouter);
|
|
261
|
-
|
|
262
152
|
if (!versionRouter) {
|
|
263
|
-
console.log("[MIDDLEWARE] Falling back to wildcard version");
|
|
264
|
-
|
|
265
153
|
version = "*";
|
|
266
|
-
|
|
267
154
|
versionRouter = await routers.get_version(version);
|
|
268
155
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
if (!versionRouter) {
|
|
272
|
-
console.log("[MIDDLEWARE] No valid version router");
|
|
273
|
-
|
|
156
|
+
if (!versionRouter)
|
|
274
157
|
return respond(
|
|
275
158
|
{
|
|
276
159
|
ok: false,
|
|
@@ -279,73 +162,38 @@ const version_middleware = async (req, res, routers) => {
|
|
|
279
162
|
},
|
|
280
163
|
res,
|
|
281
164
|
);
|
|
282
|
-
}
|
|
283
165
|
}
|
|
284
166
|
|
|
285
|
-
console.log("[MIDDLEWARE] Version router config:", versionRouter.config);
|
|
286
|
-
|
|
287
|
-
// --------------------------------------------------
|
|
288
|
-
// BODY
|
|
289
|
-
// --------------------------------------------------
|
|
290
|
-
|
|
291
|
-
error_stage = "Body Parser";
|
|
292
|
-
|
|
293
167
|
let body = {};
|
|
294
168
|
|
|
295
169
|
if (req.method === "GET") {
|
|
296
|
-
|
|
297
|
-
|
|
170
|
+
// GET keeps existing behavior: query params double as body
|
|
298
171
|
body = query;
|
|
299
172
|
} else {
|
|
300
173
|
const contentType = req.headers["content-type"] || "";
|
|
301
174
|
|
|
302
|
-
console.log("[BODY] Content-Type:", contentType);
|
|
303
|
-
|
|
304
175
|
if (contentType.includes("multipart/form-data")) {
|
|
305
|
-
console.log("[BODY] Multipart request detected");
|
|
306
|
-
|
|
307
176
|
body = await parseMultipart(req, routers.gp);
|
|
308
|
-
|
|
309
|
-
console.log("[BODY] Multipart body:", body);
|
|
310
177
|
} else {
|
|
311
|
-
|
|
312
|
-
|
|
178
|
+
error_stage = "Body Parser";
|
|
313
179
|
body = await getBody(req);
|
|
314
|
-
|
|
315
|
-
console.log("[BODY] Parsed body:", body);
|
|
316
180
|
}
|
|
317
181
|
}
|
|
318
182
|
|
|
319
|
-
// --------------------------------------------------
|
|
320
|
-
// OLD ROUTER
|
|
321
|
-
// --------------------------------------------------
|
|
322
|
-
|
|
323
183
|
if (versionRouter.config?.is_old) {
|
|
324
|
-
console.log("[ROUTER] Old router detected");
|
|
325
|
-
|
|
326
184
|
req.body = body;
|
|
327
|
-
|
|
328
|
-
console.log("[ROUTER] Passing directly to old routes");
|
|
329
|
-
|
|
330
185
|
return versionRouter.routes(req, res);
|
|
331
186
|
}
|
|
332
187
|
|
|
333
188
|
const router = routers;
|
|
334
189
|
|
|
335
|
-
//
|
|
336
|
-
// ROUTE NORMALIZATION
|
|
337
|
-
// --------------------------------------------------
|
|
338
|
-
|
|
339
|
-
error_stage = "Route Resolution";
|
|
340
|
-
|
|
190
|
+
// remove query string from route
|
|
341
191
|
let routePath = pathname;
|
|
342
192
|
|
|
343
|
-
|
|
344
|
-
|
|
193
|
+
// Remove /api/<version>
|
|
345
194
|
routePath = routePath.replace(/^\/api\/[^\/]+(?=\/|$)/i, "");
|
|
346
195
|
|
|
347
|
-
|
|
348
|
-
|
|
196
|
+
// Ensure leading slash exists
|
|
349
197
|
if (!routePath.startsWith("/")) {
|
|
350
198
|
routePath = "/" + routePath;
|
|
351
199
|
}
|
|
@@ -354,57 +202,20 @@ const version_middleware = async (req, res, routers) => {
|
|
|
354
202
|
|
|
355
203
|
let name = routePath.replace(/^\//, "");
|
|
356
204
|
|
|
357
|
-
console.log("[ROUTE] Final route path:", routePath);
|
|
358
|
-
console.log("[ROUTE] Route name:", name);
|
|
359
|
-
|
|
360
|
-
// --------------------------------------------------
|
|
361
|
-
// WEBHOOK
|
|
362
|
-
// --------------------------------------------------
|
|
363
|
-
|
|
364
205
|
const isWebhook = req.headers["is-webhook"];
|
|
365
206
|
|
|
366
|
-
console.log("[WEBHOOK] Is webhook:", isWebhook);
|
|
367
|
-
|
|
368
|
-
// --------------------------------------------------
|
|
369
207
|
// SECURITY
|
|
370
|
-
// --------------------------------------------------
|
|
371
|
-
|
|
372
208
|
error_stage = "Header";
|
|
373
|
-
|
|
374
|
-
console.log("[SECURITY] Running security handler");
|
|
375
|
-
|
|
376
209
|
const securityResult = await router.handle_security(name, req);
|
|
377
210
|
|
|
378
|
-
console.log("[SECURITY] Result:", securityResult);
|
|
379
|
-
|
|
380
211
|
if (securityResult !== true) {
|
|
381
|
-
console.log("[SECURITY] Request rejected");
|
|
382
|
-
|
|
383
212
|
return respond(securityResult, res);
|
|
384
213
|
}
|
|
385
214
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
// --------------------------------------------------
|
|
389
|
-
// DATABASE
|
|
390
|
-
// --------------------------------------------------
|
|
391
|
-
|
|
392
|
-
error_stage = "Database";
|
|
393
|
-
|
|
394
|
-
console.log("[DB] Resolving database");
|
|
395
|
-
|
|
215
|
+
// DB
|
|
396
216
|
const db = await router.resolve_db(req);
|
|
397
217
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
// --------------------------------------------------
|
|
401
|
-
// BEFORE HOOK
|
|
402
|
-
// --------------------------------------------------
|
|
403
|
-
|
|
404
|
-
error_stage = "Before Hook";
|
|
405
|
-
|
|
406
|
-
console.log("[BEFORE] Calling handle_before");
|
|
407
|
-
|
|
218
|
+
// WEBHOOK PRIOR
|
|
408
219
|
const callback = await router.handle_before(
|
|
409
220
|
{
|
|
410
221
|
req,
|
|
@@ -416,57 +227,20 @@ const version_middleware = async (req, res, routers) => {
|
|
|
416
227
|
router.gp,
|
|
417
228
|
);
|
|
418
229
|
|
|
419
|
-
|
|
230
|
+
if (callback?.response) return respond(callback.response, res);
|
|
420
231
|
|
|
421
|
-
if (callback?.
|
|
422
|
-
console.log("[BEFORE] Callback supplied response");
|
|
232
|
+
if (callback?.body) body = callback.body;
|
|
423
233
|
|
|
424
|
-
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
if (callback?.body) {
|
|
428
|
-
console.log("[BEFORE] Replacing body");
|
|
429
|
-
|
|
430
|
-
body = callback.body;
|
|
431
|
-
|
|
432
|
-
console.log("[BEFORE] New body:", body);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
if (callback?.query) {
|
|
436
|
-
console.log("[BEFORE] Replacing query");
|
|
437
|
-
|
|
438
|
-
// query must be let if this is enabled
|
|
439
|
-
// query = callback.query;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
if (callback?.headers) {
|
|
443
|
-
console.log("[BEFORE] Replacing headers");
|
|
444
|
-
|
|
445
|
-
req.headers = callback.headers;
|
|
446
|
-
|
|
447
|
-
console.log("[BEFORE] New headers:", req.headers);
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
if (callback?.db) {
|
|
451
|
-
console.log("[BEFORE] Replacing database");
|
|
452
|
-
|
|
453
|
-
db.db = callback.db;
|
|
454
|
-
}
|
|
234
|
+
if (callback?.query) query = callback.query; // only if you make `query` a `let` — see note below
|
|
455
235
|
|
|
456
|
-
if (callback?.
|
|
457
|
-
console.log("[BEFORE] Replacing route:", callback.route);
|
|
236
|
+
if (callback?.headers) req.headers = callback.headers;
|
|
458
237
|
|
|
459
|
-
|
|
460
|
-
|
|
238
|
+
if (callback?.db) db.db = callback.db;
|
|
239
|
+
if (callback?.route) name = callback.route;
|
|
461
240
|
|
|
462
|
-
// --------------------------------------------------
|
|
463
241
|
// EXECUTE
|
|
464
|
-
// --------------------------------------------------
|
|
465
242
|
|
|
466
243
|
error_stage = "Handler";
|
|
467
|
-
|
|
468
|
-
console.log("[EXECUTE] Preparing handler payload");
|
|
469
|
-
|
|
470
244
|
let payload = {
|
|
471
245
|
body,
|
|
472
246
|
query,
|
|
@@ -480,27 +254,15 @@ const version_middleware = async (req, res, routers) => {
|
|
|
480
254
|
params: {},
|
|
481
255
|
memory: router.gp.memory,
|
|
482
256
|
};
|
|
483
|
-
|
|
484
|
-
console.log("[EXECUTE] Payload:", payload);
|
|
485
|
-
|
|
486
|
-
console.log("[EXECUTE] Executing route:", name);
|
|
487
|
-
|
|
488
257
|
const result = await router.execute(name, payload);
|
|
489
258
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
// --------------------------------------------------
|
|
493
|
-
// AFTER HOOK
|
|
494
|
-
// --------------------------------------------------
|
|
495
|
-
|
|
259
|
+
// WEBHOOK AFTER
|
|
496
260
|
if (!isWebhook && version !== "*") {
|
|
497
261
|
error_stage = "Webhook";
|
|
498
262
|
|
|
499
|
-
console.log("[AFTER] Calling handle_after");
|
|
500
|
-
|
|
501
263
|
await router.handle_after(
|
|
502
264
|
{
|
|
503
|
-
req,
|
|
265
|
+
req: payload,
|
|
504
266
|
result,
|
|
505
267
|
headers: req.headers,
|
|
506
268
|
version,
|
|
@@ -509,32 +271,11 @@ const version_middleware = async (req, res, routers) => {
|
|
|
509
271
|
},
|
|
510
272
|
router.gp,
|
|
511
273
|
);
|
|
512
|
-
|
|
513
|
-
console.log("[AFTER] handle_after completed");
|
|
514
|
-
} else {
|
|
515
|
-
console.log("[AFTER] Skipping handle_after", {
|
|
516
|
-
isWebhook,
|
|
517
|
-
version,
|
|
518
|
-
});
|
|
519
274
|
}
|
|
520
275
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
const response = respond(result, res);
|
|
524
|
-
|
|
525
|
-
console.log("[MIDDLEWARE] Final response:", response);
|
|
526
|
-
|
|
527
|
-
console.log("==================================================");
|
|
528
|
-
console.log("[MIDDLEWARE] REQUEST END");
|
|
529
|
-
console.log("==================================================");
|
|
530
|
-
|
|
531
|
-
return response;
|
|
276
|
+
return respond(result, res);
|
|
532
277
|
} catch (err) {
|
|
533
|
-
console.error(
|
|
534
|
-
console.error(`[MIDDLEWARE] ${error_stage} Error:`, err);
|
|
535
|
-
console.error("[MIDDLEWARE] Error message:", err?.message);
|
|
536
|
-
console.error("[MIDDLEWARE] Error stack:", err?.stack);
|
|
537
|
-
console.error("==================================================");
|
|
278
|
+
console.error(`${error_stage} Error:`, err);
|
|
538
279
|
|
|
539
280
|
return respond(
|
|
540
281
|
{
|