@firebase/performance 0.7.6-canary.d590889d6 → 0.7.6-canary.e59cd7da1

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/index.cjs.js CHANGED
@@ -10,7 +10,7 @@ var component = require('@firebase/component');
10
10
  require('@firebase/installations');
11
11
 
12
12
  const name = "@firebase/performance";
13
- const version = "0.7.6-canary.d590889d6";
13
+ const version = "0.7.6-canary.e59cd7da1";
14
14
 
15
15
  /**
16
16
  * @license
@@ -331,6 +331,9 @@ class SettingsService {
331
331
  this.logNetworkAfterSampling = false;
332
332
  // TTL of config retrieved from remote config in hours.
333
333
  this.configTimeToLive = 12;
334
+ // The max number of events to send during a flush. This number is kept low to since Chrome has a
335
+ // shared payload limit for all sendBeacon calls in the same nav context.
336
+ this.logMaxFlushSize = 40;
334
337
  }
335
338
  getFlTransportFullUrl() {
336
339
  return this.flTransportEndpointUrl.concat('?key=', this.transportKey);
@@ -618,6 +621,12 @@ function processConfig(config) {
618
621
  settingsServiceInstance.tracesSamplingRate =
619
622
  DEFAULT_CONFIGS.tracesSamplingRate;
620
623
  }
624
+ if (entries.fpr_log_max_flush_size) {
625
+ settingsServiceInstance.logMaxFlushSize = Number(entries.fpr_log_max_flush_size);
626
+ }
627
+ else if (DEFAULT_CONFIGS.logMaxFlushSize) {
628
+ settingsServiceInstance.logMaxFlushSize = DEFAULT_CONFIGS.logMaxFlushSize;
629
+ }
621
630
  // Set the per session trace and network logging flags.
622
631
  settingsServiceInstance.logTraceAfterSampling = shouldLogAfterSampling(settingsServiceInstance.tracesSamplingRate);
623
632
  settingsServiceInstance.logNetworkAfterSampling = shouldLogAfterSampling(settingsServiceInstance.networkRequestsSamplingRate);
@@ -708,6 +717,9 @@ const DEFAULT_SEND_INTERVAL_MS = 10 * 1000;
708
717
  const INITIAL_SEND_TIME_DELAY_MS = 5.5 * 1000;
709
718
  const MAX_EVENT_COUNT_PER_REQUEST = 1000;
710
719
  const DEFAULT_REMAINING_TRIES = 3;
720
+ // Most browsers have a max payload of 64KB for sendbeacon/keep alive payload.
721
+ const MAX_SEND_BEACON_PAYLOAD_SIZE = 65536;
722
+ const TEXT_ENCODER = new TextEncoder();
711
723
  let remainingTries = DEFAULT_REMAINING_TRIES;
712
724
  /* eslint-enable camelcase */
713
725
  let queue = [];
@@ -735,13 +747,28 @@ function dispatchQueueEvents() {
735
747
  // The staged events will be used for current logRequest attempt, remaining events will be kept
736
748
  // for next attempt.
737
749
  const staged = queue.splice(0, MAX_EVENT_COUNT_PER_REQUEST);
750
+ const data = buildPayload(staged);
751
+ postToFlEndpoint(data)
752
+ .then(() => {
753
+ remainingTries = DEFAULT_REMAINING_TRIES;
754
+ })
755
+ .catch(() => {
756
+ // If the request fails for some reason, add the events that were attempted
757
+ // back to the primary queue to retry later.
758
+ queue = [...staged, ...queue];
759
+ remainingTries--;
760
+ consoleLogger.info(`Tries left: ${remainingTries}.`);
761
+ processQueue(DEFAULT_SEND_INTERVAL_MS);
762
+ });
763
+ }
764
+ function buildPayload(events) {
738
765
  /* eslint-disable camelcase */
739
766
  // We will pass the JSON serialized event to the backend.
740
- const log_event = staged.map(evt => ({
767
+ const log_event = events.map(evt => ({
741
768
  source_extension_json_proto3: evt.message,
742
769
  event_time_ms: String(evt.eventTime)
743
770
  }));
744
- const data = {
771
+ const transportBatchLog = {
745
772
  request_time_ms: String(Date.now()),
746
773
  client_info: {
747
774
  client_type: 1, // 1 is JS
@@ -751,29 +778,23 @@ function dispatchQueueEvents() {
751
778
  log_event
752
779
  };
753
780
  /* eslint-enable camelcase */
754
- postToFlEndpoint(data)
755
- .then(() => {
756
- remainingTries = DEFAULT_REMAINING_TRIES;
757
- })
758
- .catch(() => {
759
- // If the request fails for some reason, add the events that were attempted
760
- // back to the primary queue to retry later.
761
- queue = [...staged, ...queue];
762
- remainingTries--;
763
- consoleLogger.info(`Tries left: ${remainingTries}.`);
764
- processQueue(DEFAULT_SEND_INTERVAL_MS);
765
- });
781
+ return JSON.stringify(transportBatchLog);
766
782
  }
767
- function postToFlEndpoint(data) {
783
+ /** Sends to Firelog. Atempts to use sendBeacon otherwsise uses fetch. */
784
+ function postToFlEndpoint(body) {
768
785
  const flTransportFullUrl = SettingsService.getInstance().getFlTransportFullUrl();
769
- const body = JSON.stringify(data);
770
- return navigator.sendBeacon && navigator.sendBeacon(flTransportFullUrl, body)
771
- ? Promise.resolve()
772
- : fetch(flTransportFullUrl, {
786
+ const size = TEXT_ENCODER.encode(body).length;
787
+ if (size <= MAX_SEND_BEACON_PAYLOAD_SIZE &&
788
+ navigator.sendBeacon &&
789
+ navigator.sendBeacon(flTransportFullUrl, body)) {
790
+ return Promise.resolve();
791
+ }
792
+ else {
793
+ return fetch(flTransportFullUrl, {
773
794
  method: 'POST',
774
- body,
775
- keepalive: true
776
- }).then();
795
+ body
796
+ });
797
+ }
777
798
  }
778
799
  function addToQueue(evt) {
779
800
  if (!evt.eventTime || !evt.message) {
@@ -795,12 +816,33 @@ serializer) {
795
816
  };
796
817
  }
797
818
  /**
798
- * Force flush the queued events. Useful at page unload time to ensure all
799
- * events are uploaded.
819
+ * Force flush the queued events. Useful at page unload time to ensure all events are uploaded.
820
+ * Flush will attempt to use sendBeacon to send events async and defaults back to fetch as soon as a
821
+ * sendBeacon fails. Firefox
800
822
  */
801
823
  function flushQueuedEvents() {
824
+ const flTransportFullUrl = SettingsService.getInstance().getFlTransportFullUrl();
802
825
  while (queue.length > 0) {
803
- dispatchQueueEvents();
826
+ // Send the last events first to prioritize page load traces
827
+ const staged = queue.splice(-SettingsService.getInstance().logMaxFlushSize);
828
+ const body = buildPayload(staged);
829
+ if (navigator.sendBeacon &&
830
+ navigator.sendBeacon(flTransportFullUrl, body)) {
831
+ continue;
832
+ }
833
+ else {
834
+ queue = [...queue, ...staged];
835
+ break;
836
+ }
837
+ }
838
+ if (queue.length > 0) {
839
+ const body = buildPayload(queue);
840
+ fetch(flTransportFullUrl, {
841
+ method: 'POST',
842
+ body
843
+ }).catch(() => {
844
+ consoleLogger.info(`Failed flushing queued events.`);
845
+ });
804
846
  }
805
847
  }
806
848