From a2fd85145b6c0e316b2205090ba49b60974d10e0 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Wed, 11 Oct 2023 21:29:55 -0400 Subject: [PATCH] fix(ses): Fake a good-enough console Fix #1819 --- packages/ses/src/error/tame-console.js | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/ses/src/error/tame-console.js b/packages/ses/src/error/tame-console.js index 5e81cb61ef..000f08207f 100644 --- a/packages/ses/src/error/tame-console.js +++ b/packages/ses/src/error/tame-console.js @@ -1,19 +1,48 @@ // @ts-check -import { TypeError, globalThis } from '../commons.js'; +import { + TypeError, + apply, + defineProperty, + freeze, + globalThis, +} from '../commons.js'; import { loggedErrorHandler as defaultHandler } from './assert.js'; import { makeCausalConsole } from './console.js'; import { makeRejectionHandlers } from './unhandled-rejection.js'; import './types.js'; import './internal-types.js'; +const wrapLogger = (logger, thisArg) => + freeze((...args) => apply(logger, thisArg, args)); + // eslint-disable-next-line no-restricted-globals const originalConsole = /** @type {VirtualConsole} */ ( + // eslint-disable-next-line no-nested-ternary typeof console !== 'undefined' ? console + : typeof print === 'function' + ? // Make a good-enough console for eshost (including only functions that + // log at a specific level with no special argument interpretation). + // https://console.spec.whatwg.org/#logging + (p => freeze({ debug: p, log: p, info: p, warn: p, error: p }))( + // eslint-disable-next-line no-undef + wrapLogger(print), + ) : undefined ); +// Upgrade a log-only console (as in `eshost -h SpiderMonkey`). +if (originalConsole && originalConsole.log) { + for (const methodName of ['warn', 'error']) { + if (!originalConsole[methodName]) { + defineProperty(originalConsole, methodName, { + value: wrapLogger(originalConsole.log, originalConsole), + }); + } + } +} + /** * Wrap console unless suppressed. * At the moment, the console is considered a host power in the start