Deno Land

Deno is a simple, modern, and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust based on the Tokio platform (which provides the asynchronous runtime needed by JavaScript), still running Google’s V8 engine Deno is created by the same person Ryan Dahl as node.js was, Deno is there to fix some of the node.js flaws. Deno is basically just an anagram for node.js

Deno is a replacement for node.js but it is not mandatory that it should be replaced, Instead, Deno could be used at the place of node.js because it might be able to the same thing in better way.

Deno is in its early stage. It was initially released on 13th May 2018. & Preview Release date was 12th May 2020 and recently stable release 1.1.1 was on 19 June 2020.

Bu default node.js is having access to all the resources available i.e. it can access everything, it can read, write into the file system, make a request, can access environment variable and so on, having these access by default is maybe an added advantage to the developer but it is also a security risk if one is not careful with the code. Instead in Deno to enable or disable access to different security features command line is used.

deno –allow-read=/etc myscript.ts

Deno also comes up with tools to add colour to terminal text, work with an external data structure like binary, CSV, YAML and others, generate UUID’s and even write WebSockets.

How to Install Deno

Shell (Mac, Linux):
curl -fsSL https://deno.land/x/install/install.sh | sh

 

PowerShell (Windows):
iwr https://deno.land/x/install/install.ps1 -useb | iex

 

Homebrew (Mac):
brew install deno

After a successful installation of Deno on the terminal on entering deno command you will get a console. Where for example you can add numbers together

Difference Between Node and Deno

1-   Deno not only supports javaScript but also supports TypeScript with the need of manual compilation instead the compiler is built into the deno.

2-   We can also use typescript in deno file

let message: string;

message = “This is a message”

console.log(message);

3-   Deno file have an extension of .ts

4-   Deno don’t import something from HTTP module also you don’t need to npm install anything then import from the node modules folder instead of in deno it will import from a web server / URL.

import { serve } from “https://deno.land/std@0.59.0/http/server.ts”;

const s = serve({ port: 8000 });

console.log(“http://localhost:8000/”);

for await (const req of s) {

  req.respond({ body: “Hello World\n” });

}

This import serves from this URL so no need to install. After this, a service is created by calling serve and passing an object to configure this server. Here, for example, the port is being passed.

Deno has modern JS features like async iterable, or promises. It is a for loop that allows an infinite array of incoming data & events.

5-   Another feature of Deno is that it can use top-level await now. Deno can use it with an async iterable and works on plain Valina JS promises. This is another cool feature you don’t need to wrap anything where you want to use await with a needless async function which is there to enable await.

6-   When Deno is run it compiles and download the dependency whatever is stored in the URL which we are importing and caches it locally for future. That npm install replacement without all the disadvantages of npm install. Example a Huge node modules folder.

7-   In Node.js any script can spin up your web server or work with your file system. A third-party package is written in node.js and what it does under the hood is a mystery. It could do anything on the system. So, for the security, we are relying on the maintainers of the project to make sure nothing harmful enters the package or there is nothing harmful code is present. Whereas with Deno it is controlled which permission to be given to script and when to execute them & by default there are no permissions at all.

Example: to allow network access to Deno script

Deno run –allow-net deno.ts

Now it only can access network and not able to write or read to our development file system.

Limitations

Deno is an alternative to node.js but its very immature it very new, it may have bugs at this point. Deno team promises to maintain a stable API in Deno. Means with version 1.0 the general API should not change every few days on the other there are a bunch of unstable features simply features that are not fully finished or finalised yet. Deno is not compatible with Node (NPM) packages. It does not have the same ecosystem as it is there for node.js.

Leave a Reply