Skip to content

Commit

Permalink
chore: Make process/window testing more uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
gibson042 committed Jan 8, 2024
1 parent 6e92951 commit bc1e2dc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
31 changes: 15 additions & 16 deletions packages/env-options/src/env-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,21 @@ export const makeEnvironmentCaptor = aGlobal => {

/** @type {string} */
let setting = defaultSetting;
const globalProcess = aGlobal.process;
if (globalProcess && typeof globalProcess === 'object') {
const globalEnv = globalProcess.env;
if (globalEnv && typeof globalEnv === 'object') {
if (optionName in globalEnv) {
arrayPush(capturedEnvironmentOptionNames, optionName);
const optionValue = globalEnv[optionName];
// eslint-disable-next-line @endo/no-polymorphic-call
typeof optionValue === 'string' ||
Fail`Environment option named ${q(
optionName,
)}, if present, must have a corresponding string value, got ${q(
optionValue,
)}`;
setting = optionValue;
}
const globalProcess = aGlobal.process || undefined;
const globalEnv =
(typeof globalProcess === 'object' && globalProcess.env) || undefined;
if (typeof globalEnv === 'object') {
if (optionName in globalEnv) {
arrayPush(capturedEnvironmentOptionNames, optionName);
const optionValue = globalEnv[optionName];
// eslint-disable-next-line @endo/no-polymorphic-call
typeof optionValue === 'string' ||
Fail`Environment option named ${q(
optionName,
)}, if present, must have a corresponding string value, got ${q(
optionValue,
)}`;
setting = optionValue;
}
}
return setting;
Expand Down
2 changes: 1 addition & 1 deletion packages/ses/src/error/fatal-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let abandon;
// below). Currently it only checks for the `process.abort` or `process.exit`
// found on Node. It should also sniff for a vat terminating function expected
// to be found within the start compartment of SwingSet vats. What else?
if (typeof process === 'object') {
if (typeof process === 'object' && process) {
abandon = process.abort || process.exit;
}
let raise;
Expand Down
25 changes: 11 additions & 14 deletions packages/ses/src/error/tame-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ export const tameConsole = (
/* eslint-disable @endo/no-polymorphic-call */

// Node.js
const globalProcess = globalThis.process;
const globalProcess = globalThis.process || undefined;
if (
errorTrapping !== 'none' &&
globalProcess &&
typeof globalProcess === 'object' &&
typeof globalProcess.on === 'function'
) {
Expand All @@ -112,10 +111,8 @@ export const tameConsole = (
}
});
}

if (
unhandledRejectionTrapping !== 'none' &&
globalProcess &&
typeof globalProcess === 'object' &&
typeof globalProcess.on === 'function'
) {
Expand All @@ -134,25 +131,25 @@ export const tameConsole = (
}

// Browser
const globalWindow = globalThis.window || undefined;
if (
errorTrapping !== 'none' &&
globalThis.window !== undefined &&
globalThis.window.addEventListener !== undefined
typeof globalWindow === 'object' &&
typeof globalWindow.addEventListener === 'function'
) {
globalThis.window.addEventListener('error', event => {
globalWindow.addEventListener('error', event => {
event.preventDefault();
// 'platform' and 'report' just log the reason.
ourConsole.error(event.error);
if (errorTrapping === 'exit' || errorTrapping === 'abort') {
globalThis.window.location.href = `about:blank`;
globalWindow.location.href = `about:blank`;
}
});
}

if (
unhandledRejectionTrapping !== 'none' &&
globalThis.window !== undefined &&
globalThis.window.addEventListener !== undefined
typeof globalWindow === 'object' &&
typeof globalWindow.addEventListener === 'function'
) {
const handleRejection = reason => {
ourConsole.error('SES_UNHANDLED_REJECTION:', reason);
Expand All @@ -161,17 +158,17 @@ export const tameConsole = (
const h = makeRejectionHandlers(handleRejection);
if (h) {
// Rejection handlers are supported.
globalThis.window.addEventListener('unhandledrejection', event => {
globalWindow.addEventListener('unhandledrejection', event => {
event.preventDefault();
h.unhandledRejectionHandler(event.reason, event.promise);
});

globalThis.window.addEventListener('rejectionhandled', event => {
globalWindow.addEventListener('rejectionhandled', event => {
event.preventDefault();
h.rejectionHandledHandler(event.promise);
});

globalThis.window.addEventListener('beforeunload', _event => {
globalWindow.addEventListener('beforeunload', _event => {
h.processTerminationHandler();
});
}
Expand Down
10 changes: 4 additions & 6 deletions packages/ses/src/tame-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ export function tameDomains(domainTaming = 'safe') {
}

// Protect against the hazard presented by Node.js domains.
if (typeof globalThis.process === 'object' && globalThis.process !== null) {
const globalProcess = globalThis.process || undefined;
if (typeof globalProcess === 'object') {
// Check whether domains were initialized.
const domainDescriptor = getOwnPropertyDescriptor(
globalThis.process,
'domain',
);
const domainDescriptor = getOwnPropertyDescriptor(globalProcess, 'domain');
if (domainDescriptor !== undefined && domainDescriptor.get !== undefined) {
// The domain descriptor on Node.js initially has value: null, which
// becomes a get, set pair after domains initialize.
Expand All @@ -37,7 +35,7 @@ export function tameDomains(domainTaming = 'safe') {
// The domain module merely throws an exception when it attempts to define
// the domain property of the process global during its initialization.
// We have no better recourse because Node.js uses defineProperty too.
defineProperty(globalThis.process, 'domain', {
defineProperty(globalProcess, 'domain', {
value: null,
configurable: false,
writable: false,
Expand Down

0 comments on commit bc1e2dc

Please sign in to comment.