diff --git a/packages/near-membrane-shared/src/Function.ts b/packages/near-membrane-shared/src/Function.ts index 1326555c..e18ab8c8 100644 --- a/packages/near-membrane-shared/src/Function.ts +++ b/packages/near-membrane-shared/src/Function.ts @@ -26,6 +26,18 @@ export function noop() { // No operation performed. } +export function isProxyMaskedFunction(value: any): boolean { + // To extract the flag value of a blue near-membrane proxy we must perform + // a two step handshake. First, we trigger the "has" trap for the + // `LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL` property which must report + // `false`. Second, we trigger the "get" trap to return the flag value. + return ( + typeof value === 'function' && + !(LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL in value) && + (value as any)[LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL] === true + ); +} + export function proxyMaskFunction( func: Function, maskFunc: T, diff --git a/packages/near-membrane-shared/src/NearMembrane.ts b/packages/near-membrane-shared/src/NearMembrane.ts index bb8029d7..6cd24f1d 100644 --- a/packages/near-membrane-shared/src/NearMembrane.ts +++ b/packages/near-membrane-shared/src/NearMembrane.ts @@ -1,5 +1,4 @@ import { - LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL, LOCKER_NEAR_MEMBRANE_SERIALIZED_VALUE_SYMBOL, LOCKER_NEAR_MEMBRANE_SYMBOL, } from './constants'; @@ -32,15 +31,3 @@ export function isNearMembraneProxy(value: any): boolean { } return false; } - -export function isNearMembraneProxyMaskedFunction(value: any): boolean { - // To extract the flag value of a blue near-membrane proxy we must perform - // a two step handshake. First, we trigger the "has" trap for the - // `LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL` property which must report - // `false`. Second, we trigger the "get" trap to return the flag value. - return ( - typeof value === 'function' && - !(LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL in value) && - (value as any)[LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL] === true - ); -} diff --git a/packages/near-membrane-shared/src/__tests__/Function.spec.js b/packages/near-membrane-shared/src/__tests__/Function.spec.js index 40735af8..5577171c 100644 --- a/packages/near-membrane-shared/src/__tests__/Function.spec.js +++ b/packages/near-membrane-shared/src/__tests__/Function.spec.js @@ -1,11 +1,25 @@ import { ERR_ILLEGAL_PROPERTY_ACCESS, + isProxyMaskedFunction, LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL, noop, proxyMaskFunction, } from '../../dist/index.mjs.js'; describe('Function', () => { + it('isProxyMaskedFunction', () => { + expect(isProxyMaskedFunction({})).toBe(false); + expect(isProxyMaskedFunction(null)).toBe(false); + expect(isProxyMaskedFunction(undefined)).toBe(false); + const func = () => 'func'; + func[LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL] = true; + expect(isProxyMaskedFunction(func)).toBe(false); + const mask = () => 'mask'; + expect(isProxyMaskedFunction(mask)).toBe(false); + const masked = proxyMaskFunction(func, mask); + expect(isProxyMaskedFunction(masked)).toBe(true); + }); + it('noop', () => { expect(noop()).toBe(undefined); expect(noop(null)).toBe(undefined); diff --git a/packages/near-membrane-shared/src/__tests__/NearMembrane.spec.js b/packages/near-membrane-shared/src/__tests__/NearMembrane.spec.js index aa823b7b..3574a5c4 100644 --- a/packages/near-membrane-shared/src/__tests__/NearMembrane.spec.js +++ b/packages/near-membrane-shared/src/__tests__/NearMembrane.spec.js @@ -1,11 +1,8 @@ import { getNearMembraneProxySerializedValue, isNearMembraneProxy, - isNearMembraneProxyMaskedFunction, - LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL, LOCKER_NEAR_MEMBRANE_SERIALIZED_VALUE_SYMBOL, LOCKER_NEAR_MEMBRANE_SYMBOL, - proxyMaskFunction, } from '../../dist/index.mjs.js'; describe('NearMembrane', () => { @@ -30,17 +27,4 @@ describe('NearMembrane', () => { }) ).toBe(false); }); - - it('isNearMembraneProxyMaskedFunction', () => { - expect(isNearMembraneProxyMaskedFunction({})).toBe(false); - expect(isNearMembraneProxyMaskedFunction(null)).toBe(false); - expect(isNearMembraneProxyMaskedFunction(undefined)).toBe(false); - const func = () => 'func'; - func[LOCKER_NEAR_MEMBRANE_PROXY_MASKED_SYMBOL] = true; - expect(isNearMembraneProxyMaskedFunction(func)).toBe(false); - const mask = () => 'mask'; - expect(isNearMembraneProxyMaskedFunction(mask)).toBe(false); - const masked = proxyMaskFunction(func, mask); - expect(isNearMembraneProxyMaskedFunction(masked)).toBe(true); - }); }); diff --git a/test/membrane/date.spec.js b/test/membrane/date.spec.js index 49935373..75c7a0a1 100644 --- a/test/membrane/date.spec.js +++ b/test/membrane/date.spec.js @@ -1,4 +1,4 @@ -import { isNearMembraneProxyMaskedFunction } from '@locker/near-membrane-shared'; +import { isProxyMaskedFunction } from '@locker/near-membrane-shared'; import createVirtualEnvironment from '@locker/near-membrane-dom'; describe('Date', () => { @@ -11,7 +11,7 @@ describe('Date', () => { env.evaluate(` expect(Date.prototype.toJSON.toString()).toContain('[native code]'); `); - expect(isNearMembraneProxyMaskedFunction(Date.prototype.toJSON)).toBe(false); + expect(isProxyMaskedFunction(Date.prototype.toJSON)).toBe(false); }); it('Date.prototype.toJSON does not support proxy masked symbol handshake', () => { diff --git a/test/membrane/json.spec.js b/test/membrane/json.spec.js index a3144d7c..a5548724 100644 --- a/test/membrane/json.spec.js +++ b/test/membrane/json.spec.js @@ -1,4 +1,4 @@ -import { isNearMembraneProxyMaskedFunction } from '@locker/near-membrane-shared'; +import { isProxyMaskedFunction } from '@locker/near-membrane-shared'; import createVirtualEnvironment from '@locker/near-membrane-dom'; describe('JSON', () => { @@ -11,7 +11,7 @@ describe('JSON', () => { env.evaluate(` expect(JSON.stringify.toString()).toContain('[native code]'); `); - expect(isNearMembraneProxyMaskedFunction(JSON.stringify)).toBe(false); + expect(isProxyMaskedFunction(JSON.stringify)).toBe(false); }); it('JSON.stringify does not support proxy masked symbol handshake', () => {