astro 1.4.6 → 1.5.0
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/components/Code.astro +1 -1
- package/dist/@types/astro.d.ts +201 -29
- package/dist/cli/index.js +35 -36
- package/dist/config/index.js +2 -7
- package/dist/core/add/index.js +140 -23
- package/dist/core/build/index.js +3 -3
- package/dist/core/build/vite-plugin-css.js +6 -17
- package/dist/core/config/config.d.ts +1 -1
- package/dist/core/config/config.js +6 -2
- package/dist/core/config/index.d.ts +1 -1
- package/dist/core/config/index.js +2 -1
- package/dist/core/config/schema.d.ts +139 -1
- package/dist/core/config/schema.js +24 -2
- package/dist/core/config/settings.d.ts +1 -7
- package/dist/core/config/settings.js +7 -4
- package/dist/core/config/tsconfig.d.ts +10 -1
- package/dist/core/config/tsconfig.js +73 -7
- package/dist/core/constants.d.ts +1 -0
- package/dist/core/constants.js +4 -0
- package/dist/core/cookies/cookies.d.ts +1 -1
- package/dist/core/dev/index.js +7 -2
- package/dist/core/endpoint/dev/index.js +4 -1
- package/dist/core/endpoint/index.d.ts +1 -1
- package/dist/core/endpoint/index.js +44 -4
- package/dist/core/messages.js +2 -2
- package/dist/core/preview/index.d.ts +2 -11
- package/dist/core/preview/index.js +31 -125
- package/dist/core/preview/static-preview-server.d.ts +17 -0
- package/dist/core/preview/static-preview-server.js +127 -0
- package/dist/core/render/core.d.ts +1 -1
- package/dist/core/render/result.js +2 -2
- package/dist/core/util.d.ts +3 -14
- package/dist/core/util.js +4 -2
- package/dist/events/index.js +1 -1
- package/dist/integrations/index.d.ts +3 -2
- package/dist/integrations/index.js +39 -2
- package/dist/runtime/server/astro-global.js +1 -1
- package/dist/runtime/server/hydration.js +1 -1
- package/dist/runtime/server/render/page.js +1 -0
- package/dist/vite-plugin-jsx/index.js +9 -5
- package/dist/vite-plugin-jsx/tag.d.ts +3 -2
- package/dist/vite-plugin-jsx/tag.js +10 -4
- package/package.json +4 -2
|
@@ -1,13 +1,79 @@
|
|
|
1
|
+
import { deepmerge } from "deepmerge-ts";
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
import { join } from "path";
|
|
1
4
|
import * as tsr from "tsconfig-resolver";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const defaultTSConfig = { extends: "astro/tsconfigs/base" };
|
|
6
|
+
const presets = /* @__PURE__ */ new Map([
|
|
7
|
+
[
|
|
8
|
+
"vue",
|
|
9
|
+
{
|
|
10
|
+
compilerOptions: {
|
|
11
|
+
jsx: "preserve"
|
|
12
|
+
}
|
|
7
13
|
}
|
|
14
|
+
],
|
|
15
|
+
[
|
|
16
|
+
"react",
|
|
17
|
+
{
|
|
18
|
+
compilerOptions: {
|
|
19
|
+
jsx: "react-jsx",
|
|
20
|
+
jsxImportSource: "react"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
],
|
|
24
|
+
[
|
|
25
|
+
"preact",
|
|
26
|
+
{
|
|
27
|
+
compilerOptions: {
|
|
28
|
+
jsx: "react-jsx",
|
|
29
|
+
jsxImportSource: "preact"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
[
|
|
34
|
+
"solid-js",
|
|
35
|
+
{
|
|
36
|
+
compilerOptions: {
|
|
37
|
+
jsx: "preserve",
|
|
38
|
+
jsxImportSource: "solid-js"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
]);
|
|
43
|
+
function loadTSConfig(cwd, resolve = true) {
|
|
44
|
+
cwd = cwd ?? process.cwd();
|
|
45
|
+
let config = tsr.tsconfigResolverSync({
|
|
46
|
+
cwd,
|
|
47
|
+
filePath: resolve ? void 0 : cwd
|
|
48
|
+
});
|
|
49
|
+
if (!resolve && config.reason === "invalid-config" && !existsSync(join(cwd, "tsconfig.json"))) {
|
|
50
|
+
config = { reason: "not-found", path: void 0, exists: false };
|
|
51
|
+
} else {
|
|
52
|
+
return config;
|
|
53
|
+
}
|
|
54
|
+
if (config.reason === "not-found") {
|
|
55
|
+
const jsconfig = tsr.tsconfigResolverSync({
|
|
56
|
+
cwd,
|
|
57
|
+
filePath: resolve ? void 0 : cwd,
|
|
58
|
+
searchName: "jsconfig.json"
|
|
59
|
+
});
|
|
60
|
+
if (!resolve && jsconfig.reason === "invalid-config" && !existsSync(join(cwd, "jsconfig.json"))) {
|
|
61
|
+
return { reason: "not-found", path: void 0, exists: false };
|
|
62
|
+
} else {
|
|
63
|
+
return jsconfig;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return config;
|
|
67
|
+
}
|
|
68
|
+
function updateTSConfigForFramework(target, framework) {
|
|
69
|
+
if (!presets.has(framework)) {
|
|
70
|
+
return target;
|
|
8
71
|
}
|
|
9
|
-
return
|
|
72
|
+
return deepmerge(target, presets.get(framework));
|
|
10
73
|
}
|
|
11
74
|
export {
|
|
12
|
-
|
|
75
|
+
defaultTSConfig,
|
|
76
|
+
loadTSConfig,
|
|
77
|
+
presets,
|
|
78
|
+
updateTSConfigForFramework
|
|
13
79
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ASTRO_VERSION: string;
|
|
@@ -19,7 +19,7 @@ interface AstroCookieInterface {
|
|
|
19
19
|
interface AstroCookiesInterface {
|
|
20
20
|
get(key: string): AstroCookieInterface;
|
|
21
21
|
has(key: string): boolean;
|
|
22
|
-
set(key: string, value: string | Record<string, any>, options?: AstroCookieSetOptions): void;
|
|
22
|
+
set(key: string, value: string | number | boolean | Record<string, any>, options?: AstroCookieSetOptions): void;
|
|
23
23
|
delete(key: string, options?: AstroCookieDeleteOptions): void;
|
|
24
24
|
}
|
|
25
25
|
declare class AstroCookie implements AstroCookieInterface {
|
package/dist/core/dev/index.js
CHANGED
|
@@ -16,7 +16,12 @@ async function dev(settings, options) {
|
|
|
16
16
|
const devStart = performance.now();
|
|
17
17
|
applyPolyfill();
|
|
18
18
|
await options.telemetry.record([]);
|
|
19
|
-
settings = await runHookConfigSetup({
|
|
19
|
+
settings = await runHookConfigSetup({
|
|
20
|
+
settings,
|
|
21
|
+
command: "dev",
|
|
22
|
+
logging: options.logging,
|
|
23
|
+
isRestart: options.isRestart
|
|
24
|
+
});
|
|
20
25
|
const { host, port } = settings.config.server;
|
|
21
26
|
const { isRestart = false } = options;
|
|
22
27
|
const rendererClientEntries = settings.renderers.map((r) => r.clientEntrypoint).filter(Boolean);
|
|
@@ -49,7 +54,7 @@ async function dev(settings, options) {
|
|
|
49
54
|
isRestart
|
|
50
55
|
})
|
|
51
56
|
);
|
|
52
|
-
const currentVersion = "1.
|
|
57
|
+
const currentVersion = "1.5.0";
|
|
53
58
|
if (currentVersion.includes("-")) {
|
|
54
59
|
warn(options.logging, null, msg.prerelease({ currentVersion }));
|
|
55
60
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { preload } from "../../render/dev/index.js";
|
|
2
2
|
import { call as callEndpoint } from "../index.js";
|
|
3
3
|
async function call(ssrOpts) {
|
|
4
|
+
var _a;
|
|
4
5
|
const [, mod] = await preload(ssrOpts);
|
|
5
6
|
return await callEndpoint(mod, {
|
|
6
7
|
...ssrOpts,
|
|
7
|
-
ssr: ssrOpts.settings.config.output === "server"
|
|
8
|
+
ssr: ssrOpts.settings.config.output === "server",
|
|
9
|
+
site: ssrOpts.settings.config.site,
|
|
10
|
+
adapterName: (_a = ssrOpts.settings.config.adapter) == null ? void 0 : _a.name
|
|
8
11
|
});
|
|
9
12
|
}
|
|
10
13
|
export {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { EndpointHandler } from '../../@types/astro';
|
|
3
3
|
import type { RenderOptions } from '../render/core';
|
|
4
|
-
export declare type EndpointOptions = Pick<RenderOptions, 'logging' | 'origin' | 'request' | 'route' | 'routeCache' | 'pathname' | 'route' | 'site' | 'ssr' | 'status'>;
|
|
4
|
+
export declare type EndpointOptions = Pick<RenderOptions, 'logging' | 'origin' | 'request' | 'route' | 'routeCache' | 'pathname' | 'route' | 'site' | 'ssr' | 'status' | 'adapterName'>;
|
|
5
5
|
declare type EndpointCallResult = {
|
|
6
6
|
type: 'simple';
|
|
7
7
|
body: string;
|
|
@@ -1,11 +1,45 @@
|
|
|
1
1
|
import { renderEndpoint } from "../../runtime/server/index.js";
|
|
2
|
+
import { ASTRO_VERSION } from "../constants.js";
|
|
2
3
|
import { AstroCookies, attachToResponse } from "../cookies/index.js";
|
|
3
4
|
import { getParamsAndProps, GetParamsAndPropsError } from "../render/core.js";
|
|
4
|
-
|
|
5
|
+
const clientAddressSymbol = Symbol.for("astro.clientAddress");
|
|
6
|
+
function createAPIContext({
|
|
7
|
+
request,
|
|
8
|
+
params,
|
|
9
|
+
site,
|
|
10
|
+
props,
|
|
11
|
+
adapterName
|
|
12
|
+
}) {
|
|
5
13
|
return {
|
|
6
14
|
cookies: new AstroCookies(request),
|
|
7
15
|
request,
|
|
8
|
-
params
|
|
16
|
+
params,
|
|
17
|
+
site: site ? new URL(site) : void 0,
|
|
18
|
+
generator: `Astro v${ASTRO_VERSION}`,
|
|
19
|
+
props,
|
|
20
|
+
redirect(path, status) {
|
|
21
|
+
return new Response(null, {
|
|
22
|
+
status: status || 302,
|
|
23
|
+
headers: {
|
|
24
|
+
Location: path
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
url: new URL(request.url),
|
|
29
|
+
get clientAddress() {
|
|
30
|
+
if (!(clientAddressSymbol in request)) {
|
|
31
|
+
if (adapterName) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`clientAddress is not available in the ${adapterName} adapter. File an issue with the adapter to add support.`
|
|
34
|
+
);
|
|
35
|
+
} else {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`clientAddress is not available in your environment. Ensure that you are using an SSR adapter that supports this feature.`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return Reflect.get(request, clientAddressSymbol);
|
|
42
|
+
}
|
|
9
43
|
};
|
|
10
44
|
}
|
|
11
45
|
async function call(mod, opts) {
|
|
@@ -15,8 +49,14 @@ async function call(mod, opts) {
|
|
|
15
49
|
`[getStaticPath] route pattern matched, but no matching static path found. (${opts.pathname})`
|
|
16
50
|
);
|
|
17
51
|
}
|
|
18
|
-
const [params] = paramsAndPropsResp;
|
|
19
|
-
const context = createAPIContext(
|
|
52
|
+
const [params, props] = paramsAndPropsResp;
|
|
53
|
+
const context = createAPIContext({
|
|
54
|
+
request: opts.request,
|
|
55
|
+
params,
|
|
56
|
+
props,
|
|
57
|
+
site: opts.site,
|
|
58
|
+
adapterName: opts.adapterName
|
|
59
|
+
});
|
|
20
60
|
const response = await renderEndpoint(mod, context, opts.ssr);
|
|
21
61
|
if (response instanceof Response) {
|
|
22
62
|
attachToResponse(response, context.cookies);
|
package/dist/core/messages.js
CHANGED
|
@@ -47,7 +47,7 @@ function serverStart({
|
|
|
47
47
|
site,
|
|
48
48
|
isRestart = false
|
|
49
49
|
}) {
|
|
50
|
-
const version = "1.
|
|
50
|
+
const version = "1.5.0";
|
|
51
51
|
const rootPath = site ? site.pathname : "/";
|
|
52
52
|
const localPrefix = `${dim("\u2503")} Local `;
|
|
53
53
|
const networkPrefix = `${dim("\u2503")} Network `;
|
|
@@ -250,7 +250,7 @@ function printHelp({
|
|
|
250
250
|
message.push(
|
|
251
251
|
linebreak(),
|
|
252
252
|
` ${bgGreen(black(` ${commandName} `))} ${green(
|
|
253
|
-
`v${"1.
|
|
253
|
+
`v${"1.5.0"}`
|
|
254
254
|
)} ${headline}`
|
|
255
255
|
);
|
|
256
256
|
}
|
|
@@ -1,19 +1,10 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import type { AstroTelemetry } from '@astrojs/telemetry';
|
|
3
|
-
import type { AstroSettings } from '../../@types/astro';
|
|
2
|
+
import type { AstroSettings, PreviewServer } from '../../@types/astro';
|
|
4
3
|
import type { LogOptions } from '../logger/core';
|
|
5
|
-
import http from 'http';
|
|
6
4
|
interface PreviewOptions {
|
|
7
5
|
logging: LogOptions;
|
|
8
6
|
telemetry: AstroTelemetry;
|
|
9
7
|
}
|
|
10
|
-
export interface PreviewServer {
|
|
11
|
-
host?: string;
|
|
12
|
-
port: number;
|
|
13
|
-
server: http.Server;
|
|
14
|
-
closed(): Promise<void>;
|
|
15
|
-
stop(): Promise<void>;
|
|
16
|
-
}
|
|
17
8
|
/** The primary dev action */
|
|
18
|
-
export default function preview(
|
|
9
|
+
export default function preview(_settings: AstroSettings, { logging }: PreviewOptions): Promise<PreviewServer>;
|
|
19
10
|
export {};
|
|
@@ -1,134 +1,40 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import sirv from "sirv";
|
|
5
|
-
import { fileURLToPath } from "url";
|
|
6
|
-
import { notFoundTemplate, subpathNotUsedTemplate } from "../../template/4xx.js";
|
|
7
|
-
import { error, info } from "../logger/core.js";
|
|
8
|
-
import * as msg from "../messages.js";
|
|
1
|
+
import { createRequire } from "module";
|
|
2
|
+
import { runHookConfigDone, runHookConfigSetup } from "../../integrations/index.js";
|
|
3
|
+
import createStaticPreviewServer from "./static-preview-server.js";
|
|
9
4
|
import { getResolvedHostForHttpServer } from "./util.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
const startServerTime = performance.now();
|
|
18
|
-
const defaultOrigin = "http://localhost";
|
|
19
|
-
const trailingSlash = settings.config.trailingSlash;
|
|
20
|
-
let baseURL = new URL(settings.config.base, new URL(settings.config.site || "/", defaultOrigin));
|
|
21
|
-
const staticFileServer = sirv(fileURLToPath(settings.config.outDir), {
|
|
22
|
-
dev: true,
|
|
23
|
-
etag: true,
|
|
24
|
-
maxAge: 0
|
|
25
|
-
});
|
|
26
|
-
const server = http.createServer((req, res) => {
|
|
27
|
-
var _a;
|
|
28
|
-
const requestURL = new URL(req.url, defaultOrigin);
|
|
29
|
-
if (!requestURL.pathname.startsWith(baseURL.pathname)) {
|
|
30
|
-
res.statusCode = 404;
|
|
31
|
-
res.end(subpathNotUsedTemplate(baseURL.pathname, requestURL.pathname));
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
const pathname = requestURL.pathname.slice(baseURL.pathname.length - 1);
|
|
35
|
-
const isRoot = pathname === "/";
|
|
36
|
-
const hasTrailingSlash = isRoot || pathname.endsWith("/");
|
|
37
|
-
function sendError(message) {
|
|
38
|
-
res.statusCode = 404;
|
|
39
|
-
res.end(notFoundTemplate(pathname, message));
|
|
40
|
-
}
|
|
41
|
-
switch (true) {
|
|
42
|
-
case (hasTrailingSlash && trailingSlash == "never" && !isRoot):
|
|
43
|
-
sendError('Not Found (trailingSlash is set to "never")');
|
|
44
|
-
return;
|
|
45
|
-
case (!hasTrailingSlash && trailingSlash == "always" && !isRoot && !HAS_FILE_EXTENSION_REGEXP.test(pathname)):
|
|
46
|
-
sendError('Not Found (trailingSlash is set to "always")');
|
|
47
|
-
return;
|
|
48
|
-
default: {
|
|
49
|
-
req.url = "/" + ((_a = req.url) == null ? void 0 : _a.replace(baseURL.pathname, ""));
|
|
50
|
-
staticFileServer(req, res, () => {
|
|
51
|
-
const errorPagePath = fileURLToPath(settings.config.outDir + "/404.html");
|
|
52
|
-
if (fs.existsSync(errorPagePath)) {
|
|
53
|
-
res.statusCode = 404;
|
|
54
|
-
res.setHeader("Content-Type", "text/html;charset=utf-8");
|
|
55
|
-
res.end(fs.readFileSync(errorPagePath));
|
|
56
|
-
} else {
|
|
57
|
-
staticFileServer(req, res, () => {
|
|
58
|
-
sendError("Not Found");
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
5
|
+
async function preview(_settings, { logging }) {
|
|
6
|
+
const settings = await runHookConfigSetup({
|
|
7
|
+
settings: _settings,
|
|
8
|
+
command: "preview",
|
|
9
|
+
logging
|
|
65
10
|
});
|
|
66
|
-
|
|
11
|
+
await runHookConfigDone({ settings, logging });
|
|
67
12
|
const host = getResolvedHostForHttpServer(settings.config.server.host);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (!showedListenMsg) {
|
|
76
|
-
const resolvedUrls = msg.resolveServerUrls({
|
|
77
|
-
address: server.address(),
|
|
78
|
-
host: settings.config.server.host,
|
|
79
|
-
https: false
|
|
80
|
-
});
|
|
81
|
-
info(
|
|
82
|
-
logging,
|
|
83
|
-
null,
|
|
84
|
-
msg.serverStart({
|
|
85
|
-
startupTime: performance.now() - timerStart,
|
|
86
|
-
resolvedUrls,
|
|
87
|
-
host: settings.config.server.host,
|
|
88
|
-
site: baseURL
|
|
89
|
-
})
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
showedListenMsg = true;
|
|
93
|
-
resolve();
|
|
94
|
-
});
|
|
95
|
-
httpServer == null ? void 0 : httpServer.on("error", onError);
|
|
96
|
-
};
|
|
97
|
-
const onError = (err) => {
|
|
98
|
-
if (err.code && err.code === "EADDRINUSE") {
|
|
99
|
-
if (!showedPortTakenMsg) {
|
|
100
|
-
info(logging, "astro", msg.portInUse({ port }));
|
|
101
|
-
showedPortTakenMsg = true;
|
|
102
|
-
}
|
|
103
|
-
port++;
|
|
104
|
-
return listen();
|
|
105
|
-
} else {
|
|
106
|
-
error(logging, "astro", err.stack || err.message);
|
|
107
|
-
httpServer == null ? void 0 : httpServer.removeListener("error", onError);
|
|
108
|
-
reject(err);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
listen();
|
|
112
|
-
});
|
|
13
|
+
const { port } = settings.config.server;
|
|
14
|
+
if (settings.config.output === "static") {
|
|
15
|
+
const server2 = await createStaticPreviewServer(settings, { logging, host, port });
|
|
16
|
+
return server2;
|
|
17
|
+
}
|
|
18
|
+
if (!settings.adapter) {
|
|
19
|
+
throw new Error(`[preview] No adapter found.`);
|
|
113
20
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return new Promise((resolve, reject) => {
|
|
117
|
-
httpServer.addListener("close", resolve);
|
|
118
|
-
httpServer.addListener("error", reject);
|
|
119
|
-
});
|
|
21
|
+
if (!settings.adapter.previewEntrypoint) {
|
|
22
|
+
throw new Error(`[preview] adapter does not have previewEntrypoint.`);
|
|
120
23
|
}
|
|
121
|
-
|
|
24
|
+
const require2 = createRequire(settings.config.root);
|
|
25
|
+
const previewEntrypoint = require2.resolve(settings.adapter.previewEntrypoint);
|
|
26
|
+
const previewModule = await import(previewEntrypoint);
|
|
27
|
+
if (typeof previewModule.default !== "function") {
|
|
28
|
+
throw new Error(`[preview] ${settings.adapter.name} cannot preview your app.`);
|
|
29
|
+
}
|
|
30
|
+
const server = await previewModule.default({
|
|
31
|
+
outDir: settings.config.outDir,
|
|
32
|
+
client: settings.config.build.client,
|
|
33
|
+
serverEntrypoint: new URL(settings.config.build.serverEntry, settings.config.build.server),
|
|
122
34
|
host,
|
|
123
|
-
port
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
stop: async () => {
|
|
127
|
-
await new Promise((resolve, reject) => {
|
|
128
|
-
httpServer.close((err) => err ? reject(err) : resolve(void 0));
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
};
|
|
35
|
+
port
|
|
36
|
+
});
|
|
37
|
+
return server;
|
|
132
38
|
}
|
|
133
39
|
export {
|
|
134
40
|
preview as default
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { AstroSettings } from '../../@types/astro';
|
|
3
|
+
import type { LogOptions } from '../logger/core';
|
|
4
|
+
import http from 'http';
|
|
5
|
+
export interface PreviewServer {
|
|
6
|
+
host?: string;
|
|
7
|
+
port: number;
|
|
8
|
+
server: http.Server;
|
|
9
|
+
closed(): Promise<void>;
|
|
10
|
+
stop(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
/** The primary dev action */
|
|
13
|
+
export default function createStaticPreviewServer(settings: AstroSettings, { logging, host, port }: {
|
|
14
|
+
logging: LogOptions;
|
|
15
|
+
host: string | undefined;
|
|
16
|
+
port: number;
|
|
17
|
+
}): Promise<PreviewServer>;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import http from "http";
|
|
3
|
+
import { performance } from "perf_hooks";
|
|
4
|
+
import sirv from "sirv";
|
|
5
|
+
import { fileURLToPath } from "url";
|
|
6
|
+
import { notFoundTemplate, subpathNotUsedTemplate } from "../../template/4xx.js";
|
|
7
|
+
import { error, info } from "../logger/core.js";
|
|
8
|
+
import * as msg from "../messages.js";
|
|
9
|
+
const HAS_FILE_EXTENSION_REGEXP = /^.*\.[^\\]+$/;
|
|
10
|
+
async function createStaticPreviewServer(settings, { logging, host, port }) {
|
|
11
|
+
const startServerTime = performance.now();
|
|
12
|
+
const defaultOrigin = "http://localhost";
|
|
13
|
+
const trailingSlash = settings.config.trailingSlash;
|
|
14
|
+
let baseURL = new URL(settings.config.base, new URL(settings.config.site || "/", defaultOrigin));
|
|
15
|
+
const staticFileServer = sirv(fileURLToPath(settings.config.outDir), {
|
|
16
|
+
dev: true,
|
|
17
|
+
etag: true,
|
|
18
|
+
maxAge: 0
|
|
19
|
+
});
|
|
20
|
+
const server = http.createServer((req, res) => {
|
|
21
|
+
var _a;
|
|
22
|
+
const requestURL = new URL(req.url, defaultOrigin);
|
|
23
|
+
if (!requestURL.pathname.startsWith(baseURL.pathname)) {
|
|
24
|
+
res.statusCode = 404;
|
|
25
|
+
res.end(subpathNotUsedTemplate(baseURL.pathname, requestURL.pathname));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const pathname = requestURL.pathname.slice(baseURL.pathname.length - 1);
|
|
29
|
+
const isRoot = pathname === "/";
|
|
30
|
+
const hasTrailingSlash = isRoot || pathname.endsWith("/");
|
|
31
|
+
function sendError(message) {
|
|
32
|
+
res.statusCode = 404;
|
|
33
|
+
res.end(notFoundTemplate(pathname, message));
|
|
34
|
+
}
|
|
35
|
+
switch (true) {
|
|
36
|
+
case (hasTrailingSlash && trailingSlash == "never" && !isRoot):
|
|
37
|
+
sendError('Not Found (trailingSlash is set to "never")');
|
|
38
|
+
return;
|
|
39
|
+
case (!hasTrailingSlash && trailingSlash == "always" && !isRoot && !HAS_FILE_EXTENSION_REGEXP.test(pathname)):
|
|
40
|
+
sendError('Not Found (trailingSlash is set to "always")');
|
|
41
|
+
return;
|
|
42
|
+
default: {
|
|
43
|
+
req.url = "/" + ((_a = req.url) == null ? void 0 : _a.replace(baseURL.pathname, ""));
|
|
44
|
+
staticFileServer(req, res, () => {
|
|
45
|
+
const errorPagePath = fileURLToPath(settings.config.outDir + "/404.html");
|
|
46
|
+
if (fs.existsSync(errorPagePath)) {
|
|
47
|
+
res.statusCode = 404;
|
|
48
|
+
res.setHeader("Content-Type", "text/html;charset=utf-8");
|
|
49
|
+
res.end(fs.readFileSync(errorPagePath));
|
|
50
|
+
} else {
|
|
51
|
+
staticFileServer(req, res, () => {
|
|
52
|
+
sendError("Not Found");
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
let httpServer;
|
|
61
|
+
function startServer(timerStart) {
|
|
62
|
+
let showedPortTakenMsg = false;
|
|
63
|
+
let showedListenMsg = false;
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
const listen = () => {
|
|
66
|
+
httpServer = server.listen(port, host, async () => {
|
|
67
|
+
if (!showedListenMsg) {
|
|
68
|
+
const resolvedUrls = msg.resolveServerUrls({
|
|
69
|
+
address: server.address(),
|
|
70
|
+
host: settings.config.server.host,
|
|
71
|
+
https: false
|
|
72
|
+
});
|
|
73
|
+
info(
|
|
74
|
+
logging,
|
|
75
|
+
null,
|
|
76
|
+
msg.serverStart({
|
|
77
|
+
startupTime: performance.now() - timerStart,
|
|
78
|
+
resolvedUrls,
|
|
79
|
+
host: settings.config.server.host,
|
|
80
|
+
site: baseURL
|
|
81
|
+
})
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
showedListenMsg = true;
|
|
85
|
+
resolve();
|
|
86
|
+
});
|
|
87
|
+
httpServer == null ? void 0 : httpServer.on("error", onError);
|
|
88
|
+
};
|
|
89
|
+
const onError = (err) => {
|
|
90
|
+
if (err.code && err.code === "EADDRINUSE") {
|
|
91
|
+
if (!showedPortTakenMsg) {
|
|
92
|
+
info(logging, "astro", msg.portInUse({ port }));
|
|
93
|
+
showedPortTakenMsg = true;
|
|
94
|
+
}
|
|
95
|
+
port++;
|
|
96
|
+
return listen();
|
|
97
|
+
} else {
|
|
98
|
+
error(logging, "astro", err.stack || err.message);
|
|
99
|
+
httpServer == null ? void 0 : httpServer.removeListener("error", onError);
|
|
100
|
+
reject(err);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
listen();
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
await startServer(startServerTime);
|
|
107
|
+
function closed() {
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
httpServer.addListener("close", resolve);
|
|
110
|
+
httpServer.addListener("error", reject);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
host,
|
|
115
|
+
port,
|
|
116
|
+
closed,
|
|
117
|
+
server: httpServer,
|
|
118
|
+
stop: async () => {
|
|
119
|
+
await new Promise((resolve, reject) => {
|
|
120
|
+
httpServer.close((err) => err ? reject(err) : resolve(void 0));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
export {
|
|
126
|
+
createStaticPreviewServer as default
|
|
127
|
+
};
|
|
@@ -15,7 +15,7 @@ export declare const enum GetParamsAndPropsError {
|
|
|
15
15
|
}
|
|
16
16
|
export declare function getParamsAndProps(opts: GetParamsAndPropsOptions): Promise<[Params, Props] | GetParamsAndPropsError>;
|
|
17
17
|
export interface RenderOptions {
|
|
18
|
-
adapterName
|
|
18
|
+
adapterName?: string;
|
|
19
19
|
logging: LogOptions;
|
|
20
20
|
links: Set<SSRElement>;
|
|
21
21
|
styles?: Set<SSRElement>;
|
|
@@ -166,9 +166,9 @@ function createResult(args) {
|
|
|
166
166
|
props,
|
|
167
167
|
request,
|
|
168
168
|
url,
|
|
169
|
-
redirect: args.ssr ? (path) => {
|
|
169
|
+
redirect: args.ssr ? (path, status) => {
|
|
170
170
|
return new Response(null, {
|
|
171
|
-
status: 302,
|
|
171
|
+
status: status || 302,
|
|
172
172
|
headers: {
|
|
173
173
|
Location: path
|
|
174
174
|
}
|
package/dist/core/util.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import type { ErrorPayload, ViteDevServer } from 'vite';
|
|
3
2
|
import type { AstroConfig, AstroSettings, RouteType } from '../@types/astro';
|
|
4
|
-
export declare const ASTRO_VERSION: string;
|
|
5
3
|
/** Returns true if argument is an object of any prototype/class (but not null). */
|
|
6
4
|
export declare function isObject(value: unknown): value is Record<string, any>;
|
|
5
|
+
/** Cross-realm compatible URL */
|
|
6
|
+
export declare function isURL(value: unknown): value is URL;
|
|
7
7
|
/** Wraps an object in an array. If an array is passed, ignore it. */
|
|
8
8
|
export declare function arraify<T>(target: T | T[]): T[];
|
|
9
9
|
export declare function padMultilineString(source: string, n?: number): string;
|
|
@@ -45,15 +45,4 @@ export declare function getLocalAddress(serverAddress: string, host: string | bo
|
|
|
45
45
|
*/
|
|
46
46
|
export declare function resolveIdToUrl(viteServer: ViteDevServer, id: string): Promise<string>;
|
|
47
47
|
export declare function resolveJsToTs(filePath: string): string;
|
|
48
|
-
export declare const AggregateError:
|
|
49
|
-
new (errors: Iterable<any>, message?: string | undefined): {
|
|
50
|
-
errors: Array<any>;
|
|
51
|
-
name: string;
|
|
52
|
-
message: string;
|
|
53
|
-
stack?: string | undefined;
|
|
54
|
-
cause?: unknown;
|
|
55
|
-
};
|
|
56
|
-
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
57
|
-
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
58
|
-
stackTraceLimit: number;
|
|
59
|
-
};
|
|
48
|
+
export declare const AggregateError: any;
|
package/dist/core/util.js
CHANGED
|
@@ -5,10 +5,12 @@ import resolve from "resolve";
|
|
|
5
5
|
import slash from "slash";
|
|
6
6
|
import { fileURLToPath, pathToFileURL } from "url";
|
|
7
7
|
import { prependForwardSlash, removeTrailingForwardSlash } from "./path.js";
|
|
8
|
-
const ASTRO_VERSION = "1.4.6";
|
|
9
8
|
function isObject(value) {
|
|
10
9
|
return typeof value === "object" && value != null;
|
|
11
10
|
}
|
|
11
|
+
function isURL(value) {
|
|
12
|
+
return Object.prototype.toString.call(value) === "[object URL]";
|
|
13
|
+
}
|
|
12
14
|
function arraify(target) {
|
|
13
15
|
return Array.isArray(target) ? target : [target];
|
|
14
16
|
}
|
|
@@ -175,7 +177,6 @@ const AggregateError = typeof globalThis.AggregateError !== "undefined" ? global
|
|
|
175
177
|
}
|
|
176
178
|
};
|
|
177
179
|
export {
|
|
178
|
-
ASTRO_VERSION,
|
|
179
180
|
AggregateError,
|
|
180
181
|
VALID_ID_PREFIX,
|
|
181
182
|
arraify,
|
|
@@ -187,6 +188,7 @@ export {
|
|
|
187
188
|
isModeServerWithNoAdapter,
|
|
188
189
|
isObject,
|
|
189
190
|
isPage,
|
|
191
|
+
isURL,
|
|
190
192
|
padMultilineString,
|
|
191
193
|
parseNpmName,
|
|
192
194
|
relativeToSrcDir,
|
package/dist/events/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AstroTelemetry } from "@astrojs/telemetry";
|
|
2
2
|
import { createRequire } from "module";
|
|
3
|
-
import { ASTRO_VERSION } from "../core/
|
|
3
|
+
import { ASTRO_VERSION } from "../core/constants.js";
|
|
4
4
|
const require2 = createRequire(import.meta.url);
|
|
5
5
|
function getViteVersion() {
|
|
6
6
|
try {
|