1.3 About Deno

Deno

Deno stands as a user-friendly, contemporary, and highly secure runtime designed for executing JavaScript and TypeScript. An exceptional quality of Deno is its innate support for TypeScript, making it effortless to work with this language. What's under the hood of Deno is quite remarkable - it's crafted using Rust, a cutting-edge systems programming language renowned for its performance and security features.

The driving force behind Deno's JavaScript execution is Google's highly efficient V8 engine, celebrated for its remarkable speed. However, there's a twist here: V8 is tailored exclusively for JavaScript, which means that when Deno encounters TypeScript code, it undergoes a transformation. Before V8 can do its magic, Deno diligently translates TypeScript code into JavaScript. This transformation task is managed by the TSC/SWC compiler duo.

When it comes to these compilers, there's an interesting contrast to be noted. The SWC compiler, written in Rust much like Deno itself, is engineered for swift performance. Its Rust-based foundation contributes to its impressive speed. On the flip side, we have the TSC compiler, constructed using JavaScript. While it gets the job done, it's not as nimble as SWC and can be a bit sluggish in comparison.

Unique features of Deno

Deno possesses several distinctive features in comparison to Node.js. Many of these distinct attributes stem from the lessons learned and issues highlighted in the preceding section. These features set Deno apart and contribute to its innovative approach to runtime environments for JavaScript and TypeScript.

Sandboxing

Deno ensures security as its primary foundation. By default, Deno doesn't grant access to files, networks, child processes, or the environment unless explicitly permitted. Every access request must be explicitly approved. For instance, file access can be restricted using permissions to limit reading and writing to specific directories or files. Only those specified directories and files will be accessible; Deno will block access to all others.

Likewise, network access can be controlled to specific IPs, domains, and more. The ability to interact with the environment is also governed by permissions. In recent updates, Deno has introduced an additional security aspect called "deny." This denial list can comprehensively obstruct various types of access, including files, networks, processes, and environment. The deny list functions as an extra layer of security on top of the allow list, offering a dual-layered protection approach, much like adding icing to a cake.

This rigorous sandboxing technique enhances Deno's security posture significantly. When a Deno process is initiated, it doesn't inherit all the permissions of the user running it. Access privileges can be configured broadly or with fine granularity, resembling the concept of running Deno within a container, but without needing an actual container infrastructure. This ingenious approach ensures a highly secure environment for Deno's operation.

Typescript support

Deno comes with built-in support for Typescript right from the start. Unlike Node.js, you don't have to worry about installing extra libraries from outside sources. Deno has its own TSC/SWC compiler that handles Typescript. It's worth noting that Typescript code needs to be transformed into Javascript before it can be executed on the V8 engine, which is the engine that powers Deno. This is because V8 doesn't yet comprehend Typescript directly. The conversion from Typescript to Javascript takes place when you initiate Deno, except in cases where dynamic imports are involved.

This integrated Typescript support in Deno not only simplifies the setup process but also ensures a smoother experience when working with Typescript projects. The TSC/SWC compiler plays a vital role in this by seamlessly translating your Typescript code into Javascript, making it compatible with the underlying V8 engine. So, even though there's a conversion step involved, Deno handles it behind the scenes, allowing you to focus on writing your code without getting bogged down by the technicalities of the conversion process. This convenience distinguishes Deno as a runtime that cares about developer productivity and the ease of use.

Single Executable

Deno comes bundled as a unified, self-contained executable package. Within this single executable, Deno houses an entire suite of development tools, presenting a comprehensive toolkit within a single package. The executable comprises a range of essential components:

  1. Core Engine: This serves as the foundation for executing TypeScript (TS) and JavaScript (JS) code, forming the heart of Deno's runtime environment.

  2. Upgrade Manager: Deno's executable includes a built-in upgrade manager, allowing seamless updates to ensure you're always working with the latest enhancements and features.

  3. Formatter: The integrated formatter assists in maintaining consistent code style, automatically arranging your code for improved readability and collaboration.

  4. Debugger/Inspector: A debugger/inspector combination is embedded, facilitating efficient code debugging and exploration, empowering developers to identify and rectify issues with ease.

  5. Test Framework: A testing framework is pre-packaged, enabling you to conduct comprehensive and reliable tests for your codebase, ensuring robustness and accuracy.

  6. Linter: The included linter aids in code analysis, highlighting potential errors and deviations from best practices, promoting code quality and adherence to coding standards.

  7. Bundler: Deno's bundling functionality is incorporated, allowing you to consolidate and package your code and its dependencies, streamlining deployment and distribution.

  8. Code Coverage: Deno's executable also features code coverage capabilities, providing insights into how extensively your codebase is tested, facilitating a greater understanding of testing effectiveness.

  9. And More: These are just a few examples; Deno's single executable encompasses various other foundational tools and utilities, catering to diverse developer needs.

Remarkably, Deno's self-contained nature obviates the necessity for additional package managers or tool installations. Upon Deno's installation, the entire environment is swiftly acquired and set up as a singular executable. Notably, there are no dependencies to fetch and install separately, rendering it a user-friendly solution for both developers and DevOps personnel. This streamlined approach enhances the development experience, fostering efficiency and simplicity throughout the software development lifecycle.

Third-party packages

This section draws upon Ryna's most significant regrets. In the context of Node.js, there was a private package manager known as NPM, which became a crucial component of the Node.js ecosystem. Unlike Node.js, Deno takes a different approach by not mandating the use of a package manager for hosting third-party packages. This doesn't imply that Deno lacks support for third-party packages; on the contrary, it does provide support. These third-party packages can be employed as standard ES modules, accessible through HTTP or even locally. They offer flexibility in terms of hosting locations, ranging from GitHub and enterprise web servers to personal web servers or the local file system. Prior to utilization, Deno stores them in a cache.

More recently, and perhaps with some reluctance, Deno has responded to the demands for NPM support. In the beginning, the creators of Deno were firmly opposed to the notion of accommodating NPM. Similar to the Go programming language, their intention was to enable the acquisition of packages from various sources. However, due to Deno's lineage as a successor to Node.js, reconciling the challenge of losing access to 1.4 million NPM packages was a significant hurdle in gaining acceptance among developers and companies.

Top-level await

The concept of top-level await brings a valuable capability to developers when working with Deno. This feature allows the utilization of the await keyword even outside of the usual async functions. In essence, it's like having a large async function that influences the behavior of modules that import it, causing them to pause before executing their main code.

Even though this might not immediately strike as a revolutionary feature, its significance for developers is quite substantial. The await keyword, which typically waits for a Promise to resolve, can now be employed in various parts of the codebase. The requirement of wrapping code in an async IIFE (Immediately Invoked Function Expression) just to utilize await is eliminated by this enhancement.

It's worth noting that this functionality is not exclusive to Deno alone; it's made possible through the underlying engine that powers Deno, namely v8. This means that Deno can offer this feature thanks to the support provided by v8. Interestingly, the scope of this convenience has expanded beyond Deno. Node.js, a popular runtime environment, has also incorporated support for top-level await within its ES modules.

Standard library

Unlike Node.js, which lacks an official standard library, and similar to Go that boasts an impressive standard library, Deno distinguishes itself by offering an extensive standard library. This library encompasses a range of commonly used and peer-reviewed utilities such as those for handling files, generating hashes, managing HTTP requests, handling input and output, dealing with MIME types, establishing WebSocket connections, managing logs, handling dates and times, generating UUIDs, ensuring cryptographic operations, and handling streams, among others.

What sets Deno's standard library apart is its active maintenance by the Deno team. Unlike Node.js, Deno's standard library APIs are exclusively accessible through promises. This eliminates the need for the old-style callback mechanism, streamlining and modernizing the way developers interact with these utilities.

This approach not only aligns Deno with the contemporary paradigm of asynchronous programming but also simplifies the coding experience by providing consistent and predictable methods for managing asynchronous operations. This leap forward in usability and the presence of a robust and inclusive standard library make Deno an appealing choice for developers looking to build efficient and reliable applications.

Last updated