Deno- JavaScript and TypeScript Runtime
Md Abdul Momin
Posted on August 13, 2020
photo credit: https://pexels.com
Introduction
Almost two years ago, Ryan Dahl, the creator of Node.js, talked about the ten things he regretted about Node.js. At the same time, he introduced DENO, a prototype of a new, security-first, npm-less JavaScript, and typescript runtime. Recently DENO 1.0 released.
Why Deno?
We know that javascript is the battle field-tested Dynamic language for the web, and we cannot imagine the web industry without JavaScript. Through standard organizations like ECMA international, the language has been evolving day by day. It's easy to explain why is the natural choice for dynamic language tooling, whether in a browser environment or as standalone processes.
NodeJS: open-source, cross-platform, JavaScript runtime environment, invented by the same author almost ten years ago. People have found it useful for web development, tooling, creating a server, and many other use cases. In the presentation, ten things regret nodejs that are discussed in more detail.
Now the changing JavaScript world, and new additions like TypeScript, building Node projects can become a problematic effort, involving managing build systems and another heavy-handed tooling that takes away from the fun of dynamic language scripting. Furthermore, the mechanism for linking to external libraries is fundamentally centralized through the NPM repository, which is not in line with the web's ideals.
Deno
Deno is a new runtime for executing JavaScript and TypeScript outside of the web browser.
Deno attempts to provide a complete solution for quickly scripting complex functionality.
[code]
Will it replace NodeJS?
NodeJs is a battle field-tested platform and incredibly well supported that is going to evolve day by day.
Typescript Support
Under the hood, deno built on V8, Rust, and Tokio. The rusty_v8
crate provides high-quality Rust bindings to V8's C++ API
. So it is easy to explain written in particular TypeScript means we get a lot of the benefits of TypeScript even if we might choose to write our code in plain JavaScript. So deno does not require typescript compilation setup, deno do it for automatically.
Node Vs. Deno
Both are developed upon chrome V8 engines, and great for developing server-side with JavaScript. Node written in C++, deno written in Rust and typescript. Node has officially package manager called npm, and deno has no package manager, instead of package manager deno call ES module from URLs. Node uses the CommonJS syntax for importing packages, deno uses Es Modules. Deno uses modern ECMA script feature in all its API and standard library, while nodejs use a callback-based standard library. Deno offers a security layer through permission. A Node.js program can access anything the user can access.
Install Deno
Using Homebrew (macOS):
brew install deno
Using Powershell
iwr https://deno.land/x/install/install.ps1 -useb | iex
testing your installation via deno --version
to know help text use deno -help
and to upgrade previously installed deno use deno upgrade
.
deno 1.0.0
A secure JavaScript and TypeScript runtime
Docs: https://deno.land/std/manual.md
Modules: https://deno.land/std/ https://deno.land/x/
Bugs: https://github.com/denoland/deno/issues
To start the REPL:
deno
To execute a script:
deno run https://deno.land/std/examples/welcome.ts
To evaluate code in the shell:
deno eval "console.log(30933 + 404)"
USAGE:
deno [OPTIONS] [SUBCOMMAND]
OPTIONS:
-h, --help Prints help information
-L, --log-level <log-level> Set log level [possible values: debug, info]
-q, --quiet Suppress diagnostic output
-V, --version Prints version information
SUBCOMMANDS:
bundle Bundle module and dependencies into single file
cache Cache the dependencies
completions Generate shell completions
doc Show documentation for a module
eval Eval script
fmt Format source files
help Prints this message or the help of the given subcommand(s)
info Show info about cache or info related to source file
install Install script as an executable
repl Read Eval Print Loop
run Run a program given a filename or url to the module
test Run tests
types Print runtime TypeScript declarations
upgrade Upgrade deno executable to given version
ENVIRONMENT VARIABLES:
DENO_DIR Set deno's base directory (defaults to $HOME/.deno)
DENO_INSTALL_ROOT Set deno install's output directory
(defaults to $HOME/.deno/bin)
NO_COLOR Set to disable color
HTTP_PROXY Proxy address for HTTP requests
(module downloads, fetch)
HTTPS_PROXY Same but for HTTPS
Your first Deno app
Hello World
This is simple example to teach you about fundamental about deno
deno run https://deno.land/std/examples/welcome.ts
Making an http request
const url = Deno.args[0];
const res = await fetch(url);
const body = new Uint8Array(await res.arrayBuffer());
await Deno.stdout.write(body);
Let's walk through what this application does:
- Here we get the first argument passed to the application and store it in the variable url.
- Then we make a request to the url specified, await the response, and store it in a variable named res.
- Then we parse the response body as an ArrayBuffer, await the response, convert it into a Uint8Array and store it in the variable body.
- And we write the contents of the body variable to stdout.
Try this following example
deno run https://deno.land/std/examples/curl.ts https://example.com
you will see an error regarding network access. So what was the wrong? We know that Deno is a runtime that is secure by default. This means that we have to need explicitly give programs the permission to do certain privileged actions like network access.
Try outs again
deno run --allow-net=example.com https://deno.land/std/examples/curl.ts https://example.com
Simple TCP Server
This is an example of a simple server which accepts connections on port 8080, and returns to the client anything it sends.
const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
for await (const conn of listener) {
Deno.copy(conn, conn);
}
For security reasons, Deno does not allow programs to access the network without explicit permission. To allow accessing the network, use a command-line flag:
deno run --allow-net https://deno.land/std/examples/echo_server.ts
To test it, try sending data to it with netcat:
$ nc localhost 8080
hello world
hello world
Like the cat.ts
example, the copy()
function here also does not make unnecessary memory copies. It receives a packet from the kernel and sends back, without further complexity.
Resources
Posted on August 13, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.