First pass formatting with Prettier
* Added .prettierrc.json * Added .prettierignore * Formatted
This commit is contained in:
96
core/door.js
96
core/door.js
@@ -1,28 +1,28 @@
|
||||
/* jslint node: true */
|
||||
'use strict';
|
||||
|
||||
const stringFormat = require('./string_format.js');
|
||||
const { Errors } = require('./enig_error.js');
|
||||
const Events = require('./events');
|
||||
const stringFormat = require('./string_format.js');
|
||||
const { Errors } = require('./enig_error.js');
|
||||
const Events = require('./events');
|
||||
|
||||
// deps
|
||||
const pty = require('node-pty');
|
||||
const decode = require('iconv-lite').decode;
|
||||
const createServer = require('net').createServer;
|
||||
const paths = require('path');
|
||||
const _ = require('lodash');
|
||||
const pty = require('node-pty');
|
||||
const decode = require('iconv-lite').decode;
|
||||
const createServer = require('net').createServer;
|
||||
const paths = require('path');
|
||||
const _ = require('lodash');
|
||||
|
||||
module.exports = class Door {
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
this.restored = false;
|
||||
this.client = client;
|
||||
this.restored = false;
|
||||
}
|
||||
|
||||
prepare(ioType, cb) {
|
||||
this.io = ioType;
|
||||
|
||||
// we currently only have to do any real setup for 'socket'
|
||||
if('socket' !== ioType) {
|
||||
if ('socket' !== ioType) {
|
||||
return cb(null);
|
||||
}
|
||||
|
||||
@@ -32,13 +32,16 @@ module.exports = class Door {
|
||||
});
|
||||
|
||||
conn.once('error', err => {
|
||||
this.client.log.info( { error : err.message }, 'Door socket server connection');
|
||||
this.client.log.info(
|
||||
{ error: err.message },
|
||||
'Door socket server connection'
|
||||
);
|
||||
return this.restoreIo(conn);
|
||||
});
|
||||
|
||||
this.sockServer.getConnections( (err, count) => {
|
||||
this.sockServer.getConnections((err, count) => {
|
||||
// We expect only one connection from our DOOR/emulator/etc.
|
||||
if(!err && count <= 1) {
|
||||
if (!err && count <= 1) {
|
||||
this.client.term.output.pipe(conn);
|
||||
conn.on('data', this.doorDataHandler.bind(this));
|
||||
}
|
||||
@@ -53,39 +56,39 @@ module.exports = class Door {
|
||||
run(exeInfo, cb) {
|
||||
this.encoding = (exeInfo.encoding || 'cp437').toLowerCase();
|
||||
|
||||
if('socket' === this.io && !this.sockServer) {
|
||||
if ('socket' === this.io && !this.sockServer) {
|
||||
return cb(Errors.UnexpectedState('Socket server is not running'));
|
||||
}
|
||||
|
||||
const cwd = exeInfo.cwd || paths.dirname(exeInfo.cmd);
|
||||
|
||||
const formatObj = {
|
||||
dropFile : exeInfo.dropFile,
|
||||
dropFilePath : exeInfo.dropFilePath,
|
||||
node : exeInfo.node.toString(),
|
||||
srvPort : this.sockServer ? this.sockServer.address().port.toString() : '-1',
|
||||
userId : this.client.user.userId.toString(),
|
||||
userName : this.client.user.getSanitizedName(),
|
||||
userNameRaw : this.client.user.username,
|
||||
cwd : cwd,
|
||||
dropFile: exeInfo.dropFile,
|
||||
dropFilePath: exeInfo.dropFilePath,
|
||||
node: exeInfo.node.toString(),
|
||||
srvPort: this.sockServer ? this.sockServer.address().port.toString() : '-1',
|
||||
userId: this.client.user.userId.toString(),
|
||||
userName: this.client.user.getSanitizedName(),
|
||||
userNameRaw: this.client.user.username,
|
||||
cwd: cwd,
|
||||
};
|
||||
|
||||
const args = exeInfo.args.map( arg => stringFormat(arg, formatObj) );
|
||||
const args = exeInfo.args.map(arg => stringFormat(arg, formatObj));
|
||||
|
||||
this.client.log.info(
|
||||
{ cmd : exeInfo.cmd, args, io : this.io },
|
||||
{ cmd: exeInfo.cmd, args, io: this.io },
|
||||
'Executing external door process'
|
||||
);
|
||||
|
||||
try {
|
||||
this.doorPty = pty.spawn(exeInfo.cmd, args, {
|
||||
cols : this.client.term.termWidth,
|
||||
rows : this.client.term.termHeight,
|
||||
cwd : cwd,
|
||||
env : exeInfo.env,
|
||||
encoding : null, // we want to handle all encoding ourself
|
||||
cols: this.client.term.termWidth,
|
||||
rows: this.client.term.termHeight,
|
||||
cwd: cwd,
|
||||
env: exeInfo.env,
|
||||
encoding: null, // we want to handle all encoding ourself
|
||||
});
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
return cb(e);
|
||||
}
|
||||
|
||||
@@ -93,9 +96,12 @@ module.exports = class Door {
|
||||
// PID is launched. Make sure it's killed off if the user disconnects.
|
||||
//
|
||||
Events.once(Events.getSystemEvents().ClientDisconnected, evt => {
|
||||
if (this.doorPty && this.client.session.uniqueId === _.get(evt, 'client.session.uniqueId')) {
|
||||
if (
|
||||
this.doorPty &&
|
||||
this.client.session.uniqueId === _.get(evt, 'client.session.uniqueId')
|
||||
) {
|
||||
this.client.log.info(
|
||||
{ pid : this.doorPty.pid },
|
||||
{ pid: this.doorPty.pid },
|
||||
'User has disconnected; Killing door process.'
|
||||
);
|
||||
this.doorPty.kill();
|
||||
@@ -103,10 +109,11 @@ module.exports = class Door {
|
||||
});
|
||||
|
||||
this.client.log.debug(
|
||||
{ processId : this.doorPty.pid }, 'External door process spawned'
|
||||
{ processId: this.doorPty.pid },
|
||||
'External door process spawned'
|
||||
);
|
||||
|
||||
if('stdio' === this.io) {
|
||||
if ('stdio' === this.io) {
|
||||
this.client.log.debug('Using stdio for door I/O');
|
||||
|
||||
this.client.term.output.pipe(this.doorPty);
|
||||
@@ -116,22 +123,25 @@ module.exports = class Door {
|
||||
this.doorPty.once('close', () => {
|
||||
return this.restoreIo(this.doorPty);
|
||||
});
|
||||
} else if('socket' === this.io) {
|
||||
} else if ('socket' === this.io) {
|
||||
this.client.log.debug(
|
||||
{ srvPort : this.sockServer.address().port, srvSocket : this.sockServerSocket },
|
||||
{
|
||||
srvPort: this.sockServer.address().port,
|
||||
srvSocket: this.sockServerSocket,
|
||||
},
|
||||
'Using temporary socket server for door I/O'
|
||||
);
|
||||
}
|
||||
|
||||
this.doorPty.once('exit', exitCode => {
|
||||
this.client.log.info( { exitCode : exitCode }, 'Door exited');
|
||||
this.client.log.info({ exitCode: exitCode }, 'Door exited');
|
||||
|
||||
if(this.sockServer) {
|
||||
if (this.sockServer) {
|
||||
this.sockServer.close();
|
||||
}
|
||||
|
||||
// we may not get a close
|
||||
if('stdio' === this.io) {
|
||||
if ('stdio' === this.io) {
|
||||
this.restoreIo(this.doorPty);
|
||||
}
|
||||
|
||||
@@ -147,13 +157,13 @@ module.exports = class Door {
|
||||
}
|
||||
|
||||
restoreIo(piped) {
|
||||
if(!this.restored) {
|
||||
if(this.doorPty) {
|
||||
if (!this.restored) {
|
||||
if (this.doorPty) {
|
||||
this.doorPty.kill();
|
||||
}
|
||||
|
||||
const output = this.client.term.output;
|
||||
if(output) {
|
||||
if (output) {
|
||||
output.unpipe(piped);
|
||||
output.resume();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user