Skip to content

Commit

Permalink
wip(panel/console): custom tx markers + deprecated srvCmdBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Jan 11, 2024
1 parent aedab85 commit c3430c2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 53 deletions.
5 changes: 0 additions & 5 deletions core/components/FxRunner/outputHandler.ts
Expand Up @@ -28,8 +28,6 @@ type StructuredTraceType = {
*/
export default class OutputHandler {
readonly #txAdmin: TxAdmin;
enableCmdBuffer = false;
cmdBuffer = '';

constructor(txAdmin: TxAdmin) {
this.#txAdmin = txAdmin;
Expand Down Expand Up @@ -165,8 +163,5 @@ export default class OutputHandler {
write(source: string, mutex: string, data: string | Buffer) {
data = data.toString();
this.#txAdmin.logger.fxserver.writeStdIO(source, data);

//FIXME: deprecate this whenever
if (this.enableCmdBuffer) this.cmdBuffer += data.replace(/\u001b[^m]*?m/g, '');
}
};
59 changes: 38 additions & 21 deletions core/components/Logger/handlers/fxserver.js
Expand Up @@ -7,22 +7,39 @@ const console = consoleFactory(modulename);


//NOTE: There used to be a rule "\x0B-\x1F" that was replaced with "x0B-\x1A\x1C-\x1F" to allow the \x1B terminal escape character.
//NOTE: There used to be a rule for the TM symbol (\u2122), but removed because probably not needed
//NOTE: There used to be a rule "\x7F-\x9F", but removed (except \x7F) because probably not needed
//This is neccessary for the terminal to have color, but beware of side effects.
//This regex was done in the first place to prevent fxserver output to be interpreted as txAdmin output by the host terminal
//IIRC the issue was that one user with a TM on their nick was making txAdmin's console to close or freeze. I couldn't reproduce the issue.
// \x00-\x08 control chars
// \x00-\x08 Control characters in the ASCII table.
// allow \r and \t
// \x0B-\x1A control chars
// allow \e (escape for colors)
// \x1C-\x1F control chars
// \x0B-\x1A Vertical tab and control characters from shift out to substitute.
// allow \x1B (escape for colors n stuff)
// \x1C-\x1F Control characters (file separator, group separator, record separator, unit separator).
// allow all printable
// \x7F-\x9F ??????
// \u2122 trademark symbol
const regexConsole = /[\x00-\x08\x0B-\x1A\x1C-\x1F\x7F-\x9F\x80-\x9F\u2122]/g;
const regexEscape = /\u001b[^m]*?m/g;

const markLines = (msg, type) => {
return msg.trim().split('\n').map((l) => `{txMarker-${type}}${l}{/txMarker}`).join('\n') + '\n';
// \x7F Delete character.
const regexConsole = /[\x00-\x08\x0B-\x1A\x1C-\x1F\x7F\x80-\x9F]/g;
const regexCsi = /(\u001b\[|\u009B)[\d;]+[@-K]/g;
const regexColors = /\u001b[^m]*?m/g;


const markLines = (msg, type, prefix = '') => {
let colorFunc = (x) => x;
if (type === 'cmd') {
colorFunc = chalk.bgYellowBright.black;
} else if (type === 'error') {
colorFunc = chalk.bgRedBright.black;
} else if (type === 'info') {
colorFunc = chalk.bgBlueBright.black;
} else if (type === 'ok') {
colorFunc = chalk.bgGreenBright.black;
}
const taggedPrefix = prefix ? `[${prefix}] ` : '';
return msg.trim()
.split('\n')
.map((l) => colorFunc(`${taggedPrefix}${l}`))
.join('\n') + '\n';
};


Expand Down Expand Up @@ -72,21 +89,21 @@ export default class FXServerLogger extends LoggerBase {
* Writes a marker text to the logger (file, websocket, buffer, stdout)
* @param {String} type
* @param {String} data
* @param {String} src
*/
writeMarker(type, data) {
writeMarker(type, data, src) {
if (type === 'starting') {
const msg = separator('FXServer Starting');
this.lrStream.write(`\n${msg}\n`);
if (!globals.fxRunner.config.quiet) {
process.stdout.write(`\n${chalk.bgBlue(msg)}\n`);
}
const coloredMarkData = `\n\n${markLines(msg, 'info')}\n`;
const coloredMarkData = `\n\n${markLines(msg, 'info', 'SYSTEM')}\n`;
globals.webServer.webSocket.buffer('liveconsole', coloredMarkData);
this.appendRecent(coloredMarkData);
} else if (type === 'command') {
const msg = `> ${data}`;
this.lrStream.write(`${msg}\n`);
const coloredMarkData = markLines(msg, 'cmd');
this.lrStream.write(`> ${data}\n`);
const coloredMarkData = markLines(data, 'cmd', src);
globals.webServer.webSocket.buffer('liveconsole', coloredMarkData);
this.appendRecent(coloredMarkData);
} else {
Expand All @@ -104,12 +121,12 @@ export default class FXServerLogger extends LoggerBase {
*/
writeStdIO(type, data) {
//To file
this.lrStream.write(data.replace(regexEscape, ''));
this.lrStream.write(data.replace(regexColors, ''));

//Clean data
//Removing console-breaking chars
const consoleData = data
.replace(regexConsole, '') //removing console-breaking chars
.replace(/txMarker/g, 'tx\u200BMarker'); //just to prevent resources from injecting markers
.replace(regexConsole, '')
.replace(regexCsi, '');

//For the terminal
if (!globals.fxRunner.config.quiet) {
Expand All @@ -118,7 +135,7 @@ export default class FXServerLogger extends LoggerBase {
}

//For the live console
const coloredMarkData = (type === 'stdout') ? consoleData : markLines(consoleData, 'error');
const coloredMarkData = (type === 'stdout') ? consoleData : markLines(consoleData, 'error', 'STDERR');
globals.webServer.webSocket.buffer('liveconsole', coloredMarkData);
this.appendRecent(coloredMarkData);
}
Expand Down
31 changes: 7 additions & 24 deletions core/webroutes/fxserver/commands.ts
Expand Up @@ -62,7 +62,7 @@ export default async function FXServerCommands(ctx: AuthedCtx) {
ExecuteCommand(`profiler save "${escape(savePath)}"`);
setTimeout(async () => {
console.ok(`Profile saved to: ${savePath}`);
fxRunner.srvCmd(`profiler view "${escape(savePath)}"`);
fxRunner.srvCmd(`profiler view "${escape(savePath)}"`, ctx.admin.name);
}, 150);
}, 150);
}, profileDuration * 1000);
Expand Down Expand Up @@ -108,7 +108,7 @@ export default async function FXServerCommands(ctx: AuthedCtx) {
cmd = 'txaKickAll "txAdmin Web Panel"';
}
ctx.admin.logCommand(cmd);
await fxRunner.srvCmdBuffer(cmd);
fxRunner.srvCmd(cmd, ctx.admin.name);
return ctx.send<ApiToastResp>({
type: 'success',
msg: 'Kick All command sent.',
Expand All @@ -119,7 +119,7 @@ export default async function FXServerCommands(ctx: AuthedCtx) {
if (!ensurePermission(ctx, 'commands.resources')) return false;
const cmd = formatCommand('restart', parameter);
ctx.admin.logCommand(cmd);
await fxRunner.srvCmdBuffer(cmd);
fxRunner.srvCmd(cmd, ctx.admin.name);
return ctx.send<ApiToastResp>({
type: 'warning',
msg: 'Resource restart command sent.',
Expand All @@ -130,7 +130,7 @@ export default async function FXServerCommands(ctx: AuthedCtx) {
if (!ensurePermission(ctx, 'commands.resources')) return false;
const cmd = formatCommand('start', parameter);
ctx.admin.logCommand(cmd);
await fxRunner.srvCmdBuffer(cmd);
fxRunner.srvCmd(cmd, ctx.admin.name);
return ctx.send<ApiToastResp>({
type: 'warning',
msg: 'Resource start command sent.',
Expand All @@ -141,7 +141,7 @@ export default async function FXServerCommands(ctx: AuthedCtx) {
if (!ensurePermission(ctx, 'commands.resources')) return false;
const cmd = formatCommand('ensure', parameter);
ctx.admin.logCommand(cmd);
await fxRunner.srvCmdBuffer(cmd);
fxRunner.srvCmd(cmd, ctx.admin.name);
return ctx.send<ApiToastResp>({
type: 'warning',
msg: 'Resource ensure command sent.',
Expand All @@ -152,7 +152,7 @@ export default async function FXServerCommands(ctx: AuthedCtx) {
if (!ensurePermission(ctx, 'commands.resources')) return false;
const cmd = formatCommand('stop', parameter);
ctx.admin.logCommand(cmd);
await fxRunner.srvCmdBuffer(cmd);
fxRunner.srvCmd(cmd, ctx.admin.name);
return ctx.send<ApiToastResp>({
type: 'warning',
msg: 'Resource stop command sent.',
Expand All @@ -163,29 +163,12 @@ export default async function FXServerCommands(ctx: AuthedCtx) {
if (!ensurePermission(ctx, 'commands.resources')) return false;
const cmd = 'refresh';
ctx.admin.logCommand(cmd);
await fxRunner.srvCmdBuffer(cmd);
fxRunner.srvCmd(cmd, ctx.admin.name);
return ctx.send<ApiToastResp>({
type: 'warning',
msg: 'Refresh command sent.',
});

//==============================================
} else if (action == 'check_txaclient') {
const cmd = 'txaPing';
ctx.admin.logCommand(cmd);
const buffer = await fxRunner.srvCmdBuffer(cmd, 512);
if (buffer.includes('Pong!')) {
return ctx.send<ApiToastResp>({
type: 'success',
msg: 'txAdminClient is running!',
});
} else {
return ctx.send<ApiToastResp>({
type: 'error',
msg: 'txAdminClient is not running!',
});
}

//==============================================
} else {
return ctx.send<ApiToastResp>({
Expand Down
2 changes: 1 addition & 1 deletion docs/dev_notes.md
Expand Up @@ -17,7 +17,7 @@
- [x] command history (arrows only) with local storage
- [x] socket.io connection
- [x] open web links
- [ ] deal with the console markers - maybe custom decorators?
- [x] deal with the console markers - ~~maybe custom decorators?~~
- [ ] fix z-order, cant click postpone/support on the warning bar
- [ ] tidy up the code
- [ ] Update packages for all workspaces
Expand Down
1 change: 0 additions & 1 deletion panel/src/pages/LiveConsole/LiveConsole.tsx
Expand Up @@ -19,7 +19,6 @@ import terminalOptions from "./xtermOptions";
import './xtermOverrides.css';
import '@xterm/xterm/css/xterm.css';
import { getSocket, openExternalLink } from '@/lib/utils';
import { Socket } from 'socket.io-client';


const keyDebounceTime = 150; //ms
Expand Down
1 change: 0 additions & 1 deletion panel/src/pages/LiveConsole/xtermOptions.ts
Expand Up @@ -30,7 +30,6 @@ const terminalOptions: ITerminalOptions | ITerminalInitOnlyOptions = {
cursorBlink: true,
cursorStyle: 'bar',
disableStdin: true,
drawBoldTextInBrightColors: false,
fontFamily: "JetBrains Mono Variable, monospace",
fontSize: 13,
fontWeight: "300",
Expand Down

0 comments on commit c3430c2

Please sign in to comment.