mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
Use our logger
This commit is contained in:
parent
7a63f46b35
commit
e16fbabbf1
7 changed files with 37 additions and 19 deletions
|
@ -10,6 +10,7 @@ import { call as callEndpoint } from '../endpoint/index.js';
|
||||||
import { RouteCache } from '../render/route-cache.js';
|
import { RouteCache } from '../render/route-cache.js';
|
||||||
import { createLinkStylesheetElementSet, createModuleScriptElementWithSrcSet } from '../render/ssr-element.js';
|
import { createLinkStylesheetElementSet, createModuleScriptElementWithSrcSet } from '../render/ssr-element.js';
|
||||||
import { prependForwardSlash } from '../path.js';
|
import { prependForwardSlash } from '../path.js';
|
||||||
|
import { createRequest } from '../request.js';
|
||||||
|
|
||||||
export class App {
|
export class App {
|
||||||
#manifest: Manifest;
|
#manifest: Manifest;
|
||||||
|
@ -17,6 +18,7 @@ export class App {
|
||||||
#routeDataToRouteInfo: Map<RouteData, RouteInfo>;
|
#routeDataToRouteInfo: Map<RouteData, RouteInfo>;
|
||||||
#routeCache: RouteCache;
|
#routeCache: RouteCache;
|
||||||
#encoder = new TextEncoder();
|
#encoder = new TextEncoder();
|
||||||
|
#logging = defaultLogOptions;
|
||||||
|
|
||||||
constructor(manifest: Manifest) {
|
constructor(manifest: Manifest) {
|
||||||
this.#manifest = manifest;
|
this.#manifest = manifest;
|
||||||
|
@ -63,7 +65,7 @@ export class App {
|
||||||
const result = await render({
|
const result = await render({
|
||||||
legacyBuild: false,
|
legacyBuild: false,
|
||||||
links,
|
links,
|
||||||
logging: defaultLogOptions,
|
logging: this.#logging,
|
||||||
markdownRender: manifest.markdown.render,
|
markdownRender: manifest.markdown.render,
|
||||||
mod,
|
mod,
|
||||||
origin: url.origin,
|
origin: url.origin,
|
||||||
|
@ -81,8 +83,7 @@ export class App {
|
||||||
routeCache: this.#routeCache,
|
routeCache: this.#routeCache,
|
||||||
site: this.#manifest.site,
|
site: this.#manifest.site,
|
||||||
ssr: true,
|
ssr: true,
|
||||||
method: info.routeData.type === 'endpoint' ? '' : 'GET',
|
request,
|
||||||
headers: request.headers,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (result.type === 'response') {
|
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 url = new URL(request.url);
|
||||||
const handler = mod as unknown as EndpointHandler;
|
const handler = mod as unknown as EndpointHandler;
|
||||||
const result = await callEndpoint(handler, {
|
const result = await callEndpoint(handler, {
|
||||||
headers: request.headers,
|
|
||||||
logging: defaultLogOptions,
|
logging: defaultLogOptions,
|
||||||
method: request.method,
|
|
||||||
origin: url.origin,
|
origin: url.origin,
|
||||||
pathname: url.pathname,
|
pathname: url.pathname,
|
||||||
|
request,
|
||||||
routeCache: this.#routeCache,
|
routeCache: this.#routeCache,
|
||||||
ssr: true,
|
ssr: true,
|
||||||
});
|
});
|
||||||
|
|
|
@ -205,7 +205,7 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G
|
||||||
const fullyRelativePath = relPath[0] === '.' ? relPath : './' + relPath;
|
const fullyRelativePath = relPath[0] === '.' ? relPath : './' + relPath;
|
||||||
return fullyRelativePath;
|
return fullyRelativePath;
|
||||||
},
|
},
|
||||||
request: createRequest(url, new Headers()),
|
request: createRequest({ url, headers: new Headers(), logging }),
|
||||||
route: pageData.route,
|
route: pageData.route,
|
||||||
routeCache,
|
routeCache,
|
||||||
site: astroConfig.buildOptions.site,
|
site: astroConfig.buildOptions.site,
|
||||||
|
|
|
@ -4,8 +4,6 @@ import type { SSROptions } from '../../render/dev';
|
||||||
import { preload } from '../../render/dev/index.js';
|
import { preload } from '../../render/dev/index.js';
|
||||||
import { errorHandler } from '../../render/dev/error.js';
|
import { errorHandler } from '../../render/dev/error.js';
|
||||||
import { call as callEndpoint } from '../index.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) {
|
export async function call(ssrOpts: SSROptions) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,8 +1,22 @@
|
||||||
import type { IncomingHttpHeaders } from 'http';
|
import type { IncomingHttpHeaders } from 'http';
|
||||||
|
import type { LogOptions } from './logger';
|
||||||
|
import { warn } from './logger.js';
|
||||||
|
|
||||||
type HeaderType = Headers | Record<string, any> | IncomingHttpHeaders;
|
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 :
|
let headersObj = headers instanceof Headers ? headers :
|
||||||
new Headers(Object.entries(headers as Record<string, any>));
|
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, {
|
Object.defineProperties(request, {
|
||||||
canonicalURL: {
|
canonicalURL: {
|
||||||
get() {
|
get() {
|
||||||
/* eslint-disable no-console */
|
warn(logging, 'deprecation', `Astro.request.canonicalURL has been moved to Astro.canonicalURL`);
|
||||||
console.warn(`Astro.request.canonicalURL has been moved to Astro.canonicalURL`);
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
params: {
|
params: {
|
||||||
get() {
|
get() {
|
||||||
/* eslint-disable no-console */
|
warn(logging, 'deprecation', `Astro.request.params has been moved to Astro.params`);
|
||||||
console.warn(`Astro.request.params has been moved to Astro.params`);
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import shorthash from 'shorthash';
|
import shorthash from 'shorthash';
|
||||||
import type { AstroComponentMetadata, AstroGlobalPartial, EndpointHandler, Params, SSRElement, SSRLoadedRenderer, SSRResult } from '../../@types/astro';
|
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 { escapeHTML, HTMLString, markHTMLString } from './escape.js';
|
||||||
import { extractDirectives, generateHydrateScript, serializeProps } from './hydration.js';
|
import { extractDirectives, generateHydrateScript, serializeProps } from './hydration.js';
|
||||||
import { serializeListValue } from './util.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.
|
// 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 chosenMethod = request.method?.toLowerCase() ?? 'get';
|
||||||
const handler = mod[chosenMethod];
|
const handler = mod[chosenMethod];
|
||||||
|
|
||||||
|
|
|
@ -129,7 +129,12 @@ async function handleRequest(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Headers are only available when using SSR.
|
// 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 {
|
try {
|
||||||
if (!pathname.startsWith(devRoot)) {
|
if (!pathname.startsWith(devRoot)) {
|
||||||
|
|
|
@ -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 { getAstroStyleId, getAstroPageStyleId } from '../vite-plugin-build-css/index.js';
|
||||||
import { prependDotSlash, removeEndingForwardSlash } from '../core/path.js';
|
import { prependDotSlash, removeEndingForwardSlash } from '../core/path.js';
|
||||||
import { RouteCache } from '../core/render/route-cache.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
|
// This package isn't real ESM, so have to coerce it
|
||||||
const matchSrcset: typeof srcsetParse = (srcsetParse as any).default;
|
const matchSrcset: typeof srcsetParse = (srcsetParse as any).default;
|
||||||
|
@ -87,8 +88,11 @@ export function rollupPluginAstroScanHTML(options: PluginOptions): VitePlugin {
|
||||||
astroConfig,
|
astroConfig,
|
||||||
filePath: new URL(`./${component}`, astroConfig.projectRoot),
|
filePath: new URL(`./${component}`, astroConfig.projectRoot),
|
||||||
logging,
|
logging,
|
||||||
headers: new Headers(),
|
request: createRequest({
|
||||||
method: 'GET',
|
url: new URL(origin + pathname),
|
||||||
|
headers: new Headers(),
|
||||||
|
logging,
|
||||||
|
}),
|
||||||
mode: 'production',
|
mode: 'production',
|
||||||
origin,
|
origin,
|
||||||
pathname,
|
pathname,
|
||||||
|
|
Loading…
Reference in a new issue