stream.Transform.from]的使用>

问题描述 投票:0回答:1

我想快速声明一个转换流,而无需任何其他库。通过stream.Transform.from将异步生成器转换为转换流看起来是一个不错的选择。

someReadable.pipe(
        stream.Transform.from(async function* (source, writable) {
          for await (const chunk of source) {
            yield JSON.stringify(chunk, null, 2) + "\n\n";
          }
        })
      )

为什么以上都不起作用?

TypeScript抛出:

Error:(8, 9) TS2345: Argument of type 'Readable' is not assignable to parameter of type 'WritableStream'.
  Type 'Readable' is missing the following properties from type 'WritableStream': writable, write, end
Error:(8, 31) TS2345: Argument of type '(source: any, writable: any) => AsyncGenerator<string, void, unknown>' is not assignable to parameter of type 'Iterable<any> | AsyncIterable<any>'.
  Property '[Symbol.asyncIterator]' is missing in type '(source: any, writable: any) => AsyncGenerator<string, void, unknown>' but required in type 'AsyncIterable<any>'.
Error:(14, 8) TS2339: Property 'pipe' does not exist on type 'WritableStream'.

我想快速声明一个转换流,而无需任何其他库。通过stream.Transform.from将异步生成器转换为转换流看起来是一个不错的选择。 someReadable.pipe(...

node.js stream node-streams
1个回答
0
投票

read函数实际上并非来自Transform类。 TransformDuplex的子类,它是ReadableWritable的子级。 .from模块中唯一的stream函数是Readable.from函数,因此您实际上是在调用它。

© www.soinside.com 2019 - 2024. All rights reserved.