Deno — OS Signals, File System Events, and Module Metadata

John Au-Yeung - Jan 22 '21 - - Dev Community

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.

Handle OS Signals

We can handle signals with the Deno.signal method.

For example, we can write:

index.ts

console.log("Press Ctrl-C");
for await (const _ of Deno.signal(Deno.Signal.SIGINT)) {
  console.log("interrupted!");
  Deno.exit();
}
Enter fullscreen mode Exit fullscreen mode

If we press Ctrl+C, we’ll trigger the sigint signal to interrupt the program.

We watch for the signal with the for-await-of loop.

Deno.Signal.SIGINT is the object for the sigint signal.

We call Deno.exit to exit the program.

We run the program by running:

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

Also, we can write it as a promise.

For instance, we can write:

index.ts

console.log("Press Ctrl-C to end the program");
await Deno.signal(Deno.Signal.SIGINT);
console.log("interrupted!");
Deno.exit();
Enter fullscreen mode Exit fullscreen mode

to watch for the sigint signal.

Stop Watching Signals

We can stop watching signals by calling the sig.dispose method.

For example, we can write:

index.ts

const sig = Deno.signal(Deno.Signal.SIGINT);
setTimeout(() => {
  sig.dispose();
  console.log("No longer watching SIGINT signal");
}, 5000);

console.log("Watching SIGINT signals");
for await (const _ of sig) {
  console.log("interrupted");
}
Enter fullscreen mode Exit fullscreen mode

In the setTimeout callback. we call the sig.dispose method to stop watching the sigint signal.

The for-await-of loop exits after 5 seconds when sig.dispose is called.

File System Events

We can watch for file system events with the Deno.watchFs method.

For example, we can write:

index.ts

const watcher = Deno.watchFs(".");
for await (const event of watcher) {
  console.log(event);
}
Enter fullscreen mode Exit fullscreen mode

We watch the folder the script is in with Deno.watchFs .

We get the event from the event object.

Then we something like:

index.ts

{ kind: "create", paths: [ "/home/runner/IntelligentWorthwhileMice/./lock.json" ] }
{ kind: "modify", paths: [ "/home/runner/IntelligentWorthwhileMice/./lock.json" ] }
{ kind: "access", paths: [ "/home/runner/IntelligentWorthwhileMice/./lock.json" ] }
{
  kind: "create",
  paths: [ "/home/runner/IntelligentWorthwhileMice/./.3s18gah600nwj.foo.txt~" ]
}
{
  kind: "access",
  paths: [ "/home/runner/IntelligentWorthwhileMice/./.3s18gah600nwj.foo.txt~" ]
}
{
  kind: "modify",
  paths: [ "/home/runner/IntelligentWorthwhileMice/./.3s18gah600nwj.foo.txt~" ]
}
{ kind: "modify", paths: [ "/home/runner/IntelligentWorthwhileMice/./foo.txt" ] }
{
  kind: "modify",
  paths: [
    "/home/runner/IntelligentWorthwhileMice/./.3s18gah600nwj.foo.txt~",
    "/home/runner/IntelligentWorthwhileMice/./foo.txt"
  ]
}
Enter fullscreen mode Exit fullscreen mode

displayed.

Then we can run it with:

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

to watch for any file system events in the folder.

Module Metadata

We can get module meta with the import.meta property.

For example, we can write:

console.log(import.meta.url);
console.log(Deno.mainModule);
console.log(import.meta.main);
Enter fullscreen mode Exit fullscreen mode

Then we see something like:

file:///home/runner/IntelligentWorthwhileMice/index.ts
file:///home/runner/IntelligentWorthwhileMice/index.ts
true
Enter fullscreen mode Exit fullscreen mode

from the console output when we run:

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

import.meta.url and Deno.mainModule both get the path of the module.

And import.meta.main returns true if it’s the entry point module.

Conclusion

We can handle OS signals, get module metadata, and watch file system events with Deno.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player