Deno (software)
Original author(s) | Ryan Dahl |
---|---|
Developer(s) | Various |
Initial release | May 13, 2018[1] |
Stable release | 1.1.1
/ June 19, 2020[2] |
Preview release | 1.0.0-rc3
/ May 12, 2020[3] |
Repository | https://github.com/denoland/deno |
Written in | TypeScript, JavaScript, Rust, C++ |
Operating system | Linux, macOS, Microsoft Windows |
Type | Run-time environment |
License | MIT license[4][5] |
Website | deno |
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 productivity.[6] It was announced by Dahl in 2018 during his talk "10 Things I Regret About Node.js".[7] Deno explicitly takes on the role of both runtime and package manager within a single executable, rather than requiring a separate package-management program.[8][9]
History
Deno was announced on JSConf EU 2018 by Ryan Dahl in his talk "10 Things I Regret About Node.js".[7] 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.[10] 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.[11] However, Go was soon replaced with Rust due to concerns of double runtime and garbage collection pressure.[12] Tokio is introduced in place of libuv as the asynchronous event-driven platform,[13] and Flatbuffers is adopted for faster, "zero-copy" serialization and deserialization[14] but later in August 2019, FlatBuffers were finally removed[15] after publishing benchmarks that measured a significant overhead of serialization in April 2019.[16]
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.[17]
The official Deno 1.0 was released on May 13, 2020.[18]
Overview
Deno aims to be a productive scripting environment for the modern programmer.[8] Similar to Node.js, Deno emphasizes 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
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.
Deno mainly deviates from Node.js in the following aspects[8]:
- Uses ES Module as the default module system, instead of CommonJS.
- Uses URLs for loading local or remote dependencies, similar to browsers.
- Includes a built-in package manager for resource fetching, thus no need for NPM.
- Supports TypeScript out of the box, using a snapshotted TypeScript compiler with caching mechanisms.
- Aims for better compatibility with browsers with a wide range of Web APIs.
- Allows file system and network access in order to run sandboxed code.
- Redesigns API to utilize Promises, ES6 and TypeScript features.
- Minimizes core API size, while providing a large standard library with no external dependencies.
- Using message passing channels for invoking privileged system APIs and using bindings.
Example
This runs a basic Deno script without any file system or network permissions (sandbox mode):
deno run main.ts
Explicit flags are required to enable permissions:
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 just like it would in Node.js:
console.log("Hello world");
A global Deno
namespace exposes 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(file, stdout); // Performs a zero-copy asynchronous copy from `file` to `stdout`.
}
The Deno.copy
function used above works much like 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 enable 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";
// `serve` function returns an asynchronous iterator, yielding a stream of requests
for await (const req of serve({ port: 8000 })) {
req.respond({ body: "Hello World\n" });
}
When running this program, Deno will 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 the 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
- ^ "Contributors, denoland/deno, Github". Retrieved 5 July 2019.
- ^ "Releases · denoland/deno". Retrieved 19 June 2020.
- ^ "Deno 1.0.0-rc3". Retrieved 12 May 2020 – via GitHub.
- ^ "deno/LICENSE at master". GitHub. Retrieved 5 July 2019.
- ^ "The MIT License". Open Source Initiative. 17 September 2018. Retrieved 17 September 2018.
- ^ "Deno: Secure V8 TypeScript Runtime from Original Node.js Creator". InfoQ. Retrieved 2019-05-17.
- ^ a b JSConf (2018-06-06), 10 Things I Regret About Node.js - Ryan Dahl - JSConf EU 2018, retrieved 2019-05-17
- ^ a b c "Deno Manual". deno.land. Retrieved 2019-05-17.
- ^ Paul Krill (2018-06-21). "Ryan Dahl's Node.js regrets lead to Deno". InfoWorld.
- ^ Dahl, Ryan (2018-06-06). "Design mistakes in Node" (PDF). Github.
- ^ "denoland/deno, branch "golang"". Github.
- ^ "Suggestion: Look into porting to Rust and using Tokio". GitHub.
- ^ "Tokio - The asynchronous run-time for the Rust programming language". Tokio.rs.
- ^ "Protobuf seems like a lot of overhead for this use case?". Github.
- ^ "Remove flatbuffers". GitHub.
- ^ "Replace flatbuffers". GitHub.
- ^ "denoland/deno_std: deno standard modules". Github.
- ^ "Deno 1.0". deno.land. Retrieved 2020-05-14.