Skip to content

Commit

Permalink
Fix autoPageDetection command error caused by closure-compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Hongyan Jiang authored and GitHub Enterprise committed Jul 23, 2024
1 parent e47a30f commit 24ffa89
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog


## 1.7.2

- Fix autoPageDetection command error caused by closure-compiler.

## 1.7.1

- Auto detect page transitions on route change.
Expand Down
3 changes: 2 additions & 1 deletion lib/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {hasOwnProperty} from './util';
import {setPage} from './pageChange';
import {warn} from './debug';
import vars from './vars';
import {processAutoPageDetectionCommand} from './hooks/autoPageDetection';

export function processCommand(command: any[]): any {
switch (command[0]) {
Expand Down Expand Up @@ -64,7 +65,7 @@ export function processCommand(command: any[]): any {
vars.wrapEventHandlers = command[1];
break;
case 'autoPageDetection':
vars.autoPageDetection = command[1];
vars.autoPageDetection = processAutoPageDetectionCommand(command[1]);
break;
case 'wrapTimers':
vars.wrapTimers = command[1];
Expand Down
27 changes: 22 additions & 5 deletions lib/hooks/autoPageDetection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {win, doc} from '../browser';
import type {MappingRule} from '../types';
import type {AutoPageDetectionType, MappingRule} from '../types';
import vars from '../vars';
import {info, debug, error} from '../debug';
import {setPage} from '../pageChange';
Expand All @@ -20,7 +20,7 @@ function setupAutoPageDetection() {

win.addEventListener('hashchange', function (event) {
if (DEBUG) {
info(`hashchange to ${event.newURL} from ${event.newURL}, current location ${win.location}`);
info(`hashchange to ${event.newURL} from ${event.oldURL}, current location ${win.location}`);
}
handlePossibleUrlChange(event.newURL);
});
Expand Down Expand Up @@ -115,7 +115,7 @@ function handlePossibleUrlChange(newUrl: string) {

function applyCustomPageMappings(urlPath: string): string | null {
const rules = getAutoPageDetectionMappingRule();
const effectivePath = (titleAsPageNameInAutoPageDetectio() ? doc.title : urlPath) || urlPath;
const effectivePath = (titleAsPageNameInAutoPageDetection() ? doc.title : urlPath) || urlPath;
if (!effectivePath || !rules.length) {
return effectivePath;
}
Expand Down Expand Up @@ -149,11 +149,11 @@ export function isAutoPageDetectionEnabled(): boolean {
return !!vars.autoPageDetection;
}

function ignorePopstateEvent(): boolean {
export function ignorePopstateEvent(): boolean {
return typeof vars.autoPageDetection === 'object' && !!vars.autoPageDetection?.ignorePopstateEvent;
}

function titleAsPageNameInAutoPageDetectio(): boolean {
export function titleAsPageNameInAutoPageDetection(): boolean {
return typeof vars.autoPageDetection === 'object' && !!vars.autoPageDetection?.titleAsPageName;
}

Expand All @@ -163,3 +163,20 @@ function getAutoPageDetectionMappingRule(): Array<MappingRule> {
}
return vars.autoPageDetection.mappingRule;
}

export function processAutoPageDetectionCommand(input: any): boolean | AutoPageDetectionType {
const guessCmd = input as boolean | AutoPageDetectionType | null;
if (!guessCmd) {
return false;
}

if (typeof guessCmd !== 'object') {
return !!guessCmd;
}

return {
ignorePopstateEvent: guessCmd['ignorePopstateEvent'],
titleAsPageName: guessCmd['titleAsPageName'],
mappingRule: guessCmd['mappingRule']
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.7.1",
"version": "1.7.2",
"name": "@instana/weasel",
"description": "Collect end-user data",
"main": "lib/index.ts",
Expand Down
72 changes: 70 additions & 2 deletions test/unit/hooks/autoPageDetection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {expect} from 'chai';

import {win} from '@lib/browser';
import defaultVars from '@lib/vars';
import type {Beacon} from '@lib/types';
import vars from '@lib/vars';
import type {Beacon, AutoPageDetectionType} from '@lib/types';
import {stripSecrets} from '@lib/stripSecrets';
import {addCommonBeaconProperties} from '@lib/commonBeaconProperties';
import {initAutoPageDetection, isAutoPageDetectionEnabled} from '@lib/hooks/autoPageDetection';
import {ignorePopstateEvent, titleAsPageNameInAutoPageDetection, processAutoPageDetectionCommand} from '@lib/hooks/autoPageDetection';

describe('autodetection of page transition', () => {

Expand All @@ -17,6 +19,10 @@ describe('autodetection of page transition', () => {
const result: boolean = isAutoPageDetectionEnabled();
addCommonBeaconProperties(beacon);
expect(result).equal(false);

const titleResult: boolean = titleAsPageNameInAutoPageDetection();
expect(titleResult).equal(false);

expect(beacon.uf).not.exist;
});

Expand All @@ -36,11 +42,65 @@ describe('autodetection of page transition', () => {
};
const result: boolean = isAutoPageDetectionEnabled();
expect(result).equal(true);

const titleResult: boolean = titleAsPageNameInAutoPageDetection();
expect(titleResult).equal(true);

const beacon: Partial<Beacon> = {};
addCommonBeaconProperties(beacon);
expect(beacon.uf).equal('sn');
});
});

describe('autoPageDetection command test', () => {
it('autoPageDetection command parsing normal', () => {
const cmd: any = {mappingRule: [[/.*section*/, 'Section 1']], ignorePopstateEvent: false, titleAsPageName: true};
vars.autoPageDetection = processAutoPageDetectionCommand(cmd);
expect(isAutoPageDetectionEnabled()).equal(true);
expect(ignorePopstateEvent()).equal(false);
expect(titleAsPageNameInAutoPageDetection()).equal(true);
const resultAPD = vars.autoPageDetection as AutoPageDetectionType;
expect(resultAPD.mappingRule?.length).equal(1);
});

it('autoPageDetection command parsing empty', () => {
const cmd: any = {};
vars.autoPageDetection = processAutoPageDetectionCommand(cmd);
expect(isAutoPageDetectionEnabled()).equal(true);
expect(ignorePopstateEvent()).equal(false);
expect(titleAsPageNameInAutoPageDetection()).equal(false);
const resultAPD = vars.autoPageDetection as AutoPageDetectionType;
expect(typeof resultAPD).equal('object');
expect(typeof resultAPD.mappingRule).equal('undefined');
});

it('autoPageDetection command parsing abnormal configs (negative)', () => {
let cmd: any = 101; //number is unexpected
vars.autoPageDetection = processAutoPageDetectionCommand(cmd);
expect(isAutoPageDetectionEnabled()).equal(true);
let resultAPD = vars.autoPageDetection as AutoPageDetectionType;
expect(typeof resultAPD).equal('boolean');

cmd = 0; //number 0 is unexpected, 0 is treated as false
vars.autoPageDetection = processAutoPageDetectionCommand(cmd);
expect(isAutoPageDetectionEnabled()).equal(false);

cmd = 'abc'; //string is unexpected
vars.autoPageDetection = processAutoPageDetectionCommand(cmd);
expect(isAutoPageDetectionEnabled()).equal(true);
resultAPD = vars.autoPageDetection as AutoPageDetectionType;
expect(typeof resultAPD).equal('boolean');

cmd = {titleAsPageName: 'abc'}; //string is unexpected for titleAsPageName
vars.autoPageDetection = processAutoPageDetectionCommand(cmd);
expect(isAutoPageDetectionEnabled()).equal(true);
expect(titleAsPageNameInAutoPageDetection()).equal(true); //string is treated as true
resultAPD = vars.autoPageDetection as AutoPageDetectionType;
expect(typeof resultAPD).equal('object');
expect(typeof resultAPD.titleAsPageName).equal('string');
});
});

describe('listen to various event listeners for page transition', () => {

it('must listen to hashchange event and set page ', () => {
Expand Down Expand Up @@ -167,6 +227,10 @@ describe('autodetection of page transition', () => {
const title = '';
const url = 'http://localhost/#aboutNotGetMatched-about1Some-about2Where-about3SVL';
window.history.pushState(state, title, url);

const titleResult: boolean = titleAsPageNameInAutoPageDetection();
expect(titleResult).equal(false);

const beacon: Partial<Beacon> = {};
addCommonBeaconProperties(beacon);
console.log('check the regex pattern ', beacon);
Expand All @@ -181,12 +245,16 @@ describe('autodetection of page transition', () => {
'checkregex?matchId=<redacted>'
]
],
titleAsPageName: undefined
titleAsPageName: false
};
const state = {page: 10};
const title = '';
const url = 'http://localhost/checkregex12*benifits.-functions12replace-match.textpattern?matchId=6847664458';
window.history.pushState(state, title, url);

const titleResult: boolean = titleAsPageNameInAutoPageDetection();
expect(titleResult).equal(false);

const beacon: Partial<Beacon> = {};
addCommonBeaconProperties(beacon);
expect(beacon.p).equal('checkregex?matchId=<redacted>');
Expand Down

0 comments on commit 24ffa89

Please sign in to comment.