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