sandbox.web
v0.0.2
Published
Sandbox for Web based on Proxy & Proto
Maintainers
Readme
sandbox.web
Independent sandbox implementation, provides features configuration, It adopts Proxy/Proto dual mode, which is automatically selected according to the client environment in case of no special configuration
Installation
npm i sandbox.webUsage
example
import {runScript} from 'sandbox.web'
const code = `window.OO = 1`
const host = {}
runScript(code, host, {scope: 'xxxx'})
// host.window.OO => 1API
runScript(code, host?: object, options?) : anyUsed to execute CommonJS codecode :stringhost ?:objectTo provide a global object is the host, the inside of the code window/global/globalThis/self reference will be redirected to the objectoptions?:objectscope?:stringThis field is required In localStorage/sessionStorage/cookie, data will be written to this keyconfigs?:Array<IFeatureConfigure>config featureswrap?: (code: string) => stringwrap original codefeatureCreators?: Array<IFeatureCreator>external featuresmode?: 'inline' | 'anonymous'sandbox mode, 默认值anonymousinline: create the execution mode usingdocument.createElement('script')anonymous: usenew Function()to create the execution mode
example configs
import {runScript} from 'sandbox.web'
const code = `
setTimeout(() => {
//.....
}, 3000)
document.cookie = 1111
`
const host = {}
const configs = [
['document.cookie', false],
['setTimeout', false],
]
const result = runScript(code, host, {configs})
// In this demo, the setTimeout/cookie call will perform window native
// setTimeout/cookie, the sandbox will give up processing the corresponding featureexample wrap code
import {runScript} from 'sandbox.web'
function wrap(code){
return `
var module = {exports : {}}
var exports = module.exports
${code}
return module.exports
`
}
const coode = `
module.exports = [1,2,3]
`
const result = runScript(code, {}, {wrap})
// result === [1,2,3]
//In this DEMO, the code can use code similar to the CommonJS specificationdispose(host) : voidclear the sandbox corresponding to the current hostCalling this method will clear side effects in host, and dispose method of all features will be called internally to perform the cleaning operation
getFeaturesByHost(host) : Array<Feature>Obtain all feature instances stored in the sandbox corresponding to hostsetDisposer(host, fn) : voidSet additional cleanup methods, fn will be called together with dispose callsetDisposeErrorHandler(host, fn) : voidSet up a catch error method to catch the Dispose methodsetupAdapter(adapter: 'proxy' | 'proto' | 'auto') : voidForce the running mode to be specified- default : 'auto'
supported features
window.addEventListenerdocument.addEventListenerdocument.createElementdocument.cookiedocument.writeeval: [disabled by default]localStoragerequestAnimationFramesessionStorage: [disabled by default]setIntervalsetTimeout
Custom Feature
example custom features
import {runScript, IFeatureCreator} from 'sandbox.web'
const _feature: IFeatureCreator<any> = (options) => {
return {
key: 'X',
install: () => {
options.$window.pow = function (n: number) {
return n * n
}
},
dispose: () => {
options.$window.pow = undefined
},
}
}
const code = ` return window.pow(33) `
const result = runScript(code, {}, {featureCreators: [_feature]})
// result === 33 * 33
// In this demo, the `window.pow` call will execute the function defined in `_feature`IFeatureCreator : (options: IOptions) => IFeature
typing
type IFeatureCreator<W = Window, D = Document> = (options: {
$window: $PatchedHost<W>
$document: D
scope: string
}) => IFeature
interface IFeature {
key: string
install: (config?: Record<string, any> | boolean | string | null) => any
dispose: () => void
defaultOptions?: boolean | Record<string, any>
}
type IFeatureConfigureCommon = [string, Record<string, any> | boolean | string | null]
type IFeatureConfigure = string | [string] | IFeatureConfigureCommon