Skip to main content
Home

makes a worker from a given script string

This package works with Deno, BrowsersIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
This package works with Browsers
JSR Score
100%
Published
a year ago (0.1.1)

JSR

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)

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@mbartoldus/stringworker

Import symbol

import * as stringworker from "@mbartoldus/stringworker";
or

Import directly with a jsr specifier

import * as stringworker from "jsr:@mbartoldus/stringworker";

Add Package

pnpm i jsr:@mbartoldus/stringworker
or (using pnpm 10.8 or older)
pnpm dlx jsr add @mbartoldus/stringworker

Import symbol

import * as stringworker from "@mbartoldus/stringworker";

Add Package

yarn add jsr:@mbartoldus/stringworker
or (using Yarn 4.8 or older)
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";