UNPKG

646 BJavaScriptView Raw
1// @flow strict-local
2
3import {Transform} from 'stream';
4
5/*
6 * "Taps" into the contents of a flowing stream, yielding chunks to the passed
7 * callback. Continues to pass data chunks down the stream.
8 */
9export default class TapStream extends Transform {
10 _tap: Buffer => mixed;
11 constructor(tap: Buffer => mixed, options: mixed) {
12 super({...options});
13 this._tap = tap;
14 }
15
16 _transform(
17 chunk: Buffer | string,
18 encoding: string,
19 callback: (err: ?Error, chunk?: Buffer | string) => mixed
20 ) {
21 try {
22 this._tap(Buffer.from(chunk));
23 callback(null, chunk);
24 } catch (err) {
25 callback(err);
26 }
27 }
28}