跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Node.js] 关于弱网环境下 axios 流式传输的一些问题想请教下大家

发表于

背景:在 node 弱网环境下 axios 流式传输,网络带宽接近 0KB 的时候网络流(可读流)会触发 aborted ,从而使可写流触发 finish ,导致几十 MB 的文件只有几十 KB 。

版本:node10 、axios0.19.0

问题:

  1. 关于 aborted 事件,看了 axios 的源码和 stream 源码,里面好像都没有定义 aborted 事件的定义,不知道它是哪来的(相关代码是我参考 gpt 写上的)
  2. 服务端设置的 timeout 为 5 秒,客户端这边未设置 timeout ,因为 axios 中的 timeout 仅适用于响应超时,而不适用于连接超时,所以应该和 timeout 是无关的。我的理解是客户端收到响应成功后,会建立一个关于 node 流的网络连接,这个流会在内存中进行一些系统级的操作,但是这里的流在文件没下载完就关闭了,不知道是为什么,看来还是因为 aborted 事件,这就又回到了第一个问题上

代码及打印:(删除相关敏感信息)

const writer = fsx.createWriteStream(file)

axios
  .get(url, {
    responseType: 'stream',
    ...options
  })
  .then((res) => {
    res.data.pipe(writer)
    writer.on('error', (err) => {
      utils.log(`download error`, err)
      reject(err)
    })
    writer.on('finish', (res) => {
      utils.log(`download finish`, res)
      resolve(res)
    })
    writer.on('close', (err) => {
      utils.log(`download close`, err)
    })
    res.data.on('error', (err) => {
      utils.log(`download error2`, err)
    })
    res.data.on('end', (err) => {
      utils.log(`download end`, err)
    })
    res.data.on('aborted', (err) => {
      utils.log(`download aborted`, err)
    })
    res.data.on('close', () => {
      if (res.request.aborted) {
        utils.log('download aborted')
      } else {
        utils.log('download completed')
      }
    })
  })
[info] download aborted undefined
[info] download end undefined
[info] download completed
[info] download finish undefined
[info] download close undefined

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.