refactor(server-renderer): adjust streaming API

- add `pipeToNodeWritable`
- add `pipeToWebWritable`
This commit is contained in:
Evan You
2021-08-06 12:41:40 -04:00
parent 17cc4e15a7
commit 86d78d10e3
6 changed files with 174 additions and 48 deletions

View File

@@ -3,10 +3,19 @@
*/
import { createApp, h, defineAsyncComponent } from 'vue'
import { ReadableStream } from 'stream/web'
import { renderToWebStream } from '../src'
import { ReadableStream, TransformStream } from 'stream/web'
import { pipeToWebWritable, renderToWebStream } from '../src'
test('should work', async () => {
beforeEach(() => {
global.ReadableStream = ReadableStream
})
afterEach(() => {
// @ts-ignore
delete global.ReadableStream
})
test('renderToWebStream', async () => {
const Async = defineAsyncComponent(() =>
Promise.resolve({
render: () => h('div', 'async')
@@ -16,14 +25,42 @@ test('should work', async () => {
render: () => [h('div', 'parent'), h(Async)]
}
const stream = renderToWebStream(createApp(App), {}, ReadableStream)
const stream = renderToWebStream(createApp(App))
const reader = stream.getReader()
const decoder = new TextDecoder()
let res = ''
await reader.read().then(function read({ done, value }): any {
if (!done) {
res += value
res += decoder.decode(value)
return reader.read().then(read)
}
})
expect(res).toBe(`<!--[--><div>parent</div><div>async</div><!--]-->`)
})
test('pipeToWebWritable', async () => {
const Async = defineAsyncComponent(() =>
Promise.resolve({
render: () => h('div', 'async')
})
)
const App = {
render: () => [h('div', 'parent'), h(Async)]
}
const { readable, writable } = new TransformStream()
pipeToWebWritable(createApp(App), {}, writable)
const reader = readable.getReader()
const decoder = new TextDecoder()
let res = ''
await reader.read().then(function read({ done, value }): any {
if (!done) {
res += decoder.decode(value)
return reader.read().then(read)
}
})