跳到主要内容
Deno 2.4 发布,带来 deno bundle、字节/文本导入、OTel 稳定版等功能
了解更多
Deno 1.30 ...

Deno 1.30:内置 Node 模块

Deno 1.30 已打标签并发布,包含以下新特性和变更


📝 帮助改进 Deno 并有机会赢取一张 100 美元亚马逊礼品卡,请参与我们的调查


如果您已安装 Deno,可以通过运行以下命令升级到 1.30

deno upgrade

如果您是首次安装 Deno

# MacOS and Linux
curl -fsSL https://deno.land/x/install/install.sh | sh

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

点击此处查看更多安装选项。

支持内置 Node.js 模块

在 Deno 中,npm 包已经能够通过 Deno 的Node.js 兼容层访问内置 Node.js 模块,例如 fspathprocess 等等。

在此版本中,这些模块通过node: 说明符暴露给 Deno 代码。

import { readFileSync } from "node:fs";

console.log(readFileSync("deno.json", { encoding: "utf8" }));

请注意,不支持在没有导入映射的情况下通过裸说明符(例如 import { readFileSync } from "fs";)进行导入。如果您尝试这样做,并且裸说明符与导入映射中未找到的 Node.js 内置模块匹配,Deno 将提供一条有用的错误消息,询问您是否打算使用 node: 前缀导入。此外,LSP 还提供了一个快速修复,可以将说明符更新为 node:

A screenshot of an editor showing the bare specifier to node: specifier quick fix.

如果您同时使用 Deno 和 Node.js 代码,node: 方案在这两种运行时中都将起作用,因此建议您无论如何都为您的 Node.js 代码更新到此方案。

deno.json 成为导入映射

本次发布对配置文件进行了重大更新——现在可以直接将 deno.json 文件用作导入映射。在之前的版本中,可以通过指定 importMap 键和导入映射文件的路径来告诉 Deno 在何处查找导入映射文件。许多用户发现这很有用,但这种方法意味着有两个配置文件。为了使其更简洁,您现在可以在配置文件中指定 importsscopes 键,Deno 将自动开始将此配置文件视为导入映射。

deno.json 示例

{
  "imports": {
    "std/": "https://deno.land/std@0.174.0/"
  }
}

然后以下带有 std 裸说明符的脚本即可运行

import { assertEquals } from "std/testing/assert.ts";

assertEquals(1, 2);

您知道我们接下来会怎么做吗?

Node/npm 和 LSP 修复

此版本包含了超过 25 个与 npm 功能和 Node-API 相关的错误修复。此外,LSP 也持续改进,修复了 10 多个错误。完整列表请参见发布说明

Deno API 变更

稳定 API 变更

  • Deno.permissions API 获得同步对应项

    Deno.permissions.querySync({ name: "read", path: "./log.txt" });
    Deno.permissions.revokeSync({ name: "read", path: "./log.txt" });
    Deno.permissions.requestSync({ name: "read", path: "./log.txt" });

    感谢 Asher Gomez 实现此功能。

  • Deno.writeFile() 和 Deno.writeTextFile() 现在第二个参数接受 ReadableStream。

    const stream = new ReadableStream({
      pull(controller) {
        controller.enqueue(new Uint8Array([1]));
        controller.enqueue(new Uint8Array([2]));
        controller.close();
      },
    });
    await Deno.writeFile("/tmp/test.txt", stream);
    assertEquals(Deno.readFileSync(filename), new Uint8Array([1, 2]));
  • 新增 Deno.env.has(name) API

    Deno.env.set("TEST_VAR", "A");
    assert(Deno.env.has("TEST_VAR"));
    Deno.env.delete("TEST_VAR");
    assert(!Deno.env.has("TEST_VAR"));
  • Deno.Seeker API 支持 bigint 偏移量。

    您现在可以将 bigint 类型作为 Seeker 接口的参数使用

    const file = await Deno.open("./log.txt");
    const cursor = await file.seek(150n, Deno.SeekMode.Start);
  • 测试步骤可以是函数

    以前,使用测试步骤 API 要求测试步骤的第一个参数是名称或测试定义

    Deno.test("my test", async (t) => {
      const success = await t.step("step1", async () => {
        await t.step(function inner1() {});
        await t.step(function inner1() {});
      });
      if (!success) throw new Error("Expected the step to return true.");
    });

    从本次发布开始,第一个参数也可以是命名函数

    Deno.test("my test", async (t) => {
      const success = await t.step(async function step1() {
        await t.step(function inner1() {});
        await t.step(function inner1() {});
      });
      if (!success) throw new Error("Expected the step to return true.");
    });

API 稳定化

Deno.Listener.ref() 和 Deno.Listener.unref() 现在已稳定。使用这些 API 不再需要 --unstable 标志。

不稳定 API 变更

  • new Deno.Command({}).spawn() 中,stdin 选项的默认值已更改为 "inherit"——这意味着如果您未明确配置此选项,标准输入将从父进程继承。

  • Deno.dlopen 添加了按值传递结构体的支持

    const Rect = ["f64", "f64", "f64", "f64"];
    const dylib = Deno.dlopen("./dylib.so", {
      make_rect: {
        parameters: ["f64", "f64", "f64", "f64"],
        result: { struct: Rect },
      },
    });
    const rect_sync = dylib.symbols.make_rect(10, 20, 100, 200);
    assertInstanceOf(rect_sync, Uint8Array);
    assertEquals(rect_sync.length, 4 * 8);
    assertEquals(Array.from(new Float64Array(rect_sync.buffer)), [
      10,
      20,
      100,
      200,
    ]);

    感谢 @DjDeveloperrAapo Alasuutari 实现此功能。

新增不稳定 API

本次发布新增 3 个 API

  • Deno.osUptime() (需要 --allow-sys=osUptime 权限)
  • Deno.Conn.ref()
  • Deno.Conn.unref()

这些 API 需要 --unstable 标志,但我们计划在下一个版本中将其稳定化。

感谢 Kamil Ogórek 实现此功能。

移除内部 Deno.core

此版本移除了 Deno.core 命名空间。Deno.core 是一个没有稳定性保证的私有 API。此更改对大多数用户应该没有影响。

deno fmt 支持配置分号

deno fmt 长期以来一直被反复要求的功能是无分号格式化。现在可以通过使用 --options-no-semicolons 标志或在 Deno 配置文件中的 fmt 配置中指定 "semiColons": false 来实现此功能。

{
  "fmt": {
    "options": {
      "semiColons": false
    }
  }
}


📝 帮助改进 Deno 并有机会赢取一张 100 美元亚马逊礼品卡,请参与我们的调查