Skip to content

Commit

Permalink
Split input into buffer sized chunks
Browse files Browse the repository at this point in the history
Input chunks from the client exceeding the buffer size would get
truncated. Now we communicate the size of the buffer to the webtty and
it will split the input into buffer sized chunks.

Fixes yudai#1.
  • Loading branch information
sorenisanerd committed Apr 13, 2021
1 parent 77c436b commit 0d6766f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion js/dist/gotty-bundle.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions js/dist/webtty.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export declare const msgPong = "2";
export declare const msgSetWindowTitle = "3";
export declare const msgSetPreferences = "4";
export declare const msgSetReconnect = "5";
export declare const msgSetBufferSize = "6";
export interface Terminal {
info(): {
columns: number;
Expand Down Expand Up @@ -43,6 +44,7 @@ export declare class WebTTY {
args: string;
authToken: string;
reconnect: number;
bufSize: number;
constructor(term: Terminal, connectionFactory: ConnectionFactory, args: string, authToken: string);
open(): () => void;
}
16 changes: 15 additions & 1 deletion js/src/webtty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const msgPong = '2';
export const msgSetWindowTitle = '3';
export const msgSetPreferences = '4';
export const msgSetReconnect = '5';
export const msgSetBufferSize = '6';


export interface Terminal {
Expand Down Expand Up @@ -48,13 +49,15 @@ export class WebTTY {
args: string;
authToken: string;
reconnect: number;
bufSize: number;

constructor(term: Terminal, connectionFactory: ConnectionFactory, args: string, authToken: string) {
this.term = term;
this.connectionFactory = connectionFactory;
this.args = args;
this.authToken = authToken;
this.reconnect = -1;
this.bufSize = 1024;
};

open() {
Expand Down Expand Up @@ -90,7 +93,14 @@ export class WebTTY {

this.term.onInput(
(input: string) => {
connection.send(msgInput + input);
// Leave room for message type id
let effectiveBufferSize = this.bufSize - 1;

// Split input into buffer sized chunks
for (let i = 0; i < Math.ceil(input.length/effectiveBufferSize); i++) {
let inputChunk = input.substring(i*effectiveBufferSize, Math.min((i+1)*effectiveBufferSize, input.length))
connection.send(msgInput + inputChunk);
}
}
);

Expand Down Expand Up @@ -120,6 +130,10 @@ export class WebTTY {
console.log("Enabling reconnect: " + autoReconnect + " seconds")
this.reconnect = autoReconnect;
break;
case msgSetBufferSize:
const bufSize = JSON.parse(payload);
this.bufSize = bufSize;
break;
}
});

Expand Down
2 changes: 1 addition & 1 deletion server/asset.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions webtty/message_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ const (
SetPreferences = '4'
// Make terminal to reconnect
SetReconnect = '5'
// Set the input buffer size
SetBufferSize = '6'
)
6 changes: 6 additions & 0 deletions webtty/webtty.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func (wt *WebTTY) sendInitializeMessage() error {
return errors.Wrapf(err, "failed to send window title")
}

bufSizeMsg, _ := json.Marshal(wt.bufferSize)
err = wt.masterWrite(append([]byte{SetBufferSize}, bufSizeMsg...))
if err != nil {
return errors.Wrapf(err, "failed to send buffer size")
}

if wt.reconnect > 0 {
reconnect, _ := json.Marshal(wt.reconnect)
err := wt.masterWrite(append([]byte{SetReconnect}, reconnect...))
Expand Down

0 comments on commit 0d6766f

Please sign in to comment.