0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-03 22:29:08 -05:00

[ci] format

This commit is contained in:
Arsh 2024-03-07 14:46:00 +00:00 committed by astrobot-houston
parent f33cce8f6c
commit 4eab7f4ac3
8 changed files with 63 additions and 75 deletions

View file

@ -2701,7 +2701,7 @@ export interface SSRResult {
slots: Record<string, any> | null slots: Record<string, any> | null
): AstroGlobal; ): AstroGlobal;
resolve: (s: string) => Promise<string>; resolve: (s: string) => Promise<string>;
response: AstroGlobal["response"]; response: AstroGlobal['response'];
renderers: SSRLoadedRenderer[]; renderers: SSRLoadedRenderer[];
/** /**
* Map of directive name (e.g. `load`) to the directive script code * Map of directive name (e.g. `load`) to the directive script code

View file

@ -45,7 +45,7 @@ export abstract class Pipeline {
/** /**
* Used for `Astro.site`. * Used for `Astro.site`.
*/ */
readonly site = manifest.site ? new URL(manifest.site) : undefined, readonly site = manifest.site ? new URL(manifest.site) : undefined
) { ) {
this.internalMiddleware = [ this.internalMiddleware = [
createI18nMiddleware(i18n, manifest.base, manifest.trailingSlash, manifest.buildFormat), createI18nMiddleware(i18n, manifest.base, manifest.trailingSlash, manifest.buildFormat),

View file

@ -71,10 +71,7 @@ function createContext({
return (preferredLocaleList ??= computePreferredLocaleList(request, userDefinedLocales)); return (preferredLocaleList ??= computePreferredLocaleList(request, userDefinedLocales));
}, },
get currentLocale(): string | undefined { get currentLocale(): string | undefined {
return (currentLocale ??= computeCurrentLocale( return (currentLocale ??= computeCurrentLocale(route, userDefinedLocales));
route,
userDefinedLocales,
));
}, },
url, url,
get clientAddress() { get clientAddress() {

View file

@ -14,7 +14,6 @@ import {
} from '../i18n/utils.js'; } from '../i18n/utils.js';
import { renderEndpoint } from '../runtime/server/endpoint.js'; import { renderEndpoint } from '../runtime/server/endpoint.js';
import { renderPage } from '../runtime/server/index.js'; import { renderPage } from '../runtime/server/index.js';
import { renderRedirect } from './redirects/render.js';
import { import {
ASTRO_VERSION, ASTRO_VERSION,
REROUTE_DIRECTIVE_HEADER, REROUTE_DIRECTIVE_HEADER,
@ -23,11 +22,12 @@ import {
clientLocalsSymbol, clientLocalsSymbol,
responseSentSymbol, responseSentSymbol,
} from './constants.js'; } from './constants.js';
import { attachCookiesToResponse, AstroCookies } from './cookies/index.js'; import { AstroCookies, attachCookiesToResponse } from './cookies/index.js';
import { AstroError, AstroErrorData } from './errors/index.js'; import { AstroError, AstroErrorData } from './errors/index.js';
import { callMiddleware } from './middleware/callMiddleware.js'; import { callMiddleware } from './middleware/callMiddleware.js';
import { sequence } from './middleware/index.js'; import { sequence } from './middleware/index.js';
import { type Pipeline, getParams, getProps, Slots } from './render/index.js'; import { renderRedirect } from './redirects/render.js';
import { type Pipeline, Slots, getParams, getProps } from './render/index.js';
export class RenderContext { export class RenderContext {
private constructor( private constructor(
@ -140,7 +140,7 @@ export class RenderContext {
return { return {
cookies, cookies,
get clientAddress() { get clientAddress() {
return renderContext.clientAddress() return renderContext.clientAddress();
}, },
get currentLocale() { get currentLocale() {
return renderContext.computeCurrentLocale(); return renderContext.computeCurrentLocale();
@ -177,13 +177,7 @@ export class RenderContext {
async createResult(mod: ComponentInstance) { async createResult(mod: ComponentInstance) {
const { cookies, pathname, pipeline, routeData, status } = this; const { cookies, pathname, pipeline, routeData, status } = this;
const { const { clientDirectives, compressHTML, manifest, renderers, resolve } = pipeline;
clientDirectives,
compressHTML,
manifest,
renderers,
resolve
} = pipeline;
const { links, scripts, styles } = await pipeline.headElements(routeData); const { links, scripts, styles } = await pipeline.headElements(routeData);
const componentMetadata = const componentMetadata =
(await pipeline.componentMetadata(routeData)) ?? manifest.componentMetadata; (await pipeline.componentMetadata(routeData)) ?? manifest.componentMetadata;
@ -193,13 +187,13 @@ export class RenderContext {
status, status,
statusText: 'OK', statusText: 'OK',
get headers() { get headers() {
return headers return headers;
}, },
// Disallow `Astro.response.headers = new Headers` // Disallow `Astro.response.headers = new Headers`
set headers(_) { set headers(_) {
throw new AstroError(AstroErrorData.AstroResponseHeadersReassigned); throw new AstroError(AstroErrorData.AstroResponseHeadersReassigned);
} },
} satisfies AstroGlobal["response"]; } satisfies AstroGlobal['response'];
// Create the result object that will be passed into the renderPage function. // Create the result object that will be passed into the renderPage function.
// This object starts here as an empty shell (not yet the result) but then // This object starts here as an empty shell (not yet the result) but then
@ -210,7 +204,8 @@ export class RenderContext {
compressHTML, compressHTML,
cookies, cookies,
/** This function returns the `Astro` faux-global */ /** This function returns the `Astro` faux-global */
createAstro: (astroGlobal, props, slots) => this.createAstro(result, astroGlobal, props, slots), createAstro: (astroGlobal, props, slots) =>
this.createAstro(result, astroGlobal, props, slots),
links, links,
partial, partial,
pathname, pathname,
@ -250,7 +245,7 @@ export class RenderContext {
}); });
} }
return new Response(null, { status, headers: { Location: path } }); return new Response(null, { status, headers: { Location: path } });
} };
const slots = new Slots(result, slotValues, pipeline.logger) as unknown as AstroGlobal['slots']; const slots = new Slots(result, slotValues, pipeline.logger) as unknown as AstroGlobal['slots'];
// `Astro.self` is added by the compiler // `Astro.self` is added by the compiler
@ -258,7 +253,7 @@ export class RenderContext {
...astroGlobalPartial, ...astroGlobalPartial,
cookies, cookies,
get clientAddress() { get clientAddress() {
return renderContext.clientAddress() return renderContext.clientAddress();
}, },
get currentLocale() { get currentLocale() {
return renderContext.computeCurrentLocale(); return renderContext.computeCurrentLocale();
@ -280,7 +275,7 @@ export class RenderContext {
url, url,
}; };
return astroGlobalCombined as AstroGlobal return astroGlobalCombined as AstroGlobal;
} }
clientAddress() { clientAddress() {
@ -313,10 +308,10 @@ export class RenderContext {
const { defaultLocale, locales, strategy } = i18n; const { defaultLocale, locales, strategy } = i18n;
const fallbackTo = ( const fallbackTo =
strategy === 'pathname-prefix-other-locales' || strategy === 'pathname-prefix-other-locales' || strategy === 'domains-prefix-other-locales'
strategy === 'domains-prefix-other-locales' ? defaultLocale
) ? defaultLocale : undefined : undefined;
// TODO: look into why computeCurrentLocale() needs routeData.route to pass ctx.currentLocale tests, // TODO: look into why computeCurrentLocale() needs routeData.route to pass ctx.currentLocale tests,
// and url.pathname to pass Astro.currentLocale tests. // and url.pathname to pass Astro.currentLocale tests.

View file

@ -152,10 +152,7 @@ export function computePreferredLocaleList(request: Request, locales: Locales):
return result; return result;
} }
export function computeCurrentLocale( export function computeCurrentLocale(pathname: string, locales: Locales): undefined | string {
pathname: string,
locales: Locales,
): undefined | string {
for (const segment of pathname.split('/')) { for (const segment of pathname.split('/')) {
for (const locale of locales) { for (const locale of locales) {
if (typeof locale === 'string') { if (typeof locale === 'string') {
@ -177,7 +174,7 @@ export function computeCurrentLocale(
} }
} }
} }
}; }
} }
export type RoutingStrategies = export type RoutingStrategies =

View file

@ -1,8 +1,8 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test'; import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio'; import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js'; import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
describe('Astro Global', () => { describe('Astro Global', () => {
let fixture; let fixture;
@ -92,15 +92,15 @@ describe('Astro Global', () => {
root: './fixtures/astro-global/', root: './fixtures/astro-global/',
site: 'https://mysite.dev/subsite/', site: 'https://mysite.dev/subsite/',
base: '/new', base: '/new',
output: "server", output: 'server',
adapter: testAdapter() adapter: testAdapter(),
}); });
await fixture.build(); await fixture.build();
app = await fixture.loadTestAdapterApp(); app = await fixture.loadTestAdapterApp();
}); });
it('Astro.site', async () => { it('Astro.site', async () => {
const response = await app.render(new Request("https://example.com/")); const response = await app.render(new Request('https://example.com/'));
const html = await response.text(); const html = await response.text();
const $ = cheerio.load(html); const $ = cheerio.load(html);
assert.equal($('#site').attr('href'), 'https://mysite.dev/subsite/'); assert.equal($('#site').attr('href'), 'https://mysite.dev/subsite/');

View file

@ -5,7 +5,6 @@ import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js'; import { loadFixture } from './test-utils.js';
describe('API routes in SSR', () => { describe('API routes in SSR', () => {
const config = { const config = {
root: './fixtures/ssr-api-route/', root: './fixtures/ssr-api-route/',
output: 'server', output: 'server',
@ -51,9 +50,9 @@ describe('API routes in SSR', () => {
assert.match(data.generator, /^Astro v/); assert.match(data.generator, /^Astro v/);
assert.equal(data.url, 'http://example.com/context/any'); assert.equal(data.url, 'http://example.com/context/any');
assert.equal(data.clientAddress, '0.0.0.0'); assert.equal(data.clientAddress, '0.0.0.0');
assert.equal(data.site, "https://mysite.dev/subsite/"); assert.equal(data.site, 'https://mysite.dev/subsite/');
});
}); });
})
describe('Dev', () => { describe('Dev', () => {
/** @type {import('./test-utils.js').DevServer} */ /** @type {import('./test-utils.js').DevServer} */
@ -138,7 +137,7 @@ describe('API routes in SSR', () => {
assert.match(data.generator, /^Astro v/); assert.match(data.generator, /^Astro v/);
assert.equal(data.url, 'http://[::1]:4321/blog/context/any'); assert.equal(data.url, 'http://[::1]:4321/blog/context/any');
assert.equal(data.clientAddress, '::1'); assert.equal(data.clientAddress, '::1');
assert.equal(data.site, "https://mysite.dev/subsite/"); assert.equal(data.site, 'https://mysite.dev/subsite/');
}); });
}); });
}); });

View file

@ -203,7 +203,7 @@ export function createBasicPipeline(options = {}) {
options.site options.site
); );
pipeline.headElements = () => ({ scripts: new Set(), styles: new Set(), links: new Set() }); pipeline.headElements = () => ({ scripts: new Set(), styles: new Set(), links: new Set() });
pipeline.componentMetadata = () => new Map; pipeline.componentMetadata = () => new Map();
return pipeline; return pipeline;
} }