Skip to content

Commit

Permalink
fix(ses): Fake a good-enough console
Browse files Browse the repository at this point in the history
Fix #1819
  • Loading branch information
gibson042 committed Oct 12, 2023
1 parent fece445 commit a2fd851
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/ses/src/error/tame-console.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit a2fd851

Please sign in to comment.