gpt4 book ai didi

Node.js v17 来了,看看都有哪些新功能?

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Node.js v17 来了,看看都有哪些新功能?由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

Node.js v17 来了,看看都有哪些新功能?

Node.js v17 版本已发布,取代了 v16 做为当前版本,新的 v17 版本提供了一些新功能:基于 Promise 的其它核心模块 API、错误堆栈尾部增加 Node.js 版本信息、OpenSSL 3.0 支持、v8 JavaScript 引擎更新至 9.5.

基于 Promise 的 API

Node.js 项目的一项持续性战略计划是为 Node.js 核心模块提供基于 Promise 的 API 支持,近年来已为 timer、stream 模块提供了 Promise API 支持.

Node.js v17 版本为 readline 模块提供了基于 Promise 的 API 支持。该模块提供了一个接口用于从一个可读流对象逐行读取数据.

结合 process.stdin 可读取用户在终端输入的数据。如下例所示:

  1. //test.mjs
  2. import*asreadlinefrom"node:readline/promises";
  3. import{stdinasinput,stdoutasoutput}from'process';
  4. constrl=readline.createInterface({input,output});
  5. constanswer=awaitrl.question('“Nodejs技术栈”的域名是什么:');
  6. console.log(`答案:${answer}`);
  7. rl.close();

运行之后,效果如下所示:

Node.js v17 来了,看看都有哪些新功能?

readline 模块的更多信息参考 readline_readline.

错误堆栈增加 Node.js 版本

堆栈跟踪是诊断应用程序错误信息的重要组成部分,在 Node.js v17 版本中,如果因为一些致命的错误导致进程退出,在错误堆栈的尾部将包含 Node.js 的版本信息.

Node.js v17 来了,看看都有哪些新功能?

如果想忽略该信息,运行时在命令行指定 --no-extra-info-on-fatal-exception 标志.

OpenSSL 3.0 支持

Node.js v17 版本包含了近期发布的 OpenSSL 3.0,根据 OpenSSL 的发布策略,OpenSSL 1.1.1 将在 2023-09-11 结束支持,这个日期也在 Node.js v18 LTS 结束日期之前.

因为 OpenSSL 3.0 对允许的算法和密钥大小增加了严格的限制,预计会对生态系统造成一些影响,在 Node.js v17 版本包含 OpenSSL 3.0 以便在下一个 LTS 版本之前为用户的测试和反馈留出时间.

例如,md4 这是 OpenSSL 3.0 默认不再允许的一个算法,如果是在 Node.js 17 之前的 Node 版本中,应用程序是可以正常运行的,但在 Node.js v17 中将抛出一个 error code 为 ERR_OSSL_EVP_UNSUPPORTED 的错误信息.

  1. importcryptofrom'crypto';
  2. console.log(crypto.createHash('md4').update('123','utf8').digest('hex'))

Node.js v17 版本下运行之后得到如下错误信息.

Node.js v17 来了,看看都有哪些新功能?

一个临时的解决方法是运行时增加 --openssl-legacy-provider 标志,应用程序不在报错.

  1. $node--openssl-legacy-providertest.mjs
  2. c58cda49f00748a3bc0fcfa511d516cb

V8 更新至 9.5

v8 在 8.1 版本开启了 Intl.DisplayNames API,支持语言、区域、货币、脚本四种类型,现在添加了两种新的类型:calendar、dateTimeField,分别返回不同的日历类型和日期时间字段的显示名称。对于国际化应用很有帮助.

  1. constesCalendarNames=newIntl.DisplayNames(['zh'],{type:'calendar'});
  2. console.log(esCalendarNames.of('roc'));//民国纪年
  3. constenCalendarNames=newIntl.DisplayNames(['en'],{type:'calendar'});
  4. console.log(enCalendarNames.of('roc'));//MinguoCalendar

日期时间字段国际化名称展示.

  1. functionprintDate(dateTimeField){
  2. console.log(
  3. `${dateTimeField.of('year')}${dateTimeField.of('month')}${dateTimeField.of('day')}`
  4. );
  5. }
  6. printDate(newIntl.DisplayNames(['zh'],{type:'dateTimeField'}))//年月日
  7. printDate(newIntl.DisplayNames(['en'],{type:'dateTimeField'}))//yearmonthday
  8. printDate(newIntl.DisplayNames(['KOR'],{type:'dateTimeField'}))//년월일
  9. printDate(newIntl.DisplayNames(['THA'],{type:'dateTimeField'}))//ปีเดือนวัน

Intl.DateTimeFormat API 在 v8 9.5 版本中为 timeZoneName 选项新增加了四个值:shortGeneric、longGeneric、shortOffset、longOffset.

通过以下代码示例可看到之间的区别.

  1. console.log(newIntl.DateTimeFormat('zh').format(newDate()));//2021/01/01
  2. console.log(newIntl.DateTimeFormat('zh',{timeZoneName:'shortGeneric'}).format(newDate()));//2021/01/01中国时间
  3. console.log(newIntl.DateTimeFormat('zh',{timeZoneName:'longGeneric'}).format(newDate()));//2021/01/01中国标准时间
  4. console.log(newIntl.DateTimeFormat('zh',{timeZoneName:'shortOffset'}).format(newDate()));//2021/01/01GMT+8
  5. console.log(newIntl.DateTimeFormat('zh',{timeZoneName:'longOffset'}).format(newDate()));//2021/01/01GMT+08:00

参见 v8 9.5 release 文档 阅读更多信息.

其它信息

按照 Node.js 发布时间表,Node.js v12 将于 2022 年 4 月结束生命周期。Node.js v16 在 2021 年 10 月 26 升级为 LTS,即长期支持版本.

Node.js 的奇数版本不是稳定的版本(例如,当前的 Node.js v17 ),它的生命周期很短,不要用于生产环境.

对 Node.js 版本信息不了的、不知道如何安装 Node.js 的参考文章 “Node.js 版本知多少?又该如何选择?”.

Reference 。

https://medium.com/the-node-js-collection/node-js-17-is-here-8dba1e14e382 。

https://nodejs.org/en/blog/release/v17.0.0/ 。

原文链接:https://mp.weixin.qq.com/s/JliFSL-118HOrlliBkJiiQ 。

最后此篇关于Node.js v17 来了,看看都有哪些新功能?的文章就讲到这里了,如果你想了解更多关于Node.js v17 来了,看看都有哪些新功能?的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com