0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Use our logger

This commit is contained in:
Matthew Phillips 2022-03-28 11:21:53 -04:00
parent 7a63f46b35
commit e16fbabbf1
7 changed files with 37 additions and 19 deletions

View file

@ -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<RouteData, RouteInfo>;
#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<Response> {
async #callEndpoint(request: Request, _routeData: RouteData, mod: ComponentInstance): Promise<Response> {
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,
});

View file

@ -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,

View file

@ -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 {

View file

@ -1,8 +1,22 @@
import type { IncomingHttpHeaders } from 'http';
import type { LogOptions } from './logger';
import { warn } from './logger.js';
type HeaderType = Headers | Record<string, any> | 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<string, any>));
@ -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;
}
}

View file

@ -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<any, any>) {
}
// 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];

View file

@ -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)) {

View file

@ -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,