hadars 0.1.3 → 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 +19 -14
- package/dist/ssr-watch.js +4 -0
- package/package.json +1 -1
- package/src/build.ts +2 -0
- package/src/ssr-watch.ts +4 -0
- 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,
|
|
@@ -994,7 +997,9 @@ var dev = async (options) => {
|
|
|
994
997
|
`--entry=${entry}`,
|
|
995
998
|
`--outDir=${HadarsFolder}`,
|
|
996
999
|
`--outFile=${SSR_FILENAME}`,
|
|
997
|
-
`--base=${baseURL}
|
|
1000
|
+
`--base=${baseURL}`,
|
|
1001
|
+
...options.swcPlugins ? [`--swcPlugins=${JSON.stringify(options.swcPlugins)}`] : [],
|
|
1002
|
+
...options.define ? [`--define=${JSON.stringify(options.define)}`] : []
|
|
998
1003
|
], { stdio: "pipe" });
|
|
999
1004
|
child.stdin?.end();
|
|
1000
1005
|
const stdoutWebStream = nodeReadableToWebStream(child.stdout);
|
package/dist/ssr-watch.js
CHANGED
|
@@ -312,6 +312,8 @@ var entry = argv["entry"];
|
|
|
312
312
|
var outDir = argv["outDir"] || ".hadars";
|
|
313
313
|
var outFile = argv["outFile"] || "index.ssr.js";
|
|
314
314
|
var base = argv["base"] || "";
|
|
315
|
+
var swcPlugins = argv["swcPlugins"] ? JSON.parse(argv["swcPlugins"]) : void 0;
|
|
316
|
+
var define = argv["define"] ? JSON.parse(argv["define"]) : void 0;
|
|
315
317
|
if (!entry) {
|
|
316
318
|
console.error("ssr-watch: missing --entry argument");
|
|
317
319
|
process.exit(1);
|
|
@@ -331,6 +333,8 @@ if (!entry) {
|
|
|
331
333
|
base,
|
|
332
334
|
mode: "development",
|
|
333
335
|
watch: true,
|
|
336
|
+
swcPlugins,
|
|
337
|
+
define,
|
|
334
338
|
onChange: () => {
|
|
335
339
|
console.log("ssr-watch: SSR rebuilt");
|
|
336
340
|
}
|
package/package.json
CHANGED
package/src/build.ts
CHANGED
|
@@ -416,6 +416,8 @@ export const dev = async (options: HadarsRuntimeOptions) => {
|
|
|
416
416
|
`--outDir=${HadarsFolder}`,
|
|
417
417
|
`--outFile=${SSR_FILENAME}`,
|
|
418
418
|
`--base=${baseURL}`,
|
|
419
|
+
...(options.swcPlugins ? [`--swcPlugins=${JSON.stringify(options.swcPlugins)}`] : []),
|
|
420
|
+
...(options.define ? [`--define=${JSON.stringify(options.define)}`] : []),
|
|
419
421
|
], { stdio: 'pipe' });
|
|
420
422
|
child.stdin?.end();
|
|
421
423
|
|
package/src/ssr-watch.ts
CHANGED
|
@@ -19,6 +19,8 @@ const entry = argv['entry'];
|
|
|
19
19
|
const outDir = argv['outDir'] || '.hadars';
|
|
20
20
|
const outFile = argv['outFile'] || 'index.ssr.js';
|
|
21
21
|
const base = argv['base'] || '';
|
|
22
|
+
const swcPlugins = argv['swcPlugins'] ? JSON.parse(argv['swcPlugins']) : undefined;
|
|
23
|
+
const define = argv['define'] ? JSON.parse(argv['define']) : undefined;
|
|
22
24
|
|
|
23
25
|
if (!entry) {
|
|
24
26
|
console.error('ssr-watch: missing --entry argument');
|
|
@@ -40,6 +42,8 @@ if (!entry) {
|
|
|
40
42
|
base,
|
|
41
43
|
mode: 'development',
|
|
42
44
|
watch: true,
|
|
45
|
+
swcPlugins,
|
|
46
|
+
define,
|
|
43
47
|
onChange: () => {
|
|
44
48
|
console.log('ssr-watch: SSR rebuilt');
|
|
45
49
|
}
|
|
@@ -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,
|