@opra/common
Version:
Opra common package
52 lines (51 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStackFileName = getStackFileName;
exports.getErrorStack = getErrorStack;
const PATH_PATTERN = /^(?:file:\/\/)?(.+)$/;
function getStackFileName(position = 1) {
if (position >= Error.stackTraceLimit) {
throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' +
position +
'` and Error.stackTraceLimit was: `' +
Error.stackTraceLimit +
'`');
}
const oldPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
const stack = new Error().stack;
Error.prepareStackTrace = oldPrepareStackTrace;
if (stack !== null && typeof stack === 'object') {
// stack[0] holds this file
// stack[1] holds where this function was called
const s = stack[position]
? stack[position].getFileName()
: undefined;
const m = s ? PATH_PATTERN.exec(s) : undefined;
return m ? m[1] : '';
}
return '';
}
function getErrorStack(position = 1) {
if (position >= Error.stackTraceLimit) {
throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' +
position +
'` and Error.stackTraceLimit was: `' +
Error.stackTraceLimit +
'`');
}
const oldPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
const stack = new Error().stack;
Error.prepareStackTrace = oldPrepareStackTrace;
if (stack !== null && typeof stack === 'object') {
// stack[0] holds this file
// stack[1] holds where this function was called
const s = stack[position]
? stack[position].getFileName()
: undefined;
const m = s ? PATH_PATTERN.exec(s) : undefined;
return m ? m[1] : '';
}
return '';
}