From 80450450e91abff3deb9426ce3d408ef4ad2331b Mon Sep 17 00:00:00 2001 From: Paris Kasidiaris Date: Tue, 12 Dec 2017 10:50:18 +0200 Subject: [PATCH] Export zmodemAttach --- src/addons/zmodem/zmodem.ts | 109 ++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 55 deletions(-) diff --git a/src/addons/zmodem/zmodem.ts b/src/addons/zmodem/zmodem.ts index 3a79d1c8..68ce691c 100644 --- a/src/addons/zmodem/zmodem.ts +++ b/src/addons/zmodem/zmodem.ts @@ -27,63 +27,62 @@ * via `detach()` and a re-`attach()`.) */ +export function zmodemAttach(term, ws, opts) { + if (!opts) opts = {}; + + var senderFunc = function _ws_sender_func(octets) { + ws.send(new Uint8Array(octets)); + }; + + var zsentry; + + function _shouldWrite() { + return !!zsentry.get_confirmed_session() || !opts.noTerminalWriteOutsideSession; + } + + zsentry = new Zmodem.Sentry( { + to_terminal: function _to_terminal(octets) { + if (_shouldWrite()) { + term.write( + String.fromCharCode.apply(String, octets) + ); + } + }, + + sender: senderFunc, + + on_retract: function _on_retract() { + term.emit('zmodemRetract'); + }, + + on_detect: function _on_detect(detection) { + term.emit('zmodemDetect', detection); + }, + } ); + + function handleWSMessage(evt) { + + //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') { + if (_shouldWrite()) { + term.write(evt.data); + } + } + else { + zsentry.consume(evt.data); + } + } + + ws.binaryType = 'arraybuffer'; + ws.addEventListener('message', handleWSMessage); +} + export function apply(terminalConstructor) { let Zmodem = (typeof window == 'object') ? (window).ZModem : {Browser: null}; // Nullify browser for tests - terminalConstructor.prototype.zmodemAttach = function(ws, opts) { - var term = this; - - if (!opts) opts = {}; - - var senderFunc = function _ws_sender_func(octets) { - ws.send( new Uint8Array(octets) ); - }; - - var zsentry; - - function _shouldWrite() { - return !!zsentry.get_confirmed_session() || !opts.noTerminalWriteOutsideSession; - } - - zsentry = new Zmodem.Sentry( { - to_terminal: function _to_terminal(octets) { - if (_shouldWrite()) { - term.write( - String.fromCharCode.apply(String, octets) - ); - } - }, - - sender: senderFunc, - - on_retract: function _on_retract() { - term.emit('zmodemRetract'); - }, - - on_detect: function _on_detect(detection) { - term.emit('zmodemDetect', detection); - }, - } ); - - function handleWSMessage(evt) { - - //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') { - if (_shouldWrite()) { - term.write(evt.data); - } - } - else { - zsentry.consume(evt.data); - } - } - - ws.binaryType = 'arraybuffer'; - ws.addEventListener('message', handleWSMessage); - } - + terminalConstructor.prototype.zmodemAttach = zmodemAttach.bind(this, this); terminalConstructor.prototype.zmodemBrowser = Zmodem.Browser }