-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ses): Support an incomplete shimmed globalThis.process #1923
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6e92951
fix(ses): Support an incomplete shimmed globalEnv.process
gibson042 bc1e2dc
chore: Make process/window testing more uniform
gibson042 5d637d0
feat(ses): Fail fast when a required process.exit or process.abort me…
gibson042 08cbae5
style(ses): DRY out a failFast helper function
gibson042 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// @ts-check | ||
|
||
import { | ||
// Using TypeError minimizes risk of exposing the feral Error constructor | ||
TypeError, | ||
apply, | ||
defineProperty, | ||
|
@@ -13,6 +14,10 @@ import { makeRejectionHandlers } from './unhandled-rejection.js'; | |
import './types.js'; | ||
import './internal-types.js'; | ||
|
||
const failFast = message => { | ||
throw TypeError(message); | ||
}; | ||
|
||
const wrapLogger = (logger, thisArg) => | ||
freeze((...args) => apply(logger, thisArg, args)); | ||
|
||
|
@@ -60,9 +65,9 @@ export const tameConsole = ( | |
unhandledRejectionTrapping = 'report', | ||
optGetStackString = undefined, | ||
) => { | ||
if (consoleTaming !== 'safe' && consoleTaming !== 'unsafe') { | ||
throw TypeError(`unrecognized consoleTaming ${consoleTaming}`); | ||
} | ||
consoleTaming === 'safe' || | ||
consoleTaming === 'unsafe' || | ||
failFast(`unrecognized consoleTaming ${consoleTaming}`); | ||
|
||
let loggedErrorHandler; | ||
if (optGetStackString === undefined) { | ||
|
@@ -95,21 +100,36 @@ export const tameConsole = ( | |
/* eslint-disable @endo/no-polymorphic-call */ | ||
|
||
// Node.js | ||
if (errorTrapping !== 'none' && globalThis.process !== undefined) { | ||
globalThis.process.on('uncaughtException', error => { | ||
const globalProcess = globalThis.process || undefined; | ||
if ( | ||
errorTrapping !== 'none' && | ||
typeof globalProcess === 'object' && | ||
typeof globalProcess.on === 'function' | ||
mhofman marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should test for existence of abort and exit as well. |
||
) { | ||
let terminate; | ||
if (errorTrapping === 'platform' || errorTrapping === 'exit') { | ||
const { exit } = globalProcess; | ||
// If there is a function-valued process.on but no function-valued process.exit, | ||
// fail early without caring whether errorTrapping is "platform" only by default. | ||
typeof exit === 'function' || failFast('missing process.exit'); | ||
terminate = () => exit(globalProcess.exitCode || -1); | ||
} else if (errorTrapping === 'abort') { | ||
terminate = globalProcess.abort; | ||
typeof terminate === 'function' || failFast('missing process.abort'); | ||
} | ||
|
||
globalProcess.on('uncaughtException', error => { | ||
// causalConsole is born frozen so not vulnerable to method tampering. | ||
ourConsole.error(error); | ||
if (errorTrapping === 'platform' || errorTrapping === 'exit') { | ||
globalThis.process.exit(globalThis.process.exitCode || -1); | ||
} else if (errorTrapping === 'abort') { | ||
globalThis.process.abort(); | ||
if (terminate) { | ||
terminate(); | ||
} | ||
}); | ||
} | ||
|
||
if ( | ||
unhandledRejectionTrapping !== 'none' && | ||
globalThis.process !== undefined | ||
typeof globalProcess === 'object' && | ||
typeof globalProcess.on === 'function' | ||
) { | ||
const handleRejection = reason => { | ||
// 'platform' and 'report' just log the reason. | ||
|
@@ -119,32 +139,32 @@ export const tameConsole = ( | |
const h = makeRejectionHandlers(handleRejection); | ||
if (h) { | ||
// Rejection handlers are supported. | ||
globalThis.process.on('unhandledRejection', h.unhandledRejectionHandler); | ||
globalThis.process.on('rejectionHandled', h.rejectionHandledHandler); | ||
globalThis.process.on('exit', h.processTerminationHandler); | ||
globalProcess.on('unhandledRejection', h.unhandledRejectionHandler); | ||
globalProcess.on('rejectionHandled', h.rejectionHandledHandler); | ||
globalProcess.on('exit', h.processTerminationHandler); | ||
} | ||
} | ||
|
||
// 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); | ||
|
@@ -153,17 +173,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(); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* global globalThis */ | ||
|
||
import test from 'ava'; | ||
import '../index.js'; | ||
|
||
test('shimmed globalThis.process', t => { | ||
const process = {}; | ||
Object.defineProperty(globalThis, 'process', { | ||
value: process, | ||
configurable: false, | ||
writable: false, | ||
}); | ||
t.is(globalThis.process, process); | ||
t.is(globalThis.process.on, undefined); | ||
lockdown({ | ||
consoleTaming: 'safe', | ||
errorTrapping: 'platform', | ||
unhandledRejectionTrapping: 'report', | ||
}); | ||
t.is(globalThis.process, process); | ||
t.is(globalThis.process.on, undefined); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this regresses if
env === null
. Unlikely, I’m sure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't given the
|| undefined