Deno

Deno 是一个简单、现代且安全的 JavaScript 和 TypeScript 运行时,它使用 V8 并使用 Rust 构建。

特征

  • 默认安全。 除非明确启用,否则不能访问文件、网络或环境。

  • 开箱即用地支持 TypeScript。

  • 只提供一个可执行文件。

  • 内置实用程序,如依赖项检查器 (deno info) 和代码格式化程序 (deno fmt)。

  • 一组经过审核的标准模块,保证可以与 Deno 一起使用。

安装

Shell (Mac, Linux):

curl -fsSL https://deno.land/x/install/install.sh | sh
  • Homebrew (Mac):
brew install deno

windows 安装

  • PowerShell (Windows):
iwr https://deno.land/x/install/install.ps1 -useb | iex
  • Chocolatey (Windows):
choco install deno

快速开始

windows10 安装

powershell 执行的时候使用管理员权限,避免没有文件夹权限。

windows10 安装日志:

PS C:\Windows\system32> choco install deno
Chocolatey v0.10.11
Installing the following packages:
deno
By installing you accept licenses for the packages.

deno v1.13.2 [Approved]
deno package files install completed. Performing other installation steps.
The package deno wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint): Y

Using system proxy server '127.0.0.1:60560'.
Downloading deno 64 bit
  from 'https://github.com/denoland/deno/releases/download/v1.13.2/deno-x86_64-pc-windows-msvc.zip'
Using system proxy server '127.0.0.1:60560'.
Progress: 100% - Completed download of C:\Users\binbin.hou\AppData\Local\Temp\chocolatey\deno\1.13.2\deno-x86_64-pc-windows-msvc.zip (24.51 MB).
Download of deno-x86_64-pc-windows-msvc.zip (24.51 MB) completed.
Hashes match.
Extracting C:\Users\binbin.hou\AppData\Local\Temp\chocolatey\deno\1.13.2\deno-x86_64-pc-windows-msvc.zip to C:\ProgramData\chocolatey\lib\deno...
C:\ProgramData\chocolatey\lib\deno
Run 'deno --help' to get started
 ShimGen has successfully created a shim for deno.exe
 The install of deno was successful.
  Software installed to 'C:\ProgramData\chocolatey\lib\deno'

Chocolatey installed 1/1 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

测试

运行测试脚本

deno run https://deno.land/std/examples/welcome.ts

日志:

Download https://deno.land/std/examples/welcome.ts
Warning Implicitly using latest version (0.106.0) for https://deno.land/std/examples/welcome.ts
Download https://deno.land/std@0.106.0/examples/welcome.ts
Check https://deno.land/std/examples/welcome.ts
Welcome to Deno!

ps: welcome.ts 的内容非常简单:

console.log("Welcome to Deno!");

更复杂的例子

const listener = Deno.listen({ port: 8000 });
console.log("http://localhost:8000/");

for await (const conn of listener) {
  serve(conn);
}

async function serve(conn: Deno.Conn) {
  for await (const { respondWith } of Deno.serveHttp(conn)) {
    respondWith(new Response("Hello world"));
  }
}

您可以在手册中找到更深入的介绍、示例和环境设置指南。

运行时文档中提供了完整的 API 参考。

参考资料

https://github.com/denoland/deno