MCPcopy
hub / github.com/electerm/electerm / waitForText

Function waitForText

test/unit-ci/session-transports.spec.js:159–191  ·  view source on GitHub ↗
(emitter, matcher, timeout = 5000)

Source from the content-addressed store, hash-verified

157}
158
159function waitForText (emitter, matcher, timeout = 5000) {
160 return new Promise((resolve, reject) => {
161 let output = ''
162 const timer = setTimeout(() => {
163 cleanup()
164 reject(new Error(`Timed out waiting for text. Received: ${output}`))
165 }, timeout)
166
167 const onData = (chunk) => {
168 output += chunk.toString()
169 if (matcher(output, chunk)) {
170 cleanup()
171 resolve(output)
172 }
173 }
174
175 const onClose = () => {
176 cleanup()
177 reject(new Error(`Stream closed before matcher succeeded. Received: ${output}`))
178 }
179
180 const cleanup = () => {
181 clearTimeout(timer)
182 emitter.off('data', onData)
183 emitter.off('close', onClose)
184 emitter.off('end', onClose)
185 }
186
187 emitter.on('data', onData)
188 emitter.on('close', onClose)
189 emitter.on('end', onClose)
190 })
191}
192
193describe('session-ftp transport flows', () => {
194 let ftpServer

Callers 1

Calls 2

cleanupFunction · 0.70
onMethod · 0.45

Tested by

no test coverage detected