@qelos/plugin-netlify-api 1.1.0 → 1.1.1
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/functions/qelos-api-proxy.ts +45 -4
- package/package.json +1 -1
|
@@ -29,12 +29,18 @@ const ADD_BYPASS_ADMIN_HEADER =
|
|
|
29
29
|
|
|
30
30
|
export const handler: Handler = (event) =>
|
|
31
31
|
new Promise((resolve) => {
|
|
32
|
-
// Build forwarded headers
|
|
32
|
+
// Build forwarded headers. Upstream Qelos expects Host = public app domain (e.g. app.auto.qelos.io),
|
|
33
|
+
// not the raw API IP — use the visitor-facing host (case-insensitive headers; rawUrl fallback).
|
|
33
34
|
const headers: Record<string, string> = {};
|
|
34
|
-
for (const [key, val] of Object.entries(event.headers)) {
|
|
35
|
-
if (val
|
|
35
|
+
for (const [key, val] of Object.entries(event.headers ?? {})) {
|
|
36
|
+
if (val == null || val === "") continue;
|
|
37
|
+
if (Array.isArray(val)) {
|
|
38
|
+
headers[key] = val.join(", ");
|
|
39
|
+
} else if (typeof val === "string") {
|
|
40
|
+
headers[key] = val;
|
|
41
|
+
}
|
|
36
42
|
}
|
|
37
|
-
headers["host"] = event
|
|
43
|
+
headers["host"] = publicHostForUpstream(event);
|
|
38
44
|
if (ADD_BYPASS_ADMIN_HEADER) {
|
|
39
45
|
headers["x-bypass-admin"] = "true";
|
|
40
46
|
}
|
|
@@ -86,3 +92,38 @@ export const handler: Handler = (event) =>
|
|
|
86
92
|
if (body) proxyReq.write(body);
|
|
87
93
|
proxyReq.end();
|
|
88
94
|
});
|
|
95
|
+
|
|
96
|
+
function publicHostForUpstream(event: {
|
|
97
|
+
headers: Record<string, string | string[] | undefined>;
|
|
98
|
+
rawUrl: string;
|
|
99
|
+
}): string {
|
|
100
|
+
const forwarded = pickHeader(event.headers, "x-forwarded-host");
|
|
101
|
+
if (forwarded) {
|
|
102
|
+
return forwarded.split(",")[0].trim();
|
|
103
|
+
}
|
|
104
|
+
const h = pickHeader(event.headers, "host");
|
|
105
|
+
if (h) {
|
|
106
|
+
return h;
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
return new URL(event.rawUrl).hostname;
|
|
110
|
+
} catch {
|
|
111
|
+
return PROXY_HOST;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function pickHeader(
|
|
116
|
+
headers: Record<string, string | string[] | undefined>,
|
|
117
|
+
name: string,
|
|
118
|
+
): string | undefined {
|
|
119
|
+
const want = name.toLowerCase();
|
|
120
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
121
|
+
if (k.toLowerCase() !== want) continue;
|
|
122
|
+
if (v == null) continue;
|
|
123
|
+
const s = Array.isArray(v) ? v[0] : v;
|
|
124
|
+
if (typeof s === "string" && s.length > 0) {
|
|
125
|
+
return s;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|