( rootNode: any, opts: RenderToStreamOptions )
| 33 | * @public |
| 34 | */ |
| 35 | export async function renderToStream( |
| 36 | rootNode: any, |
| 37 | opts: RenderToStreamOptions |
| 38 | ): Promise<RenderToStreamResult> { |
| 39 | let stream = opts.stream; |
| 40 | let bufferSize = 0; |
| 41 | let totalSize = 0; |
| 42 | let networkFlushes = 0; |
| 43 | let firstFlushTime = 0; |
| 44 | let buffer: string = ''; |
| 45 | let snapshotResult: SnapshotResult | undefined; |
| 46 | const inOrderStreaming = opts.streaming?.inOrder ?? { |
| 47 | strategy: 'auto', |
| 48 | maximunInitialChunk: 50000, |
| 49 | maximunChunk: 30000, |
| 50 | }; |
| 51 | const containerTagName = opts.containerTagName ?? 'html'; |
| 52 | const containerAttributes = opts.containerAttributes ?? {}; |
| 53 | const nativeStream = stream; |
| 54 | const firstFlushTimer = createTimer(); |
| 55 | const buildBase = getBuildBase(opts); |
| 56 | const resolvedManifest = resolveManifest(opts.manifest); |
| 57 | const nonce = opts.serverData?.nonce; |
| 58 | function flush() { |
| 59 | if (buffer) { |
| 60 | nativeStream.write(buffer); |
| 61 | buffer = ''; |
| 62 | bufferSize = 0; |
| 63 | networkFlushes++; |
| 64 | if (networkFlushes === 1) { |
| 65 | firstFlushTime = firstFlushTimer(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | function enqueue(chunk: string) { |
| 70 | const len = chunk.length; |
| 71 | bufferSize += len; |
| 72 | totalSize += len; |
| 73 | buffer += chunk; |
| 74 | } |
| 75 | switch (inOrderStreaming.strategy) { |
| 76 | case 'disabled': |
| 77 | stream = { |
| 78 | write: enqueue, |
| 79 | }; |
| 80 | break; |
| 81 | case 'direct': |
| 82 | stream = nativeStream; |
| 83 | break; |
| 84 | case 'auto': |
| 85 | let count = 0; |
| 86 | let forceFlush = false; |
| 87 | const minimunChunkSize = inOrderStreaming.maximunChunk ?? 0; |
| 88 | const initialChunkSize = inOrderStreaming.maximunInitialChunk ?? 0; |
| 89 | stream = { |
| 90 | write(chunk) { |
| 91 | if (chunk === '<!--qkssr-f-->') { |
| 92 | forceFlush ||= true; |
no test coverage detected
searching dependent graphs…