V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Belmode
V2EX  ›  Node.js

请教大家一个在 hono.js 中使用 ts 的类型兼容性问题

  •  
  •   Belmode · 9 天前 · 1065 次点击

    我现在想在 node.js 平台上使用 hono.js ,现在需要实现一个文件下载需求

    
        //几个类型导入:
        import { stream } from "hono/streaming"
        import { Readable, Writable } from "node:stream"
        import { ReadableStream } from "node:stream/web"
    
    	// ......
        const fileStream = createReadStream(filePath)
        // 将文件流作为响应返回给客户端
        return stream(
          c,
          async (stream) => {
            // Write a process to be executed when aborted.
            stream.onAbort(() => {
              console.log('Aborted!')
            })
            // Write a Uint8Array.
            await stream.write(
              new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f])
            )
            // Pipe a readable stream.
            // 这里出现了类型不兼容
            // await stream.pipe(Readable.toWeb(fileStream))
            await stream.pipe(ReadableStream.from(fileStream))
          },
          async (err, stream) => {
            // stream.writeln('An error occurred!')
            console.error('An error occurred!', err)
          }
        )
    

    ts 提示: 类型“import("stream/web").ReadableStream<any>”的参数不能赋给类型“ReadableStream<any>”的参数. 属性“pipeThrough”的类型不兼容。

    我该如何解决,或者说如何在 nodejs 环境使用...

    谢谢大家了!🙇‍😘

    10 条回复    2025-02-11 23:19:22 +08:00
    fov6363
        1
    fov6363  
       9 天前
    加个 any 先跑起来
    fov6363
        2
    fov6363  
       9 天前   ❤️ 1
    await stream.pipe((ReadableStream as any).from(fileStream))
    Belmode
        3
    Belmode  
    OP
       9 天前
    @fov6363 #2 多谢了,这样做确实可以。

    但我心中还是有疑问,作为 ts 菜鸟。我是哪里配置的有问题呢,还是说 WEB API 和 Nodejs 的 API 不兼容导致的。
    fov6363
        4
    fov6363  
       9 天前
    @Belmode 看代码是与 hono/streaming 的 stream 类型冲突了,检查一下 hono/streaming 的 typescript 与你 package.json 中的 typescript 版本是否一致
    Belmode
        5
    Belmode  
    OP
       9 天前
    @fov6363 #4 typescript 更新成一样的了,还是不行。这段流的类型都是指向了 nodejs 内部的类型。
    fov6363
        6
    fov6363  
       9 天前
    @Belmode 看起来 web 与 node 的类型不兼容,如果功能没问题的话,可以先用 any
    Aolose
        7
    Aolose  
       9 天前
    说 any 的别误导人啊,不兼容就是不兼容
    web 的 stream 和 node 的 stream 接口都不一样 你 as any 还要什么 ts ?
    我没有看过 hono 的 stream 不过从这个 pipe 看应该是实现的 node stream 的设计
    你转 httpresponse body 的 stream 可以使构建一个 TransformStream 或者 readablestream
    jiangzhexin
        8
    jiangzhexin  
       9 天前
    hono 的 ReadableStream 就是 Web Standards ,我一般建议是尽量避免使用 node 私有的 API ,毕竟像 hono 这种框架主要卖点就是 Web Standards

    特别是 Readable.toWeb() 的结果也是 node 内部的 ReadableStream 类型

    你可以想办法把 fileStream 转化为标准的 ReadableStream ,或者创建这个 fileStream 就避免创建出 node 私有的 ReadableStream
    Aolose
        9
    Aolose  
       9 天前
    看了下 hono 的 stream 设计 这块和 web 的 stream 没什么关系
    stream 方法已经做了包装
    回调函数中的 stream 你直接把流往里头写入就好 这其中不需要什么转换
    Aolose
        10
    Aolose  
       9 天前
    @Aolose
    偷懒失败 fsStream.on('data', a=>stream.write(a))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4618 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 09:50 · PVG 17:50 · LAX 01:50 · JFK 04:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.