0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -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
): AstroGlobal;
resolve: (s: string) => Promise<string>;
response: AstroGlobal["response"];
response: AstroGlobal['response'];
renderers: SSRLoadedRenderer[];
/**
* 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`.
*/
readonly site = manifest.site ? new URL(manifest.site) : undefined,
readonly site = manifest.site ? new URL(manifest.site) : undefined
) {
this.internalMiddleware = [
createI18nMiddleware(i18n, manifest.base, manifest.trailingSlash, manifest.buildFormat),

View file

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

View file

@ -14,7 +14,6 @@ import {
} from '../i18n/utils.js';
import { renderEndpoint } from '../runtime/server/endpoint.js';
import { renderPage } from '../runtime/server/index.js';
import { renderRedirect } from './redirects/render.js';
import {
ASTRO_VERSION,
REROUTE_DIRECTIVE_HEADER,
@ -23,11 +22,12 @@ import {
clientLocalsSymbol,
responseSentSymbol,
} 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 { callMiddleware } from './middleware/callMiddleware.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 {
private constructor(
@ -140,7 +140,7 @@ export class RenderContext {
return {
cookies,
get clientAddress() {
return renderContext.clientAddress()
return renderContext.clientAddress();
},
get currentLocale() {
return renderContext.computeCurrentLocale();
@ -177,13 +177,7 @@ export class RenderContext {
async createResult(mod: ComponentInstance) {
const { cookies, pathname, pipeline, routeData, status } = this;
const {
clientDirectives,
compressHTML,
manifest,
renderers,
resolve
} = pipeline;
const { clientDirectives, compressHTML, manifest, renderers, resolve } = pipeline;
const { links, scripts, styles } = await pipeline.headElements(routeData);
const componentMetadata =
(await pipeline.componentMetadata(routeData)) ?? manifest.componentMetadata;
@ -193,13 +187,13 @@ export class RenderContext {
status,
statusText: 'OK',
get headers() {
return headers
return headers;
},
// Disallow `Astro.response.headers = new Headers`
set headers(_) {
throw new AstroError(AstroErrorData.AstroResponseHeadersReassigned);
}
} satisfies AstroGlobal["response"];
},
} satisfies AstroGlobal['response'];
// 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
@ -210,7 +204,8 @@ export class RenderContext {
compressHTML,
cookies,
/** 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,
partial,
pathname,
@ -250,7 +245,7 @@ export class RenderContext {
});
}
return new Response(null, { status, headers: { Location: path } });
}
};
const slots = new Slots(result, slotValues, pipeline.logger) as unknown as AstroGlobal['slots'];
// `Astro.self` is added by the compiler
@ -258,7 +253,7 @@ export class RenderContext {
...astroGlobalPartial,
cookies,
get clientAddress() {
return renderContext.clientAddress()
return renderContext.clientAddress();
},
get currentLocale() {
return renderContext.computeCurrentLocale();
@ -279,8 +274,8 @@ export class RenderContext {
site: pipeline.site,
url,
};
return astroGlobalCombined as AstroGlobal
return astroGlobalCombined as AstroGlobal;
}
clientAddress() {
@ -313,11 +308,11 @@ export class RenderContext {
const { defaultLocale, locales, strategy } = i18n;
const fallbackTo = (
strategy === 'pathname-prefix-other-locales' ||
strategy === 'domains-prefix-other-locales'
) ? defaultLocale : undefined
const fallbackTo =
strategy === 'pathname-prefix-other-locales' || strategy === 'domains-prefix-other-locales'
? defaultLocale
: undefined;
// TODO: look into why computeCurrentLocale() needs routeData.route to pass ctx.currentLocale tests,
// and url.pathname to pass Astro.currentLocale tests.
// A single call with `routeData.pathname ?? routeData.route` as the pathname still fails.

View file

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

View file

@ -1,8 +1,8 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
describe('Astro Global', () => {
let fixture;
@ -92,15 +92,15 @@ describe('Astro Global', () => {
root: './fixtures/astro-global/',
site: 'https://mysite.dev/subsite/',
base: '/new',
output: "server",
adapter: testAdapter()
output: 'server',
adapter: testAdapter(),
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
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 $ = cheerio.load(html);
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';
describe('API routes in SSR', () => {
const config = {
root: './fixtures/ssr-api-route/',
output: 'server',
@ -17,43 +16,43 @@ describe('API routes in SSR', () => {
describe('Build', () => {
/** @type {import('./test-utils.js').App} */
let app;
before(async () => {
const fixture = await loadFixture(config);
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
before(async () => {
const fixture = await loadFixture(config);
await fixture.build();
app = await fixture.loadTestAdapterApp();
});
it('Basic pages work', async () => {
const request = new Request('http://example.com/');
const response = await app.render(request);
const html = await response.text();
assert.notEqual(html, '');
});
it('Basic pages work', async () => {
const request = new Request('http://example.com/');
const response = await app.render(request);
const html = await response.text();
assert.notEqual(html, '');
});
it('Can load the API route too', async () => {
const request = new Request('http://example.com/food.json');
const response = await app.render(request);
assert.equal(response.status, 200);
const body = await response.json();
assert.equal(body.length, 3);
});
it('Can load the API route too', async () => {
const request = new Request('http://example.com/food.json');
const response = await app.render(request);
assert.equal(response.status, 200);
const body = await response.json();
assert.equal(body.length, 3);
});
it('Has valid api context', async () => {
const request = new Request('http://example.com/context/any');
const response = await app.render(request);
assert.equal(response.status, 200);
const data = await response.json();
assert.equal(data.cookiesExist, true);
assert.equal(data.requestExist, true);
assert.equal(data.redirectExist, true);
assert.equal(data.propsExist, true);
assert.deepEqual(data.params, { param: 'any' });
assert.match(data.generator, /^Astro v/);
assert.equal(data.url, 'http://example.com/context/any');
assert.equal(data.clientAddress, '0.0.0.0');
assert.equal(data.site, "https://mysite.dev/subsite/");
it('Has valid api context', async () => {
const request = new Request('http://example.com/context/any');
const response = await app.render(request);
assert.equal(response.status, 200);
const data = await response.json();
assert.equal(data.cookiesExist, true);
assert.equal(data.requestExist, true);
assert.equal(data.redirectExist, true);
assert.equal(data.propsExist, true);
assert.deepEqual(data.params, { param: 'any' });
assert.match(data.generator, /^Astro v/);
assert.equal(data.url, 'http://example.com/context/any');
assert.equal(data.clientAddress, '0.0.0.0');
assert.equal(data.site, 'https://mysite.dev/subsite/');
});
});
})
describe('Dev', () => {
/** @type {import('./test-utils.js').DevServer} */
@ -138,7 +137,7 @@ describe('API routes in SSR', () => {
assert.match(data.generator, /^Astro v/);
assert.equal(data.url, 'http://[::1]:4321/blog/context/any');
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
);
pipeline.headElements = () => ({ scripts: new Set(), styles: new Set(), links: new Set() });
pipeline.componentMetadata = () => new Map;
pipeline.componentMetadata = () => new Map();
return pipeline;
}