Skip to content

Commit

Permalink
Patch console methods if --stdio is used (#1301)
Browse files Browse the repository at this point in the history
* Patch console methods if --stdio is used

Closes #1299

* Fix stylistic issue

* Implement more console methods

This implements `console.count`, `console.counrReset`, `console.debug`,
and `console.trace`. It also adds support for `console.dir` options.

---------

Co-authored-by: Dirk Bäumer <[email protected]>
  • Loading branch information
remcohaszing and dbaeumer authored Sep 18, 2023
1 parent 3dab7ac commit a57d7f1
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 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,76 @@ 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(' ');
}

const counters = new Map<string, number>();

console.assert = function 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.count = function count(label = 'default') {
const message = String(label);
let counter = counters.get(message) ?? 0;
counter += 1;
counters.set(message, counter);
logger.log(`${message}: ${message}`);
};

console.countReset = function countReset(label) {
if(label === undefined) {
counters.clear();
} else {
counters.delete(String(label));
}
};

console.debug = function debug(...args) {
logger.log(serialize(args));
};

console.dir = function dir(arg, options){
// @ts-expect-error https://github.com/DefinitelyTyped/DefinitelyTyped/pull/66626
logger.log(inspect(arg, options));
};

console.log = function log(...args) {
logger.log(serialize(args));
};

console.error = function error(...args){
logger.error(serialize(args));
};

console.trace = function trace(...args) {
const stack = new Error().stack!.replace(/(.+\n){2}/, '');
let message = 'Trace';
if (args.length !== 0) {
message += `: ${serialize(args)}`;
}
logger.log(`${message}\n${stack}`);
};

console.warn = function warn(...args) {
logger.warn(serialize(args));
};
}

0 comments on commit a57d7f1

Please sign in to comment.