Deno 1.30 发布,内置 Node 模块

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

Deno 1.30 已发布,值得关注的更新包括:

对内置 Node.js 模块的支持

在 Deno 中,npm 包已经可以通过 Deno 的 Node.js 兼容层访问内置的 Node.js 模块,如 fs、path、process 等等。

在这个版本中,这些模块通过 node: specifiers 暴露给 Deno 代码:

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

如果你同时使用 Deno 和 Node.js 的代码,node: 方案将在两个运行时中工作。

deno.json 成为一个导入映射

这个版本带来了对配置文件的重大更新,现在可以直接使用 deno.json 文件作为导入映射。

在以前的版本中,可以通过指定 importMap 键和导入映射文件的路径来告诉 Deno 去哪里寻找导入映射文件。

许多用户发现它很有用,但是这种方法意味着有两个配置文件。为了更简洁,你现在可以在你的配置文件中指定 importsscopes键,Deno 将自动开始把配置文件当作导入映射。

Node/npm 和 LSP 的修复

这个版本包括超过 25 个与 npm 功能和 Node-API 相关的错误修复。此外,LSP 继续得到改进,有超过 10 个 bug 修复。

Deno API 的改变

对稳定 API 的改变:

  • Deno.permissions APIs 获得了同步对应部分
Deno.permissions.querySync({ name: "read", path: "./log.txt" }) 。
Deno.permissions.revokeSync({ name: "read", path: "./log.txt" })。
Deno.permissions.requestSync({ name: "read", path: "./log.txt" });
  • 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"));
    
  • ……

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,
    ]);
    

新的不稳定的 API:

这个版本增加了 3 个新的 API:

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

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

移除内部 Deno.core

这个版本删除了 Deno.core命名空间。Deno.core是一个私有的 API,没有稳定性保证。这一变化对大多数用户应该没有影响。

Deno fmt 支持配置分号

Deno fmt 的一个长期以来被反复要求的功能是能够在没有分号的情况下进行格式化。现在可以通过使用 --options-no-semicolons 标志或在 Deno 配置文件的 fmt 配置中指定 "semiColons": false 来实现这一功能。

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

更多详情可查看:https://github.com/denoland/deno/releases/tag/v1.30.0

猜你喜欢

转载自www.oschina.net/news/226137/deno-1-30-released