From 575bf9205e7b5875c6b95d49339ff448f59eaad4 Mon Sep 17 00:00:00 2001 From: matthewp Date: Wed, 12 Oct 2022 21:27:56 +0000 Subject: [PATCH] [ci] format --- examples/ssr/astro.config.mjs | 2 +- packages/astro/src/@types/astro.ts | 16 ++--- packages/astro/src/core/config/config.ts | 2 +- packages/astro/src/core/config/schema.ts | 59 ++++++++++--------- packages/astro/src/core/preview/index.ts | 6 +- packages/astro/src/integrations/index.ts | 20 +++++-- packages/integrations/cloudflare/src/index.ts | 4 +- packages/integrations/deno/src/index.ts | 2 +- packages/integrations/image/src/index.ts | 2 +- .../netlify/src/integration-edge-functions.ts | 2 +- .../netlify/src/integration-functions.ts | 4 +- packages/integrations/node/src/http-server.ts | 33 +++++++---- packages/integrations/node/src/index.ts | 10 ++-- packages/integrations/node/src/middleware.ts | 8 ++- packages/integrations/node/src/preview.ts | 40 ++++++------- packages/integrations/node/src/server.ts | 8 +-- packages/integrations/node/src/standalone.ts | 21 ++++--- packages/integrations/node/src/types.ts | 5 +- .../integrations/vercel/src/edge/adapter.ts | 4 +- .../vercel/src/serverless/adapter.ts | 4 +- 20 files changed, 138 insertions(+), 114 deletions(-) diff --git a/examples/ssr/astro.config.mjs b/examples/ssr/astro.config.mjs index 2ff8bbaf5..b79949397 100644 --- a/examples/ssr/astro.config.mjs +++ b/examples/ssr/astro.config.mjs @@ -6,7 +6,7 @@ import node from '@astrojs/node'; export default defineConfig({ output: 'server', adapter: node({ - mode: 'standalone' + mode: 'standalone', }), integrations: [svelte()], }); diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 7157de666..6fd80b1d8 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -544,9 +544,9 @@ export interface AstroUserConfig { * @description * Controls the output directory of your client-side CSS and JavaScript when `output: 'server'` only. * `outDir` controls where the code is built to. - * + * * This value is relative to the `outDir`. - * + * * ```js * { * output: 'server', @@ -564,9 +564,9 @@ export interface AstroUserConfig { * @default `'./dist/server'` * @description * Controls the output directory of server JavaScript when building to SSR. - * + * * This value is relative to the `outDir`. - * + * * ```js * { * build: { @@ -585,10 +585,10 @@ export interface AstroUserConfig { * Specifies the file name of the server entrypoint when building to SSR. * This entrypoint is usually dependent on which host you are deploying to and * will be set by your adapter for you. - * + * * Note that it is recommended that this file ends with `.mjs` so that the runtime * detects that the file is a JavaScript module. - * + * * ```js * { * build: { @@ -1422,7 +1422,9 @@ export interface PreviewServerParams { port: number; } -export type CreatePreviewServer = (params: PreviewServerParams) => PreviewServer | Promise; +export type CreatePreviewServer = ( + params: PreviewServerParams +) => PreviewServer | Promise; export interface PreviewModule { default: CreatePreviewServer; diff --git a/packages/astro/src/core/config/config.ts b/packages/astro/src/core/config/config.ts index d939c6e87..840971e56 100644 --- a/packages/astro/src/core/config/config.ts +++ b/packages/astro/src/core/config/config.ts @@ -346,7 +346,7 @@ function mergeConfigRecursively( merged[key] = [...arraify(existing ?? []), ...arraify(value ?? [])]; continue; } - if(isURL(existing) && isURL(value)) { + if (isURL(existing) && isURL(value)) { merged[key] = value; continue; } diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 3c390c650..794c756ce 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -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,28 +249,26 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) { .string() .default(ASTRO_CONFIG_DEFAULTS.outDir) .transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)), - build: z.object({ - format: z - .union([z.literal('file'), z.literal('directory')]) + build: z + .object({ + format: z + .union([z.literal('file'), z.literal('directory')]) + .optional() + .default(ASTRO_CONFIG_DEFAULTS.build.format), + client: z + .string() + .optional() + .default(ASTRO_CONFIG_DEFAULTS.build.client) + .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), + }) .optional() - .default(ASTRO_CONFIG_DEFAULTS.build.format), - client: z - .string() - .optional() - .default(ASTRO_CONFIG_DEFAULTS.build.client) - .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), - }) - .optional() - .default({}), + .default({}), server: z.preprocess( // preprocess (val) => @@ -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; diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index ab244b14c..276e2d067 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -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 { @@ -42,7 +42,7 @@ export default async function preview( const previewEntrypoint = require.resolve(settings.adapter.previewEntrypoint); const previewModule = (await import(previewEntrypoint)) as Partial; - if(typeof previewModule.default !== 'function') { + if (typeof previewModule.default !== 'function') { throw new Error(`[preview] ${settings.adapter.name} cannot preview your app.`); } @@ -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; diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 7e333dbf9..7e826d48d 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -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,18 +224,23 @@ 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']) { const undoClientWarning = warnDeprecated(integration, 'client'); diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 9cf6412b8..44112e8be 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -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 }) => { @@ -83,7 +83,7 @@ export default function createIntegration(args?: Options): AstroIntegration { }, 'astro:build:start': ({ buildConfig }) => { // Backwards compat - if(needsBuildConfig) { + if (needsBuildConfig) { buildConfig.client = new URL('./static/', _config.outDir); buildConfig.server = new URL('./', _config.outDir); buildConfig.serverEntry = '_worker.js'; diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index 9b0032710..968bec24a 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -49,7 +49,7 @@ export default function createIntegration(args?: Options): AstroIntegration { }, 'astro:build:start': ({ buildConfig }) => { // Backwards compat - if(needsBuildConfig) { + if (needsBuildConfig) { _buildConfig = buildConfig; } }, diff --git a/packages/integrations/image/src/index.ts b/packages/integrations/image/src/index.ts index 46b14b6b8..24e70a35d 100644 --- a/packages/integrations/image/src/index.ts +++ b/packages/integrations/image/src/index.ts @@ -100,7 +100,7 @@ export default function integration(options: IntegrationOptions = {}): AstroInte }, 'astro:build:start': ({ buildConfig }) => { // Backwards compat - if(needsBuildConfig) { + if (needsBuildConfig) { _buildConfig = buildConfig; } }, diff --git a/packages/integrations/netlify/src/integration-edge-functions.ts b/packages/integrations/netlify/src/integration-edge-functions.ts index b69667dde..f9e5f449d 100644 --- a/packages/integrations/netlify/src/integration-edge-functions.ts +++ b/packages/integrations/netlify/src/integration-edge-functions.ts @@ -157,7 +157,7 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}) } }, 'astro:build:start': ({ buildConfig }) => { - if(needsBuildConfig) { + if (needsBuildConfig) { buildConfig.client = _config.outDir; buildConfig.server = new URL('./.netlify/edge-functions/', _config.root); buildConfig.serverEntry = 'entry.js'; diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts index 025250bc1..e1d6de420 100644 --- a/packages/integrations/netlify/src/integration-functions.ts +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -34,7 +34,7 @@ function netlifyFunctions({ build: { client: outDir, server: new URL('./.netlify/functions-internal/', config.root), - } + }, }); }, 'astro:config:done': ({ config, setAdapter }) => { @@ -50,7 +50,7 @@ function netlifyFunctions({ } }, 'astro:build:start': ({ buildConfig }) => { - if(needsBuildConfig) { + if (needsBuildConfig) { buildConfig.client = _config.outDir; buildConfig.server = new URL('./.netlify/functions-internal/', _config.root); entryFile = buildConfig.serverEntry.replace(/\.m?js/, ''); diff --git a/packages/integrations/node/src/http-server.ts b/packages/integrations/node/src/http-server.ts index 34192c5f9..98cde3728 100644 --- a/packages/integrations/node/src/http-server.ts +++ b/packages/integrations/node/src/http-server.ts @@ -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,9 +10,12 @@ 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) { + if (req.url) { const fileURL = new URL('.' + req.url, client); const stream = send(req, fileURLToPath(fileURL), { @@ -21,8 +24,8 @@ export function createServer({ client, port, host }: CreateServerOptions, handle let forwardError = false; - stream.on('error', err => { - if(forwardError) { + stream.on('error', (err) => { + if (forwardError) { // eslint-disable-next-line no-console console.error(err.toString()); res.writeHead(500); @@ -42,14 +45,18 @@ export function createServer({ client, port, host }: CreateServerOptions, handle } }; - let httpServer: http.Server | - https.Server; - - if(process.env.SERVER_CERT_PATH && process.env.SERVER_KEY_PATH) { - httpServer = https.createServer({ - key: fs.readFileSync(process.env.SERVER_KEY_PATH), - cert: fs.readFileSync(process.env.SERVER_CERT_PATH), - }, listener); + let httpServer: + | http.Server + | https.Server; + + if (process.env.SERVER_CERT_PATH && process.env.SERVER_KEY_PATH) { + httpServer = https.createServer( + { + key: fs.readFileSync(process.env.SERVER_KEY_PATH), + cert: fs.readFileSync(process.env.SERVER_CERT_PATH), + }, + listener + ); } else { httpServer = http.createServer(listener); } diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts index 80dfacdab..095f0b6b0 100644 --- a/packages/integrations/node/src/index.ts +++ b/packages/integrations/node/src/index.ts @@ -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.`) + if (!userOptions?.mode) { + throw new Error(`[@astrojs/node] Setting the 'mode' option is required.`); } let needsBuildConfig = false; @@ -38,11 +38,11 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr }, 'astro:build:start': ({ buildConfig }) => { // Backwards compat - if(needsBuildConfig) { + if (needsBuildConfig) { _options.client = buildConfig.client.toString(); _options.server = buildConfig.server.toString(); } - } + }, }, }; } diff --git a/packages/integrations/node/src/middleware.ts b/packages/integrations/node/src/middleware.ts index 772461f2a..a1058399d 100644 --- a/packages/integrations/node/src/middleware.ts +++ b/packages/integrations/node/src/middleware.ts @@ -2,8 +2,12 @@ import type { NodeApp } from 'astro/app/node'; 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) { +export default function (app: NodeApp) { + return async function ( + req: IncomingMessage, + res: ServerResponse, + next?: (err?: unknown) => void + ) { try { const route = app.match(req); diff --git a/packages/integrations/node/src/preview.ts b/packages/integrations/node/src/preview.ts index 33c2f18e2..443befa19 100644 --- a/packages/integrations/node/src/preview.ts +++ b/packages/integrations/node/src/preview.ts @@ -1,28 +1,27 @@ 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; type MaybeServerModule = Partial; let ssrHandler: ServerModule['handler']; try { process.env.ASTRO_NODE_AUTOSTART = 'disabled'; const ssrModule: MaybeServerModule = await import(serverEntrypoint.toString()); - if(typeof ssrModule.handler === '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?`); + } catch (_err) { + 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({ - client, - port, - host, - }, handler); + const server = createServer( + { + client, + port, + host, + }, + 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 }; diff --git a/packages/integrations/node/src/server.ts b/packages/integrations/node/src/server.ts index 202e66b7e..9cff04cf5 100644 --- a/packages/integrations/node/src/server.ts +++ b/packages/integrations/node/src/server.ts @@ -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,12 +12,12 @@ polyfill(globalThis, { export function createExports(manifest: SSRManifest) { const app = new NodeApp(manifest); return { - handler: middleware(app) + handler: middleware(app), }; } 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; } diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts index 8fef96ed5..e0435a2ea 100644 --- a/packages/integrations/node/src/standalone.ts +++ b/packages/integrations/node/src/standalone.ts @@ -1,15 +1,15 @@ 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); const serverURLRaw = new URL(options.server); const rel = path.relative(fileURLToPath(serverURLRaw), fileURLToPath(clientURLRaw)); - + const serverEntryURL = new URL(import.meta.url); 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) { - 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({ - client, - port, - host, - }, handler); + const server = createServer( + { + client, + port, + host, + }, + handler + ); // eslint-disable-next-line no-console console.log(`Server listening on http://${host}:${port}`); diff --git a/packages/integrations/node/src/types.ts b/packages/integrations/node/src/types.ts index aaf3be942..2a9ecc2a1 100644 --- a/packages/integrations/node/src/types.ts +++ b/packages/integrations/node/src/types.ts @@ -1,12 +1,11 @@ - export interface UserOptions { /** * Specifies the mode that the adapter builds to. - * + * * - '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. */ - mode: 'middleware' | 'standalone'; + mode: 'middleware' | 'standalone'; } export interface Options extends UserOptions { diff --git a/packages/integrations/vercel/src/edge/adapter.ts b/packages/integrations/vercel/src/edge/adapter.ts index ecd13e1f8..018ab7032 100644 --- a/packages/integrations/vercel/src/edge/adapter.ts +++ b/packages/integrations/vercel/src/edge/adapter.ts @@ -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 }) => { @@ -41,7 +41,7 @@ export default function vercelEdge(): AstroIntegration { functionFolder = config.build.server; }, 'astro:build:start': ({ buildConfig }) => { - if(needsBuildConfig) { + if (needsBuildConfig) { buildConfig.client = new URL('./static/', _config.outDir); serverEntry = buildConfig.serverEntry = 'entry.mjs'; functionFolder = buildConfig.server = new URL('./functions/render.func/', _config.outDir); diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index dfc0367ae..5b65c331e 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -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 }) => { @@ -51,7 +51,7 @@ export default function vercelEdge(): AstroIntegration { } }, 'astro:build:start': ({ buildConfig }) => { - if(needsBuildConfig) { + if (needsBuildConfig) { buildConfig.client = new URL('./static/', _config.outDir); buildTempFolder = buildConfig.server = new URL('./dist/', _config.root); serverEntry = buildConfig.serverEntry = 'entry.js';