Skip to content

Commit

Permalink
[BUG] 'Cannot use 'in' operator to search for 'ver' in Timeout', name…
Browse files Browse the repository at this point in the history
…: 'TypeError'}​​​​​#1656 (#1657)
  • Loading branch information
MSNev committed Sep 3, 2021
1 parent aaa8078 commit 44ec248
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 37 deletions.
18 changes: 13 additions & 5 deletions channels/applicationinsights-channel-js/src/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,21 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControlsAI {
const xhr: any = getGlobalInst("XMLHttpRequest");
if(xhr) {
const testXhr = new xhr();
if ("withCredentials" in testXhr) {
_self._sender = _xhrSender;
_self._XMLHttpRequestSupported = true;
} else if (typeof XDomainRequest !== strUndefined) {
try {
if ("withCredentials" in testXhr) {
_self._sender = _xhrSender;
_self._XMLHttpRequestSupported = true;
}
} catch(e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}

if (!_self._sender && typeof XDomainRequest !== strUndefined) {
_self._sender = _xdrSender; // IE 8 and 9
}
} else {
}

if (!_self._sender) {
const fetch: any = getGlobalInst("fetch");
if (fetch) {
_self._sender = _fetchSender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ export function isValueAssigned(value: any) {
* @returns true if the event is a right click
*/
export function isRightClick(evt: any): boolean {
if ('which' in evt) { // Chrome, FF, ...
return (evt.which === 3);
} else if ('button' in evt) { // IE, ...
return (evt.button === 2);
try {
if ('which' in evt) { // Chrome, FF, ...
return (evt.which === 3);
} else if ('button' in evt) { // IE, ...
return (evt.button === 2);
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}
}

Expand All @@ -89,10 +93,14 @@ export function isRightClick(evt: any): boolean {
* @returns true if the event is a left click
*/
export function isLeftClick(evt: any): boolean {
if ('which' in evt) { // Chrome, FF, ...
return (evt.which === 1);
} else if ('button' in evt) { // IE, ...
return (evt.button === 1);
try {
if ('which' in evt) { // Chrome, FF, ...
return (evt.which === 1);
} else if ('button' in evt) { // IE, ...
return (evt.button === 1);
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}
}

Expand All @@ -102,10 +110,14 @@ export function isLeftClick(evt: any): boolean {
* @returns true if the event is a middle click
*/
export function isMiddleClick(evt: any): boolean {
if ('which' in evt) { // Chrome, FF, ...
return (evt.which === 2);
} else if ('button' in evt) { // IE, ...
return (evt.button === 4);
try {
if ('which' in evt) { // Chrome, FF, ...
return (evt.which === 2);
} else if ('button' in evt) { // IE, ...
return (evt.button === 4);
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}
}

Expand All @@ -115,8 +127,12 @@ export function isMiddleClick(evt: any): boolean {
* @returns true if the event is a keyboard enter
*/
export function isKeyboardEnter(evt: KeyboardEvent): boolean {
if ('keyCode' in evt) { // Chrome, FF, ...
return (evt.keyCode === 13);
try {
if ('keyCode' in evt) { // Chrome, FF, ...
return (evt.keyCode === 13);
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}
}

Expand All @@ -126,8 +142,12 @@ export function isKeyboardEnter(evt: KeyboardEvent): boolean {
* @returns true if the event is a space enter
*/
export function isKeyboardSpace(evt: KeyboardEvent) {
if ('keyCode' in evt) { // Chrome, FF, ...
return (evt.keyCode === 32);
try {
if ('keyCode' in evt) { // Chrome, FF, ...
return (evt.keyCode === 32);
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}
}

Expand Down
13 changes: 11 additions & 2 deletions shared/AppInsightsCommon/src/HelperFuncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ export function msToTimeSpan(totalms: number): string {
}

export function isBeaconApiSupported(): boolean {
let nav = getNavigator();
return ('sendBeacon' in nav && (nav as any).sendBeacon);
let result = false;
try {
let nav = getNavigator();
if (nav) {
result = ('sendBeacon' in nav && (nav as any).sendBeacon);
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}

return result;
}

export function getExtensionByName(extensions: IPlugin[], identifier: string): IPlugin | null {
Expand Down
16 changes: 12 additions & 4 deletions shared/AppInsightsCommon/src/Telemetry/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,24 @@ function _formatMessage(theEvent: any, errorType: string) {
}

function _isExceptionDetailsInternal(value:any): value is IExceptionDetailsInternal {
if (isObject(value)) {
return "hasFullStack" in value && "typeName" in value;
try {
if (isObject(value)) {
return "hasFullStack" in value && "typeName" in value;
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}

return false;
}

function _isExceptionInternal(value:any): value is IExceptionInternal {
if (isObject(value)) {
return ("ver" in value && "exceptions" in value && "properties" in value);
try {
if (isObject(value)) {
return ("ver" in value && "exceptions" in value && "properties" in value);
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}

return false;
Expand Down
32 changes: 22 additions & 10 deletions shared/AppInsightsCore/src/JavaScriptSDK/HelperFuncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,16 @@ export function toISOString(date: Date) {
*/
export function arrForEach<T>(arr: T[], callbackfn: (value: T, index?: number, array?: T[]) => void|number, thisArg?: any): void {
let len = arr.length;
for (let idx = 0; idx < len; idx++) {
if (idx in arr) {
if (callbackfn.call(thisArg || arr, arr[idx], idx, arr) === -1) {
break;
try {
for (let idx = 0; idx < len; idx++) {
if (idx in arr) {
if (callbackfn.call(thisArg || arr, arr[idx], idx, arr) === -1) {
break;
}
}
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}
}

Expand All @@ -322,10 +326,14 @@ export function arrForEach<T>(arr: T[], callbackfn: (value: T, index?: number, a
export function arrIndexOf<T>(arr: T[], searchElement: T, fromIndex?: number): number {
let len = arr.length;
let from = fromIndex || 0;
for (let lp = Math.max(from >= 0 ? from : len - Math.abs(from), 0); lp < len; lp++) {
if (lp in arr && arr[lp] === searchElement) {
return lp;
try {
for (let lp = Math.max(from >= 0 ? from : len - Math.abs(from), 0); lp < len; lp++) {
if (lp in arr && arr[lp] === searchElement) {
return lp;
}
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}

return -1;
Expand All @@ -344,10 +352,14 @@ export function arrMap<T, R>(arr: T[], callbackfn: (value: T, index?: number, ar
let _this = thisArg || arr;
let results = new Array(len);

for (let lp = 0; lp < len; lp++) {
if (lp in arr) {
results[lp] = callbackfn.call(_this, arr[lp], arr);
try {
for (let lp = 0; lp < len; lp++) {
if (lp in arr) {
results[lp] = callbackfn.call(_this, arr[lp], arr);
}
}
} catch (e) {
// This can happen with some native browser objects, but should not happen for the type we are checking for
}

return results;
Expand Down

0 comments on commit 44ec248

Please sign in to comment.