UNPKG

914 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.transformTap = void 0;
4const stream_1 = require("stream");
5/**
6 * Similar to RxJS `tap` - allows to run a function for each stream item, without affecting the result.
7 * Item is passed through to the output.
8 *
9 * Can also act as a counter, since `index` is passed to `fn`
10 */
11function transformTap(fn, opt = {}) {
12 const { logger = console } = opt;
13 let index = -1;
14 return new stream_1.Transform({
15 objectMode: true,
16 ...opt,
17 async transform(chunk, _, cb) {
18 // console.log('tap', chunk)
19 try {
20 await fn(chunk, ++index);
21 }
22 catch (err) {
23 logger.error(err);
24 // suppressed error
25 }
26 cb(null, chunk); // pass through the item
27 },
28 });
29}
30exports.transformTap = transformTap;