Skip to content

Commit 2570c4b

Browse files
feat!: clean up internal symbols usage (#14420)
1 parent 1672c5e commit 2570c4b

File tree

7 files changed

+28
-37
lines changed

7 files changed

+28
-37
lines changed

packages/astro/src/core/middleware/index.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '../../i18n/utils.js';
77
import type { MiddlewareHandler, Params, RewritePayload } from '../../types/public/common.js';
88
import type { APIContext, AstroSharedContextCsp } from '../../types/public/context.js';
9-
import { ASTRO_VERSION, clientLocalsSymbol } from '../constants.js';
9+
import { ASTRO_VERSION } from '../constants.js';
1010
import { AstroCookies } from '../cookies/index.js';
1111
import { AstroError, AstroErrorData } from '../errors/index.js';
1212
import { getClientIpAddress } from '../routing/request.js';
@@ -43,7 +43,7 @@ export type CreateContext = {
4343
/**
4444
* Initial value of the locals
4545
*/
46-
locals: App.Locals;
46+
locals?: App.Locals;
4747
};
4848

4949
/**
@@ -54,7 +54,7 @@ function createContext({
5454
params = {},
5555
userDefinedLocales = [],
5656
defaultLocale = '',
57-
locals,
57+
locals = {},
5858
}: CreateContext): APIContext {
5959
let preferredLocale: string | undefined = undefined;
6060
let preferredLocaleList: string[] | undefined = undefined;
@@ -110,15 +110,10 @@ function createContext({
110110
return clientIpAddress;
111111
},
112112
get locals() {
113-
// TODO: deprecate this usage. This is used only by the edge middleware for now, so its usage should be basically none.
114-
let _locals = locals ?? Reflect.get(request, clientLocalsSymbol);
115-
if (locals === undefined) {
116-
_locals = {};
117-
}
118-
if (typeof _locals !== 'object') {
113+
if (typeof locals !== 'object') {
119114
throw new AstroError(AstroErrorData.LocalsNotAnObject);
120115
}
121-
return _locals;
116+
return locals;
122117
},
123118
set locals(_) {
124119
throw new AstroError(AstroErrorData.LocalsReassigned);

packages/astro/src/core/render-context.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import type { RouteData, SSRResult } from '../types/public/internal.js';
2222
import type { SSRActions } from './app/types.js';
2323
import {
2424
ASTRO_VERSION,
25-
clientAddressSymbol,
2625
REROUTE_DIRECTIVE_HEADER,
2726
REWRITE_DIRECTIVE_HEADER_KEY,
2827
REWRITE_DIRECTIVE_HEADER_VALUE,
@@ -751,7 +750,7 @@ export class RenderContext {
751750
}
752751

753752
getClientAddress() {
754-
const { pipeline, request, routeData, clientAddress } = this;
753+
const { pipeline, routeData, clientAddress } = this;
755754

756755
if (routeData.prerender) {
757756
throw new AstroError({
@@ -764,13 +763,6 @@ export class RenderContext {
764763
return clientAddress;
765764
}
766765

767-
// TODO: Legacy, should not need to get here.
768-
// Some adapters set this symbol so we can't remove support yet.
769-
// Adapters should be updated to provide it via RenderOptions instead.
770-
if (clientAddressSymbol in request) {
771-
return Reflect.get(request, clientAddressSymbol) as string;
772-
}
773-
774766
if (pipeline.adapterName) {
775767
throw new AstroError({
776768
...AstroErrorData.ClientAddressNotAvailable,

packages/astro/test/fixtures/ssr-prerender-chunks/deps/test-adapter/server.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ import { App } from 'astro/app';
2626
}
2727
}
2828

29-
Reflect.set(
30-
request,
31-
Symbol.for('astro.clientAddress'),
32-
request.headers.get('cf-connecting-ip')
33-
);
34-
3529
const locals = {
3630
runtime: {
3731
env: env,
@@ -44,7 +38,11 @@ import { App } from 'astro/app';
4438
},
4539
};
4640

47-
const response = await app.render(request, { routeData, locals });
41+
const response = await app.render(request, {
42+
routeData,
43+
locals,
44+
clientAddress: request.headers.get('cf-connecting-ip'),
45+
});
4846

4947
if (app.setCookieHeaders) {
5048
for (const setCookieHeader of app.setCookieHeaders(response)) {

packages/astro/test/test-adapter.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,17 @@ export default function ({
8181
return new Response(data);
8282
}
8383
84+
return super.render(request, {
85+
routeData,
86+
locals,
87+
addCookieHeader,
88+
prerenderedErrorPageFetch,
8489
${
8590
provideAddress
86-
? `request[Symbol.for('astro.clientAddress')] = clientAddress ?? '0.0.0.0';`
91+
? `clientAddress: clientAddress ?? '0.0.0.0',`
8792
: ''
8893
}
89-
return super.render(request, { routeData, locals, addCookieHeader, prerenderedErrorPageFetch });
94+
});
9095
}
9196
}
9297

packages/integrations/cloudflare/src/utils/handler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ export async function handle(
6464
}
6565
}
6666

67-
Reflect.set(request, Symbol.for('astro.clientAddress'), request.headers.get('cf-connecting-ip'));
68-
6967
const locals: Runtime = {
7068
runtime: {
7169
env: env,
@@ -92,6 +90,7 @@ export async function handle(
9290
prerenderedErrorPageFetch: async (url) => {
9391
return env.ASSETS.fetch(url.replace(/\.html$/, ''));
9492
},
93+
clientAddress: request.headers.get('cf-connecting-ip') ?? undefined,
9594
},
9695
);
9796

packages/integrations/netlify/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,11 @@ export default function netlifyIntegration(
698698
if (existingSessionModule) {
699699
server.moduleGraph.invalidateModule(existingSessionModule);
700700
}
701+
702+
const clientLocalsSymbol = Symbol.for('astro.locals');
703+
701704
server.middlewares.use((req, _res, next) => {
702-
const locals = Symbol.for('astro.locals');
703-
Reflect.set(req, locals, {
704-
...Reflect.get(req, locals),
705+
Reflect.set(req, clientLocalsSymbol, {
705706
netlify: { context: getLocalDevNetlifyContext(req) },
706707
});
707708
next();

packages/integrations/netlify/src/ssr-function.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ export interface Args {
1212
middlewareSecret: string;
1313
}
1414

15-
const clientAddressSymbol = Symbol.for('astro.clientAddress');
16-
1715
export const createExports = (manifest: SSRManifest, { middlewareSecret }: Args) => {
1816
const app = new App(manifest);
1917

@@ -30,7 +28,6 @@ export const createExports = (manifest: SSRManifest, { middlewareSecret }: Args)
3028
});
3129
}
3230

33-
Reflect.set(request, clientAddressSymbol, context.ip);
3431
let locals: Record<string, unknown> = {};
3532

3633
const astroLocalsHeader = request.headers.get('x-astro-locals');
@@ -46,7 +43,11 @@ export const createExports = (manifest: SSRManifest, { middlewareSecret }: Args)
4643

4744
locals.netlify = { context };
4845

49-
const response = await app.render(request, { routeData, locals });
46+
const response = await app.render(request, {
47+
routeData,
48+
locals,
49+
clientAddress: context.ip
50+
});
5051

5152
if (app.setCookieHeaders) {
5253
for (const setCookieHeader of app.setCookieHeaders(response)) {

0 commit comments

Comments
 (0)