Node18 即将支持 import HTTP资源!

简介: 大家好,我是零一,最近看到Node官方提交了一条commit ,并且已经合入 master分支

由此可见,Node18可能会支持一个非常 nice 的功能,那就是 支持 import 远程HTTPS资源和本地的HTTP资源,啥意思?看个例子🌰


尝鲜


现在有这样一个文件:


// demo.mjs
import sayHelloWorld from "https://example/say-helloWorld.mjs";
console.log(sayHelloWorld());


可以看到,这里选择加载了一个远程的HTTPS文件资源,该文件内容如下:


// say-helloWorld.mjs
export default function sayHelloWorld () {
  return 'Hello,World! 零一'
}


接下来运行一下 demo.mjs看看会发生什么,因为在Node18中,该功能属于实验性功能,所以我们需要加上参数 --experimental-network-imports 来启动该特性:


node --experimental-network-imports demo.mjs


运行结果:


// Hello,World! 零一


当然了,本地起的HTTP服务上的文件资源也是可以一样导入的,例如:


import sayHelloWorld from "http://10.59.24.2:8080/say-helloWorld.mjs"   // ok


注意


我们使用该特性导入的脚本资源文件有一个前提条件,那就是该文件所在的服务器上响应此文件请求的类型 content_type 必须为 application/javascript,否则就无法加载


我们可以用 curl 来查看一下想要加载的文件响应类型,例如:


$ curl -s -o /dev/null -w '%{content_type}' 'https://example/say-helloWorld.mjs'
# application/javascript


错误场景


  • ERR_NETWORK_IMPORT_BAD_RESPONSE:当导入的资源不存在时,即响应 404,就会报该错误


Error [ERR_NETWORK_IMPORT_BAD_RESPONSE]: import 'https://xxxx/xxxx' received a bad response: HTTP response returned status code of 404


  • ERR_NETWORK_IMPORT_DISALLOWED:当请求一个HTTPS资源且被重定向到一个非网络资源时,是不被允许的


Error [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'ftp://xxxxx/say-helloWorld.mjs' by 'https://h3manth.com/foo.mjs' is not support: cannot redirect to non-network location


  • ERR_UNKNOWN_MODULE_FORMAT:当请求的资源不是 ESM 时,会报该错误


RangeError [ERR_UNKNOWN_MODULE_FORMAT]: Unknown module format: null for URL https://xxxxxx/say-helloWorld.js


  • ERR_UNSUPPORTED_ESM_URL_SCHEME:当加载的资源URL的协议不被支持时,会报该错误,例如 ftp:


Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data, https, http are supported by the default ESM loader. Received protocol 'ftp:'


实现原理


其实原理也比较简单:


先调用 fetchModule 方法检查资源模块是否缓存过(cacheForGET


function fetchModule(parsed, { parentURL }) {
  const { href } = parsed;
  const existing = cacheForGET.get(href);
  if (existing) {
    return existing;
  }
  if (parsed.protocol === 'http:') {
    return PromisePrototypeThen(isLocalAddress(parsed.hostname), (is) => {
      // Makes few checks for ERR_NETWORK_IMPORT_DISALLOWED
      return fetchWithRedirects(parsed);
    });
  }
  return fetchWithRedirects(parsed);
}


若没有缓存,则调用 fetchWithRedirects


function fetchWithRedirects(parsed) {
  const existing = cacheForGET.get(parsed.href);
  if (existing) {
    return existing;
  }
  const handler = parsed.protocol === 'http:' ? HTTPGet : HTTPSGet;
  const result = new Promise((fulfill, reject) => {
    const req = handler(parsed, {
      headers: {
        Accept: '*/*'
      }
    })
    .on('error', reject)
    .on('response', (res) => {
      // 错误检查
      // 缓存内容
      // 返回模块内容
    }
}


然后根据资源类型去调用不同的方法(HTTPGetHTTPSGet


let HTTPSAgent;
function HTTPSGet(url, opts) {
  const https = require('https'); // For HTTPGet, we use the `http` builtin
  HTTPSAgent ??= new https.Agent({
    keepAlive: true
  });
  return https.get(url, {
    agent: HTTPSAgent,
    ...opts
  });
}


总结


这个功能还是很香的,谁用谁知道,但目前有一些缺点:


  1. 实验性功能,可能还会改动,而且使用需要加参数 --experimental-network-imports


  1. 本地只支持 http,一旦涉及到 https 就很淡疼


  1. 该功能的代码实现就限制死了只支持 http:https:,其它一概不支持


希望本文对你们有所帮助~


我是零一,分享技术,不止前端,我们下期见~

相关文章
|
3月前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
37 0
|
3月前
|
JavaScript 前端开发
基于 Node.js 环境,使用内置 http 模块,创建 Web 服务程序
基于 Node.js 环境,使用内置 http 模块,创建 Web 服务程序
|
2月前
|
JSON JavaScript 中间件
【Node.js】从基础到精通(三)—— HTTP 模块探索
【Node.js】从基础到精通(三)—— HTTP 模块探索
24 0
|
3月前
|
JSON JavaScript 中间件
使用 Node.js 开发一个简单的 web 服务器响应 HTTP get 请求
使用 Node.js 开发一个简单的 web 服务器响应 HTTP get 请求
|
3月前
|
JSON JavaScript API
使用 Node.js 开发一个简单的 web 服务器响应 HTTP post 请求
使用 Node.js 开发一个简单的 web 服务器响应 HTTP post 请求
|
3月前
|
网络协议 应用服务中间件 nginx
nginx 302 301 设置 url 转跳 nginx 资源重定向 nginx tcp 和 http 转发
nginx 代理后端网站,和 网站资源目录重定向到其他连接地址
164 3
|
3月前
|
存储 JSON JavaScript
Node.js 上开发一个 HTTP 服务器,监听某个端口,接收 HTTP POST 请求并处理传入的数据
Node.js 上开发一个 HTTP 服务器,监听某个端口,接收 HTTP POST 请求并处理传入的数据
|
3月前
|
安全 Go
解决https页面加载http资源报错
请注意,混合内容可能导致安全性问题,因此在使用上述方法时要小心。最好的方式是尽量减少或完全消除混合内容,以确保页面的安全性。
269 0
|
3月前
|
JavaScript
GET http://192.168.2.198:8080/sockjs-node/info?t=1626862752216 net::ERR_CONNECTION_TIMED_OUT
GET http://192.168.2.198:8080/sockjs-node/info?t=1626862752216 net::ERR_CONNECTION_TIMED_OUT
51 0
|
3月前
|
前端开发 应用服务中间件 nginx
使用nginx-http-concat资源请求合并功能 优化网站响应
使用nginx-http-concat资源请求合并功能 优化网站响应
37 0