@cloudcare/browser-rum 3.3.14 → 3.3.15
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 +48 -0
- package/README.zh.md +35 -0
- package/bundle/dataflux-rum.js +1 -1
- package/cjs/boot/buildEnv.js +1 -1
- package/cjs/boot/perStartRum.js +3 -0
- package/cjs/boot/perStartRum.js.map +1 -1
- package/cjs/boot/recorderApi.js +72 -56
- package/cjs/boot/recorderApi.js.map +1 -1
- package/cjs/boot/rumPublicApi.js +35 -3
- package/cjs/boot/rumPublicApi.js.map +1 -1
- package/cjs/domain/assembly.js +7 -1
- package/cjs/domain/assembly.js.map +1 -1
- package/cjs/domain/configuration.js +62 -0
- package/cjs/domain/configuration.js.map +1 -1
- package/cjs/domain/deflate/deflateWorker.js +1 -1
- package/cjs/domain/replay/record/canvasEncodeWorker.js +1 -1
- package/cjs/domain/requestCollection.js +3 -1
- package/cjs/domain/requestCollection.js.map +1 -1
- package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +5 -0
- package/cjs/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
- package/cjs/domain/tracing/tracer.js +5 -1
- package/cjs/domain/tracing/tracer.js.map +1 -1
- package/cjs/domain/visitorId/index.js +379 -0
- package/cjs/domain/visitorId/index.js.map +1 -0
- package/esm/boot/buildEnv.js +1 -1
- package/esm/boot/perStartRum.js +3 -0
- package/esm/boot/perStartRum.js.map +1 -1
- package/esm/boot/recorderApi.js +72 -56
- package/esm/boot/recorderApi.js.map +1 -1
- package/esm/boot/rumPublicApi.js +36 -4
- package/esm/boot/rumPublicApi.js.map +1 -1
- package/esm/domain/assembly.js +7 -1
- package/esm/domain/assembly.js.map +1 -1
- package/esm/domain/configuration.js +62 -1
- package/esm/domain/configuration.js.map +1 -1
- package/esm/domain/deflate/deflateWorker.js +1 -1
- package/esm/domain/replay/record/canvasEncodeWorker.js +1 -1
- package/esm/domain/requestCollection.js +4 -2
- package/esm/domain/requestCollection.js.map +1 -1
- package/esm/domain/rumEventsCollection/resource/resourceCollection.js +5 -0
- package/esm/domain/rumEventsCollection/resource/resourceCollection.js.map +1 -1
- package/esm/domain/tracing/tracer.js +6 -2
- package/esm/domain/tracing/tracer.js.map +1 -1
- package/esm/domain/visitorId/index.js +368 -0
- package/esm/domain/visitorId/index.js.map +1 -0
- package/package.json +4 -4
- package/src/boot/perStartRum.js +4 -0
- package/src/boot/recorderApi.js +73 -59
- package/src/boot/rumPublicApi.js +38 -4
- package/src/domain/assembly.js +13 -1
- package/src/domain/configuration.js +154 -0
- package/src/domain/requestCollection.js +4 -1
- package/src/domain/rumEventsCollection/resource/resourceCollection.js +3 -0
- package/src/domain/tracing/tracer.js +15 -2
- package/src/domain/visitorId/index.js +522 -0
- package/types/index.d.ts +2 -0
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DOM_EVENT,
|
|
3
|
+
ONE_HOUR,
|
|
4
|
+
UUID,
|
|
5
|
+
WeakSet,
|
|
6
|
+
addEventListener,
|
|
7
|
+
display,
|
|
8
|
+
getOrigin,
|
|
9
|
+
initFetchObservable,
|
|
10
|
+
initXhrObservable,
|
|
11
|
+
isRequest,
|
|
12
|
+
noop,
|
|
13
|
+
normalizeUrl,
|
|
14
|
+
shallowClone,
|
|
15
|
+
timeStampNow
|
|
16
|
+
} from '@cloudcare/browser-core'
|
|
17
|
+
import { isAllowedRequestUrl } from '../rumEventsCollection/resource/resourceUtils'
|
|
18
|
+
import { createRandomHexIdentifier } from '../tracing/traceIdentifier'
|
|
19
|
+
|
|
20
|
+
export var VISITOR_ID_CONTEXT_KEY = 'visitor_id'
|
|
21
|
+
export var VISITOR_ID_STORAGE_KEY = '_gc_visitor_id'
|
|
22
|
+
export var DEFAULT_VISITOR_ID_TTL_HOURS = 24
|
|
23
|
+
|
|
24
|
+
var PERSISTENT_ID_PREFIX = 'p'
|
|
25
|
+
var SESSION_ID_PREFIX = 's'
|
|
26
|
+
var VISITOR_ID_PATTERN = /^[ps][0-9a-f]{32}$/
|
|
27
|
+
var FETCH_INIT_FIELDS = [
|
|
28
|
+
'method', 'headers', 'body', 'referrer', 'referrerPolicy', 'mode',
|
|
29
|
+
'credentials', 'cache', 'redirect', 'integrity', 'keepalive', 'signal',
|
|
30
|
+
'window', 'duplex', 'priority', 'attributionReporting', 'browsingTopics'
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
export function startVisitorId(configuration) {
|
|
34
|
+
if (!configuration.visitorId.enabled) {
|
|
35
|
+
return createVisitorIdStub()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var manager = createVisitorIdManager(configuration.visitorId)
|
|
39
|
+
var requestCollection = startVisitorIdRequestCollection(
|
|
40
|
+
configuration,
|
|
41
|
+
manager
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
getId: manager.getId,
|
|
46
|
+
reset: manager.reset,
|
|
47
|
+
stop: function () {
|
|
48
|
+
requestCollection.stop()
|
|
49
|
+
manager.stop()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function createVisitorIdManager(visitorIdConfiguration, options) {
|
|
55
|
+
if (options === undefined) {
|
|
56
|
+
options = {}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var getNow = options.getNow || timeStampNow
|
|
60
|
+
var createIdentifier = options.createIdentifier || createIdentifierValue
|
|
61
|
+
var localStorage = getStorage(options, 'localStorage')
|
|
62
|
+
var sessionStorage = getStorage(options, 'sessionStorage')
|
|
63
|
+
var ttl = visitorIdConfiguration.ttlHours * ONE_HOUR
|
|
64
|
+
var currentRecord = loadOrCreateRecord()
|
|
65
|
+
var storageListener = localStorage
|
|
66
|
+
? addEventListener(window, DOM_EVENT.STORAGE, function (event) {
|
|
67
|
+
if (event.key !== VISITOR_ID_STORAGE_KEY) {
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
if (event.storageArea && event.storageArea !== localStorage) {
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
// Storage events may be queued before a later reset in this or another tab.
|
|
74
|
+
// Read the current value instead of replaying the event's stale snapshot.
|
|
75
|
+
var nextRecord = readRecord(
|
|
76
|
+
localStorage,
|
|
77
|
+
PERSISTENT_ID_PREFIX,
|
|
78
|
+
getNow(),
|
|
79
|
+
ttl
|
|
80
|
+
)
|
|
81
|
+
if (
|
|
82
|
+
nextRecord &&
|
|
83
|
+
currentRecord &&
|
|
84
|
+
compareRecords(nextRecord, currentRecord) < 0
|
|
85
|
+
) {
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
currentRecord = nextRecord
|
|
89
|
+
})
|
|
90
|
+
: { stop: noop }
|
|
91
|
+
|
|
92
|
+
function loadOrCreateRecord() {
|
|
93
|
+
var now = getNow()
|
|
94
|
+
var persistentRecord = readRecord(
|
|
95
|
+
localStorage,
|
|
96
|
+
PERSISTENT_ID_PREFIX,
|
|
97
|
+
now,
|
|
98
|
+
ttl
|
|
99
|
+
)
|
|
100
|
+
var sessionRecord = readRecord(sessionStorage, SESSION_ID_PREFIX, now, ttl)
|
|
101
|
+
if (sessionRecord && persistentRecord) {
|
|
102
|
+
var order = compareRecords(sessionRecord, persistentRecord)
|
|
103
|
+
return order > 0 ||
|
|
104
|
+
(order === 0 &&
|
|
105
|
+
sessionRecord.revision === undefined &&
|
|
106
|
+
persistentRecord.revision === undefined)
|
|
107
|
+
? sessionRecord
|
|
108
|
+
: persistentRecord
|
|
109
|
+
}
|
|
110
|
+
if (sessionRecord || persistentRecord) {
|
|
111
|
+
return sessionRecord || persistentRecord
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return createAndPersistRecord(now)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function createAndPersistRecord(now) {
|
|
118
|
+
var identifier = createIdentifier()
|
|
119
|
+
// Date.now() can return the same value for several resets. Keep their order
|
|
120
|
+
// independently from the TTL timestamp, including when session cleanup fails.
|
|
121
|
+
var revision = 0
|
|
122
|
+
;[
|
|
123
|
+
currentRecord,
|
|
124
|
+
readRecord(localStorage, PERSISTENT_ID_PREFIX, now, ttl),
|
|
125
|
+
readRecord(sessionStorage, SESSION_ID_PREFIX, now, ttl)
|
|
126
|
+
].forEach(function (record) {
|
|
127
|
+
if (record && record.createdAt === now) {
|
|
128
|
+
revision = Math.max(revision, (record.revision || 0) + 1)
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
var persistentRecord = {
|
|
132
|
+
id: PERSISTENT_ID_PREFIX + identifier,
|
|
133
|
+
createdAt: now,
|
|
134
|
+
revision: revision
|
|
135
|
+
}
|
|
136
|
+
var storedPersistentRecord = writeRecord(
|
|
137
|
+
localStorage,
|
|
138
|
+
persistentRecord,
|
|
139
|
+
PERSISTENT_ID_PREFIX,
|
|
140
|
+
now,
|
|
141
|
+
ttl
|
|
142
|
+
)
|
|
143
|
+
if (storedPersistentRecord) {
|
|
144
|
+
try {
|
|
145
|
+
sessionStorage && sessionStorage.removeItem(VISITOR_ID_STORAGE_KEY)
|
|
146
|
+
} catch (error) {
|
|
147
|
+
// Ordering metadata still supersedes the stale session record.
|
|
148
|
+
}
|
|
149
|
+
return storedPersistentRecord
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
var sessionRecord = {
|
|
153
|
+
id: SESSION_ID_PREFIX + identifier,
|
|
154
|
+
createdAt: now,
|
|
155
|
+
revision: revision
|
|
156
|
+
}
|
|
157
|
+
return (
|
|
158
|
+
writeRecord(sessionStorage, sessionRecord, SESSION_ID_PREFIX, now, ttl) ||
|
|
159
|
+
sessionRecord
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function ensureCurrentRecord() {
|
|
164
|
+
var now = getNow()
|
|
165
|
+
if (!isValidRecord(currentRecord, undefined, now, ttl)) {
|
|
166
|
+
currentRecord = loadOrCreateRecord()
|
|
167
|
+
}
|
|
168
|
+
return currentRecord
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
getId: function () {
|
|
173
|
+
return ensureCurrentRecord().id
|
|
174
|
+
},
|
|
175
|
+
reset: function () {
|
|
176
|
+
currentRecord = createAndPersistRecord(getNow())
|
|
177
|
+
},
|
|
178
|
+
stop: storageListener.stop
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function startVisitorIdRequestCollection(configuration, manager) {
|
|
183
|
+
var xhrHeaderReservation
|
|
184
|
+
var xhrSubscription = initXhrObservable().subscribe(function (context) {
|
|
185
|
+
if (context.state === 'start') {
|
|
186
|
+
context.visitorId = xhrHeaderReservation.apply(context)
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
xhrHeaderReservation = startVisitorIdXhrHeaderReservation(
|
|
190
|
+
configuration,
|
|
191
|
+
manager
|
|
192
|
+
)
|
|
193
|
+
var fetchSubscription = initFetchObservable().subscribe(function (context) {
|
|
194
|
+
if (
|
|
195
|
+
context.state === 'start' &&
|
|
196
|
+
shouldInjectVisitorId(configuration, context.url)
|
|
197
|
+
) {
|
|
198
|
+
var visitorId = manager.getId()
|
|
199
|
+
if (
|
|
200
|
+
injectVisitorIdIntoFetch(
|
|
201
|
+
context,
|
|
202
|
+
configuration.visitorId.header,
|
|
203
|
+
visitorId
|
|
204
|
+
)
|
|
205
|
+
) {
|
|
206
|
+
context.visitorId = visitorId
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
stop: function () {
|
|
213
|
+
xhrHeaderReservation.stop()
|
|
214
|
+
xhrSubscription.unsubscribe()
|
|
215
|
+
fetchSubscription.unsubscribe()
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function shouldInjectVisitorId(configuration, url) {
|
|
221
|
+
return (
|
|
222
|
+
isAllowedRequestUrl(configuration, url) &&
|
|
223
|
+
matchesVisitorIdUrl(configuration.visitorId.match, url)
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function matchesVisitorIdUrl(matchOptions, url) {
|
|
228
|
+
for (var index = 0; index < matchOptions.length; index += 1) {
|
|
229
|
+
var matchOption = matchOptions[index]
|
|
230
|
+
try {
|
|
231
|
+
if (typeof matchOption === 'function' && matchOption(url)) {
|
|
232
|
+
return true
|
|
233
|
+
}
|
|
234
|
+
if (matchOption instanceof RegExp) {
|
|
235
|
+
var matched
|
|
236
|
+
try {
|
|
237
|
+
matchOption.lastIndex = 0
|
|
238
|
+
matched = matchOption.test(url)
|
|
239
|
+
} finally {
|
|
240
|
+
matchOption.lastIndex = 0
|
|
241
|
+
}
|
|
242
|
+
if (matched) {
|
|
243
|
+
return true
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (
|
|
247
|
+
typeof matchOption === 'string' &&
|
|
248
|
+
url.indexOf(matchOption) === 0 &&
|
|
249
|
+
getOrigin(matchOption) === getOrigin(url)
|
|
250
|
+
) {
|
|
251
|
+
return true
|
|
252
|
+
}
|
|
253
|
+
} catch (error) {
|
|
254
|
+
display.error('Visitor ID URL matcher threw an error:', error)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return false
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function injectVisitorIdIntoFetch(context, header, visitorId) {
|
|
261
|
+
try {
|
|
262
|
+
var originalInit = context.init
|
|
263
|
+
if (
|
|
264
|
+
originalInit != null &&
|
|
265
|
+
typeof originalInit !== 'object' &&
|
|
266
|
+
typeof originalInit !== 'function'
|
|
267
|
+
) {
|
|
268
|
+
return
|
|
269
|
+
}
|
|
270
|
+
var init = shallowClone(originalInit)
|
|
271
|
+
// RequestInit is a WebIDL dictionary: inherited and non-enumerable fields
|
|
272
|
+
// count too. Materialize them so later tracing instrumentation preserves them.
|
|
273
|
+
FETCH_INIT_FIELDS.forEach(function (field) {
|
|
274
|
+
if (
|
|
275
|
+
originalInit &&
|
|
276
|
+
field in originalInit &&
|
|
277
|
+
!Object.prototype.hasOwnProperty.call(init, field)
|
|
278
|
+
) {
|
|
279
|
+
init[field] = originalInit[field]
|
|
280
|
+
}
|
|
281
|
+
})
|
|
282
|
+
var headers = new Headers(
|
|
283
|
+
init.headers === undefined && isRequest(context.input)
|
|
284
|
+
? context.input.headers
|
|
285
|
+
: init.headers
|
|
286
|
+
)
|
|
287
|
+
if (headers.has(header)) {
|
|
288
|
+
displayVisitorIdHeaderOverrideWarning('fetch')
|
|
289
|
+
}
|
|
290
|
+
headers.set(header, visitorId)
|
|
291
|
+
init.headers = headers
|
|
292
|
+
// Avoid constructing a Request here: that can consume the caller's body even
|
|
293
|
+
// if a later header operation fails. Native fetch handles the body once.
|
|
294
|
+
context.init = init
|
|
295
|
+
return true
|
|
296
|
+
} catch (error) {
|
|
297
|
+
display.error('Unable to inject the Visitor ID header into fetch:', error)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function startVisitorIdXhrHeaderReservation(configuration, manager) {
|
|
302
|
+
var header = configuration.visitorId.header
|
|
303
|
+
var matchedXhrs = new WeakSet()
|
|
304
|
+
var callerHeaderXhrs = new WeakSet()
|
|
305
|
+
var sentXhrs = new WeakSet()
|
|
306
|
+
var injectedIds = new WeakMap()
|
|
307
|
+
var originalOpen = XMLHttpRequest.prototype.open
|
|
308
|
+
var stopped = false
|
|
309
|
+
var openInstrumentation = function () {
|
|
310
|
+
if (stopped) {
|
|
311
|
+
return originalOpen.apply(this, arguments)
|
|
312
|
+
}
|
|
313
|
+
var xhr = this
|
|
314
|
+
matchedXhrs.delete(xhr)
|
|
315
|
+
callerHeaderXhrs.delete(xhr)
|
|
316
|
+
sentXhrs.delete(xhr)
|
|
317
|
+
injectedIds.delete(xhr)
|
|
318
|
+
try {
|
|
319
|
+
if (
|
|
320
|
+
shouldInjectVisitorId(configuration, normalizeUrl(String(arguments[1])))
|
|
321
|
+
) {
|
|
322
|
+
matchedXhrs.add(xhr)
|
|
323
|
+
}
|
|
324
|
+
} catch (error) {
|
|
325
|
+
// Let native open() validate the URL and preserve its original exception.
|
|
326
|
+
}
|
|
327
|
+
try {
|
|
328
|
+
// open() synchronously dispatches OPENED; handlers may set headers/send.
|
|
329
|
+
return originalOpen.apply(xhr, arguments)
|
|
330
|
+
} catch (error) {
|
|
331
|
+
matchedXhrs.delete(xhr)
|
|
332
|
+
callerHeaderXhrs.delete(xhr)
|
|
333
|
+
sentXhrs.delete(xhr)
|
|
334
|
+
injectedIds.delete(xhr)
|
|
335
|
+
throw error
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
XMLHttpRequest.prototype.open = openInstrumentation
|
|
339
|
+
var originalSend = XMLHttpRequest.prototype.send
|
|
340
|
+
var sendInstrumentation = function () {
|
|
341
|
+
if (stopped || !matchedXhrs.has(this)) {
|
|
342
|
+
return originalSend.apply(this, arguments)
|
|
343
|
+
}
|
|
344
|
+
var xhr = this
|
|
345
|
+
var wasSent = sentXhrs.has(xhr)
|
|
346
|
+
sentXhrs.add(xhr)
|
|
347
|
+
try {
|
|
348
|
+
return originalSend.apply(xhr, arguments)
|
|
349
|
+
} catch (error) {
|
|
350
|
+
// A failed first send leaves the native XHR open for header edits/retry.
|
|
351
|
+
// A failed duplicate send must not reopen an already-sent reservation.
|
|
352
|
+
if (!wasSent) {
|
|
353
|
+
sentXhrs.delete(xhr)
|
|
354
|
+
}
|
|
355
|
+
throw error
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
XMLHttpRequest.prototype.send = sendInstrumentation
|
|
359
|
+
var originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader
|
|
360
|
+
var setRequestHeaderInstrumentation = function (name, value) {
|
|
361
|
+
if (
|
|
362
|
+
!stopped &&
|
|
363
|
+
typeof name === 'string' &&
|
|
364
|
+
name.toLowerCase() === header &&
|
|
365
|
+
matchedXhrs.has(this) &&
|
|
366
|
+
!sentXhrs.has(this)
|
|
367
|
+
) {
|
|
368
|
+
callerHeaderXhrs.add(this)
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
return originalSetRequestHeader.apply(this, arguments)
|
|
372
|
+
}
|
|
373
|
+
XMLHttpRequest.prototype.setRequestHeader = setRequestHeaderInstrumentation
|
|
374
|
+
|
|
375
|
+
return {
|
|
376
|
+
apply: function (context) {
|
|
377
|
+
var xhr = context.xhr
|
|
378
|
+
if (!matchedXhrs.has(xhr)) {
|
|
379
|
+
return
|
|
380
|
+
}
|
|
381
|
+
if (callerHeaderXhrs.delete(xhr)) {
|
|
382
|
+
displayVisitorIdHeaderOverrideWarning('XHR')
|
|
383
|
+
}
|
|
384
|
+
if (injectedIds.has(xhr)) {
|
|
385
|
+
// Native send() may throw before sending and leave the XHR reusable.
|
|
386
|
+
// Headers cannot be replaced once set, so retain the first injected ID
|
|
387
|
+
// even if resetVisitorId() was called before the retry.
|
|
388
|
+
return injectedIds.get(xhr)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
var visitorId = manager.getId()
|
|
392
|
+
try {
|
|
393
|
+
originalSetRequestHeader.call(xhr, header, visitorId)
|
|
394
|
+
injectedIds.set(xhr, visitorId)
|
|
395
|
+
return visitorId
|
|
396
|
+
} catch (error) {
|
|
397
|
+
display.error(
|
|
398
|
+
'Unable to inject the Visitor ID header into XHR:',
|
|
399
|
+
error
|
|
400
|
+
)
|
|
401
|
+
return
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
stop: function () {
|
|
405
|
+
stopped = true
|
|
406
|
+
if (XMLHttpRequest.prototype.open === openInstrumentation) {
|
|
407
|
+
XMLHttpRequest.prototype.open = originalOpen
|
|
408
|
+
}
|
|
409
|
+
if (XMLHttpRequest.prototype.send === sendInstrumentation) {
|
|
410
|
+
XMLHttpRequest.prototype.send = originalSend
|
|
411
|
+
}
|
|
412
|
+
if (
|
|
413
|
+
XMLHttpRequest.prototype.setRequestHeader ===
|
|
414
|
+
setRequestHeaderInstrumentation
|
|
415
|
+
) {
|
|
416
|
+
XMLHttpRequest.prototype.setRequestHeader = originalSetRequestHeader
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function displayVisitorIdHeaderOverrideWarning(requestType) {
|
|
423
|
+
display.warn(
|
|
424
|
+
'Visitor ID header is already set on this ' +
|
|
425
|
+
requestType +
|
|
426
|
+
' request. It was replaced with the SDK-managed value.'
|
|
427
|
+
)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function getStorage(options, storageName) {
|
|
431
|
+
if (Object.prototype.hasOwnProperty.call(options, storageName)) {
|
|
432
|
+
return options[storageName]
|
|
433
|
+
}
|
|
434
|
+
try {
|
|
435
|
+
return window[storageName]
|
|
436
|
+
} catch (error) {
|
|
437
|
+
return undefined
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function readRecord(storage, expectedPrefix, now, ttl) {
|
|
442
|
+
if (!storage) {
|
|
443
|
+
return undefined
|
|
444
|
+
}
|
|
445
|
+
try {
|
|
446
|
+
return parseRecord(
|
|
447
|
+
storage.getItem(VISITOR_ID_STORAGE_KEY),
|
|
448
|
+
expectedPrefix,
|
|
449
|
+
now,
|
|
450
|
+
ttl
|
|
451
|
+
)
|
|
452
|
+
} catch (error) {
|
|
453
|
+
return undefined
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function writeRecord(storage, record, expectedPrefix, now, ttl) {
|
|
458
|
+
if (!storage) {
|
|
459
|
+
return undefined
|
|
460
|
+
}
|
|
461
|
+
try {
|
|
462
|
+
storage.setItem(VISITOR_ID_STORAGE_KEY, JSON.stringify(record))
|
|
463
|
+
} catch (error) {
|
|
464
|
+
return undefined
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
var storedRecord = readRecord(storage, expectedPrefix, now, ttl)
|
|
468
|
+
return storedRecord &&
|
|
469
|
+
storedRecord.id === record.id &&
|
|
470
|
+
compareRecords(storedRecord, record) === 0
|
|
471
|
+
? storedRecord
|
|
472
|
+
: undefined
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function parseRecord(rawRecord, expectedPrefix, now, ttl) {
|
|
476
|
+
if (!rawRecord) {
|
|
477
|
+
return undefined
|
|
478
|
+
}
|
|
479
|
+
try {
|
|
480
|
+
var record = JSON.parse(rawRecord)
|
|
481
|
+
return isValidRecord(record, expectedPrefix, now, ttl) ? record : undefined
|
|
482
|
+
} catch (error) {
|
|
483
|
+
return undefined
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function isValidRecord(record, expectedPrefix, now, ttl) {
|
|
488
|
+
return (
|
|
489
|
+
!!record &&
|
|
490
|
+
VISITOR_ID_PATTERN.test(record.id) &&
|
|
491
|
+
(!expectedPrefix || record.id.charAt(0) === expectedPrefix) &&
|
|
492
|
+
typeof record.createdAt === 'number' &&
|
|
493
|
+
(record.revision === undefined ||
|
|
494
|
+
(typeof record.revision === 'number' &&
|
|
495
|
+
isFinite(record.revision) &&
|
|
496
|
+
record.revision >= 0 &&
|
|
497
|
+
Math.floor(record.revision) === record.revision)) &&
|
|
498
|
+
record.createdAt <= now &&
|
|
499
|
+
record.createdAt + ttl > now
|
|
500
|
+
)
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function compareRecords(first, second) {
|
|
504
|
+
return first.createdAt - second.createdAt ||
|
|
505
|
+
(first.revision || 0) - (second.revision || 0)
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function createIdentifierValue() {
|
|
509
|
+
try {
|
|
510
|
+
return createRandomHexIdentifier(16)
|
|
511
|
+
} catch (error) {
|
|
512
|
+
return UUID().replace(/-/g, '')
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function createVisitorIdStub() {
|
|
517
|
+
return {
|
|
518
|
+
getId: noop,
|
|
519
|
+
reset: noop,
|
|
520
|
+
stop: noop
|
|
521
|
+
}
|
|
522
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -246,6 +246,8 @@ export declare const datafluxRum: {
|
|
|
246
246
|
getGlobalContext: () => Context
|
|
247
247
|
setGlobalContext: (context: any) => void
|
|
248
248
|
clearGlobalContext: () => void
|
|
249
|
+
/** Rotates the SDK-managed Visitor ID immediately. */
|
|
250
|
+
resetVisitorId: () => void
|
|
249
251
|
getInternalContext: (
|
|
250
252
|
startTime?: number | undefined
|
|
251
253
|
) => InternalContext | undefined
|