@newrelic/browser-agent 0.1.231 → 1.232.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cjs/common/config/state/configurable.js +27 -21
- package/dist/cjs/common/config/state/init.js +8 -0
- package/dist/cjs/common/config/state/runtime.js +24 -26
- package/dist/cjs/common/constants/env.cdn.js +1 -1
- package/dist/cjs/common/constants/env.npm.js +1 -1
- package/dist/cjs/common/context/shared-context.js +2 -1
- package/dist/cjs/common/event-emitter/contextual-ee.test.js +2 -2
- package/dist/cjs/common/event-emitter/register-handler.test.js +1 -1
- package/dist/cjs/common/event-listener/event-listener-opts.js +4 -2
- package/dist/cjs/common/harvest/harvest-scheduler.js +14 -11
- package/dist/cjs/common/harvest/harvest.js +3 -1
- package/dist/cjs/common/session/constants.js +12 -0
- package/dist/cjs/common/session/session-entity.js +278 -0
- package/dist/cjs/common/session/session-entity.test.js +436 -0
- package/dist/cjs/common/storage/first-party-cookies.js +35 -0
- package/dist/cjs/common/storage/local-memory.js +35 -0
- package/dist/cjs/common/storage/local-memory.test.js +20 -0
- package/dist/cjs/common/storage/local-storage.js +33 -0
- package/dist/cjs/common/storage/local-storage.test.js +14 -0
- package/dist/cjs/common/timer/interaction-timer.js +78 -0
- package/dist/cjs/common/timer/interaction-timer.test.js +216 -0
- package/dist/cjs/common/timer/timer.js +32 -0
- package/dist/cjs/common/timer/timer.test.js +105 -0
- package/dist/cjs/common/unload/eol.js +2 -2
- package/dist/cjs/common/url/canonicalize-url.js +32 -0
- package/dist/cjs/common/url/canonicalize-url.test.js +42 -0
- package/dist/cjs/common/url/clean-url.js +10 -3
- package/dist/cjs/common/util/data-size.js +6 -0
- package/dist/cjs/common/util/data-size.test.js +47 -0
- package/dist/cjs/common/util/global-scope.js +4 -2
- package/dist/cjs/common/util/invoke.js +73 -0
- package/dist/cjs/common/util/invoke.test.js +49 -0
- package/dist/cjs/common/util/obfuscate.js +0 -4
- package/dist/cjs/common/window/page-visibility.js +3 -1
- package/dist/cjs/common/wrap/wrap-fetch.js +1 -3
- package/dist/cjs/common/wrap/wrap-function.js +1 -3
- package/dist/cjs/common/wrap/wrap-timer.js +1 -1
- package/dist/cjs/features/ajax/aggregate/index.js +2 -2
- package/dist/cjs/features/ajax/instrument/index.js +1 -1
- package/dist/cjs/features/jserrors/aggregate/canonical-function-name.js +12 -4
- package/dist/cjs/features/jserrors/aggregate/compute-stack-trace.js +93 -10
- package/dist/cjs/features/jserrors/aggregate/compute-stack-trace.test.js +164 -38
- package/dist/cjs/features/jserrors/aggregate/index.js +25 -46
- package/dist/cjs/features/jserrors/instrument/index.js +0 -2
- package/dist/cjs/features/metrics/aggregate/index.js +13 -2
- package/dist/cjs/features/page_action/aggregate/index.js +2 -2
- package/dist/cjs/features/page_view_event/aggregate/index.js +6 -3
- package/dist/cjs/features/page_view_timing/aggregate/index.js +6 -6
- package/dist/cjs/features/session_trace/aggregate/index.js +3 -5
- package/dist/cjs/features/spa/aggregate/index.js +6 -5
- package/dist/cjs/features/utils/agent-session.js +73 -0
- package/dist/cjs/features/utils/feature-base.js +1 -1
- package/dist/cjs/features/utils/instrument-base.js +7 -2
- package/dist/cjs/features/utils/lazy-loader.js +1 -1
- package/dist/cjs/loaders/agent.js +1 -1
- package/dist/cjs/loaders/api/api.js +1 -4
- package/dist/cjs/loaders/api/apiAsync.js +3 -2
- package/dist/cjs/loaders/configure/configure.js +0 -6
- package/dist/esm/common/config/state/configurable.js +26 -20
- package/dist/esm/common/config/state/init.js +8 -0
- package/dist/esm/common/config/state/runtime.js +24 -26
- package/dist/esm/common/constants/env.cdn.js +1 -1
- package/dist/esm/common/constants/env.npm.js +1 -1
- package/dist/esm/common/context/shared-context.js +2 -1
- package/dist/esm/common/event-emitter/contextual-ee.test.js +2 -2
- package/dist/esm/common/event-emitter/register-handler.test.js +1 -1
- package/dist/esm/common/event-listener/event-listener-opts.js +4 -2
- package/dist/esm/common/harvest/harvest-scheduler.js +14 -11
- package/dist/esm/common/harvest/harvest.js +3 -1
- package/dist/esm/common/session/constants.js +3 -0
- package/dist/esm/common/session/session-entity.js +271 -0
- package/dist/esm/common/session/session-entity.test.js +434 -0
- package/dist/esm/common/storage/first-party-cookies.js +28 -0
- package/dist/esm/common/storage/local-memory.js +28 -0
- package/dist/esm/common/storage/local-memory.test.js +18 -0
- package/dist/esm/common/storage/local-storage.js +26 -0
- package/dist/esm/common/storage/local-storage.test.js +12 -0
- package/dist/esm/common/timer/interaction-timer.js +71 -0
- package/dist/esm/common/timer/interaction-timer.test.js +214 -0
- package/dist/esm/common/timer/timer.js +25 -0
- package/dist/esm/common/timer/timer.test.js +103 -0
- package/dist/esm/common/unload/eol.js +1 -1
- package/dist/esm/common/url/canonicalize-url.js +27 -0
- package/dist/esm/common/url/canonicalize-url.test.js +38 -0
- package/dist/esm/common/url/clean-url.js +10 -3
- package/dist/esm/common/util/data-size.js +7 -0
- package/dist/esm/common/util/data-size.test.js +45 -0
- package/dist/esm/common/util/global-scope.js +1 -0
- package/dist/esm/common/util/invoke.js +66 -0
- package/dist/esm/common/util/invoke.test.js +47 -0
- package/dist/esm/common/util/obfuscate.js +0 -4
- package/dist/esm/common/window/page-visibility.js +3 -1
- package/dist/esm/common/wrap/wrap-fetch.js +1 -2
- package/dist/esm/common/wrap/wrap-function.js +1 -2
- package/dist/esm/common/wrap/wrap-timer.js +1 -1
- package/dist/esm/features/ajax/aggregate/index.js +2 -2
- package/dist/esm/features/ajax/instrument/index.js +1 -1
- package/dist/esm/features/jserrors/aggregate/canonical-function-name.js +12 -4
- package/dist/esm/features/jserrors/aggregate/compute-stack-trace.js +93 -10
- package/dist/esm/features/jserrors/aggregate/compute-stack-trace.test.js +149 -25
- package/dist/esm/features/jserrors/aggregate/index.js +26 -46
- package/dist/esm/features/jserrors/instrument/index.js +0 -1
- package/dist/esm/features/metrics/aggregate/index.js +14 -3
- package/dist/esm/features/page_action/aggregate/index.js +2 -2
- package/dist/esm/features/page_view_event/aggregate/index.js +6 -3
- package/dist/esm/features/page_view_timing/aggregate/index.js +6 -6
- package/dist/esm/features/session_trace/aggregate/index.js +3 -4
- package/dist/esm/features/spa/aggregate/index.js +6 -5
- package/dist/esm/features/utils/agent-session.js +67 -0
- package/dist/esm/features/utils/feature-base.js +1 -1
- package/dist/esm/features/utils/instrument-base.js +7 -2
- package/dist/esm/features/utils/lazy-loader.js +1 -1
- package/dist/esm/loaders/agent.js +1 -1
- package/dist/esm/loaders/api/api.js +2 -5
- package/dist/esm/loaders/api/apiAsync.js +2 -1
- package/dist/esm/loaders/configure/configure.js +2 -8
- package/dist/types/common/config/state/configurable.d.ts.map +1 -1
- package/dist/types/common/config/state/init.d.ts.map +1 -1
- package/dist/types/common/config/state/runtime.d.ts.map +1 -1
- package/dist/types/common/context/shared-context.d.ts.map +1 -1
- package/dist/types/common/event-listener/event-listener-opts.d.ts +2 -2
- package/dist/types/common/event-listener/event-listener-opts.d.ts.map +1 -1
- package/dist/types/common/harvest/harvest-scheduler.d.ts +1 -0
- package/dist/types/common/harvest/harvest-scheduler.d.ts.map +1 -1
- package/dist/types/common/harvest/harvest.d.ts.map +1 -1
- package/dist/types/common/session/constants.d.ts +4 -0
- package/dist/types/common/session/constants.d.ts.map +1 -0
- package/dist/types/common/session/session-entity.d.ts +72 -0
- package/dist/types/common/session/session-entity.d.ts.map +1 -0
- package/dist/types/common/storage/first-party-cookies.d.ts +8 -0
- package/dist/types/common/storage/first-party-cookies.d.ts.map +1 -0
- package/dist/types/common/storage/local-memory.d.ts +8 -0
- package/dist/types/common/storage/local-memory.d.ts.map +1 -0
- package/dist/types/common/storage/local-storage.d.ts +6 -0
- package/dist/types/common/storage/local-storage.d.ts.map +1 -0
- package/dist/types/common/timer/interaction-timer.d.ts +11 -0
- package/dist/types/common/timer/interaction-timer.d.ts.map +1 -0
- package/dist/types/common/timer/timer.d.ts +12 -0
- package/dist/types/common/timer/timer.d.ts.map +1 -0
- package/dist/types/common/url/canonicalize-url.d.ts +9 -0
- package/dist/types/common/url/canonicalize-url.d.ts.map +1 -0
- package/dist/types/common/url/clean-url.d.ts +7 -1
- package/dist/types/common/url/clean-url.d.ts.map +1 -1
- package/dist/types/common/util/data-size.d.ts +7 -1
- package/dist/types/common/util/data-size.d.ts.map +1 -1
- package/dist/types/common/util/global-scope.d.ts +1 -0
- package/dist/types/common/util/global-scope.d.ts.map +1 -1
- package/dist/types/common/util/invoke.d.ts +35 -0
- package/dist/types/common/util/invoke.d.ts.map +1 -0
- package/dist/types/common/util/obfuscate.d.ts.map +1 -1
- package/dist/types/common/window/page-visibility.d.ts +1 -1
- package/dist/types/common/window/page-visibility.d.ts.map +1 -1
- package/dist/types/common/wrap/wrap-fetch.d.ts.map +1 -1
- package/dist/types/common/wrap/wrap-function.d.ts.map +1 -1
- package/dist/types/features/ajax/aggregate/index.d.ts +2 -2
- package/dist/types/features/ajax/aggregate/index.d.ts.map +1 -1
- package/dist/types/features/jserrors/aggregate/canonical-function-name.d.ts +8 -1
- package/dist/types/features/jserrors/aggregate/canonical-function-name.d.ts.map +1 -1
- package/dist/types/features/jserrors/aggregate/compute-stack-trace.d.ts +48 -19
- package/dist/types/features/jserrors/aggregate/compute-stack-trace.d.ts.map +1 -1
- package/dist/types/features/jserrors/aggregate/index.d.ts +14 -5
- package/dist/types/features/jserrors/aggregate/index.d.ts.map +1 -1
- package/dist/types/features/jserrors/instrument/index.d.ts.map +1 -1
- package/dist/types/features/metrics/aggregate/index.d.ts +2 -2
- package/dist/types/features/metrics/aggregate/index.d.ts.map +1 -1
- package/dist/types/features/page_action/aggregate/index.d.ts +3 -3
- package/dist/types/features/page_action/aggregate/index.d.ts.map +1 -1
- package/dist/types/features/page_view_event/aggregate/index.d.ts +2 -2
- package/dist/types/features/page_view_event/aggregate/index.d.ts.map +1 -1
- package/dist/types/features/page_view_timing/aggregate/index.d.ts +2 -2
- package/dist/types/features/page_view_timing/aggregate/index.d.ts.map +1 -1
- package/dist/types/features/session_trace/aggregate/index.d.ts +2 -2
- package/dist/types/features/session_trace/aggregate/index.d.ts.map +1 -1
- package/dist/types/features/spa/aggregate/index.d.ts +2 -2
- package/dist/types/features/spa/aggregate/index.d.ts.map +1 -1
- package/dist/types/features/utils/agent-session.d.ts +2 -0
- package/dist/types/features/utils/agent-session.d.ts.map +1 -0
- package/dist/types/features/utils/instrument-base.d.ts.map +1 -1
- package/dist/types/features/utils/lazy-loader.d.ts +2 -2
- package/dist/types/features/utils/lazy-loader.d.ts.map +1 -1
- package/dist/types/loaders/api/api.d.ts.map +1 -1
- package/dist/types/loaders/api/apiAsync.d.ts.map +1 -1
- package/dist/types/loaders/configure/configure.d.ts.map +1 -1
- package/package.json +9 -8
- package/src/common/config/state/configurable.js +26 -19
- package/src/common/config/state/init.js +7 -0
- package/src/common/config/state/runtime.js +22 -27
- package/src/common/context/shared-context.js +2 -1
- package/src/common/event-emitter/contextual-ee.test.js +2 -2
- package/src/common/event-emitter/register-handler.test.js +1 -1
- package/src/common/event-listener/event-listener-opts.js +4 -4
- package/src/common/harvest/harvest-scheduler.js +12 -8
- package/src/common/harvest/harvest.js +3 -1
- package/src/common/session/constants.js +3 -0
- package/src/common/session/session-entity.js +271 -0
- package/src/common/session/session-entity.test.js +317 -0
- package/src/common/storage/first-party-cookies.js +31 -0
- package/src/common/storage/local-memory.js +30 -0
- package/src/common/storage/local-memory.test.js +19 -0
- package/src/common/storage/local-storage.js +28 -0
- package/src/common/storage/local-storage.test.js +17 -0
- package/src/common/timer/interaction-timer.js +75 -0
- package/src/common/timer/interaction-timer.test.js +167 -0
- package/src/common/timer/timer.js +31 -0
- package/src/common/timer/timer.test.js +100 -0
- package/src/common/unload/eol.js +1 -1
- package/src/common/url/canonicalize-url.js +28 -0
- package/src/common/url/canonicalize-url.test.js +34 -0
- package/src/common/url/clean-url.js +10 -3
- package/src/common/util/data-size.js +6 -0
- package/src/common/util/data-size.test.js +50 -0
- package/src/common/util/global-scope.js +2 -0
- package/src/common/util/invoke.js +55 -0
- package/src/common/util/invoke.test.js +65 -0
- package/src/common/util/obfuscate.js +0 -4
- package/src/common/window/page-visibility.js +2 -2
- package/src/common/wrap/wrap-fetch.js +1 -2
- package/src/common/wrap/wrap-function.js +1 -2
- package/src/common/wrap/wrap-timer.js +1 -1
- package/src/features/ajax/aggregate/index.js +2 -2
- package/src/features/ajax/instrument/index.js +1 -1
- package/src/features/jserrors/aggregate/canonical-function-name.js +12 -4
- package/src/features/jserrors/aggregate/compute-stack-trace.js +85 -11
- package/src/features/jserrors/aggregate/compute-stack-trace.test.js +141 -24
- package/src/features/jserrors/aggregate/index.js +24 -50
- package/src/features/jserrors/instrument/index.js +0 -1
- package/src/features/metrics/aggregate/index.js +18 -3
- package/src/features/page_action/aggregate/index.js +2 -2
- package/src/features/page_view_event/aggregate/index.js +6 -3
- package/src/features/page_view_timing/aggregate/index.js +6 -6
- package/src/features/session_trace/aggregate/index.js +3 -4
- package/src/features/spa/aggregate/index.js +5 -5
- package/src/features/utils/agent-session.js +68 -0
- package/src/features/utils/feature-base.js +1 -1
- package/src/features/utils/instrument-base.js +5 -2
- package/src/features/utils/lazy-loader.js +1 -1
- package/src/loaders/agent.js +1 -1
- package/src/loaders/api/api.js +2 -5
- package/src/loaders/api/apiAsync.js +2 -1
- package/src/loaders/configure/configure.js +2 -7
- package/dist/cjs/common/util/single.js +0 -23
- package/dist/cjs/common/window/session-storage.js +0 -87
- package/dist/cjs/features/utils/aggregate-base.js +0 -13
- package/dist/esm/common/util/single.js +0 -16
- package/dist/esm/common/window/session-storage.js +0 -77
- package/dist/esm/features/utils/aggregate-base.js +0 -6
- package/dist/types/common/util/single.d.ts +0 -2
- package/dist/types/common/util/single.d.ts.map +0 -1
- package/dist/types/common/window/session-storage.d.ts +0 -18
- package/dist/types/common/window/session-storage.d.ts.map +0 -1
- package/dist/types/features/utils/aggregate-base.d.ts +0 -4
- package/dist/types/features/utils/aggregate-base.d.ts.map +0 -1
- package/src/common/util/single.js +0 -18
- package/src/common/window/session-storage.js +0 -75
- package/src/features/utils/aggregate-base.js +0 -7
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { InteractionTimer } from './interaction-timer';
|
|
2
|
+
jest.useFakeTimers();
|
|
3
|
+
let now;
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
now = Date.now();
|
|
6
|
+
jest.setSystemTime(now);
|
|
7
|
+
});
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
jest.clearAllMocks();
|
|
10
|
+
});
|
|
11
|
+
describe('constructor', () => {
|
|
12
|
+
test('appropriate properties are set with valid values -- no refresh', () => {
|
|
13
|
+
const timer = new InteractionTimer({
|
|
14
|
+
onEnd: jest.fn()
|
|
15
|
+
}, 100);
|
|
16
|
+
const requiredKeys = ['onEnd', 'initialMs', 'startTimestamp', 'timer'];
|
|
17
|
+
expect(requiredKeys.every(rk => !!timer[rk])).toBeTruthy();
|
|
18
|
+
});
|
|
19
|
+
test('appropriate properties are set with valid values -- with refresh', () => {
|
|
20
|
+
const timer = new InteractionTimer({
|
|
21
|
+
onEnd: jest.fn()
|
|
22
|
+
}, 100);
|
|
23
|
+
const requiredKeys = ['onEnd', 'refresh', 'initialMs', 'startTimestamp', 'timer'];
|
|
24
|
+
expect(requiredKeys.every(rk => !!timer[rk])).toBeTruthy();
|
|
25
|
+
});
|
|
26
|
+
test('required keys are enforced', () => {
|
|
27
|
+
try {
|
|
28
|
+
new InteractionTimer({}, 100);
|
|
29
|
+
} catch (e) {
|
|
30
|
+
expect(e).toEqual(new Error('onEnd handler is required'));
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
new InteractionTimer({
|
|
34
|
+
onEnd: jest.fn()
|
|
35
|
+
});
|
|
36
|
+
} catch (e) {
|
|
37
|
+
expect(e).toEqual(new Error('ms duration is required'));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
test('refresh type timers set event listeners', () => {
|
|
41
|
+
// eslint-disable-next-line
|
|
42
|
+
let ee = {
|
|
43
|
+
on: jest.fn().mockImplementation((evt, cb) => {
|
|
44
|
+
cb([{
|
|
45
|
+
type: 'click'
|
|
46
|
+
}]);
|
|
47
|
+
})
|
|
48
|
+
};
|
|
49
|
+
let it = new InteractionTimer({
|
|
50
|
+
onEnd: jest.fn(),
|
|
51
|
+
onRefresh: jest.fn(),
|
|
52
|
+
ee
|
|
53
|
+
}, 100);
|
|
54
|
+
// scroll, keypress, click
|
|
55
|
+
expect(ee.on).toHaveBeenCalledTimes(1);
|
|
56
|
+
expect(it.onRefresh).toHaveBeenCalledTimes(1);
|
|
57
|
+
|
|
58
|
+
// eslint-disable-next-line
|
|
59
|
+
ee = {
|
|
60
|
+
on: jest.fn().mockImplementation((evt, cb) => {
|
|
61
|
+
cb([{
|
|
62
|
+
type: 'scroll'
|
|
63
|
+
}]);
|
|
64
|
+
})
|
|
65
|
+
};
|
|
66
|
+
it = new InteractionTimer({
|
|
67
|
+
onEnd: jest.fn(),
|
|
68
|
+
onRefresh: jest.fn(),
|
|
69
|
+
ee
|
|
70
|
+
}, 100);
|
|
71
|
+
// scroll, keypress, click
|
|
72
|
+
expect(ee.on).toHaveBeenCalledTimes(1);
|
|
73
|
+
expect(it.onRefresh).toHaveBeenCalledTimes(1);
|
|
74
|
+
|
|
75
|
+
// eslint-disable-next-line
|
|
76
|
+
ee = {
|
|
77
|
+
on: jest.fn().mockImplementation((evt, cb) => {
|
|
78
|
+
cb([{
|
|
79
|
+
type: 'keydown'
|
|
80
|
+
}]);
|
|
81
|
+
})
|
|
82
|
+
};
|
|
83
|
+
it = new InteractionTimer({
|
|
84
|
+
onEnd: jest.fn(),
|
|
85
|
+
onRefresh: jest.fn(),
|
|
86
|
+
ee
|
|
87
|
+
}, 100);
|
|
88
|
+
// scroll, keypress, click
|
|
89
|
+
expect(ee.on).toHaveBeenCalledTimes(1);
|
|
90
|
+
expect(it.onRefresh).toHaveBeenCalledTimes(1);
|
|
91
|
+
const aelSpy = jest.spyOn(document, 'addEventListener');
|
|
92
|
+
// eslint-disable-next-line
|
|
93
|
+
ee = {
|
|
94
|
+
on: jest.fn().mockImplementation((evt, cb) => {
|
|
95
|
+
cb([{
|
|
96
|
+
type: 'keydown'
|
|
97
|
+
}]);
|
|
98
|
+
})
|
|
99
|
+
};
|
|
100
|
+
it = new InteractionTimer({
|
|
101
|
+
onEnd: jest.fn(),
|
|
102
|
+
onRefresh: jest.fn(),
|
|
103
|
+
ee
|
|
104
|
+
}, 100);
|
|
105
|
+
// visibility change
|
|
106
|
+
expect(aelSpy).toHaveBeenCalledTimes(1);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
describe('create()', () => {
|
|
110
|
+
test('Create sets a timeout that can execute a cb', () => {
|
|
111
|
+
const timer = new InteractionTimer({
|
|
112
|
+
onEnd: jest.fn()
|
|
113
|
+
}, 100);
|
|
114
|
+
expect(timer.timer).toBeTruthy();
|
|
115
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
116
|
+
jest.runOnlyPendingTimers();
|
|
117
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
118
|
+
});
|
|
119
|
+
test('Create can fallback to use defaults', () => {
|
|
120
|
+
let called = 0;
|
|
121
|
+
const timer1 = new InteractionTimer({
|
|
122
|
+
onEnd: jest.fn()
|
|
123
|
+
}, 100);
|
|
124
|
+
timer1.create();
|
|
125
|
+
const timer2 = new InteractionTimer({
|
|
126
|
+
onEnd: jest.fn()
|
|
127
|
+
}, 100);
|
|
128
|
+
timer2.create(timer2.onEnd);
|
|
129
|
+
const timer3 = new InteractionTimer({
|
|
130
|
+
onEnd: jest.fn()
|
|
131
|
+
}, 100);
|
|
132
|
+
timer3.create(undefined, 100);
|
|
133
|
+
jest.runAllTimers(200);
|
|
134
|
+
expect(timer1.onEnd).toHaveBeenCalledTimes(1);
|
|
135
|
+
expect(timer2.onEnd).toHaveBeenCalledTimes(1);
|
|
136
|
+
expect(timer3.onEnd).toHaveBeenCalledTimes(1);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
describe('refresh()', () => {
|
|
140
|
+
test('refresh prevents the callback from firing', () => {
|
|
141
|
+
const timer = new InteractionTimer({
|
|
142
|
+
onEnd: jest.fn(),
|
|
143
|
+
onRefresh: jest.fn()
|
|
144
|
+
}, 100);
|
|
145
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
146
|
+
jest.advanceTimersByTime(75);
|
|
147
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
148
|
+
timer.refresh();
|
|
149
|
+
jest.advanceTimersByTime(75);
|
|
150
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
151
|
+
jest.advanceTimersByTime(100);
|
|
152
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
153
|
+
});
|
|
154
|
+
test('refresh executes a callback for consumers', () => {
|
|
155
|
+
const timer = new InteractionTimer({
|
|
156
|
+
onEnd: jest.fn(),
|
|
157
|
+
onRefresh: jest.fn()
|
|
158
|
+
}, 100);
|
|
159
|
+
timer.refresh();
|
|
160
|
+
expect(timer.onRefresh).toHaveBeenCalledTimes(1);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
describe('pause()', () => {
|
|
164
|
+
test('pause prevents the callback from firing', () => {
|
|
165
|
+
const timer = new InteractionTimer({
|
|
166
|
+
onEnd: jest.fn()
|
|
167
|
+
}, 100);
|
|
168
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
169
|
+
timer.pause();
|
|
170
|
+
jest.advanceTimersByTime(150);
|
|
171
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
172
|
+
});
|
|
173
|
+
test('pause sets remainingMs timestamp', () => {
|
|
174
|
+
const timer = new InteractionTimer({
|
|
175
|
+
onEnd: jest.fn()
|
|
176
|
+
}, 100);
|
|
177
|
+
expect(timer.remainingMs).toEqual(undefined);
|
|
178
|
+
timer.pause();
|
|
179
|
+
expect(timer.remainingMs).toEqual(timer.initialMs - (now - timer.startTimestamp));
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
describe('clear()', () => {
|
|
183
|
+
test('clear prevents the callback from firing and deletes the pointer', () => {
|
|
184
|
+
const timer = new InteractionTimer({
|
|
185
|
+
onEnd: jest.fn()
|
|
186
|
+
}, 100);
|
|
187
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
188
|
+
timer.clear();
|
|
189
|
+
jest.advanceTimersByTime(150);
|
|
190
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
191
|
+
expect(timer.timer).toEqual(null);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
describe('end()', () => {
|
|
195
|
+
test('end clears the callback and calls the onEnd callback', () => {
|
|
196
|
+
const timer = new InteractionTimer({
|
|
197
|
+
onEnd: jest.fn()
|
|
198
|
+
}, 100);
|
|
199
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
200
|
+
timer.end();
|
|
201
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
202
|
+
expect(timer.timer).toEqual(null);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
describe('isValid', () => {
|
|
206
|
+
test('isValid validates timeStamps', () => {
|
|
207
|
+
const timer = new InteractionTimer({
|
|
208
|
+
onEnd: jest.fn()
|
|
209
|
+
}, 100);
|
|
210
|
+
expect(timer.isValid()).toEqual(true);
|
|
211
|
+
timer.startTimestamp -= 100;
|
|
212
|
+
expect(timer.isValid()).toEqual(false);
|
|
213
|
+
});
|
|
214
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class Timer {
|
|
2
|
+
constructor(opts, ms) {
|
|
3
|
+
if (!opts.onEnd) throw new Error('onEnd handler is required');
|
|
4
|
+
if (!ms) throw new Error('ms duration is required');
|
|
5
|
+
this.onEnd = opts.onEnd;
|
|
6
|
+
this.initialMs = ms;
|
|
7
|
+
this.startTimestamp = Date.now();
|
|
8
|
+
this.timer = this.create(this.onEnd, ms);
|
|
9
|
+
}
|
|
10
|
+
create(cb, ms) {
|
|
11
|
+
if (this.timer) this.clear();
|
|
12
|
+
return setTimeout(() => cb ? cb() : this.onEnd(), ms || this.initialMs);
|
|
13
|
+
}
|
|
14
|
+
clear() {
|
|
15
|
+
clearTimeout(this.timer);
|
|
16
|
+
this.timer = null;
|
|
17
|
+
}
|
|
18
|
+
end() {
|
|
19
|
+
this.clear();
|
|
20
|
+
this.onEnd();
|
|
21
|
+
}
|
|
22
|
+
isValid() {
|
|
23
|
+
return this.initialMs - (Date.now() - this.startTimestamp) > 0;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Timer } from './timer';
|
|
2
|
+
jest.useFakeTimers();
|
|
3
|
+
let now;
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
now = Date.now();
|
|
6
|
+
jest.setSystemTime(now);
|
|
7
|
+
});
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
jest.clearAllMocks();
|
|
10
|
+
});
|
|
11
|
+
describe('constructor', () => {
|
|
12
|
+
test('appropriate properties are set with valid values -- no refresh', () => {
|
|
13
|
+
const timer = new Timer({
|
|
14
|
+
onEnd: jest.fn()
|
|
15
|
+
}, 100);
|
|
16
|
+
const requiredKeys = ['onEnd', 'initialMs', 'startTimestamp', 'timer'];
|
|
17
|
+
expect(requiredKeys.every(rk => !!timer[rk])).toBeTruthy();
|
|
18
|
+
});
|
|
19
|
+
test('appropriate properties are set with valid values -- with refresh', () => {
|
|
20
|
+
const timer = new Timer({
|
|
21
|
+
onEnd: jest.fn()
|
|
22
|
+
}, 100);
|
|
23
|
+
const requiredKeys = ['onEnd', 'initialMs', 'startTimestamp', 'timer'];
|
|
24
|
+
expect(requiredKeys.every(rk => !!timer[rk])).toBeTruthy();
|
|
25
|
+
});
|
|
26
|
+
test('required keys are enforced', () => {
|
|
27
|
+
try {
|
|
28
|
+
new Timer({}, 100);
|
|
29
|
+
} catch (e) {
|
|
30
|
+
expect(e).toEqual(new Error('onEnd handler is required'));
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
new Timer({
|
|
34
|
+
onEnd: jest.fn()
|
|
35
|
+
});
|
|
36
|
+
} catch (e) {
|
|
37
|
+
expect(e).toEqual(new Error('ms duration is required'));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe('create()', () => {
|
|
42
|
+
test('Create sets a timeout that can execute a cb', () => {
|
|
43
|
+
const timer = new Timer({
|
|
44
|
+
onEnd: jest.fn()
|
|
45
|
+
}, 100);
|
|
46
|
+
expect(timer.timer).toBeTruthy();
|
|
47
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
48
|
+
jest.runOnlyPendingTimers();
|
|
49
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
50
|
+
});
|
|
51
|
+
test('Create can fallback to use defaults', () => {
|
|
52
|
+
let called = 0;
|
|
53
|
+
const timer1 = new Timer({
|
|
54
|
+
onEnd: jest.fn()
|
|
55
|
+
}, 100);
|
|
56
|
+
timer1.create();
|
|
57
|
+
const timer2 = new Timer({
|
|
58
|
+
onEnd: jest.fn()
|
|
59
|
+
}, 100);
|
|
60
|
+
timer2.create(timer2.onEnd);
|
|
61
|
+
const timer3 = new Timer({
|
|
62
|
+
onEnd: jest.fn()
|
|
63
|
+
}, 100);
|
|
64
|
+
timer3.create(undefined, 100);
|
|
65
|
+
jest.runAllTimers(200);
|
|
66
|
+
expect(timer1.onEnd).toHaveBeenCalledTimes(1);
|
|
67
|
+
expect(timer2.onEnd).toHaveBeenCalledTimes(1);
|
|
68
|
+
expect(timer3.onEnd).toHaveBeenCalledTimes(1);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
describe('clear()', () => {
|
|
72
|
+
test('clear prevents the callback from firing and deletes the pointer', () => {
|
|
73
|
+
const timer = new Timer({
|
|
74
|
+
onEnd: jest.fn()
|
|
75
|
+
}, 100);
|
|
76
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
77
|
+
timer.clear();
|
|
78
|
+
jest.advanceTimersByTime(150);
|
|
79
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
80
|
+
expect(timer.timer).toEqual(null);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
describe('end()', () => {
|
|
84
|
+
test('end clears the callback and calls the onEnd callback', () => {
|
|
85
|
+
const timer = new Timer({
|
|
86
|
+
onEnd: jest.fn()
|
|
87
|
+
}, 100);
|
|
88
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
89
|
+
timer.end();
|
|
90
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
91
|
+
expect(timer.timer).toEqual(null);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
describe('isValid', () => {
|
|
95
|
+
test('isValid validates timeStamps', () => {
|
|
96
|
+
const timer = new Timer({
|
|
97
|
+
onEnd: jest.fn()
|
|
98
|
+
}, 100);
|
|
99
|
+
expect(timer.isValid()).toEqual(true);
|
|
100
|
+
timer.startTimestamp -= 100;
|
|
101
|
+
expect(timer.isValid()).toEqual(false);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { ffVersion } from '../browser-version/firefox-version';
|
|
6
6
|
import { windowAddEventListener } from '../event-listener/event-listener-opts';
|
|
7
|
-
import { single } from '../util/
|
|
7
|
+
import { single } from '../util/invoke';
|
|
8
8
|
import { globalScope, isWorkerScope, isBrowserScope } from '../util/global-scope';
|
|
9
9
|
import { subscribeToVisibilityChange } from '../window/page-visibility';
|
|
10
10
|
if (isWorkerScope) {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { initialLocation } from '../util/global-scope';
|
|
7
|
+
import { cleanURL } from './clean-url';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Converts a URL to its basic form without a query string or fragment. If the resulting URL is the same as the
|
|
11
|
+
* loader's origin URL, returns '<inline>'.
|
|
12
|
+
* @param {string} url - The URL to be canonicalized.
|
|
13
|
+
* @param {string} loaderOriginUrl - The origin URL of the agent loader, used for inline detection.
|
|
14
|
+
* @returns {string} The canonicalized URL, or '<inline>' if the URL matches the loader origin URL.
|
|
15
|
+
*/
|
|
16
|
+
export function canonicalizeUrl(url) {
|
|
17
|
+
if (typeof url !== 'string') return '';
|
|
18
|
+
const cleanedUrl = cleanURL(url);
|
|
19
|
+
const cleanedGlobalScopeUrl = cleanURL(initialLocation);
|
|
20
|
+
|
|
21
|
+
// If the URL matches the origin URL of the loader, we assume it originated within an inline script.
|
|
22
|
+
if (cleanedUrl === cleanedGlobalScopeUrl) {
|
|
23
|
+
return '<inline>';
|
|
24
|
+
} else {
|
|
25
|
+
return cleanedUrl;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
afterEach(() => {
|
|
2
|
+
jest.resetModules();
|
|
3
|
+
});
|
|
4
|
+
test.each([null, undefined, 34])('returns empty string when url argument is %s', async url => {
|
|
5
|
+
const {
|
|
6
|
+
canonicalizeUrl
|
|
7
|
+
} = await import('./canonicalize-url');
|
|
8
|
+
expect(canonicalizeUrl(url)).toEqual('');
|
|
9
|
+
});
|
|
10
|
+
test('strips URLs of query strings and fragments', async () => {
|
|
11
|
+
jest.doMock('../util/global-scope', () => ({
|
|
12
|
+
initialLocation: 'http://different-domain.com/'
|
|
13
|
+
}));
|
|
14
|
+
const {
|
|
15
|
+
canonicalizeUrl
|
|
16
|
+
} = await import('./canonicalize-url');
|
|
17
|
+
expect(canonicalizeUrl('http://example.com/path?query=string#fragment')).toBe('http://example.com/path');
|
|
18
|
+
expect(canonicalizeUrl('https://www.example.com/path/to/file.html?param=value')).toBe('https://www.example.com/path/to/file.html');
|
|
19
|
+
expect(canonicalizeUrl('https://www.example.com/?param=value#fragment')).toBe('https://www.example.com/');
|
|
20
|
+
});
|
|
21
|
+
test('returns <inline> when matching the page URL of the loader', async () => {
|
|
22
|
+
jest.doMock('../util/global-scope', () => ({
|
|
23
|
+
initialLocation: 'http://example.com/'
|
|
24
|
+
}));
|
|
25
|
+
const {
|
|
26
|
+
canonicalizeUrl
|
|
27
|
+
} = await import('./canonicalize-url');
|
|
28
|
+
expect(canonicalizeUrl('http://example.com/')).toEqual('<inline>');
|
|
29
|
+
});
|
|
30
|
+
test('does not identify sub-paths of the loader origin as <inline>', async () => {
|
|
31
|
+
jest.doMock('../util/global-scope', () => ({
|
|
32
|
+
initialLocation: 'http://example.com/'
|
|
33
|
+
}));
|
|
34
|
+
const {
|
|
35
|
+
canonicalizeUrl
|
|
36
|
+
} = await import('./canonicalize-url');
|
|
37
|
+
expect(canonicalizeUrl('http://example.com/path/to/script.js')).not.toEqual('<inline>');
|
|
38
|
+
});
|
|
@@ -3,8 +3,15 @@
|
|
|
3
3
|
* SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
var
|
|
7
|
-
var
|
|
6
|
+
var patternWithHash = /([^?#]*)[^#]*(#[^?]*|$).*/;
|
|
7
|
+
var patternWithoutHash = /([^?#]*)().*/;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Cleans a URL by removing the query string and fragment (hash portion).
|
|
11
|
+
* @param {string} url - The original URL to be cleaned.
|
|
12
|
+
* @param {boolean} [keepHash=false] - Whether to preserve the hash portion of the URL.
|
|
13
|
+
* @returns {string} The cleaned URL.
|
|
14
|
+
*/
|
|
8
15
|
export function cleanURL(url, keepHash) {
|
|
9
|
-
return url.replace(keepHash ?
|
|
16
|
+
return url.replace(keepHash ? patternWithHash : patternWithoutHash, '$1$2');
|
|
10
17
|
}
|
|
@@ -4,6 +4,13 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { stringify } from './stringify';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns the size of the provided data. Designed for measuring XHR responses.
|
|
10
|
+
*
|
|
11
|
+
* @param {*} data - The data to be measured.
|
|
12
|
+
* @returns {(number|undefined)} - The size of the data or undefined if size cannot be determined.
|
|
13
|
+
*/
|
|
7
14
|
export function dataSize(data) {
|
|
8
15
|
if (typeof data === 'string' && data.length) return data.length;
|
|
9
16
|
if (typeof data !== 'object') return undefined;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { dataSize } from './data-size';
|
|
2
|
+
describe('dataSize', () => {
|
|
3
|
+
test('returns length of string', () => {
|
|
4
|
+
const str = 'Hello, world!';
|
|
5
|
+
expect(dataSize(str)).toBe(str.length);
|
|
6
|
+
});
|
|
7
|
+
test('returns undefined for non-object, number, or empty string', () => {
|
|
8
|
+
expect(dataSize(Infinity)).toBeUndefined();
|
|
9
|
+
expect(dataSize(12345)).toBeUndefined(); // might not actually be by design, but this is how it works today
|
|
10
|
+
expect(dataSize('')).toBeUndefined();
|
|
11
|
+
});
|
|
12
|
+
test('returns byte length of ArrayBuffer object', () => {
|
|
13
|
+
const buffer = new ArrayBuffer(8);
|
|
14
|
+
expect(dataSize(buffer)).toBe(8);
|
|
15
|
+
});
|
|
16
|
+
test('returns size of Blob object', () => {
|
|
17
|
+
const blob = new Blob(['Hello, world!'], {
|
|
18
|
+
type: 'text/plain'
|
|
19
|
+
});
|
|
20
|
+
expect(dataSize(blob)).toBe(blob.size);
|
|
21
|
+
});
|
|
22
|
+
test('returns undefined for FormData object', () => {
|
|
23
|
+
const formData = new FormData();
|
|
24
|
+
expect(dataSize(formData)).toBeUndefined();
|
|
25
|
+
});
|
|
26
|
+
test('returns length of JSON string representation of object', () => {
|
|
27
|
+
const obj = {
|
|
28
|
+
str: 'Hello, world!',
|
|
29
|
+
num: 12345,
|
|
30
|
+
nestedObj: {
|
|
31
|
+
arr: [1, 2, 3]
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const expectedSize = JSON.stringify(obj).length;
|
|
35
|
+
expect(dataSize(obj)).toBe(expectedSize);
|
|
36
|
+
});
|
|
37
|
+
test('returns undefined for object with toJSON method that throws an error', () => {
|
|
38
|
+
const obj = {
|
|
39
|
+
toJSON: () => {
|
|
40
|
+
throw new Error('Error in toJSON');
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
expect(dataSize(obj)).toBeUndefined();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -14,6 +14,7 @@ export let globalScope = (() => {
|
|
|
14
14
|
}
|
|
15
15
|
throw new Error('New Relic browser agent shutting down due to error: Unable to locate global scope. This is possibly due to code redefining browser global variables like "self" and "window".');
|
|
16
16
|
})();
|
|
17
|
+
export const initialLocation = '' + globalScope.location;
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* The below methods are only used for testing and should be removed once the
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reduce the invocation of the supplied function so that it is only invoked
|
|
3
|
+
* once within a given timeout.
|
|
4
|
+
*
|
|
5
|
+
* If `wait` is `0`, the function will be invoked during the next tick.
|
|
6
|
+
* If `options.leading` is false or not provided, the function will be invoked
|
|
7
|
+
* N milliseconds after the last invocation of the returned function where
|
|
8
|
+
* N is the `timeout` value.
|
|
9
|
+
* If `options.leading` is true, the function will be invoked immediately upon
|
|
10
|
+
* the first invocation of the returned function and not again for N milliseconds
|
|
11
|
+
* where N is the `timeout` value.
|
|
12
|
+
* @param {function} func Function whose invocation should be limited so it is only invoked
|
|
13
|
+
* once within a given timeout period.
|
|
14
|
+
* @param {number} timeout Time in milliseconds that the function should only be invoked
|
|
15
|
+
* once within.
|
|
16
|
+
* @param {object} options Debounce options
|
|
17
|
+
* @param {boolean} options.leading Forces the function to be invoked on the first
|
|
18
|
+
* invocation of the returned function instead of N milliseconds after the last
|
|
19
|
+
* invocation.
|
|
20
|
+
* @returns {function} A wrapping function that will ensure the provided function
|
|
21
|
+
* is invoked only once within the given timeout.
|
|
22
|
+
*/
|
|
23
|
+
export function debounce(func) {
|
|
24
|
+
var _this = this;
|
|
25
|
+
let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
|
|
26
|
+
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
27
|
+
const leading = options?.leading || false;
|
|
28
|
+
let timer;
|
|
29
|
+
return function () {
|
|
30
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
31
|
+
args[_key] = arguments[_key];
|
|
32
|
+
}
|
|
33
|
+
if (leading && timer === undefined) {
|
|
34
|
+
func.apply(_this, args);
|
|
35
|
+
timer = setTimeout(() => timer = clearTimeout(timer), timeout);
|
|
36
|
+
}
|
|
37
|
+
if (!leading) {
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
timer = setTimeout(() => {
|
|
40
|
+
func.apply(_this, args);
|
|
41
|
+
}, timeout);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Reduce the invocation of the supplied function so that it is only invoked
|
|
48
|
+
* once.
|
|
49
|
+
* @param {function} func Function whose invocation should be limited so it is only invoked
|
|
50
|
+
* once.
|
|
51
|
+
* @returns {function} A wrapping function that will ensure the provided function
|
|
52
|
+
* is invoked only once.
|
|
53
|
+
*/
|
|
54
|
+
export function single(func) {
|
|
55
|
+
var _this2 = this;
|
|
56
|
+
let called = false;
|
|
57
|
+
return function () {
|
|
58
|
+
if (!called) {
|
|
59
|
+
called = true;
|
|
60
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
61
|
+
args[_key2] = arguments[_key2];
|
|
62
|
+
}
|
|
63
|
+
func.apply(_this2, args);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { debounce, single } from './invoke';
|
|
2
|
+
jest.useFakeTimers();
|
|
3
|
+
describe('debounce', () => {
|
|
4
|
+
test('should run the supplied function after 100ms', () => {
|
|
5
|
+
let mockCallback = jest.fn();
|
|
6
|
+
let debouncedMethod = debounce(mockCallback, 100);
|
|
7
|
+
execFnTimes(debouncedMethod, 100);
|
|
8
|
+
expect(mockCallback).not.toHaveBeenCalled();
|
|
9
|
+
jest.advanceTimersByTime(1000);
|
|
10
|
+
expect(mockCallback).toHaveBeenCalledTimes(1);
|
|
11
|
+
});
|
|
12
|
+
test('should rerun the supplied function when called again after 100ms', () => {
|
|
13
|
+
let mockCallback = jest.fn();
|
|
14
|
+
let debouncedMethod = debounce(mockCallback, 100);
|
|
15
|
+
execFnTimes(debouncedMethod, 100);
|
|
16
|
+
jest.advanceTimersByTime(200);
|
|
17
|
+
execFnTimes(debouncedMethod, 100);
|
|
18
|
+
jest.advanceTimersByTime(2000);
|
|
19
|
+
expect(mockCallback).toHaveBeenCalledTimes(2);
|
|
20
|
+
});
|
|
21
|
+
test('should run the supplied function on the first event and debounce subsequent events', () => {
|
|
22
|
+
let mockCallback = jest.fn();
|
|
23
|
+
let debouncedMethod = debounce(mockCallback, 100, {
|
|
24
|
+
leading: true
|
|
25
|
+
});
|
|
26
|
+
execFnTimes(debouncedMethod, 100);
|
|
27
|
+
expect(mockCallback).toHaveBeenCalledTimes(1);
|
|
28
|
+
jest.advanceTimersByTime(200);
|
|
29
|
+
expect(mockCallback).toHaveBeenCalledTimes(1);
|
|
30
|
+
execFnTimes(debouncedMethod, 100);
|
|
31
|
+
jest.advanceTimersByTime(200);
|
|
32
|
+
expect(mockCallback).toHaveBeenCalledTimes(2);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe('single', () => {
|
|
36
|
+
test('should run the supplied function only once', () => {
|
|
37
|
+
let mockCallback = jest.fn();
|
|
38
|
+
let singleMethod = single(mockCallback, 100);
|
|
39
|
+
execFnTimes(singleMethod, 100);
|
|
40
|
+
expect(mockCallback).toHaveBeenCalledTimes(1);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
function execFnTimes(fn, count) {
|
|
44
|
+
for (let i = 0; i < count; i++) {
|
|
45
|
+
fn();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -7,10 +7,6 @@ var fileProtocolRule = {
|
|
|
7
7
|
replacement: 'file://OBFUSCATED'
|
|
8
8
|
};
|
|
9
9
|
export class Obfuscator extends SharedContext {
|
|
10
|
-
constructor(parent) {
|
|
11
|
-
super(parent); // gets any allowed properties from the parent and stores them in `sharedContext`
|
|
12
|
-
}
|
|
13
|
-
|
|
14
10
|
shouldObfuscate() {
|
|
15
11
|
return getRules(this.sharedContext.agentIdentifier).length > 0;
|
|
16
12
|
}
|
|
@@ -12,7 +12,9 @@ import { documentAddEventListener } from '../event-listener/event-listener-opts'
|
|
|
12
12
|
*/
|
|
13
13
|
export function subscribeToVisibilityChange(cb) {
|
|
14
14
|
let toHiddenOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
15
|
-
|
|
15
|
+
let capture = arguments.length > 2 ? arguments[2] : undefined;
|
|
16
|
+
let abortSignal = arguments.length > 3 ? arguments[3] : undefined;
|
|
17
|
+
documentAddEventListener('visibilitychange', handleVisibilityChange, capture, abortSignal);
|
|
16
18
|
return;
|
|
17
19
|
function handleVisibilityChange() {
|
|
18
20
|
if (toHiddenOnly) {
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
* This module is used by: ajax, spa.
|
|
8
8
|
*/
|
|
9
9
|
import { ee as baseEE } from '../event-emitter/contextual-ee';
|
|
10
|
-
import slice from 'lodash._slice';
|
|
11
10
|
import { globalScope } from '../util/global-scope';
|
|
12
11
|
import { flag } from './wrap-function';
|
|
13
12
|
var prefix = 'fetch-';
|
|
@@ -68,7 +67,7 @@ export function wrapFetch(sharedEE) {
|
|
|
68
67
|
var fn = target[name];
|
|
69
68
|
if (typeof fn === 'function') {
|
|
70
69
|
target[name] = function () {
|
|
71
|
-
var args =
|
|
70
|
+
var args = Array.from(arguments);
|
|
72
71
|
var ctx = {};
|
|
73
72
|
// we are wrapping args in an array so we can preserve the reference
|
|
74
73
|
ee.emit(prefix + 'before-start', [args], ctx);
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { ee } from '../event-emitter/contextual-ee';
|
|
10
|
-
import slice from 'lodash._slice';
|
|
11
10
|
export const flag = 'nr@original';
|
|
12
11
|
|
|
13
12
|
/**
|
|
@@ -72,7 +71,7 @@ export function createWrapperWithEmitter(emitter, always) {
|
|
|
72
71
|
var result;
|
|
73
72
|
try {
|
|
74
73
|
originalThis = this;
|
|
75
|
-
args =
|
|
74
|
+
args = Array.from(arguments);
|
|
76
75
|
if (typeof getContext === 'function') {
|
|
77
76
|
ctx = getContext(args, originalThis);
|
|
78
77
|
} else {
|