Skip to content

Commit 83df4f5

Browse files
committed
refactor: extract proxy creation into utility
1 parent 5bceecc commit 83df4f5

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { AsyncLocalStorage } from "node:async_hooks";
2+
3+
/**
4+
* Creates a proxy for to use for an instance of AsyncLocalStorage.
5+
*
6+
* @param als AsyncLocalStorage instance.
7+
*/
8+
export function createALSProxy<T>(als: AsyncLocalStorage<T>) {
9+
return new Proxy(
10+
{},
11+
{
12+
ownKeys: () => Reflect.ownKeys(als.getStore()!),
13+
getOwnPropertyDescriptor: (_, ...args) => Reflect.getOwnPropertyDescriptor(als.getStore()!, ...args),
14+
get: (_, property) => Reflect.get(als.getStore()!, property),
15+
set: (_, property, value) => Reflect.set(als.getStore()!, property, value),
16+
}
17+
);
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./create-als-proxy";

packages/cloudflare/src/cli/templates/worker.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,15 @@ import { MockedResponse } from "next/dist/server/lib/mock-request";
77
import type { NextConfig } from "next";
88
import type { NodeRequestHandler } from "next/dist/server/next-server";
99
import Stream from "node:stream";
10+
import { createALSProxy } from "./utils";
1011

1112
const NON_BODY_RESPONSES = new Set([101, 204, 205, 304]);
1213

1314
const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
1415

1516
// Note: this symbol needs to be kept in sync with the one defined in `src/api/get-cloudflare-context.ts`
1617
// eslint-disable-next-line @typescript-eslint/no-explicit-any
17-
(globalThis as any)[Symbol.for("__cloudflare-context__")] = new Proxy(
18-
{},
19-
{
20-
ownKeys: () => Reflect.ownKeys(cloudflareContextALS.getStore()!),
21-
getOwnPropertyDescriptor: (_, ...args) =>
22-
Reflect.getOwnPropertyDescriptor(cloudflareContextALS.getStore()!, ...args),
23-
get: (_, property) => Reflect.get(cloudflareContextALS.getStore()!, property),
24-
set: (_, property, value) => Reflect.set(cloudflareContextALS.getStore()!, property, value),
25-
}
26-
);
18+
(globalThis as any)[Symbol.for("__cloudflare-context__")] = createALSProxy(cloudflareContextALS);
2719

2820
// Injected at build time
2921
const nextConfig: NextConfig = JSON.parse(process.env.__NEXT_PRIVATE_STANDALONE_CONFIG ?? "{}");

0 commit comments

Comments
 (0)