2022-10-18

Exploring DENO's Features

Deno is a single executable runtime for JavaScript and TypeScript that has some interesting features not available on Node.

Install

Deno can be installed on Windows from the Power Shell; if you aren't the administrator, you can set a different installation folder DENO_INSTALL where you own write permissions. Then run:

    PS C:\Users\Seve> $env:DENO_INSTALL = "C:\Users\Seve\deno"
    PS C:\Users\Seve> irm https://deno.land/install.ps1 | iex

The deno executable will be located in DENO_INSTALL\bin folder, this folder can be added to the PATH or the executable can be moved to a different folder included in the PATH environmental variable. Since it's just a single file, it can be moved around the file system with ease.

Remote Import

Import allows you to include and use modules held remotely, I cloned this set of functions on a remote server. Below is shown an example where functions from two remote modules numbers and strings are used in a local program ( deno.exe cache --reload may be required).

    //tshelpers.ts
    import { randomInt } from 'https://tessarinseve.pythonanywhere.com/staticdeno/numbers.ts';
    import { cleanupNumericString} from 'https://tessarinseve.pythonanywhere.com/staticdeno/strings.ts';
    console.log("Random int number:"+randomInt())
    console.log("Numeric from cat123dog: " +cleanupNumericString("cat123dog"))

Deno Script

If your Windows has Git SCM/MSYS2 or similar installed, it's worth considering the following repository.

Download the script deno-script.sh with:

   wget https://raw.githubusercontent.com/jiraguha/deno-script/master/deno-script.sh

this script alone is sufficient to run programs written in JavaScript/TypeScript when the first line includes the proper #!:

   #!C:\\Users\\YourUserName\\bin\\deno-script.sh

Clearly, the path after #! depends on where the previous script is located.

Fetch Data

Deno supports the Streams API which can be useful to send and receive large files.

The example below downloads locally the sqlite database todo.db:

    #!C:\\Users\\seve\\bin\\deno-script.sh -A
    //filename: downloadsql.ts
    import { writableStreamFromWriter } from "https://deno.land/std@0.159.0/streams/mod.ts";

    const fileResponse = await fetch("https://tessarinseve.pythonanywhere.com/staticweb/todo.db");

    if (fileResponse.body) {
      const file = await Deno.open("./todo.db", { write: true, create: true });
      const writableStream = writableStreamFromWriter(file);
      await fileResponse.body.pipeTo(writableStream);
    }

Option -A allows all permissions. This script runs from the command line simply with:

   ./downloadsql.ts