我想快速声明一个转换流,而无需任何其他库。通过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(...
read
函数实际上并非来自Transform
类。 Transform
是Duplex
的子类,它是Readable
和Writable
的子级。 .from
模块中唯一的stream
函数是Readable.from
函数,因此您实际上是在调用它。