Skip to content

Commit

Permalink
fix: Support absence of TextEncoder/TextDecoder/console
Browse files Browse the repository at this point in the history
Merge pull request #1820 from endojs/gibson-1819-lockdown-eshost
Fixes #1819
  • Loading branch information
gibson042 committed Oct 12, 2023
2 parents 877be98 + a2fd851 commit 41e1215
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/lockdown/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default () => {
// this assignment is not rejected, even if it does nothing.
Error.stackTraceLimit = Infinity;

harden(TextEncoder);
harden(TextDecoder);
harden(globalThis.TextEncoder); // Absent in eshost
harden(globalThis.TextDecoder); // Absent in eshost
harden(globalThis.URL); // Absent only on XSnap
harden(globalThis.Base64); // Present only on XSnap
};
4 changes: 4 additions & 0 deletions packages/ses/src/error/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ freeze(ErrorInfo);

/** @type {MakeCausalConsole} */
const makeCausalConsole = (baseConsole, loggedErrorHandler) => {
if (!baseConsole) {
return undefined;
}

const { getStackString, tagError, takeMessageLogArgs, takeNoteLogArgsArray } =
loggedErrorHandler;

Expand Down
4 changes: 2 additions & 2 deletions packages/ses/src/error/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
* calls methods of the `loggedErrorHandler` to customize how it handles logged
* errors.
*
* @param {VirtualConsole} baseConsole
* @param {VirtualConsole | undefined} baseConsole
* @param {LoggedErrorHandler} loggedErrorHandler
* @returns {VirtualConsole}
* @returns {VirtualConsole | undefined}
*/
42 changes: 38 additions & 4 deletions packages/ses/src/error/tame-console.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
// @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 = console;
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.
Expand Down Expand Up @@ -40,10 +73,11 @@ export const tameConsole = (
getStackString: optGetStackString,
};
}
const ourConsole =
const ourConsole = /** @type {VirtualConsole} */ (
consoleTaming === 'unsafe'
? originalConsole
: makeCausalConsole(originalConsole, loggedErrorHandler);
: makeCausalConsole(originalConsole, loggedErrorHandler)
);

// Attach platform-specific error traps such that any error that gets thrown
// at top-of-turn (the bottom of stack) will get logged by our causal
Expand Down

0 comments on commit 41e1215

Please sign in to comment.