From eee669cc9cca1e1fc0f51c70bc17926610fc551e Mon Sep 17 00:00:00 2001 From: delphinus Date: Thu, 31 Mar 2016 00:25:48 +0900 Subject: [PATCH 1/8] Correct return values --- stacktrace-js/stacktrace-js.d.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/stacktrace-js/stacktrace-js.d.ts b/stacktrace-js/stacktrace-js.d.ts index aebd6ddf80..045cf70104 100644 --- a/stacktrace-js/stacktrace-js.d.ts +++ b/stacktrace-js/stacktrace-js.d.ts @@ -49,18 +49,20 @@ declare namespace StackTrace { * * @param {Function} fn to be instrumented * @param {Function} callback function to call with a stack trace on invocation - * @param {Function} errorCallback optional function to call with error if unable to get stack trace. + * @param {Function} errback optional function to call with error if unable to get stack trace. * @param {Object} thisArg optional context object (e.g. window) + * @return {Function} instrumented function */ - export function instrument(fn:() => void, callback:(stackFrames:StackFrame[]) => void, errorCallback:(error:Error) => void, thisArg?:any): void; + export function instrument(fn: TFunc, callback: (stackFrames:StackFrame[]) => void, errback?: (error: Error) => void, thisArg?: any): TFunc; /** * Given a function that has been instrumented, * revert the function to it's original (non-instrumented) state. * * @param fn {Function} + * @return {Function} original function */ - export function deinstrument(fn:() => void): void; + export function deinstrument(fn: TFunc): TFunc; /** * Given an Array of StackFrames, serialize and POST to given URL. From ee10fc38b1acd26ec5f9f484ab11fe2eac1ebc61 Mon Sep 17 00:00:00 2001 From: delphinus Date: Thu, 31 Mar 2016 00:28:23 +0900 Subject: [PATCH 2/8] Correct return values --- stacktrace-js/stacktrace-js.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stacktrace-js/stacktrace-js.d.ts b/stacktrace-js/stacktrace-js.d.ts index 045cf70104..1bb55aa5a7 100644 --- a/stacktrace-js/stacktrace-js.d.ts +++ b/stacktrace-js/stacktrace-js.d.ts @@ -69,7 +69,7 @@ declare namespace StackTrace { * * @param stackframes - Array[StackFrame] * @param url - URL as String - * @return Promise + * @return Promise */ - export function report(stackframes: StackFrame[], url: string): Promise; + export function report(stackframes: StackFrame[], url: string): Promise; } From 371b6dfbf352740dd931d4b5ff95a24886074db5 Mon Sep 17 00:00:00 2001 From: delphinus Date: Thu, 31 Mar 2016 00:28:44 +0900 Subject: [PATCH 3/8] Define stacktrace-js module --- stacktrace-js/stacktrace-js.d.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stacktrace-js/stacktrace-js.d.ts b/stacktrace-js/stacktrace-js.d.ts index 1bb55aa5a7..c4ed865400 100644 --- a/stacktrace-js/stacktrace-js.d.ts +++ b/stacktrace-js/stacktrace-js.d.ts @@ -73,3 +73,7 @@ declare namespace StackTrace { */ export function report(stackframes: StackFrame[], url: string): Promise; } + +declare module "stacktrace-js" { + export = StackTrace; +} From e42a58381c705d7670176dbc0fac7728c5b3a681 Mon Sep 17 00:00:00 2001 From: delphinus Date: Thu, 31 Mar 2016 00:31:00 +0900 Subject: [PATCH 4/8] Correct definition for options --- stacktrace-js/stacktrace-js.d.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/stacktrace-js/stacktrace-js.d.ts b/stacktrace-js/stacktrace-js.d.ts index c4ed865400..7bf7570fec 100644 --- a/stacktrace-js/stacktrace-js.d.ts +++ b/stacktrace-js/stacktrace-js.d.ts @@ -4,10 +4,21 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace StackTrace { + + export interface SourceCache { + [key: string]: string | Promise; + } + + /** + * Options for StackTrace + * @param filter Function(StackFrame => Boolean) - Only include stack entries matching for which filter returns true + * @param sourceCache Object (String URL => String Source) - Pre-populate source cache to avoid network requests + * @param offline Boolean (default: false) - Set to true to prevent all network requests + */ export interface StackTraceOptions { - filter?: (stackFrame:StackFrame) => boolean; - sourceCache?: { URL:string }; - offline?: boolean; + filter?: (stackFrame: StackFrame) => boolean; + sourceCache?: SourceCache; + offline?: boolean; } export interface StackFrame { From 48f9ebd33917a18720ce81cc2a90938b313e5f8f Mon Sep 17 00:00:00 2001 From: delphinus Date: Thu, 31 Mar 2016 00:31:23 +0900 Subject: [PATCH 5/8] Add methods for StackFrame --- stacktrace-js/stacktrace-js.d.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/stacktrace-js/stacktrace-js.d.ts b/stacktrace-js/stacktrace-js.d.ts index 7bf7570fec..a682189c09 100644 --- a/stacktrace-js/stacktrace-js.d.ts +++ b/stacktrace-js/stacktrace-js.d.ts @@ -22,14 +22,17 @@ declare namespace StackTrace { } export interface StackFrame { - constructor(functionName:string, args:any, fileName:string, lineNumber:number, columnNumber:number): StackFrame; + constructor(functionName: string, args: any, fileName: string, lineNumber: number, columnNumber: number): StackFrame; - functionName?:string; - args?:any; - fileName?:string; - lineNumber?:number; - columnNumber?:number; - toString():string; + functionName: string; + args: any; + fileName: string; + lineNumber: number; + columnNumber: number; + source: string; + isEval: boolean; + isNative: boolean; + toString(): string; } /** From 6d023860d1253397311d708bce3df48d11b4ab2f Mon Sep 17 00:00:00 2001 From: delphinus Date: Thu, 31 Mar 2016 00:31:36 +0900 Subject: [PATCH 6/8] Fix typo --- stacktrace-js/stacktrace-js.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stacktrace-js/stacktrace-js.d.ts b/stacktrace-js/stacktrace-js.d.ts index a682189c09..bc9571eb5a 100644 --- a/stacktrace-js/stacktrace-js.d.ts +++ b/stacktrace-js/stacktrace-js.d.ts @@ -48,7 +48,7 @@ declare namespace StackTrace { * @param options Object for options * @return Array[StackFrame] */ - export function fromError(error:Error, options?:StackTraceOptions): Promise; + export function fromError(error: Error, options?: StackTraceOptions): Promise; /** * Use StackGenerator to generate a backtrace. From b4542d0d5fc8bd5e8595084cacb08c651b7e8d9a Mon Sep 17 00:00:00 2001 From: delphinus Date: Thu, 31 Mar 2016 00:32:18 +0900 Subject: [PATCH 7/8] Add further tests --- stacktrace-js/stacktrace-js-tests.ts | 36 ++++++++++++++++++---------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/stacktrace-js/stacktrace-js-tests.ts b/stacktrace-js/stacktrace-js-tests.ts index c4c9bcf879..04f92f7d2d 100644 --- a/stacktrace-js/stacktrace-js-tests.ts +++ b/stacktrace-js/stacktrace-js-tests.ts @@ -1,24 +1,36 @@ /// -function interestingFn() { +function interestingFn(): string { return 'https://github.com/exceptionless/Exceptionless'; } -var callback = function(stackframes:StackTrace.StackFrame[]) { - var stringifiedStack = stackframes.map(function(sf:StackTrace.StackFrame) { +const callback = (stackframes: StackTrace.StackFrame[]) => { + const stringifiedStack = stackframes.map((sf: StackTrace.StackFrame): string => { + console.log(sf.functionName); + console.log(sf.args); + console.log(sf.fileName); + console.log(sf.lineNumber); + console.log(sf.columnNumber); + console.log(sf.source); + console.log(sf.isEval); + console.log(sf.isNative); return sf.toString(); }).join('\n'); console.log(stringifiedStack); }; -var errorCallback = function(err:Error) { console.log(err.message); }; +const errorCallback = (err: Error) => console.log(err.message); +const logger = (stackframes: StackTrace.StackFrame[]) => console.log(stackframes); +const options: StackTrace.StackTraceOptions = { + filter: (stackframe: StackTrace.StackFrame) => true, + sourceCache: {}, + offline: false +}; +const error = new Error('BOOM!'); -StackTrace.get(); +StackTrace.get(options).then(logger); +StackTrace.fromError(error, options).then(logger); +StackTrace.generateArtificially(options).then(logger); -// Somewhere else... -var error = new Error('BOOM!'); -StackTrace.fromError(error); -StackTrace.generateArtificially(); - -StackTrace.instrument(interestingFn, callback, errorCallback); -StackTrace.deinstrument(interestingFn); +const instrumented: () => string = StackTrace.instrument(interestingFn, callback, errorCallback); +const original: () => string = StackTrace.deinstrument(interestingFn); From f9104f1e24c58134b99e03f3e28d71d1ca50fce4 Mon Sep 17 00:00:00 2001 From: delphinus Date: Thu, 31 Mar 2016 00:40:48 +0900 Subject: [PATCH 8/8] Fix typo --- stacktrace-js/stacktrace-js.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stacktrace-js/stacktrace-js.d.ts b/stacktrace-js/stacktrace-js.d.ts index bc9571eb5a..4f0bac1635 100644 --- a/stacktrace-js/stacktrace-js.d.ts +++ b/stacktrace-js/stacktrace-js.d.ts @@ -67,7 +67,7 @@ declare namespace StackTrace { * @param {Object} thisArg optional context object (e.g. window) * @return {Function} instrumented function */ - export function instrument(fn: TFunc, callback: (stackFrames:StackFrame[]) => void, errback?: (error: Error) => void, thisArg?: any): TFunc; + export function instrument(fn: TFunc, callback: (stackFrames: StackFrame[]) => void, errback?: (error: Error) => void, thisArg?: any): TFunc; /** * Given a function that has been instrumented,