From e16fbabbf113c7c5cd7fa916016cac7ce655554f Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 28 Mar 2022 11:21:53 -0400 Subject: [PATCH] Use our logger --- packages/astro/src/core/app/index.ts | 12 +++++----- packages/astro/src/core/build/generate.ts | 2 +- packages/astro/src/core/endpoint/dev/index.ts | 2 -- packages/astro/src/core/request.ts | 22 ++++++++++++++----- packages/astro/src/runtime/server/index.ts | 3 +-- .../src/vite-plugin-astro-server/index.ts | 7 +++++- .../astro/src/vite-plugin-build-html/index.ts | 8 +++++-- 7 files changed, 37 insertions(+), 19 deletions(-) diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 4044923986..df3c94a68b 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -10,6 +10,7 @@ import { call as callEndpoint } from '../endpoint/index.js'; import { RouteCache } from '../render/route-cache.js'; import { createLinkStylesheetElementSet, createModuleScriptElementWithSrcSet } from '../render/ssr-element.js'; import { prependForwardSlash } from '../path.js'; +import { createRequest } from '../request.js'; export class App { #manifest: Manifest; @@ -17,6 +18,7 @@ export class App { #routeDataToRouteInfo: Map; #routeCache: RouteCache; #encoder = new TextEncoder(); + #logging = defaultLogOptions; constructor(manifest: Manifest) { this.#manifest = manifest; @@ -63,7 +65,7 @@ export class App { const result = await render({ legacyBuild: false, links, - logging: defaultLogOptions, + logging: this.#logging, markdownRender: manifest.markdown.render, mod, origin: url.origin, @@ -81,8 +83,7 @@ export class App { routeCache: this.#routeCache, site: this.#manifest.site, ssr: true, - method: info.routeData.type === 'endpoint' ? '' : 'GET', - headers: request.headers, + request, }); if (result.type === 'response') { @@ -100,15 +101,14 @@ export class App { }); } - async #callEndpoint(request: Request, routeData: RouteData, mod: ComponentInstance): Promise { + async #callEndpoint(request: Request, _routeData: RouteData, mod: ComponentInstance): Promise { const url = new URL(request.url); const handler = mod as unknown as EndpointHandler; const result = await callEndpoint(handler, { - headers: request.headers, logging: defaultLogOptions, - method: request.method, origin: url.origin, pathname: url.pathname, + request, routeCache: this.#routeCache, ssr: true, }); diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index ad122596c2..2fe83262ae 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -205,7 +205,7 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G const fullyRelativePath = relPath[0] === '.' ? relPath : './' + relPath; return fullyRelativePath; }, - request: createRequest(url, new Headers()), + request: createRequest({ url, headers: new Headers(), logging }), route: pageData.route, routeCache, site: astroConfig.buildOptions.site, diff --git a/packages/astro/src/core/endpoint/dev/index.ts b/packages/astro/src/core/endpoint/dev/index.ts index 3190a500f4..6dc6d14b1e 100644 --- a/packages/astro/src/core/endpoint/dev/index.ts +++ b/packages/astro/src/core/endpoint/dev/index.ts @@ -4,8 +4,6 @@ import type { SSROptions } from '../../render/dev'; import { preload } from '../../render/dev/index.js'; import { errorHandler } from '../../render/dev/error.js'; import { call as callEndpoint } from '../index.js'; -import { getParamsAndProps, GetParamsAndPropsError } from '../../render/core.js'; -import { createRequest } from '../../render/request.js'; export async function call(ssrOpts: SSROptions) { try { diff --git a/packages/astro/src/core/request.ts b/packages/astro/src/core/request.ts index f82b2e4bb5..d0b711a9f0 100644 --- a/packages/astro/src/core/request.ts +++ b/packages/astro/src/core/request.ts @@ -1,8 +1,22 @@ import type { IncomingHttpHeaders } from 'http'; +import type { LogOptions } from './logger'; +import { warn } from './logger.js'; type HeaderType = Headers | Record | IncomingHttpHeaders; -export function createRequest(url: URL | string, headers: HeaderType, method: string = 'GET'): Request { +export interface CreateRequestOptions { + url: URL | string; + headers: HeaderType; + method?: string; + logging: LogOptions; +} + +export function createRequest({ + url, + headers, + method = 'GET', + logging +}: CreateRequestOptions): Request { let headersObj = headers instanceof Headers ? headers : new Headers(Object.entries(headers as Record)); @@ -14,15 +28,13 @@ export function createRequest(url: URL | string, headers: HeaderType, method: st Object.defineProperties(request, { canonicalURL: { get() { - /* eslint-disable no-console */ - console.warn(`Astro.request.canonicalURL has been moved to Astro.canonicalURL`); + warn(logging, 'deprecation', `Astro.request.canonicalURL has been moved to Astro.canonicalURL`); return undefined; } }, params: { get() { - /* eslint-disable no-console */ - console.warn(`Astro.request.params has been moved to Astro.params`); + warn(logging, 'deprecation', `Astro.request.params has been moved to Astro.params`); return undefined; } } diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index d977219acd..5382e0731c 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -1,6 +1,5 @@ import shorthash from 'shorthash'; import type { AstroComponentMetadata, AstroGlobalPartial, EndpointHandler, Params, SSRElement, SSRLoadedRenderer, SSRResult } from '../../@types/astro'; -import type { AstroRequest } from '../../core/render/request'; import { escapeHTML, HTMLString, markHTMLString } from './escape.js'; import { extractDirectives, generateHydrateScript, serializeProps } from './hydration.js'; import { serializeListValue } from './util.js'; @@ -397,7 +396,7 @@ export function defineScriptVars(vars: Record) { } // Renders an endpoint request to completion, returning the body. -export async function renderEndpoint(mod: EndpointHandler, request: AstroRequest, params: Params) { +export async function renderEndpoint(mod: EndpointHandler, request: Request, params: Params) { const chosenMethod = request.method?.toLowerCase() ?? 'get'; const handler = mod[chosenMethod]; diff --git a/packages/astro/src/vite-plugin-astro-server/index.ts b/packages/astro/src/vite-plugin-astro-server/index.ts index ec45ba8d51..de1f7e7930 100644 --- a/packages/astro/src/vite-plugin-astro-server/index.ts +++ b/packages/astro/src/vite-plugin-astro-server/index.ts @@ -129,7 +129,12 @@ async function handleRequest( } // Headers are only available when using SSR. - const request = createRequest(url, buildingToSSR ? req.headers : new Headers(), req.method); + const request = createRequest({ + url, + headers: buildingToSSR ? req.headers : new Headers(), + method: req.method, + logging + }); try { if (!pathname.startsWith(devRoot)) { diff --git a/packages/astro/src/vite-plugin-build-html/index.ts b/packages/astro/src/vite-plugin-build-html/index.ts index 1dc6e3392f..75680fdd1d 100644 --- a/packages/astro/src/vite-plugin-build-html/index.ts +++ b/packages/astro/src/vite-plugin-build-html/index.ts @@ -16,6 +16,7 @@ import { render as ssrRender } from '../core/render/dev/index.js'; import { getAstroStyleId, getAstroPageStyleId } from '../vite-plugin-build-css/index.js'; import { prependDotSlash, removeEndingForwardSlash } from '../core/path.js'; import { RouteCache } from '../core/render/route-cache.js'; +import { createRequest } from '../core/request.js'; // This package isn't real ESM, so have to coerce it const matchSrcset: typeof srcsetParse = (srcsetParse as any).default; @@ -87,8 +88,11 @@ export function rollupPluginAstroScanHTML(options: PluginOptions): VitePlugin { astroConfig, filePath: new URL(`./${component}`, astroConfig.projectRoot), logging, - headers: new Headers(), - method: 'GET', + request: createRequest({ + url: new URL(origin + pathname), + headers: new Headers(), + logging, + }), mode: 'production', origin, pathname,