Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for Astro.clientAddress #3973

Merged
merged 6 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/olive-dryers-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'astro': minor
'@astrojs/cloudflare': minor
'@astrojs/deno': minor
'@astrojs/netlify': minor
'@astrojs/vercel': minor
'@astrojs/node': minor
---

Adds support for Astro.clientAddress

The new `Astro.clientAddress` property allows you to get the IP address of the requested user.

```astro
<div>Your address { Astro.clientAddress }</div>
```

This property is only available when building for SSR, and only if the adapter you are using supports providing the IP address. If you attempt to access the property in a SSG app it will throw an error.
4 changes: 4 additions & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export interface AstroGlobal extends AstroGlobalPartial {
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astrocanonicalurl)
*/
canonicalURL: URL;
/** The address (usually IP address) of the user. Used with SSR only.
*
*/
clientAddress: string;
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
/** Parameters passed to a dynamic page generated using [getStaticPaths](https://docs.astro.build/en/reference/api-reference/#getstaticpaths)
*
* Example usage:
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ export class App {
}

const response = await render({
adapterName: manifest.adapterName,
matthewp marked this conversation as resolved.
Show resolved Hide resolved
links,
logging: this.#logging,
markdown: manifest.markdown,
mod,
mode: 'production',
origin: url.origin,
pathname: url.pathname,
scripts,
Expand Down
8 changes: 7 additions & 1 deletion packages/astro/src/core/app/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import { IncomingMessage } from 'http';
import { deserializeManifest } from './common.js';
import { App } from './index.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');

function createRequestFromNodeRequest(req: IncomingMessage): Request {
let url = `http://${req.headers.host}${req.url}`;
const entries = Object.entries(req.headers as Record<string, any>);
let rawHeaders = req.headers as Record<string, any>;
const entries = Object.entries(rawHeaders);
let request = new Request(url, {
method: req.method || 'GET',
headers: new Headers(entries),
});
if(req.socket.remoteAddress) {
Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress);
}
return request;
}

Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type SerializedRouteInfo = Omit<RouteInfo, 'routeData'> & {
};

export interface SSRManifest {
adapterName: string;
routes: RouteInfo[];
site?: string;
base?: string;
Expand Down
29 changes: 18 additions & 11 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,24 @@ class AstroBuilder {
colors.dim(`Completed in ${getTimeStat(this.timer.init, performance.now())}.`)
);

await staticBuild({
allPages,
astroConfig: this.config,
logging: this.logging,
manifest: this.manifest,
origin: this.origin,
pageNames,
routeCache: this.routeCache,
viteConfig,
buildConfig,
});
try {
await staticBuild({
allPages,
astroConfig: this.config,
logging: this.logging,
manifest: this.manifest,
origin: this.origin,
pageNames,
routeCache: this.routeCache,
viteConfig,
buildConfig,
});
} catch(err: unknown) {
// If the build doesn't complete, still shutdown the Vite server so the process doesn't hang.
await viteServer.close();
throw err;
}


// Write any additionally generated assets to disk.
this.timer.assetsStart = performance.now();
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
...(viteConfig.plugins || []),
// SSR needs to be last
isBuildingToSSR(opts.astroConfig) &&
vitePluginSSR(opts, internals, opts.astroConfig._ctx.adapter!),
vitePluginSSR(internals, opts.astroConfig._ctx.adapter!),
vitePluginAnalyzer(opts.astroConfig, internals),
],
publicDir: ssr ? false : viteConfig.publicDir,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const manifestReplace = '@@ASTRO_MANIFEST_REPLACE@@';
const replaceExp = new RegExp(`['"](${manifestReplace})['"]`, 'g');

export function vitePluginSSR(
buildOpts: StaticBuildOptions,
internals: BuildInternals,
adapter: AstroAdapter
): VitePlugin {
Expand Down Expand Up @@ -153,6 +152,7 @@ function buildManifest(
'data:text/javascript;charset=utf-8,//[no before-hydration script]';

const ssrManifest: SerializedSSRManifest = {
adapterName: opts.astroConfig._ctx.adapter!.name,
routes,
site: astroConfig.site,
base: astroConfig.base,
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Params,
Props,
RouteData,
RuntimeMode,
SSRElement,
SSRLoadedRenderer,
} from '../../@types/astro';
Expand Down Expand Up @@ -66,11 +67,13 @@ export async function getParamsAndProps(
}

export interface RenderOptions {
adapterName: string | undefined;
logging: LogOptions;
links: Set<SSRElement>;
styles?: Set<SSRElement>;
markdown: MarkdownRenderingOptions;
mod: ComponentInstance;
mode: RuntimeMode;
origin: string;
pathname: string;
scripts: Set<SSRElement>;
Expand All @@ -92,6 +95,7 @@ export async function render(opts: RenderOptions): Promise<Response> {
origin,
markdown,
mod,
mode,
pathname,
scripts,
renderers,
Expand Down Expand Up @@ -130,6 +134,7 @@ export async function render(opts: RenderOptions): Promise<Response> {
styles,
logging,
markdown,
mode,
origin,
params,
props: pageProps,
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/render/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ export async function render(
});

let response = await coreRender({
adapterName: astroConfig.adapter?.name,
links,
styles,
logging,
markdown: astroConfig.markdown,
mod,
mode,
origin,
pathname,
scripts,
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/src/core/render/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
Page,
Params,
Props,
RuntimeMode,
SSRElement,
SSRLoadedRenderer,
SSRResult,
Expand All @@ -15,6 +16,8 @@ import { LogOptions, warn } from '../logger/core.js';
import { isScriptRequest } from './script.js';
import { createCanonicalURL, isCSSRequest } from './util.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');

function onlyAvailableInSSR(name: string) {
return function _onlyAvailableInSSR() {
// TODO add more guidance when we have docs and adapters.
Expand All @@ -28,6 +31,7 @@ export interface CreateResultArgs {
logging: LogOptions;
origin: string;
markdown: MarkdownRenderingOptions;
mode: RuntimeMode;
params: Params;
pathname: string;
props: Props;
Expand Down Expand Up @@ -151,6 +155,13 @@ export function createResult(args: CreateResultArgs): SSRResult {
const Astro = {
__proto__: astroGlobal,
canonicalURL,
get clientAddress() {
if(!(clientAddressSymbol in request)) {
throw new Error(`Astro.clientAddress is not available in your environment. Ensure that you are using an SSR adapter that supports this feature.`)
}

return Reflect.get(request, clientAddressSymbol);
},
params,
props,
request,
Expand Down
6 changes: 6 additions & 0 deletions packages/astro/src/core/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ type RequestBody = ArrayBuffer | Blob | ReadableStream | URLSearchParams | FormD

export interface CreateRequestOptions {
url: URL | string;
clientAddress: string | undefined;
headers: HeaderType;
method?: string;
body?: RequestBody | undefined;
logging: LogOptions;
ssr: boolean;
}

const clientAddressSymbol = Symbol.for('astro.clientAddress');

export function createRequest({
url,
headers,
clientAddress,
method = 'GET',
body = undefined,
logging,
Expand Down Expand Up @@ -67,6 +71,8 @@ export function createRequest({
return _headers;
},
});
} else if(clientAddress) {
Reflect.set(request, clientAddressSymbol, clientAddress);
}

return request;
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ async function handleRequest(
body,
logging,
ssr: buildingToSSR,
clientAddress: buildingToSSR ? req.socket.remoteAddress : undefined,
});

try {
Expand Down
107 changes: 107 additions & 0 deletions packages/astro/test/client-address.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';
import { nodeLogDestination } from '../dist/core/logger/node.js';
import * as cheerio from 'cheerio';

describe('Astro.clientAddress', () => {
describe('SSR', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/client-address/',
experimental: {
ssr: true,
},
adapter: testAdapter(),
});
});

describe('Production', () => {
before(async () => {
await fixture.build();
});

it('Can get the address', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
const html = await response.text();
const $ = cheerio.load(html);
expect($('#address').text()).to.equal('0.0.0.0');
});
});

describe('Development', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;

before(async () => {
devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});

it('Gets the address', async () => {
let res = await fixture.fetch('/');
expect(res.status).to.equal(200);
let html = await res.text();
let $ = cheerio.load(html);
let address = $('#address');

// Just checking that something is here. Not specifying address as it
// might differ per machine.
expect(address.length).to.be.greaterThan(0);
});
});
});

describe('SSG', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/client-address/',
});
});

describe('Build', () => {
it('throws during generation', async () => {
try {
await fixture.build();
expect(false).to.equal(true, 'Build should not have completed');
} catch(err) {
expect(err.message).to.match(/Astro\.clientAddress/, 'Error message mentions Astro.clientAddress');
}
})
});

describe('Development', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;

before(async () => {
// We expect an error, so silence the output
const logging = {
dest: nodeLogDestination,
level: 'silent',
};
devServer = await fixture.startDevServer({ logging });
});

after(async () => {
await devServer.stop();
});

it('is not accessible', async () => {
let res = await fixture.fetch('/');
expect(res.status).to.equal(500);
});
})
});
});
12 changes: 12 additions & 0 deletions packages/astro/test/fixtures/client-address/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
const address = Astro.clientAddress;
---
<html>
<head>
<title>Astro.clientAddress</title>
</head>
<body>
<h1>Astro.clientAddress</h1>
<div id="address">{ address }</div>
</body>
</html>
Loading