UNPKG

674 BPlain TextView Raw
1import { Transform } from 'stream'
2import { TransformTyped } from '../stream.model'
3
4/**
5 * Transforms objectMode=false Buffers/strings into objectMode=true strings.
6 *
7 * Useful in this _pipeline:
8 * fs.createReadStream(inputPath),
9 * createUnzip(), // binary
10 * transformSplit(), // string chunks, but objectMode==false
11 * transformToString(), // string chunks, but objectMode==true
12 */
13export function transformToString(): TransformTyped<Buffer, string> {
14 return new Transform({
15 objectMode: false,
16 readableObjectMode: true,
17 transform(chunk: Buffer, _, cb) {
18 // console.log(`enc: ${_}`, chunk.toString())
19 cb(null, chunk.toString())
20 },
21 })
22}