[ci] format

This commit is contained in:
matthewp 2022-10-12 21:27:56 +00:00 committed by fredkbot
parent e55af8a232
commit 575bf9205e
20 changed files with 138 additions and 114 deletions

View file

@ -6,7 +6,7 @@ import node from '@astrojs/node';
export default defineConfig({
output: 'server',
adapter: node({
mode: 'standalone'
mode: 'standalone',
}),
integrations: [svelte()],
});

View file

@ -1422,7 +1422,9 @@ export interface PreviewServerParams {
port: number;
}
export type CreatePreviewServer = (params: PreviewServerParams) => PreviewServer | Promise<PreviewServer>;
export type CreatePreviewServer = (
params: PreviewServerParams
) => PreviewServer | Promise<PreviewServer>;
export interface PreviewModule {
default: CreatePreviewServer;

View file

@ -21,7 +21,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
format: 'directory',
client: './dist/client/',
server: './dist/server/',
serverEntry: 'entry.mjs'
serverEntry: 'entry.mjs',
},
server: {
host: false,
@ -112,10 +112,7 @@ export const AstroConfigSchema = z.object({
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.server)
.transform((val) => new URL(val)),
serverEntry: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
})
.optional()
.default({}),
@ -252,7 +249,8 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
.string()
.default(ASTRO_CONFIG_DEFAULTS.outDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
build: z.object({
build: z
.object({
format: z
.union([z.literal('file'), z.literal('directory')])
.optional()
@ -261,16 +259,13 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.client)
.transform(val => new URL(val, fileProtocolRoot)),
.transform((val) => new URL(val, fileProtocolRoot)),
server: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.server)
.transform(val => new URL(val, fileProtocolRoot)),
serverEntry: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
.transform((val) => new URL(val, fileProtocolRoot)),
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
})
.optional()
.default({}),
@ -306,13 +301,19 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
})
.optional()
.default({}),
}).transform(config => {
}).transform((config) => {
// If the user changed outDir but not build.server, build.config, adjust so those
// are relative to the outDir, as is the expected default.
if(!config.build.server.toString().startsWith(config.outDir.toString()) && config.build.server.toString().endsWith('dist/server/')) {
if (
!config.build.server.toString().startsWith(config.outDir.toString()) &&
config.build.server.toString().endsWith('dist/server/')
) {
config.build.server = new URL('./dist/server/', config.outDir);
}
if(!config.build.client.toString().startsWith(config.outDir.toString()) && config.build.client.toString().endsWith('dist/client/')) {
if (
!config.build.client.toString().startsWith(config.outDir.toString()) &&
config.build.client.toString().endsWith('dist/client/')
) {
config.build.client = new URL('./dist/client/', config.outDir);
}
return config;

View file

@ -1,9 +1,9 @@
import type { AstroTelemetry } from '@astrojs/telemetry';
import { createRequire } from 'module';
import type { AstroSettings, PreviewModule, PreviewServer } from '../../@types/astro';
import { runHookConfigDone, runHookConfigSetup } from '../../integrations/index.js';
import type { LogOptions } from '../logger/core';
import createStaticPreviewServer from './static-preview-server.js';
import { createRequire } from 'module';
import { getResolvedHostForHttpServer } from './util.js';
interface PreviewOptions {
@ -51,7 +51,7 @@ export default async function preview(
client: settings.config.build.client,
serverEntrypoint: new URL(settings.config.build.serverEntry, settings.config.build.server),
host,
port
port,
});
return server;

View file

@ -212,7 +212,10 @@ export async function runHookBuildStart({
buildConfig: BuildConfig;
logging: LogOptions;
}) {
function warnDeprecated(integration: AstroIntegration, prop: 'server' | 'client' | 'serverEntry') {
function warnDeprecated(
integration: AstroIntegration,
prop: 'server' | 'client' | 'serverEntry'
) {
let value: any = Reflect.get(buildConfig, prop);
Object.defineProperty(buildConfig, prop, {
enumerable: true,
@ -221,17 +224,22 @@ export async function runHookBuildStart({
},
set(newValue) {
value = newValue;
warn(logging, 'astro:build:start', `Your adapter ${bold(integration.name)} is using a deprecated API, buildConfig. ${bold(prop)} config should be set via config.build.${prop} instead.`);
}
warn(
logging,
'astro:build:start',
`Your adapter ${bold(integration.name)} is using a deprecated API, buildConfig. ${bold(
prop
)} config should be set via config.build.${prop} instead.`
);
},
});
return () => {
Object.defineProperty(buildConfig, prop, {
enumerable: true,
value
value,
});
};
}
}
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:start']) {

View file

@ -48,7 +48,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
client: new URL('./static/', config.outDir),
server: new URL('./', config.outDir),
serverEntry: '_worker.js',
}
},
});
},
'astro:config:done': ({ setAdapter, config }) => {

View file

@ -34,7 +34,7 @@ function netlifyFunctions({
build: {
client: outDir,
server: new URL('./.netlify/functions-internal/', config.root),
}
},
});
},
'astro:config:done': ({ config, setAdapter }) => {

View file

@ -1,8 +1,8 @@
import fs from 'fs';
import http from 'http';
import https from 'https';
import { fileURLToPath } from 'url';
import send from 'send';
import { fileURLToPath } from 'url';
interface CreateServerOptions {
client: URL;
@ -10,7 +10,10 @@ interface CreateServerOptions {
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) => {
if (req.url) {
const fileURL = new URL('.' + req.url, client);
@ -21,7 +24,7 @@ export function createServer({ client, port, host }: CreateServerOptions, handle
let forwardError = false;
stream.on('error', err => {
stream.on('error', (err) => {
if (forwardError) {
// eslint-disable-next-line no-console
console.error(err.toString());
@ -42,14 +45,18 @@ export function createServer({ client, port, host }: CreateServerOptions, handle
}
};
let httpServer: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> |
https.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
let httpServer:
| 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({
httpServer = https.createServer(
{
key: fs.readFileSync(process.env.SERVER_KEY_PATH),
cert: fs.readFileSync(process.env.SERVER_CERT_PATH),
}, listener);
},
listener
);
} else {
httpServer = http.createServer(listener);
}

View file

@ -7,13 +7,13 @@ export function getAdapter(options: Options): AstroAdapter {
serverEntrypoint: '@astrojs/node/server.js',
previewEntrypoint: '@astrojs/node/preview.js',
exports: ['handler'],
args: options
args: options,
};
}
export default function createIntegration(userOptions: UserOptions): AstroIntegration {
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;
@ -42,7 +42,7 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr
_options.client = buildConfig.client.toString();
_options.server = buildConfig.server.toString();
}
}
},
},
};
}

View file

@ -3,7 +3,11 @@ import type { IncomingMessage, ServerResponse } from 'http';
import type { Readable } from 'stream';
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 {
const route = app.match(req);

View file

@ -1,15 +1,10 @@
import type { CreatePreviewServer } from 'astro';
import type { createExports } from './server';
import http from 'http';
import { fileURLToPath } from 'url';
import { createServer } from './http-server.js';
import type { createExports } from './server';
const preview: CreatePreviewServer = async function({
client,
serverEntrypoint,
host,
port,
}) {
const preview: CreatePreviewServer = async function ({ client, serverEntrypoint, host, port }) {
type ServerModule = ReturnType<typeof createExports>;
type MaybeServerModule = Partial<ServerModule>;
let ssrHandler: ServerModule['handler'];
@ -19,10 +14,14 @@ const preview: CreatePreviewServer = async function({
if (typeof ssrModule.handler === 'function') {
ssrHandler = ssrModule.handler;
} 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) {
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) => {
@ -37,18 +36,19 @@ const preview: CreatePreviewServer = async function({
});
};
const server = createServer({
const server = createServer(
{
client,
port,
host,
}, handler);
},
handler
);
// eslint-disable-next-line no-console
console.log(`Preview server listening on http://${host}:${port}`);
return server;
}
export {
preview as default
};
export { preview as default };

View file

@ -1,9 +1,9 @@
import type { SSRManifest } from 'astro';
import type { Options } from './types';
import { polyfill } from '@astrojs/webapi';
import type { SSRManifest } from 'astro';
import { NodeApp } from 'astro/app/node';
import middleware from './middleware.js';
import startServer from './standalone.js';
import type { Options } from './types';
polyfill(globalThis, {
exclude: 'window document',
@ -12,7 +12,7 @@ polyfill(globalThis, {
export function createExports(manifest: SSRManifest) {
const app = new NodeApp(manifest);
return {
handler: middleware(app)
handler: middleware(app),
};
}

View file

@ -1,9 +1,9 @@
import type { NodeApp } from 'astro/app/node';
import type { Options } from './types';
import path from 'path';
import { fileURLToPath } from 'url';
import middleware from './middleware.js';
import { createServer } from './http-server.js';
import middleware from './middleware.js';
import type { Options } from './types';
function resolvePaths(options: Options) {
const clientURLRaw = new URL(options.client);
@ -35,16 +35,19 @@ export function getResolvedHostForHttpServer(host: string | boolean) {
}
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 handler = middleware(app);
const host = getResolvedHostForHttpServer(options.host);
const server = createServer({
const server = createServer(
{
client,
port,
host,
}, handler);
},
handler
);
// eslint-disable-next-line no-console
console.log(`Server listening on http://${host}:${port}`);

View file

@ -1,4 +1,3 @@
export interface UserOptions {
/**
* Specifies the mode that the adapter builds to.

View file

@ -31,7 +31,7 @@ export default function vercelEdge(): AstroIntegration {
serverEntry: 'entry.mjs',
client: new URL('./static/', outDir),
server: new URL('./functions/render.func/', config.outDir),
}
},
});
},
'astro:config:done': ({ setAdapter, config }) => {

View file

@ -33,7 +33,7 @@ export default function vercelEdge(): AstroIntegration {
serverEntry: 'entry.js',
client: new URL('./static/', outDir),
server: new URL('./dist/', config.root),
}
},
});
},
'astro:config:done': ({ setAdapter, config }) => {