Deno (software)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Deno
Deno.svg
Original author(s)Ryan Dahl
Developer(s)Various
Initial releaseMay 13, 2018; 19 months ago (2018-05-13)[1]
Preview release
0.28.1 / January 3, 2020; 9 days ago (2020-01-03)[2]
Repositoryhttps://github.com/denoland/deno
Written inTypeScript, JavaScript, Rust, C++
Operating systemLinux, macOS, Microsoft Windows
TypeRun-time environment
LicenseMIT license[3][4]
Websitedeno.land

Deno is a runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language. It was created by Ryan Dahl, original creator of Node.js, and is focused on security and productivity.[5] It was announced by Dahl in 2018 during his talk "10 Things I Regret About Node.js".[6] Deno explicitly takes on the role of both runtime and package manager within a single executable, rather than requiring a separate package-management program.[7][8]

History[edit]

Deno was announced on JSConf EU 2018 by Ryan Dahl in his talk "10 Things I Regret About Node.js".[6] In his talk, Ryan mentioned his regrets about the initial design decisions with Node.js, focusing on his choices of not using Promises in API design, usage of the legacy GYP building system, node_modules and package.json, leaving out file extensions, magical module resolution with index.js and breaking the sandboxed environment of V8.[9] He eventually presented the prototype of Deno, aiming to achieve system call bindings through message passing with serialization tools such as Protocol Buffers, and to provide command line flags for access control.

Deno was initially written in Go and used Protocol Buffers for serialization between privileged (Go, with system call access) and unprivileged (V8) sides.[10] However, Go was soon replaced with Rust due to concerns of double runtime and garbage collection pressure.[11] Tokio is introduced in place of libuv as the asynchronous event-driven platform,[12] and Flatbuffers is adopted for faster, "zero-copy" serialization and deserialization[13] but later in August 2019, FlatBuffers were finally removed[14] after publishing benchmarks that measured an significant overhead of serialization in April 2019.[15]

A standard library, modeled after Go's standard library, was created in November 2018 to provide extensive tools and utilities, partially solving Node.js' dependency tree explosion problem.[16]

Overview[edit]

Deno aims to be a productive and secure scripting environment for the modern programmer.[7] Similar to Node.js, Deno emphasize on event-driven architecture, providing a set of non-blocking core IO utilities, along with their blocking versions. Deno could be used to create web servers, perform scientific computations, etc.

Comparison with Node.js[edit]

Deno and Node.js are both runtimes built on Google's V8 JavaScript engine, the same engine used in Google Chrome. They both have internal event loops and provide command-line interfaces for running scripts and a wide range of system utilities.

In the meantime, Deno mainly deviates from Node.js in the following aspects[7]:

  1. Uses ES Module as the default module system, instead of CommonJS.
  2. Uses URLs for loading local or remote dependencies, similar to browsers.
  3. Includes a built-in package manager for resource fetching, thus no need for NPM.
  4. Supports TypeScript out of the box, using a snapshotted TypeScript compiler with caching mechanisms.
  5. Aims better compatibility with browsers with a wide range of Web API.
  6. Allows control to file system and network access in order to run sandboxed code.
  7. Redesigns API to utilize Promises, ES6 and TypeScript features.
  8. Minimizes core API size, while providing a large standard library with no external dependencies.
  9. Using message passing channels for invoking privileged system APIs and using bindings.

Example[edit]

The following runs a basic Deno script without any read/write/network permissions (sandbox mode):

deno run main.ts

Explicit flags are required to expose corresponding permission:

deno run --allow-read --allow-net main.ts

To inspect the dependency tree of the script, use the info subcommand:

deno info main.ts

A basic hello-world program in Deno looks like the following (as in Node.js):

console.log("Hello world");

Deno provides the global namespace for most Deno specific APIs that are not available in the browser. A Unix cat program could be implemented as follows:

/* cat.ts */

/* Deno APIs are exposed through the `Deno` namespace. */
const { stdout, open, copy, args } = Deno;

// Top-level await is supported
for (let i = 0; i < args.length; i++) {
    const filename = args[i]; // Obtains command-line arguments.
    const file = await open(filename); // Opens the corresponding file for reading.
    await copy(stdout, file); // Performs a zero-copy asynchronous copy from `file` to `stdout`.
}

The above Deno.copy function works in the similar way as Go's io.Copy, where stdout (standard output) is the destination Writer and file is the source Reader. To run this program, we need to provide the read permission to the filesystem:

deno run --allow-read cat.ts myfile

The following Deno script implements a basic HTTP server:

// Imports `serve` from the remote Deno standard library, using URL.
import { serve } from "https://deno.land/std@v0.21.0/http/server.ts";

const body = new TextEncoder().encode("Hello World\n");
// `serve` function returns an asynchronous iterator, yielding a stream of requests
for await (const req of serve(":8000")) {
    req.respond({ body });
}

When running this program, Deno would automatically download and cache the remote standard library files and compile the code. Similarly, we can run a standard library script (such as a file server) directly without explicitly downloading, by providing the URL as input filename (-A turns on all permissions):

$ deno run -A https://deno.land/std/http/file_server.ts
Download https://deno.land/std/http/file_server.ts
Compile https://deno.land/std/http/file_server.ts
...
HTTP server listening on http://0.0.0.0:4500/

References[edit]

  1. ^ "Contributors, denoland/deno, Github". Retrieved 5 July 2019.
  2. ^ "Deno 0.28.1". Retrieved 3 January 2020 – via GitHub.
  3. ^ "deno/LICENSE at master". GitHub. Retrieved 5 July 2019.
  4. ^ "The MIT License". Open Source Initiative. 17 September 2018. Retrieved 17 September 2018.
  5. ^ "Deno: Secure V8 TypeScript Runtime from Original Node.js Creator". InfoQ. Retrieved 2019-05-17.
  6. ^ a b JSConf (2018-06-06), 10 Things I Regret About Node.js - Ryan Dahl - JSConf EU 2018, retrieved 2019-05-17
  7. ^ a b c "Deno Manual". deno.land. Retrieved 2019-05-17.
  8. ^ Paul Krill (2018-06-21). "Ryan Dahl's Node.js regrets lead to Deno". InfoWorld.
  9. ^ Dahl, Ryan (2018-06-06). "Design mistakes in Node" (PDF). Github.
  10. ^ "denoland/deno, branch "golang"". Github.
  11. ^ "Suggestion: Look into porting to Rust and using Tokio". GitHub.
  12. ^ "Tokio - The asynchronous run-time for the Rust programming language". Tokio.rs.
  13. ^ "Protobuf seems like a lot of overhead for this use case?". Github.
  14. ^ "Remove flatbuffers". GitHub.
  15. ^ "Replace flatbuffers". GitHub.
  16. ^ "denoland/deno_std: deno standard modules". Github.

External links[edit]