private async sendEventImmediately()

in src/eventbuffer/InMemoryJSONEventBuffer.ts [374:458]


  private async sendEventImmediately(item: EventData): Promise<void> {
    this.logger.debug(
      `Event Reporting - InMemoryJSONEventBuffer - sendEventImmediately - important event received ${JSON.stringify(
        item
      )}`
    );
    const { name, ts, attributes } = item;
    const event: JSONIngestionEvent = {
      type: this.type,
      v: this.v,
      payloads: [
        {
          name,
          ts,
          ...attributes,
        },
      ],
    };
    let failed = false;
    let response: Response = null;
    const body = this.makeRequestBody([event]);
    try {
      response = await this.send(body);
      if (response.ok) {
        try {
          const data = await response.json();
          this.logger.debug(
            `Event Reporting - InMemoryJSONEventBuffer - sendEventImmediately - send successful event: ${body}, message: ${JSON.stringify(
              data
            )}`
          );
        } catch (err) {
          /* istanbul ignore next */
          this.logger.warn(
            `Event Reporting - InMemoryJSONEventBuffer - sendEventImmediately - Error reading OK response ${err} for event ${body}`
          );
        }
        return;
      } else {
        this.logger.error(
          `Event Reporting - InMemoryJSONEventBuffer - sendEventImmediately - Failed to send an important event ${body} with response status ${response.status}`
        );
        failed = true;
      }
    } catch (error) {
      this.logger.warn(
        `Event Reporting - There may be a failure in sending an important event ${body} to the ingestion endpoint ${error}.`
      );
      failed = true;
      try {
        /**
         * Important events like meetingEnded, meetingStartFailed may result into page-redirects.
         * In such a case, Firefox aborts the fetch request with 'NS_BINDING_ABORT' state.
         * Chrome and Safari show fetch request as cancelled and the fetch failure is catched, but,
         * events appear at ingestion backend. Chrome and Safari behavior is unreliable, but Firefox consistently fails,
         * hence, we beacon data as a last resort when using Firefox.
         * During the page-redirect, we do not have access to check fetch's response to handle Chrome and Safari behavior,
         * hence, event ingestion may fail.
         *
         */
        if (this.metadata.browserName.toLowerCase() === 'firefox') {
          const body = this.makeBeaconRequestBody([event]);
          this.logger.debug(
            `Event Reporting - InMemoryJSONEventBuffer - sendEventImmediately - beaconing data out ${body}`
          );
          if (!navigator.sendBeacon(`${this.ingestionURL}?beacon=1`, body)) {
            failed = true;
          } else {
            failed = false;
          }
        }
      } catch (error) {
        this.logger.warn(`Event Reporting - Error sending beacon for an important event ${body}`);
        failed = true;
      }
    }

    /* istanbul ignore else */
    if (failed) {
      this.logger.debug(
        `Event Reporting - InMemoryJSONEventBuffer - sendEventImmediately - pushing to failed events ${body}`
      );
      this.failedIngestionEvents.push(event);
    }
  }