An Introduction To Deno.JS
Posted By : Raghav Sehgal | 22-Jul-2020
Deno provides a runtime environment for JavaScript and TypeScript. It is based on the V8 JavaScript engine and the Rust programming language and is becoming increasingly popular in SaaS application development services.
Deno could also be a secure TypeScript runtime built on V8, the Google runtime engine for JavaScript.
It was built with:
- Deno’s core was written in Rust.
- The event loop is written in Rust(Tokio).
- It supports both JavaScript and TypeScript.
- Google’s V8 engine.
Security (permissions) - It executes the code during a sandbox, which suggests that runtime has no access to:
- The filing system
- The network
- Execution of other scripts
- The environment variables
- Let’s take a glance at how the permission system works.
const encoder = new TextEncoder();
const data = encoder.encode('Hello world\n');
await Deno.writeFile('hello2.txt', data);
})();
When we run it by executing: deno run write-hello.ts
We are prompted with the following:
It requests to write access to "/Users/user/folder/hello.txt". Grant? [a/y/n/d (a = allow-always, y = allow-once, n = deny- once, d = deny-always)]
We are prompted twice since each call from the sandbox must invite permission. In fact, if we chose the allow always option, we might only get asked once. If we elect the deny option, the PermissionDenied error is going to be thrown, and therefore the process is going to be terminated since we don’t have any error-handling logic.
If we execute the script with the subsequent command: deno run --allow-write write-hello.ts
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
What If An Internet Site Goes Down?
Since the cache is stored on our local disk, the creators of Deno recommend checking it in git-version system and keeping it within the repository. This way, even when the web site goes down, all the developers retain access to the downloaded version.
Deno stores the cache within the directory specified under the $DENO_DIR environmental variable. If we don’t set the variable ourselves, it'll be set to the system’s default cache directory. we will set the $DENO_DIR somewhere in our local repository and check it into the version system.
Do I Have To Import It By The URL All The Time?
Constantly typing URLs would be very tedious. Deno gives us two options to avoid this problem.
The first option is to re-export the imported module from an area file, like so:
export { test, assertEquals } from "https://deno.land/std/testing/mod.ts";
Let’s say the file above is named local-test-utils.ts. Now, if we would like to again make use of either test or assertEquals functions, we will just reference it like this:
import { test, assertEquals } from './local-test-utils.ts';
So it doesn’t matter if it’s loaded from a URL or not.
The second option is to make an imports map, which we specify during a JSON file:
{
"imports": {
"http/": "https://deno.land/std/http/"
}
}
And then import it as such:
import { serve } from "http/server.ts";
In order for it to figure , we've to inform Deno about the imports map by including the --importmap flag:
deno run --importmap=import_map.json hello_server.ts
Will It Replace Node.js Anytime Soon?
Deno is not any different, just because immediately, it’s just the culmination of around 2 years of labor on a thought. It hasn’t been reviewed and put into weird and unintended use cases to ascertain how it deals with those border situations. Maybe during a year, we’ll start hearing from companies sharing their experiences with it, how they’ve solved the newly found shortcomings, and eventually, the community behind it'll adapt it to some extent where it's useful. Will it replace Node then? Who knows! We’ll need to wait and see.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Raghav Sehgal
Raghav Sehgal is a Front-end Developer having Good exposure of HTML, CSS3, Bootstrap, JavaScript, Angular 6. He likes to listen music, improve coding skills, learn new technologies.