File size: 658 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
import * as stream from 'stream'
import { Deferred, sleep } from './sleep'

export function Readable(write: number) {
  const encoder = new TextEncoder()
  const cleanedUp = new Deferred()
  const aborted = new Deferred()
  let i = 0

  const readable = {
    finished: Promise.all([cleanedUp.promise, aborted.promise]).then(() => i),

    abort() {
      aborted.resolve()
    },
    stream: new stream.Readable({
      async read() {
        if (i >= write) {
          return
        }

        await sleep(100)
        this.push(encoder.encode(String(i++)))
      },
      destroy() {
        cleanedUp.resolve()
      },
    }),
  }
  return readable
}