UNPKG

@sebastianwessel/quickjs

Version:

A typescript package to execute JavaScript and TypeScript code in a WebAssembly QuickJS sandbox

96 lines (95 loc) 4.1 kB
"use strict"; /** * 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. * * @author Sebastian Wessel * @copyright Sebastian Wessel * @license MIT * @link https://sebastianwessel.github.io/quickjs/ * @link https://github.com/sebastianwessel/quickjs * * @example * ```typescript * import variant from '@jitl/quickjs-ng-wasmfile-release-sync' * import { type SandboxOptions, loadQuickJs } from '@sebastianwessel/quickjs' * * // General setup like loading and init of the QuickJS wasm * // It is a resource-intensive job and should be done only once if possible * const { runSandboxed } = await loadQuickJs(variant) * * 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' } * ``` * * * @module */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); __exportStar(require("./adapter/fetch.js"), exports); __exportStar(require("./createVirtualFileSystem.js"), exports); __exportStar(require("./getTypescriptSupport.js"), exports); __exportStar(require("./loadAsyncQuickJs.js"), exports); __exportStar(require("./loadQuickJs.js"), exports); __exportStar(require("./sandbox/asyncVersion/getAsyncModuleLoader.js"), exports); __exportStar(require("./sandbox/asyncVersion/modulePathNormalizerAsync.js"), exports); __exportStar(require("./sandbox/expose/expose.js"), exports); __exportStar(require("./sandbox/handleToNative/handleToNative.js"), exports); __exportStar(require("./sandbox/handleToNative/serializer/index.js"), exports); __exportStar(require("./sandbox/syncVersion/getModuleLoader.js"), exports); __exportStar(require("./sandbox/syncVersion/modulePathNormalizer.js"), exports); __exportStar(require("./types/CodeFunctionInput.js"), exports); __exportStar(require("./types/ErrorResponse.js"), exports); __exportStar(require("./types/LoadQuickJsOptions.js"), exports); __exportStar(require("./types/OkResponse.js"), exports); __exportStar(require("./types/OkResponseCheck.js"), exports); __exportStar(require("./types/Prettify.js"), exports); __exportStar(require("./types/RuntimeOptions.js"), exports); __exportStar(require("./types/SandboxEvalCode.js"), exports); __exportStar(require("./types/SandboxFunction.js"), exports); __exportStar(require("./types/SandboxOptions.js"), exports); __exportStar(require("./types/SandboxValidateCode.js"), exports); __exportStar(require("./types/Serializer.js"), exports);