stream.test.ts
This file
Path
src/helper/streaming/stream.test.ts
128 lines · 3.5 KiB · TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 1why, one step back1import { stream } from '.'chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
line 2why, one step back2import { Context } from '../../context'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 3–128why, one step back34describe('Basic Streaming Helper', () => {5 const req = new Request('http://localhost/')6 let c: Context7 beforeEach(() => {8 c = new Context(req)9 })1011 it('Check stream Response', async () => {12 const res = stream(c, async (stream) => {13 for (let i = 0; i < 3; i++) {14 await stream.write(new Uint8Array([i]))15 await stream.sleep(1)16 }17 })18 if (!res.body) {19 throw new Error('Body is null')20 }21 const reader = res.body.getReader()22 for (let i = 0; i < 3; i++) {23 const { value } = await reader.read()24 expect(value).toEqual(new Uint8Array([i]))25 }26 })2728 it('Check stream Response if aborted by client', async () => {29 let aborted = false30 const res = stream(c, async (stream) => {31 stream.onAbort(() => {32 aborted = true33 })34 for (let i = 0; i < 3; i++) {35 await stream.write(new Uint8Array([i]))36 await stream.sleep(1)37 }38 })39 if (!res.body) {40 throw new Error('Body is null')41 }42 const reader = res.body.getReader()43 const { value } = await reader.read()44 expect(value).toEqual(new Uint8Array([0]))45 reader.cancel()46 expect(aborted).toBeTruthy()47 })4849 it('Check stream Response if aborted by abort signal', async () => {50 // Emulate an old version of Bun (version 1.1.0) for this specific test case51 // @ts-expect-error Bun is not typed52 global.Bun = {53 version: '1.1.0',54 }55 const ac = new AbortController()56 const req = new Request('http://localhost/', { signal: ac.signal })57 const c = new Context(req)5859 let aborted = false60 const res = stream(c, async (stream) => {61 stream.onAbort(() => {62 aborted = true63 })64 for (let i = 0; i < 3; i++) {65 await stream.write(new Uint8Array([i]))66 await stream.sleep(1)67 }68 })69 if (!res.body) {70 throw new Error('Body is null')71 }72 const reader = res.body.getReader()73 const { value } = await reader.read()74 expect(value).toEqual(new Uint8Array([0]))75 ac.abort()76 expect(aborted).toBeTruthy()77 // @ts-expect-error Bun is not typed78 delete global.Bun79 })8081 it('Check stream Response if pipe is aborted by abort signal', async () => {82 // Emulate an old version of Bun (version 1.1.0) for this specific test case83 // @ts-expect-error Bun is not typed84 global.Bun = {85 version: '1.1.0',86 }87 const ac = new AbortController()88 const req = new Request('http://localhost/', { signal: ac.signal })89 const c = new Context(req)9091 let aborted = false92 const res = stream(c, async (stream) => {93 stream.onAbort(() => {94 aborted = true95 })96 await stream.pipe(new ReadableStream())97 })98 if (!res.body) {99 throw new Error('Body is null')100 }101 const reader = res.body.getReader()102 const pReading = reader.read()103 ac.abort()104 await pReading105 expect(aborted).toBeTruthy()106 // @ts-expect-error Bun is not typed107 delete global.Bun108 })109110 it('Check stream Response if error occurred', async () => {111 const onError = vi.fn()112 const res = stream(113 c,114 async () => {115 throw new Error('error')116 },117 onError118 )119 if (!res.body) {120 throw new Error('Body is null')121 }122 const reader = res.body.getReader()123 const { value } = await reader.read()124 expect(value).toBeUndefined()125 expect(onError).toBeCalledTimes(1)126 expect(onError).toBeCalledWith(new Error('error'), expect.anything()) // 2nd argument is StreamingApi instance127 })128})