@mbartoldus/stringworker@0.1.1
makes a worker from a given script string
stringWorker
The main export of this library is simply a function which returns a worker, running a script provided as a string.
Use with the same caution one would apply to
eval.
const worker = stringWorker(`console.log('hello from the worker thread')`)
For relative paths to imported modules, use import.meta.resolve.
const worker = stringWorker(`import '${import.meta.resolve('./example.js')}'`)
As with any worker, remember to terminate it when it's done running.
worker.terminate()
bakeScript
In some cases you may wish to dynamically generate a script for use in a worker. For ease of bundling, it may be beneficial to store that generated script as a string, which can then be ran through stringWorker. The function bakeScript assists in that task.
For example, imagine a script which performs console.log("hello world"). It would need to export a string like this:
export default `console.log("hello world")`
The function bakeScript would generate the text for such a file, including any necessary escape characters.
const script = bakeScript('console.log("hello world")') // the script can then be saved to a file await Deno.writeFile("script.js", new TextEncoder().encode(script))
That script's default export can then be imported and ran using stringWorker, and the resulting code can be bundled into one module.
import script from './script.js' stringWorker(script)
Add Package
deno add jsr:@mbartoldus/stringworker
Import symbol
import * as stringworker from "@mbartoldus/stringworker";
Import directly with a jsr specifier
import * as stringworker from "jsr:@mbartoldus/stringworker";
Add Package
pnpm i jsr:@mbartoldus/stringworker
pnpm dlx jsr add @mbartoldus/stringworker
Import symbol
import * as stringworker from "@mbartoldus/stringworker";
Add Package
yarn add jsr:@mbartoldus/stringworker
yarn dlx jsr add @mbartoldus/stringworker
Import symbol
import * as stringworker from "@mbartoldus/stringworker";
Add Package
vlt install jsr:@mbartoldus/stringworker
Import symbol
import * as stringworker from "@mbartoldus/stringworker";
Add Package
npx jsr add @mbartoldus/stringworker
Import symbol
import * as stringworker from "@mbartoldus/stringworker";
Add Package
bunx jsr add @mbartoldus/stringworker
Import symbol
import * as stringworker from "@mbartoldus/stringworker";