Run code with a temporary globalThis.
npm i @dandre3000/sandbox
import { createRestrictedGlobalProxy , createSandboxFunction } from '@dandre3000/sandbox'
const sandbox = createRestrictedGlobalProxy ( )
sandbox . foo = 'bar'
const fn = createSandboxFunction ( ( ) => console . log ( foo , window , process , userAgent , navigator ) , sandbox )
fn ( ) // bar undefined undefined undefined undefined
type RestrictedGlobalProxy = Pick<'Infinity' | 'NaN' | 'undefined' | 'isFinite' | 'isNaN' | 'parseFloat' | 'parseInt' | 'decodeURI' | 'decodeURIComponent' | 'encodeURI' | 'encodeURIComponent' | 'Object' | 'Function' | 'Boolean' | 'Symbol' | 'Error' | 'AggregateError' | 'RangeError' | 'ReferenceError' | 'SyntaxError' | 'TypeError' | 'URIError' | 'Number' | 'BigInt' | 'Math' | 'String' | 'RegExp' | 'Array' | 'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray' | 'Int16Array' | 'Uint16Array' | 'Int32Array' | 'Uint32Array' | 'BigInt64Array' | 'BigUint64Array' | 'Float16Array' | 'Float32Array' | 'Float64Array' | 'Map' | 'Set' | 'WeakMap' | 'WeakSet' | 'ArrayBuffer' | 'DataView' | 'JSON' | 'Iterator' | 'Promise' | 'Reflect' | 'Proxy' | 'Intl' | 'console'> & {
global: RestrictedGlobalProxy;
globalThis: RestrictedGlobalProxy;
}
createRestrictedGlobalProxy: () => RestrictedGlobalProxy
createSandboxFunction: createSandboxFunction: (fn: Function, context: any) => Function
MIT