Skip to content

Commit 9ecefdf

Browse files
committed
Revert "use ALS for entire process object"
This reverts commit 4be38fa.
1 parent 4628b62 commit 9ecefdf

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

packages/cloudflare/src/cli/templates/utils/create-als-proxy.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import type { AsyncLocalStorage } from "node:async_hooks";
55
*
66
* @param als AsyncLocalStorage instance
77
*/
8-
export function createALSProxy<T extends object>(als: AsyncLocalStorage<T>) {
9-
return new Proxy({} as T, {
10-
ownKeys: () => Reflect.ownKeys(als.getStore()!),
11-
getOwnPropertyDescriptor: (_, ...args) => Reflect.getOwnPropertyDescriptor(als.getStore()!, ...args),
12-
get: (_, property) => Reflect.get(als.getStore()!, property),
13-
set: (_, property, value) => Reflect.set(als.getStore()!, property, value),
14-
});
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+
);
1518
}

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

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ import { createALSProxy } from "./utils";
1313

1414
const NON_BODY_RESPONSES = new Set([101, 204, 205, 304]);
1515

16-
const processALS = new AsyncLocalStorage<typeof process>();
16+
const processEnvALS = new AsyncLocalStorage<Record<string, unknown>>();
1717
const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
1818

1919
// Note: this symbol needs to be kept in sync with the one defined in `src/api/get-cloudflare-context.ts`
2020
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2121
(globalThis as any)[Symbol.for("__cloudflare-context__")] = createALSProxy(cloudflareContextALS);
2222

23-
globalThis.process = createALSProxy(processALS);
23+
globalThis.process = {
24+
...globalThis.process,
25+
// @ts-expect-error - populated when we run inside the ALS context
26+
env: createALSProxy(processEnvALS),
27+
};
2428

2529
// Injected at build time
2630
const nextConfig: NextConfig = JSON.parse(process.env.__NEXT_PRIVATE_STANDALONE_CONFIG ?? "{}");
@@ -29,45 +33,42 @@ let requestHandler: NodeRequestHandler | null = null;
2933

3034
export default {
3135
async fetch(request, env, ctx) {
32-
return processALS.run(
33-
{ ...globalThis.process, env: { ...globalThis.process.env, NODE_ENV: "production", ...env } },
34-
() => {
35-
return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
36-
if (requestHandler == null) {
37-
// Note: "next/dist/server/next-server" is a cjs module so we have to `require` it not to confuse esbuild
38-
// (since esbuild can run in projects with different module resolutions)
39-
// eslint-disable-next-line @typescript-eslint/no-require-imports
40-
const NextNodeServer = require("next/dist/server/next-server")
41-
.default as typeof import("next/dist/server/next-server").default;
42-
43-
requestHandler = new NextNodeServer({
44-
conf: nextConfig,
45-
customServer: false,
46-
dev: false,
47-
dir: "",
48-
minimalMode: false,
49-
}).getRequestHandler();
50-
}
36+
return processEnvALS.run({ NODE_ENV: "production", ...env }, () => {
37+
return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
38+
if (requestHandler == null) {
39+
// Note: "next/dist/server/next-server" is a cjs module so we have to `require` it not to confuse esbuild
40+
// (since esbuild can run in projects with different module resolutions)
41+
// eslint-disable-next-line @typescript-eslint/no-require-imports
42+
const NextNodeServer = require("next/dist/server/next-server")
43+
.default as typeof import("next/dist/server/next-server").default;
44+
45+
requestHandler = new NextNodeServer({
46+
conf: nextConfig,
47+
customServer: false,
48+
dev: false,
49+
dir: "",
50+
minimalMode: false,
51+
}).getRequestHandler();
52+
}
5153

52-
const url = new URL(request.url);
54+
const url = new URL(request.url);
5355

54-
if (url.pathname === "/_next/image") {
55-
const imageUrl =
56-
url.searchParams.get("url") ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg";
57-
if (imageUrl.startsWith("/")) {
58-
return env.ASSETS.fetch(new URL(imageUrl, request.url));
59-
}
60-
return fetch(imageUrl, { cf: { cacheEverything: true } });
56+
if (url.pathname === "/_next/image") {
57+
const imageUrl =
58+
url.searchParams.get("url") ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg";
59+
if (imageUrl.startsWith("/")) {
60+
return env.ASSETS.fetch(new URL(imageUrl, request.url));
6161
}
62+
return fetch(imageUrl, { cf: { cacheEverything: true } });
63+
}
6264

63-
const { req, res, webResponse } = getWrappedStreams(request, ctx);
65+
const { req, res, webResponse } = getWrappedStreams(request, ctx);
6466

65-
ctx.waitUntil(Promise.resolve(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res))));
67+
ctx.waitUntil(Promise.resolve(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res))));
6668

67-
return await webResponse();
68-
});
69-
}
70-
);
69+
return await webResponse();
70+
});
71+
});
7172
},
7273
} as ExportedHandler<{ ASSETS: Fetcher }>;
7374

0 commit comments

Comments
 (0)