[ci] format
This commit is contained in:
parent
5a23483efb
commit
4ee997da43
10 changed files with 33 additions and 27 deletions
|
@ -93,7 +93,7 @@ export interface AstroGlobal extends AstroGlobalPartial {
|
||||||
*/
|
*/
|
||||||
canonicalURL: URL;
|
canonicalURL: URL;
|
||||||
/** The address (usually IP address) of the user. Used with SSR only.
|
/** The address (usually IP address) of the user. Used with SSR only.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
clientAddress: string;
|
clientAddress: string;
|
||||||
/** Parameters passed to a dynamic page generated using [getStaticPaths](https://docs.astro.build/en/reference/api-reference/#getstaticpaths)
|
/** Parameters passed to a dynamic page generated using [getStaticPaths](https://docs.astro.build/en/reference/api-reference/#getstaticpaths)
|
||||||
|
|
|
@ -10,8 +10,8 @@ import type { RouteInfo, SSRManifest as Manifest } from './types';
|
||||||
|
|
||||||
import mime from 'mime';
|
import mime from 'mime';
|
||||||
import { call as callEndpoint } from '../endpoint/index.js';
|
import { call as callEndpoint } from '../endpoint/index.js';
|
||||||
import { error } from '../logger/core.js';
|
|
||||||
import { consoleLogDestination } from '../logger/console.js';
|
import { consoleLogDestination } from '../logger/console.js';
|
||||||
|
import { error } from '../logger/core.js';
|
||||||
import { joinPaths, prependForwardSlash } from '../path.js';
|
import { joinPaths, prependForwardSlash } from '../path.js';
|
||||||
import { render } from '../render/core.js';
|
import { render } from '../render/core.js';
|
||||||
import { RouteCache } from '../render/route-cache.js';
|
import { RouteCache } from '../render/route-cache.js';
|
||||||
|
@ -125,13 +125,13 @@ export class App {
|
||||||
request,
|
request,
|
||||||
streaming: this.#streaming,
|
streaming: this.#streaming,
|
||||||
});
|
});
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
error(this.#logging, 'ssr', err);
|
error(this.#logging, 'ssr', err);
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 500,
|
status: 500,
|
||||||
statusText: 'Internal server error'
|
statusText: 'Internal server error',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ function createRequestFromNodeRequest(req: IncomingMessage): Request {
|
||||||
method: req.method || 'GET',
|
method: req.method || 'GET',
|
||||||
headers: new Headers(entries),
|
headers: new Headers(entries),
|
||||||
});
|
});
|
||||||
if(req.socket.remoteAddress) {
|
if (req.socket.remoteAddress) {
|
||||||
Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress);
|
Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress);
|
||||||
}
|
}
|
||||||
return request;
|
return request;
|
||||||
|
|
|
@ -142,13 +142,12 @@ class AstroBuilder {
|
||||||
viteConfig,
|
viteConfig,
|
||||||
buildConfig,
|
buildConfig,
|
||||||
});
|
});
|
||||||
} catch(err: unknown) {
|
} catch (err: unknown) {
|
||||||
// If the build doesn't complete, still shutdown the Vite server so the process doesn't hang.
|
// If the build doesn't complete, still shutdown the Vite server so the process doesn't hang.
|
||||||
await viteServer.close();
|
await viteServer.close();
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Write any additionally generated assets to disk.
|
// Write any additionally generated assets to disk.
|
||||||
this.timer.assetsStart = performance.now();
|
this.timer.assetsStart = performance.now();
|
||||||
Object.keys(assets).map((k) => {
|
Object.keys(assets).map((k) => {
|
||||||
|
|
|
@ -144,8 +144,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
|
||||||
}),
|
}),
|
||||||
...(viteConfig.plugins || []),
|
...(viteConfig.plugins || []),
|
||||||
// SSR needs to be last
|
// SSR needs to be last
|
||||||
isBuildingToSSR(opts.astroConfig) &&
|
isBuildingToSSR(opts.astroConfig) && vitePluginSSR(internals, opts.astroConfig._ctx.adapter!),
|
||||||
vitePluginSSR(internals, opts.astroConfig._ctx.adapter!),
|
|
||||||
vitePluginAnalyzer(opts.astroConfig, internals),
|
vitePluginAnalyzer(opts.astroConfig, internals),
|
||||||
],
|
],
|
||||||
publicDir: ssr ? false : viteConfig.publicDir,
|
publicDir: ssr ? false : viteConfig.publicDir,
|
||||||
|
|
|
@ -19,10 +19,7 @@ const resolvedVirtualModuleId = '\0' + virtualModuleId;
|
||||||
const manifestReplace = '@@ASTRO_MANIFEST_REPLACE@@';
|
const manifestReplace = '@@ASTRO_MANIFEST_REPLACE@@';
|
||||||
const replaceExp = new RegExp(`['"](${manifestReplace})['"]`, 'g');
|
const replaceExp = new RegExp(`['"](${manifestReplace})['"]`, 'g');
|
||||||
|
|
||||||
export function vitePluginSSR(
|
export function vitePluginSSR(internals: BuildInternals, adapter: AstroAdapter): VitePlugin {
|
||||||
internals: BuildInternals,
|
|
||||||
adapter: AstroAdapter
|
|
||||||
): VitePlugin {
|
|
||||||
return {
|
return {
|
||||||
name: '@astrojs/vite-plugin-astro-ssr',
|
name: '@astrojs/vite-plugin-astro-ssr',
|
||||||
enforce: 'post',
|
enforce: 'post',
|
||||||
|
|
|
@ -157,11 +157,15 @@ export function createResult(args: CreateResultArgs): SSRResult {
|
||||||
__proto__: astroGlobal,
|
__proto__: astroGlobal,
|
||||||
canonicalURL,
|
canonicalURL,
|
||||||
get clientAddress() {
|
get clientAddress() {
|
||||||
if(!(clientAddressSymbol in request)) {
|
if (!(clientAddressSymbol in request)) {
|
||||||
if(args.adapterName) {
|
if (args.adapterName) {
|
||||||
throw new Error(`Astro.clientAddress is not available in the ${args.adapterName} adapter. File an issue with the adapter to add support.`);
|
throw new Error(
|
||||||
|
`Astro.clientAddress is not available in the ${args.adapterName} adapter. File an issue with the adapter to add support.`
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Astro.clientAddress is not available in your environment. Ensure that you are using an SSR adapter that supports this feature.`)
|
throw new Error(
|
||||||
|
`Astro.clientAddress is not available in your environment. Ensure that you are using an SSR adapter that supports this feature.`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,8 +71,8 @@ export function createRequest({
|
||||||
return _headers;
|
return _headers;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else if(clientAddress) {
|
} else if (clientAddress) {
|
||||||
Reflect.set(request, clientAddressSymbol, clientAddress);
|
Reflect.set(request, clientAddressSymbol, clientAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
return request;
|
return request;
|
||||||
|
|
|
@ -53,7 +53,7 @@ describe('Astro.clientAddress', () => {
|
||||||
let $ = cheerio.load(html);
|
let $ = cheerio.load(html);
|
||||||
let address = $('#address');
|
let address = $('#address');
|
||||||
|
|
||||||
// Just checking that something is here. Not specifying address as it
|
// Just checking that something is here. Not specifying address as it
|
||||||
// might differ per machine.
|
// might differ per machine.
|
||||||
expect(address.length).to.be.greaterThan(0);
|
expect(address.length).to.be.greaterThan(0);
|
||||||
});
|
});
|
||||||
|
@ -81,7 +81,7 @@ describe('Astro.clientAddress', () => {
|
||||||
const response = await app.render(request);
|
const response = await app.render(request);
|
||||||
expect(response.status).to.equal(500);
|
expect(response.status).to.equal(500);
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
describe('SSG', () => {
|
describe('SSG', () => {
|
||||||
/** @type {import('./test-utils').Fixture} */
|
/** @type {import('./test-utils').Fixture} */
|
||||||
|
@ -98,10 +98,13 @@ describe('Astro.clientAddress', () => {
|
||||||
try {
|
try {
|
||||||
await fixture.build();
|
await fixture.build();
|
||||||
expect(false).to.equal(true, 'Build should not have completed');
|
expect(false).to.equal(true, 'Build should not have completed');
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
expect(err.message).to.match(/Astro\.clientAddress/, 'Error message mentions Astro.clientAddress');
|
expect(err.message).to.match(
|
||||||
|
/Astro\.clientAddress/,
|
||||||
|
'Error message mentions Astro.clientAddress'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Development', () => {
|
describe('Development', () => {
|
||||||
|
@ -125,6 +128,6 @@ describe('Astro.clientAddress', () => {
|
||||||
let res = await fixture.fetch('/');
|
let res = await fixture.fetch('/');
|
||||||
expect(res.status).to.equal(500);
|
expect(res.status).to.equal(500);
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,7 +20,11 @@ export function createExports(manifest: SSRManifest) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (app.match(request)) {
|
if (app.match(request)) {
|
||||||
Reflect.set(request, Symbol.for('astro.clientAddress'), request.headers.get('cf-connecting-ip'));
|
Reflect.set(
|
||||||
|
request,
|
||||||
|
Symbol.for('astro.clientAddress'),
|
||||||
|
request.headers.get('cf-connecting-ip')
|
||||||
|
);
|
||||||
return app.render(request);
|
return app.render(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue