Skip to content

Commit

Permalink
Do not freeze array buffer views
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D49106461

fbshipit-source-id: c5046677b93283601482f4bfaea3723776658129
  • Loading branch information
gordyf authored and facebook-github-bot committed Sep 8, 2023
1 parent c93322b commit 4123383
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/relay-runtime/util/__tests__/deepFreeze-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ describe('deepFreeze()', () => {
it('copes with null values', () => {
expect(deepFreeze({a: null})).toBeFrozen();
});

it('does not throw on array buffers', () => {
const x = new Uint16Array([21, 31]);
expect(() => deepFreeze(x)).not.toThrow();
expect(() => deepFreeze({x})).not.toThrow();
});
});
17 changes: 17 additions & 0 deletions packages/relay-runtime/util/deepFreeze.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* returns the now-frozen original object.
*/
function deepFreeze<T: {...}>(object: T): T {
if (!shouldBeFrozen(object)) {
return object;
}
Object.freeze(object);
Object.getOwnPropertyNames(object).forEach(name => {
const property = object[name];
Expand All @@ -32,4 +35,18 @@ function deepFreeze<T: {...}>(object: T): T {
return object;
}

function shouldBeFrozen(value: mixed): boolean {
// Primitives and functions:
if (value === null || typeof value !== 'object') {
return false;
}

// Views on array buffers cannot be frozen
if (ArrayBuffer.isView(value)) {
return false;
}

return true;
}

module.exports = deepFreeze;

0 comments on commit 4123383

Please sign in to comment.