summaryrefslogtreecommitdiffstats
path: root/test/utils/async-iterators.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/utils/async-iterators.js')
-rw-r--r--test/utils/async-iterators.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/test/utils/async-iterators.js b/test/utils/async-iterators.js
new file mode 100644
index 0000000..da7e0a8
--- /dev/null
+++ b/test/utils/async-iterators.js
@@ -0,0 +1,25 @@
+'use strict'
+
+async function * wrapWithAsyncIterable (asyncIterable, indefinite = false) {
+ for await (const chunk of asyncIterable) {
+ yield chunk
+ }
+ if (indefinite) {
+ await new Promise(() => {})
+ }
+}
+
+const STREAM = 'stream'
+const ASYNC_ITERATOR = 'async-iterator'
+function maybeWrapStream (stream, type) {
+ if (type === STREAM) {
+ return stream
+ }
+ if (type === ASYNC_ITERATOR) {
+ return wrapWithAsyncIterable(stream)
+ }
+
+ throw new Error(`bad input ${type} should be ${STREAM} or ${ASYNC_ITERATOR}`)
+}
+
+module.exports = { wrapWithAsyncIterable, maybeWrapStream, consts: { STREAM, ASYNC_ITERATOR } }