diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 3efbb7ae8..86d7bc984 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -24,6 +24,8 @@ import type { AstroIntegrationLogger, Logger, LoggerLevel } from '../core/logger import type { AstroComponentFactory, AstroComponentInstance } from '../runtime/server'; import type { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../core/constants.js'; +export { type AstroIntegrationLogger }; + export type { MarkdownHeading, MarkdownMetadata, @@ -2203,6 +2205,7 @@ export interface PreviewServerParams { host: string | undefined; port: number; base: string; + logger: AstroIntegrationLogger; } export type CreatePreviewServer = ( diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 8fa3f5f14..5773b16d3 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -27,6 +27,7 @@ import { matchRoute } from '../routing/match.js'; import { EndpointNotFoundError, SSRRoutePipeline } from './ssrPipeline.js'; import type { RouteInfo } from './types'; export { deserializeManifest } from './common.js'; +import { AstroIntegrationLogger } from '../logger/core.js'; const clientLocalsSymbol = Symbol.for('astro.locals'); @@ -58,6 +59,7 @@ export class App { #pipeline: SSRRoutePipeline; #onRequest: MiddlewareEndpointHandler | undefined; #middlewareLoaded: boolean; + #adapterLogger: AstroIntegrationLogger; constructor(manifest: SSRManifest, streaming = true) { this.#manifest = manifest; @@ -68,10 +70,14 @@ export class App { this.#baseWithoutTrailingSlash = removeTrailingForwardSlash(this.#manifest.base); this.#pipeline = new SSRRoutePipeline(this.#createEnvironment(streaming)); this.#middlewareLoaded = false; + this.#adapterLogger = new AstroIntegrationLogger( + this.#logger.options, + this.#manifest.adapterName + ); } - set setManifest(newManifest: SSRManifest) { - this.#manifest = newManifest; + getAdapterLogger(): AstroIntegrationLogger { + return this.#adapterLogger; } /** diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index a4e7e7a7b..2ad7ab5aa 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -9,6 +9,7 @@ import { createNodeLogger } from '../config/logging.js'; import { createSettings } from '../config/settings.js'; import createStaticPreviewServer from './static-preview-server.js'; import { getResolvedHostForHttpServer } from './util.js'; +import { AstroIntegrationLogger } from '../../core/logger/core.js'; /** * Starts a local server to serve your static dist/ directory. This command is useful for previewing @@ -62,6 +63,7 @@ export default async function preview(inlineConfig: AstroInlineConfig): Promise< host: getResolvedHostForHttpServer(settings.config.server.host), port: settings.config.server.port, base: settings.config.base, + logger: new AstroIntegrationLogger(logger.options, settings.adapter.name), }); return server; diff --git a/packages/integrations/cloudflare/src/runtime.ts b/packages/integrations/cloudflare/src/runtime.ts deleted file mode 100644 index 03c15d4a3..000000000 --- a/packages/integrations/cloudflare/src/runtime.ts +++ /dev/null @@ -1,45 +0,0 @@ -// TODO: remove `getRuntime()` in Astro 3.0 -import type { Cache, CacheStorage, IncomingRequestCfProperties } from '@cloudflare/workers-types'; - -export type WorkerRuntime = { - name: 'cloudflare'; - env: T; - waitUntil(promise: Promise): void; - passThroughOnException(): void; - caches?: CacheStorage & { default: Cache }; - cf?: IncomingRequestCfProperties; -}; - -export type PagesRuntime = { - name: 'cloudflare'; - env: T; - functionPath: string; - params: Record; - data: U; - waitUntil(promise: Promise): void; - next(request: Request): void; - caches?: CacheStorage & { default: Cache }; - cf?: IncomingRequestCfProperties; -}; - -/** - * @deprecated since version 6.8.0 - * The `getRuntime` utility has been deprecated and should be updated to the new [`Astro.locals`](https://docs.astro.build/en/guides/middleware/#locals) API. - * ```diff - * - import { getRuntime } from '@astrojs/cloudflare/runtime'; - * - getRuntime(Astro.request); - * - * + const runtime = Astro.locals.runtime; - * ``` - */ -export function getRuntime( - request: Request -): WorkerRuntime | PagesRuntime { - if (!!request) { - return Reflect.get(request, Symbol.for('runtime')); - } else { - throw new Error( - 'To retrieve the current cloudflare runtime you need to pass in the Astro request object' - ); - } -} diff --git a/packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro b/packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro index 6ba48e803..0e904752f 100644 --- a/packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro +++ b/packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro @@ -1,6 +1,5 @@ --- -import { getRuntime } from '@astrojs/cloudflare/runtime'; -const runtime = getRuntime(Astro.request); +const runtime = Astro.locals.runtime; --- diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts index 7435a60ed..20f33f4a8 100644 --- a/packages/integrations/node/src/index.ts +++ b/packages/integrations/node/src/index.ts @@ -1,6 +1,6 @@ import type { AstroAdapter, AstroIntegration } from 'astro'; import type { Options, UserOptions } from './types'; - +import { AstroError } from 'astro/errors'; export function getAdapter(options: Options): AstroAdapter { return { name: '@astrojs/node', @@ -23,7 +23,7 @@ export function getAdapter(options: Options): AstroAdapter { export default function createIntegration(userOptions: UserOptions): AstroIntegration { if (!userOptions?.mode) { - throw new Error(`[@astrojs/node] Setting the 'mode' option is required.`); + throw new AstroError(`Setting the 'mode' option is required.`); } let _options: Options; diff --git a/packages/integrations/node/src/preview.ts b/packages/integrations/node/src/preview.ts index 62a4301c1..2928ac0bf 100644 --- a/packages/integrations/node/src/preview.ts +++ b/packages/integrations/node/src/preview.ts @@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url'; import { getNetworkAddress } from './get-network-address.js'; import { createServer } from './http-server.js'; import type { createExports } from './server'; +import { AstroError } from 'astro/errors'; const preview: CreatePreviewServer = async function ({ client, @@ -11,6 +12,7 @@ const preview: CreatePreviewServer = async function ({ host, port, base, + logger, }) { type ServerModule = ReturnType; type MaybeServerModule = Partial; @@ -21,13 +23,13 @@ const preview: CreatePreviewServer = async function ({ if (typeof ssrModule.handler === 'function') { ssrHandler = ssrModule.handler; } else { - throw new Error( + throw new AstroError( `The server entrypoint doesn't have a handler. Are you sure this is the right file?` ); } } catch (err) { if ((err as any).code === 'ERR_MODULE_NOT_FOUND') { - throw new Error( + throw new AstroError( `The server entrypoint ${fileURLToPath( serverEntrypoint )} does not exist. Have you ran a build yet?` @@ -63,13 +65,11 @@ const preview: CreatePreviewServer = async function ({ const address = getNetworkAddress('http', host, port); if (host === undefined) { - // eslint-disable-next-line no-console - console.log( + logger.info( `Preview server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n` ); } else { - // eslint-disable-next-line no-console - console.log(`Preview server listening on ${address.local[0]}`); + logger.info(`Preview server listening on ${address.local[0]}`); } return server; diff --git a/packages/integrations/node/src/response-iterator.ts b/packages/integrations/node/src/response-iterator.ts index 0dd6e63af..ef7a42c40 100644 --- a/packages/integrations/node/src/response-iterator.ts +++ b/packages/integrations/node/src/response-iterator.ts @@ -6,6 +6,7 @@ import type { ReadableStreamDefaultReadResult } from 'node:stream/web'; import { Readable as NodeReadableStream } from 'stream'; +import { AstroError } from 'astro/errors'; interface NodeStreamIterator { next(): Promise>; @@ -221,5 +222,7 @@ export function responseIterator(response: Response | Buffer): AsyncIterableI if (isNodeReadableStream(body)) return nodeStreamIterator(body); - throw new Error('Unknown body type for responseIterator. Please pass a streamable response.'); + throw new AstroError( + 'Unknown body type for responseIterator. Please pass a streamable response.' + ); } diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts index 94dc26758..66d1b9c6a 100644 --- a/packages/integrations/node/src/standalone.ts +++ b/packages/integrations/node/src/standalone.ts @@ -37,6 +37,7 @@ export function getResolvedHostForHttpServer(host: string | boolean) { } export default function startServer(app: NodeApp, options: Options) { + const logger = app.getAdapterLogger(); const port = process.env.PORT ? Number(process.env.PORT) : options.port ?? 8080; const { client } = resolvePaths(options); const handler = middleware(app, options.mode); @@ -59,13 +60,11 @@ export default function startServer(app: NodeApp, options: Options) { const address = getNetworkAddress(protocol, host, port); if (host === undefined) { - // eslint-disable-next-line no-console - console.log( - `Preview server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n` + logger.info( + `Server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n` ); } else { - // eslint-disable-next-line no-console - console.log(`Preview server listening on ${address.local[0]}`); + logger.info(`Server listening on ${address.local[0]}`); } return { diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index 0bfdd31f7..22c35b87f 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -1,5 +1,5 @@ import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro'; - +import { AstroError } from 'astro/errors'; import glob from 'fast-glob'; import { basename } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; @@ -136,10 +136,9 @@ export default function vercelServerless({ serverEntry = config.build.serverEntry; if (config.output === 'static') { - throw new Error(` - [@astrojs/vercel] \`output: "server"\` or \`output: "hybrid"\` is required to use the serverless adapter. - - `); + throw new AstroError( + '`output: "server"` or `output: "hybrid"` is required to use the serverless adapter.' + ); } },