refactor(server-renderer): adjust streaming API
- add `pipeToNodeWritable` - add `pipeToWebWritable`
This commit is contained in:
parent
17cc4e15a7
commit
86d78d10e3
3
packages/global.d.ts
vendored
3
packages/global.d.ts
vendored
@ -36,5 +36,6 @@ declare module 'file-saver' {
|
|||||||
|
|
||||||
declare module 'stream/web' {
|
declare module 'stream/web' {
|
||||||
const r: typeof ReadableStream
|
const r: typeof ReadableStream
|
||||||
export { r as ReadableStream }
|
const t: typeof TransformStream
|
||||||
|
export { r as ReadableStream, t as TransformStream }
|
||||||
}
|
}
|
||||||
|
@ -50,10 +50,7 @@ Renders input as a [Node.js Readable stream](https://nodejs.org/api/stream.html#
|
|||||||
**Signature**
|
**Signature**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
function renderToNodeStream(
|
function renderToNodeStream(input: App | VNode, context?: SSRContext): Readable
|
||||||
input: App | VNode,
|
|
||||||
context?: SSRContext
|
|
||||||
): Readable
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Usage**
|
**Usage**
|
||||||
@ -63,12 +60,27 @@ function renderToNodeStream(
|
|||||||
renderToNodeStream(app).pipe(res)
|
renderToNodeStream(app).pipe(res)
|
||||||
```
|
```
|
||||||
|
|
||||||
In the ESM build of `@vue/server-renderer`, which is decoupled from Node.js environments, the `Readable` constructor must be explicitly passed in as the 3rd argument:
|
**Note:** This method is not supported in the ESM build of `@vue/server-renderer`, which is decoupled from Node.js environments. Use `pipeToNodeWritable` instead.
|
||||||
|
|
||||||
|
### `pipeToNodeWritable`
|
||||||
|
|
||||||
|
Render and pipe to an existing [Node.js Writable stream](https://nodejs.org/api/stream.html#stream_writable_streams) instance.
|
||||||
|
|
||||||
|
**Signature**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
function pipeToNodeWritable(
|
||||||
|
input: App | VNode,
|
||||||
|
context: SSRContext = {},
|
||||||
|
writable: Writable
|
||||||
|
): void
|
||||||
|
```
|
||||||
|
|
||||||
|
**Usage**
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { Readable } from 'stream'
|
// inside a Node.js http handler
|
||||||
|
pipeToNodeWritable(app, {}, res)
|
||||||
renderToNodeStream(app, {}, Readable).pipe(res)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### `renderToWebStream`
|
### `renderToWebStream`
|
||||||
@ -88,19 +100,40 @@ function renderToWebStream(
|
|||||||
**Usage**
|
**Usage**
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// e.g. inside an environment with ReadableStream support
|
// inside an environment with ReadableStream support
|
||||||
return new Response(renderToWebStream(app))
|
return new Response(renderToWebStream(app))
|
||||||
```
|
```
|
||||||
|
|
||||||
Note in environments that do not expose `ReadableStream` constructor in the global scope, the constructor must be explicitly passed in as the 3rd argument. For example in Node.js 16.5.0+ where web streams are also supported:
|
**Note:** in environments that do not expose `ReadableStream` constructor in the global scope, `pipeToWebWritable` should be used instead.
|
||||||
|
|
||||||
```js
|
### `pipeToWebWritable`
|
||||||
import { ReadableStream } from 'stream/web'
|
|
||||||
|
|
||||||
const stream = renderToWebStream(app, {}, ReadableStream)
|
Render and pipe to an existing [Web WritableStream](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream) instance.
|
||||||
|
|
||||||
|
**Signature**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
function pipeToWebWritable(
|
||||||
|
input: App | VNode,
|
||||||
|
context: SSRContext = {},
|
||||||
|
writable: WritableStream
|
||||||
|
): void
|
||||||
```
|
```
|
||||||
|
|
||||||
## `renderToSimpleStream`
|
**Usage**
|
||||||
|
|
||||||
|
This is typically used in combination with [`TransformStream`](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream):
|
||||||
|
|
||||||
|
```js
|
||||||
|
// TransformStream is available in environments such as CloudFlare workers.
|
||||||
|
// in Node.js, TransformStream needs to be explicitly imported from 'stream/web'
|
||||||
|
const { readable, writable } = new TransformStream()
|
||||||
|
pipeToWebWritable(app, {}, writable)
|
||||||
|
|
||||||
|
return new Response(readable)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `renderToSimpleStream`
|
||||||
|
|
||||||
Renders input in streaming mode using a simple readable interface.
|
Renders input in streaming mode using a simple readable interface.
|
||||||
|
|
||||||
|
@ -24,10 +24,10 @@ import {
|
|||||||
} from 'vue'
|
} from 'vue'
|
||||||
import { escapeHtml } from '@vue/shared'
|
import { escapeHtml } from '@vue/shared'
|
||||||
import { renderToString } from '../src/renderToString'
|
import { renderToString } from '../src/renderToString'
|
||||||
import { renderToNodeStream } from '../src/renderToStream'
|
import { renderToNodeStream, pipeToNodeWritable } from '../src/renderToStream'
|
||||||
import { ssrRenderSlot, SSRSlot } from '../src/helpers/ssrRenderSlot'
|
import { ssrRenderSlot, SSRSlot } from '../src/helpers/ssrRenderSlot'
|
||||||
import { ssrRenderComponent } from '../src/helpers/ssrRenderComponent'
|
import { ssrRenderComponent } from '../src/helpers/ssrRenderComponent'
|
||||||
import { Readable } from 'stream'
|
import { Readable, Transform } from 'stream'
|
||||||
import { ssrRenderVNode } from '../src'
|
import { ssrRenderVNode } from '../src'
|
||||||
|
|
||||||
const promisifyStream = (stream: Readable) => {
|
const promisifyStream = (stream: Readable) => {
|
||||||
@ -45,12 +45,25 @@ const promisifyStream = (stream: Readable) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const renderToStream = (app: any, context?: any) =>
|
const renderToStream = (app: any, context?: any) => {
|
||||||
promisifyStream(renderToNodeStream(app, context))
|
return promisifyStream(renderToNodeStream(app, context))
|
||||||
|
}
|
||||||
|
|
||||||
|
const pipeToWritable = (app: any, context?: any) => {
|
||||||
|
const stream = new Transform({
|
||||||
|
transform(data, _encoding, cb) {
|
||||||
|
this.push(data)
|
||||||
|
cb()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
pipeToNodeWritable(app, context, stream)
|
||||||
|
return promisifyStream(stream)
|
||||||
|
}
|
||||||
|
|
||||||
// we run the same tests twice, once for renderToString, once for renderToStream
|
// we run the same tests twice, once for renderToString, once for renderToStream
|
||||||
testRender(`renderToString`, renderToString)
|
testRender(`renderToString`, renderToString)
|
||||||
testRender(`renderToStream`, renderToStream)
|
testRender(`renderToNodeStream`, renderToStream)
|
||||||
|
testRender(`pipeToNodeWritable`, pipeToWritable)
|
||||||
|
|
||||||
function testRender(type: string, render: typeof renderToString) {
|
function testRender(type: string, render: typeof renderToString) {
|
||||||
describe(`ssr: ${type}`, () => {
|
describe(`ssr: ${type}`, () => {
|
||||||
@ -760,7 +773,7 @@ function testRender(type: string, render: typeof renderToString) {
|
|||||||
test('handle compiler errors', async () => {
|
test('handle compiler errors', async () => {
|
||||||
await render(
|
await render(
|
||||||
// render different content since compilation is cached
|
// render different content since compilation is cached
|
||||||
createApp({ template: `<${type === 'renderToString' ? 'div' : 'p'}` })
|
createApp({ template: `<div>${type}</` })
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
|
@ -3,10 +3,19 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { createApp, h, defineAsyncComponent } from 'vue'
|
import { createApp, h, defineAsyncComponent } from 'vue'
|
||||||
import { ReadableStream } from 'stream/web'
|
import { ReadableStream, TransformStream } from 'stream/web'
|
||||||
import { renderToWebStream } from '../src'
|
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(() =>
|
const Async = defineAsyncComponent(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
render: () => h('div', 'async')
|
render: () => h('div', 'async')
|
||||||
@ -16,14 +25,42 @@ test('should work', async () => {
|
|||||||
render: () => [h('div', 'parent'), h(Async)]
|
render: () => [h('div', 'parent'), h(Async)]
|
||||||
}
|
}
|
||||||
|
|
||||||
const stream = renderToWebStream(createApp(App), {}, ReadableStream)
|
const stream = renderToWebStream(createApp(App))
|
||||||
|
|
||||||
const reader = stream.getReader()
|
const reader = stream.getReader()
|
||||||
|
const decoder = new TextDecoder()
|
||||||
|
|
||||||
let res = ''
|
let res = ''
|
||||||
await reader.read().then(function read({ done, value }): any {
|
await reader.read().then(function read({ done, value }): any {
|
||||||
if (!done) {
|
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)
|
return reader.read().then(read)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -2,11 +2,14 @@
|
|||||||
export { SSRContext } from './render'
|
export { SSRContext } from './render'
|
||||||
export { renderToString } from './renderToString'
|
export { renderToString } from './renderToString'
|
||||||
export {
|
export {
|
||||||
renderToStream,
|
|
||||||
renderToSimpleStream,
|
renderToSimpleStream,
|
||||||
renderToNodeStream,
|
renderToNodeStream,
|
||||||
|
pipeToNodeWritable,
|
||||||
renderToWebStream,
|
renderToWebStream,
|
||||||
SimpleReadable
|
pipeToWebWritable,
|
||||||
|
SimpleReadable,
|
||||||
|
// deprecated
|
||||||
|
renderToStream
|
||||||
} from './renderToStream'
|
} from './renderToStream'
|
||||||
|
|
||||||
// internal runtime helpers
|
// internal runtime helpers
|
||||||
|
@ -8,7 +8,7 @@ import {
|
|||||||
} from 'vue'
|
} from 'vue'
|
||||||
import { isString, isPromise } from '@vue/shared'
|
import { isString, isPromise } from '@vue/shared'
|
||||||
import { renderComponentVNode, SSRBuffer, SSRContext } from './render'
|
import { renderComponentVNode, SSRBuffer, SSRContext } from './render'
|
||||||
import { Readable } from 'stream'
|
import { Readable, Writable } from 'stream'
|
||||||
|
|
||||||
const { isVNode } = ssrUtils
|
const { isVNode } = ssrUtils
|
||||||
|
|
||||||
@ -99,51 +99,64 @@ export function renderToStream(
|
|||||||
|
|
||||||
export function renderToNodeStream(
|
export function renderToNodeStream(
|
||||||
input: App | VNode,
|
input: App | VNode,
|
||||||
context: SSRContext = {},
|
context: SSRContext = {}
|
||||||
UserReadable?: typeof Readable
|
|
||||||
): Readable {
|
): Readable {
|
||||||
const stream: Readable = UserReadable
|
const stream: Readable = __NODE_JS__
|
||||||
? new UserReadable()
|
|
||||||
: __NODE_JS__
|
|
||||||
? new (require('stream').Readable)()
|
? new (require('stream').Readable)()
|
||||||
: null
|
: null
|
||||||
|
|
||||||
if (!stream) {
|
if (!stream) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`ESM build of renderToStream() requires explicitly passing in the Node.js ` +
|
`ESM build of renderToStream() does not support renderToNodeStream(). ` +
|
||||||
`Readable constructor the 3rd argument. Example:\n\n` +
|
`Use pipeToNodeWritable() with an existing Node.js Writable stream ` +
|
||||||
` import { Readable } from 'stream'\n` +
|
`instance instead.`
|
||||||
` const stream = renderToStream(app, {}, Readable)`
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return renderToSimpleStream(input, context, stream)
|
return renderToSimpleStream(input, context, stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasGlobalWebStream = typeof ReadableStream === 'function'
|
export function pipeToNodeWritable(
|
||||||
|
input: App | VNode,
|
||||||
|
context: SSRContext = {},
|
||||||
|
writable: Writable
|
||||||
|
) {
|
||||||
|
renderToSimpleStream(input, context, {
|
||||||
|
push(content) {
|
||||||
|
if (content != null) {
|
||||||
|
writable.write(content)
|
||||||
|
} else {
|
||||||
|
writable.end()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
destroy(err) {
|
||||||
|
writable.destroy(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function renderToWebStream(
|
export function renderToWebStream(
|
||||||
input: App | VNode,
|
input: App | VNode,
|
||||||
context: SSRContext = {},
|
context: SSRContext = {}
|
||||||
Ctor?: { new (): ReadableStream }
|
|
||||||
): ReadableStream {
|
): ReadableStream {
|
||||||
if (!Ctor && !hasGlobalWebStream) {
|
if (typeof ReadableStream !== 'function') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`ReadableStream constructor is not available in the global scope and ` +
|
`ReadableStream constructor is not available in the global scope. ` +
|
||||||
`must be explicitly passed in as the 3rd argument:\n\n` +
|
`If the target environment does support web streams, consider using ` +
|
||||||
` import { ReadableStream } from 'stream/web'\n` +
|
`pipeToWebWritable() with an existing WritableStream instance instead.`
|
||||||
` const stream = renderToWebStream(app, {}, ReadableStream)`
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const encoder = new TextEncoder()
|
||||||
let cancelled = false
|
let cancelled = false
|
||||||
return new (Ctor || ReadableStream)({
|
|
||||||
|
return new ReadableStream({
|
||||||
start(controller) {
|
start(controller) {
|
||||||
renderToSimpleStream(input, context, {
|
renderToSimpleStream(input, context, {
|
||||||
push(content) {
|
push(content) {
|
||||||
if (cancelled) return
|
if (cancelled) return
|
||||||
if (content != null) {
|
if (content != null) {
|
||||||
controller.enqueue(content)
|
controller.enqueue(encoder.encode(content))
|
||||||
} else {
|
} else {
|
||||||
controller.close()
|
controller.close()
|
||||||
}
|
}
|
||||||
@ -158,3 +171,29 @@ export function renderToWebStream(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function pipeToWebWritable(
|
||||||
|
input: App | VNode,
|
||||||
|
context: SSRContext = {},
|
||||||
|
writable: WritableStream
|
||||||
|
): void {
|
||||||
|
const writer = writable.getWriter()
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
|
||||||
|
writer.ready.then(() => {
|
||||||
|
renderToSimpleStream(input, context, {
|
||||||
|
push(content) {
|
||||||
|
if (content != null) {
|
||||||
|
writer.write(encoder.encode(content))
|
||||||
|
} else {
|
||||||
|
writer.close()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
destroy(err) {
|
||||||
|
// TODO better error handling?
|
||||||
|
console.log(err)
|
||||||
|
writer.close()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user