|
- import { Transform } from 'stream'
- import { StreamOptions } from './common'
-
- export default class ConverterStream extends Transform {
- private buffer: string
-
- constructor(
- private readonly converter: (value: string | number | bigint) => string,
- readonly options: StreamOptions,
- ) {
- super()
- this.buffer = ''
- }
-
- _transform(chunk: Buffer, _encoding: BufferEncoding, callback: Function) {
- let chunkStr = chunk.toString(this.options.encoding)
- this.buffer += chunkStr
- callback()
- }
-
- _flush(callback: Function) {
- this.push(this.converter(this.buffer.trim()), this.options.encoding)
- callback()
- }
- }
|