@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,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interactionTimer = require("./interaction-timer");
|
|
4
|
+
jest.useFakeTimers();
|
|
5
|
+
let now;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
now = Date.now();
|
|
8
|
+
jest.setSystemTime(now);
|
|
9
|
+
});
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
jest.clearAllMocks();
|
|
12
|
+
});
|
|
13
|
+
describe('constructor', () => {
|
|
14
|
+
test('appropriate properties are set with valid values -- no refresh', () => {
|
|
15
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
16
|
+
onEnd: jest.fn()
|
|
17
|
+
}, 100);
|
|
18
|
+
const requiredKeys = ['onEnd', 'initialMs', 'startTimestamp', 'timer'];
|
|
19
|
+
expect(requiredKeys.every(rk => !!timer[rk])).toBeTruthy();
|
|
20
|
+
});
|
|
21
|
+
test('appropriate properties are set with valid values -- with refresh', () => {
|
|
22
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
23
|
+
onEnd: jest.fn()
|
|
24
|
+
}, 100);
|
|
25
|
+
const requiredKeys = ['onEnd', 'refresh', 'initialMs', 'startTimestamp', 'timer'];
|
|
26
|
+
expect(requiredKeys.every(rk => !!timer[rk])).toBeTruthy();
|
|
27
|
+
});
|
|
28
|
+
test('required keys are enforced', () => {
|
|
29
|
+
try {
|
|
30
|
+
new _interactionTimer.InteractionTimer({}, 100);
|
|
31
|
+
} catch (e) {
|
|
32
|
+
expect(e).toEqual(new Error('onEnd handler is required'));
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
new _interactionTimer.InteractionTimer({
|
|
36
|
+
onEnd: jest.fn()
|
|
37
|
+
});
|
|
38
|
+
} catch (e) {
|
|
39
|
+
expect(e).toEqual(new Error('ms duration is required'));
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
test('refresh type timers set event listeners', () => {
|
|
43
|
+
// eslint-disable-next-line
|
|
44
|
+
let ee = {
|
|
45
|
+
on: jest.fn().mockImplementation((evt, cb) => {
|
|
46
|
+
cb([{
|
|
47
|
+
type: 'click'
|
|
48
|
+
}]);
|
|
49
|
+
})
|
|
50
|
+
};
|
|
51
|
+
let it = new _interactionTimer.InteractionTimer({
|
|
52
|
+
onEnd: jest.fn(),
|
|
53
|
+
onRefresh: jest.fn(),
|
|
54
|
+
ee
|
|
55
|
+
}, 100);
|
|
56
|
+
// scroll, keypress, click
|
|
57
|
+
expect(ee.on).toHaveBeenCalledTimes(1);
|
|
58
|
+
expect(it.onRefresh).toHaveBeenCalledTimes(1);
|
|
59
|
+
|
|
60
|
+
// eslint-disable-next-line
|
|
61
|
+
ee = {
|
|
62
|
+
on: jest.fn().mockImplementation((evt, cb) => {
|
|
63
|
+
cb([{
|
|
64
|
+
type: 'scroll'
|
|
65
|
+
}]);
|
|
66
|
+
})
|
|
67
|
+
};
|
|
68
|
+
it = new _interactionTimer.InteractionTimer({
|
|
69
|
+
onEnd: jest.fn(),
|
|
70
|
+
onRefresh: jest.fn(),
|
|
71
|
+
ee
|
|
72
|
+
}, 100);
|
|
73
|
+
// scroll, keypress, click
|
|
74
|
+
expect(ee.on).toHaveBeenCalledTimes(1);
|
|
75
|
+
expect(it.onRefresh).toHaveBeenCalledTimes(1);
|
|
76
|
+
|
|
77
|
+
// eslint-disable-next-line
|
|
78
|
+
ee = {
|
|
79
|
+
on: jest.fn().mockImplementation((evt, cb) => {
|
|
80
|
+
cb([{
|
|
81
|
+
type: 'keydown'
|
|
82
|
+
}]);
|
|
83
|
+
})
|
|
84
|
+
};
|
|
85
|
+
it = new _interactionTimer.InteractionTimer({
|
|
86
|
+
onEnd: jest.fn(),
|
|
87
|
+
onRefresh: jest.fn(),
|
|
88
|
+
ee
|
|
89
|
+
}, 100);
|
|
90
|
+
// scroll, keypress, click
|
|
91
|
+
expect(ee.on).toHaveBeenCalledTimes(1);
|
|
92
|
+
expect(it.onRefresh).toHaveBeenCalledTimes(1);
|
|
93
|
+
const aelSpy = jest.spyOn(document, 'addEventListener');
|
|
94
|
+
// eslint-disable-next-line
|
|
95
|
+
ee = {
|
|
96
|
+
on: jest.fn().mockImplementation((evt, cb) => {
|
|
97
|
+
cb([{
|
|
98
|
+
type: 'keydown'
|
|
99
|
+
}]);
|
|
100
|
+
})
|
|
101
|
+
};
|
|
102
|
+
it = new _interactionTimer.InteractionTimer({
|
|
103
|
+
onEnd: jest.fn(),
|
|
104
|
+
onRefresh: jest.fn(),
|
|
105
|
+
ee
|
|
106
|
+
}, 100);
|
|
107
|
+
// visibility change
|
|
108
|
+
expect(aelSpy).toHaveBeenCalledTimes(1);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
describe('create()', () => {
|
|
112
|
+
test('Create sets a timeout that can execute a cb', () => {
|
|
113
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
114
|
+
onEnd: jest.fn()
|
|
115
|
+
}, 100);
|
|
116
|
+
expect(timer.timer).toBeTruthy();
|
|
117
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
118
|
+
jest.runOnlyPendingTimers();
|
|
119
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
120
|
+
});
|
|
121
|
+
test('Create can fallback to use defaults', () => {
|
|
122
|
+
let called = 0;
|
|
123
|
+
const timer1 = new _interactionTimer.InteractionTimer({
|
|
124
|
+
onEnd: jest.fn()
|
|
125
|
+
}, 100);
|
|
126
|
+
timer1.create();
|
|
127
|
+
const timer2 = new _interactionTimer.InteractionTimer({
|
|
128
|
+
onEnd: jest.fn()
|
|
129
|
+
}, 100);
|
|
130
|
+
timer2.create(timer2.onEnd);
|
|
131
|
+
const timer3 = new _interactionTimer.InteractionTimer({
|
|
132
|
+
onEnd: jest.fn()
|
|
133
|
+
}, 100);
|
|
134
|
+
timer3.create(undefined, 100);
|
|
135
|
+
jest.runAllTimers(200);
|
|
136
|
+
expect(timer1.onEnd).toHaveBeenCalledTimes(1);
|
|
137
|
+
expect(timer2.onEnd).toHaveBeenCalledTimes(1);
|
|
138
|
+
expect(timer3.onEnd).toHaveBeenCalledTimes(1);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
describe('refresh()', () => {
|
|
142
|
+
test('refresh prevents the callback from firing', () => {
|
|
143
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
144
|
+
onEnd: jest.fn(),
|
|
145
|
+
onRefresh: jest.fn()
|
|
146
|
+
}, 100);
|
|
147
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
148
|
+
jest.advanceTimersByTime(75);
|
|
149
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
150
|
+
timer.refresh();
|
|
151
|
+
jest.advanceTimersByTime(75);
|
|
152
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
153
|
+
jest.advanceTimersByTime(100);
|
|
154
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
155
|
+
});
|
|
156
|
+
test('refresh executes a callback for consumers', () => {
|
|
157
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
158
|
+
onEnd: jest.fn(),
|
|
159
|
+
onRefresh: jest.fn()
|
|
160
|
+
}, 100);
|
|
161
|
+
timer.refresh();
|
|
162
|
+
expect(timer.onRefresh).toHaveBeenCalledTimes(1);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
describe('pause()', () => {
|
|
166
|
+
test('pause prevents the callback from firing', () => {
|
|
167
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
168
|
+
onEnd: jest.fn()
|
|
169
|
+
}, 100);
|
|
170
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
171
|
+
timer.pause();
|
|
172
|
+
jest.advanceTimersByTime(150);
|
|
173
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
174
|
+
});
|
|
175
|
+
test('pause sets remainingMs timestamp', () => {
|
|
176
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
177
|
+
onEnd: jest.fn()
|
|
178
|
+
}, 100);
|
|
179
|
+
expect(timer.remainingMs).toEqual(undefined);
|
|
180
|
+
timer.pause();
|
|
181
|
+
expect(timer.remainingMs).toEqual(timer.initialMs - (now - timer.startTimestamp));
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
describe('clear()', () => {
|
|
185
|
+
test('clear prevents the callback from firing and deletes the pointer', () => {
|
|
186
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
187
|
+
onEnd: jest.fn()
|
|
188
|
+
}, 100);
|
|
189
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
190
|
+
timer.clear();
|
|
191
|
+
jest.advanceTimersByTime(150);
|
|
192
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
193
|
+
expect(timer.timer).toEqual(null);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
describe('end()', () => {
|
|
197
|
+
test('end clears the callback and calls the onEnd callback', () => {
|
|
198
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
199
|
+
onEnd: jest.fn()
|
|
200
|
+
}, 100);
|
|
201
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
202
|
+
timer.end();
|
|
203
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
204
|
+
expect(timer.timer).toEqual(null);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
describe('isValid', () => {
|
|
208
|
+
test('isValid validates timeStamps', () => {
|
|
209
|
+
const timer = new _interactionTimer.InteractionTimer({
|
|
210
|
+
onEnd: jest.fn()
|
|
211
|
+
}, 100);
|
|
212
|
+
expect(timer.isValid()).toEqual(true);
|
|
213
|
+
timer.startTimestamp -= 100;
|
|
214
|
+
expect(timer.isValid()).toEqual(false);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Timer = void 0;
|
|
7
|
+
class Timer {
|
|
8
|
+
constructor(opts, ms) {
|
|
9
|
+
if (!opts.onEnd) throw new Error('onEnd handler is required');
|
|
10
|
+
if (!ms) throw new Error('ms duration is required');
|
|
11
|
+
this.onEnd = opts.onEnd;
|
|
12
|
+
this.initialMs = ms;
|
|
13
|
+
this.startTimestamp = Date.now();
|
|
14
|
+
this.timer = this.create(this.onEnd, ms);
|
|
15
|
+
}
|
|
16
|
+
create(cb, ms) {
|
|
17
|
+
if (this.timer) this.clear();
|
|
18
|
+
return setTimeout(() => cb ? cb() : this.onEnd(), ms || this.initialMs);
|
|
19
|
+
}
|
|
20
|
+
clear() {
|
|
21
|
+
clearTimeout(this.timer);
|
|
22
|
+
this.timer = null;
|
|
23
|
+
}
|
|
24
|
+
end() {
|
|
25
|
+
this.clear();
|
|
26
|
+
this.onEnd();
|
|
27
|
+
}
|
|
28
|
+
isValid() {
|
|
29
|
+
return this.initialMs - (Date.now() - this.startTimestamp) > 0;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.Timer = Timer;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _timer = require("./timer");
|
|
4
|
+
jest.useFakeTimers();
|
|
5
|
+
let now;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
now = Date.now();
|
|
8
|
+
jest.setSystemTime(now);
|
|
9
|
+
});
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
jest.clearAllMocks();
|
|
12
|
+
});
|
|
13
|
+
describe('constructor', () => {
|
|
14
|
+
test('appropriate properties are set with valid values -- no refresh', () => {
|
|
15
|
+
const timer = new _timer.Timer({
|
|
16
|
+
onEnd: jest.fn()
|
|
17
|
+
}, 100);
|
|
18
|
+
const requiredKeys = ['onEnd', 'initialMs', 'startTimestamp', 'timer'];
|
|
19
|
+
expect(requiredKeys.every(rk => !!timer[rk])).toBeTruthy();
|
|
20
|
+
});
|
|
21
|
+
test('appropriate properties are set with valid values -- with refresh', () => {
|
|
22
|
+
const timer = new _timer.Timer({
|
|
23
|
+
onEnd: jest.fn()
|
|
24
|
+
}, 100);
|
|
25
|
+
const requiredKeys = ['onEnd', 'initialMs', 'startTimestamp', 'timer'];
|
|
26
|
+
expect(requiredKeys.every(rk => !!timer[rk])).toBeTruthy();
|
|
27
|
+
});
|
|
28
|
+
test('required keys are enforced', () => {
|
|
29
|
+
try {
|
|
30
|
+
new _timer.Timer({}, 100);
|
|
31
|
+
} catch (e) {
|
|
32
|
+
expect(e).toEqual(new Error('onEnd handler is required'));
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
new _timer.Timer({
|
|
36
|
+
onEnd: jest.fn()
|
|
37
|
+
});
|
|
38
|
+
} catch (e) {
|
|
39
|
+
expect(e).toEqual(new Error('ms duration is required'));
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
describe('create()', () => {
|
|
44
|
+
test('Create sets a timeout that can execute a cb', () => {
|
|
45
|
+
const timer = new _timer.Timer({
|
|
46
|
+
onEnd: jest.fn()
|
|
47
|
+
}, 100);
|
|
48
|
+
expect(timer.timer).toBeTruthy();
|
|
49
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
50
|
+
jest.runOnlyPendingTimers();
|
|
51
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
52
|
+
});
|
|
53
|
+
test('Create can fallback to use defaults', () => {
|
|
54
|
+
let called = 0;
|
|
55
|
+
const timer1 = new _timer.Timer({
|
|
56
|
+
onEnd: jest.fn()
|
|
57
|
+
}, 100);
|
|
58
|
+
timer1.create();
|
|
59
|
+
const timer2 = new _timer.Timer({
|
|
60
|
+
onEnd: jest.fn()
|
|
61
|
+
}, 100);
|
|
62
|
+
timer2.create(timer2.onEnd);
|
|
63
|
+
const timer3 = new _timer.Timer({
|
|
64
|
+
onEnd: jest.fn()
|
|
65
|
+
}, 100);
|
|
66
|
+
timer3.create(undefined, 100);
|
|
67
|
+
jest.runAllTimers(200);
|
|
68
|
+
expect(timer1.onEnd).toHaveBeenCalledTimes(1);
|
|
69
|
+
expect(timer2.onEnd).toHaveBeenCalledTimes(1);
|
|
70
|
+
expect(timer3.onEnd).toHaveBeenCalledTimes(1);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe('clear()', () => {
|
|
74
|
+
test('clear prevents the callback from firing and deletes the pointer', () => {
|
|
75
|
+
const timer = new _timer.Timer({
|
|
76
|
+
onEnd: jest.fn()
|
|
77
|
+
}, 100);
|
|
78
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
79
|
+
timer.clear();
|
|
80
|
+
jest.advanceTimersByTime(150);
|
|
81
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
82
|
+
expect(timer.timer).toEqual(null);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
describe('end()', () => {
|
|
86
|
+
test('end clears the callback and calls the onEnd callback', () => {
|
|
87
|
+
const timer = new _timer.Timer({
|
|
88
|
+
onEnd: jest.fn()
|
|
89
|
+
}, 100);
|
|
90
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(0);
|
|
91
|
+
timer.end();
|
|
92
|
+
expect(timer.onEnd).toHaveBeenCalledTimes(1);
|
|
93
|
+
expect(timer.timer).toEqual(null);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
describe('isValid', () => {
|
|
97
|
+
test('isValid validates timeStamps', () => {
|
|
98
|
+
const timer = new _timer.Timer({
|
|
99
|
+
onEnd: jest.fn()
|
|
100
|
+
}, 100);
|
|
101
|
+
expect(timer.isValid()).toEqual(true);
|
|
102
|
+
timer.startTimestamp -= 100;
|
|
103
|
+
expect(timer.isValid()).toEqual(false);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.subscribeToEOL = subscribeToEOL;
|
|
7
7
|
var _firefoxVersion = require("../browser-version/firefox-version");
|
|
8
8
|
var _eventListenerOpts = require("../event-listener/event-listener-opts");
|
|
9
|
-
var
|
|
9
|
+
var _invoke = require("../util/invoke");
|
|
10
10
|
var _globalScope = require("../util/global-scope");
|
|
11
11
|
var _pageVisibility = require("../window/page-visibility");
|
|
12
12
|
/*
|
|
@@ -40,7 +40,7 @@ function subscribeToEOL(cb, allowBFCache) {
|
|
|
40
40
|
(0, _eventListenerOpts.windowAddEventListener)('pagehide', cb); // when user navigates away, and because safari iOS v14.4- doesn't fully support vis change
|
|
41
41
|
// --this ought to be removed once support for version below 14.5 phases out
|
|
42
42
|
} else {
|
|
43
|
-
var oneCall = (0,
|
|
43
|
+
var oneCall = (0, _invoke.single)(cb);
|
|
44
44
|
|
|
45
45
|
// Firefox has a bug wherein a slow-loading resource loaded from the 'pagehide'
|
|
46
46
|
// or 'unload' event will delay the 'load' event firing on the next page load.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.canonicalizeUrl = canonicalizeUrl;
|
|
7
|
+
var _globalScope = require("../util/global-scope");
|
|
8
|
+
var _cleanUrl = require("./clean-url");
|
|
9
|
+
/*
|
|
10
|
+
* Copyright 2020 New Relic Corporation. All rights reserved.
|
|
11
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Converts a URL to its basic form without a query string or fragment. If the resulting URL is the same as the
|
|
16
|
+
* loader's origin URL, returns '<inline>'.
|
|
17
|
+
* @param {string} url - The URL to be canonicalized.
|
|
18
|
+
* @param {string} loaderOriginUrl - The origin URL of the agent loader, used for inline detection.
|
|
19
|
+
* @returns {string} The canonicalized URL, or '<inline>' if the URL matches the loader origin URL.
|
|
20
|
+
*/
|
|
21
|
+
function canonicalizeUrl(url) {
|
|
22
|
+
if (typeof url !== 'string') return '';
|
|
23
|
+
const cleanedUrl = (0, _cleanUrl.cleanURL)(url);
|
|
24
|
+
const cleanedGlobalScopeUrl = (0, _cleanUrl.cleanURL)(_globalScope.initialLocation);
|
|
25
|
+
|
|
26
|
+
// If the URL matches the origin URL of the loader, we assume it originated within an inline script.
|
|
27
|
+
if (cleanedUrl === cleanedGlobalScopeUrl) {
|
|
28
|
+
return '<inline>';
|
|
29
|
+
} else {
|
|
30
|
+
return cleanedUrl;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
4
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
jest.resetModules();
|
|
7
|
+
});
|
|
8
|
+
test.each([null, undefined, 34])('returns empty string when url argument is %s', async url => {
|
|
9
|
+
const {
|
|
10
|
+
canonicalizeUrl
|
|
11
|
+
} = await Promise.resolve().then(() => _interopRequireWildcard(require('./canonicalize-url')));
|
|
12
|
+
expect(canonicalizeUrl(url)).toEqual('');
|
|
13
|
+
});
|
|
14
|
+
test('strips URLs of query strings and fragments', async () => {
|
|
15
|
+
jest.doMock('../util/global-scope', () => ({
|
|
16
|
+
initialLocation: 'http://different-domain.com/'
|
|
17
|
+
}));
|
|
18
|
+
const {
|
|
19
|
+
canonicalizeUrl
|
|
20
|
+
} = await Promise.resolve().then(() => _interopRequireWildcard(require('./canonicalize-url')));
|
|
21
|
+
expect(canonicalizeUrl('http://example.com/path?query=string#fragment')).toBe('http://example.com/path');
|
|
22
|
+
expect(canonicalizeUrl('https://www.example.com/path/to/file.html?param=value')).toBe('https://www.example.com/path/to/file.html');
|
|
23
|
+
expect(canonicalizeUrl('https://www.example.com/?param=value#fragment')).toBe('https://www.example.com/');
|
|
24
|
+
});
|
|
25
|
+
test('returns <inline> when matching the page URL of the loader', async () => {
|
|
26
|
+
jest.doMock('../util/global-scope', () => ({
|
|
27
|
+
initialLocation: 'http://example.com/'
|
|
28
|
+
}));
|
|
29
|
+
const {
|
|
30
|
+
canonicalizeUrl
|
|
31
|
+
} = await Promise.resolve().then(() => _interopRequireWildcard(require('./canonicalize-url')));
|
|
32
|
+
expect(canonicalizeUrl('http://example.com/')).toEqual('<inline>');
|
|
33
|
+
});
|
|
34
|
+
test('does not identify sub-paths of the loader origin as <inline>', async () => {
|
|
35
|
+
jest.doMock('../util/global-scope', () => ({
|
|
36
|
+
initialLocation: 'http://example.com/'
|
|
37
|
+
}));
|
|
38
|
+
const {
|
|
39
|
+
canonicalizeUrl
|
|
40
|
+
} = await Promise.resolve().then(() => _interopRequireWildcard(require('./canonicalize-url')));
|
|
41
|
+
expect(canonicalizeUrl('http://example.com/path/to/script.js')).not.toEqual('<inline>');
|
|
42
|
+
});
|
|
@@ -9,8 +9,15 @@ exports.cleanURL = cleanURL;
|
|
|
9
9
|
* SPDX-License-Identifier: Apache-2.0
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
var
|
|
13
|
-
var
|
|
12
|
+
var patternWithHash = /([^?#]*)[^#]*(#[^?]*|$).*/;
|
|
13
|
+
var patternWithoutHash = /([^?#]*)().*/;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Cleans a URL by removing the query string and fragment (hash portion).
|
|
17
|
+
* @param {string} url - The original URL to be cleaned.
|
|
18
|
+
* @param {boolean} [keepHash=false] - Whether to preserve the hash portion of the URL.
|
|
19
|
+
* @returns {string} The cleaned URL.
|
|
20
|
+
*/
|
|
14
21
|
function cleanURL(url, keepHash) {
|
|
15
|
-
return url.replace(keepHash ?
|
|
22
|
+
return url.replace(keepHash ? patternWithHash : patternWithoutHash, '$1$2');
|
|
16
23
|
}
|
|
@@ -10,6 +10,12 @@ var _stringify = require("./stringify");
|
|
|
10
10
|
* SPDX-License-Identifier: Apache-2.0
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Returns the size of the provided data. Designed for measuring XHR responses.
|
|
15
|
+
*
|
|
16
|
+
* @param {*} data - The data to be measured.
|
|
17
|
+
* @returns {(number|undefined)} - The size of the data or undefined if size cannot be determined.
|
|
18
|
+
*/
|
|
13
19
|
function dataSize(data) {
|
|
14
20
|
if (typeof data === 'string' && data.length) return data.length;
|
|
15
21
|
if (typeof data !== 'object') return undefined;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _dataSize = require("./data-size");
|
|
4
|
+
describe('dataSize', () => {
|
|
5
|
+
test('returns length of string', () => {
|
|
6
|
+
const str = 'Hello, world!';
|
|
7
|
+
expect((0, _dataSize.dataSize)(str)).toBe(str.length);
|
|
8
|
+
});
|
|
9
|
+
test('returns undefined for non-object, number, or empty string', () => {
|
|
10
|
+
expect((0, _dataSize.dataSize)(Infinity)).toBeUndefined();
|
|
11
|
+
expect((0, _dataSize.dataSize)(12345)).toBeUndefined(); // might not actually be by design, but this is how it works today
|
|
12
|
+
expect((0, _dataSize.dataSize)('')).toBeUndefined();
|
|
13
|
+
});
|
|
14
|
+
test('returns byte length of ArrayBuffer object', () => {
|
|
15
|
+
const buffer = new ArrayBuffer(8);
|
|
16
|
+
expect((0, _dataSize.dataSize)(buffer)).toBe(8);
|
|
17
|
+
});
|
|
18
|
+
test('returns size of Blob object', () => {
|
|
19
|
+
const blob = new Blob(['Hello, world!'], {
|
|
20
|
+
type: 'text/plain'
|
|
21
|
+
});
|
|
22
|
+
expect((0, _dataSize.dataSize)(blob)).toBe(blob.size);
|
|
23
|
+
});
|
|
24
|
+
test('returns undefined for FormData object', () => {
|
|
25
|
+
const formData = new FormData();
|
|
26
|
+
expect((0, _dataSize.dataSize)(formData)).toBeUndefined();
|
|
27
|
+
});
|
|
28
|
+
test('returns length of JSON string representation of object', () => {
|
|
29
|
+
const obj = {
|
|
30
|
+
str: 'Hello, world!',
|
|
31
|
+
num: 12345,
|
|
32
|
+
nestedObj: {
|
|
33
|
+
arr: [1, 2, 3]
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const expectedSize = JSON.stringify(obj).length;
|
|
37
|
+
expect((0, _dataSize.dataSize)(obj)).toBe(expectedSize);
|
|
38
|
+
});
|
|
39
|
+
test('returns undefined for object with toJSON method that throws an error', () => {
|
|
40
|
+
const obj = {
|
|
41
|
+
toJSON: () => {
|
|
42
|
+
throw new Error('Error in toJSON');
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
expect((0, _dataSize.dataSize)(obj)).toBeUndefined();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getGlobalScope = getGlobalScope;
|
|
7
|
-
exports.isWorkerScope = exports.isBrowserScope = exports.globalScope = void 0;
|
|
7
|
+
exports.isWorkerScope = exports.isBrowserScope = exports.initialLocation = exports.globalScope = void 0;
|
|
8
8
|
exports.resetScope = resetScope;
|
|
9
9
|
exports.setScope = setScope;
|
|
10
10
|
/* global globalThis, WorkerGlobalScope, WorkerNavigator */
|
|
@@ -25,6 +25,8 @@ let globalScope = (() => {
|
|
|
25
25
|
}
|
|
26
26
|
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".');
|
|
27
27
|
})();
|
|
28
|
+
exports.globalScope = globalScope;
|
|
29
|
+
const initialLocation = '' + globalScope.location;
|
|
28
30
|
|
|
29
31
|
/**
|
|
30
32
|
* The below methods are only used for testing and should be removed once the
|
|
@@ -32,7 +34,7 @@ let globalScope = (() => {
|
|
|
32
34
|
* tests/browser/protocol.browser.js
|
|
33
35
|
* tests/browser/obfuscate.browser.js
|
|
34
36
|
*/
|
|
35
|
-
exports.
|
|
37
|
+
exports.initialLocation = initialLocation;
|
|
36
38
|
function setScope(obj) {
|
|
37
39
|
exports.globalScope = globalScope = {
|
|
38
40
|
...obj
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.debounce = debounce;
|
|
7
|
+
exports.single = single;
|
|
8
|
+
/**
|
|
9
|
+
* Reduce the invocation of the supplied function so that it is only invoked
|
|
10
|
+
* once within a given timeout.
|
|
11
|
+
*
|
|
12
|
+
* If `wait` is `0`, the function will be invoked during the next tick.
|
|
13
|
+
* If `options.leading` is false or not provided, the function will be invoked
|
|
14
|
+
* N milliseconds after the last invocation of the returned function where
|
|
15
|
+
* N is the `timeout` value.
|
|
16
|
+
* If `options.leading` is true, the function will be invoked immediately upon
|
|
17
|
+
* the first invocation of the returned function and not again for N milliseconds
|
|
18
|
+
* where N is the `timeout` value.
|
|
19
|
+
* @param {function} func Function whose invocation should be limited so it is only invoked
|
|
20
|
+
* once within a given timeout period.
|
|
21
|
+
* @param {number} timeout Time in milliseconds that the function should only be invoked
|
|
22
|
+
* once within.
|
|
23
|
+
* @param {object} options Debounce options
|
|
24
|
+
* @param {boolean} options.leading Forces the function to be invoked on the first
|
|
25
|
+
* invocation of the returned function instead of N milliseconds after the last
|
|
26
|
+
* invocation.
|
|
27
|
+
* @returns {function} A wrapping function that will ensure the provided function
|
|
28
|
+
* is invoked only once within the given timeout.
|
|
29
|
+
*/
|
|
30
|
+
function debounce(func) {
|
|
31
|
+
var _this = this;
|
|
32
|
+
let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
|
|
33
|
+
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
34
|
+
const leading = options?.leading || false;
|
|
35
|
+
let timer;
|
|
36
|
+
return function () {
|
|
37
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
38
|
+
args[_key] = arguments[_key];
|
|
39
|
+
}
|
|
40
|
+
if (leading && timer === undefined) {
|
|
41
|
+
func.apply(_this, args);
|
|
42
|
+
timer = setTimeout(() => timer = clearTimeout(timer), timeout);
|
|
43
|
+
}
|
|
44
|
+
if (!leading) {
|
|
45
|
+
clearTimeout(timer);
|
|
46
|
+
timer = setTimeout(() => {
|
|
47
|
+
func.apply(_this, args);
|
|
48
|
+
}, timeout);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Reduce the invocation of the supplied function so that it is only invoked
|
|
55
|
+
* once.
|
|
56
|
+
* @param {function} func Function whose invocation should be limited so it is only invoked
|
|
57
|
+
* once.
|
|
58
|
+
* @returns {function} A wrapping function that will ensure the provided function
|
|
59
|
+
* is invoked only once.
|
|
60
|
+
*/
|
|
61
|
+
function single(func) {
|
|
62
|
+
var _this2 = this;
|
|
63
|
+
let called = false;
|
|
64
|
+
return function () {
|
|
65
|
+
if (!called) {
|
|
66
|
+
called = true;
|
|
67
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
68
|
+
args[_key2] = arguments[_key2];
|
|
69
|
+
}
|
|
70
|
+
func.apply(_this2, args);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _invoke = require("./invoke");
|
|
4
|
+
jest.useFakeTimers();
|
|
5
|
+
describe('debounce', () => {
|
|
6
|
+
test('should run the supplied function after 100ms', () => {
|
|
7
|
+
let mockCallback = jest.fn();
|
|
8
|
+
let debouncedMethod = (0, _invoke.debounce)(mockCallback, 100);
|
|
9
|
+
execFnTimes(debouncedMethod, 100);
|
|
10
|
+
expect(mockCallback).not.toHaveBeenCalled();
|
|
11
|
+
jest.advanceTimersByTime(1000);
|
|
12
|
+
expect(mockCallback).toHaveBeenCalledTimes(1);
|
|
13
|
+
});
|
|
14
|
+
test('should rerun the supplied function when called again after 100ms', () => {
|
|
15
|
+
let mockCallback = jest.fn();
|
|
16
|
+
let debouncedMethod = (0, _invoke.debounce)(mockCallback, 100);
|
|
17
|
+
execFnTimes(debouncedMethod, 100);
|
|
18
|
+
jest.advanceTimersByTime(200);
|
|
19
|
+
execFnTimes(debouncedMethod, 100);
|
|
20
|
+
jest.advanceTimersByTime(2000);
|
|
21
|
+
expect(mockCallback).toHaveBeenCalledTimes(2);
|
|
22
|
+
});
|
|
23
|
+
test('should run the supplied function on the first event and debounce subsequent events', () => {
|
|
24
|
+
let mockCallback = jest.fn();
|
|
25
|
+
let debouncedMethod = (0, _invoke.debounce)(mockCallback, 100, {
|
|
26
|
+
leading: true
|
|
27
|
+
});
|
|
28
|
+
execFnTimes(debouncedMethod, 100);
|
|
29
|
+
expect(mockCallback).toHaveBeenCalledTimes(1);
|
|
30
|
+
jest.advanceTimersByTime(200);
|
|
31
|
+
expect(mockCallback).toHaveBeenCalledTimes(1);
|
|
32
|
+
execFnTimes(debouncedMethod, 100);
|
|
33
|
+
jest.advanceTimersByTime(200);
|
|
34
|
+
expect(mockCallback).toHaveBeenCalledTimes(2);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
describe('single', () => {
|
|
38
|
+
test('should run the supplied function only once', () => {
|
|
39
|
+
let mockCallback = jest.fn();
|
|
40
|
+
let singleMethod = (0, _invoke.single)(mockCallback, 100);
|
|
41
|
+
execFnTimes(singleMethod, 100);
|
|
42
|
+
expect(mockCallback).toHaveBeenCalledTimes(1);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
function execFnTimes(fn, count) {
|
|
46
|
+
for (let i = 0; i < count; i++) {
|
|
47
|
+
fn();
|
|
48
|
+
}
|
|
49
|
+
}
|