@firebase/performance 0.6.9 → 0.6.10
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/dist/esm/index.esm2017.js +2 -2
- package/dist/esm/index.esm2017.js.map +1 -1
- package/dist/index.cjs.js +290 -323
- package/dist/index.cjs.js.map +1 -1
- package/package.json +6 -8
- package/dist/esm/index.esm.js +0 -1554
- package/dist/esm/index.esm.js.map +0 -1
package/dist/esm/index.esm.js
DELETED
|
@@ -1,1554 +0,0 @@
|
|
|
1
|
-
import { ErrorFactory, areCookiesEnabled, isIndexedDBAvailable, validateIndexedDBOpenable, getModularInstance, deepEqual } from '@firebase/util';
|
|
2
|
-
import { Logger, LogLevel } from '@firebase/logger';
|
|
3
|
-
import { __spreadArray, __assign } from 'tslib';
|
|
4
|
-
import { getApp, _getProvider, _registerComponent, registerVersion } from '@firebase/app';
|
|
5
|
-
import { Component } from '@firebase/component';
|
|
6
|
-
import '@firebase/installations';
|
|
7
|
-
|
|
8
|
-
var name = "@firebase/performance";
|
|
9
|
-
var version = "0.6.9";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @license
|
|
13
|
-
* Copyright 2020 Google LLC
|
|
14
|
-
*
|
|
15
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
16
|
-
* you may not use this file except in compliance with the License.
|
|
17
|
-
* You may obtain a copy of the License at
|
|
18
|
-
*
|
|
19
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
20
|
-
*
|
|
21
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
22
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
23
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
24
|
-
* See the License for the specific language governing permissions and
|
|
25
|
-
* limitations under the License.
|
|
26
|
-
*/
|
|
27
|
-
var SDK_VERSION = version;
|
|
28
|
-
/** The prefix for start User Timing marks used for creating Traces. */
|
|
29
|
-
var TRACE_START_MARK_PREFIX = 'FB-PERF-TRACE-START';
|
|
30
|
-
/** The prefix for stop User Timing marks used for creating Traces. */
|
|
31
|
-
var TRACE_STOP_MARK_PREFIX = 'FB-PERF-TRACE-STOP';
|
|
32
|
-
/** The prefix for User Timing measure used for creating Traces. */
|
|
33
|
-
var TRACE_MEASURE_PREFIX = 'FB-PERF-TRACE-MEASURE';
|
|
34
|
-
/** The prefix for out of the box page load Trace name. */
|
|
35
|
-
var OOB_TRACE_PAGE_LOAD_PREFIX = '_wt_';
|
|
36
|
-
var FIRST_PAINT_COUNTER_NAME = '_fp';
|
|
37
|
-
var FIRST_CONTENTFUL_PAINT_COUNTER_NAME = '_fcp';
|
|
38
|
-
var FIRST_INPUT_DELAY_COUNTER_NAME = '_fid';
|
|
39
|
-
var CONFIG_LOCAL_STORAGE_KEY = '@firebase/performance/config';
|
|
40
|
-
var CONFIG_EXPIRY_LOCAL_STORAGE_KEY = '@firebase/performance/configexpire';
|
|
41
|
-
var SERVICE = 'performance';
|
|
42
|
-
var SERVICE_NAME = 'Performance';
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @license
|
|
46
|
-
* Copyright 2020 Google LLC
|
|
47
|
-
*
|
|
48
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
49
|
-
* you may not use this file except in compliance with the License.
|
|
50
|
-
* You may obtain a copy of the License at
|
|
51
|
-
*
|
|
52
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
53
|
-
*
|
|
54
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
55
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
56
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
57
|
-
* See the License for the specific language governing permissions and
|
|
58
|
-
* limitations under the License.
|
|
59
|
-
*/
|
|
60
|
-
var _a;
|
|
61
|
-
var ERROR_DESCRIPTION_MAP = (_a = {},
|
|
62
|
-
_a["trace started" /* ErrorCode.TRACE_STARTED_BEFORE */] = 'Trace {$traceName} was started before.',
|
|
63
|
-
_a["trace stopped" /* ErrorCode.TRACE_STOPPED_BEFORE */] = 'Trace {$traceName} is not running.',
|
|
64
|
-
_a["nonpositive trace startTime" /* ErrorCode.NONPOSITIVE_TRACE_START_TIME */] = 'Trace {$traceName} startTime should be positive.',
|
|
65
|
-
_a["nonpositive trace duration" /* ErrorCode.NONPOSITIVE_TRACE_DURATION */] = 'Trace {$traceName} duration should be positive.',
|
|
66
|
-
_a["no window" /* ErrorCode.NO_WINDOW */] = 'Window is not available.',
|
|
67
|
-
_a["no app id" /* ErrorCode.NO_APP_ID */] = 'App id is not available.',
|
|
68
|
-
_a["no project id" /* ErrorCode.NO_PROJECT_ID */] = 'Project id is not available.',
|
|
69
|
-
_a["no api key" /* ErrorCode.NO_API_KEY */] = 'Api key is not available.',
|
|
70
|
-
_a["invalid cc log" /* ErrorCode.INVALID_CC_LOG */] = 'Attempted to queue invalid cc event',
|
|
71
|
-
_a["FB not default" /* ErrorCode.FB_NOT_DEFAULT */] = 'Performance can only start when Firebase app instance is the default one.',
|
|
72
|
-
_a["RC response not ok" /* ErrorCode.RC_NOT_OK */] = 'RC response is not ok',
|
|
73
|
-
_a["invalid attribute name" /* ErrorCode.INVALID_ATTRIBUTE_NAME */] = 'Attribute name {$attributeName} is invalid.',
|
|
74
|
-
_a["invalid attribute value" /* ErrorCode.INVALID_ATTRIBUTE_VALUE */] = 'Attribute value {$attributeValue} is invalid.',
|
|
75
|
-
_a["invalid custom metric name" /* ErrorCode.INVALID_CUSTOM_METRIC_NAME */] = 'Custom metric name {$customMetricName} is invalid',
|
|
76
|
-
_a["invalid String merger input" /* ErrorCode.INVALID_STRING_MERGER_PARAMETER */] = 'Input for String merger is invalid, contact support team to resolve.',
|
|
77
|
-
_a["already initialized" /* ErrorCode.ALREADY_INITIALIZED */] = 'initializePerformance() has already been called with ' +
|
|
78
|
-
'different options. To avoid this error, call initializePerformance() with the ' +
|
|
79
|
-
'same options as when it was originally called, or call getPerformance() to return the' +
|
|
80
|
-
' already initialized instance.',
|
|
81
|
-
_a);
|
|
82
|
-
var ERROR_FACTORY = new ErrorFactory(SERVICE, SERVICE_NAME, ERROR_DESCRIPTION_MAP);
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* @license
|
|
86
|
-
* Copyright 2020 Google LLC
|
|
87
|
-
*
|
|
88
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
89
|
-
* you may not use this file except in compliance with the License.
|
|
90
|
-
* You may obtain a copy of the License at
|
|
91
|
-
*
|
|
92
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
93
|
-
*
|
|
94
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
95
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
96
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
97
|
-
* See the License for the specific language governing permissions and
|
|
98
|
-
* limitations under the License.
|
|
99
|
-
*/
|
|
100
|
-
var consoleLogger = new Logger(SERVICE_NAME);
|
|
101
|
-
consoleLogger.logLevel = LogLevel.INFO;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* @license
|
|
105
|
-
* Copyright 2020 Google LLC
|
|
106
|
-
*
|
|
107
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
108
|
-
* you may not use this file except in compliance with the License.
|
|
109
|
-
* You may obtain a copy of the License at
|
|
110
|
-
*
|
|
111
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
112
|
-
*
|
|
113
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
114
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
115
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
116
|
-
* See the License for the specific language governing permissions and
|
|
117
|
-
* limitations under the License.
|
|
118
|
-
*/
|
|
119
|
-
var apiInstance;
|
|
120
|
-
var windowInstance;
|
|
121
|
-
/**
|
|
122
|
-
* This class holds a reference to various browser related objects injected by
|
|
123
|
-
* set methods.
|
|
124
|
-
*/
|
|
125
|
-
var Api = /** @class */ (function () {
|
|
126
|
-
function Api(window) {
|
|
127
|
-
this.window = window;
|
|
128
|
-
if (!window) {
|
|
129
|
-
throw ERROR_FACTORY.create("no window" /* ErrorCode.NO_WINDOW */);
|
|
130
|
-
}
|
|
131
|
-
this.performance = window.performance;
|
|
132
|
-
this.PerformanceObserver = window.PerformanceObserver;
|
|
133
|
-
this.windowLocation = window.location;
|
|
134
|
-
this.navigator = window.navigator;
|
|
135
|
-
this.document = window.document;
|
|
136
|
-
if (this.navigator && this.navigator.cookieEnabled) {
|
|
137
|
-
// If user blocks cookies on the browser, accessing localStorage will
|
|
138
|
-
// throw an exception.
|
|
139
|
-
this.localStorage = window.localStorage;
|
|
140
|
-
}
|
|
141
|
-
if (window.perfMetrics && window.perfMetrics.onFirstInputDelay) {
|
|
142
|
-
this.onFirstInputDelay = window.perfMetrics.onFirstInputDelay;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
Api.prototype.getUrl = function () {
|
|
146
|
-
// Do not capture the string query part of url.
|
|
147
|
-
return this.windowLocation.href.split('?')[0];
|
|
148
|
-
};
|
|
149
|
-
Api.prototype.mark = function (name) {
|
|
150
|
-
if (!this.performance || !this.performance.mark) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
this.performance.mark(name);
|
|
154
|
-
};
|
|
155
|
-
Api.prototype.measure = function (measureName, mark1, mark2) {
|
|
156
|
-
if (!this.performance || !this.performance.measure) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
this.performance.measure(measureName, mark1, mark2);
|
|
160
|
-
};
|
|
161
|
-
Api.prototype.getEntriesByType = function (type) {
|
|
162
|
-
if (!this.performance || !this.performance.getEntriesByType) {
|
|
163
|
-
return [];
|
|
164
|
-
}
|
|
165
|
-
return this.performance.getEntriesByType(type);
|
|
166
|
-
};
|
|
167
|
-
Api.prototype.getEntriesByName = function (name) {
|
|
168
|
-
if (!this.performance || !this.performance.getEntriesByName) {
|
|
169
|
-
return [];
|
|
170
|
-
}
|
|
171
|
-
return this.performance.getEntriesByName(name);
|
|
172
|
-
};
|
|
173
|
-
Api.prototype.getTimeOrigin = function () {
|
|
174
|
-
// Polyfill the time origin with performance.timing.navigationStart.
|
|
175
|
-
return (this.performance &&
|
|
176
|
-
(this.performance.timeOrigin || this.performance.timing.navigationStart));
|
|
177
|
-
};
|
|
178
|
-
Api.prototype.requiredApisAvailable = function () {
|
|
179
|
-
if (!fetch || !Promise || !areCookiesEnabled()) {
|
|
180
|
-
consoleLogger.info('Firebase Performance cannot start if browser does not support fetch and Promise or cookie is disabled.');
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
if (!isIndexedDBAvailable()) {
|
|
184
|
-
consoleLogger.info('IndexedDB is not supported by current browser');
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
return true;
|
|
188
|
-
};
|
|
189
|
-
Api.prototype.setupObserver = function (entryType, callback) {
|
|
190
|
-
if (!this.PerformanceObserver) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
var observer = new this.PerformanceObserver(function (list) {
|
|
194
|
-
for (var _i = 0, _a = list.getEntries(); _i < _a.length; _i++) {
|
|
195
|
-
var entry = _a[_i];
|
|
196
|
-
// `entry` is a PerformanceEntry instance.
|
|
197
|
-
callback(entry);
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
// Start observing the entry types you care about.
|
|
201
|
-
observer.observe({ entryTypes: [entryType] });
|
|
202
|
-
};
|
|
203
|
-
Api.getInstance = function () {
|
|
204
|
-
if (apiInstance === undefined) {
|
|
205
|
-
apiInstance = new Api(windowInstance);
|
|
206
|
-
}
|
|
207
|
-
return apiInstance;
|
|
208
|
-
};
|
|
209
|
-
return Api;
|
|
210
|
-
}());
|
|
211
|
-
function setupApi(window) {
|
|
212
|
-
windowInstance = window;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* @license
|
|
217
|
-
* Copyright 2020 Google LLC
|
|
218
|
-
*
|
|
219
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
220
|
-
* you may not use this file except in compliance with the License.
|
|
221
|
-
* You may obtain a copy of the License at
|
|
222
|
-
*
|
|
223
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
224
|
-
*
|
|
225
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
226
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
227
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
228
|
-
* See the License for the specific language governing permissions and
|
|
229
|
-
* limitations under the License.
|
|
230
|
-
*/
|
|
231
|
-
var iid;
|
|
232
|
-
function getIidPromise(installationsService) {
|
|
233
|
-
var iidPromise = installationsService.getId();
|
|
234
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
235
|
-
iidPromise.then(function (iidVal) {
|
|
236
|
-
iid = iidVal;
|
|
237
|
-
});
|
|
238
|
-
return iidPromise;
|
|
239
|
-
}
|
|
240
|
-
// This method should be used after the iid is retrieved by getIidPromise method.
|
|
241
|
-
function getIid() {
|
|
242
|
-
return iid;
|
|
243
|
-
}
|
|
244
|
-
function getAuthTokenPromise(installationsService) {
|
|
245
|
-
var authTokenPromise = installationsService.getToken();
|
|
246
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
247
|
-
authTokenPromise.then(function (authTokenVal) {
|
|
248
|
-
});
|
|
249
|
-
return authTokenPromise;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* @license
|
|
254
|
-
* Copyright 2020 Google LLC
|
|
255
|
-
*
|
|
256
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
257
|
-
* you may not use this file except in compliance with the License.
|
|
258
|
-
* You may obtain a copy of the License at
|
|
259
|
-
*
|
|
260
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
261
|
-
*
|
|
262
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
263
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
264
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
265
|
-
* See the License for the specific language governing permissions and
|
|
266
|
-
* limitations under the License.
|
|
267
|
-
*/
|
|
268
|
-
function mergeStrings(part1, part2) {
|
|
269
|
-
var sizeDiff = part1.length - part2.length;
|
|
270
|
-
if (sizeDiff < 0 || sizeDiff > 1) {
|
|
271
|
-
throw ERROR_FACTORY.create("invalid String merger input" /* ErrorCode.INVALID_STRING_MERGER_PARAMETER */);
|
|
272
|
-
}
|
|
273
|
-
var resultArray = [];
|
|
274
|
-
for (var i = 0; i < part1.length; i++) {
|
|
275
|
-
resultArray.push(part1.charAt(i));
|
|
276
|
-
if (part2.length > i) {
|
|
277
|
-
resultArray.push(part2.charAt(i));
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
return resultArray.join('');
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* @license
|
|
285
|
-
* Copyright 2019 Google LLC
|
|
286
|
-
*
|
|
287
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
288
|
-
* you may not use this file except in compliance with the License.
|
|
289
|
-
* You may obtain a copy of the License at
|
|
290
|
-
*
|
|
291
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
292
|
-
*
|
|
293
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
294
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
295
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
296
|
-
* See the License for the specific language governing permissions and
|
|
297
|
-
* limitations under the License.
|
|
298
|
-
*/
|
|
299
|
-
var settingsServiceInstance;
|
|
300
|
-
var SettingsService = /** @class */ (function () {
|
|
301
|
-
function SettingsService() {
|
|
302
|
-
// The variable which controls logging of automatic traces and HTTP/S network monitoring.
|
|
303
|
-
this.instrumentationEnabled = true;
|
|
304
|
-
// The variable which controls logging of custom traces.
|
|
305
|
-
this.dataCollectionEnabled = true;
|
|
306
|
-
// Configuration flags set through remote config.
|
|
307
|
-
this.loggingEnabled = false;
|
|
308
|
-
// Sampling rate between 0 and 1.
|
|
309
|
-
this.tracesSamplingRate = 1;
|
|
310
|
-
this.networkRequestsSamplingRate = 1;
|
|
311
|
-
// Address of logging service.
|
|
312
|
-
this.logEndPointUrl = 'https://firebaselogging.googleapis.com/v0cc/log?format=json_proto';
|
|
313
|
-
// Performance event transport endpoint URL which should be compatible with proto3.
|
|
314
|
-
// New Address for transport service, not configurable via Remote Config.
|
|
315
|
-
this.flTransportEndpointUrl = mergeStrings('hts/frbslgigp.ogepscmv/ieo/eaylg', 'tp:/ieaeogn-agolai.o/1frlglgc/o');
|
|
316
|
-
this.transportKey = mergeStrings('AzSC8r6ReiGqFMyfvgow', 'Iayx0u-XT3vksVM-pIV');
|
|
317
|
-
// Source type for performance event logs.
|
|
318
|
-
this.logSource = 462;
|
|
319
|
-
// Flags which control per session logging of traces and network requests.
|
|
320
|
-
this.logTraceAfterSampling = false;
|
|
321
|
-
this.logNetworkAfterSampling = false;
|
|
322
|
-
// TTL of config retrieved from remote config in hours.
|
|
323
|
-
this.configTimeToLive = 12;
|
|
324
|
-
}
|
|
325
|
-
SettingsService.prototype.getFlTransportFullUrl = function () {
|
|
326
|
-
return this.flTransportEndpointUrl.concat('?key=', this.transportKey);
|
|
327
|
-
};
|
|
328
|
-
SettingsService.getInstance = function () {
|
|
329
|
-
if (settingsServiceInstance === undefined) {
|
|
330
|
-
settingsServiceInstance = new SettingsService();
|
|
331
|
-
}
|
|
332
|
-
return settingsServiceInstance;
|
|
333
|
-
};
|
|
334
|
-
return SettingsService;
|
|
335
|
-
}());
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* @license
|
|
339
|
-
* Copyright 2020 Google LLC
|
|
340
|
-
*
|
|
341
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
342
|
-
* you may not use this file except in compliance with the License.
|
|
343
|
-
* You may obtain a copy of the License at
|
|
344
|
-
*
|
|
345
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
346
|
-
*
|
|
347
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
348
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
349
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
350
|
-
* See the License for the specific language governing permissions and
|
|
351
|
-
* limitations under the License.
|
|
352
|
-
*/
|
|
353
|
-
var VisibilityState;
|
|
354
|
-
(function (VisibilityState) {
|
|
355
|
-
VisibilityState[VisibilityState["UNKNOWN"] = 0] = "UNKNOWN";
|
|
356
|
-
VisibilityState[VisibilityState["VISIBLE"] = 1] = "VISIBLE";
|
|
357
|
-
VisibilityState[VisibilityState["HIDDEN"] = 2] = "HIDDEN";
|
|
358
|
-
})(VisibilityState || (VisibilityState = {}));
|
|
359
|
-
var RESERVED_ATTRIBUTE_PREFIXES = ['firebase_', 'google_', 'ga_'];
|
|
360
|
-
var ATTRIBUTE_FORMAT_REGEX = new RegExp('^[a-zA-Z]\\w*$');
|
|
361
|
-
var MAX_ATTRIBUTE_NAME_LENGTH = 40;
|
|
362
|
-
var MAX_ATTRIBUTE_VALUE_LENGTH = 100;
|
|
363
|
-
function getServiceWorkerStatus() {
|
|
364
|
-
var navigator = Api.getInstance().navigator;
|
|
365
|
-
if (navigator === null || navigator === void 0 ? void 0 : navigator.serviceWorker) {
|
|
366
|
-
if (navigator.serviceWorker.controller) {
|
|
367
|
-
return 2 /* ServiceWorkerStatus.CONTROLLED */;
|
|
368
|
-
}
|
|
369
|
-
else {
|
|
370
|
-
return 3 /* ServiceWorkerStatus.UNCONTROLLED */;
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
return 1 /* ServiceWorkerStatus.UNSUPPORTED */;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
function getVisibilityState() {
|
|
378
|
-
var document = Api.getInstance().document;
|
|
379
|
-
var visibilityState = document.visibilityState;
|
|
380
|
-
switch (visibilityState) {
|
|
381
|
-
case 'visible':
|
|
382
|
-
return VisibilityState.VISIBLE;
|
|
383
|
-
case 'hidden':
|
|
384
|
-
return VisibilityState.HIDDEN;
|
|
385
|
-
default:
|
|
386
|
-
return VisibilityState.UNKNOWN;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
function getEffectiveConnectionType() {
|
|
390
|
-
var navigator = Api.getInstance().navigator;
|
|
391
|
-
var navigatorConnection = navigator.connection;
|
|
392
|
-
var effectiveType = navigatorConnection && navigatorConnection.effectiveType;
|
|
393
|
-
switch (effectiveType) {
|
|
394
|
-
case 'slow-2g':
|
|
395
|
-
return 1 /* EffectiveConnectionType.CONNECTION_SLOW_2G */;
|
|
396
|
-
case '2g':
|
|
397
|
-
return 2 /* EffectiveConnectionType.CONNECTION_2G */;
|
|
398
|
-
case '3g':
|
|
399
|
-
return 3 /* EffectiveConnectionType.CONNECTION_3G */;
|
|
400
|
-
case '4g':
|
|
401
|
-
return 4 /* EffectiveConnectionType.CONNECTION_4G */;
|
|
402
|
-
default:
|
|
403
|
-
return 0 /* EffectiveConnectionType.UNKNOWN */;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
function isValidCustomAttributeName(name) {
|
|
407
|
-
if (name.length === 0 || name.length > MAX_ATTRIBUTE_NAME_LENGTH) {
|
|
408
|
-
return false;
|
|
409
|
-
}
|
|
410
|
-
var matchesReservedPrefix = RESERVED_ATTRIBUTE_PREFIXES.some(function (prefix) {
|
|
411
|
-
return name.startsWith(prefix);
|
|
412
|
-
});
|
|
413
|
-
return !matchesReservedPrefix && !!name.match(ATTRIBUTE_FORMAT_REGEX);
|
|
414
|
-
}
|
|
415
|
-
function isValidCustomAttributeValue(value) {
|
|
416
|
-
return value.length !== 0 && value.length <= MAX_ATTRIBUTE_VALUE_LENGTH;
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* @license
|
|
421
|
-
* Copyright 2020 Google LLC
|
|
422
|
-
*
|
|
423
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
424
|
-
* you may not use this file except in compliance with the License.
|
|
425
|
-
* You may obtain a copy of the License at
|
|
426
|
-
*
|
|
427
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
428
|
-
*
|
|
429
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
430
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
431
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
432
|
-
* See the License for the specific language governing permissions and
|
|
433
|
-
* limitations under the License.
|
|
434
|
-
*/
|
|
435
|
-
function getAppId(firebaseApp) {
|
|
436
|
-
var _a;
|
|
437
|
-
var appId = (_a = firebaseApp.options) === null || _a === void 0 ? void 0 : _a.appId;
|
|
438
|
-
if (!appId) {
|
|
439
|
-
throw ERROR_FACTORY.create("no app id" /* ErrorCode.NO_APP_ID */);
|
|
440
|
-
}
|
|
441
|
-
return appId;
|
|
442
|
-
}
|
|
443
|
-
function getProjectId(firebaseApp) {
|
|
444
|
-
var _a;
|
|
445
|
-
var projectId = (_a = firebaseApp.options) === null || _a === void 0 ? void 0 : _a.projectId;
|
|
446
|
-
if (!projectId) {
|
|
447
|
-
throw ERROR_FACTORY.create("no project id" /* ErrorCode.NO_PROJECT_ID */);
|
|
448
|
-
}
|
|
449
|
-
return projectId;
|
|
450
|
-
}
|
|
451
|
-
function getApiKey(firebaseApp) {
|
|
452
|
-
var _a;
|
|
453
|
-
var apiKey = (_a = firebaseApp.options) === null || _a === void 0 ? void 0 : _a.apiKey;
|
|
454
|
-
if (!apiKey) {
|
|
455
|
-
throw ERROR_FACTORY.create("no api key" /* ErrorCode.NO_API_KEY */);
|
|
456
|
-
}
|
|
457
|
-
return apiKey;
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* @license
|
|
462
|
-
* Copyright 2020 Google LLC
|
|
463
|
-
*
|
|
464
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
465
|
-
* you may not use this file except in compliance with the License.
|
|
466
|
-
* You may obtain a copy of the License at
|
|
467
|
-
*
|
|
468
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
469
|
-
*
|
|
470
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
471
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
472
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
473
|
-
* See the License for the specific language governing permissions and
|
|
474
|
-
* limitations under the License.
|
|
475
|
-
*/
|
|
476
|
-
var REMOTE_CONFIG_SDK_VERSION = '0.0.1';
|
|
477
|
-
// These values will be used if the remote config object is successfully
|
|
478
|
-
// retrieved, but the template does not have these fields.
|
|
479
|
-
var DEFAULT_CONFIGS = {
|
|
480
|
-
loggingEnabled: true
|
|
481
|
-
};
|
|
482
|
-
var FIS_AUTH_PREFIX = 'FIREBASE_INSTALLATIONS_AUTH';
|
|
483
|
-
function getConfig(performanceController, iid) {
|
|
484
|
-
var config = getStoredConfig();
|
|
485
|
-
if (config) {
|
|
486
|
-
processConfig(config);
|
|
487
|
-
return Promise.resolve();
|
|
488
|
-
}
|
|
489
|
-
return getRemoteConfig(performanceController, iid)
|
|
490
|
-
.then(processConfig)
|
|
491
|
-
.then(function (config) { return storeConfig(config); },
|
|
492
|
-
/** Do nothing for error, use defaults set in settings service. */
|
|
493
|
-
function () { });
|
|
494
|
-
}
|
|
495
|
-
function getStoredConfig() {
|
|
496
|
-
var localStorage = Api.getInstance().localStorage;
|
|
497
|
-
if (!localStorage) {
|
|
498
|
-
return;
|
|
499
|
-
}
|
|
500
|
-
var expiryString = localStorage.getItem(CONFIG_EXPIRY_LOCAL_STORAGE_KEY);
|
|
501
|
-
if (!expiryString || !configValid(expiryString)) {
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
var configStringified = localStorage.getItem(CONFIG_LOCAL_STORAGE_KEY);
|
|
505
|
-
if (!configStringified) {
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
try {
|
|
509
|
-
var configResponse = JSON.parse(configStringified);
|
|
510
|
-
return configResponse;
|
|
511
|
-
}
|
|
512
|
-
catch (_a) {
|
|
513
|
-
return;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
function storeConfig(config) {
|
|
517
|
-
var localStorage = Api.getInstance().localStorage;
|
|
518
|
-
if (!config || !localStorage) {
|
|
519
|
-
return;
|
|
520
|
-
}
|
|
521
|
-
localStorage.setItem(CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config));
|
|
522
|
-
localStorage.setItem(CONFIG_EXPIRY_LOCAL_STORAGE_KEY, String(Date.now() +
|
|
523
|
-
SettingsService.getInstance().configTimeToLive * 60 * 60 * 1000));
|
|
524
|
-
}
|
|
525
|
-
var COULD_NOT_GET_CONFIG_MSG = 'Could not fetch config, will use default configs';
|
|
526
|
-
function getRemoteConfig(performanceController, iid) {
|
|
527
|
-
// Perf needs auth token only to retrieve remote config.
|
|
528
|
-
return getAuthTokenPromise(performanceController.installations)
|
|
529
|
-
.then(function (authToken) {
|
|
530
|
-
var projectId = getProjectId(performanceController.app);
|
|
531
|
-
var apiKey = getApiKey(performanceController.app);
|
|
532
|
-
var configEndPoint = "https://firebaseremoteconfig.googleapis.com/v1/projects/".concat(projectId, "/namespaces/fireperf:fetch?key=").concat(apiKey);
|
|
533
|
-
var request = new Request(configEndPoint, {
|
|
534
|
-
method: 'POST',
|
|
535
|
-
headers: { Authorization: "".concat(FIS_AUTH_PREFIX, " ").concat(authToken) },
|
|
536
|
-
/* eslint-disable camelcase */
|
|
537
|
-
body: JSON.stringify({
|
|
538
|
-
app_instance_id: iid,
|
|
539
|
-
app_instance_id_token: authToken,
|
|
540
|
-
app_id: getAppId(performanceController.app),
|
|
541
|
-
app_version: SDK_VERSION,
|
|
542
|
-
sdk_version: REMOTE_CONFIG_SDK_VERSION
|
|
543
|
-
})
|
|
544
|
-
/* eslint-enable camelcase */
|
|
545
|
-
});
|
|
546
|
-
return fetch(request).then(function (response) {
|
|
547
|
-
if (response.ok) {
|
|
548
|
-
return response.json();
|
|
549
|
-
}
|
|
550
|
-
// In case response is not ok. This will be caught by catch.
|
|
551
|
-
throw ERROR_FACTORY.create("RC response not ok" /* ErrorCode.RC_NOT_OK */);
|
|
552
|
-
});
|
|
553
|
-
})
|
|
554
|
-
.catch(function () {
|
|
555
|
-
consoleLogger.info(COULD_NOT_GET_CONFIG_MSG);
|
|
556
|
-
return undefined;
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
/**
|
|
560
|
-
* Processes config coming either from calling RC or from local storage.
|
|
561
|
-
* This method only runs if call is successful or config in storage
|
|
562
|
-
* is valid.
|
|
563
|
-
*/
|
|
564
|
-
function processConfig(config) {
|
|
565
|
-
if (!config) {
|
|
566
|
-
return config;
|
|
567
|
-
}
|
|
568
|
-
var settingsServiceInstance = SettingsService.getInstance();
|
|
569
|
-
var entries = config.entries || {};
|
|
570
|
-
if (entries.fpr_enabled !== undefined) {
|
|
571
|
-
// TODO: Change the assignment of loggingEnabled once the received type is
|
|
572
|
-
// known.
|
|
573
|
-
settingsServiceInstance.loggingEnabled =
|
|
574
|
-
String(entries.fpr_enabled) === 'true';
|
|
575
|
-
}
|
|
576
|
-
else {
|
|
577
|
-
// Config retrieved successfully, but there is no fpr_enabled in template.
|
|
578
|
-
// Use secondary configs value.
|
|
579
|
-
settingsServiceInstance.loggingEnabled = DEFAULT_CONFIGS.loggingEnabled;
|
|
580
|
-
}
|
|
581
|
-
if (entries.fpr_log_source) {
|
|
582
|
-
settingsServiceInstance.logSource = Number(entries.fpr_log_source);
|
|
583
|
-
}
|
|
584
|
-
else if (DEFAULT_CONFIGS.logSource) {
|
|
585
|
-
settingsServiceInstance.logSource = DEFAULT_CONFIGS.logSource;
|
|
586
|
-
}
|
|
587
|
-
if (entries.fpr_log_endpoint_url) {
|
|
588
|
-
settingsServiceInstance.logEndPointUrl = entries.fpr_log_endpoint_url;
|
|
589
|
-
}
|
|
590
|
-
else if (DEFAULT_CONFIGS.logEndPointUrl) {
|
|
591
|
-
settingsServiceInstance.logEndPointUrl = DEFAULT_CONFIGS.logEndPointUrl;
|
|
592
|
-
}
|
|
593
|
-
// Key from Remote Config has to be non-empty string, otherwise use local value.
|
|
594
|
-
if (entries.fpr_log_transport_key) {
|
|
595
|
-
settingsServiceInstance.transportKey = entries.fpr_log_transport_key;
|
|
596
|
-
}
|
|
597
|
-
else if (DEFAULT_CONFIGS.transportKey) {
|
|
598
|
-
settingsServiceInstance.transportKey = DEFAULT_CONFIGS.transportKey;
|
|
599
|
-
}
|
|
600
|
-
if (entries.fpr_vc_network_request_sampling_rate !== undefined) {
|
|
601
|
-
settingsServiceInstance.networkRequestsSamplingRate = Number(entries.fpr_vc_network_request_sampling_rate);
|
|
602
|
-
}
|
|
603
|
-
else if (DEFAULT_CONFIGS.networkRequestsSamplingRate !== undefined) {
|
|
604
|
-
settingsServiceInstance.networkRequestsSamplingRate =
|
|
605
|
-
DEFAULT_CONFIGS.networkRequestsSamplingRate;
|
|
606
|
-
}
|
|
607
|
-
if (entries.fpr_vc_trace_sampling_rate !== undefined) {
|
|
608
|
-
settingsServiceInstance.tracesSamplingRate = Number(entries.fpr_vc_trace_sampling_rate);
|
|
609
|
-
}
|
|
610
|
-
else if (DEFAULT_CONFIGS.tracesSamplingRate !== undefined) {
|
|
611
|
-
settingsServiceInstance.tracesSamplingRate =
|
|
612
|
-
DEFAULT_CONFIGS.tracesSamplingRate;
|
|
613
|
-
}
|
|
614
|
-
// Set the per session trace and network logging flags.
|
|
615
|
-
settingsServiceInstance.logTraceAfterSampling = shouldLogAfterSampling(settingsServiceInstance.tracesSamplingRate);
|
|
616
|
-
settingsServiceInstance.logNetworkAfterSampling = shouldLogAfterSampling(settingsServiceInstance.networkRequestsSamplingRate);
|
|
617
|
-
return config;
|
|
618
|
-
}
|
|
619
|
-
function configValid(expiry) {
|
|
620
|
-
return Number(expiry) > Date.now();
|
|
621
|
-
}
|
|
622
|
-
function shouldLogAfterSampling(samplingRate) {
|
|
623
|
-
return Math.random() <= samplingRate;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
/**
|
|
627
|
-
* @license
|
|
628
|
-
* Copyright 2020 Google LLC
|
|
629
|
-
*
|
|
630
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
631
|
-
* you may not use this file except in compliance with the License.
|
|
632
|
-
* You may obtain a copy of the License at
|
|
633
|
-
*
|
|
634
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
635
|
-
*
|
|
636
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
637
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
638
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
639
|
-
* See the License for the specific language governing permissions and
|
|
640
|
-
* limitations under the License.
|
|
641
|
-
*/
|
|
642
|
-
var initializationStatus = 1 /* InitializationStatus.notInitialized */;
|
|
643
|
-
var initializationPromise;
|
|
644
|
-
function getInitializationPromise(performanceController) {
|
|
645
|
-
initializationStatus = 2 /* InitializationStatus.initializationPending */;
|
|
646
|
-
initializationPromise =
|
|
647
|
-
initializationPromise || initializePerf(performanceController);
|
|
648
|
-
return initializationPromise;
|
|
649
|
-
}
|
|
650
|
-
function isPerfInitialized() {
|
|
651
|
-
return initializationStatus === 3 /* InitializationStatus.initialized */;
|
|
652
|
-
}
|
|
653
|
-
function initializePerf(performanceController) {
|
|
654
|
-
return getDocumentReadyComplete()
|
|
655
|
-
.then(function () { return getIidPromise(performanceController.installations); })
|
|
656
|
-
.then(function (iid) { return getConfig(performanceController, iid); })
|
|
657
|
-
.then(function () { return changeInitializationStatus(); }, function () { return changeInitializationStatus(); });
|
|
658
|
-
}
|
|
659
|
-
/**
|
|
660
|
-
* Returns a promise which resolves whenever the document readystate is complete or
|
|
661
|
-
* immediately if it is called after page load complete.
|
|
662
|
-
*/
|
|
663
|
-
function getDocumentReadyComplete() {
|
|
664
|
-
var document = Api.getInstance().document;
|
|
665
|
-
return new Promise(function (resolve) {
|
|
666
|
-
if (document && document.readyState !== 'complete') {
|
|
667
|
-
var handler_1 = function () {
|
|
668
|
-
if (document.readyState === 'complete') {
|
|
669
|
-
document.removeEventListener('readystatechange', handler_1);
|
|
670
|
-
resolve();
|
|
671
|
-
}
|
|
672
|
-
};
|
|
673
|
-
document.addEventListener('readystatechange', handler_1);
|
|
674
|
-
}
|
|
675
|
-
else {
|
|
676
|
-
resolve();
|
|
677
|
-
}
|
|
678
|
-
});
|
|
679
|
-
}
|
|
680
|
-
function changeInitializationStatus() {
|
|
681
|
-
initializationStatus = 3 /* InitializationStatus.initialized */;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
/**
|
|
685
|
-
* @license
|
|
686
|
-
* Copyright 2020 Google LLC
|
|
687
|
-
*
|
|
688
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
689
|
-
* you may not use this file except in compliance with the License.
|
|
690
|
-
* You may obtain a copy of the License at
|
|
691
|
-
*
|
|
692
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
693
|
-
*
|
|
694
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
695
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
696
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
697
|
-
* See the License for the specific language governing permissions and
|
|
698
|
-
* limitations under the License.
|
|
699
|
-
*/
|
|
700
|
-
var DEFAULT_SEND_INTERVAL_MS = 10 * 1000;
|
|
701
|
-
var INITIAL_SEND_TIME_DELAY_MS = 5.5 * 1000;
|
|
702
|
-
// If end point does not work, the call will be tried for these many times.
|
|
703
|
-
var DEFAULT_REMAINING_TRIES = 3;
|
|
704
|
-
var MAX_EVENT_COUNT_PER_REQUEST = 1000;
|
|
705
|
-
var remainingTries = DEFAULT_REMAINING_TRIES;
|
|
706
|
-
/* eslint-enable camelcase */
|
|
707
|
-
var queue = [];
|
|
708
|
-
var isTransportSetup = false;
|
|
709
|
-
function setupTransportService() {
|
|
710
|
-
if (!isTransportSetup) {
|
|
711
|
-
processQueue(INITIAL_SEND_TIME_DELAY_MS);
|
|
712
|
-
isTransportSetup = true;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
function processQueue(timeOffset) {
|
|
716
|
-
setTimeout(function () {
|
|
717
|
-
// If there is no remainingTries left, stop retrying.
|
|
718
|
-
if (remainingTries === 0) {
|
|
719
|
-
return;
|
|
720
|
-
}
|
|
721
|
-
// If there are no events to process, wait for DEFAULT_SEND_INTERVAL_MS and try again.
|
|
722
|
-
if (!queue.length) {
|
|
723
|
-
return processQueue(DEFAULT_SEND_INTERVAL_MS);
|
|
724
|
-
}
|
|
725
|
-
dispatchQueueEvents();
|
|
726
|
-
}, timeOffset);
|
|
727
|
-
}
|
|
728
|
-
function dispatchQueueEvents() {
|
|
729
|
-
// Extract events up to the maximum cap of single logRequest from top of "official queue".
|
|
730
|
-
// The staged events will be used for current logRequest attempt, remaining events will be kept
|
|
731
|
-
// for next attempt.
|
|
732
|
-
var staged = queue.splice(0, MAX_EVENT_COUNT_PER_REQUEST);
|
|
733
|
-
/* eslint-disable camelcase */
|
|
734
|
-
// We will pass the JSON serialized event to the backend.
|
|
735
|
-
var log_event = staged.map(function (evt) { return ({
|
|
736
|
-
source_extension_json_proto3: evt.message,
|
|
737
|
-
event_time_ms: String(evt.eventTime)
|
|
738
|
-
}); });
|
|
739
|
-
var data = {
|
|
740
|
-
request_time_ms: String(Date.now()),
|
|
741
|
-
client_info: {
|
|
742
|
-
client_type: 1,
|
|
743
|
-
js_client_info: {}
|
|
744
|
-
},
|
|
745
|
-
log_source: SettingsService.getInstance().logSource,
|
|
746
|
-
log_event: log_event
|
|
747
|
-
};
|
|
748
|
-
/* eslint-enable camelcase */
|
|
749
|
-
sendEventsToFl(data, staged).catch(function () {
|
|
750
|
-
// If the request fails for some reason, add the events that were attempted
|
|
751
|
-
// back to the primary queue to retry later.
|
|
752
|
-
queue = __spreadArray(__spreadArray([], staged, true), queue, true);
|
|
753
|
-
remainingTries--;
|
|
754
|
-
consoleLogger.info("Tries left: ".concat(remainingTries, "."));
|
|
755
|
-
processQueue(DEFAULT_SEND_INTERVAL_MS);
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
function sendEventsToFl(data, staged) {
|
|
759
|
-
return postToFlEndpoint(data)
|
|
760
|
-
.then(function (res) {
|
|
761
|
-
if (!res.ok) {
|
|
762
|
-
consoleLogger.info('Call to Firebase backend failed.');
|
|
763
|
-
}
|
|
764
|
-
return res.json();
|
|
765
|
-
})
|
|
766
|
-
.then(function (res) {
|
|
767
|
-
// Find the next call wait time from the response.
|
|
768
|
-
var transportWait = Number(res.nextRequestWaitMillis);
|
|
769
|
-
var requestOffset = DEFAULT_SEND_INTERVAL_MS;
|
|
770
|
-
if (!isNaN(transportWait)) {
|
|
771
|
-
requestOffset = Math.max(transportWait, requestOffset);
|
|
772
|
-
}
|
|
773
|
-
// Delete request if response include RESPONSE_ACTION_UNKNOWN or DELETE_REQUEST action.
|
|
774
|
-
// Otherwise, retry request using normal scheduling if response include RETRY_REQUEST_LATER.
|
|
775
|
-
var logResponseDetails = res.logResponseDetails;
|
|
776
|
-
if (Array.isArray(logResponseDetails) &&
|
|
777
|
-
logResponseDetails.length > 0 &&
|
|
778
|
-
logResponseDetails[0].responseAction === 'RETRY_REQUEST_LATER') {
|
|
779
|
-
queue = __spreadArray(__spreadArray([], staged, true), queue, true);
|
|
780
|
-
consoleLogger.info("Retry transport request later.");
|
|
781
|
-
}
|
|
782
|
-
remainingTries = DEFAULT_REMAINING_TRIES;
|
|
783
|
-
// Schedule the next process.
|
|
784
|
-
processQueue(requestOffset);
|
|
785
|
-
});
|
|
786
|
-
}
|
|
787
|
-
function postToFlEndpoint(data) {
|
|
788
|
-
var flTransportFullUrl = SettingsService.getInstance().getFlTransportFullUrl();
|
|
789
|
-
return fetch(flTransportFullUrl, {
|
|
790
|
-
method: 'POST',
|
|
791
|
-
body: JSON.stringify(data)
|
|
792
|
-
});
|
|
793
|
-
}
|
|
794
|
-
function addToQueue(evt) {
|
|
795
|
-
if (!evt.eventTime || !evt.message) {
|
|
796
|
-
throw ERROR_FACTORY.create("invalid cc log" /* ErrorCode.INVALID_CC_LOG */);
|
|
797
|
-
}
|
|
798
|
-
// Add the new event to the queue.
|
|
799
|
-
queue = __spreadArray(__spreadArray([], queue, true), [evt], false);
|
|
800
|
-
}
|
|
801
|
-
/** Log handler for cc service to send the performance logs to the server. */
|
|
802
|
-
function transportHandler(
|
|
803
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
804
|
-
serializer) {
|
|
805
|
-
return function () {
|
|
806
|
-
var args = [];
|
|
807
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
808
|
-
args[_i] = arguments[_i];
|
|
809
|
-
}
|
|
810
|
-
var message = serializer.apply(void 0, args);
|
|
811
|
-
addToQueue({
|
|
812
|
-
message: message,
|
|
813
|
-
eventTime: Date.now()
|
|
814
|
-
});
|
|
815
|
-
};
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
/**
|
|
819
|
-
* @license
|
|
820
|
-
* Copyright 2020 Google LLC
|
|
821
|
-
*
|
|
822
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
823
|
-
* you may not use this file except in compliance with the License.
|
|
824
|
-
* You may obtain a copy of the License at
|
|
825
|
-
*
|
|
826
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
827
|
-
*
|
|
828
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
829
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
830
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
831
|
-
* See the License for the specific language governing permissions and
|
|
832
|
-
* limitations under the License.
|
|
833
|
-
*/
|
|
834
|
-
var logger;
|
|
835
|
-
// This method is not called before initialization.
|
|
836
|
-
function sendLog(resource, resourceType) {
|
|
837
|
-
if (!logger) {
|
|
838
|
-
logger = transportHandler(serializer);
|
|
839
|
-
}
|
|
840
|
-
logger(resource, resourceType);
|
|
841
|
-
}
|
|
842
|
-
function logTrace(trace) {
|
|
843
|
-
var settingsService = SettingsService.getInstance();
|
|
844
|
-
// Do not log if trace is auto generated and instrumentation is disabled.
|
|
845
|
-
if (!settingsService.instrumentationEnabled && trace.isAuto) {
|
|
846
|
-
return;
|
|
847
|
-
}
|
|
848
|
-
// Do not log if trace is custom and data collection is disabled.
|
|
849
|
-
if (!settingsService.dataCollectionEnabled && !trace.isAuto) {
|
|
850
|
-
return;
|
|
851
|
-
}
|
|
852
|
-
// Do not log if required apis are not available.
|
|
853
|
-
if (!Api.getInstance().requiredApisAvailable()) {
|
|
854
|
-
return;
|
|
855
|
-
}
|
|
856
|
-
// Only log the page load auto traces if page is visible.
|
|
857
|
-
if (trace.isAuto && getVisibilityState() !== VisibilityState.VISIBLE) {
|
|
858
|
-
return;
|
|
859
|
-
}
|
|
860
|
-
if (isPerfInitialized()) {
|
|
861
|
-
sendTraceLog(trace);
|
|
862
|
-
}
|
|
863
|
-
else {
|
|
864
|
-
// Custom traces can be used before the initialization but logging
|
|
865
|
-
// should wait until after.
|
|
866
|
-
getInitializationPromise(trace.performanceController).then(function () { return sendTraceLog(trace); }, function () { return sendTraceLog(trace); });
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
function sendTraceLog(trace) {
|
|
870
|
-
if (!getIid()) {
|
|
871
|
-
return;
|
|
872
|
-
}
|
|
873
|
-
var settingsService = SettingsService.getInstance();
|
|
874
|
-
if (!settingsService.loggingEnabled ||
|
|
875
|
-
!settingsService.logTraceAfterSampling) {
|
|
876
|
-
return;
|
|
877
|
-
}
|
|
878
|
-
setTimeout(function () { return sendLog(trace, 1 /* ResourceType.Trace */); }, 0);
|
|
879
|
-
}
|
|
880
|
-
function logNetworkRequest(networkRequest) {
|
|
881
|
-
var settingsService = SettingsService.getInstance();
|
|
882
|
-
// Do not log network requests if instrumentation is disabled.
|
|
883
|
-
if (!settingsService.instrumentationEnabled) {
|
|
884
|
-
return;
|
|
885
|
-
}
|
|
886
|
-
// Do not log the js sdk's call to transport service domain to avoid unnecessary cycle.
|
|
887
|
-
// Need to blacklist both old and new endpoints to avoid migration gap.
|
|
888
|
-
var networkRequestUrl = networkRequest.url;
|
|
889
|
-
// Blacklist old log endpoint and new transport endpoint.
|
|
890
|
-
// Because Performance SDK doesn't instrument requests sent from SDK itself.
|
|
891
|
-
var logEndpointUrl = settingsService.logEndPointUrl.split('?')[0];
|
|
892
|
-
var flEndpointUrl = settingsService.flTransportEndpointUrl.split('?')[0];
|
|
893
|
-
if (networkRequestUrl === logEndpointUrl ||
|
|
894
|
-
networkRequestUrl === flEndpointUrl) {
|
|
895
|
-
return;
|
|
896
|
-
}
|
|
897
|
-
if (!settingsService.loggingEnabled ||
|
|
898
|
-
!settingsService.logNetworkAfterSampling) {
|
|
899
|
-
return;
|
|
900
|
-
}
|
|
901
|
-
setTimeout(function () { return sendLog(networkRequest, 0 /* ResourceType.NetworkRequest */); }, 0);
|
|
902
|
-
}
|
|
903
|
-
function serializer(resource, resourceType) {
|
|
904
|
-
if (resourceType === 0 /* ResourceType.NetworkRequest */) {
|
|
905
|
-
return serializeNetworkRequest(resource);
|
|
906
|
-
}
|
|
907
|
-
return serializeTrace(resource);
|
|
908
|
-
}
|
|
909
|
-
function serializeNetworkRequest(networkRequest) {
|
|
910
|
-
var networkRequestMetric = {
|
|
911
|
-
url: networkRequest.url,
|
|
912
|
-
http_method: networkRequest.httpMethod || 0,
|
|
913
|
-
http_response_code: 200,
|
|
914
|
-
response_payload_bytes: networkRequest.responsePayloadBytes,
|
|
915
|
-
client_start_time_us: networkRequest.startTimeUs,
|
|
916
|
-
time_to_response_initiated_us: networkRequest.timeToResponseInitiatedUs,
|
|
917
|
-
time_to_response_completed_us: networkRequest.timeToResponseCompletedUs
|
|
918
|
-
};
|
|
919
|
-
var perfMetric = {
|
|
920
|
-
application_info: getApplicationInfo(networkRequest.performanceController.app),
|
|
921
|
-
network_request_metric: networkRequestMetric
|
|
922
|
-
};
|
|
923
|
-
return JSON.stringify(perfMetric);
|
|
924
|
-
}
|
|
925
|
-
function serializeTrace(trace) {
|
|
926
|
-
var traceMetric = {
|
|
927
|
-
name: trace.name,
|
|
928
|
-
is_auto: trace.isAuto,
|
|
929
|
-
client_start_time_us: trace.startTimeUs,
|
|
930
|
-
duration_us: trace.durationUs
|
|
931
|
-
};
|
|
932
|
-
if (Object.keys(trace.counters).length !== 0) {
|
|
933
|
-
traceMetric.counters = trace.counters;
|
|
934
|
-
}
|
|
935
|
-
var customAttributes = trace.getAttributes();
|
|
936
|
-
if (Object.keys(customAttributes).length !== 0) {
|
|
937
|
-
traceMetric.custom_attributes = customAttributes;
|
|
938
|
-
}
|
|
939
|
-
var perfMetric = {
|
|
940
|
-
application_info: getApplicationInfo(trace.performanceController.app),
|
|
941
|
-
trace_metric: traceMetric
|
|
942
|
-
};
|
|
943
|
-
return JSON.stringify(perfMetric);
|
|
944
|
-
}
|
|
945
|
-
function getApplicationInfo(firebaseApp) {
|
|
946
|
-
return {
|
|
947
|
-
google_app_id: getAppId(firebaseApp),
|
|
948
|
-
app_instance_id: getIid(),
|
|
949
|
-
web_app_info: {
|
|
950
|
-
sdk_version: SDK_VERSION,
|
|
951
|
-
page_url: Api.getInstance().getUrl(),
|
|
952
|
-
service_worker_status: getServiceWorkerStatus(),
|
|
953
|
-
visibility_state: getVisibilityState(),
|
|
954
|
-
effective_connection_type: getEffectiveConnectionType()
|
|
955
|
-
},
|
|
956
|
-
application_process_state: 0
|
|
957
|
-
};
|
|
958
|
-
}
|
|
959
|
-
/* eslint-enable camelcase */
|
|
960
|
-
|
|
961
|
-
/**
|
|
962
|
-
* @license
|
|
963
|
-
* Copyright 2020 Google LLC
|
|
964
|
-
*
|
|
965
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
966
|
-
* you may not use this file except in compliance with the License.
|
|
967
|
-
* You may obtain a copy of the License at
|
|
968
|
-
*
|
|
969
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
970
|
-
*
|
|
971
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
972
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
973
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
974
|
-
* See the License for the specific language governing permissions and
|
|
975
|
-
* limitations under the License.
|
|
976
|
-
*/
|
|
977
|
-
var MAX_METRIC_NAME_LENGTH = 100;
|
|
978
|
-
var RESERVED_AUTO_PREFIX = '_';
|
|
979
|
-
var oobMetrics = [
|
|
980
|
-
FIRST_PAINT_COUNTER_NAME,
|
|
981
|
-
FIRST_CONTENTFUL_PAINT_COUNTER_NAME,
|
|
982
|
-
FIRST_INPUT_DELAY_COUNTER_NAME
|
|
983
|
-
];
|
|
984
|
-
/**
|
|
985
|
-
* Returns true if the metric is custom and does not start with reserved prefix, or if
|
|
986
|
-
* the metric is one of out of the box page load trace metrics.
|
|
987
|
-
*/
|
|
988
|
-
function isValidMetricName(name, traceName) {
|
|
989
|
-
if (name.length === 0 || name.length > MAX_METRIC_NAME_LENGTH) {
|
|
990
|
-
return false;
|
|
991
|
-
}
|
|
992
|
-
return ((traceName &&
|
|
993
|
-
traceName.startsWith(OOB_TRACE_PAGE_LOAD_PREFIX) &&
|
|
994
|
-
oobMetrics.indexOf(name) > -1) ||
|
|
995
|
-
!name.startsWith(RESERVED_AUTO_PREFIX));
|
|
996
|
-
}
|
|
997
|
-
/**
|
|
998
|
-
* Converts the provided value to an integer value to be used in case of a metric.
|
|
999
|
-
* @param providedValue Provided number value of the metric that needs to be converted to an integer.
|
|
1000
|
-
*
|
|
1001
|
-
* @returns Converted integer number to be set for the metric.
|
|
1002
|
-
*/
|
|
1003
|
-
function convertMetricValueToInteger(providedValue) {
|
|
1004
|
-
var valueAsInteger = Math.floor(providedValue);
|
|
1005
|
-
if (valueAsInteger < providedValue) {
|
|
1006
|
-
consoleLogger.info("Metric value should be an Integer, setting the value as : ".concat(valueAsInteger, "."));
|
|
1007
|
-
}
|
|
1008
|
-
return valueAsInteger;
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
/**
|
|
1012
|
-
* @license
|
|
1013
|
-
* Copyright 2020 Google LLC
|
|
1014
|
-
*
|
|
1015
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1016
|
-
* you may not use this file except in compliance with the License.
|
|
1017
|
-
* You may obtain a copy of the License at
|
|
1018
|
-
*
|
|
1019
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1020
|
-
*
|
|
1021
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1022
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1023
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1024
|
-
* See the License for the specific language governing permissions and
|
|
1025
|
-
* limitations under the License.
|
|
1026
|
-
*/
|
|
1027
|
-
var Trace = /** @class */ (function () {
|
|
1028
|
-
/**
|
|
1029
|
-
* @param performanceController The performance controller running.
|
|
1030
|
-
* @param name The name of the trace.
|
|
1031
|
-
* @param isAuto If the trace is auto-instrumented.
|
|
1032
|
-
* @param traceMeasureName The name of the measure marker in user timing specification. This field
|
|
1033
|
-
* is only set when the trace is built for logging when the user directly uses the user timing
|
|
1034
|
-
* api (performance.mark and performance.measure).
|
|
1035
|
-
*/
|
|
1036
|
-
function Trace(performanceController, name, isAuto, traceMeasureName) {
|
|
1037
|
-
if (isAuto === void 0) { isAuto = false; }
|
|
1038
|
-
this.performanceController = performanceController;
|
|
1039
|
-
this.name = name;
|
|
1040
|
-
this.isAuto = isAuto;
|
|
1041
|
-
this.state = 1 /* TraceState.UNINITIALIZED */;
|
|
1042
|
-
this.customAttributes = {};
|
|
1043
|
-
this.counters = {};
|
|
1044
|
-
this.api = Api.getInstance();
|
|
1045
|
-
this.randomId = Math.floor(Math.random() * 1000000);
|
|
1046
|
-
if (!this.isAuto) {
|
|
1047
|
-
this.traceStartMark = "".concat(TRACE_START_MARK_PREFIX, "-").concat(this.randomId, "-").concat(this.name);
|
|
1048
|
-
this.traceStopMark = "".concat(TRACE_STOP_MARK_PREFIX, "-").concat(this.randomId, "-").concat(this.name);
|
|
1049
|
-
this.traceMeasure =
|
|
1050
|
-
traceMeasureName ||
|
|
1051
|
-
"".concat(TRACE_MEASURE_PREFIX, "-").concat(this.randomId, "-").concat(this.name);
|
|
1052
|
-
if (traceMeasureName) {
|
|
1053
|
-
// For the case of direct user timing traces, no start stop will happen. The measure object
|
|
1054
|
-
// is already available.
|
|
1055
|
-
this.calculateTraceMetrics();
|
|
1056
|
-
}
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
/**
|
|
1060
|
-
* Starts a trace. The measurement of the duration starts at this point.
|
|
1061
|
-
*/
|
|
1062
|
-
Trace.prototype.start = function () {
|
|
1063
|
-
if (this.state !== 1 /* TraceState.UNINITIALIZED */) {
|
|
1064
|
-
throw ERROR_FACTORY.create("trace started" /* ErrorCode.TRACE_STARTED_BEFORE */, {
|
|
1065
|
-
traceName: this.name
|
|
1066
|
-
});
|
|
1067
|
-
}
|
|
1068
|
-
this.api.mark(this.traceStartMark);
|
|
1069
|
-
this.state = 2 /* TraceState.RUNNING */;
|
|
1070
|
-
};
|
|
1071
|
-
/**
|
|
1072
|
-
* Stops the trace. The measurement of the duration of the trace stops at this point and trace
|
|
1073
|
-
* is logged.
|
|
1074
|
-
*/
|
|
1075
|
-
Trace.prototype.stop = function () {
|
|
1076
|
-
if (this.state !== 2 /* TraceState.RUNNING */) {
|
|
1077
|
-
throw ERROR_FACTORY.create("trace stopped" /* ErrorCode.TRACE_STOPPED_BEFORE */, {
|
|
1078
|
-
traceName: this.name
|
|
1079
|
-
});
|
|
1080
|
-
}
|
|
1081
|
-
this.state = 3 /* TraceState.TERMINATED */;
|
|
1082
|
-
this.api.mark(this.traceStopMark);
|
|
1083
|
-
this.api.measure(this.traceMeasure, this.traceStartMark, this.traceStopMark);
|
|
1084
|
-
this.calculateTraceMetrics();
|
|
1085
|
-
logTrace(this);
|
|
1086
|
-
};
|
|
1087
|
-
/**
|
|
1088
|
-
* Records a trace with predetermined values. If this method is used a trace is created and logged
|
|
1089
|
-
* directly. No need to use start and stop methods.
|
|
1090
|
-
* @param startTime Trace start time since epoch in millisec
|
|
1091
|
-
* @param duration The duration of the trace in millisec
|
|
1092
|
-
* @param options An object which can optionally hold maps of custom metrics and custom attributes
|
|
1093
|
-
*/
|
|
1094
|
-
Trace.prototype.record = function (startTime, duration, options) {
|
|
1095
|
-
if (startTime <= 0) {
|
|
1096
|
-
throw ERROR_FACTORY.create("nonpositive trace startTime" /* ErrorCode.NONPOSITIVE_TRACE_START_TIME */, {
|
|
1097
|
-
traceName: this.name
|
|
1098
|
-
});
|
|
1099
|
-
}
|
|
1100
|
-
if (duration <= 0) {
|
|
1101
|
-
throw ERROR_FACTORY.create("nonpositive trace duration" /* ErrorCode.NONPOSITIVE_TRACE_DURATION */, {
|
|
1102
|
-
traceName: this.name
|
|
1103
|
-
});
|
|
1104
|
-
}
|
|
1105
|
-
this.durationUs = Math.floor(duration * 1000);
|
|
1106
|
-
this.startTimeUs = Math.floor(startTime * 1000);
|
|
1107
|
-
if (options && options.attributes) {
|
|
1108
|
-
this.customAttributes = __assign({}, options.attributes);
|
|
1109
|
-
}
|
|
1110
|
-
if (options && options.metrics) {
|
|
1111
|
-
for (var _i = 0, _a = Object.keys(options.metrics); _i < _a.length; _i++) {
|
|
1112
|
-
var metricName = _a[_i];
|
|
1113
|
-
if (!isNaN(Number(options.metrics[metricName]))) {
|
|
1114
|
-
this.counters[metricName] = Math.floor(Number(options.metrics[metricName]));
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
logTrace(this);
|
|
1119
|
-
};
|
|
1120
|
-
/**
|
|
1121
|
-
* Increments a custom metric by a certain number or 1 if number not specified. Will create a new
|
|
1122
|
-
* custom metric if one with the given name does not exist. The value will be floored down to an
|
|
1123
|
-
* integer.
|
|
1124
|
-
* @param counter Name of the custom metric
|
|
1125
|
-
* @param numAsInteger Increment by value
|
|
1126
|
-
*/
|
|
1127
|
-
Trace.prototype.incrementMetric = function (counter, numAsInteger) {
|
|
1128
|
-
if (numAsInteger === void 0) { numAsInteger = 1; }
|
|
1129
|
-
if (this.counters[counter] === undefined) {
|
|
1130
|
-
this.putMetric(counter, numAsInteger);
|
|
1131
|
-
}
|
|
1132
|
-
else {
|
|
1133
|
-
this.putMetric(counter, this.counters[counter] + numAsInteger);
|
|
1134
|
-
}
|
|
1135
|
-
};
|
|
1136
|
-
/**
|
|
1137
|
-
* Sets a custom metric to a specified value. Will create a new custom metric if one with the
|
|
1138
|
-
* given name does not exist. The value will be floored down to an integer.
|
|
1139
|
-
* @param counter Name of the custom metric
|
|
1140
|
-
* @param numAsInteger Set custom metric to this value
|
|
1141
|
-
*/
|
|
1142
|
-
Trace.prototype.putMetric = function (counter, numAsInteger) {
|
|
1143
|
-
if (isValidMetricName(counter, this.name)) {
|
|
1144
|
-
this.counters[counter] = convertMetricValueToInteger(numAsInteger !== null && numAsInteger !== void 0 ? numAsInteger : 0);
|
|
1145
|
-
}
|
|
1146
|
-
else {
|
|
1147
|
-
throw ERROR_FACTORY.create("invalid custom metric name" /* ErrorCode.INVALID_CUSTOM_METRIC_NAME */, {
|
|
1148
|
-
customMetricName: counter
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
};
|
|
1152
|
-
/**
|
|
1153
|
-
* Returns the value of the custom metric by that name. If a custom metric with that name does
|
|
1154
|
-
* not exist will return zero.
|
|
1155
|
-
* @param counter
|
|
1156
|
-
*/
|
|
1157
|
-
Trace.prototype.getMetric = function (counter) {
|
|
1158
|
-
return this.counters[counter] || 0;
|
|
1159
|
-
};
|
|
1160
|
-
/**
|
|
1161
|
-
* Sets a custom attribute of a trace to a certain value.
|
|
1162
|
-
* @param attr
|
|
1163
|
-
* @param value
|
|
1164
|
-
*/
|
|
1165
|
-
Trace.prototype.putAttribute = function (attr, value) {
|
|
1166
|
-
var isValidName = isValidCustomAttributeName(attr);
|
|
1167
|
-
var isValidValue = isValidCustomAttributeValue(value);
|
|
1168
|
-
if (isValidName && isValidValue) {
|
|
1169
|
-
this.customAttributes[attr] = value;
|
|
1170
|
-
return;
|
|
1171
|
-
}
|
|
1172
|
-
// Throw appropriate error when the attribute name or value is invalid.
|
|
1173
|
-
if (!isValidName) {
|
|
1174
|
-
throw ERROR_FACTORY.create("invalid attribute name" /* ErrorCode.INVALID_ATTRIBUTE_NAME */, {
|
|
1175
|
-
attributeName: attr
|
|
1176
|
-
});
|
|
1177
|
-
}
|
|
1178
|
-
if (!isValidValue) {
|
|
1179
|
-
throw ERROR_FACTORY.create("invalid attribute value" /* ErrorCode.INVALID_ATTRIBUTE_VALUE */, {
|
|
1180
|
-
attributeValue: value
|
|
1181
|
-
});
|
|
1182
|
-
}
|
|
1183
|
-
};
|
|
1184
|
-
/**
|
|
1185
|
-
* Retrieves the value a custom attribute of a trace is set to.
|
|
1186
|
-
* @param attr
|
|
1187
|
-
*/
|
|
1188
|
-
Trace.prototype.getAttribute = function (attr) {
|
|
1189
|
-
return this.customAttributes[attr];
|
|
1190
|
-
};
|
|
1191
|
-
Trace.prototype.removeAttribute = function (attr) {
|
|
1192
|
-
if (this.customAttributes[attr] === undefined) {
|
|
1193
|
-
return;
|
|
1194
|
-
}
|
|
1195
|
-
delete this.customAttributes[attr];
|
|
1196
|
-
};
|
|
1197
|
-
Trace.prototype.getAttributes = function () {
|
|
1198
|
-
return __assign({}, this.customAttributes);
|
|
1199
|
-
};
|
|
1200
|
-
Trace.prototype.setStartTime = function (startTime) {
|
|
1201
|
-
this.startTimeUs = startTime;
|
|
1202
|
-
};
|
|
1203
|
-
Trace.prototype.setDuration = function (duration) {
|
|
1204
|
-
this.durationUs = duration;
|
|
1205
|
-
};
|
|
1206
|
-
/**
|
|
1207
|
-
* Calculates and assigns the duration and start time of the trace using the measure performance
|
|
1208
|
-
* entry.
|
|
1209
|
-
*/
|
|
1210
|
-
Trace.prototype.calculateTraceMetrics = function () {
|
|
1211
|
-
var perfMeasureEntries = this.api.getEntriesByName(this.traceMeasure);
|
|
1212
|
-
var perfMeasureEntry = perfMeasureEntries && perfMeasureEntries[0];
|
|
1213
|
-
if (perfMeasureEntry) {
|
|
1214
|
-
this.durationUs = Math.floor(perfMeasureEntry.duration * 1000);
|
|
1215
|
-
this.startTimeUs = Math.floor((perfMeasureEntry.startTime + this.api.getTimeOrigin()) * 1000);
|
|
1216
|
-
}
|
|
1217
|
-
};
|
|
1218
|
-
/**
|
|
1219
|
-
* @param navigationTimings A single element array which contains the navigationTIming object of
|
|
1220
|
-
* the page load
|
|
1221
|
-
* @param paintTimings A array which contains paintTiming object of the page load
|
|
1222
|
-
* @param firstInputDelay First input delay in millisec
|
|
1223
|
-
*/
|
|
1224
|
-
Trace.createOobTrace = function (performanceController, navigationTimings, paintTimings, firstInputDelay) {
|
|
1225
|
-
var route = Api.getInstance().getUrl();
|
|
1226
|
-
if (!route) {
|
|
1227
|
-
return;
|
|
1228
|
-
}
|
|
1229
|
-
var trace = new Trace(performanceController, OOB_TRACE_PAGE_LOAD_PREFIX + route, true);
|
|
1230
|
-
var timeOriginUs = Math.floor(Api.getInstance().getTimeOrigin() * 1000);
|
|
1231
|
-
trace.setStartTime(timeOriginUs);
|
|
1232
|
-
// navigationTimings includes only one element.
|
|
1233
|
-
if (navigationTimings && navigationTimings[0]) {
|
|
1234
|
-
trace.setDuration(Math.floor(navigationTimings[0].duration * 1000));
|
|
1235
|
-
trace.putMetric('domInteractive', Math.floor(navigationTimings[0].domInteractive * 1000));
|
|
1236
|
-
trace.putMetric('domContentLoadedEventEnd', Math.floor(navigationTimings[0].domContentLoadedEventEnd * 1000));
|
|
1237
|
-
trace.putMetric('loadEventEnd', Math.floor(navigationTimings[0].loadEventEnd * 1000));
|
|
1238
|
-
}
|
|
1239
|
-
var FIRST_PAINT = 'first-paint';
|
|
1240
|
-
var FIRST_CONTENTFUL_PAINT = 'first-contentful-paint';
|
|
1241
|
-
if (paintTimings) {
|
|
1242
|
-
var firstPaint = paintTimings.find(function (paintObject) { return paintObject.name === FIRST_PAINT; });
|
|
1243
|
-
if (firstPaint && firstPaint.startTime) {
|
|
1244
|
-
trace.putMetric(FIRST_PAINT_COUNTER_NAME, Math.floor(firstPaint.startTime * 1000));
|
|
1245
|
-
}
|
|
1246
|
-
var firstContentfulPaint = paintTimings.find(function (paintObject) { return paintObject.name === FIRST_CONTENTFUL_PAINT; });
|
|
1247
|
-
if (firstContentfulPaint && firstContentfulPaint.startTime) {
|
|
1248
|
-
trace.putMetric(FIRST_CONTENTFUL_PAINT_COUNTER_NAME, Math.floor(firstContentfulPaint.startTime * 1000));
|
|
1249
|
-
}
|
|
1250
|
-
if (firstInputDelay) {
|
|
1251
|
-
trace.putMetric(FIRST_INPUT_DELAY_COUNTER_NAME, Math.floor(firstInputDelay * 1000));
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
logTrace(trace);
|
|
1255
|
-
};
|
|
1256
|
-
Trace.createUserTimingTrace = function (performanceController, measureName) {
|
|
1257
|
-
var trace = new Trace(performanceController, measureName, false, measureName);
|
|
1258
|
-
logTrace(trace);
|
|
1259
|
-
};
|
|
1260
|
-
return Trace;
|
|
1261
|
-
}());
|
|
1262
|
-
|
|
1263
|
-
/**
|
|
1264
|
-
* @license
|
|
1265
|
-
* Copyright 2020 Google LLC
|
|
1266
|
-
*
|
|
1267
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1268
|
-
* you may not use this file except in compliance with the License.
|
|
1269
|
-
* You may obtain a copy of the License at
|
|
1270
|
-
*
|
|
1271
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1272
|
-
*
|
|
1273
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1274
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1275
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1276
|
-
* See the License for the specific language governing permissions and
|
|
1277
|
-
* limitations under the License.
|
|
1278
|
-
*/
|
|
1279
|
-
function createNetworkRequestEntry(performanceController, entry) {
|
|
1280
|
-
var performanceEntry = entry;
|
|
1281
|
-
if (!performanceEntry || performanceEntry.responseStart === undefined) {
|
|
1282
|
-
return;
|
|
1283
|
-
}
|
|
1284
|
-
var timeOrigin = Api.getInstance().getTimeOrigin();
|
|
1285
|
-
var startTimeUs = Math.floor((performanceEntry.startTime + timeOrigin) * 1000);
|
|
1286
|
-
var timeToResponseInitiatedUs = performanceEntry.responseStart
|
|
1287
|
-
? Math.floor((performanceEntry.responseStart - performanceEntry.startTime) * 1000)
|
|
1288
|
-
: undefined;
|
|
1289
|
-
var timeToResponseCompletedUs = Math.floor((performanceEntry.responseEnd - performanceEntry.startTime) * 1000);
|
|
1290
|
-
// Remove the query params from logged network request url.
|
|
1291
|
-
var url = performanceEntry.name && performanceEntry.name.split('?')[0];
|
|
1292
|
-
var networkRequest = {
|
|
1293
|
-
performanceController: performanceController,
|
|
1294
|
-
url: url,
|
|
1295
|
-
responsePayloadBytes: performanceEntry.transferSize,
|
|
1296
|
-
startTimeUs: startTimeUs,
|
|
1297
|
-
timeToResponseInitiatedUs: timeToResponseInitiatedUs,
|
|
1298
|
-
timeToResponseCompletedUs: timeToResponseCompletedUs
|
|
1299
|
-
};
|
|
1300
|
-
logNetworkRequest(networkRequest);
|
|
1301
|
-
}
|
|
1302
|
-
|
|
1303
|
-
/**
|
|
1304
|
-
* @license
|
|
1305
|
-
* Copyright 2020 Google LLC
|
|
1306
|
-
*
|
|
1307
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1308
|
-
* you may not use this file except in compliance with the License.
|
|
1309
|
-
* You may obtain a copy of the License at
|
|
1310
|
-
*
|
|
1311
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1312
|
-
*
|
|
1313
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1314
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1315
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1316
|
-
* See the License for the specific language governing permissions and
|
|
1317
|
-
* limitations under the License.
|
|
1318
|
-
*/
|
|
1319
|
-
var FID_WAIT_TIME_MS = 5000;
|
|
1320
|
-
function setupOobResources(performanceController) {
|
|
1321
|
-
// Do not initialize unless iid is available.
|
|
1322
|
-
if (!getIid()) {
|
|
1323
|
-
return;
|
|
1324
|
-
}
|
|
1325
|
-
// The load event might not have fired yet, and that means performance navigation timing
|
|
1326
|
-
// object has a duration of 0. The setup should run after all current tasks in js queue.
|
|
1327
|
-
setTimeout(function () { return setupOobTraces(performanceController); }, 0);
|
|
1328
|
-
setTimeout(function () { return setupNetworkRequests(performanceController); }, 0);
|
|
1329
|
-
setTimeout(function () { return setupUserTimingTraces(performanceController); }, 0);
|
|
1330
|
-
}
|
|
1331
|
-
function setupNetworkRequests(performanceController) {
|
|
1332
|
-
var api = Api.getInstance();
|
|
1333
|
-
var resources = api.getEntriesByType('resource');
|
|
1334
|
-
for (var _i = 0, resources_1 = resources; _i < resources_1.length; _i++) {
|
|
1335
|
-
var resource = resources_1[_i];
|
|
1336
|
-
createNetworkRequestEntry(performanceController, resource);
|
|
1337
|
-
}
|
|
1338
|
-
api.setupObserver('resource', function (entry) {
|
|
1339
|
-
return createNetworkRequestEntry(performanceController, entry);
|
|
1340
|
-
});
|
|
1341
|
-
}
|
|
1342
|
-
function setupOobTraces(performanceController) {
|
|
1343
|
-
var api = Api.getInstance();
|
|
1344
|
-
var navigationTimings = api.getEntriesByType('navigation');
|
|
1345
|
-
var paintTimings = api.getEntriesByType('paint');
|
|
1346
|
-
// If First Input Delay polyfill is added to the page, report the fid value.
|
|
1347
|
-
// https://github.com/GoogleChromeLabs/first-input-delay
|
|
1348
|
-
if (api.onFirstInputDelay) {
|
|
1349
|
-
// If the fid call back is not called for certain time, continue without it.
|
|
1350
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1351
|
-
var timeoutId_1 = setTimeout(function () {
|
|
1352
|
-
Trace.createOobTrace(performanceController, navigationTimings, paintTimings);
|
|
1353
|
-
timeoutId_1 = undefined;
|
|
1354
|
-
}, FID_WAIT_TIME_MS);
|
|
1355
|
-
api.onFirstInputDelay(function (fid) {
|
|
1356
|
-
if (timeoutId_1) {
|
|
1357
|
-
clearTimeout(timeoutId_1);
|
|
1358
|
-
Trace.createOobTrace(performanceController, navigationTimings, paintTimings, fid);
|
|
1359
|
-
}
|
|
1360
|
-
});
|
|
1361
|
-
}
|
|
1362
|
-
else {
|
|
1363
|
-
Trace.createOobTrace(performanceController, navigationTimings, paintTimings);
|
|
1364
|
-
}
|
|
1365
|
-
}
|
|
1366
|
-
function setupUserTimingTraces(performanceController) {
|
|
1367
|
-
var api = Api.getInstance();
|
|
1368
|
-
// Run through the measure performance entries collected up to this point.
|
|
1369
|
-
var measures = api.getEntriesByType('measure');
|
|
1370
|
-
for (var _i = 0, measures_1 = measures; _i < measures_1.length; _i++) {
|
|
1371
|
-
var measure = measures_1[_i];
|
|
1372
|
-
createUserTimingTrace(performanceController, measure);
|
|
1373
|
-
}
|
|
1374
|
-
// Setup an observer to capture the measures from this point on.
|
|
1375
|
-
api.setupObserver('measure', function (entry) {
|
|
1376
|
-
return createUserTimingTrace(performanceController, entry);
|
|
1377
|
-
});
|
|
1378
|
-
}
|
|
1379
|
-
function createUserTimingTrace(performanceController, measure) {
|
|
1380
|
-
var measureName = measure.name;
|
|
1381
|
-
// Do not create a trace, if the user timing marks and measures are created by the sdk itself.
|
|
1382
|
-
if (measureName.substring(0, TRACE_MEASURE_PREFIX.length) ===
|
|
1383
|
-
TRACE_MEASURE_PREFIX) {
|
|
1384
|
-
return;
|
|
1385
|
-
}
|
|
1386
|
-
Trace.createUserTimingTrace(performanceController, measureName);
|
|
1387
|
-
}
|
|
1388
|
-
|
|
1389
|
-
/**
|
|
1390
|
-
* @license
|
|
1391
|
-
* Copyright 2020 Google LLC
|
|
1392
|
-
*
|
|
1393
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1394
|
-
* you may not use this file except in compliance with the License.
|
|
1395
|
-
* You may obtain a copy of the License at
|
|
1396
|
-
*
|
|
1397
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1398
|
-
*
|
|
1399
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1400
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1401
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1402
|
-
* See the License for the specific language governing permissions and
|
|
1403
|
-
* limitations under the License.
|
|
1404
|
-
*/
|
|
1405
|
-
var PerformanceController = /** @class */ (function () {
|
|
1406
|
-
function PerformanceController(app, installations) {
|
|
1407
|
-
this.app = app;
|
|
1408
|
-
this.installations = installations;
|
|
1409
|
-
this.initialized = false;
|
|
1410
|
-
}
|
|
1411
|
-
/**
|
|
1412
|
-
* This method *must* be called internally as part of creating a
|
|
1413
|
-
* PerformanceController instance.
|
|
1414
|
-
*
|
|
1415
|
-
* Currently it's not possible to pass the settings object through the
|
|
1416
|
-
* constructor using Components, so this method exists to be called with the
|
|
1417
|
-
* desired settings, to ensure nothing is collected without the user's
|
|
1418
|
-
* consent.
|
|
1419
|
-
*/
|
|
1420
|
-
PerformanceController.prototype._init = function (settings) {
|
|
1421
|
-
var _this = this;
|
|
1422
|
-
if (this.initialized) {
|
|
1423
|
-
return;
|
|
1424
|
-
}
|
|
1425
|
-
if ((settings === null || settings === void 0 ? void 0 : settings.dataCollectionEnabled) !== undefined) {
|
|
1426
|
-
this.dataCollectionEnabled = settings.dataCollectionEnabled;
|
|
1427
|
-
}
|
|
1428
|
-
if ((settings === null || settings === void 0 ? void 0 : settings.instrumentationEnabled) !== undefined) {
|
|
1429
|
-
this.instrumentationEnabled = settings.instrumentationEnabled;
|
|
1430
|
-
}
|
|
1431
|
-
if (Api.getInstance().requiredApisAvailable()) {
|
|
1432
|
-
validateIndexedDBOpenable()
|
|
1433
|
-
.then(function (isAvailable) {
|
|
1434
|
-
if (isAvailable) {
|
|
1435
|
-
setupTransportService();
|
|
1436
|
-
getInitializationPromise(_this).then(function () { return setupOobResources(_this); }, function () { return setupOobResources(_this); });
|
|
1437
|
-
_this.initialized = true;
|
|
1438
|
-
}
|
|
1439
|
-
})
|
|
1440
|
-
.catch(function (error) {
|
|
1441
|
-
consoleLogger.info("Environment doesn't support IndexedDB: ".concat(error));
|
|
1442
|
-
});
|
|
1443
|
-
}
|
|
1444
|
-
else {
|
|
1445
|
-
consoleLogger.info('Firebase Performance cannot start if the browser does not support ' +
|
|
1446
|
-
'"Fetch" and "Promise", or cookies are disabled.');
|
|
1447
|
-
}
|
|
1448
|
-
};
|
|
1449
|
-
Object.defineProperty(PerformanceController.prototype, "instrumentationEnabled", {
|
|
1450
|
-
get: function () {
|
|
1451
|
-
return SettingsService.getInstance().instrumentationEnabled;
|
|
1452
|
-
},
|
|
1453
|
-
set: function (val) {
|
|
1454
|
-
SettingsService.getInstance().instrumentationEnabled = val;
|
|
1455
|
-
},
|
|
1456
|
-
enumerable: false,
|
|
1457
|
-
configurable: true
|
|
1458
|
-
});
|
|
1459
|
-
Object.defineProperty(PerformanceController.prototype, "dataCollectionEnabled", {
|
|
1460
|
-
get: function () {
|
|
1461
|
-
return SettingsService.getInstance().dataCollectionEnabled;
|
|
1462
|
-
},
|
|
1463
|
-
set: function (val) {
|
|
1464
|
-
SettingsService.getInstance().dataCollectionEnabled = val;
|
|
1465
|
-
},
|
|
1466
|
-
enumerable: false,
|
|
1467
|
-
configurable: true
|
|
1468
|
-
});
|
|
1469
|
-
return PerformanceController;
|
|
1470
|
-
}());
|
|
1471
|
-
|
|
1472
|
-
/**
|
|
1473
|
-
* The Firebase Performance Monitoring Web SDK.
|
|
1474
|
-
* This SDK does not work in a Node.js environment.
|
|
1475
|
-
*
|
|
1476
|
-
* @packageDocumentation
|
|
1477
|
-
*/
|
|
1478
|
-
var DEFAULT_ENTRY_NAME = '[DEFAULT]';
|
|
1479
|
-
/**
|
|
1480
|
-
* Returns a {@link FirebasePerformance} instance for the given app.
|
|
1481
|
-
* @param app - The {@link @firebase/app#FirebaseApp} to use.
|
|
1482
|
-
* @public
|
|
1483
|
-
*/
|
|
1484
|
-
function getPerformance(app) {
|
|
1485
|
-
if (app === void 0) { app = getApp(); }
|
|
1486
|
-
app = getModularInstance(app);
|
|
1487
|
-
var provider = _getProvider(app, 'performance');
|
|
1488
|
-
var perfInstance = provider.getImmediate();
|
|
1489
|
-
return perfInstance;
|
|
1490
|
-
}
|
|
1491
|
-
/**
|
|
1492
|
-
* Returns a {@link FirebasePerformance} instance for the given app. Can only be called once.
|
|
1493
|
-
* @param app - The {@link @firebase/app#FirebaseApp} to use.
|
|
1494
|
-
* @param settings - Optional settings for the {@link FirebasePerformance} instance.
|
|
1495
|
-
* @public
|
|
1496
|
-
*/
|
|
1497
|
-
function initializePerformance(app, settings) {
|
|
1498
|
-
app = getModularInstance(app);
|
|
1499
|
-
var provider = _getProvider(app, 'performance');
|
|
1500
|
-
// throw if an instance was already created.
|
|
1501
|
-
// It could happen if initializePerformance() is called more than once, or getPerformance() is called first.
|
|
1502
|
-
if (provider.isInitialized()) {
|
|
1503
|
-
var existingInstance = provider.getImmediate();
|
|
1504
|
-
var initialSettings = provider.getOptions();
|
|
1505
|
-
if (deepEqual(initialSettings, settings !== null && settings !== void 0 ? settings : {})) {
|
|
1506
|
-
return existingInstance;
|
|
1507
|
-
}
|
|
1508
|
-
else {
|
|
1509
|
-
throw ERROR_FACTORY.create("already initialized" /* ErrorCode.ALREADY_INITIALIZED */);
|
|
1510
|
-
}
|
|
1511
|
-
}
|
|
1512
|
-
var perfInstance = provider.initialize({
|
|
1513
|
-
options: settings
|
|
1514
|
-
});
|
|
1515
|
-
return perfInstance;
|
|
1516
|
-
}
|
|
1517
|
-
/**
|
|
1518
|
-
* Returns a new `PerformanceTrace` instance.
|
|
1519
|
-
* @param performance - The {@link FirebasePerformance} instance to use.
|
|
1520
|
-
* @param name - The name of the trace.
|
|
1521
|
-
* @public
|
|
1522
|
-
*/
|
|
1523
|
-
function trace(performance, name) {
|
|
1524
|
-
performance = getModularInstance(performance);
|
|
1525
|
-
return new Trace(performance, name);
|
|
1526
|
-
}
|
|
1527
|
-
var factory = function (container, _a) {
|
|
1528
|
-
var settings = _a.options;
|
|
1529
|
-
// Dependencies
|
|
1530
|
-
var app = container.getProvider('app').getImmediate();
|
|
1531
|
-
var installations = container
|
|
1532
|
-
.getProvider('installations-internal')
|
|
1533
|
-
.getImmediate();
|
|
1534
|
-
if (app.name !== DEFAULT_ENTRY_NAME) {
|
|
1535
|
-
throw ERROR_FACTORY.create("FB not default" /* ErrorCode.FB_NOT_DEFAULT */);
|
|
1536
|
-
}
|
|
1537
|
-
if (typeof window === 'undefined') {
|
|
1538
|
-
throw ERROR_FACTORY.create("no window" /* ErrorCode.NO_WINDOW */);
|
|
1539
|
-
}
|
|
1540
|
-
setupApi(window);
|
|
1541
|
-
var perfInstance = new PerformanceController(app, installations);
|
|
1542
|
-
perfInstance._init(settings);
|
|
1543
|
-
return perfInstance;
|
|
1544
|
-
};
|
|
1545
|
-
function registerPerformance() {
|
|
1546
|
-
_registerComponent(new Component('performance', factory, "PUBLIC" /* ComponentType.PUBLIC */));
|
|
1547
|
-
registerVersion(name, version);
|
|
1548
|
-
// BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
|
|
1549
|
-
registerVersion(name, version, 'esm5');
|
|
1550
|
-
}
|
|
1551
|
-
registerPerformance();
|
|
1552
|
-
|
|
1553
|
-
export { getPerformance, initializePerformance, trace };
|
|
1554
|
-
//# sourceMappingURL=index.esm.js.map
|