Skip to content
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

[BUGFIX] do not throw on deepFreezeValue with objects with properties that are JS Proxy instances #2317

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion packages/shared/util/Recoil_deepFreezeValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ function deepFreezeValue(value: mixed) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
const prop = value[key];
// Prevent infinite recurssion for circular references.
if (typeof prop === 'object' && prop != null && !Object.isFrozen(prop)) {
const objectNotFrozen = typeof prop === 'object' && prop != null && !Object.isFrozen(prop);

// Don't try to freeze proxy objects
// 1. Object.freeze() on a proxy proxy with not frozen target will throw
// 2. Freeze of the proxy target can break proxy handler behavior
// 3. So with 1 and 2, better to leave proxy frozen state up to consumer
const isObjectAProxy = prop !== null
&& typeof prop === 'object'
&& Object.getPrototypeOf(prop) !== Object.getPrototypeOf({});

if (objectNotFrozen && !isObjectAProxy) {
deepFreezeValue(prop);
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/shared/util/__tests__/Recoil_deepFreezeValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe('deepFreezeValue', () => {
).not.toThrow();
});

test('don\'t freeze Proxy objects', () => {
const obj = {test: new Proxy({}, {})};
expect(() => deepFreezeValue(obj)).not.toThrow();
expect(() => deepFreezeValue(obj.test)).not.toThrow();
expect(Object.isFrozen(obj)).toBe(true);
expect(Object.isFrozen(obj.test)).toBe(false);
});

test('check no error: object with Window property', () => {
if (typeof window === 'undefined') {
return;
Expand Down