Skip to content

fix: make patchAsyncStorage work with non-mutable node:module modules #730

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-mirrors-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix: make `patchAsyncStorage` work with non-mutable `node:module` modules
8 changes: 6 additions & 2 deletions packages/open-next/src/core/patchAsyncStorage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const mod = require("node:module");
let mod = require("node:module");

const resolveFilename = mod._resolveFilename;

export function patchAsyncStorage() {
mod._resolveFilename = ((
const _resolveFilename = ((
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct path on workers.

_resolveFilename is not used on workers (it throws) as we do not use dynamic require - requires/imports are resolved at build time.

Copy link
Contributor

@vicb vicb Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use a build time patch instead, replacing the Next version with the patch.

That should be pretty easy to do via the onResolve hook of an ESBuild plugin

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok I see, yes that's a good call thanks 🙂👍

I'm happy to close the PR if we don't see the point in this then (however it is a bit too bad that aws does something that then we need to un-do, I wonder if that could be solved somehow at some point 😕)

originalResolveFilename: typeof resolveFilename,
request: string,
parent: any,
Expand All @@ -29,4 +29,8 @@ export function patchAsyncStorage() {

// We use `bind` here to avoid referencing outside variables to create potential memory leaks.
}).bind(null, resolveFilename);
mod = {
...mod,
_resolveFilename,
};
}