Node.js v17.6.0 发布,允许从 HTTP 和 HTTPS URL 导入模块

Node.js v17.6.0 版本于 2022-02-23 发布,布允一个显著的入模特性是试验性支持从 HTTP 和 HTTPS 导入模块,这里面也包括很多问题,布允包括从安全方面考虑,入模目前在 Node.js 中使用还是布允有一些限制及一些其它的常规小错误修复。

允许从 HTTP 和 HTTPS URL 导入模块

Node.js v17.6.0 一个新的入模实验性功能是允许我们从 HTTP 或 HTTPS URL 导入 ES Module。这使得一些类似于 Web 浏览器导入的布允工作也可以在 Node.js 中完成,同时也消除了一些 Node.js 与 Deno 之间的入模差异,即 Deno 允许使用 HTTPS 导入包。布允因为一些安全性和稳定性的入模问题和浏览器相比还是有些差异的。

以下是布允一个导入 HTTP 资源的网站模板简单示例,该功能现在处于实验性状态,入模运行时需添加 标志。布允--experimental-network-imports

// hello.mjs

export default function hello(message) {

console.log(`Hello ${ message}`);

}

$ http-server

Starting up http-server,入模 serving ./

Available on:

http://127.0.0.1:8080

// index.mjs

import hello from http://127.0.0.1:8080/hello.mjs;

console.log(hello(codingMay)); // Hello codingMay

当前并非所有的 ES Modules 模块都可以加载,以下两个 Example,布允第一个尽管是加载的 HTTPS 资源,但不是 HTTP/1,实施例 2 导入了非网络依赖资源。

// Example1: 加载 HTTPS 资源

import hello from https://gitee.com/qufei1993/esmodule-https-import-example/blob/master/hello.mjs;

console.log(hello(codingMay));

输出错误:RangeError [ERR_UNKNOWN_MODULE_FORMAT]: Unknown module format: null for URL https://gitee.com/qufei1993/esmodule-https-import-example/blob/master/hello.mjs

// Example2: 加载其它非网络资源

// hello.mjs

import fsPromise from fs/promises;

export const readFile = filename => fsPromise.readFile(filename);

// index.mjs

import hello from http://127.0.0.1:8080/hello.mjs;

输出错误:TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file

HTTP 和 HTTPS 导入的一些限制:

仅支持 HTTP/1,不支持 HTTP2/HTTP3。HTTP 仅限于环回地址。身份验证不会发至服务器,例如 Authorization、Cookie 和 Proxy-Authorization 标头不会发送到服务。永远不会在目标服务器上检查 CORS。无法加载非网络依赖项。默认情况下不启用基于网络的亿华云计算加载,需要通过 标志打开加载 HTTP 或 HTTPS 资源。--experimental-network-importsProcess 获取活跃具柄和请求方法废弃通知

这个改变主要是在文档记录了 和 的弃用通知,以便支持更好的公共API。_getActiveHandles_getActiveRequests

这两个以下划线开头的 API 被代替的公共 API 方法 在 Node.js v17.3.0 所添加,该方法返回事件循环活动状态的资源类型。process.getActiveResourcesInfo()

import { getActiveResourcesInfo } from process;

import { createServer } from http;

console.log(Before:, getActiveResourcesInfo()); // Before: [ CloseReq, TTYWrap, TTYWrap, TTYWrap ]

setTimeout(() => {

console.log(After:, getActiveResourcesInfo()); // After: [ TTYWrap, TTYWrap, TTYWrap, TCPServerWrap, Timeout ]

}, 5000);

createServer((req, res) => res.end(OK)).listen(3000);其它的一些升级事项stream:恢复 map 规范合规性。build:移除损坏的 x32 arch 支持。fetch:当 fetch 启用时(),全局对象添加 FormData。--experimental-fetchfs:cp 和 cpSync 支持相对链接复制。流程:废弃多重解决。deps: 更新 npm 到 8.5.1。云南idc服务商
滇ICP备2023000592号-31