Skip to content

Commit

Permalink
Patch console methods if --stdio is used
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Aug 22, 2023
1 parent 7899568 commit 1e27b0d
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion server/src/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* ------------------------------------------------------------------------------------------ */
/// <reference path="../../typings/thenable.d.ts" />

import { inspect } from 'node:util';

import * as Is from '../common/utils/is';
import { Connection, _, _Connection, Features, WatchDog, createConnection as createCommonConnection } from '../common/server';

Expand Down Expand Up @@ -190,6 +192,7 @@ function _createConnection<PConsole = _, PTracer = _, PTelemetry = _, PClient =
options?: ConnectionStrategy | ConnectionOptions,
factories?: Features<PConsole, PTracer, PTelemetry, PClient, PWindow, PWorkspace, PLanguages, PNotebooks>,
): _Connection<PConsole, PTracer, PTelemetry, PClient, PWindow, PWorkspace, PLanguages, PNotebooks> {
let stdio = false;
if (!input && !output && process.argv.length > 2) {
let port: number | undefined = void 0;
let pipeName: string | undefined = void 0;
Expand All @@ -201,6 +204,7 @@ function _createConnection<PConsole = _, PTracer = _, PTelemetry = _, PClient =
output = new IPCMessageWriter(process);
break;
} else if (arg === '--stdio') {
stdio = true;
input = process.stdin;
output = process.stdout;
break;
Expand Down Expand Up @@ -255,7 +259,40 @@ function _createConnection<PConsole = _, PTracer = _, PTelemetry = _, PClient =

const connectionFactory = (logger: Logger): ProtocolConnection => {
const result = createProtocolConnection(input as any, output as any, logger, options);
if(stdio) {
patchConsole(logger);
}
return result;
};
return createCommonConnection(connectionFactory, watchDog, factories);
}
}

function patchConsole(logger: Logger): undefined {
function serialize(args: unknown[]) {
return args.map(arg => typeof arg === 'string' ? arg : inspect(arg)).join(' ');
}

console.assert = (assertion, ...args) => {
if (assertion) {
return;
}
if (args.length === 0) {
logger.error('Assertion failed');
} else {
const [message, ...rest] = args;
logger.error(`Assertion failed: ${message} ${serialize(rest)}`);
}
};
console.dir = function dir(arg){
logger.log(inspect(arg));
};
console.log = function log(...args) {
logger.log(serialize(args));
};
console.error = function error(...args){
logger.error(serialize(args));
};
console.warn = function warn(...args) {
logger.warn(serialize(args));
};
}

0 comments on commit 1e27b0d

Please sign in to comment.