Skip to main content

A TypeScript package for executing JavaScript code in a secure WebAssembly QuickJS sandbox. Perfect for isolating and running untrusted scripts efficiently.

This package works with Node.js, BunIt is unknown whether this package works with Cloudflare Workers, Deno, Browsers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
It is unknown whether this package works with Deno
This package works with Bun
It is unknown whether this package works with Browsers
JSR Score
58%
Published
4 weeks ago (2.1.1)

QuickJS sandbox for JavaScript/Typescript applications

This TypeScript package allows you to safely execute JavaScript code within a WebAssembly sandbox using the QuickJS engine. Perfect for isolating and running untrusted code securely, it leverages the lightweight and fast QuickJS engine compiled to WebAssembly, providing a robust environment for code execution.

Examples

Example 1

import { type SandboxOptions, loadQuickJs } from '@sebastianwessel/quickjs'

// General setup like loading and init of the QuickJS wasm
// It is a ressource intensive job and should be done only once if possible
const { runSandboxed } = await loadQuickJs()

const options: SandboxOptions = {
  allowFetch: true, // inject fetch and allow the code to fetch data
  allowFs: true, // mount a virtual file system and provide node:fs module
  env: {
    MY_ENV_VAR: 'env var value',
  },
}

const code = `
import { join } from 'path'

const fn = async ()=>{
  console.log(join('src','dist')) // logs "src/dist" on host system

  console.log(env.MY_ENV_VAR) // logs "env var value" on host system

  const url = new URL('https://example.com')

  const f = await fetch(url)

  return f.text()
}

export default await fn()
`

const result = await runSandboxed(async ({ evalCode }) => evalCode(code), options)

console.log(result) // { ok: true, data: '<!doctype html>\n<html>\n[....]</html>\n' }

Add Package

deno add jsr:@sebastianwessel/quickjs

Import symbol

import * as quickjs from "@sebastianwessel/quickjs";

---- OR ----

Import directly with a jsr specifier

import * as quickjs from "jsr:@sebastianwessel/quickjs";

Add Package

npx jsr add @sebastianwessel/quickjs

Import symbol

import * as quickjs from "@sebastianwessel/quickjs";

Add Package

yarn dlx jsr add @sebastianwessel/quickjs

Import symbol

import * as quickjs from "@sebastianwessel/quickjs";

Add Package

pnpm dlx jsr add @sebastianwessel/quickjs

Import symbol

import * as quickjs from "@sebastianwessel/quickjs";

Add Package

bunx jsr add @sebastianwessel/quickjs

Import symbol

import * as quickjs from "@sebastianwessel/quickjs";