Skip to content

Commit

Permalink
Fix session mode compatibility with xterm and d-chat and add flush in…
Browse files Browse the repository at this point in the history
…terval

Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Nov 13, 2019
1 parent 6e4064a commit a1472fb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 79 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ public/
/authorized_pubkeys
/wallet.json
/wallet.pswd
build
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: all
all:
yarn build
rm -rf build/linux-amd64 && pkg -t linux-x64 -o build/linux-amd64/nshd . && wget -c https://github.com/oznu/node-pty-prebuilt-multiarch/releases/download/v0.9.0/node-pty-prebuilt-multiarch-v0.9.0-node-v64-linux-x64.tar.gz -O - | tar -xz && mv build/Release/pty.node build/linux-amd64/
rm -rf build/macos-amd64 && pkg -t mac-x64 -o build/macos-amd64/nshd . && wget -c https://github.com/oznu/node-pty-prebuilt-multiarch/releases/download/v0.9.0/node-pty-prebuilt-multiarch-v0.9.0-node-v64-darwin-x64.tar.gz -O - | tar -xz && mv build/Release/pty.node build/macos-amd64/

.PHONY: tar
tar:
cd build && rm -f linux-amd64.tar.gz && tar --exclude ".DS_Store" --exclude "__MACOSX" -czvf linux-amd64.tar.gz linux-amd64
cd build && rm -f macos-amd64.tar.gz && tar --exclude ".DS_Store" --exclude "__MACOSX" -czvf macos-amd64.tar.gz macos-amd64
79 changes: 38 additions & 41 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,26 +200,10 @@ const client = (0, _nknMulticlient.default)({
identifier: identifier
});
let ptyProcess;
let res;
let sendRes;
let sessionSrc;
client.on('connect', () => {
console.log('Listening at', client.addr);

if (session) {
ptyProcess = (0, _nodePty.spawn)(shell, [], {
name: 'xterm-color',
cols: 120,
rows: 30,
cwd: isWindows ? process.env.USERPROFILE : process.env.HOME,
env: process.env
});
ptyProcess.onData(data => {
if (typeof sendRes === 'function') {
sendRes(data);
}
});
}

for (let i = 0, length = client.clients.length; i < length; i++) {
let c = client.clients[i];
setInterval(function () {
Expand Down Expand Up @@ -250,6 +234,40 @@ client.on('connect', () => {
}
}
}, pingInterval);

if (session && !ptyProcess) {
let sessionBuffer = '';
let res;
ptyProcess = (0, _nodePty.spawn)(shell, [], {
name: 'xterm-color',
cols: 120,
rows: 30,
cwd: isWindows ? process.env.USERPROFILE : process.env.HOME,
env: process.env
});
ptyProcess.onData(data => {
sessionBuffer += data;
});

function flushSession() {
if (sessionBuffer.length > 0) {
res = {
stdout: sessionBuffer
};
sessionBuffer = '';
client.send(sessionSrc, JSON.stringify(res), {
msgHoldingSeconds: 0
}).catch(e => {
console.error("Send msg error:", e);
});
setTimeout(flushSession, 10);
} else {
setTimeout(flushSession, 25);
}
}

flushSession();
}
});
client.on('message', async (src, payload, payloadType, encrypt) => {
if (!encrypt) {
Expand Down Expand Up @@ -303,30 +321,9 @@ client.on('message', async (src, payload, payloadType, encrypt) => {
} else {
options.timeout = msg.execTimeout || asyncExecTimeout;

if (session) {
sendRes = function (data) {
if (msg.content) {
res = {
content: '```\n' + data + '\n```',
contentType: 'text',
timestamp: new Date().toUTCString(),
isPrivate: true
};
} else {
res = {
stdout,
stderr
};
}

client.send(src, JSON.stringify(res), {
msgHoldingSeconds: 0
}).catch(e => {
console.error('Send msg error:', e);
});
};

ptyProcess.write(cmd + '\r');
if (session && !msg.content) {
sessionSrc = src;
ptyProcess.write(cmd);
} else {
(0, _child_process.exec)(cmd, options, (error, stdout, stderr) => {
let res;
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.0",
"version": "1.0.1",
"description": "NKN Shell Daemon",
"main": "nshd.js",
"bin": {
Expand Down
74 changes: 37 additions & 37 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,24 +187,10 @@ const client = nknClient({
})

let ptyProcess
let res
let sendRes
let sessionSrc

client.on('connect', () => {
console.log('Listening at', client.addr)
if(session) {
ptyProcess = spawn(shell, [], {
name: 'xterm-color',
cols: 120,
rows: 30,
cwd: isWindows ? process.env.USERPROFILE : process.env.HOME,
env: process.env
})
ptyProcess.onData(data => {
if (typeof sendRes === 'function') {
sendRes(data)
}
})
}

for (let i = 0, length = client.clients.length; i < length; i++) {
let c = client.clients[i]
Expand All @@ -216,6 +202,7 @@ client.on('connect', () => {
}
}, pingInterval)
}

let lastUpdateTime = new Date()
setInterval(async function () {
try {
Expand All @@ -231,6 +218,36 @@ client.on('connect', () => {
}
}
}, pingInterval)

if (session && !ptyProcess) {
let sessionBuffer = ''
let res
ptyProcess = spawn(shell, [], {
name: 'xterm-color',
cols: 120,
rows: 30,
cwd: isWindows ? process.env.USERPROFILE : process.env.HOME,
env: process.env,
})
ptyProcess.onData(data => {
sessionBuffer += data
})
function flushSession() {
if (sessionBuffer.length > 0) {
res = {
stdout: sessionBuffer
}
sessionBuffer = ''
client.send(sessionSrc, JSON.stringify(res), {msgHoldingSeconds: 0}).catch(e => {
console.error("Send msg error:", e);
});
setTimeout(flushSession, 10);
} else {
setTimeout(flushSession, 25);
}
}
flushSession()
}
})

client.on('message', async (src, payload, payloadType, encrypt) => {
Expand Down Expand Up @@ -282,27 +299,10 @@ client.on('message', async (src, payload, payloadType, encrypt) => {
})
} else {
options.timeout = msg.execTimeout || asyncExecTimeout
if(session) {
sendRes = function (data) {
if (msg.content) {
res = {
content: '```\n' + data + '\n```',
contentType: 'text',
timestamp: new Date().toUTCString(),
isPrivate: true,
}
} else {
res = {
stdout,
stderr,
}
}
client.send(src, JSON.stringify(res), {msgHoldingSeconds: 0}).catch(e => {
console.error('Send msg error:', e)
})
}
ptyProcess.write(cmd + '\r')
}else{
if (session && !msg.content) {
sessionSrc = src
ptyProcess.write(cmd)
} else {
exec(cmd, options, (error, stdout, stderr) => {
let res;
if (msg.content) {
Expand Down

0 comments on commit a1472fb

Please sign in to comment.