Author: Maciej Cieslar

Translator: Crazy Technology House

Original: https://blog.logrocket.com/wh…

Reproduction is strictly prohibited without permission

Ryan Dahl, founder of Node.js, spent a year and a half studying Deno, a new JavaScript runtime environment that addresses all of Node’s problems.

Don’t get me wrong, Node itself is a great server-side JavaScript runtime environment, mainly because it has a huge ecosystem and JavaScript. However, Dahl admits that he should consider more things: security, modules and dependencies.

It’s not that Dahl thinks the platform will grow in a short time. But as early as 2009, JavaScript was still such a strange niche language, everyone was laughing at it, and it did have many functions.

What is Deno and what are its main characteristics?

Deno is a secure Typescript runtime built on V8, which is Google’s JavaScript runtime engine.

It is constructed by the following technologies:

  • Rust (Deno’s core is written in Rust and Node in C++)
  • Tokio (event loop written in Rust)
  • TypeScript (Deno supports JavaScript and TypeScript out of the box)
  • V8 (JavaScript runtime used by Google in Chrome and Node, etc.)

So let’s take a look at Deno’s capabilities.

Security (permissions)

One of Deno’s most important functions is to focus on security.

In contrast to Node, Deno defaults to executing code in the sandbox, which means that the runtime does not have access to:

  • file system
  • network
  • Execute other scripts
  • environment variable

Let’s see how the privilege system works.

(async () => {
    const encoder = new TextEncoder();
    const data = encoder.encode('Hello world\n');

    await Deno.writeFile('hello.txt', data);
    await Deno.writeFile('hello2.txt', data);
})();

The script creates two nameshello.txtandhello2.txtA text file containing a “Hello world” message. The code is executing in the sandbox, so it cannot access the file system.

Also note that we use Deno namespaces instead of FS modules, as in Node. Deno namespaces provide many basic auxiliary functions. If you use namespaces, you lose browser compatibility, which will be discussed later.

Run it this way:

deno run write-hello.ts

The following will be prompted:

Deno requests 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)]

In fact, we will be prompted twice, because every call from the sandbox must request permission. Of course, if we chooseallow alwaysOptions will only be asked once.

If you choosedenyOptions, which will be thrownPermissionDeniedError, and since there is no error handling logic in our code, the process will be terminated.

If the script is executed with the following commands:

deno run --allow-write write-hello.ts

You will find that both files have been created without prompting.

Except for file system--allow-writeBesides the signs, there are also--allow-net--allow-envand--allow-runFlags are used to enable network requests, access the environment, and run subprocesses.

Modular

Like browsers, Deno loads modules through URLs. Many people are confused at first when they see import statements with URLs on the server side, but it does make sense — as long as you can bear it:

import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

You might ask, what’s the big deal of importing packages through URLs? The answer is simple: Deno packages can be distributed without centralized registration through URLs, such asnpmThere are many other explanations to be found here.

By importing code through a URL, we enable package creators to host their code where they think it is appropriate.No more package. JSON and node_modules are required.

When the application is started, Deno downloads all imported modules and caches them. Once they are cached, Deno will not download them again until they pass--reloadSpecial requirements for signs.

Here are some important questions:

What if the website fails?

Because it is not centralized registration, the website hosting the module may be deleted for a variety of reasons. It depends on its state during development — or worse, it is risky in production.

As mentioned earlier, Deno caches downloaded modules. Because the cache is stored on local disks, Deno’s creators recommend that it be checked in a version control system (git) and stored in a repository. In this way, even if the website fails, all developers can access the downloaded version.

Deno stores caches in$DENO_DIRThe directory specified under the environment variable. If you don’t set variables yourself, it will be set to the default cache directory of the system. You can set it somewhere in the local repository$DENO_DIRAnd check it into version control system.

Do I have to import through the URL all the time?

It will be very cumbersome to enter the web address continuously. Fortunately, Deno offers us two options to avoid doing so.

The first option is to re-import the imported module from the local file, as follows:

export { test, assertEquals } from "https://deno.land/std/testing/mod.ts";

Suppose the file above is namedlocal-test-utils.ts。 Now, if you want to use it againtestorassertEqualsFunction, you can refer to it like this:

import { test, assertEquals } from './local-test-utils.ts';

Therefore, it does not matter whether it is loaded from the URL or not.

The second option is to create an import map, which can be specified in the JSON file first:

{
   "imports": {
      "http/": "https://deno.land/std/http/"
   }
}

Then import it:

import { serve } from "http/server.ts";

In order for it to work, it must include--importmapThe flag tells Deno to import the mapping:

deno run --importmap=import_map.json hello_server.ts

What about package version control?

The package provider must support version control, but from the client side, it only needs to set the version number in the URL, as follows:https://unpkg.com/[email protected]/dist/liltest.js

Browser Compatibility

Deno is designed to be browser compatible. Technically, when using ES modules, we can make our programs available in browsers without using any build tools, such as webpack.

However, tools like Babel convert code into ES5 version JavaScript, so that code can run even in older browsers that do not support all the latest features of the language. But it also comes at the expense of containing a lot of unnecessary code in the final file and expanding the output file.

It’s up to us to decide what our main goals are and make choices accordingly.

TypeScript supports out-of-the-box use

Deno can easily use TypeScript without any configuration files. At the same time, you can write programs in pure JavaScript and execute them without any trouble.

summary

Deno, a new runtime for TypeScript and JavaScript, is an interesting project that has been developing steadily for a long time now. But there is still a long way to go before it can be considered stable for use in the production environment.

With its distributed approach, it needs to release the JavaScript ecosystem from the centralized package registry (npm).

Dahl said he wanted to release version 1.0 by the end of the summer, so if you’re interested in Deno’s future development, you can visit its GitHub.


Wechat Public Number: Front-end Pioneer

Welcome to scan the two-dimensional code, pay attention to the public number, and push you fresh front-end technical articles every day.

What is Deno and how is it different from Node. js?

Welcome to continue reading other highly praised articles in this column:

  • Deep Understanding of Shadow DOM v1
  • Step by step teach you how to use WebVR to implement virtual reality games
  • Thirteen Modern CSS Frameworks to Improve your Development Efficiency
  • Fast Start Bootstrap Vue
  • How does the JavaScript engine work? Everything you need to know from call stack to Promise
  • WebSocket Reality: Real-time Communication between Node and React
  • 20 interview questions about Git
  • In-depth analysis of Node.js console.log
  • What exactly is Node. js?
  • Build an API server with Node.js in 30 minutes
  • Object Copy of Javascript
  • Programmers earn less than 30K a month before they are 30 years old. Where to go?
  • 14 Best JavaScript Data Visualization Libraries
  • Eight top-level VS Code extension plug-ins for the front end
  • Complete guide to Node.js multithreading
  • Four Solutions and Implementation of Converting HTML to PDF

  • More articles…