mirror of
https://github.com/withastro/astro.git
synced 2025-03-10 23:01:26 -05:00
[ci] format
This commit is contained in:
parent
fa399ad7c3
commit
b13c041307
7 changed files with 69 additions and 56 deletions
|
@ -1,8 +1,8 @@
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
import https from 'https';
|
import https from 'https';
|
||||||
import { fileURLToPath } from 'url';
|
|
||||||
import send from 'send';
|
import send from 'send';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
|
||||||
interface CreateServerOptions {
|
interface CreateServerOptions {
|
||||||
client: URL;
|
client: URL;
|
||||||
|
@ -10,9 +10,12 @@ interface CreateServerOptions {
|
||||||
host: string | undefined;
|
host: string | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createServer({ client, port, host }: CreateServerOptions, handler: http.RequestListener) {
|
export function createServer(
|
||||||
|
{ client, port, host }: CreateServerOptions,
|
||||||
|
handler: http.RequestListener
|
||||||
|
) {
|
||||||
const listener: http.RequestListener = (req, res) => {
|
const listener: http.RequestListener = (req, res) => {
|
||||||
if(req.url) {
|
if (req.url) {
|
||||||
const fileURL = new URL('.' + req.url, client);
|
const fileURL = new URL('.' + req.url, client);
|
||||||
|
|
||||||
const stream = send(req, fileURLToPath(fileURL), {
|
const stream = send(req, fileURLToPath(fileURL), {
|
||||||
|
@ -21,8 +24,8 @@ export function createServer({ client, port, host }: CreateServerOptions, handle
|
||||||
|
|
||||||
let forwardError = false;
|
let forwardError = false;
|
||||||
|
|
||||||
stream.on('error', err => {
|
stream.on('error', (err) => {
|
||||||
if(forwardError) {
|
if (forwardError) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error(err.toString());
|
console.error(err.toString());
|
||||||
res.writeHead(500);
|
res.writeHead(500);
|
||||||
|
@ -42,14 +45,18 @@ export function createServer({ client, port, host }: CreateServerOptions, handle
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let httpServer: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> |
|
let httpServer:
|
||||||
https.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
| http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>
|
||||||
|
| https.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
||||||
if(process.env.SERVER_CERT_PATH && process.env.SERVER_KEY_PATH) {
|
|
||||||
httpServer = https.createServer({
|
if (process.env.SERVER_CERT_PATH && process.env.SERVER_KEY_PATH) {
|
||||||
key: fs.readFileSync(process.env.SERVER_KEY_PATH),
|
httpServer = https.createServer(
|
||||||
cert: fs.readFileSync(process.env.SERVER_CERT_PATH),
|
{
|
||||||
}, listener);
|
key: fs.readFileSync(process.env.SERVER_KEY_PATH),
|
||||||
|
cert: fs.readFileSync(process.env.SERVER_CERT_PATH),
|
||||||
|
},
|
||||||
|
listener
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
httpServer = http.createServer(listener);
|
httpServer = http.createServer(listener);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,13 @@ export function getAdapter(options: Options): AstroAdapter {
|
||||||
serverEntrypoint: '@astrojs/node/server.js',
|
serverEntrypoint: '@astrojs/node/server.js',
|
||||||
previewEntrypoint: '@astrojs/node/preview.js',
|
previewEntrypoint: '@astrojs/node/preview.js',
|
||||||
exports: ['handler'],
|
exports: ['handler'],
|
||||||
args: options
|
args: options,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function createIntegration(userOptions: UserOptions): AstroIntegration {
|
export default function createIntegration(userOptions: UserOptions): AstroIntegration {
|
||||||
if(!userOptions?.mode) {
|
if (!userOptions?.mode) {
|
||||||
throw new Error(`[@astrojs/node] Setting the 'mode' option is required.`)
|
throw new Error(`[@astrojs/node] Setting the 'mode' option is required.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let needsBuildConfig = false;
|
let needsBuildConfig = false;
|
||||||
|
@ -38,11 +38,11 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr
|
||||||
},
|
},
|
||||||
'astro:build:start': ({ buildConfig }) => {
|
'astro:build:start': ({ buildConfig }) => {
|
||||||
// Backwards compat
|
// Backwards compat
|
||||||
if(needsBuildConfig) {
|
if (needsBuildConfig) {
|
||||||
_options.client = buildConfig.client.toString();
|
_options.client = buildConfig.client.toString();
|
||||||
_options.server = buildConfig.server.toString();
|
_options.server = buildConfig.server.toString();
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,12 @@ import type { NodeApp } from 'astro/app/node';
|
||||||
import type { IncomingMessage, ServerResponse } from 'http';
|
import type { IncomingMessage, ServerResponse } from 'http';
|
||||||
import type { Readable } from 'stream';
|
import type { Readable } from 'stream';
|
||||||
|
|
||||||
export default function(app: NodeApp) {
|
export default function (app: NodeApp) {
|
||||||
return async function(req: IncomingMessage, res: ServerResponse, next?: (err?: unknown) => void) {
|
return async function (
|
||||||
|
req: IncomingMessage,
|
||||||
|
res: ServerResponse,
|
||||||
|
next?: (err?: unknown) => void
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
const route = app.match(req);
|
const route = app.match(req);
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,27 @@
|
||||||
import type { CreatePreviewServer } from 'astro';
|
import type { CreatePreviewServer } from 'astro';
|
||||||
import type { createExports } from './server';
|
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import { createServer } from './http-server.js';
|
import { createServer } from './http-server.js';
|
||||||
|
import type { createExports } from './server';
|
||||||
|
|
||||||
const preview: CreatePreviewServer = async function({
|
const preview: CreatePreviewServer = async function ({ client, serverEntrypoint, host, port }) {
|
||||||
client,
|
|
||||||
serverEntrypoint,
|
|
||||||
host,
|
|
||||||
port,
|
|
||||||
}) {
|
|
||||||
type ServerModule = ReturnType<typeof createExports>;
|
type ServerModule = ReturnType<typeof createExports>;
|
||||||
type MaybeServerModule = Partial<ServerModule>;
|
type MaybeServerModule = Partial<ServerModule>;
|
||||||
let ssrHandler: ServerModule['handler'];
|
let ssrHandler: ServerModule['handler'];
|
||||||
try {
|
try {
|
||||||
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
||||||
const ssrModule: MaybeServerModule = await import(serverEntrypoint.toString());
|
const ssrModule: MaybeServerModule = await import(serverEntrypoint.toString());
|
||||||
if(typeof ssrModule.handler === 'function') {
|
if (typeof ssrModule.handler === 'function') {
|
||||||
ssrHandler = ssrModule.handler;
|
ssrHandler = ssrModule.handler;
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`The server entrypoint doesn't have a handler. Are you sure this is the right file?`);
|
throw new Error(
|
||||||
|
`The server entrypoint doesn't have a handler. Are you sure this is the right file?`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch(_err) {
|
} catch (_err) {
|
||||||
throw new Error(`The server entrypoint ${fileURLToPath} does not exist. Have you ran a build yet?`);
|
throw new Error(
|
||||||
|
`The server entrypoint ${fileURLToPath} does not exist. Have you ran a build yet?`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const handler: http.RequestListener = (req, res) => {
|
const handler: http.RequestListener = (req, res) => {
|
||||||
|
@ -37,18 +36,19 @@ const preview: CreatePreviewServer = async function({
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const server = createServer({
|
const server = createServer(
|
||||||
client,
|
{
|
||||||
port,
|
client,
|
||||||
host,
|
port,
|
||||||
}, handler);
|
host,
|
||||||
|
},
|
||||||
|
handler
|
||||||
|
);
|
||||||
|
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(`Preview server listening on http://${host}:${port}`);
|
console.log(`Preview server listening on http://${host}:${port}`);
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
}
|
|
||||||
|
|
||||||
export {
|
|
||||||
preview as default
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export { preview as default };
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import type { SSRManifest } from 'astro';
|
|
||||||
import type { Options } from './types';
|
|
||||||
import { polyfill } from '@astrojs/webapi';
|
import { polyfill } from '@astrojs/webapi';
|
||||||
|
import type { SSRManifest } from 'astro';
|
||||||
import { NodeApp } from 'astro/app/node';
|
import { NodeApp } from 'astro/app/node';
|
||||||
import middleware from './middleware.js';
|
import middleware from './middleware.js';
|
||||||
import startServer from './standalone.js';
|
import startServer from './standalone.js';
|
||||||
|
import type { Options } from './types';
|
||||||
|
|
||||||
polyfill(globalThis, {
|
polyfill(globalThis, {
|
||||||
exclude: 'window document',
|
exclude: 'window document',
|
||||||
|
@ -12,12 +12,12 @@ polyfill(globalThis, {
|
||||||
export function createExports(manifest: SSRManifest) {
|
export function createExports(manifest: SSRManifest) {
|
||||||
const app = new NodeApp(manifest);
|
const app = new NodeApp(manifest);
|
||||||
return {
|
return {
|
||||||
handler: middleware(app)
|
handler: middleware(app),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function start(manifest: SSRManifest, options: Options) {
|
export function start(manifest: SSRManifest, options: Options) {
|
||||||
if(options.mode !== 'standalone' || process.env.ASTRO_NODE_AUTOSTART === 'disabled') {
|
if (options.mode !== 'standalone' || process.env.ASTRO_NODE_AUTOSTART === 'disabled') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
import type { NodeApp } from 'astro/app/node';
|
import type { NodeApp } from 'astro/app/node';
|
||||||
import type { Options } from './types';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import middleware from './middleware.js';
|
|
||||||
import { createServer } from './http-server.js';
|
import { createServer } from './http-server.js';
|
||||||
|
import middleware from './middleware.js';
|
||||||
|
import type { Options } from './types';
|
||||||
|
|
||||||
function resolvePaths(options: Options) {
|
function resolvePaths(options: Options) {
|
||||||
const clientURLRaw = new URL(options.client);
|
const clientURLRaw = new URL(options.client);
|
||||||
const serverURLRaw = new URL(options.server);
|
const serverURLRaw = new URL(options.server);
|
||||||
const rel = path.relative(fileURLToPath(serverURLRaw), fileURLToPath(clientURLRaw));
|
const rel = path.relative(fileURLToPath(serverURLRaw), fileURLToPath(clientURLRaw));
|
||||||
|
|
||||||
const serverEntryURL = new URL(import.meta.url);
|
const serverEntryURL = new URL(import.meta.url);
|
||||||
const clientURL = new URL(appendForwardSlash(rel), serverEntryURL);
|
const clientURL = new URL(appendForwardSlash(rel), serverEntryURL);
|
||||||
|
|
||||||
|
@ -35,16 +35,19 @@ export function getResolvedHostForHttpServer(host: string | boolean) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function startServer(app: NodeApp, options: Options) {
|
export default function startServer(app: NodeApp, options: Options) {
|
||||||
const port = process.env.PORT ? Number(process.env.port) : (options.port ?? 8080);
|
const port = process.env.PORT ? Number(process.env.port) : options.port ?? 8080;
|
||||||
const { client } = resolvePaths(options);
|
const { client } = resolvePaths(options);
|
||||||
const handler = middleware(app);
|
const handler = middleware(app);
|
||||||
|
|
||||||
const host = getResolvedHostForHttpServer(options.host);
|
const host = getResolvedHostForHttpServer(options.host);
|
||||||
const server = createServer({
|
const server = createServer(
|
||||||
client,
|
{
|
||||||
port,
|
client,
|
||||||
host,
|
port,
|
||||||
}, handler);
|
host,
|
||||||
|
},
|
||||||
|
handler
|
||||||
|
);
|
||||||
|
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(`Server listening on http://${host}:${port}`);
|
console.log(`Server listening on http://${host}:${port}`);
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
|
|
||||||
export interface UserOptions {
|
export interface UserOptions {
|
||||||
/**
|
/**
|
||||||
* Specifies the mode that the adapter builds to.
|
* Specifies the mode that the adapter builds to.
|
||||||
*
|
*
|
||||||
* - 'middleware' - Build to middleware, to be used within another Node.js server, such as Express.
|
* - 'middleware' - Build to middleware, to be used within another Node.js server, such as Express.
|
||||||
* - 'standalone' - Build to a standalone server. The server starts up just by running the built script.
|
* - 'standalone' - Build to a standalone server. The server starts up just by running the built script.
|
||||||
*/
|
*/
|
||||||
mode: 'middleware' | 'standalone';
|
mode: 'middleware' | 'standalone';
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Options extends UserOptions {
|
export interface Options extends UserOptions {
|
||||||
|
|
Loading…
Add table
Reference in a new issue