From 6c3af6e19aa6bc4ebdf548043ca03a6afed45328 Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Fri, 27 Oct 2017 20:12:47 -0500 Subject: [PATCH] provide for better compatibility with attach.js --- demo/main.js | 4 +++- src/addons/attach/attach.js | 20 ++++++++++++++++++-- src/addons/zmodem/zmodem.js | 29 ++++++++++++++++++++++------- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/demo/main.js b/demo/main.js index b3ea72a6..7e14119b 100644 --- a/demo/main.js +++ b/demo/main.js @@ -112,7 +112,9 @@ function createTerminal() { socket.onclose = runFakeTerminal; socket.onerror = runFakeTerminal; - term.zmodemAttach(socket); + term.zmodemAttach(socket, { + noTerminalWriteOutsideSession: true, + } ); term.on("zmodemDetect", (detection) => { term.detach(); diff --git a/src/addons/attach/attach.js b/src/addons/attach/attach.js index 997d1097..712d911d 100644 --- a/src/addons/attach/attach.js +++ b/src/addons/attach/attach.js @@ -56,11 +56,27 @@ } }; + var myTextDecoder; + term._getMessage = function (ev) { + var str; + if (typeof ev.data === "object") { + if (ev.data instanceof ArrayBuffer) { + if (!myTextDecoder) { + myTextDecoder = new TextDecoder(); + } + + str = myTextDecoder.decode( ev.data ); + } + else { + throw "TODO: handle Blob?"; + } + } + if (buffered) { - term._pushToBuffer(ev.data); + term._pushToBuffer(str || ev.data); } else { - term.write(ev.data); + term.write(str || ev.data); } }; diff --git a/src/addons/zmodem/zmodem.js b/src/addons/zmodem/zmodem.js index c9a7c8a2..e3ebfeee 100644 --- a/src/addons/zmodem/zmodem.js +++ b/src/addons/zmodem/zmodem.js @@ -41,18 +41,28 @@ Object.assign( Terminal.prototype, { - zmodemAttach: function zmodemAttach(ws) { + zmodemAttach: function zmodemAttach(ws, opts) { var term = this; + if (!opts) opts = {}; + var senderFunc = function _ws_sender_func(octets) { ws.send( new Uint8Array(octets) ); }; - var zsentry = new Zmodem.Sentry( { + var zsentry; + + function _shouldWrite() { + return !!zsentry.get_confirmed_session() || !opts.noTerminalWriteOutsideSession; + } + + zsentry = new Zmodem.Sentry( { to_terminal: function _to_terminal(octets) { - term.write( - String.fromCharCode.apply(String, octets) - ); + if (_shouldWrite()) { + term.write( + String.fromCharCode.apply(String, octets) + ); + } }, sender: senderFunc, @@ -68,9 +78,14 @@ function handleWSMessage(evt) { - //For some reason the first message from the server is text. + //In testing with xterm.js’s demo the first message was + //always text even if the rest were binary. While that + //may be specific to xterm.js’s demo, ultimately we + //should reject anything that isn’t binary. if (typeof evt.data === "string") { - term.write(evt.data); + if (_shouldWrite()) { + term.write(evt.data); + } } else { zsentry.consume(evt.data);