npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sandbox.web

v0.0.2

Published

Sandbox for Web based on Proxy & Proto

Downloads

6

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.web

Usage

example

import {runScript} from 'sandbox.web'

const code = `window.OO = 1`
const host = {}
runScript(code, host, {scope: 'xxxx'})

// host.window.OO => 1

API

  • runScript(code, host?: object, options?) : any Used to execute CommonJS code
    • code :string
    • host ?:object To provide a global object is the host, the inside of the code window/global/globalThis/self reference will be redirected to the object
    • options?:object
      • scope?:string This field is required In localStorage/sessionStorage/cookie, data will be written to this key
      • configs?:Array<IFeatureConfigure> config features
      • wrap?: (code: string) => string wrap original code
      • featureCreators?: Array<IFeatureCreator> external features
      • mode?: 'inline' | 'anonymous' sandbox mode, 默认值 anonymous
        • inline : create the execution mode using document.createElement('script')
        • anonymous : use new 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 feature

example 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 specification
  • dispose(host) : void clear the sandbox corresponding to the current host

    Calling 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 host

  • setDisposer(host, fn) : void Set additional cleanup methods, fn will be called together with dispose call

  • setDisposeErrorHandler(host, fn) : void Set up a catch error method to catch the Dispose method

  • setupAdapter(adapter: 'proxy' | 'proto' | 'auto') : void Force the running mode to be specified

    • default : 'auto'

supported features

  • window.addEventListener
  • document.addEventListener
  • document.createElement
  • document.cookie
  • document.write
  • eval : [disabled by default]
  • localStorage
  • requestAnimationFrame
  • sessionStorage : [disabled by default]
  • setInterval
  • setTimeout

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