-
Notifications
You must be signed in to change notification settings - Fork 150
feat: node middleware #725
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d165559
initial implementation
conico974 078ab38
fix linting
conico974 cde9148
hack for deployed lambda
conico974 2d301ae
use standalone for middleware trace
conico974 d1c38bb
support for external middleware
conico974 77cabae
fix test and linting
conico974 f6864b6
fix top level await in the middleware
conico974 37929ad
fix comment
conico974 619825c
changeset
conico974 ecdc7bb
add function manifest to edge plugins
conico974 22a6f91
fix matcher when function manifest exist but no node middleware
conico974 4ab0151
fix in case it can't find function manifest
conico974 36d97d0
remove comment
conico974 99796a1
review fix
conico974 6e8b400
Apply suggestions from code review
conico974 a863666
review fix
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
This file contains 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,5 @@ | ||
--- | ||
"@opennextjs/aws": minor | ||
--- | ||
|
||
Add support for the node middleware |
This file contains 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 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 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 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,2 @@ | ||
//TODO: Move all other manifest path here as well | ||
export const MIDDLEWARE_TRACE_FILE = "server/middleware.js.nft.json"; |
This file contains 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 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 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 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
138 changes: 138 additions & 0 deletions
138
packages/open-next/src/build/middleware/buildNodeMiddleware.ts
This file contains 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,138 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
import type { | ||
IncludedOriginResolver, | ||
LazyLoadedOverride, | ||
OverrideOptions, | ||
} from "types/open-next.js"; | ||
import type { OriginResolver } from "types/overrides.js"; | ||
import { getCrossPlatformPathRegex } from "utils/regex.js"; | ||
import { openNextExternalMiddlewarePlugin } from "../../plugins/externalMiddleware.js"; | ||
import { openNextReplacementPlugin } from "../../plugins/replacement.js"; | ||
import { openNextResolvePlugin } from "../../plugins/resolve.js"; | ||
import { copyTracedFiles } from "../copyTracedFiles.js"; | ||
import * as buildHelper from "../helper.js"; | ||
import { installDependencies } from "../installDeps.js"; | ||
|
||
type Override = OverrideOptions & { | ||
originResolver?: LazyLoadedOverride<OriginResolver> | IncludedOriginResolver; | ||
}; | ||
|
||
export async function buildExternalNodeMiddleware( | ||
options: buildHelper.BuildOptions, | ||
) { | ||
const { appBuildOutputPath, config, outputDir } = options; | ||
if (!config.middleware?.external) { | ||
throw new Error( | ||
"This function should only be called for external middleware", | ||
); | ||
} | ||
const outputPath = path.join(outputDir, "middleware"); | ||
fs.mkdirSync(outputPath, { recursive: true }); | ||
|
||
// Copy open-next.config.mjs | ||
buildHelper.copyOpenNextConfig( | ||
options.buildDir, | ||
outputPath, | ||
await buildHelper.isEdgeRuntime(config.middleware.override), | ||
); | ||
const overrides = { | ||
...config.middleware.override, | ||
originResolver: config.middleware.originResolver, | ||
}; | ||
const includeCache = config.dangerous?.enableCacheInterception; | ||
const packagePath = buildHelper.getPackagePath(options); | ||
|
||
// TODO: change this so that we don't copy unnecessary files | ||
await copyTracedFiles({ | ||
buildOutputPath: appBuildOutputPath, | ||
packagePath, | ||
outputDir: outputPath, | ||
routes: [], | ||
bundledNextServer: false, | ||
skipServerFiles: true, | ||
}); | ||
|
||
function override<T extends keyof Override>(target: T) { | ||
return typeof overrides?.[target] === "string" | ||
? overrides[target] | ||
: undefined; | ||
} | ||
|
||
// Bundle middleware | ||
await buildHelper.esbuildAsync( | ||
{ | ||
entryPoints: [ | ||
path.join(options.openNextDistDir, "adapters", "middleware.js"), | ||
], | ||
outfile: path.join(outputPath, "handler.mjs"), | ||
external: ["./.next/*"], | ||
platform: "node", | ||
plugins: [ | ||
openNextResolvePlugin({ | ||
overrides: { | ||
wrapper: override("wrapper") ?? "aws-lambda", | ||
converter: override("converter") ?? "aws-cloudfront", | ||
...(includeCache | ||
? { | ||
tagCache: override("tagCache") ?? "dynamodb-lite", | ||
incrementalCache: override("incrementalCache") ?? "s3-lite", | ||
queue: override("queue") ?? "sqs-lite", | ||
} | ||
: {}), | ||
originResolver: override("originResolver") ?? "pattern-env", | ||
proxyExternalRequest: override("proxyExternalRequest") ?? "node", | ||
}, | ||
fnName: "middleware", | ||
}), | ||
openNextReplacementPlugin({ | ||
name: "externalMiddlewareOverrides", | ||
target: getCrossPlatformPathRegex("adapters/middleware.js"), | ||
deletes: includeCache ? [] : ["includeCacheInMiddleware"], | ||
}), | ||
openNextExternalMiddlewarePlugin( | ||
path.join( | ||
options.openNextDistDir, | ||
"core", | ||
"nodeMiddlewareHandler.js", | ||
), | ||
), | ||
], | ||
banner: { | ||
js: [ | ||
`globalThis.monorepoPackagePath = '${packagePath}';`, | ||
"import process from 'node:process';", | ||
"import { Buffer } from 'node:buffer';", | ||
"import { AsyncLocalStorage } from 'node:async_hooks';", | ||
"import { createRequire as topLevelCreateRequire } from 'module';", | ||
"const require = topLevelCreateRequire(import.meta.url);", | ||
"import bannerUrl from 'url';", | ||
"const __dirname = bannerUrl.fileURLToPath(new URL('.', import.meta.url));", | ||
].join(""), | ||
}, | ||
}, | ||
options, | ||
); | ||
|
||
// Do we need to copy or do something with env file here? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I'm not sure. I can't think of anything obvious. |
||
|
||
installDependencies(outputPath, config.middleware?.install); | ||
} | ||
|
||
export async function buildBundledNodeMiddleware( | ||
options: buildHelper.BuildOptions, | ||
) { | ||
await buildHelper.esbuildAsync( | ||
{ | ||
entryPoints: [ | ||
path.join(options.openNextDistDir, "core/nodeMiddlewareHandler.js"), | ||
], | ||
external: ["./.next/*"], | ||
outfile: path.join(options.buildDir, "middleware.mjs"), | ||
bundle: true, | ||
platform: "node", | ||
}, | ||
options, | ||
); | ||
} |
This file contains 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.
comment: this might not play well with Cloudflare but we probably need to rethink how we compile to edge vs node
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.
Yeah i think we should rely on the
wrapper
to tell us how we should compile (i.e. node vs workers).For the moment i think it's fine, node middleware is still experimental