Deno 1.29:自定义 npm 注册表支持
Deno 1.29 已打标签并发布,带来以下新特性和变更:
如果您已安装 Deno,可以通过运行以下命令升级到 1.29:
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
点击此处了解更多安装选项。
npm 兼容性改进
此版本带来了多项 npm 兼容性改进,以及自 1.28.0 以来的 30+ 项 bug 修复。
通过环境变量支持自定义注册表
Deno 现在遵循 NPM_CONFIG_REGISTRY
环境变量,这允许指定自定义 npm 注册表。
# change this to a custom registry
> NPM_CONFIG_REGISTRY=https://registry.npmjs.org deno run main.ts
在未来的版本中,将支持按包范围使用不同注册表以及设置凭据。请关注问题 #16105 以获取更新。
deno install
支持
npm 说明符现在可以与 deno install
一起使用。
> deno install -A npm:cowsay@1.5.0
✅ Successfully installed cowsay
C:\Users\david\.deno\bin\cowsay.cmd
C:\Users\david\.deno\bin\cowsay (shell)
> cowsay Hello from deno!
__________________
< Hello from deno! >
------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
这还将在首次运行时为该命令创建一个锁文件,以确保每次后续运行都使用相同的 npm 依赖版本。
REPL 变更
REPL 是开发者工具链中非常有价值的工具;它允许快速实验。此版本为 REPL 带来了大量更新:
npm 支持
您现在可以直接从 REPL 使用 npm 包。与其他 Deno 子命令一样,无需预先安装步骤 - 只需导入包即可使用。
$ deno
> import cowsay from "npm:cowsay"
undefined
> console.log(cowsay.say({ text: "hello world" }))
_____________
< hello world >
-------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
undefined
>
deno repl
默认无权限运行
在此版本之前,deno
和 deno repl
命令都会以完全权限启动 REPL。虽然对于快速实验来说,无需回答权限提示很有用,但这与 Deno 的“默认安全”理念有些冲突。
此版本更改了 deno repl
子命令,使其默认不授予任何权限。权限可以使用 --allow-*
标志指定,或者在调用需要权限的 API 时可以推迟到权限提示。
$ deno repl --allow-write
Deno 1.29.0
Run repl.help() to see help
exit using ctrl+d, ctrl+c, or close()
> Deno.writeTextFileSync("hello.txt", "hello world");
undefined
$ deno repl
Deno 1.29.0
Run repl.help() to see help
exit using ctrl+d, ctrl+c, or close()
> Deno.writeTextFileSync("hello.txt", "hello world");
⚠️ ┌ Deno requests write access to "hello.txt".
├ Requested by `Deno.writeFileSync()` API
├ Run again with --allow-write to bypass this prompt.
└ Allow? [y/n] (y = yes, allow; n = no, deny) > y
✅ Granted write access to "hello.txt".
undefined
为了更清晰,运行 deno
会显示一个横幅,告知所有权限都已允许,并建议如果您想限制权限,请使用 deno repl
子命令。
$ deno
Deno 1.29.0
Run repl.help() to see help
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
>
更可靠的历史记录处理
REPL 现在将在每次执行语句时更新历史文件,使历史记录不再零散。
--quiet
标志现在得到遵守
如果您使用 --eval
或 --eval-file
来构建自定义 REPL,可能不希望在启动时看到默认的横幅。REPL 现在遵守 --quiet
标志,这将隐藏 REPL 的所有辅助输出。
$ deno --quiet
>
用户体验改进
deno init
改进
deno init
子命令是在 v1.25 版本中添加的,允许用户快速搭建新项目(尽管它是完全可选的)。虽然这个子命令非常方便,但它也极其精简——只生成 main.ts
和 main_test.ts
文件。
为了使其更有用并允许 IDE 发现我们刚刚初始化了一个新的 Deno 项目,deno init
还将生成一个 deno.jsonc
文件以及一个 main_bench.ts
文件。
该子命令的输出也已刷新。
Deno v1.28 中的 deno init
$ deno init ./my_deno_project
✅ Project initialized
Run these commands to get started
cd ./my_deno_project
deno run main.ts
deno test
Deno v1.29 中的 deno init
$ deno init ./my_deno_project
✅ Project initialized
Run these commands to get started
cd ./my_deno_project
// Run the program
deno run main.ts
// Run the program and watch for file changes
deno task dev
// Run the tests
deno test
// Run the benchmarks
deno bench
感谢 sigmaSd 贡献这些改进。
配置文件改进
从本版本开始,您可以在配置文件中为 deno bench
指定文件包含和排除,以及锁文件。
配置 deno bench
{
"bench": {
"files": {
"include": ["./benches/"],
"exclude": ["./benches/testdata/"]
}
}
}
如果您想为锁文件使用非默认文件名,可以这样做……
{
"lock": "./lock.file"
}
……或者您可以完全禁用锁文件的使用
{
"lock": false
}
您的 IDE 应该能够自动建议这些新选项。
感谢 Geert-Jan Zwiers 和 @roj1512 贡献这些改进。
--allow-*
标志使用不当时的警告
Deno 允许您通过提供各种 --allow-*
标志来选择进入沙箱。执行 deno run
时,这些标志需要放在脚本名称**之前**,否则它们将作为 Deno.args
传递给脚本本身。
// example.js
console.log("cli args", Deno.args);
await Deno.writeTextFile("./hello.txt", "hello world");
在 Deno v1.29 之前,您会得到这样的输出:
$ deno run example.js --allow-write
cli args [ "--allow-write" ]
⚠️ ┌ Deno requests write access to "./hello.txt".
├ Requested by `Deno.writeFile()` API
├ Run again with --allow-write to bypass this prompt.
└ Allow? [y/n] (y = yes, allow; n = no, deny) >
我们发现将权限标志放在脚本名称之后是一个常见的错误,因此从这个版本开始,Deno 将警告您这可能不是故意的,并建议如何正确运行程序。
$ deno run example.js --allow-write
Permission flags have likely been incorrectly set after the script argument.
To grant permissions, set them before the script argument. For example:
deno run --allow-read=. main.js
cli args [ "--allow-write" ]
⚠️ ┌ Deno requests write access to "./hello.txt".
├ Requested by `Deno.writeFile()` API
├ Run again with --allow-write to bypass this prompt.
└ Allow? [y/n] (y = yes, allow; n = no, deny) >
感谢 Asher Gomez 贡献此改进。
在顶层 await 中显示未解决的 Promise
Deno 支持顶层 await,这允许您使用 await
关键字而无需将代码封装在异步 IIFE 中。这非常有用且符合人体工程学,但在某些情况下,被等待的 Promise 从未解析——通常是由于代码中的 bug。
// example.js
await new Promise((resolve) => {}); // this promise will never resolve!
console.log("hello world");
在这种情况下,会返回错误并终止程序执行。
deno run example.js
error: Module evaluation is still pending but there are no pending ops or dynamic imports. This situation is often caused by unresolved promises.
虽然错误提示了可能的问题,但需要用户通读代码并尝试找出问题所在。得益于新的 v8
API,现在会显示更友好的错误以及未解决 Promise 的堆栈跟踪。
$ deno run example.js
error: Top-level await promise never resolved
await new Promise((_resolve) => {});
^
at <anonymous> (file:///Users/ib/dev/deno/example.js:1:1)
node_modules
和 .git
文件夹
默认忽略 从这个版本开始,所有内置的 Deno 工具将默认跳过 node_modules/
和 .git/
目录。您可以运行格式化工具(deno fmt
)或代码检查工具(deno lint
),而无需担心它们会遍历这些大型目录。您仍然可以通过明确指定它们来选择搜索这些目录(例如 deno fmt node_modules/
)。
deno check --remote
标志重命名为 --all
deno run
和 deno check
命令之间存在差异,对于 deno run
,类型检查所有代码需要执行 deno run --check=all main.ts
,但对于 deno check
,则需要使用 --remote
标志——deno check --remote main.ts
。
现在,通过将 deno check
中的 --remote
重命名为 --all
(也包括 npm 包),此问题已得到解决。之前的命令仍将正常工作,但推荐使用新命令。
--inspect-wait
CLI 标志
新增 Deno 能够连接到 Chrome DevTools、VSCode 等调试器。您可以通过指定 --inspect
或 --inspect-brk
标志来做到这一点。前者将启用调试器并立即运行您的代码,而后者将启用调试器,等待它连接并在代码的第一行停止执行。
我们收到报告称,在某些情况下,需要等待调试器连接,但不一定在用户代码的第一行中断。我们很高兴地宣布,我们添加了一个新的 --inspect-wait
标志,它满足了这一要求——它将在运行用户代码之前等待调试器会话建立,但不会在代码的第一行设置断点。
与 --inspect
和 --inspect-brk
类似,--inspect-wait
标志允许您提供一个可选值,其中包含调试器连接的网络地址,例如:--inspect-wait=127.0.0.1:3000
。
VSCode 扩展中默认启用内联提示
我们在 Deno v1.27 中添加了对内联提示的支持。它们默认是关闭的,因为我们想确保一切正常。我们现在确信此功能按预期工作,并很高兴地宣布 Deno 的 VSCode 扩展最新版本现在默认使用内联提示(遵循默认的 VSCode 设置)。
如果您希望禁用它们,可以通过在 VSCode 用户设置中设置以下内容来完成:
{
"deno.inlayHints.parameterNames.enabled": "none",
"deno.inlayHints.parameterTypes.enabled": false,
"deno.inlayHints.variableTypes.enabled": false,
"deno.inlayHints.propertyDeclarationTypes.enabled": false,
"deno.inlayHints.functionLikeReturnTypes.enabled": false,
"deno.inlayHints.enumMemberValues.enabled": false
}
或者,您可以通过设置以下内容来禁用 VSCode 中的所有内联提示:
{
"editor.inlayHints.enabled": "off"
}
Deno
API 变更
稳定化
以下 API 已在此版本中稳定,不再需要使用 --unstable
标志:
Deno.TcpConn.setNoDelay()
Deno.TcpConn.setKeepAlive()
Deno.spawn
移除不稳定的 此版本中的一个重大变更是移除了不稳定的 Deno.spawn()
、Deno.spawnSync()
和 Deno.spawnChild()
API。所有这些都已被统一的 Deno.Command
API 取代。子进程 API 很难设计得既能满足常见任务的人体工程学,又能在需要对子进程进行低级控制时足够灵活。
让我们看看如何迁移您的代码的示例。
const { stdout, stderr } = await Deno.spawn("echo", { args: ["foo"] });
// becomes...
const { stdout, stderr } = await new Deno.Command("echo", { args: ["foo"] })
.output();
const { stdout, stderr } = Deno.spawnSync("echo", { args: ["foo"] });
// becomes...
const { stdout, stderr } = new Deno.Command("echo", { args: ["foo"] })
.outputSync();
const child = Deno.spawnChild("cat", { stdin: "piped" });
// becomes...
const child = new Deno.Command("cat", { stdin: "piped" }).spawn();
Deno.Command#.spawn()
对于 stdout 和 stderr 默认继承
不稳定的 Deno.Command
在调用 .spawn()
时,对于 stdout 和 stderr 默认是“inherit”。在调用 .output()
和 .outputSync()
时,它仍然默认为“piped”。
// outputs "1\n" to stdout and "2\n" to stderr
new Deno.Command("deno", {
args: ["eval", "console.log(1); console.error(2);"],
}).spawn();
Deno.writeFile
和 Deno.writeTextFile
的 createNew
选项
此版本为 Deno.writeFile
、Deno.writeTextFile
以及相应的同步 API 添加了 createNew
选项。
// ok
await Deno.writeTextFile("file.txt", "some text", { createNew: true });
// error: Deno.errors.AlreadyExists
await Deno.writeTextFile("file.txt", "some text", { createNew: true });
以前,您需要使用 Deno.open
API 才能获得此功能,这过于冗长。
TypeScript 4.9
Deno v1.29 附带最新稳定版本的 TypeScript。有关 TypeScript 新功能的更多信息,请参阅 TypeScript 4.9 博客文章。
标准模块变更
testing/types
模块
添加 在此版本中添加了 testing/types
模块。它是一个测试实用程序,用于检查复杂类型的行为。该模块的灵感来自 David Sherret 的 Conditional Type Checks 并从其移植。
import {
assertType,
IsExact,
IsNullable,
} from "https://deno.land/std@0.167.0/testing/types.ts";
const result = "some result" as string | number;
// compile error if the type of `result` is not exactly `string | number`
assertType<IsExact<typeof result, string | number>>(true);
// causes a compile error that `true` is not assignable to `false`
assertType<IsNullable<string>>(true); // error: string is not nullable
感谢 Lino Le Van 贡献此功能。
结构变更:趋向单一导出模式
在标准模块中,有些地方多个 API 实现于单个文件中。这类模块的例子有 bytes
、datetime
、io
等。另一方面,有些模块则采用每个导出项都实现于单个文件中的模式(我们在此称之为单一导出模式)。这类模块的例子有 collections
、fs
等。到目前为止,关于模块组织使用哪种模式没有明确的策略,但我们决定在适当的情况下转向后者(单一导出)模式。
在此版本中,以下 4 个模块:archive
、bytes
、io
和 stream
已重构为单一导出模式。因此,以下路径已被弃用,并将在后续版本中移除(具体移除版本请参阅 IDE 中的弃用消息)。如果您从这些路径导入,请更新您的导入路径。
std/streams/buffer.ts
std/streams/conversion.ts
std/streams/delimiter.ts
std/io/buffer.ts
(除了Buffer
类)std/io/files.ts
std/io/readers.ts
std/io/util.ts
std/io/writers.ts
std/archive/tar.ts
(Untar
导出已弃用。)
您可以在 主页的搜索对话框中找到更新的路径。
感谢 Asher Gomez 建议并贡献此更改。
API 重命名
此版本中重命名了以下 API。如果您在代码中直接使用这些 API,请更新它们。
std/dotenv/mod.ts
config
->load
configSync
->loadSync
- 注意:参数也已重命名。详情请参阅 类型定义。
std/fmt/bytes.ts
prettyBytes
->format
std/fmt/duration.ts
prettyDuration
->format
感谢 Tim Reichen 建议并贡献这些更改。
如果您想在未来的发布中帮助完成一些工作,请查看我们的招聘信息。