-
Notifications
You must be signed in to change notification settings - Fork 3
Add support for node middleware in cloudflare #38
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
Open
conico974
wants to merge
3
commits into
conico/share-build
Choose a base branch
from
conico/node-middleware
base: conico/share-build
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
76999eb
feat(middleware): implement middleware bundle support and enhance bui…
conico974 b13d62a
feat(middleware): enhance middleware support with runtime patches and…
conico974 7093c5d
feat(opentelemetry): add OpenTelemetry global utils patching and tests
conico974 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...s-cloudflare/e2e/app-router/middleware.ts → examples-cloudflare/e2e/app-router/proxy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/cloudflare/src/cli/build/patches/plugins/opentelemetry.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { expect, test } from "vitest"; | ||
|
|
||
| import { patchOpenTelemetryGlobalUtilsCode } from "./opentelemetry.js"; | ||
|
|
||
| const code = `"use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.unregisterGlobal = exports.getGlobal = exports.registerGlobal = void 0; | ||
| const platform_1 = require("../platform"); | ||
| const version_1 = require("../version"); | ||
| const semver_1 = require("./semver"); | ||
| const major = version_1.VERSION.split('.')[0]; | ||
| const GLOBAL_OPENTELEMETRY_API_KEY = Symbol.for(\`opentelemetry.js.api.\${major}\`); | ||
| const _global = platform_1._globalThis; | ||
| function registerGlobal(type, instance, diag, allowOverride = false) { | ||
| const api = (_global[GLOBAL_OPENTELEMETRY_API_KEY] = { | ||
| version: version_1.VERSION, | ||
| }); | ||
| return semver_1.isCompatible(api.version); | ||
| } | ||
| `; | ||
|
|
||
| test("patches OpenTelemetry to use the Cloudflare global", () => { | ||
| const patched = patchOpenTelemetryGlobalUtilsCode(code); | ||
|
|
||
| expect(patched).toContain('// const platform_1 = require("../platform");'); | ||
| expect(patched).toContain("const _global = globalThis;"); | ||
| expect(patched).toContain('const version_1 = require("../version");'); | ||
| expect(patched).toContain("return semver_1.isCompatible(api.version);"); | ||
| expect(patched).not.toContain("platform_1._globalThis"); | ||
| }); | ||
|
|
||
| test("captures the platform and global variable names", () => { | ||
| const patched = patchOpenTelemetryGlobalUtilsCode(` | ||
| const nodePlatform = require("../platform"); | ||
| const globalStore = nodePlatform._globalThis; | ||
| const unrelatedStore = otherPlatform._globalThis; | ||
| `); | ||
|
|
||
| expect(patched).toContain('// const nodePlatform = require("../platform");'); | ||
| expect(patched).toContain("const globalStore = globalThis;"); | ||
| expect(patched).toContain("const unrelatedStore = otherPlatform._globalThis;"); | ||
| }); |
41 changes: 41 additions & 0 deletions
41
packages/cloudflare/src/cli/build/patches/plugins/opentelemetry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { patchCode } from "@opennextjs/core/build/patch/astCodePatcher.js"; | ||
| import type { CodePatcher } from "@opennextjs/core/build/patch/codePatcher.js"; | ||
| import { getCrossPlatformPathRegex } from "@opennextjs/core/utils/regex.js"; | ||
|
|
||
| export const commentPlatformImportRule = ` | ||
| rule: | ||
| pattern: const $PLATFORM = require("../platform"); | ||
| fix: // const $PLATFORM = require("../platform"); | ||
| `; | ||
|
|
||
| export const replaceGlobalRule = ` | ||
| rule: | ||
| all: | ||
| - pattern: const $GLOBAL = $PLATFORM._globalThis; | ||
| - inside: | ||
| kind: program | ||
| stopBy: end | ||
| has: | ||
| pattern: const $PLATFORM = require("../platform"); | ||
| fix: const $GLOBAL = globalThis; | ||
| `; | ||
|
|
||
| export function patchOpenTelemetryGlobalUtilsCode(code: string): string { | ||
| let patchedCode = patchCode(code, replaceGlobalRule); | ||
| patchedCode = patchCode(patchedCode, commentPlatformImportRule); | ||
| return patchedCode; | ||
| } | ||
|
|
||
| export const patchOpenTelemetryGlobalUtils: CodePatcher = { | ||
| name: "patch-opentelemetry-global-utils", | ||
| patches: [ | ||
| { | ||
| pathFilter: getCrossPlatformPathRegex( | ||
| String.raw`@opentelemetry/api/build/src/internal/global-utils\.js$`, | ||
| { escape: false } | ||
| ), | ||
| contentFilter: /require\(["']\.\.\/platform["']\)/, | ||
| patchCode: async ({ code }) => patchOpenTelemetryGlobalUtilsCode(code), | ||
| }, | ||
| ], | ||
| }; |
33 changes: 33 additions & 0 deletions
33
packages/cloudflare/src/cli/build/patches/plugins/turbopack.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| import { patchTurbopackRuntime } from "./turbopack.js"; | ||
|
|
||
| describe("turbopack runtime", () => { | ||
| const runtimeCode = ` | ||
| function loadRuntimeChunkPath(chunkPath) { | ||
| const resolved = chunkPath; | ||
| return require(resolved); | ||
| } | ||
| `; | ||
|
|
||
| test("uses the middleware traced chunks", async () => { | ||
| const patch = patchTurbopackRuntime.patches[0]!; | ||
| const result = await patch.patchCode({ | ||
| code: runtimeCode, | ||
| tracedFiles: ["/app/.open-next/middleware/app/.next/server/chunks/ssr/chunk.js"], | ||
| } as never); | ||
|
|
||
| expect(result).toContain('case "server/chunks/ssr/chunk.js"'); | ||
| expect(result).toContain( | ||
| 'return require("/app/.open-next/middleware/app/.next/server/chunks/ssr/chunk.js")' | ||
| ); | ||
| }); | ||
|
|
||
| test("supports middleware with no chunks", async () => { | ||
| const patch = patchTurbopackRuntime.patches[0]!; | ||
| const result = await patch.patchCode({ code: runtimeCode, tracedFiles: [] } as never); | ||
|
|
||
| expect(result).toContain("function requireChunk(chunkPath)"); | ||
| expect(result).toContain("default:"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Middleware handler plugin added twice with conflicting target paths in Cloudflare builds
The external-middleware plugin is registered twice with different handler paths (
openNextExternalMiddlewarePlugin(...)atpackages/core/src/build/middleware/buildNodeMiddleware.ts:137-139and again viaadditionalPluginsatpackages/cloudflare/src/cli/adapter.ts:95-100), so the first plugin wins and the adapter's intended handler is silently ignored.Impact: On Cloudflare, the middleware bundle may use the wrong handler module, potentially breaking middleware execution at runtime.
Duplicate esbuild plugin with conflicting resolve targets
The core function
buildExternalNodeMiddlewareunconditionally addsopenNextExternalMiddlewarePluginpointing tocore/nodeMiddlewareHandler.jsatpackages/core/src/build/middleware/buildNodeMiddleware.ts:137-139. Then it spreads...additionalPluginsat line 140, which for the Cloudflare adapter includes anotheropenNextExternalMiddlewarePluginpointing tocore/edgeFunctionHandler.js(seepackages/cloudflare/src/cli/adapter.ts:95-100).Since
buildExternalNodeMiddlewareis only called whenconfig.middleware?.externalis true, the conditional guard in the Cloudflare adapter'smiddlewareBundle.additionalPlugins(config.middleware?.external ? [...] : []) always evaluates to true in this code path, meaning the plugin is always duplicated.In esbuild, plugins are processed in registration order, so the core's plugin (registered first) resolves the import to
nodeMiddlewareHandler.js, and the adapter's plugin (registered second) never gets a chance to redirect it toedgeFunctionHandler.js.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.