fix: should only merge plain objects#9675
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Ignored Deployment
|
|
I'll try this out today or Monday. Really excited to see if it works for us - this is by far our biggest pain point with nextauth. |
|
@ThangHuuVu @balazsorban44 Can you guys approve to get workflows running? I've only run some of the tests locally for network reasons, so I'd like to see the results of the CI run. |
|
Hi, this is likely not going to be what you intended. The new core doesn't use We will revisit this at a later point. You can follow #8491 until then. |
|
@balazsorban44 But version And PR #8491 only indicates the absence of But even if I wanted to fix this on the main branch, I understand that maybe your team wants to focus on the big new prerelease. I'll make my patch file available here for others to look at, and for now, using the |
|
Below is the patched Commandspnpm patch next-auth
# After the modification
pnpm patch-commit <path>merge.js"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.merge = merge;
function isPlainObject(value) {
if (typeof value !== 'object' || value === null) {
return false;
}
const prototype = Object.getPrototypeOf(value);
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
}
function merge(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isPlainObject(target) && isPlainObject(source)) {
for (const key in source) {
if (isPlainObject(source[key])) {
if (!target[key]) Object.assign(target, {
[key]: {}
});
merge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
}
return merge(target, ...sources);
} |
|
Hi All, I get the error 'this.createSocket is not a function' Any idea? I've tried both client.js and merge.js edit and also both the same time |
🤔 No ideas. I used |
☕️ Reasoning
Refs: #2509, #3944.
I got the info from #3944 (reply in thread), which points to the
mergefunction as the reason for the loss of some constructed object types, for example, theAgenttype (new HttpsProxyAgent()) will be wrongly overwritten asobject.This stack overflow answer doesn't consider more boundary cases, we should let it only merge pure objects and let constructed objects or the native objects overwrite directly, thus preserving its original type.
So in this PR, I use a tool library
is-plain-objto replace the existingisObjectfunction to resolve this problem. I manually tested with:in my Next.js project, and seems it works.
🧢 Checklist
🎫 Affected issues
📌 Resources