Workers

Deno supports Web Worker API.

Workers can be used to run code on multiple threads. Each instance of Worker is run on a separate thread, dedicated only to that worker.

Currently Deno supports only module type workers; thus it’s essential to pass the type: "module" option when creating a new worker.

Use of relative module specifiers in the main worker are only supported with --location <href> passed on the CLI. This is not recommended for portability. You can instead use the URL constructor and import.meta.url to easily create a specifier for some nearby script. Dedicated workers, however, have a location and this capability by default.

  1. // Good
  2. new Worker(new URL("./worker.js", import.meta.url).href, { type: "module" });
  3. // Bad
  4. new Worker(new URL("./worker.js", import.meta.url).href);
  5. new Worker(new URL("./worker.js", import.meta.url).href, { type: "classic" });
  6. new Worker("./worker.js", { type: "module" });

As with regular modules, you can use top-level await in worker modules. However, you should be careful to always register the message handler before the first await, since messages can be lost otherwise. This is not a bug in Deno, it’s just an unfortunate interaction of features, and it also happens in all browsers that support module workers.

```ts, ignore import { delay } from “https://deno.land/std@0.136.0/async/mod.ts“;

// First await: waits for a second, then continues running the module. await delay(1000);

// The message handler is only set after that 1s delay, so some of the messages // that reached the worker during that second might have been fired when no // handler was registered. self.onmessage = (evt) => { console.log(evt.data); };

  1. ### Instantiation permissions
  2. Creating a new `Worker` instance is similar to a dynamic import; therefore Deno
  3. requires appropriate permission for this action.
  4. For workers using local modules; `--allow-read` permission is required:
  5. **main.ts**
  6. ```ts
  7. new Worker(new URL("./worker.ts", import.meta.url).href, { type: "module" });

worker.ts

  1. console.log("hello world");
  2. self.close();
  1. $ deno run main.ts
  2. error: Uncaught PermissionDenied: read access to "./worker.ts", run again with the --allow-read flag
  3. $ deno run --allow-read main.ts
  4. hello world

For workers using remote modules; --allow-net permission is required:

main.ts

  1. new Worker("https://example.com/worker.ts", { type: "module" });

worker.ts (at https://example.com/worker.ts)

  1. console.log("hello world");
  2. self.close();
  1. $ deno run main.ts
  2. error: Uncaught PermissionDenied: net access to "https://example.com/worker.ts", run again with the --allow-net flag
  3. $ deno run --allow-net main.ts
  4. hello world

Using Deno in worker

This is an unstable Deno feature. Learn more about unstable features.

By default the Deno namespace is not available in worker scope.

To enable the Deno namespace pass deno.namespace = true option when creating new worker:

main.js

  1. const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
  2. type: "module",
  3. deno: {
  4. namespace: true,
  5. },
  6. });
  7. worker.postMessage({ filename: "./log.txt" });

worker.js

```ts, ignore self.onmessage = async (e) => { const { filename } = e.data; const text = await Deno.readTextFile(filename); console.log(text); self.close(); };

  1. **log.txt**

hello world

  1. ```shell
  2. $ deno run --allow-read --unstable main.js
  3. hello world

Specifying worker permissions

This is an unstable Deno feature. Learn more about unstable features.

The permissions available for the worker are analogous to the CLI permission flags, meaning every permission enabled there can be disabled at the level of the Worker API. You can find a more detailed description of each of the permission options here.

By default a worker will inherit permissions from the thread it was created in, however in order to allow users to limit the access of this worker we provide the deno.permissions option in the worker API.

  • For permissions that support granular access you can pass in a list of the desired resources the worker will have access to, and for those who only have the on/off option you can pass true/false respectively.

    1. const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
    2. type: "module",
    3. deno: {
    4. namespace: true,
    5. permissions: {
    6. net: [
    7. "https://deno.land/",
    8. ],
    9. read: [
    10. new URL("./file_1.txt", import.meta.url),
    11. new URL("./file_2.txt", import.meta.url),
    12. ],
    13. write: false,
    14. },
    15. },
    16. });
  • Granular access permissions receive both absolute and relative routes as arguments, however take into account that relative routes will be resolved relative to the file the worker is instantiated in, not the path the worker file is currently in

    1. const worker = new Worker(
    2. new URL("./worker/worker.js", import.meta.url).href,
    3. {
    4. type: "module",
    5. deno: {
    6. namespace: true,
    7. permissions: {
    8. read: [
    9. "/home/user/Documents/deno/worker/file_1.txt",
    10. "./worker/file_2.txt",
    11. ],
    12. },
    13. },
    14. },
    15. );
  • Both deno.permissions and its children support the option "inherit", which implies it will borrow its parent permissions.

    1. // This worker will inherit its parent permissions
    2. const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
    3. type: "module",
    4. deno: {
    5. namespace: true,
    6. permissions: "inherit",
    7. },
    8. });
    1. // This worker will inherit only the net permissions of its parent
    2. const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
    3. type: "module",
    4. deno: {
    5. namespace: true,
    6. permissions: {
    7. env: false,
    8. hrtime: false,
    9. net: "inherit",
    10. ffi: false,
    11. read: false,
    12. run: false,
    13. write: false,
    14. },
    15. },
    16. });
  • Not specifying the deno.permissions option or one of its children will cause the worker to inherit by default.

    1. // This worker will inherit its parent permissions
    2. const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
    3. type: "module",
    4. });
    1. // This worker will inherit all the permissions of its parent BUT net
    2. const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
    3. type: "module",
    4. deno: {
    5. namespace: true,
    6. permissions: {
    7. net: false,
    8. },
    9. },
    10. });
  • You can disable the permissions of the worker all together by passing "none" to the deno.permissions option.

    1. // This worker will not have any permissions enabled
    2. const worker = new Worker(new URL("./worker.js", import.meta.url).href, {
    3. type: "module",
    4. deno: {
    5. namespace: true,
    6. permissions: "none",
    7. },
    8. });