|
| 1 | +'use strict' |
| 2 | +let __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { 'default': mod } |
| 4 | +} |
| 5 | + |
| 6 | +Object.defineProperty(exports, '__esModule', { value: true }) |
| 7 | +exports.FilterTaggedContent = void 0 |
| 8 | + |
| 9 | +const stream_1 = require('stream') |
| 10 | +const string_decoder_1 = require('string_decoder') |
| 11 | +const LineDecoder_1 = require('./LineDecoder') |
| 12 | +const debug_1 = __importDefault(require('debug')) |
| 13 | +const writeWithBackpressure_1 = require('./writeWithBackpressure') |
| 14 | +const debug = (0, debug_1.default)('cypress:stderr-filtering:FilterTaggedContent') |
| 15 | + |
| 16 | +/** |
| 17 | + * Filters content based on start and end tags, supporting multi-line tagged content. |
| 18 | + * |
| 19 | + * This transform stream processes incoming data and routes content between two output streams |
| 20 | + * based on tag detection. Content between start and end tags is sent to the filtered stream, |
| 21 | + * while content outside tags is sent to the main output stream. The class handles cases where |
| 22 | + * tags span multiple lines by maintaining state across line boundaries. |
| 23 | + * |
| 24 | + * Example usage: |
| 25 | + * ```typescript |
| 26 | + * const filter = new FilterTaggedContent('<secret>', '</secret>', filteredStream) |
| 27 | + * inputStream.pipe(filter).pipe(outputStream) |
| 28 | + * ``` |
| 29 | + */ |
| 30 | +class FilterTaggedContent extends stream_1.Transform { |
| 31 | + /** |
| 32 | + * Creates a new FilterTaggedContent instance. |
| 33 | + * |
| 34 | + * @param startTag The string that marks the beginning of content to filter |
| 35 | + * @param endTag The string that marks the end of content to filter |
| 36 | + * @param filtered The writable stream for filtered content |
| 37 | + */ |
| 38 | + constructor (startTag, endTag, wasteStream) { |
| 39 | + super({ |
| 40 | + transform: (chunk, encoding, next) => this.transform(chunk, encoding, next), |
| 41 | + flush: (callback) => this.flush(callback), |
| 42 | + }) |
| 43 | + |
| 44 | + this.startTag = startTag |
| 45 | + this.endTag = endTag |
| 46 | + this.wasteStream = wasteStream |
| 47 | + this.inTaggedContent = false |
| 48 | + /** |
| 49 | + * Processes incoming chunks and routes content based on tag detection. |
| 50 | + * |
| 51 | + * @param chunk The buffer chunk to process |
| 52 | + * @param encoding The encoding of the chunk |
| 53 | + * @param next Callback to call when processing is complete |
| 54 | + */ |
| 55 | + this.transform = async (chunk, encoding, next) => { |
| 56 | + let _a; let _b; let _c |
| 57 | + |
| 58 | + try { |
| 59 | + this.ensureDecoders(encoding) |
| 60 | + const str = (_b = (_a = this.strDecoder) === null || _a === void 0 ? void 0 : _a.write(chunk)) !== null && _b !== void 0 ? _b : ''; |
| 61 | + |
| 62 | + (_c = this.lineDecoder) === null || _c === void 0 ? void 0 : _c.write(str) |
| 63 | + debug('processing str for tags: "%s"', str) |
| 64 | + for (const line of Array.from(this.lineDecoder || [])) { |
| 65 | + await this.processLine(line) |
| 66 | + } |
| 67 | + next() |
| 68 | + } catch (err) { |
| 69 | + next(err) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Flushes any remaining buffered content when the stream ends. |
| 75 | + * |
| 76 | + * @param callback Callback to call when flushing is complete |
| 77 | + */ |
| 78 | + this.flush = async (callback) => { |
| 79 | + let _a |
| 80 | + |
| 81 | + debug('flushing') |
| 82 | + this.ensureDecoders() |
| 83 | + try { |
| 84 | + for (const line of Array.from(((_a = this.lineDecoder) === null || _a === void 0 ? void 0 : _a.end()) || [])) { |
| 85 | + await this.processLine(line) |
| 86 | + } |
| 87 | + callback() |
| 88 | + } catch (err) { |
| 89 | + callback(err) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + ensureDecoders (encoding) { |
| 94 | + let _a |
| 95 | + const enc = (_a = (encoding === 'buffer' ? 'utf8' : encoding)) !== null && _a !== void 0 ? _a : 'utf8' |
| 96 | + |
| 97 | + if (!this.lineDecoder) { |
| 98 | + this.lineDecoder = new LineDecoder_1.LineDecoder() |
| 99 | + } |
| 100 | + |
| 101 | + if (!this.strDecoder) { |
| 102 | + this.strDecoder = new string_decoder_1.StringDecoder(enc) |
| 103 | + } |
| 104 | + } |
| 105 | + /** |
| 106 | + * Processes a single line and routes content based on tag positions. |
| 107 | + * |
| 108 | + * This method handles the complex logic of detecting start and end tags within a line, |
| 109 | + * maintaining state across lines, and routing content to the appropriate streams. |
| 110 | + * It supports cases where both tags appear on the same line, only one tag appears, |
| 111 | + * or no tags appear but the line is part of ongoing tagged content. |
| 112 | + * |
| 113 | + * @param line The line to process |
| 114 | + */ |
| 115 | + async processLine (line) { |
| 116 | + const startPos = line.indexOf(this.startTag) |
| 117 | + const endPos = line.lastIndexOf(this.endTag) |
| 118 | + |
| 119 | + if (startPos >= 0 && endPos >= 0) { |
| 120 | + // Both tags on same line |
| 121 | + if (startPos > 0) { |
| 122 | + await this.pass(line.slice(0, startPos)) |
| 123 | + } |
| 124 | + |
| 125 | + await this.writeToWasteStream(line.slice(startPos + this.startTag.length, endPos)) |
| 126 | + if (endPos + this.endTag.length < line.length) { |
| 127 | + await this.pass(line.slice(endPos + this.endTag.length)) |
| 128 | + } |
| 129 | + } else if (startPos >= 0) { |
| 130 | + // Start tag found |
| 131 | + if (startPos > 0) { |
| 132 | + await this.pass(line.slice(0, startPos)) |
| 133 | + } |
| 134 | + |
| 135 | + await this.writeToWasteStream(line.slice(startPos + this.startTag.length)) |
| 136 | + this.inTaggedContent = true |
| 137 | + } else if (endPos >= 0) { |
| 138 | + // End tag found |
| 139 | + await this.writeToWasteStream(line.slice(0, endPos)) |
| 140 | + if (endPos + this.endTag.length < line.length) { |
| 141 | + await this.pass(line.slice(endPos + this.endTag.length)) |
| 142 | + } |
| 143 | + |
| 144 | + this.inTaggedContent = false |
| 145 | + } else if (this.inTaggedContent) { |
| 146 | + // Currently in tagged content |
| 147 | + await this.writeToWasteStream(line) |
| 148 | + } else { |
| 149 | + // Not in tagged content |
| 150 | + await this.pass(line) |
| 151 | + } |
| 152 | + } |
| 153 | + async writeToWasteStream (line, encoding) { |
| 154 | + let _a |
| 155 | + |
| 156 | + debug('writing to waste stream: "%s"', line) |
| 157 | + await (0, writeWithBackpressure_1.writeWithBackpressure)(this.wasteStream, Buffer.from(line, (_a = (encoding === 'buffer' ? 'utf8' : encoding)) !== null && _a !== void 0 ? _a : 'utf8')) |
| 158 | + } |
| 159 | + async pass (line, encoding) { |
| 160 | + let _a |
| 161 | + |
| 162 | + debug('passing: "%s"', line) |
| 163 | + this.push(Buffer.from(line, (_a = (encoding === 'buffer' ? 'utf8' : encoding)) !== null && _a !== void 0 ? _a : 'utf8')) |
| 164 | + } |
| 165 | +} |
| 166 | +exports.FilterTaggedContent = FilterTaggedContent |
0 commit comments