Skip to content

Commit a57d7f1

Browse files
Patch console methods if --stdio is used (#1301)
* 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]>
1 parent 3dab7ac commit a57d7f1

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

server/src/node/main.ts

+74-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* ------------------------------------------------------------------------------------------ */
55
/// <reference path="../../typings/thenable.d.ts" />
66

7+
import { inspect } from 'node:util';
8+
79
import * as Is from '../common/utils/is';
810
import { Connection, _, _Connection, Features, WatchDog, createConnection as createCommonConnection } from '../common/server';
911

@@ -190,6 +192,7 @@ function _createConnection<PConsole = _, PTracer = _, PTelemetry = _, PClient =
190192
options?: ConnectionStrategy | ConnectionOptions,
191193
factories?: Features<PConsole, PTracer, PTelemetry, PClient, PWindow, PWorkspace, PLanguages, PNotebooks>,
192194
): _Connection<PConsole, PTracer, PTelemetry, PClient, PWindow, PWorkspace, PLanguages, PNotebooks> {
195+
let stdio = false;
193196
if (!input && !output && process.argv.length > 2) {
194197
let port: number | undefined = void 0;
195198
let pipeName: string | undefined = void 0;
@@ -201,6 +204,7 @@ function _createConnection<PConsole = _, PTracer = _, PTelemetry = _, PClient =
201204
output = new IPCMessageWriter(process);
202205
break;
203206
} else if (arg === '--stdio') {
207+
stdio = true;
204208
input = process.stdin;
205209
output = process.stdout;
206210
break;
@@ -255,7 +259,76 @@ function _createConnection<PConsole = _, PTracer = _, PTelemetry = _, PClient =
255259

256260
const connectionFactory = (logger: Logger): ProtocolConnection => {
257261
const result = createProtocolConnection(input as any, output as any, logger, options);
262+
if(stdio) {
263+
patchConsole(logger);
264+
}
258265
return result;
259266
};
260267
return createCommonConnection(connectionFactory, watchDog, factories);
261-
}
268+
}
269+
270+
function patchConsole(logger: Logger): undefined {
271+
function serialize(args: unknown[]) {
272+
return args.map(arg => typeof arg === 'string' ? arg : inspect(arg)).join(' ');
273+
}
274+
275+
const counters = new Map<string, number>();
276+
277+
console.assert = function assert(assertion, ...args) {
278+
if (assertion) {
279+
return;
280+
}
281+
if (args.length === 0) {
282+
logger.error('Assertion failed');
283+
} else {
284+
const [message, ...rest] = args;
285+
logger.error(`Assertion failed: ${message} ${serialize(rest)}`);
286+
}
287+
};
288+
289+
console.count = function count(label = 'default') {
290+
const message = String(label);
291+
let counter = counters.get(message) ?? 0;
292+
counter += 1;
293+
counters.set(message, counter);
294+
logger.log(`${message}: ${message}`);
295+
};
296+
297+
console.countReset = function countReset(label) {
298+
if(label === undefined) {
299+
counters.clear();
300+
} else {
301+
counters.delete(String(label));
302+
}
303+
};
304+
305+
console.debug = function debug(...args) {
306+
logger.log(serialize(args));
307+
};
308+
309+
console.dir = function dir(arg, options){
310+
// @ts-expect-error https://github.com/DefinitelyTyped/DefinitelyTyped/pull/66626
311+
logger.log(inspect(arg, options));
312+
};
313+
314+
console.log = function log(...args) {
315+
logger.log(serialize(args));
316+
};
317+
318+
console.error = function error(...args){
319+
logger.error(serialize(args));
320+
};
321+
322+
console.trace = function trace(...args) {
323+
const stack = new Error().stack!.replace(/(.+\n){2}/, '');
324+
let message = 'Trace';
325+
if (args.length !== 0) {
326+
message += `: ${serialize(args)}`;
327+
}
328+
logger.log(`${message}\n${stack}`);
329+
};
330+
331+
console.warn = function warn(...args) {
332+
logger.warn(serialize(args));
333+
};
334+
}

0 commit comments

Comments
 (0)