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

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

[[431063]]

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 * as readline from "node:readline/promises"
  3. import { stdin as input, stdout as output } from 'process'
  4. const rl = readline.createInterface({ input, output }); 
  5. const answer = await rl.question('“Nodejs技术栈” 的域名是什么:'); 
  6. console.log(`答案: ${answer}`); 
  7. rl.close(); 

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

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

错误堆栈增加 Node.js 版本

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

如果想忽略该信息,运行时在命令行指定 --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. import crypto from 'crypto'
  2. console.log(crypto.createHash('md4').update('123''utf8').digest('hex')) 

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

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

  1. $ node --openssl-legacy-provider test.mjs 
  2. c58cda49f00748a3bc0fcfa511d516cb 

V8 更新至 9.5

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

  1. const esCalendarNames = new Intl.DisplayNames(['zh'], { type: 'calendar' }); 
  2. console.log(esCalendarNames.of('roc'));  // 民国纪年 
  3. const enCalendarNames = new Intl.DisplayNames(['en'], { type: 'calendar' }); 
  4. console.log(enCalendarNames.of('roc'));  // Minguo Calendar 

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

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

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

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

  1. console.log(new Intl.DateTimeFormat('zh').format(new Date())); // 2021/01/01 
  2. console.log(new Intl.DateTimeFormat('zh', { timeZoneName: 'shortGeneric' }).format(new Date())); // 2021/01/01 中国时间 
  3. console.log(new Intl.DateTimeFormat('zh', { timeZoneName: 'longGeneric' }).format(new Date())); // 2021/01/01 中国标准时间 
  4. console.log(new Intl.DateTimeFormat('zh', { timeZoneName: 'shortOffset' }).format(new Date())); // 2021/01/01 GMT+8 
  5. console.log(new Intl.DateTimeFormat('zh', { timeZoneName: 'longOffset' }).format(new Date())); // 2021/01/01 GMT+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/

 

责任编辑:武晓燕 来源: Nodejs技术栈
相关推荐

2021-12-25 22:29:57

Node.js 微任务处理事件循环

2021-02-01 08:16:14

ChromeNode.js

2023-06-30 08:05:41

2023-10-18 10:10:29

Node.js 21前端

2021-08-19 09:37:06

Go 1.17语言架构

2010-04-01 09:03:31

RHEL 5.5

2020-02-20 16:30:22

iOS 13.4苹果iPhone

2013-11-01 09:34:56

Node.js技术

2015-03-10 10:59:18

Node.js开发指南基础介绍

2022-06-07 16:21:17

Node.jsNode

2021-04-22 08:03:32

Node.jsJavaScriptv8

2023-11-16 14:56:13

2011-09-08 13:46:14

node.js

2011-09-02 14:47:48

Node

2011-09-09 14:23:13

Node.js

2011-11-01 10:30:36

Node.js

2012-10-24 14:56:30

IBMdw

2011-11-10 08:55:00

Node.js

2017-06-02 10:00:26

前端Node.js

2022-02-12 20:33:29

Node.jsStreamfetch API
点赞
收藏

51CTO技术栈公众号