@qwik.dev/router 2.0.0-beta.4 → 2.0.0-beta.41
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/adapters/static/vite.d.ts +1 -1
- package/lib/adapters/azure-swa/vite/index.d.ts +2 -2
- package/lib/adapters/azure-swa/vite/index.mjs +44 -58
- package/lib/adapters/bun-server/vite/index.d.ts +2 -2
- package/lib/adapters/bun-server/vite/index.mjs +16 -23
- package/lib/adapters/cloud-run/vite/index.d.ts +2 -2
- package/lib/adapters/cloud-run/vite/index.mjs +15 -20
- package/lib/adapters/cloudflare-pages/vite/index.d.ts +2 -2
- package/lib/adapters/cloudflare-pages/vite/index.mjs +45 -77
- package/lib/adapters/deno-server/vite/index.d.ts +2 -2
- package/lib/adapters/deno-server/vite/index.mjs +25 -35
- package/lib/adapters/netlify-edge/vite/index.d.ts +2 -2
- package/lib/adapters/netlify-edge/vite/index.mjs +49 -89
- package/lib/adapters/node-server/vite/index.d.ts +2 -2
- package/lib/adapters/node-server/vite/index.mjs +16 -23
- package/lib/adapters/shared/vite/index.d.ts +9 -22
- package/lib/adapters/shared/vite/index.mjs +280 -333
- package/lib/adapters/ssg/vite/index.d.ts +13 -0
- package/lib/adapters/ssg/vite/index.mjs +13 -0
- package/lib/adapters/vercel-edge/vite/index.d.ts +3 -3
- package/lib/adapters/vercel-edge/vite/index.mjs +73 -80
- package/lib/chunks/dev-preloaded-route-loader.mjs +2 -0
- package/lib/chunks/format.mjs +24 -0
- package/lib/chunks/fs.mjs +140 -0
- package/lib/chunks/head.qwik.mjs +1202 -0
- package/lib/chunks/http-error.qwik.mjs +27 -0
- package/lib/chunks/pathname.mjs +57 -0
- package/lib/chunks/request-body-limit.mjs +27 -0
- package/lib/chunks/request-path.mjs +49 -0
- package/lib/chunks/routes.mjs +26 -0
- package/lib/chunks/server-error.mjs +16 -0
- package/lib/chunks/system.mjs +265 -0
- package/lib/chunks/url.mjs +59 -0
- package/lib/chunks/user-response.mjs +1730 -0
- package/lib/chunks/worker-thread.mjs +264 -0
- package/lib/index.d.ts +631 -174
- package/lib/index.qwik.mjs +1656 -1964
- package/lib/middleware/aws-lambda/index.d.ts +15 -16
- package/lib/middleware/aws-lambda/index.mjs +45 -54
- package/lib/middleware/azure-swa/index.mjs +85 -295
- package/lib/middleware/bun/index.d.ts +20 -5
- package/lib/middleware/bun/index.mjs +119 -205
- package/lib/middleware/cloudflare-pages/index.mjs +88 -105
- package/lib/middleware/deno/index.d.ts +22 -5
- package/lib/middleware/deno/index.mjs +105 -192
- package/lib/middleware/firebase/index.mjs +22 -35
- package/lib/middleware/netlify-edge/index.mjs +63 -81
- package/lib/middleware/node/index.d.ts +17 -5
- package/lib/middleware/node/index.mjs +299 -273
- package/lib/middleware/request-handler/index.d.ts +206 -99
- package/lib/middleware/request-handler/index.mjs +105 -1648
- package/lib/middleware/vercel-edge/index.mjs +76 -101
- package/lib/modules.d.ts +11 -16
- package/lib/service-worker/index.mjs +13 -0
- package/lib/{static → ssg}/index.d.ts +46 -22
- package/lib/ssg/index.mjs +257 -0
- package/lib/vite/index.d.ts +58 -10
- package/lib/vite/index.mjs +2471 -27446
- package/modules.d.ts +11 -16
- package/package.json +62 -70
- package/ssg.d.ts +2 -0
- package/static.d.ts +1 -1
- package/lib/adapters/azure-swa/vite/index.cjs +0 -96
- package/lib/adapters/bun-server/vite/index.cjs +0 -50
- package/lib/adapters/cloud-run/vite/index.cjs +0 -47
- package/lib/adapters/cloudflare-pages/vite/index.cjs +0 -115
- package/lib/adapters/deno-server/vite/index.cjs +0 -62
- package/lib/adapters/netlify-edge/vite/index.cjs +0 -129
- package/lib/adapters/node-server/vite/index.cjs +0 -50
- package/lib/adapters/shared/vite/index.cjs +0 -378
- package/lib/adapters/static/vite/index.cjs +0 -368
- package/lib/adapters/static/vite/index.d.ts +0 -10
- package/lib/adapters/static/vite/index.mjs +0 -331
- package/lib/adapters/vercel-edge/vite/index.cjs +0 -118
- package/lib/index.qwik.cjs +0 -2022
- package/lib/middleware/node/index.cjs +0 -314
- package/lib/middleware/request-handler/index.cjs +0 -1691
- package/lib/service-worker.cjs +0 -17
- package/lib/service-worker.mjs +0 -15
- package/lib/static/deno.mjs +0 -8
- package/lib/static/index.cjs +0 -67
- package/lib/static/index.mjs +0 -48
- package/lib/static/node.cjs +0 -1126
- package/lib/static/node.mjs +0 -1088
- package/lib/vite/index.cjs +0 -27525
- package/middleware/request-handler/generated/not-found-paths.ts +0 -7
- package/middleware/request-handler/generated/static-paths.ts +0 -35
|
@@ -1,208 +1,122 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import { isStaticPath } from "@qwik-router-static-paths";
|
|
4
|
-
import { _deserialize, _serialize, _verifySerializable } from "@qwik.dev/core/internal";
|
|
1
|
+
import { n as MIME_TYPES, t as normalizeRequestUrl } from "../../chunks/url.mjs";
|
|
2
|
+
import { isDev } from "@qwik.dev/core/build";
|
|
5
3
|
import { setServerPlatform } from "@qwik.dev/core/server";
|
|
6
|
-
import {
|
|
7
|
-
_TextEncoderStream_polyfill,
|
|
8
|
-
mergeHeadersCookies,
|
|
9
|
-
requestHandler
|
|
10
|
-
} from "../request-handler/index.mjs";
|
|
11
4
|
import { extname, join } from "node:path";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
asx: "video/x-ms-asf",
|
|
19
|
-
avi: "video/x-msvideo",
|
|
20
|
-
avif: "image/avif",
|
|
21
|
-
bmp: "image/x-ms-bmp",
|
|
22
|
-
css: "text/css",
|
|
23
|
-
flv: "video/x-flv",
|
|
24
|
-
gif: "image/gif",
|
|
25
|
-
htm: "text/html",
|
|
26
|
-
html: "text/html",
|
|
27
|
-
ico: "image/x-icon",
|
|
28
|
-
jng: "image/x-jng",
|
|
29
|
-
jpeg: "image/jpeg",
|
|
30
|
-
jpg: "image/jpeg",
|
|
31
|
-
js: "application/javascript",
|
|
32
|
-
json: "application/json",
|
|
33
|
-
kar: "audio/midi",
|
|
34
|
-
m4a: "audio/x-m4a",
|
|
35
|
-
m4v: "video/x-m4v",
|
|
36
|
-
mid: "audio/midi",
|
|
37
|
-
midi: "audio/midi",
|
|
38
|
-
mng: "video/x-mng",
|
|
39
|
-
mov: "video/quicktime",
|
|
40
|
-
mp3: "audio/mpeg",
|
|
41
|
-
mp4: "video/mp4",
|
|
42
|
-
mpeg: "video/mpeg",
|
|
43
|
-
mpg: "video/mpeg",
|
|
44
|
-
ogg: "audio/ogg",
|
|
45
|
-
pdf: "application/pdf",
|
|
46
|
-
png: "image/png",
|
|
47
|
-
rar: "application/x-rar-compressed",
|
|
48
|
-
shtml: "text/html",
|
|
49
|
-
svg: "image/svg+xml",
|
|
50
|
-
svgz: "image/svg+xml",
|
|
51
|
-
tif: "image/tiff",
|
|
52
|
-
tiff: "image/tiff",
|
|
53
|
-
ts: "video/mp2t",
|
|
54
|
-
txt: "text/plain",
|
|
55
|
-
wbmp: "image/vnd.wap.wbmp",
|
|
56
|
-
webm: "video/webm",
|
|
57
|
-
webp: "image/webp",
|
|
58
|
-
wmv: "video/x-ms-wmv",
|
|
59
|
-
woff: "font/woff",
|
|
60
|
-
woff2: "font/woff2",
|
|
61
|
-
xml: "text/xml",
|
|
62
|
-
zip: "application/zip"
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
// packages/qwik-router/src/middleware/bun/index.ts
|
|
66
|
-
function createQwikRouter(opts) {
|
|
67
|
-
var _a;
|
|
68
|
-
if (opts.qwikCityPlan && !opts.qwikRouterConfig) {
|
|
69
|
-
console.warn("qwikCityPlan is deprecated. Use qwikRouterConfig instead.");
|
|
70
|
-
opts.qwikRouterConfig = opts.qwikCityPlan;
|
|
71
|
-
} else if (!opts.qwikRouterConfig) {
|
|
72
|
-
throw new Error("qwikRouterConfig is required.");
|
|
73
|
-
}
|
|
74
|
-
globalThis.TextEncoderStream ||= _TextEncoderStream_polyfill;
|
|
75
|
-
const qwikSerializer = { _deserialize, _serialize, _verifySerializable };
|
|
76
|
-
if (opts.manifest) {
|
|
77
|
-
setServerPlatform(opts.manifest);
|
|
78
|
-
}
|
|
79
|
-
const staticFolder = ((_a = opts.static) == null ? void 0 : _a.root) ?? join(Bun.fileURLToPath(import.meta.url), "..", "..", "dist");
|
|
80
|
-
async function router(request) {
|
|
81
|
-
try {
|
|
82
|
-
const url = new URL(request.url);
|
|
83
|
-
const serverRequestEv = {
|
|
84
|
-
mode: "server",
|
|
85
|
-
locale: void 0,
|
|
86
|
-
url,
|
|
87
|
-
env: {
|
|
88
|
-
get(key) {
|
|
89
|
-
return Bun.env[key];
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
request,
|
|
93
|
-
getWritableStream: (status, headers, cookies, resolve) => {
|
|
94
|
-
const { readable, writable } = new TransformStream();
|
|
95
|
-
const response = new Response(readable, {
|
|
96
|
-
status,
|
|
97
|
-
headers: mergeHeadersCookies(headers, cookies)
|
|
98
|
-
});
|
|
99
|
-
resolve(response);
|
|
100
|
-
return writable;
|
|
101
|
-
},
|
|
102
|
-
platform: {
|
|
103
|
-
ssr: true
|
|
104
|
-
},
|
|
105
|
-
getClientConn: () => {
|
|
106
|
-
return opts.getClientConn ? opts.getClientConn(request) : {};
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
const handledResponse = await requestHandler(serverRequestEv, opts, qwikSerializer);
|
|
110
|
-
if (handledResponse) {
|
|
111
|
-
handledResponse.completion.then((v) => {
|
|
112
|
-
if (v) {
|
|
113
|
-
console.error(v);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
const response = await handledResponse.response;
|
|
117
|
-
if (response) {
|
|
118
|
-
const status = response.status;
|
|
119
|
-
const location = response.headers.get("Location");
|
|
120
|
-
const isRedirect = status >= 301 && status <= 308 && location;
|
|
121
|
-
if (isRedirect) {
|
|
122
|
-
return new Response(null, response);
|
|
123
|
-
}
|
|
124
|
-
return response;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return null;
|
|
128
|
-
} catch (e) {
|
|
129
|
-
console.error(e);
|
|
130
|
-
return new Response(String(e || "Error"), {
|
|
131
|
-
status: 500,
|
|
132
|
-
headers: { "Content-Type": "text/plain; charset=utf-8", "X-Error": "bun-server" }
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
const notFound = async (request) => {
|
|
137
|
-
try {
|
|
138
|
-
const url = new URL(request.url);
|
|
139
|
-
const notFoundHtml = isStaticPath(request.method || "GET", url) ? "Not Found" : getNotFound(url.pathname);
|
|
140
|
-
return new Response(notFoundHtml, {
|
|
141
|
-
status: 404,
|
|
142
|
-
headers: { "Content-Type": "text/html; charset=utf-8", "X-Not-Found": url.pathname }
|
|
143
|
-
});
|
|
144
|
-
} catch (e) {
|
|
145
|
-
console.error(e);
|
|
146
|
-
return new Response(String(e || "Error"), {
|
|
147
|
-
status: 500,
|
|
148
|
-
headers: { "Content-Type": "text/plain; charset=utf-8", "X-Error": "bun-server" }
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
const openStaticFile = async (url) => {
|
|
153
|
-
const pathname = url.pathname;
|
|
154
|
-
const fileName = pathname.slice(url.pathname.lastIndexOf("/"));
|
|
155
|
-
let filePath;
|
|
156
|
-
if (fileName.includes(".")) {
|
|
157
|
-
filePath = join(staticFolder, pathname);
|
|
158
|
-
} else if (opts.qwikRouterConfig.trailingSlash) {
|
|
159
|
-
filePath = join(staticFolder, pathname + "index.html");
|
|
160
|
-
} else {
|
|
161
|
-
filePath = join(staticFolder, pathname, "index.html");
|
|
162
|
-
}
|
|
163
|
-
return {
|
|
164
|
-
filePath,
|
|
165
|
-
content: Bun.file(filePath)
|
|
166
|
-
};
|
|
167
|
-
};
|
|
168
|
-
const staticFile = async (request) => {
|
|
169
|
-
var _a2;
|
|
170
|
-
try {
|
|
171
|
-
const url = new URL(request.url);
|
|
172
|
-
if (isStaticPath(request.method || "GET", url)) {
|
|
173
|
-
const { filePath, content } = await openStaticFile(url);
|
|
174
|
-
if (!await content.exists()) {
|
|
175
|
-
return new Response("Not Found", {
|
|
176
|
-
status: 404,
|
|
177
|
-
headers: { "Content-Type": "text/plain; charset=utf-8", "X-Not-Found": url.pathname }
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
const ext = extname(filePath).replace(/^\./, "");
|
|
181
|
-
return new Response(await content.stream(), {
|
|
182
|
-
status: 200,
|
|
183
|
-
headers: {
|
|
184
|
-
"content-type": MIME_TYPES[ext] || "text/plain; charset=utf-8",
|
|
185
|
-
"Cache-Control": ((_a2 = opts.static) == null ? void 0 : _a2.cacheControl) || "max-age=3600"
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
return null;
|
|
190
|
-
} catch (e) {
|
|
191
|
-
console.error(e);
|
|
192
|
-
return new Response(String(e || "Error"), {
|
|
193
|
-
status: 500,
|
|
194
|
-
headers: { "Content-Type": "text/plain; charset=utf-8", "X-Error": "bun-server" }
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
return {
|
|
199
|
-
router,
|
|
200
|
-
notFound,
|
|
201
|
-
staticFile
|
|
202
|
-
};
|
|
5
|
+
import { _TextEncoderStream_polyfill, isStaticPath, mergeHeadersCookies, requestHandler } from "@qwik.dev/router/middleware/request-handler";
|
|
6
|
+
function getRequestUrl(request, opts) {
|
|
7
|
+
const url = new URL(request.url);
|
|
8
|
+
const origin = opts.getOrigin?.(request) ?? Bun.env.ORIGIN;
|
|
9
|
+
if (!origin) return url;
|
|
10
|
+
return normalizeRequestUrl(`${url.pathname}${url.search}${url.hash}`, origin);
|
|
203
11
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
12
|
+
/** @public */ function createQwikRouter(opts) {
|
|
13
|
+
globalThis.TextEncoderStream ||= _TextEncoderStream_polyfill;
|
|
14
|
+
if (opts.manifest) setServerPlatform(opts.manifest);
|
|
15
|
+
const staticFolder = opts.static?.root ?? join(Bun.fileURLToPath(import.meta.url), "..", "..", "dist");
|
|
16
|
+
async function router(request) {
|
|
17
|
+
try {
|
|
18
|
+
const serverRequestEv = {
|
|
19
|
+
mode: "server",
|
|
20
|
+
locale: void 0,
|
|
21
|
+
url: getRequestUrl(request, opts),
|
|
22
|
+
env: { get(key) {
|
|
23
|
+
return Bun.env[key];
|
|
24
|
+
} },
|
|
25
|
+
request,
|
|
26
|
+
getWritableStream: (status, headers, cookies, resolve) => {
|
|
27
|
+
const { readable, writable } = new TransformStream();
|
|
28
|
+
resolve(new Response(readable, {
|
|
29
|
+
status,
|
|
30
|
+
headers: mergeHeadersCookies(headers, cookies)
|
|
31
|
+
}));
|
|
32
|
+
return writable;
|
|
33
|
+
},
|
|
34
|
+
platform: { ssr: true },
|
|
35
|
+
getClientConn: () => {
|
|
36
|
+
return opts.getClientConn ? opts.getClientConn(request) : {};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const handledResponse = await requestHandler(serverRequestEv, opts);
|
|
40
|
+
if (handledResponse) {
|
|
41
|
+
handledResponse.completion.then((completion) => {
|
|
42
|
+
if (completion) console.error(completion);
|
|
43
|
+
});
|
|
44
|
+
const response = await handledResponse.response;
|
|
45
|
+
if (response) {
|
|
46
|
+
const status = response.status;
|
|
47
|
+
const location = response.headers.get("Location");
|
|
48
|
+
if (status >= 301 && status <= 308 && location) return new Response(null, response);
|
|
49
|
+
return response;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.error(e);
|
|
55
|
+
return new Response(isDev ? String(e || "Error") : "Internal Server Error", {
|
|
56
|
+
status: 500,
|
|
57
|
+
headers: {
|
|
58
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
59
|
+
"X-Error": "bun-server"
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** @deprecated `router` handles 404 responses. Will be removed in V3. */ const notFound = async (_request) => {
|
|
65
|
+
return null;
|
|
66
|
+
};
|
|
67
|
+
const openStaticFile = async (url) => {
|
|
68
|
+
const pathname = url.pathname;
|
|
69
|
+
const fileName = pathname.slice(url.pathname.lastIndexOf("/"));
|
|
70
|
+
let filePath;
|
|
71
|
+
if (fileName.includes(".")) filePath = join(staticFolder, pathname);
|
|
72
|
+
else if (!globalThis.__NO_TRAILING_SLASH__) filePath = join(staticFolder, pathname + "index.html");
|
|
73
|
+
else filePath = join(staticFolder, pathname, "index.html");
|
|
74
|
+
return {
|
|
75
|
+
filePath,
|
|
76
|
+
content: Bun.file(filePath)
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
const staticFile = async (request) => {
|
|
80
|
+
try {
|
|
81
|
+
const url = getRequestUrl(request, opts);
|
|
82
|
+
if (isStaticPath(request.method || "GET", url)) {
|
|
83
|
+
const { filePath, content } = await openStaticFile(url);
|
|
84
|
+
if (!await content.exists()) return new Response("Not Found", {
|
|
85
|
+
status: 404,
|
|
86
|
+
headers: {
|
|
87
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
88
|
+
"X-Not-Found": url.pathname
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
const ext = extname(filePath).replace(/^\./, "");
|
|
92
|
+
return new Response(await content.stream(), {
|
|
93
|
+
status: 200,
|
|
94
|
+
headers: {
|
|
95
|
+
"content-type": MIME_TYPES[ext] || "text/plain; charset=utf-8",
|
|
96
|
+
"Cache-Control": opts.static?.cacheControl || "max-age=3600"
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
} catch (e) {
|
|
102
|
+
console.error(e);
|
|
103
|
+
return new Response(isDev ? String(e || "Error") : "Internal Server Error", {
|
|
104
|
+
status: 500,
|
|
105
|
+
headers: {
|
|
106
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
107
|
+
"X-Error": "bun-server"
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
router,
|
|
114
|
+
notFound,
|
|
115
|
+
staticFile
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* @deprecated Use `createQwikRouter` instead. Will be removed in V3
|
|
120
|
+
* @public
|
|
121
|
+
*/ var createQwikCity = createQwikRouter;
|
|
122
|
+
export { createQwikCity, createQwikRouter };
|
|
@@ -1,107 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
import { getNotFound } from "@qwik-router-not-found-paths";
|
|
3
|
-
import { isStaticPath } from "@qwik-router-static-paths";
|
|
4
|
-
import { _deserialize, _serialize, _verifySerializable } from "@qwik.dev/core/internal";
|
|
1
|
+
import { isDev } from "@qwik.dev/core/build";
|
|
5
2
|
import { setServerPlatform } from "@qwik.dev/core/server";
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const notFoundHtml = isStaticPath(request.method || "GET", url) ? "Not Found" : getNotFound(url.pathname);
|
|
89
|
-
return new Response(notFoundHtml, {
|
|
90
|
-
status: 404,
|
|
91
|
-
headers: { "Content-Type": "text/html; charset=utf-8", "X-Not-Found": url.pathname }
|
|
92
|
-
});
|
|
93
|
-
} catch (e) {
|
|
94
|
-
console.error(e);
|
|
95
|
-
return new Response(String(e || "Error"), {
|
|
96
|
-
status: 500,
|
|
97
|
-
headers: { "Content-Type": "text/plain; charset=utf-8", "X-Error": "cloudflare-pages" }
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return onCloudflarePagesFetch;
|
|
3
|
+
import { _TextEncoderStream_polyfill, getErrorHtml, isStaticPath, mergeHeadersCookies, requestHandler } from "@qwik.dev/router/middleware/request-handler";
|
|
4
|
+
/** @public */ function createQwikRouter(opts) {
|
|
5
|
+
try {
|
|
6
|
+
new globalThis.TextEncoderStream();
|
|
7
|
+
} catch {
|
|
8
|
+
globalThis.TextEncoderStream = _TextEncoderStream_polyfill;
|
|
9
|
+
}
|
|
10
|
+
if (opts.manifest) setServerPlatform(opts.manifest);
|
|
11
|
+
async function onCloudflarePagesFetch(request, env, ctx) {
|
|
12
|
+
try {
|
|
13
|
+
const url = new URL(request.url);
|
|
14
|
+
if (isStaticPath(request.method, url)) return env.ASSETS.fetch(request);
|
|
15
|
+
const useCache = url.hostname !== "127.0.0.1" && url.hostname !== "localhost" && url.port === "" && request.method === "GET";
|
|
16
|
+
const cacheKey = new Request(url.href, request);
|
|
17
|
+
const cache = useCache ? await caches.open("custom:qwikrouter") : null;
|
|
18
|
+
if (cache) {
|
|
19
|
+
const cachedResponse = await cache.match(cacheKey);
|
|
20
|
+
if (cachedResponse) return cachedResponse;
|
|
21
|
+
}
|
|
22
|
+
const handledResponse = await requestHandler({
|
|
23
|
+
mode: "server",
|
|
24
|
+
locale: void 0,
|
|
25
|
+
url,
|
|
26
|
+
request,
|
|
27
|
+
env: { get(key) {
|
|
28
|
+
return env[key];
|
|
29
|
+
} },
|
|
30
|
+
getWritableStream: (status, headers, cookies, resolve) => {
|
|
31
|
+
const responseInit = {
|
|
32
|
+
status,
|
|
33
|
+
headers: mergeHeadersCookies(headers, cookies)
|
|
34
|
+
};
|
|
35
|
+
if (status === 204 || status === 205 || status === 304) {
|
|
36
|
+
resolve(new Response(null, responseInit));
|
|
37
|
+
return new WritableStream();
|
|
38
|
+
}
|
|
39
|
+
const { readable, writable } = new TransformStream();
|
|
40
|
+
resolve(new Response(readable, responseInit));
|
|
41
|
+
return writable;
|
|
42
|
+
},
|
|
43
|
+
getClientConn: () => {
|
|
44
|
+
return {
|
|
45
|
+
ip: request.headers.get("CF-connecting-ip") || "",
|
|
46
|
+
country: request.headers.get("CF-IPCountry") || ""
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
platform: {
|
|
50
|
+
request,
|
|
51
|
+
env,
|
|
52
|
+
ctx
|
|
53
|
+
}
|
|
54
|
+
}, opts);
|
|
55
|
+
if (handledResponse) {
|
|
56
|
+
handledResponse.completion.then((completion) => {
|
|
57
|
+
if (completion) console.error(completion);
|
|
58
|
+
});
|
|
59
|
+
const response = await handledResponse.response;
|
|
60
|
+
if (response) {
|
|
61
|
+
if (response.ok && cache && response.headers.has("Cache-Control")) ctx.waitUntil(cache.put(cacheKey, response.clone()));
|
|
62
|
+
return response;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const notFoundHtml = !request.headers.get("accept")?.includes("text/html") || isStaticPath(request.method || "GET", url) ? "Not Found" : getErrorHtml(404, "Not Found");
|
|
66
|
+
return new Response(notFoundHtml, {
|
|
67
|
+
status: 404,
|
|
68
|
+
headers: {
|
|
69
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
70
|
+
"X-Not-Found": url.pathname
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
} catch (e) {
|
|
74
|
+
console.error(e);
|
|
75
|
+
return new Response(isDev ? String(e || "Error") : "Internal Server Error", {
|
|
76
|
+
status: 500,
|
|
77
|
+
headers: {
|
|
78
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
79
|
+
"X-Error": "cloudflare-pages"
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return onCloudflarePagesFetch;
|
|
102
85
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
};
|
|
86
|
+
/**
|
|
87
|
+
* @deprecated Use `createQwikRouter` instead. Will be removed in V3
|
|
88
|
+
* @public
|
|
89
|
+
*/ var createQwikCity = createQwikRouter;
|
|
90
|
+
export { createQwikCity, createQwikRouter };
|
|
@@ -8,11 +8,7 @@ import type { ServerRenderOptions } from '@qwik.dev/router/middleware/request-ha
|
|
|
8
8
|
export declare const createQwikCity: typeof createQwikRouter;
|
|
9
9
|
|
|
10
10
|
/** @public */
|
|
11
|
-
export declare function createQwikRouter(opts: QwikRouterDenoOptions):
|
|
12
|
-
router: (request: Request, info: ServeHandlerInfo) => Promise<Response | null>;
|
|
13
|
-
notFound: (request: Request) => Promise<Response>;
|
|
14
|
-
staticFile: (request: Request) => Promise<Response | null>;
|
|
15
|
-
};
|
|
11
|
+
export declare function createQwikRouter(opts: QwikRouterDenoOptions): QwikRouterDenoMiddleware;
|
|
16
12
|
|
|
17
13
|
/** @public */
|
|
18
14
|
export declare interface NetAddr {
|
|
@@ -27,8 +23,18 @@ export declare interface NetAddr {
|
|
|
27
23
|
*/
|
|
28
24
|
export declare type QwikCityDenoOptions = QwikRouterDenoOptions;
|
|
29
25
|
|
|
26
|
+
/** @public */
|
|
27
|
+
export declare interface QwikRouterDenoMiddleware {
|
|
28
|
+
router: (request: Request, info: ServeHandlerInfo) => Promise<Response | null>;
|
|
29
|
+
/** @deprecated `router` handles 404 responses. Will be removed in V3. */
|
|
30
|
+
notFound: (request: Request) => Promise<Response>;
|
|
31
|
+
staticFile: (request: Request) => Promise<Response | null>;
|
|
32
|
+
}
|
|
33
|
+
|
|
30
34
|
/** @public */
|
|
31
35
|
export declare interface QwikRouterDenoOptions extends ServerRenderOptions {
|
|
36
|
+
/** Maximum request body size in bytes. Defaults to 10 MiB. */
|
|
37
|
+
requestBodyLimit?: number;
|
|
32
38
|
/** Options for serving static files */
|
|
33
39
|
static?: {
|
|
34
40
|
/** The root folder for statics files. Defaults to /dist */
|
|
@@ -36,6 +42,17 @@ export declare interface QwikRouterDenoOptions extends ServerRenderOptions {
|
|
|
36
42
|
/** Set the Cache-Control header for all static files */
|
|
37
43
|
cacheControl?: string;
|
|
38
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* Provide a function that computes the origin of the server, used to resolve relative URLs and
|
|
47
|
+
* validate the request origin against CSRF attacks.
|
|
48
|
+
*
|
|
49
|
+
* When not specified, it defaults to the `ORIGIN` environment variable (if set).
|
|
50
|
+
*
|
|
51
|
+
* If `ORIGIN` is not set, it's derived from the incoming request, which is not recommended for
|
|
52
|
+
* production use.
|
|
53
|
+
*/
|
|
54
|
+
getOrigin?: (request: Request, info?: ServeHandlerInfo) => string | null;
|
|
55
|
+
/** Provide a function that returns a `ClientConn` for the given request. */
|
|
39
56
|
getClientConn?: (request: Request, info: ServeHandlerInfo) => ClientConn;
|
|
40
57
|
}
|
|
41
58
|
|