astro 1.6.2 → 1.6.4
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/@types/astro.d.ts +1 -0
- package/dist/@types/typed-emitter.d.ts +42 -0
- package/dist/@types/typed-emitter.js +0 -0
- package/dist/cli/index.js +18 -56
- package/dist/core/add/babel.d.ts +3 -1
- package/dist/core/app/index.d.ts +1 -0
- package/dist/core/app/index.js +16 -4
- package/dist/core/build/generate.js +10 -6
- package/dist/core/build/vite-plugin-ssr.js +5 -2
- package/dist/core/compile/compile.d.ts +3 -3
- package/dist/core/compile/compile.js +15 -10
- package/dist/core/compile/style.d.ts +7 -4
- package/dist/core/compile/style.js +65 -12
- package/dist/core/config/config.d.ts +7 -3
- package/dist/core/config/config.js +15 -12
- package/dist/core/config/index.d.ts +2 -2
- package/dist/core/config/index.js +4 -1
- package/dist/core/config/schema.d.ts +69 -69
- package/dist/core/config/settings.d.ts +3 -1
- package/dist/core/config/settings.js +23 -5
- package/dist/core/constants.js +1 -1
- package/dist/core/create-vite.d.ts +4 -1
- package/dist/core/create-vite.js +6 -3
- package/dist/core/dev/container.d.ts +36 -0
- package/dist/core/dev/container.js +109 -0
- package/dist/core/dev/dev.d.ts +24 -0
- package/dist/core/dev/dev.js +55 -0
- package/dist/core/dev/index.d.ts +3 -18
- package/dist/core/dev/index.js +9 -81
- package/dist/core/dev/restart.d.ts +25 -0
- package/dist/core/dev/restart.js +143 -0
- package/dist/core/endpoint/index.d.ts +2 -1
- package/dist/core/endpoint/index.js +12 -1
- package/dist/core/errors/dev/vite.d.ts +3 -2
- package/dist/core/errors/dev/vite.js +4 -6
- package/dist/core/messages.js +2 -2
- package/dist/core/module-loader/index.d.ts +3 -0
- package/dist/core/module-loader/index.js +6 -0
- package/dist/core/module-loader/loader.d.ts +42 -0
- package/dist/core/module-loader/loader.js +39 -0
- package/dist/core/module-loader/vite.d.ts +3 -0
- package/dist/core/module-loader/vite.js +62 -0
- package/dist/core/preview/index.js +2 -1
- package/dist/core/render/dev/css.d.ts +2 -2
- package/dist/core/render/dev/css.js +3 -3
- package/dist/core/render/dev/environment.d.ts +3 -3
- package/dist/core/render/dev/environment.js +3 -3
- package/dist/core/render/dev/index.d.ts +4 -4
- package/dist/core/render/dev/index.js +8 -8
- package/dist/core/render/dev/resolve.d.ts +2 -2
- package/dist/core/render/dev/resolve.js +2 -2
- package/dist/core/render/dev/scripts.d.ts +2 -2
- package/dist/core/render/dev/scripts.js +4 -4
- package/dist/core/render/dev/vite.d.ts +2 -2
- package/dist/core/render/dev/vite.js +5 -5
- package/dist/core/routing/manifest/create.d.ts +10 -3
- package/dist/core/routing/manifest/create.js +7 -6
- package/dist/core/util.d.ts +3 -4
- package/dist/core/util.js +10 -17
- package/dist/vite-plugin-astro/hmr.js +1 -1
- package/dist/vite-plugin-astro/index.js +10 -21
- package/dist/vite-plugin-astro-postprocess/index.js +1 -1
- package/dist/vite-plugin-astro-server/base.d.ts +4 -0
- package/dist/vite-plugin-astro-server/base.js +36 -0
- package/dist/vite-plugin-astro-server/common.d.ts +2 -0
- package/dist/vite-plugin-astro-server/common.js +8 -0
- package/dist/vite-plugin-astro-server/controller.d.ts +26 -0
- package/dist/vite-plugin-astro-server/controller.js +77 -0
- package/dist/vite-plugin-astro-server/index.d.ts +3 -10
- package/dist/vite-plugin-astro-server/index.js +7 -336
- package/dist/vite-plugin-astro-server/plugin.d.ts +11 -0
- package/dist/vite-plugin-astro-server/plugin.js +54 -0
- package/dist/vite-plugin-astro-server/request.d.ts +7 -0
- package/dist/vite-plugin-astro-server/request.js +52 -0
- package/dist/vite-plugin-astro-server/response.d.ts +9 -0
- package/dist/vite-plugin-astro-server/response.js +85 -0
- package/dist/vite-plugin-astro-server/route.d.ts +13 -0
- package/dist/vite-plugin-astro-server/route.js +144 -0
- package/dist/vite-plugin-astro-server/server-state.d.ts +15 -0
- package/dist/vite-plugin-astro-server/server-state.js +42 -0
- package/dist/vite-plugin-jsx/index.js +2 -2
- package/dist/vite-plugin-load-fallback/index.d.ts +11 -0
- package/dist/vite-plugin-load-fallback/index.js +59 -0
- package/dist/vite-plugin-markdown/index.js +1 -1
- package/dist/vite-plugin-markdown-legacy/index.js +8 -24
- package/package.json +4 -2
- package/types.d.ts +1 -1
- package/dist/vite-style-transform/index.d.ts +0 -2
- package/dist/vite-style-transform/index.js +0 -5
- package/dist/vite-style-transform/style-transform.d.ts +0 -10
- package/dist/vite-style-transform/style-transform.js +0 -80
- package/dist/vite-style-transform/transform-with-vite.d.ts +0 -18
- package/dist/vite-style-transform/transform-with-vite.js +0 -33
|
@@ -1,338 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { call as callEndpoint } from "../core/endpoint/dev/index.js";
|
|
5
|
-
import { collectErrorMetadata, getViteErrorPayload } from "../core/errors/dev/index.js";
|
|
6
|
-
import { createSafeError } from "../core/errors/index.js";
|
|
7
|
-
import { error, info, warn } from "../core/logger/core.js";
|
|
8
|
-
import * as msg from "../core/messages.js";
|
|
9
|
-
import { appendForwardSlash } from "../core/path.js";
|
|
10
|
-
import { createDevelopmentEnvironment, preload, renderPage } from "../core/render/dev/index.js";
|
|
11
|
-
import { getParamsAndProps, GetParamsAndPropsError } from "../core/render/index.js";
|
|
12
|
-
import { createRequest } from "../core/request.js";
|
|
13
|
-
import { createRouteManifest, matchAllRoutes } from "../core/routing/index.js";
|
|
14
|
-
import { resolvePages } from "../core/util.js";
|
|
15
|
-
import notFoundTemplate, { subpathNotUsedTemplate } from "../template/4xx.js";
|
|
16
|
-
function writeHtmlResponse(res, statusCode, html) {
|
|
17
|
-
res.writeHead(statusCode, {
|
|
18
|
-
"Content-Type": "text/html; charset=utf-8",
|
|
19
|
-
"Content-Length": Buffer.byteLength(html, "utf-8")
|
|
20
|
-
});
|
|
21
|
-
res.write(html);
|
|
22
|
-
res.end();
|
|
23
|
-
}
|
|
24
|
-
async function writeWebResponse(res, webResponse) {
|
|
25
|
-
const { status, headers, body } = webResponse;
|
|
26
|
-
let _headers = {};
|
|
27
|
-
if ("raw" in headers) {
|
|
28
|
-
for (const [key, value] of Object.entries(headers.raw())) {
|
|
29
|
-
res.setHeader(key, value);
|
|
30
|
-
}
|
|
31
|
-
} else {
|
|
32
|
-
_headers = Object.fromEntries(headers.entries());
|
|
33
|
-
}
|
|
34
|
-
const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse));
|
|
35
|
-
if (setCookieHeaders.length) {
|
|
36
|
-
res.setHeader("Set-Cookie", setCookieHeaders);
|
|
37
|
-
}
|
|
38
|
-
res.writeHead(status, _headers);
|
|
39
|
-
if (body) {
|
|
40
|
-
if (Symbol.for("astro.responseBody") in webResponse) {
|
|
41
|
-
let stream = webResponse[Symbol.for("astro.responseBody")];
|
|
42
|
-
for await (const chunk of stream) {
|
|
43
|
-
res.write(chunk.toString());
|
|
44
|
-
}
|
|
45
|
-
} else if (body instanceof Readable) {
|
|
46
|
-
body.pipe(res);
|
|
47
|
-
return;
|
|
48
|
-
} else if (typeof body === "string") {
|
|
49
|
-
res.write(body);
|
|
50
|
-
} else {
|
|
51
|
-
const reader = body.getReader();
|
|
52
|
-
while (true) {
|
|
53
|
-
const { done, value } = await reader.read();
|
|
54
|
-
if (done)
|
|
55
|
-
break;
|
|
56
|
-
if (value) {
|
|
57
|
-
res.write(value);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
res.end();
|
|
63
|
-
}
|
|
64
|
-
async function writeSSRResult(webResponse, res) {
|
|
65
|
-
return writeWebResponse(res, webResponse);
|
|
66
|
-
}
|
|
67
|
-
async function handle404Response(origin, req, res) {
|
|
68
|
-
const pathname = decodeURI(new URL(origin + req.url).pathname);
|
|
69
|
-
const html = notFoundTemplate({
|
|
70
|
-
statusCode: 404,
|
|
71
|
-
title: "Not found",
|
|
72
|
-
tabTitle: "404: Not Found",
|
|
73
|
-
pathname
|
|
74
|
-
});
|
|
75
|
-
writeHtmlResponse(res, 404, html);
|
|
76
|
-
}
|
|
77
|
-
async function handle500Response(viteServer, origin, req, res, err) {
|
|
78
|
-
res.on("close", () => setTimeout(() => viteServer.ws.send(getViteErrorPayload(err)), 200));
|
|
79
|
-
if (res.headersSent) {
|
|
80
|
-
res.write(`<script type="module" src="/@vite/client"><\/script>`);
|
|
81
|
-
res.end();
|
|
82
|
-
} else {
|
|
83
|
-
writeHtmlResponse(
|
|
84
|
-
res,
|
|
85
|
-
500,
|
|
86
|
-
`<title>${err.name}</title><script type="module" src="/@vite/client"><\/script>`
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
function getCustom404Route({ config }, manifest) {
|
|
91
|
-
const relPages = resolvePages(config).href.replace(config.root.href, "");
|
|
92
|
-
const pattern = new RegExp(`${appendForwardSlash(relPages)}404.(astro|md)`);
|
|
93
|
-
return manifest.routes.find((r) => r.component.match(pattern));
|
|
94
|
-
}
|
|
95
|
-
function log404(logging, pathname) {
|
|
96
|
-
info(logging, "serve", msg.req({ url: pathname, statusCode: 404 }));
|
|
97
|
-
}
|
|
98
|
-
function baseMiddleware(settings, logging) {
|
|
99
|
-
const { config } = settings;
|
|
100
|
-
const site = config.site ? new URL(config.base, config.site) : void 0;
|
|
101
|
-
const devRoot = site ? site.pathname : "/";
|
|
102
|
-
return function devBaseMiddleware(req, res, next) {
|
|
103
|
-
var _a;
|
|
104
|
-
const url = req.url;
|
|
105
|
-
const pathname = decodeURI(new URL(url, "http://vitejs.dev").pathname);
|
|
106
|
-
if (pathname.startsWith(devRoot)) {
|
|
107
|
-
req.url = url.replace(devRoot, "/");
|
|
108
|
-
return next();
|
|
109
|
-
}
|
|
110
|
-
if (pathname === "/" || pathname === "/index.html") {
|
|
111
|
-
log404(logging, pathname);
|
|
112
|
-
const html = subpathNotUsedTemplate(devRoot, pathname);
|
|
113
|
-
return writeHtmlResponse(res, 404, html);
|
|
114
|
-
}
|
|
115
|
-
if ((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html")) {
|
|
116
|
-
log404(logging, pathname);
|
|
117
|
-
const html = notFoundTemplate({
|
|
118
|
-
statusCode: 404,
|
|
119
|
-
title: "Not found",
|
|
120
|
-
tabTitle: "404: Not Found",
|
|
121
|
-
pathname
|
|
122
|
-
});
|
|
123
|
-
return writeHtmlResponse(res, 404, html);
|
|
124
|
-
}
|
|
125
|
-
next();
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
async function matchRoute(pathname, env, manifest) {
|
|
129
|
-
const { logging, settings, routeCache } = env;
|
|
130
|
-
const matches = matchAllRoutes(pathname, manifest);
|
|
131
|
-
for await (const maybeRoute of matches) {
|
|
132
|
-
const filePath = new URL(`./${maybeRoute.component}`, settings.config.root);
|
|
133
|
-
const preloadedComponent = await preload({ env, filePath });
|
|
134
|
-
const [, mod] = preloadedComponent;
|
|
135
|
-
const paramsAndPropsRes = await getParamsAndProps({
|
|
136
|
-
mod,
|
|
137
|
-
route: maybeRoute,
|
|
138
|
-
routeCache,
|
|
139
|
-
pathname,
|
|
140
|
-
logging,
|
|
141
|
-
ssr: settings.config.output === "server"
|
|
142
|
-
});
|
|
143
|
-
if (paramsAndPropsRes !== GetParamsAndPropsError.NoMatchingStaticPath) {
|
|
144
|
-
return {
|
|
145
|
-
route: maybeRoute,
|
|
146
|
-
filePath,
|
|
147
|
-
preloadedComponent,
|
|
148
|
-
mod
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (matches.length) {
|
|
153
|
-
warn(
|
|
154
|
-
logging,
|
|
155
|
-
"getStaticPaths",
|
|
156
|
-
`Route pattern matched, but no matching static path found. (${pathname})`
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
log404(logging, pathname);
|
|
160
|
-
const custom404 = getCustom404Route(settings, manifest);
|
|
161
|
-
if (custom404) {
|
|
162
|
-
const filePath = new URL(`./${custom404.component}`, settings.config.root);
|
|
163
|
-
const preloadedComponent = await preload({ env, filePath });
|
|
164
|
-
const [, mod] = preloadedComponent;
|
|
165
|
-
return {
|
|
166
|
-
route: custom404,
|
|
167
|
-
filePath,
|
|
168
|
-
preloadedComponent,
|
|
169
|
-
mod
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
return void 0;
|
|
173
|
-
}
|
|
174
|
-
async function handleRequest(env, manifest, req, res) {
|
|
175
|
-
var _a;
|
|
176
|
-
const { settings, viteServer } = env;
|
|
177
|
-
const { config } = settings;
|
|
178
|
-
const origin = `${viteServer.config.server.https ? "https" : "http"}://${req.headers.host}`;
|
|
179
|
-
const buildingToSSR = config.output === "server";
|
|
180
|
-
const url = new URL(origin + ((_a = req.url) == null ? void 0 : _a.replace(/(index)?\.html$/, "")));
|
|
181
|
-
const pathname = decodeURI(url.pathname);
|
|
182
|
-
url.pathname = config.base.substring(0, config.base.length - 1) + url.pathname;
|
|
183
|
-
if (!buildingToSSR && pathname !== "/_image") {
|
|
184
|
-
const allSearchParams = Array.from(url.searchParams);
|
|
185
|
-
for (const [key] of allSearchParams) {
|
|
186
|
-
url.searchParams.delete(key);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
let body = void 0;
|
|
190
|
-
if (!(req.method === "GET" || req.method === "HEAD")) {
|
|
191
|
-
let bytes = [];
|
|
192
|
-
await new Promise((resolve) => {
|
|
193
|
-
req.on("data", (part) => {
|
|
194
|
-
bytes.push(part);
|
|
195
|
-
});
|
|
196
|
-
req.on("end", resolve);
|
|
197
|
-
});
|
|
198
|
-
body = Buffer.concat(bytes);
|
|
199
|
-
}
|
|
200
|
-
try {
|
|
201
|
-
const matchedRoute = await matchRoute(pathname, env, manifest);
|
|
202
|
-
return await handleRoute(matchedRoute, url, pathname, body, origin, env, manifest, req, res);
|
|
203
|
-
} catch (_err) {
|
|
204
|
-
const err = createSafeError(_err);
|
|
205
|
-
const errorWithMetadata = collectErrorMetadata(err);
|
|
206
|
-
error(env.logging, null, msg.formatErrorMessage(errorWithMetadata));
|
|
207
|
-
handle500Response(viteServer, origin, req, res, errorWithMetadata);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
function isRedirect(statusCode) {
|
|
211
|
-
return statusCode >= 300 && statusCode < 400;
|
|
212
|
-
}
|
|
213
|
-
function throwIfRedirectNotAllowed(response, config) {
|
|
214
|
-
if (config.output !== "server" && isRedirect(response.status)) {
|
|
215
|
-
throw new Error(
|
|
216
|
-
`Redirects are only available when using output: 'server'. Update your Astro config if you need SSR features.`
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
async function handleRoute(matchedRoute, url, pathname, body, origin, env, manifest, req, res) {
|
|
221
|
-
const { logging, settings } = env;
|
|
222
|
-
if (!matchedRoute) {
|
|
223
|
-
return handle404Response(origin, req, res);
|
|
224
|
-
}
|
|
225
|
-
const { config } = settings;
|
|
226
|
-
const filePath = matchedRoute.filePath;
|
|
227
|
-
const { route, preloadedComponent, mod } = matchedRoute;
|
|
228
|
-
const buildingToSSR = config.output === "server";
|
|
229
|
-
const request = createRequest({
|
|
230
|
-
url,
|
|
231
|
-
headers: buildingToSSR ? req.headers : new Headers(),
|
|
232
|
-
method: req.method,
|
|
233
|
-
body,
|
|
234
|
-
logging,
|
|
235
|
-
ssr: buildingToSSR,
|
|
236
|
-
clientAddress: buildingToSSR ? req.socket.remoteAddress : void 0
|
|
237
|
-
});
|
|
238
|
-
const paramsAndPropsRes = await getParamsAndProps({
|
|
239
|
-
mod,
|
|
240
|
-
route,
|
|
241
|
-
routeCache: env.routeCache,
|
|
242
|
-
pathname,
|
|
243
|
-
logging,
|
|
244
|
-
ssr: config.output === "server"
|
|
245
|
-
});
|
|
246
|
-
const options = {
|
|
247
|
-
env,
|
|
248
|
-
filePath,
|
|
249
|
-
origin,
|
|
250
|
-
preload: preloadedComponent,
|
|
251
|
-
pathname,
|
|
252
|
-
request,
|
|
253
|
-
route
|
|
254
|
-
};
|
|
255
|
-
if (route.type === "endpoint") {
|
|
256
|
-
const result = await callEndpoint(options);
|
|
257
|
-
if (result.type === "response") {
|
|
258
|
-
if (result.response.headers.get("X-Astro-Response") === "Not-Found") {
|
|
259
|
-
const fourOhFourRoute = await matchRoute("/404", env, manifest);
|
|
260
|
-
return handleRoute(
|
|
261
|
-
fourOhFourRoute,
|
|
262
|
-
new URL("/404", url),
|
|
263
|
-
"/404",
|
|
264
|
-
body,
|
|
265
|
-
origin,
|
|
266
|
-
env,
|
|
267
|
-
manifest,
|
|
268
|
-
req,
|
|
269
|
-
res
|
|
270
|
-
);
|
|
271
|
-
}
|
|
272
|
-
throwIfRedirectNotAllowed(result.response, config);
|
|
273
|
-
await writeWebResponse(res, result.response);
|
|
274
|
-
} else {
|
|
275
|
-
let contentType = "text/plain";
|
|
276
|
-
const filepath = route.pathname || route.segments.map((segment) => segment.map((p) => p.content).join("")).join("/");
|
|
277
|
-
const computedMimeType = mime.getType(filepath);
|
|
278
|
-
if (computedMimeType) {
|
|
279
|
-
contentType = computedMimeType;
|
|
280
|
-
}
|
|
281
|
-
const response = new Response(result.body, {
|
|
282
|
-
status: 200,
|
|
283
|
-
headers: {
|
|
284
|
-
"Content-Type": `${contentType};charset=utf-8`
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
attachToResponse(response, result.cookies);
|
|
288
|
-
await writeWebResponse(res, response);
|
|
289
|
-
}
|
|
290
|
-
} else {
|
|
291
|
-
const result = await renderPage(options);
|
|
292
|
-
throwIfRedirectNotAllowed(result, config);
|
|
293
|
-
return await writeSSRResult(result, res);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
function createPlugin({ settings, logging }) {
|
|
297
|
-
return {
|
|
298
|
-
name: "astro:server",
|
|
299
|
-
configureServer(viteServer) {
|
|
300
|
-
let env = createDevelopmentEnvironment(settings, logging, viteServer);
|
|
301
|
-
let manifest = createRouteManifest({ settings }, logging);
|
|
302
|
-
function rebuildManifest(needsManifestRebuild, file) {
|
|
303
|
-
env.routeCache.clearAll();
|
|
304
|
-
if (needsManifestRebuild) {
|
|
305
|
-
manifest = createRouteManifest({ settings }, logging);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
viteServer.watcher.on("add", rebuildManifest.bind(null, true));
|
|
309
|
-
viteServer.watcher.on("unlink", rebuildManifest.bind(null, true));
|
|
310
|
-
viteServer.watcher.on("change", rebuildManifest.bind(null, false));
|
|
311
|
-
return () => {
|
|
312
|
-
if (settings.config.base !== "/") {
|
|
313
|
-
viteServer.middlewares.stack.unshift({
|
|
314
|
-
route: "",
|
|
315
|
-
handle: baseMiddleware(settings, logging)
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
viteServer.middlewares.use(async (req, res) => {
|
|
319
|
-
if (!req.url || !req.method) {
|
|
320
|
-
throw new Error("Incomplete request");
|
|
321
|
-
}
|
|
322
|
-
handleRequest(env, manifest, req, res);
|
|
323
|
-
});
|
|
324
|
-
};
|
|
325
|
-
},
|
|
326
|
-
transform(code, id, opts = {}) {
|
|
327
|
-
if (opts.ssr)
|
|
328
|
-
return;
|
|
329
|
-
if (!id.includes("vite/dist/client/client.mjs"))
|
|
330
|
-
return;
|
|
331
|
-
return code.replace(/\.tip \{[^}]*\}/gm, ".tip {\n display: none;\n}").replace(/\[vite\]/g, "[astro]");
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
}
|
|
1
|
+
import { createController, runWithErrorHandling } from "./controller.js";
|
|
2
|
+
import { default as default2 } from "./plugin.js";
|
|
3
|
+
import { handleRequest } from "./request.js";
|
|
335
4
|
export {
|
|
336
|
-
|
|
337
|
-
|
|
5
|
+
createController,
|
|
6
|
+
handleRequest,
|
|
7
|
+
runWithErrorHandling,
|
|
8
|
+
default2 as vitePluginAstroServer
|
|
338
9
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type * as vite from 'vite';
|
|
3
|
+
import type { AstroSettings } from '../@types/astro';
|
|
4
|
+
import type fs from 'fs';
|
|
5
|
+
import { LogOptions } from '../core/logger/core.js';
|
|
6
|
+
export interface AstroPluginOptions {
|
|
7
|
+
settings: AstroSettings;
|
|
8
|
+
logging: LogOptions;
|
|
9
|
+
fs: typeof fs;
|
|
10
|
+
}
|
|
11
|
+
export default function createVitePluginAstroServer({ settings, logging, fs: fsMod, }: AstroPluginOptions): vite.Plugin;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createViteLoader } from "../core/module-loader/index.js";
|
|
2
|
+
import { createDevelopmentEnvironment } from "../core/render/dev/index.js";
|
|
3
|
+
import { createRouteManifest } from "../core/routing/index.js";
|
|
4
|
+
import { baseMiddleware } from "./base.js";
|
|
5
|
+
import { createController } from "./controller.js";
|
|
6
|
+
import { handleRequest } from "./request.js";
|
|
7
|
+
function createVitePluginAstroServer({
|
|
8
|
+
settings,
|
|
9
|
+
logging,
|
|
10
|
+
fs: fsMod
|
|
11
|
+
}) {
|
|
12
|
+
return {
|
|
13
|
+
name: "astro:server",
|
|
14
|
+
configureServer(viteServer) {
|
|
15
|
+
const loader = createViteLoader(viteServer);
|
|
16
|
+
let env = createDevelopmentEnvironment(settings, logging, loader);
|
|
17
|
+
let manifest = createRouteManifest({ settings, fsMod }, logging);
|
|
18
|
+
const serverController = createController({ loader });
|
|
19
|
+
function rebuildManifest(needsManifestRebuild, _file) {
|
|
20
|
+
env.routeCache.clearAll();
|
|
21
|
+
if (needsManifestRebuild) {
|
|
22
|
+
manifest = createRouteManifest({ settings }, logging);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
viteServer.watcher.on("add", rebuildManifest.bind(null, true));
|
|
26
|
+
viteServer.watcher.on("unlink", rebuildManifest.bind(null, true));
|
|
27
|
+
viteServer.watcher.on("change", rebuildManifest.bind(null, false));
|
|
28
|
+
return () => {
|
|
29
|
+
if (settings.config.base !== "/") {
|
|
30
|
+
viteServer.middlewares.stack.unshift({
|
|
31
|
+
route: "",
|
|
32
|
+
handle: baseMiddleware(settings, logging)
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
viteServer.middlewares.use(async (req, res) => {
|
|
36
|
+
if (!req.url || !req.method) {
|
|
37
|
+
throw new Error("Incomplete request");
|
|
38
|
+
}
|
|
39
|
+
handleRequest(env, manifest, serverController, req, res);
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
transform(code, id, opts = {}) {
|
|
44
|
+
if (opts.ssr)
|
|
45
|
+
return;
|
|
46
|
+
if (!id.includes("vite/dist/client/client.mjs"))
|
|
47
|
+
return;
|
|
48
|
+
return code.replace(/\.tip \{[^}]*\}/gm, ".tip {\n display: none;\n}").replace(/\[vite\]/g, "[astro]");
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export {
|
|
53
|
+
createVitePluginAstroServer as default
|
|
54
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type http from 'http';
|
|
3
|
+
import type { ManifestData } from '../@types/astro';
|
|
4
|
+
import type { DevelopmentEnvironment } from '../core/render/dev/index';
|
|
5
|
+
import type { DevServerController } from './controller';
|
|
6
|
+
/** The main logic to route dev server requests to pages in Astro. */
|
|
7
|
+
export declare function handleRequest(env: DevelopmentEnvironment, manifest: ManifestData, controller: DevServerController, req: http.IncomingMessage, res: http.ServerResponse): Promise<void>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { collectErrorMetadata } from "../core/errors/dev/index.js";
|
|
2
|
+
import { createSafeError } from "../core/errors/index.js";
|
|
3
|
+
import { error } from "../core/logger/core.js";
|
|
4
|
+
import * as msg from "../core/messages.js";
|
|
5
|
+
import { runWithErrorHandling } from "./controller.js";
|
|
6
|
+
import { handle500Response } from "./response.js";
|
|
7
|
+
import { handleRoute, matchRoute } from "./route.js";
|
|
8
|
+
async function handleRequest(env, manifest, controller, req, res) {
|
|
9
|
+
var _a;
|
|
10
|
+
const { settings, loader: moduleLoader } = env;
|
|
11
|
+
const { config } = settings;
|
|
12
|
+
const origin = `${moduleLoader.isHttps() ? "https" : "http"}://${req.headers.host}`;
|
|
13
|
+
const buildingToSSR = config.output === "server";
|
|
14
|
+
const url = new URL(origin + ((_a = req.url) == null ? void 0 : _a.replace(/(index)?\.html$/, "")));
|
|
15
|
+
const pathname = decodeURI(url.pathname);
|
|
16
|
+
url.pathname = config.base.substring(0, config.base.length - 1) + url.pathname;
|
|
17
|
+
if (!buildingToSSR && pathname !== "/_image") {
|
|
18
|
+
const allSearchParams = Array.from(url.searchParams);
|
|
19
|
+
for (const [key] of allSearchParams) {
|
|
20
|
+
url.searchParams.delete(key);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
let body = void 0;
|
|
24
|
+
if (!(req.method === "GET" || req.method === "HEAD")) {
|
|
25
|
+
let bytes = [];
|
|
26
|
+
await new Promise((resolve) => {
|
|
27
|
+
req.on("data", (part) => {
|
|
28
|
+
bytes.push(part);
|
|
29
|
+
});
|
|
30
|
+
req.on("end", resolve);
|
|
31
|
+
});
|
|
32
|
+
body = Buffer.concat(bytes);
|
|
33
|
+
}
|
|
34
|
+
await runWithErrorHandling({
|
|
35
|
+
controller,
|
|
36
|
+
pathname,
|
|
37
|
+
async run() {
|
|
38
|
+
const matchedRoute = await matchRoute(pathname, env, manifest);
|
|
39
|
+
return await handleRoute(matchedRoute, url, pathname, body, origin, env, manifest, req, res);
|
|
40
|
+
},
|
|
41
|
+
onError(_err) {
|
|
42
|
+
const err = createSafeError(_err);
|
|
43
|
+
const errorWithMetadata = collectErrorMetadata(err);
|
|
44
|
+
error(env.logging, null, msg.formatErrorMessage(errorWithMetadata));
|
|
45
|
+
handle500Response(moduleLoader, res, errorWithMetadata);
|
|
46
|
+
return err;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
handleRequest
|
|
52
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type http from 'http';
|
|
3
|
+
import type { ErrorWithMetadata } from '../core/errors/index.js';
|
|
4
|
+
import type { ModuleLoader } from '../core/module-loader/index';
|
|
5
|
+
export declare function handle404Response(origin: string, req: http.IncomingMessage, res: http.ServerResponse): Promise<void>;
|
|
6
|
+
export declare function handle500Response(loader: ModuleLoader, res: http.ServerResponse, err: ErrorWithMetadata): Promise<void>;
|
|
7
|
+
export declare function writeHtmlResponse(res: http.ServerResponse, statusCode: number, html: string): void;
|
|
8
|
+
export declare function writeWebResponse(res: http.ServerResponse, webResponse: Response): Promise<void>;
|
|
9
|
+
export declare function writeSSRResult(webResponse: Response, res: http.ServerResponse): Promise<void>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Readable } from "stream";
|
|
2
|
+
import { getSetCookiesFromResponse } from "../core/cookies/index.js";
|
|
3
|
+
import { getViteErrorPayload } from "../core/errors/dev/index.js";
|
|
4
|
+
import notFoundTemplate from "../template/4xx.js";
|
|
5
|
+
async function handle404Response(origin, req, res) {
|
|
6
|
+
const pathname = decodeURI(new URL(origin + req.url).pathname);
|
|
7
|
+
const html = notFoundTemplate({
|
|
8
|
+
statusCode: 404,
|
|
9
|
+
title: "Not found",
|
|
10
|
+
tabTitle: "404: Not Found",
|
|
11
|
+
pathname
|
|
12
|
+
});
|
|
13
|
+
writeHtmlResponse(res, 404, html);
|
|
14
|
+
}
|
|
15
|
+
async function handle500Response(loader, res, err) {
|
|
16
|
+
res.on("close", () => setTimeout(() => loader.webSocketSend(getViteErrorPayload(err)), 200));
|
|
17
|
+
if (res.headersSent) {
|
|
18
|
+
res.write(`<script type="module" src="/@vite/client"><\/script>`);
|
|
19
|
+
res.end();
|
|
20
|
+
} else {
|
|
21
|
+
writeHtmlResponse(
|
|
22
|
+
res,
|
|
23
|
+
500,
|
|
24
|
+
`<title>${err.name}</title><script type="module" src="/@vite/client"><\/script>`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function writeHtmlResponse(res, statusCode, html) {
|
|
29
|
+
res.writeHead(statusCode, {
|
|
30
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
31
|
+
"Content-Length": Buffer.byteLength(html, "utf-8")
|
|
32
|
+
});
|
|
33
|
+
res.write(html);
|
|
34
|
+
res.end();
|
|
35
|
+
}
|
|
36
|
+
async function writeWebResponse(res, webResponse) {
|
|
37
|
+
const { status, headers, body } = webResponse;
|
|
38
|
+
let _headers = {};
|
|
39
|
+
if ("raw" in headers) {
|
|
40
|
+
for (const [key, value] of Object.entries(headers.raw())) {
|
|
41
|
+
res.setHeader(key, value);
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
_headers = Object.fromEntries(headers.entries());
|
|
45
|
+
}
|
|
46
|
+
const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse));
|
|
47
|
+
if (setCookieHeaders.length) {
|
|
48
|
+
res.setHeader("Set-Cookie", setCookieHeaders);
|
|
49
|
+
}
|
|
50
|
+
res.writeHead(status, _headers);
|
|
51
|
+
if (body) {
|
|
52
|
+
if (Symbol.for("astro.responseBody") in webResponse) {
|
|
53
|
+
let stream = webResponse[Symbol.for("astro.responseBody")];
|
|
54
|
+
for await (const chunk of stream) {
|
|
55
|
+
res.write(chunk.toString());
|
|
56
|
+
}
|
|
57
|
+
} else if (body instanceof Readable) {
|
|
58
|
+
body.pipe(res);
|
|
59
|
+
return;
|
|
60
|
+
} else if (typeof body === "string") {
|
|
61
|
+
res.write(body);
|
|
62
|
+
} else {
|
|
63
|
+
const reader = body.getReader();
|
|
64
|
+
while (true) {
|
|
65
|
+
const { done, value } = await reader.read();
|
|
66
|
+
if (done)
|
|
67
|
+
break;
|
|
68
|
+
if (value) {
|
|
69
|
+
res.write(value);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
res.end();
|
|
75
|
+
}
|
|
76
|
+
async function writeSSRResult(webResponse, res) {
|
|
77
|
+
return writeWebResponse(res, webResponse);
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
handle404Response,
|
|
81
|
+
handle500Response,
|
|
82
|
+
writeHtmlResponse,
|
|
83
|
+
writeSSRResult,
|
|
84
|
+
writeWebResponse
|
|
85
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type http from 'http';
|
|
3
|
+
import type { ManifestData } from '../@types/astro';
|
|
4
|
+
import { DevelopmentEnvironment } from '../core/render/dev/index';
|
|
5
|
+
declare type AsyncReturnType<T extends (...args: any) => Promise<any>> = T extends (...args: any) => Promise<infer R> ? R : any;
|
|
6
|
+
export declare function matchRoute(pathname: string, env: DevelopmentEnvironment, manifest: ManifestData): Promise<{
|
|
7
|
+
route: import("../@types/astro").RouteData;
|
|
8
|
+
filePath: URL;
|
|
9
|
+
preloadedComponent: import("../core/render/dev/index").ComponentPreload;
|
|
10
|
+
mod: import("../@types/astro").ComponentInstance;
|
|
11
|
+
} | undefined>;
|
|
12
|
+
export declare function handleRoute(matchedRoute: AsyncReturnType<typeof matchRoute>, url: URL, pathname: string, body: ArrayBuffer | undefined, origin: string, env: DevelopmentEnvironment, manifest: ManifestData, req: http.IncomingMessage, res: http.ServerResponse): Promise<void>;
|
|
13
|
+
export {};
|