From d03695692fddecbaf8d7e40dc3e6c598d76e5e72 Mon Sep 17 00:00:00 2001 From: Yilun Date: Wed, 13 Nov 2019 16:19:25 -0800 Subject: [PATCH] Add resize protocol Signed-off-by: Yilun --- lib/index.js | 44 +++++++++++++++++++++++++++++++++++--------- package.json | 2 +- src/index.js | 40 +++++++++++++++++++++++++++++++--------- 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/lib/index.js b/lib/index.js index c311fff..351168d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -226,11 +226,17 @@ class Session { stdout: this.outputBuffer }; this.outputBuffer = ''; - this.client.send(this.remoteAddr, JSON.stringify(res), { - msgHoldingSeconds: 0 - }).catch(e => { + + try { + this.client.send(this.remoteAddr, JSON.stringify(res), { + msgHoldingSeconds: 0 + }).catch(e => { + console.error("Send msg error:", e); + }); + } catch (e) { console.error("Send msg error:", e); - }); + } + setTimeout(this.flushSession, sessionFlushIntervalInUse); } else { setTimeout(this.flushSession, sessionFlushIntervalIdle); @@ -244,6 +250,10 @@ class Session { this.ptyProcess.write(cmd); } + resize(size) { + this.ptyProcess.resize(size.cols, size.rows); + } + } let sessions = {}; @@ -309,7 +319,21 @@ client.on('message', async (src, payload, payloadType, encrypt) => { return false; } + if (session && msg.resize) { + if (!sessions[src]) { + sessions[src] = new Session(client, src); + } + + sessions[src].resize(msg.resize); + console.log('Resize to', msg.resize, 'from', src); + } + let cmd = msg.cmd || msg.content; + + if (!cmd) { + return false; + } + let options = { uid: au.uid, gid: au.gid @@ -340,7 +364,7 @@ client.on('message', async (src, payload, payloadType, encrypt) => { sessions[src].write(cmd); } else { - (0, _child_process.exec)(cmd, options, (error, stdout, stderr) => { + (0, _child_process.exec)(cmd, options, async (error, stdout, stderr) => { let res; if (msg.content) { @@ -358,11 +382,13 @@ client.on('message', async (src, payload, payloadType, encrypt) => { }; } - client.send(src, JSON.stringify(res), { - msgHoldingSeconds: 0 - }).catch(e => { + try { + await client.send(src, JSON.stringify(res), { + msgHoldingSeconds: 0 + }); + } catch (e) { console.error("Send msg error:", e); - }); + } }); } } diff --git a/package.json b/package.json index e166829..3e37269 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nkn-shell-daemon", - "version": "1.0.2", + "version": "1.0.3", "description": "NKN Shell Daemon", "main": "nshd.js", "bin": { diff --git a/src/index.js b/src/index.js index d750d23..ee58aca 100644 --- a/src/index.js +++ b/src/index.js @@ -214,12 +214,16 @@ class Session { stdout: this.outputBuffer } this.outputBuffer = '' - this.client.send(this.remoteAddr, JSON.stringify(res), { msgHoldingSeconds: 0 }).catch(e => { - console.error("Send msg error:", e); - }); - setTimeout(this.flushSession, sessionFlushIntervalInUse); + try { + this.client.send(this.remoteAddr, JSON.stringify(res), { msgHoldingSeconds: 0 }).catch(e => { + console.error("Send msg error:", e) + }); + } catch (e) { + console.error("Send msg error:", e) + } + setTimeout(this.flushSession, sessionFlushIntervalInUse) } else { - setTimeout(this.flushSession, sessionFlushIntervalIdle); + setTimeout(this.flushSession, sessionFlushIntervalIdle) } } @@ -229,6 +233,10 @@ class Session { write(cmd) { this.ptyProcess.write(cmd) } + + resize(size) { + this.ptyProcess.resize(size.cols, size.rows) + } } let sessions = {} @@ -291,7 +299,19 @@ client.on('message', async (src, payload, payloadType, encrypt) => { return false } + if (session && msg.resize) { + if (!sessions[src]) { + sessions[src] = new Session(client, src) + } + sessions[src].resize(msg.resize) + console.log('Resize to', msg.resize, 'from', src) + } + let cmd = msg.cmd || msg.content + if (!cmd) { + return false + } + let options = { uid: au.uid, gid: au.gid, @@ -319,7 +339,7 @@ client.on('message', async (src, payload, payloadType, encrypt) => { } sessions[src].write(cmd) } else { - exec(cmd, options, (error, stdout, stderr) => { + exec(cmd, options, async (error, stdout, stderr) => { let res; if (msg.content) { // d-chat protocol res = { @@ -334,9 +354,11 @@ client.on('message', async (src, payload, payloadType, encrypt) => { stderr, } } - client.send(src, JSON.stringify(res), { msgHoldingSeconds: 0 }).catch(e => { - console.error("Send msg error:", e); - }); + try { + await client.send(src, JSON.stringify(res), { msgHoldingSeconds: 0 }) + } catch (e) { + console.error("Send msg error:", e) + } }); } }