node-network-devtools icon indicating copy to clipboard operation
node-network-devtools copied to clipboard

[Feature] 如何兼容 Node 16

Open code-lixm opened this issue 3 months ago • 6 comments

Desc

我的使用场景是在 electron中有node 进程启动服务,但是我的electron需要兼容 window7/8.1,所以electron的版本需要低于V22.但是此项目 devtool的要求是 node 18+,于是我想升级electron 内核支持 node18+,需要升级到 electron v23,但是在 electron 升级到 23 版本后,不再兼容 win7/8.1 具体链接:Electron 23.0.0

To Resolve?

能不能降低 API 版本以兼容 node16 ...

Resolutions(Optional)

...

code-lixm avatar Sep 30 '25 03:09 code-lixm

哈喽,我会在今天晚上尝试兼容~当然如果可以,可以给一些错误截图

GrinZero avatar Sep 30 '25 03:09 GrinZero

感谢 ReferenceError: ReadableStream is not defined 第一个问题已解决,ReadableStream defined in node V18

function ensureWebStreamsAPI() {
  if (typeof global.ReadableStream === 'undefined') {
    const { ReadableStream, WritableStream, TransformStream } = require('node:stream/web')
    global.ReadableStream = ReadableStream
    global.WritableStream = WritableStream
    global.TransformStream = TransformStream
  }
}

也有可能是第一个问题的解决方式,导致出现了第二个问题 Unhandled RangeError: Invalid WebSocket frame: RSV1 must be clear 这个问题是 ws 的问题,猜测问题可能出现在 permessage-deflate 我的 electron 版本是 15.5.7,对应内核在 node V16.5

code-lixm avatar Sep 30 '25 04:09 code-lixm

脑测一下,试试 require('node:stream')

GrinZero avatar Sep 30 '25 06:09 GrinZero

脑测一下,试试 require('node:stream')

Unhandled TypeError [ERR_INVALID_ARG_TYPE]: The "list[0]" argument must be an instance of Buffer or Uint8Array. Received type string ('ok') 现在出现了这个报错,现在一步一个脚印,这个错误一般是因为什么

code-lixm avatar Sep 30 '25 07:09 code-lixm

看起来是这里出错了,还是跟 websocket 有关系 ,要看看你那里是尝试用 ws 连接哪里了;

Image

晚点我切到 16 测一下

GrinZero avatar Sep 30 '25 07:09 GrinZero

跟你同步一下进度。 我尝试了

function ensureNode16() {
  if (typeof global.ReadableStream === 'undefined') {
    const { ReadableStream, WritableStream, TransformStream } = require('node:stream/web')
    global.ReadableStream = ReadableStream
    global.WritableStream = WritableStream
    global.TransformStream = TransformStream
  }
}

出现的报错是:

Image

所以我简单 patch 了一下 ws:

    module.exports.isValidUTF8 = function (buf) {
      const curIsValidUTF8 = typeof isValidUTF8 === 'function' ? isValidUTF8 : isValidUTF8.Validation.isValidUTF8;
      return buf.length < 32 ? _isValidUTF8(buf) : curIsValidUTF8(buf);
    };

这样就可以运行了。 我这里是 v16.20.2

GrinZero avatar Oct 15 '25 09:10 GrinZero