Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle blob in attach Addon #1355

Merged
merged 2 commits into from
Mar 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/addons/attach/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,44 @@ export function attach(term: Terminal, socket: WebSocket, bidirectional: boolean

addonTerminal.__getMessage = function(ev: MessageEvent): void {
let str;
if (typeof ev.data === 'object') {
if (ev.data instanceof ArrayBuffer) {
if (!myTextDecoder) {
myTextDecoder = new TextDecoder();
}

str = myTextDecoder.decode( ev.data );
if (typeof ev.data == 'object') {
if (!myTextDecoder) {
myTextDecoder = new TextDecoder();
}
if (ev.data instanceof ArrayBuffer) {
str = myTextDecoder.decode(ev.data);
displayData(str);
} else {
throw 'TODO: handle Blob?';
let fileReader = new FileReader();

fileReader.addEventListener('load', function() {
str = myTextDecoder.decode(this.result);
displayData(str);
});
fileReader.readAsArrayBuffer(ev.data);
}
} else if (typeof ev.data == 'string') {
displayData(ev.data)
} else {
throw Error(`Cannot handle "${typeof ev.data}" websocket message.`);
}
};

/**
* Push data to buffer or write it in the terminal.
* This is used as a callback for FileReader.onload.
*
* @param str String decoded by FileReader.
* @param data The data of the EventMessage.
*/
function displayData(str?: string, data?: string) {
if (buffered) {
addonTerminal.__pushToBuffer(str || ev.data);
addonTerminal.__pushToBuffer(str || data);
} else {
addonTerminal.write(str || ev.data);
addonTerminal.write(str || data);
}
};
}

addonTerminal.__sendData = (data: string) => {
if (socket.readyState !== 1) {
Expand Down