hadars 0.1.4 → 0.1.5
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/dist/cli.js +16 -13
- package/package.json +1 -1
- package/src/utils/proxyHandler.tsx +16 -13
package/dist/cli.js
CHANGED
|
@@ -48,35 +48,38 @@ var createProxyHandler = (options) => {
|
|
|
48
48
|
}
|
|
49
49
|
const proxyRules = Object.entries(proxy).sort((a, b) => b[0].length - a[0].length);
|
|
50
50
|
return async (req) => {
|
|
51
|
-
if (req.method === "OPTIONS" && options.proxyCORS) {
|
|
52
|
-
return new Response(null, {
|
|
53
|
-
status: 204,
|
|
54
|
-
headers: getCORSHeaders(req)
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
51
|
for (const [path2, target] of proxyRules) {
|
|
58
52
|
if (req.pathname.startsWith(path2)) {
|
|
53
|
+
if (req.method === "OPTIONS" && proxyCORS) {
|
|
54
|
+
return new Response(null, {
|
|
55
|
+
status: 204,
|
|
56
|
+
headers: getCORSHeaders(req)
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
59
|
const targetURL = new URL(target);
|
|
60
60
|
targetURL.pathname = targetURL.pathname.replace(/\/$/, "") + req.pathname.slice(path2.length);
|
|
61
61
|
targetURL.search = req.search;
|
|
62
62
|
const sendHeaders = cloneHeaders(req.headers);
|
|
63
63
|
sendHeaders.set("Host", targetURL.host);
|
|
64
|
+
const hasBody = !["GET", "HEAD"].includes(req.method);
|
|
64
65
|
const proxyReq = new Request(targetURL.toString(), {
|
|
65
66
|
method: req.method,
|
|
66
67
|
headers: sendHeaders,
|
|
67
|
-
body:
|
|
68
|
-
redirect: "follow"
|
|
68
|
+
body: hasBody ? req.body : void 0,
|
|
69
|
+
redirect: "follow",
|
|
70
|
+
// Node.js (undici) requires duplex:'half' when body is a ReadableStream
|
|
71
|
+
...hasBody ? { duplex: "half" } : {}
|
|
69
72
|
});
|
|
70
73
|
const res = await fetch(proxyReq);
|
|
71
|
-
if (proxyCORS) {
|
|
72
|
-
Object.entries(getCORSHeaders(req)).forEach(([key, value]) => {
|
|
73
|
-
res.headers.set(key, value);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
74
|
const body = await res.arrayBuffer();
|
|
77
75
|
const clonedRes = new Headers(res.headers);
|
|
78
76
|
clonedRes.delete("content-length");
|
|
79
77
|
clonedRes.delete("content-encoding");
|
|
78
|
+
if (proxyCORS) {
|
|
79
|
+
Object.entries(getCORSHeaders(req)).forEach(([key, value]) => {
|
|
80
|
+
clonedRes.set(key, value);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
80
83
|
return new Response(body, {
|
|
81
84
|
status: res.status,
|
|
82
85
|
statusText: res.statusText,
|
package/package.json
CHANGED
|
@@ -53,14 +53,14 @@ export const createProxyHandler = (options: HadarsOptions): ProxyHandler => {
|
|
|
53
53
|
const proxyRules = Object.entries(proxy).sort((a, b) => b[0].length - a[0].length);
|
|
54
54
|
|
|
55
55
|
return async (req: HadarsRequest) => {
|
|
56
|
-
if (req.method === 'OPTIONS' && options.proxyCORS) {
|
|
57
|
-
return new Response(null, {
|
|
58
|
-
status: 204,
|
|
59
|
-
headers: getCORSHeaders(req),
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
56
|
for (const [path, target] of proxyRules) {
|
|
63
57
|
if (req.pathname.startsWith(path)) {
|
|
58
|
+
if (req.method === 'OPTIONS' && proxyCORS) {
|
|
59
|
+
return new Response(null, {
|
|
60
|
+
status: 204,
|
|
61
|
+
headers: getCORSHeaders(req),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
64
|
const targetURL = new URL(target);
|
|
65
65
|
targetURL.pathname = targetURL.pathname.replace(/\/$/, '') + req.pathname.slice(path.length);
|
|
66
66
|
targetURL.search = req.search;
|
|
@@ -69,25 +69,28 @@ export const createProxyHandler = (options: HadarsOptions): ProxyHandler => {
|
|
|
69
69
|
// Overwrite the Host header to match the target
|
|
70
70
|
sendHeaders.set('Host', targetURL.host);
|
|
71
71
|
|
|
72
|
+
const hasBody = !['GET', 'HEAD'].includes(req.method);
|
|
72
73
|
const proxyReq = new Request(targetURL.toString(), {
|
|
73
74
|
method: req.method,
|
|
74
75
|
headers: sendHeaders,
|
|
75
|
-
body:
|
|
76
|
+
body: hasBody ? req.body : undefined,
|
|
76
77
|
redirect: 'follow',
|
|
77
|
-
|
|
78
|
+
// Node.js (undici) requires duplex:'half' when body is a ReadableStream
|
|
79
|
+
...(hasBody ? { duplex: 'half' } : {}),
|
|
80
|
+
} as RequestInit);
|
|
78
81
|
|
|
79
82
|
const res = await fetch(proxyReq);
|
|
80
|
-
if (proxyCORS) {
|
|
81
|
-
Object.entries(getCORSHeaders(req)).forEach(([key, value]) => {
|
|
82
|
-
res.headers.set(key, value);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
83
|
// Read the response body
|
|
86
84
|
const body = await res.arrayBuffer();
|
|
87
85
|
// remove content-length and content-encoding headers to avoid issues with modified body
|
|
88
86
|
const clonedRes = new Headers(res.headers);
|
|
89
87
|
clonedRes.delete('content-length');
|
|
90
88
|
clonedRes.delete('content-encoding');
|
|
89
|
+
if (proxyCORS) {
|
|
90
|
+
Object.entries(getCORSHeaders(req)).forEach(([key, value]) => {
|
|
91
|
+
clonedRes.set(key, value);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
91
94
|
// return a new Response with the modified headers and original body
|
|
92
95
|
return new Response(body, {
|
|
93
96
|
status: res.status,
|