mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
Co-authored-by: Frederik Rabøl <[email protected]> Co-authored-by: Christoph Fricke <[email protected]> Co-authored-by: Piotr <[email protected]> Co-authored-by: Kristján Oddsson <[email protected]> Co-authored-by: thepassle <[email protected]> Co-authored-by: Kevin Østerkilde <[email protected]> Co-authored-by: Laryssa Rocha <[email protected]> Co-authored-by: Matthew Costabile <[email protected]>
30 lines
683 B
TypeScript
30 lines
683 B
TypeScript
import { http, HttpResponse, delay } from 'msw'
|
|
import { setupWorker } from 'msw/browser'
|
|
|
|
const encoder = new TextEncoder()
|
|
const chunks = ['hello', 'streaming', 'world']
|
|
|
|
const worker = setupWorker(
|
|
http.get('/stream', () => {
|
|
const stream = new ReadableStream({
|
|
async start(controller) {
|
|
for (const chunk of chunks) {
|
|
controller.enqueue(encoder.encode(chunk))
|
|
await delay(250)
|
|
}
|
|
|
|
controller.close()
|
|
},
|
|
})
|
|
|
|
return new HttpResponse(stream, {
|
|
headers: {
|
|
'Content-Type': 'application/octet-stream',
|
|
'Content-Length': chunks.join('').length.toString(),
|
|
},
|
|
})
|
|
}),
|
|
)
|
|
|
|
worker.start()
|