🎉 Deno:1.0 正式定于 5 月 13 日发布!功能回顾 🤠 简介 🌈 Deno 诞生 🎉 Deno 1.0 - 定于 5 月 13 日发布

2025-05-27

🎉 Deno:1.0 正式定于 5 月 13 日发布!功能回顾

🤠 简介

🌈 Deno 诞生

🎉 Deno 1.0 - 计划于 5 月 13 日发布

🤠 简介

这一切始于两年前,Node.js 的创建者 Ryan Dahl 在 JSConf 2018 上发表了题为“我对 Node.js 感到遗憾的 10 件事”的演讲:

他实际上对 Node.js 感到遗憾的有 7 件事:

  1. 不坚持使用 Promises:他在 2009 年 6 月添加了它们,但在 2010 年 2 月删除了它们,使用 Node.js 中的 Promises,它可以加快 async/await 标准的交付速度

  2. 安全性: Node 进程具有广泛的访问权限,包括系统调用、网络、磁盘 I/O

  3. 构建系统:你需要 GYP 来编译原生 C 库并将其链接到 Node.js 模块,Chrome 曾经使用 GYB,但现在 Node 是唯一的用户

  4. package.json:它包含不必要的信息,例如 LICENCES 和存储库 - 它还使用集中式模块存储库

  5. node_modules:对他来说可能更糟,解析很复杂,文件夹大小通常很大……

  6. 需要不带扩展名:模块加载必须猜测扩展名,而浏览器的工作方式并非如此

  7. index.js:它使模块加载系统变得复杂

🌈 Deno 诞生

凭借在构建 Node.js 过程中学到的所有知识,Ryan 提出了一个名为Deno的新想法:

Deno是一个安全的 JavaScript 和 TypeScript 运行时。
它旨在为现代程序员提供高效且安全的脚本编写环境。它基于 V8、Rust 和 TypeScript 构建。

它不像 Node.js 那样使用 C++,而是基于Rust构建,并在底层使用Tokio。它整合了许多最优秀的开源技术。

🎉 Deno 1.0 - 计划于 5 月 13 日发布

因此,经过近两年的时间,API 已正式冻结,1.0 版本的发布计划于 5 月 13 日。它解决了 Ryan 在他的讲座中谈到的设计缺陷。

开始吧🙌

要安装 Deno,请按照以下说明操作:

使用 Shell:

curl -fsSL https://deno.land/x/install/install.sh | sh

或者使用 PowerShell:

iwr https://deno.land/x/install/install.ps1 -useb | iex

使用 Homebrew(macOS 或 Linux):

brew install deno

使用 Chocolatey(Windows):

choco install deno

查看deno_install了解更多安装选项。

1.0 版本包含哪些内容?👀

Deno 1.0 配备了许多适用于现代开发的实用功能。在下一节中,我们将介绍新版本中所有最强大的功能。

开箱即用,支持 TypeScript

好吧,标题里已经说明了一切。你也可以tsconfig.json使用以下命令来自带:

deno run -c tsconfig.json [program.ts]

安全设计

程序默认无需权限即可运行,如果代码需要权限,则会发出警报。

你需要使用命令行选项来告诉 Deno 程序需要哪些权限。运行后deno run -h你将看到完整的权限列表:

-A, --allow-all                    Allow all permissions
    --allow-env                    Allow environment access
    --allow-hrtime                 Allow high resolution time measurement
    --allow-net=<allow-net>        Allow network access
    --allow-plugin                 Allow loading plugins
    --allow-read=<allow-read>      Allow file system read access
    --allow-run                    Allow running subprocesses
    --allow-write=<allow-write>    Allow file system write access
    --cached-only                  Require that remote dependencies are already cached
    --cert <FILE>                  Load certificate authority from PEM encoded file
-c, --config <FILE>                Load tsconfig.json configuration file
-h, --help                         Prints help information
    --importmap <FILE>             UNSTABLE: Load import map file
    --inspect=<HOST:PORT>          activate inspector on host:port (default: 127.0.0.1:9229)
    --inspect-brk=<HOST:PORT>      activate inspector on host:port and break at start of user script
    --lock <FILE>                  Check the specified lock file
    --lock-write                   Write lock file. Use with --lock.
-L, --log-level <log-level>        Set log level [possible values: debug, info]
    --no-remote                    Do not resolve remote modules
-q, --quiet                        Suppress diagnostic output
-r, --reload=<CACHE_BLACKLIST>     Reload source code cache (recompile TypeScript)
    --seed <NUMBER>                Seed Math.random()
    --unstable                     Enable unstable APIs
    --v8-flags=<v8-flags>          Set V8 command line options. For help: --v8-flags=--help

内置 ECMAScript 模块

Deno 不支持require(),它使用 ES 模块:

import * as log from "https://deno.land/std/log/mod.ts";

包管理非常简单,只需提供您想要使用的包的 URL 即可。由于 URL 可能会更改,出于安全考虑,您可以使用锁定文件(使用 --lock 命令行标志)来确保您运行的代码符合预期。

超级简单的包管理

Deno 不使用 npm。它使用通过 URL 或文件路径引用的模块:

import { serve } from "https://deno.land/std@v0.42.0/http/server.ts";

const s = serve({ port: 8000 });

for await (const req of s) {
  req.respond({ body: "Hello World\n" });
}

您可以直接在 URL 中指定包的版本。例如https://deno.land/std@v0.42.0/http/server.ts

此外,Deno 还提供了内置的依赖检查器(deno info)。

使用deps.ts而不是package.json

Deno 的依赖管理约定是使用一个名为 的唯一文件deps.ts来存储所有依赖项。例如,我们可以看看deps.tsoak ,它是 Deno 受 Koa 启发的http服务器的流行中间件框架:

// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.

// This file contains the external dependencies that oak depends upon

export {
  HTTPOptions,
  HTTPSOptions,
  Response,
  serve,
  Server,
  ServerRequest,
  serveTLS,
} from "https://deno.land/std@v1.0.0-rc1/http/server.ts";
export {
  Status,
  STATUS_TEXT,
} from "https://deno.land/std@v1.0.0-rc1/http/http_status.ts";
export {
  Cookies,
  Cookie,
  setCookie,
  getCookies,
  delCookie,
} from "https://deno.land/std@v1.0.0-rc1/http/cookie.ts";
export {
  basename,
  extname,
  join,
  isAbsolute,
  normalize,
  parse,
  resolve,
  sep,
} from "https://deno.land/std@v1.0.0-rc1/path/mod.ts";
export { HmacSha256 } from "https://deno.land/std@v1.0.0-rc1/util/sha256.ts";
export { assert } from "https://deno.land/std@v1.0.0-rc1/testing/asserts.ts";
export {
  contentType,
  lookup,
} from "https://deno.land/x/media_types@v2.0.0/mod.ts";

(来源:https: //github.com/oakserver/oak/blob/master/deps.ts

JSDoc 内置deno doc

我们致力于提供完整的文档。Deno 内置了 JSDoc,因此您可以在文件中编写 JSDoc 注释。

测试运行器Deno.test()

std/testing/asserts.ts模块提供了一系列断言助手:

  • equal()
  • assert()
  • assertEquals()
  • assertNotEquals()
  • assertStrictEq()
  • assertStrContains()
  • assertMatch()
  • assertArrayContains()
  • assertThrows()
  • assertThrowsAsync()
  • unimplemented()
  • unreachable()

例如:

import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

Deno.test({
  name: "testing example",
  fn(): void {
    assertEquals("world", "world");
    assertEquals({ hello: "world" }, { hello: "world" });
  },
});

(来源:https://github.com/denoland/deno/tree/master/std/testing

跑步deno test file.spec.ts

Compile file.spec.ts
Download https://deno.land/std/testing/asserts.ts
Download https://deno.land/std/fmt/colors.ts
Download https://deno.land/std/testing/diff.ts
running 1 tests
test testing example ... ok (9ms)

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (9ms)

格式化deno fmt <files>

它由dprint提供,是著名的 Prettier 的替代品。

编译和捆绑deno bundle

目前它被标记为不稳定。因此,使用它需要您自行承担风险。

调试器deno run -A --inspect-brk fileToDebug.ts

有了它,您可以打开 Chrome 调试器(chrome://inspect)并开始检查该进程!

已审核(审计)标准库

标准库保证与 Deno 兼容,它们没有外部依赖,并且经过 Deno 核心团队的审核。模块根据 Deno 的版本进行标记,因此 v0.4.0 版本的标准库保证与 Deno v0.4.0 兼容。

模块列表:

W3C 网络标准的使用

Deno 提供以下 W3C Web 标准 API:

  • Request
  • Response
  • addEventListener
  • atob
  • btoa
  • ReadableStream
  • clearInterval
  • clearTimeout
  • dispatchEvent
  • fetch
  • queueMicrotask
  • removeEventListener
  • setInterval
  • setTimeout
  • AbortSignal
  • Blob
  • File
  • FormData
  • Headers
  • URL
  • URLSearchParams
  • console
  • isConsoleInstance
  • location
  • onload
  • onunload
  • self
  • window
  • AbortController
  • CustomEvent
  • DOMException
  • ErrorEvent
  • Event
  • EventTarget
  • MessageEvent
  • TextDecoder
  • TextEncoder
  • Worker
  • ImportMeta
  • Location

示例:使用 Deno 构建一个简单的 HTTP 服务器

import { Application } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

// Logger
app.use(async (ctx, next) => {
  await next();
  const rt = ctx.response.headers.get("X-Response-Time");
  console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`);
});


// Hello World!
app.use((ctx) => {
  ctx.response.body = "Hello World!";
});

await app.listen({ port: 8000 });

让我们使用以下命令来运行它deno server.ts

Compile file:///.../server.ts
Download https://deno.land/x/oak/mod.ts
Download https://deno.land/x/oak/application.ts
Download https://deno.land/x/oak/context.ts
Download https://deno.land/x/oak/cookies.ts
Download https://deno.land/x/oak/httpError.ts
Download https://deno.land/x/oak/middleware.ts
Download https://deno.land/x/oak/request.ts
Download https://deno.land/x/oak/response.ts
Download https://deno.land/x/oak/router.ts
Download https://deno.land/x/oak/send.ts
Download https://deno.land/x/oak/types.ts
Download https://deno.land/x/oak/deps.ts
Download https://deno.land/x/oak/keyStack.ts
Download https://deno.land/x/oak/tssCompare.ts
Download https://deno.land/std@v1.0.0-rc1/http/server.ts
Download https://deno.land/std@v1.0.0-rc1/http/http_status.ts
Download https://deno.land/std@v1.0.0-rc1/http/cookie.ts
Download https://deno.land/std@v1.0.0-rc1/path/mod.ts
Download https://deno.land/std@v1.0.0-rc1/util/sha256.ts
Download https://deno.land/std@v1.0.0-rc1/testing/asserts.ts
Download https://deno.land/x/media_types@v2.0.0/mod.ts
Download https://deno.land/std@v1.0.0-rc1/encoding/utf8.ts
Download https://deno.land/std@v1.0.0-rc1/io/bufio.ts
Download https://deno.land/std@v1.0.0-rc1/util/async.ts
Download https://deno.land/std@v1.0.0-rc1/http/io.ts
Download https://deno.land/std@v1.0.0-rc1/io/util.ts
Download https://deno.land/std@v1.0.0-rc1/path/win32.ts
Download https://deno.land/std@v1.0.0-rc1/path/posix.ts
Download https://deno.land/std@v1.0.0-rc1/path/constants.ts
Download https://deno.land/std@v1.0.0-rc1/path/common.ts
Download https://deno.land/std@v1.0.0-rc1/path/constants.ts
Download https://deno.land/std@v1.0.0-rc1/path/interface.ts
Download https://deno.land/std@v1.0.0-rc1/path/glob.ts
Download https://deno.land/std@v1.0.0-rc1/path/globrex.ts
Download https://deno.land/std@v1.0.0-rc1/path/utils.ts
Download https://deno.land/std@v1.0.0-rc1/fmt/colors.ts
Download https://deno.land/std@v1.0.0-rc1/testing/diff.ts
Download https://deno.land/std@v1.0.0-rc1/textproto/mod.ts
Download https://deno.land/std@v1.0.0-rc1/bytes/mod.ts
Download https://deno.land/std@v1.0.0-rc1/datetime/mod.ts
Download https://deno.land/x/media_types@v2.0.0/db.ts
Download https://deno.land/x/media_types@v2.0.0/deps.ts
Download https://deno.land/std@v0.42.0/path/mod.ts
Download https://deno.land/std@v0.42.0/path/win32.ts
Download https://deno.land/std@v0.42.0/path/posix.ts
Download https://deno.land/std@v0.42.0/path/constants.ts
Download https://deno.land/std@v0.42.0/path/common.ts
Download https://deno.land/std@v0.42.0/path/constants.ts
Download https://deno.land/std@v0.42.0/path/interface.ts
Download https://deno.land/std@v0.42.0/path/glob.ts
Download https://deno.land/std@v0.42.0/path/globrex.ts
Download https://deno.land/std@v0.42.0/path/utils.ts
Download https://deno.land/std@v0.42.0/testing/asserts.ts
Download https://deno.land/std@v0.42.0/fmt/colors.ts
Download https://deno.land/std@v0.42.0/testing/diff.ts
Download https://deno.land/x/oak/encoding.ts
Download https://deno.land/x/oak/isMediaType.ts
Download https://deno.land/x/oak/mediaType.ts
Download https://deno.land/x/oak/mediaTyper.ts
Download https://deno.land/x/oak/util.ts
Download https://deno.land/x/oak/pathToRegExp.ts
error: Uncaught PermissionDenied: network access to "127.0.0.1:8000", run again with the --allow-net flag
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendSync ($deno$/ops/dispatch_json.ts:72:10)
    at Object.listen ($deno$/ops/net.ts:51:10)
    at listen ($deno$/net.ts:164:18)
    at Application.serve (server.ts:261:20)
    at Application.listen (application.ts:106:31)
    at server.ts:18:11

让我们添加缺少的权限deno --allow-net server.ts

$ curl http://127.0.0.1:8000
Hello World!

您现在已经准备好迎接即将发布的 Deno 1.0 了——敬请期待😍 如果您喜欢这篇文章,请给我留言!您也可以在 Twitter 上关注我@loverdeolivier 🙌

本文最初发布在我的博客olivier.codes上- https://olivier.codes/2020/05/08/Deno-1-0-officially-scheduled-on-May-13-Review-of-the-features/

文章来源:https://dev.to/olivierloverde/deno-1-0-officially-scheduled-on-may-13-review-of-the-features-1kb0
PREV
使用 Terraform 模块掌握基础设施即代码:实用指南
NEXT
使用 React Context 管理状态 1. 管理状态 2. 状态管理方式 CONTEXT API 总结