(stream: Readable, waitForPattern?: string | RegExp)
| 194 | * Will resolve early if |
| 195 | */ |
| 196 | export function getStream(stream: Readable, waitForPattern?: string | RegExp) { |
| 197 | let resolve: (value: string) => void; |
| 198 | const promise = new Promise<string>((res) => { |
| 199 | resolve = res; |
| 200 | }); |
| 201 | const received: Buffer[] = []; |
| 202 | let combinedBuffer: Buffer = Buffer.concat([]); |
| 203 | let combinedString: string = ''; |
| 204 | |
| 205 | stream.on('data', (data) => { |
| 206 | received.push(data); |
| 207 | combine(); |
| 208 | if ( |
| 209 | (typeof waitForPattern === 'string' && |
| 210 | combinedString.indexOf(waitForPattern) >= 0) || |
| 211 | (waitForPattern instanceof RegExp && combinedString.match(waitForPattern)) |
| 212 | ) |
| 213 | resolve(combinedString); |
| 214 | combinedBuffer = Buffer.concat(received); |
| 215 | }); |
| 216 | stream.on('end', () => { |
| 217 | resolve(combinedString); |
| 218 | }); |
| 219 | |
| 220 | return promise; |
| 221 | |
| 222 | function combine() { |
| 223 | combinedBuffer = Buffer.concat(received); |
| 224 | combinedString = combinedBuffer.toString('utf8'); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | //#region Reset node environment |
| 229 |
no test coverage detected
searching dependent graphs…