Home / File/ render-stream.ts — vue Source File

render-stream.ts — vue Source File

Architecture documentation for render-stream.ts, a typescript file in the vue codebase. 4 imports, 1 dependents.

File typescript ServerRenderer TemplateRenderer 4 imports 1 dependents 1 classes

Entity Profile

Dependency Diagram

graph LR
  1d39271f_a04e_7bb6_d848_5fe05aeadfba["render-stream.ts"]
  b39ee3e2_2806_ad3f_11bf_d581e690be13["write.ts"]
  1d39271f_a04e_7bb6_d848_5fe05aeadfba --> b39ee3e2_2806_ad3f_11bf_d581e690be13
  5086a0f6_8276_ebf4_9891_182f4be074f2["createWriteFunction"]
  1d39271f_a04e_7bb6_d848_5fe05aeadfba --> 5086a0f6_8276_ebf4_9891_182f4be074f2
  88745fde_3d83_000a_cda6_1c8dad22f755["stream"]
  1d39271f_a04e_7bb6_d848_5fe05aeadfba --> 88745fde_3d83_000a_cda6_1c8dad22f755
  156bf2e1_8a13_f22d_5437_54f14bcef8fa["util"]
  1d39271f_a04e_7bb6_d848_5fe05aeadfba --> 156bf2e1_8a13_f22d_5437_54f14bcef8fa
  38cdd14a_61b8_35ed_893f_b542594d7017["create-renderer.ts"]
  38cdd14a_61b8_35ed_893f_b542594d7017 --> 1d39271f_a04e_7bb6_d848_5fe05aeadfba
  style 1d39271f_a04e_7bb6_d848_5fe05aeadfba fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Original RenderStream implementation by Sasha Aickin (@aickin)
 * Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Modified by Evan You (@yyx990803)
 */

// const stream = require('stream')
import { Readable } from 'stream'

import { isTrue, isUndef } from 'shared/util'
import { createWriteFunction } from './write'

export default class RenderStream extends Readable {
  buffer: string
  render: (write: Function, done: Function) => void
  expectedSize: number
  write: Function
  //@ts-expect-error
  next: Function
  end: Function
  //@ts-expect-error
  done: boolean

  constructor(render: Function) {
    super()
    this.buffer = ''
    //@ts-expect-error
    this.render = render
    this.expectedSize = 0

    this.write = createWriteFunction(
      (text, next) => {
        const n = this.expectedSize
        this.buffer += text
        if (this.buffer.length >= n) {
          this.next = next
          this.pushBySize(n)
          return true // we will decide when to call next
        }
        return false
      },
      err => {
        this.emit('error', err)
      }
    )

    this.end = () => {
      this.emit('beforeEnd')
      // the rendering is finished; we should push out the last of the buffer.
      this.done = true
      this.push(this.buffer)
    }
  }

  pushBySize(n: number) {
    const bufferToPush = this.buffer.substring(0, n)
    this.buffer = this.buffer.substring(n)
    this.push(bufferToPush)
  }

  tryRender() {
    try {
      this.render(this.write, this.end)
    } catch (e) {
      this.emit('error', e)
    }
  }

  tryNext() {
    try {
      this.next()
    } catch (e) {
      this.emit('error', e)
    }
  }

  _read(n: number) {
    this.expectedSize = n
    // it's possible that the last chunk added bumped the buffer up to > 2 * n,
    // which means we will need to go through multiple read calls to drain it
    // down to < n.
    if (isTrue(this.done)) {
      this.push(null)
      return
    }
    if (this.buffer.length >= n) {
      this.pushBySize(n)
      return
    }
    if (isUndef(this.next)) {
      // start the rendering chain.
      this.tryRender()
    } else {
      // continue with the rendering.
      this.tryNext()
    }
  }
}

Subdomains

Classes

Dependencies

Frequently Asked Questions

What does render-stream.ts do?
render-stream.ts is a source file in the vue codebase, written in typescript. It belongs to the ServerRenderer domain, TemplateRenderer subdomain.
What does render-stream.ts depend on?
render-stream.ts imports 4 module(s): createWriteFunction, stream, util, write.ts.
What files import render-stream.ts?
render-stream.ts is imported by 1 file(s): create-renderer.ts.
Where is render-stream.ts in the architecture?
render-stream.ts is located at packages/server-renderer/src/render-stream.ts (domain: ServerRenderer, subdomain: TemplateRenderer, directory: packages/server-renderer/src).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free