1.3 About Deno
Deno
Deno is a user-friendly, contemporary, and highly secure runtime designed for executing JavaScript and TypeScript. Notably, Deno provides native support for TypeScript, making it seamless to work with this language. Under the hood, Deno is built using Rust, a cutting-edge systems programming language known for its performance and security features.
The V8 engine, renowned for its speed, drives Deno's JavaScript execution. However, when encountering TypeScript code, Deno translates it into JavaScript using the TSC/SWC compiler duo before executing it with V8. This translation process is noteworthy, as it highlights Deno's ability to bridge the gap between TypeScript and JavaScript.
Interestingly, the compilers used in this process differ in their design. The SWC compiler, written in Rust like Deno, prioritizes performance and speed, leveraging Rust's capabilities. In contrast, the TSC compiler, built using JavaScript, completes the task but may be slower compared to SWC.
Unique features of Deno
Deno distinguishes itself from Node.js through several key features, many of which are direct responses to the lessons learned and challenges identified in the previous section. These distinctive attributes embody Deno's innovative approach to creating a runtime environment for JavaScript and TypeScript, setting it apart from its predecessor.
Sandboxing
Deno prioritizes security as its core principle. By default, it adopts a least-privilege approach, denying access to files, networks, child processes, and the environment unless explicitly granted. Every access request must be explicitly approved, ensuring a high level of control and security. For example, file access can be restricted to specific directories or files using permissions, limiting reading and writing capabilities. Similarly, network access can be controlled to specific IPs, domains, and more. Environmental interactions are also governed by permissions, ensuring a granular level of control.
Recent updates to Deno have introduced an additional security feature called "deny," which allows for the comprehensive blocking of various types of access. This denial list functions as an extra layer of security, complementing the allow list and providing a dual-layered protection approach.
Deno's rigorous sandboxing technique significantly enhances its security posture. When a Deno process is initiated, it does not inherit the user's permissions, and access privileges can be configured broadly or with fine granularity. This approach creates a highly secure environment for Deno's operation, similar to running within a container without the need for actual container infrastructure.
Typescript support
Deno offers built-in support for TypeScript, eliminating the need for external library installations. Deno's integrated TSC/SWC compiler handles TypeScript compilation, converting it to JavaScript for execution on the V8 engine. Note that V8 currently doesn't support TypeScript directly, so this conversion is essential.
When initiating Deno, the compiler translates TypeScript code to JavaScript, except in cases involving dynamic imports. This seamless integration simplifies the setup process and ensures a smooth experience for TypeScript projects. The TSC/SWC compiler plays a crucial role in this process, enabling Deno to handle the conversion behind the scenes. This allows developers to focus on writing code without worrying about technicalities, making Deno a runtime that prioritizes developer productivity and ease of use.
Single Executable
Deno is distributed as a self-contained, bundled executable, incorporating a comprehensive suite of development tools. This single executable contains various essential components, including:
Core Engine: Executes TypeScript (TS) and JavaScript (JS) code, forming the runtime environment's foundation.
Upgrade Manager: Enables seamless updates, ensuring access to the latest features and enhancements.
Formatter: Automatically formats code for consistency and readability.
Debugger/Inspector: Facilitates efficient code debugging and exploration.
Test Framework: Allows for comprehensive and reliable testing.
Linter: Analyzes code, highlighting potential errors and deviations from best practices.
Bundler: Consolidates and packages code and dependencies for streamlined deployment.
Code Coverage: Provides insights into testing effectiveness.
Additional Tools and Utilities: Deno's executable includes various other foundational tools, catering to diverse developer needs.
Notably, Deno's self-contained nature eliminates the need for additional package managers or tool installations. Upon installation, the entire environment is set up as a singular executable, with no separate dependencies to fetch and install. This streamlined approach enhances the development experience, fostering efficiency and simplicity throughout the software development lifecycle.
Third-party packages
In contrast to Node.js, which relies heavily on its private package manager, NPM, Deno adopts a different strategy. Deno does not require a package manager to host third-party packages. Instead, it supports the use of standard ES modules, which can be accessed via HTTP or locally. This approach offers flexibility in hosting locations, including GitHub, enterprise web servers, personal web servers, or the local file system. Deno caches these packages before use.
Initially, Deno's creators were hesitant to support NPM, aiming to follow the Go programming language's approach of acquiring packages from diverse sources. However, as a successor to Node.js, Deno faced the challenge of losing access to the vast library of 1.4 million NPM packages. This posed a significant obstacle to gaining acceptance among developers and companies. In response to demand, Deno has since incorporated support for NPM, reconciling the need for package management with its original design principles.
At the time of writing, Deno fully supports NPM.
Top-level await
Deno's top-level await feature provides developers with a valuable tool for working with asynchronous code. This capability allows the use of the await keyword outside of traditional async functions, effectively creating a large async scope that influences the behavior of importing modules. These modules will pause execution before running their main code, awaiting the resolution of the Promise.
While this feature may not seem revolutionary at first glance, its impact on development is significant. The await keyword can now be employed throughout the codebase, eliminating the need for wrapping code in async IIFEs (Immediately Invoked Function Expressions) solely for using await.
It's important to note that this functionality is not unique to Deno, but rather is made possible by the underlying V8 engine that powers Deno. As a result, Deno can offer this feature, and its convenience has even extended beyond Deno to other runtime environments like Node.js, which has also incorporated support for top-level await in its ES modules.
Standard library
Deno distinguishes itself from Node.js by offering a comprehensive standard library, similar to Go's impressive standard library. This extensive library provides a range of peer-reviewed utilities for common tasks, including:
File handling
Hash generation
HTTP request management
Input/output management
MIME type handling
WebSocket connections
Logging
Date and time management
UUID generation
Cryptographic operations
Stream handling
The Deno team actively maintains this library, ensuring its continued relevance and quality. Notably, Deno's standard library APIs exclusively use promises, eliminating the need for outdated callback mechanisms. This modern approach aligns Deno with contemporary asynchronous programming practices, simplifying the coding experience with consistent and predictable methods for managing asynchronous operations.This enhanced usability, combined with the robust and inclusive standard library, makes Deno an attractive choice for developers seeking to build efficient and reliable applications.
Last updated