@travetto/base
Advanced tools
Comparing version 0.0.24 to 0.0.29
@@ -10,4 +10,5 @@ { | ||
"glob": "^7.1.2", | ||
"trace": "^3.1.0", | ||
"tslint": "^5.7.0", | ||
"typescript": "^2.5.2" | ||
"typescript": "^2.7.2" | ||
}, | ||
@@ -22,3 +23,3 @@ "description": "", | ||
"scripts": {}, | ||
"version": "0.0.24" | ||
"version": "0.0.29" | ||
} |
@@ -1,102 +0,32 @@ | ||
import * as async_hooks from 'async_hooks'; | ||
import * as fs from 'fs'; | ||
import { AppEnv } from './env'; | ||
let ogPrep: any; | ||
let ogName = __filename.replace(/\.js$/, ''); | ||
const FILTERS = [ | ||
__filename.replace(/\.js$/, ''), | ||
'timers.js', | ||
'async_hooks', | ||
'module.js', | ||
'(native)', | ||
'<anonymous>', | ||
'source-map-support.js' | ||
] | ||
class StackTraceListener { | ||
currentId: number = -1; | ||
stackSeparator = '[Continued From]\n'; | ||
stackMap = new Map<number, string[]>(); | ||
customPrep: any; | ||
filters = [ | ||
ogName, | ||
'timers.js', | ||
'async_hooks', | ||
'module.js', | ||
'(native)', | ||
'<anonymous>', | ||
'source-map-support.js' | ||
]; | ||
if (!AppEnv.prod) { | ||
require('trace'); | ||
const chain = require('stack-chain'); | ||
chain.filter.attach(function (error: Error, frames: any[]) { | ||
// Filter out traces related to this file | ||
const rewrite = frames.filter(function (callSite) { | ||
let name: string = callSite.getFileName() || ''; | ||
for (let f of FILTERS) { | ||
if (name.includes(f)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
constructor() { | ||
this.init = this.init.bind(this); | ||
this.destroy = this.destroy.bind(this); | ||
this.customPrep = (err: any, stck: any) => this.prepareStackTrace(ogPrep ? ogPrep(err, stck) : err, stck); | ||
} | ||
addFilters(filters: string[]) { | ||
this.filters.push(...filters); | ||
} | ||
log(msg: string) { | ||
fs.writeSync(1, msg + '\n'); | ||
} | ||
filterErrorStack(err: string, removeHead: boolean) { | ||
const [head, ...rest] = (err || '').split('\n'); | ||
return (removeHead ? '' : `${head}\n`) + (rest | ||
.filter(l => !this.filters.find(f => l.includes(f))) | ||
.map(x => x.replace(/ (Function|Proxy)\./, ' <Proxied>.')) | ||
.join('\n')); | ||
} | ||
prepareStackTrace(err: any, stack: any) { | ||
let parentId = async_hooks.executionAsyncId() || this.currentId; | ||
let errMsg = (typeof err === 'string' ? err : err.stack) || ''; | ||
errMsg = this.filterErrorStack(errMsg, false); | ||
if (this.stackMap.has(parentId)) { | ||
errMsg = [errMsg, ...this.stackMap.get(parentId)!].filter(x => !!x.trim()).slice(0, 6) | ||
.join('\n' + this.stackSeparator); | ||
} | ||
if (typeof err === 'string') { | ||
err = errMsg; | ||
} else { | ||
err.stack = errMsg; | ||
} | ||
return err; | ||
} | ||
init(id: number, type: string, triggerAsyncId: number, resource: any) { | ||
if ((Error as any).prepareStackTrace !== this.customPrep) { | ||
ogPrep = (Error as any).prepareStackTrace; | ||
(Error as any).prepareStackTrace = this.customPrep; | ||
} | ||
let stack: any = {}; | ||
let prep: any = (Error as any).prepareStackTrace; | ||
(Error as any).prepareStackTrace = ogPrep; | ||
Error.captureStackTrace(stack); | ||
(Error as any).prepareStackTrace = prep; | ||
let parentId = triggerAsyncId || this.currentId; | ||
let parent = this.stackMap.get(parentId)! || []; | ||
let frame = this.filterErrorStack(stack.stack, true); | ||
this.stackMap.set(id, parent[0] === frame ? parent : [frame, ...parent]); | ||
} | ||
before(id: number) { | ||
this.currentId = id; | ||
} | ||
after(id: number) { | ||
this.currentId = -1; | ||
} | ||
destroy(id: number) { | ||
this.stackMap.delete(id); | ||
} | ||
return rewrite; | ||
}); | ||
} | ||
let listener: StackTraceListener; | ||
if (!AppEnv.prod) { | ||
async_hooks.createHook(listener = new StackTraceListener()).enable(); | ||
} | ||
// Log unhandled rejections | ||
@@ -108,5 +38,5 @@ process.on('unhandledRejection', (reason, p) => { | ||
export function addStackFilters(...names: string[]) { | ||
if (listener) { | ||
listener.addFilters(names); | ||
if (FILTERS) { | ||
FILTERS.push(...names); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10754
6
16
333
+ Addedtrace@^3.1.0
+ Addedstack-chain@2.0.0(transitive)
+ Addedtrace@3.2.0(transitive)
Updatedtypescript@^2.7.2