-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
56 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 +1,29 @@ | ||
import { type StackTrace } from './types'; | ||
import { parse as parseStackTrace } from 'stacktrace-parser'; | ||
|
||
export const getStackTrace = (): StackTrace => { | ||
const oldStackTraceLimit = Error.stackTraceLimit; | ||
const oldPrepareStackTrace = Error.prepareStackTrace; | ||
|
||
Error.prepareStackTrace = (error, structuredStackTrace) => { | ||
return structuredStackTrace; | ||
}; | ||
|
||
const honeypot: { stack: NodeJS.CallSite[] } = { | ||
stack: [], | ||
}; | ||
|
||
Error.captureStackTrace(honeypot); | ||
|
||
const callSites = honeypot.stack; | ||
|
||
Error.stackTraceLimit = oldStackTraceLimit; | ||
Error.prepareStackTrace = oldPrepareStackTrace; | ||
|
||
const trail: readonly NodeJS.CallSite[] = callSites.slice(1); | ||
|
||
return { | ||
callSites: trail.map((callSite) => { | ||
// The reason we are parsing the stack trace rather than | ||
// using captureStackTrace is because captureStackTrace | ||
// does not resolve source maps, i.e. the stack trace | ||
// will contain the compiled code references rather than | ||
// the original source code references. | ||
// | ||
// eslint-disable-next-line unicorn/error-message | ||
const stackTrace = new Error().stack; | ||
|
||
if (!stackTrace) { | ||
throw new Error('Could not get stack trace'); | ||
} | ||
|
||
return parseStackTrace(stackTrace) | ||
.map((stackFrame) => { | ||
return { | ||
columnNumber: callSite.getColumnNumber(), | ||
fileName: callSite.getFileName() ?? null, | ||
functionName: callSite.getFunctionName(), | ||
lineNumber: callSite.getLineNumber(), | ||
arguments: stackFrame.arguments, | ||
columnNumber: stackFrame.column, | ||
fileName: stackFrame.file, | ||
functionName: stackFrame.methodName, | ||
lineNumber: stackFrame.lineNumber, | ||
}; | ||
}), | ||
}; | ||
}) | ||
.slice(1); | ||
}; |
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,3 +1,3 @@ | ||
export { getStackTrace } from './getStackTrace'; | ||
export { serializeStackTrace } from './serializeStackTrace'; | ||
export { CallSite, StackTrace } from './types'; | ||
export { StackFrame, StackTrace } from './types'; |
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,10 +1,9 @@ | ||
export type CallSite = { | ||
export type StackFrame = { | ||
arguments: readonly string[]; | ||
columnNumber: number | null; | ||
fileName: string | null; | ||
functionName: string | null; | ||
lineNumber: number | null; | ||
}; | ||
|
||
export type StackTrace = { | ||
callSites: CallSite[]; | ||
}; | ||
export type StackTrace = StackFrame[]; |