Search code examples
node.jsvue.jsxtermssh2

NodeJS: SSH2 - Read last 1000 letters from stream


I have a Vue App with a component for xterm and parallel to it I'm using ssh2/shell as a client with a global Vue plugin.
I can connect via my ssh plugin to a server on my xterm component and writing my ssh stream into the xterm with the stream.on('data', cb) event.
So far so good, it works perfect.

My problem starts here... if I switch to another Vue route and come back to my xterm component.
The xterm component is recreated and, of cource, empty. Is there a way to access the old stream data and also the stream data I missed since I left the component from my existing stream object (it exists in my global Vue plugin)? I just need to initialize my xterm component with the last 1000 letters of my ssh connection.


Solution

  • I simply call on my 'on Data' event a separate function and copy the data/chunks to a string. When I recreate my terminal component, I initialize the content with the buffer string. Also, I can watch if my "buffer" is too full and slice it.
    If someone gets in the same situation, here is a example:

    stream.on('data', (data) => {
        ...
        this.buffer(data)
    })
    ...
    buffer(data) {
        this.streamBuffer += data // Saving data/chunks
    
        const bufferLength = this.streamBuffer.length
        if (bufferLength > 10000) {
            this.streamBuffer = this.streamBuffer.slice(bufferLength - 8000)
        }
    }