2

GitHub - slashd-analytics/run: Run user-provided code in a Web Worker

 1 year ago
source link: https://github.com/slashd-analytics/run
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Slashd Run

Slashd Run is a tiny library that runs user-provided code into a Web Worker. Its main purpose is to allow data transformation through snippet of code, therefore some global capabilities are disabled.

A Web Worker cannot access the DOM therefore it cannot manipulate the webpage that use it.

Ideally, the application allows to execute some code provided by the user, alongside some data to get back a result.

Install

Use your favorite package manager:

npm install @slashd/run

Then, include it in the browser:

<script src="node_modules/dist/slashd-run.min.js"></script>

or with ES6 in a module or within a bundler:

import SlashdRun from '@slashd/run'

How to use

The library requires a one-off init somewhere in your code, i.e.:

import SlashdRun from '@slashd/run'
SlashdRun.setup()

The exe method returns a promise, so you can use it with await:

const myCode = `return Math.random() * param`

const res = await SlashdRun.exe(myCode, {param:20})

// res is i.e. 12.345657676

Configuration

You can specify to load external libraries within the worker by adding them in the setup as an array of external paths:

SlashdRun.setup(['https://unpkg.com/lodash', 'https://www.example.com/mylibrary.js'])

With the above setup, it's possible to use lodash in the provided code:

const myCode = `_.difference(arr1, arr2);`

const res = await SlashdRun.exe(myCode, {arr1:[2, 1], arr2:[2, 3]})

// => [1]

Contribute:

Install dependencies:

npm i

Start the watcher

npm start 

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK