Skip to content

Commit 335fac5

Browse files
committed
refactor: extract proxy creation into utility
1 parent a91ac71 commit 335fac5

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
@@ -1,6 +1,7 @@
11
import { AsyncLocalStorage } from "node:async_hooks";
22
import type { IncomingMessage } from "node:http";
33
import Stream from "node:stream";
4+
import { createALSProxy } from "./utils";
45

56
import type { ExportedHandler, Fetcher } from "@cloudflare/workers-types";
67
import type { NextConfig } from "next";
@@ -16,16 +17,7 @@ const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
1617

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

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

0 commit comments

Comments
 (0)