Skip to content

Commit

Permalink
Add resize protocol
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <zyl.skysniper@gmail.com>
  • Loading branch information
yilunzhang committed Nov 14, 2019
1 parent a40b74e commit d036956
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
44 changes: 35 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -244,6 +250,10 @@ class Session {
this.ptyProcess.write(cmd);
}

resize(size) {
this.ptyProcess.resize(size.cols, size.rows);
}

}

let sessions = {};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
});
}
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nkn-shell-daemon",
"version": "1.0.2",
"version": "1.0.3",
"description": "NKN Shell Daemon",
"main": "nshd.js",
"bin": {
Expand Down
40 changes: 31 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -229,6 +233,10 @@ class Session {
write(cmd) {
this.ptyProcess.write(cmd)
}

resize(size) {
this.ptyProcess.resize(size.cols, size.rows)
}
}

let sessions = {}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand All @@ -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)
}
});
}
}
Expand Down

0 comments on commit d036956

Please sign in to comment.