Use ts-check in server and start
This commit is contained in:
+41
-22
@@ -4,12 +4,14 @@
|
||||
* demo to the public as is would introduce security risks for the host.
|
||||
**/
|
||||
|
||||
var express = require('express');
|
||||
var expressWs = require('express-ws');
|
||||
var os = require('os');
|
||||
var pty = require('node-pty');
|
||||
// @ts-check
|
||||
|
||||
// Whether to use binary transport.
|
||||
const express = require('express');
|
||||
const expressWs = require('express-ws');
|
||||
const os = require('os');
|
||||
const pty = require('node-pty');
|
||||
|
||||
/** Whether to use binary transport. */
|
||||
const USE_BINARY = os.platform() !== "win32";
|
||||
|
||||
function startServer() {
|
||||
@@ -41,18 +43,30 @@ function startServer() {
|
||||
app.use('/src', express.static(__dirname + '/src'));
|
||||
|
||||
app.post('/terminals', (req, res) => {
|
||||
const env = Object.assign({}, process.env);
|
||||
/** @type {{ [key: string]: string }} */
|
||||
const env = {};
|
||||
for (const k of Object.keys(process.env)) {
|
||||
const v = process.env[k];
|
||||
if (v) {
|
||||
env[k] = v;
|
||||
}
|
||||
}
|
||||
// const env = Object.assign({}, process.env);
|
||||
env['COLORTERM'] = 'truecolor';
|
||||
var cols = parseInt(req.query.cols),
|
||||
rows = parseInt(req.query.rows),
|
||||
term = pty.spawn(process.platform === 'win32' ? 'pwsh.exe' : 'bash', [], {
|
||||
name: 'xterm-256color',
|
||||
cols: cols || 80,
|
||||
rows: rows || 24,
|
||||
cwd: process.platform === 'win32' ? undefined : env.PWD,
|
||||
env: env,
|
||||
encoding: USE_BINARY ? null : 'utf8'
|
||||
});
|
||||
if (typeof req.query.cols !== 'string' || typeof req.query.rows !== 'string') {
|
||||
console.error({ req });
|
||||
throw new Error('Unexpected query args');
|
||||
}
|
||||
const cols = parseInt(req.query.cols);
|
||||
const rows = parseInt(req.query.rows);
|
||||
const term = pty.spawn(process.platform === 'win32' ? 'pwsh.exe' : 'bash', [], {
|
||||
name: 'xterm-256color',
|
||||
cols: cols ?? 80,
|
||||
rows: rows ?? 24,
|
||||
cwd: process.platform === 'win32' ? undefined : env.PWD,
|
||||
env,
|
||||
encoding: USE_BINARY ? null : 'utf8'
|
||||
});
|
||||
|
||||
console.log('Created terminal with PID: ' + term.pid);
|
||||
terminals[term.pid] = term;
|
||||
@@ -65,16 +79,21 @@ function startServer() {
|
||||
});
|
||||
|
||||
app.post('/terminals/:pid/size', (req, res) => {
|
||||
var pid = parseInt(req.params.pid),
|
||||
cols = parseInt(req.query.cols),
|
||||
rows = parseInt(req.query.rows),
|
||||
term = terminals[pid];
|
||||
if (typeof req.query.cols !== 'string' || typeof req.query.rows !== 'string') {
|
||||
console.error({ req });
|
||||
throw new Error('Unexpected query args');
|
||||
}
|
||||
const pid = parseInt(req.params.pid);
|
||||
const cols = parseInt(req.query.cols);
|
||||
const rows = parseInt(req.query.rows);
|
||||
const term = terminals[pid];
|
||||
|
||||
term.resize(cols, rows);
|
||||
console.log('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.');
|
||||
res.end();
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
app.ws('/terminals/:pid', function (ws, req) {
|
||||
var term = terminals[parseInt(req.params.pid)];
|
||||
console.log('Connected to terminal ' + term.pid);
|
||||
@@ -160,11 +179,11 @@ function startServer() {
|
||||
});
|
||||
});
|
||||
|
||||
var port = process.env.PORT || 3000,
|
||||
var port = parseInt(process.env.PORT ?? '3000'),
|
||||
host = os.platform() === 'win32' ? '127.0.0.1' : '0.0.0.0';
|
||||
|
||||
console.log('App listening to http://127.0.0.1:' + port);
|
||||
app.listen(port, host);
|
||||
app.listen(port, host, 0);
|
||||
}
|
||||
|
||||
module.exports = startServer;
|
||||
|
||||
Reference in New Issue
Block a user