DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

Deno — Workers, Libraries, and Modules

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

Deno is a new server-side runtime environment for running JavaScript and TypeScript apps.

In this article, we’ll take a look at how to get started with developing apps for Deno.

Workers

Deno supports the web worker API.

For example, we can write:

index.ts

const worker = new Worker(new URL("worker.js", import.meta.url).href, {
  type: "module",
  deno: true,
});
worker.postMessage({ filename: "./log.txt" });
Enter fullscreen mode Exit fullscreen mode

worker.js

self.onmessage = async (e) => {
  const { filename } = e.data;
  const text = await Deno.readTextFile(filename);
  console.log(text);
  self.close();
};
Enter fullscreen mode Exit fullscreen mode

log.txt

hello
Enter fullscreen mode Exit fullscreen mode

Then when we run:

deno run  --unstable --allow-read index.ts
Enter fullscreen mode Exit fullscreen mode

We create the worker with the Worker constructor.

We pass in the URL to the worker.

And the object specifies the type as 'module' and deno set to true so that we can run the worker.

worker.js has the worker.

self is the worker instance. The onmessage method accepts message passed in by calling postMessage .

And then we call Deno.readTextFile method to read a file in the worker.

We then should see hello logged in the console.

Reloading Modules

Deno caches modules by default without fetching or recompiling it.

If we need to reload them, we run:

deno cache --reload index.ts
Enter fullscreen mode Exit fullscreen mode

to reload everything used by index.ts .

We can also reload a specific module used by index.ts by running:

deno cache --reload=https://deno.land/std@0.75.0 index.ts
Enter fullscreen mode Exit fullscreen mode

If we want to reload multiple modules used by index.ts , we run:

deno cache --reload=https://deno.land/std@0.75.0/fs/copy.ts,https://deno.land/std@0.75.0/fmt/colors.ts index.ts
Enter fullscreen mode Exit fullscreen mode

Lock Files

Deno stores and check the subresource integrity for modules using a small JSON file.

The -lock=lock.json flag enables and specifies lock file checking.

To update the lock file, we run:

--lock=lock.json --lock-write
Enter fullscreen mode Exit fullscreen mode

We can use the  — -lock=lock.json option to check the data when we run our app.

Standard Library

We should run pinned versions of imports to avoid unintended changes.

For example, instead of writing:

import { copy } from "https://deno.land/std/fs/copy.ts";
Enter fullscreen mode Exit fullscreen mode

We should include the version number with the import by writing:

import { copy } from "https://deno.land/std@0.75.0/fs/copy.ts";
Enter fullscreen mode Exit fullscreen mode

We may use libraries that have unstable Deno APIs.

The copy module we have above is unstable, so to use it, we run:

deno run --allow-read --allow-write --unstable main.ts
Enter fullscreen mode Exit fullscreen mode

to enable unstable APIs.

Import and Export Modules

We can use standard ES modules to with Deno apps.

For example, we can write:

math.ts

export const add = (a: number, b: number) => a + b;
export const multiply = (a: number, b: number) => a * b;
Enter fullscreen mode Exit fullscreen mode

index.ts

import { add, multiply } from "./math.ts";

console.log(add(1, 2))
console.log(multiply(1, 2))
Enter fullscreen mode Exit fullscreen mode

We have math.ts that exports the add and multiply functions.

Then we use them in index.ts .

Remote Import

We can also import modules directly from a remote URL.

For example, we can write:

import {
  add,
  multiply,
} from "https://x.nest.land/ramda@0.27.0/source/index.js";

console.log(add(1, 2))
console.log(multiply(1, 2))
Enter fullscreen mode Exit fullscreen mode

We import the add and multiple functions from https://x.nest.land/ramda@0.27.0/source/index.js .

Then we can use them the same way.

Conclusion

We can use web workers directly with Deno.

Also, we can import modules from various sources.

Top comments (0)