next.js/test/e2e/cancel-request/readable.ts
readable.ts32 lines658 B
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
}
Quest for Codev2.0.0
/
SIGN IN