summaryrefslogtreecommitdiffstats
path: root/test/utils/async-iterators.js
blob: da7e0a8a33a3091b9430219b539a29ef4d606cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 } }