Manual Reference Source Test

src/modules/identity-cookie.js

import {checkIsLocal} from '../helpers/local';

/**
 * Identity SDK cookie helpers
 */
class IdentityCookie {
  /**
   * Set a list of available cookie names
   * @param {Object} config
   */
  constructor(config = {}) {
    let propertiesName = 'properties';

    if (config.showAttributes) {
      const {show, season} = config.showAttributes;

      if (show && season) {
        propertiesName = `${show}S${season}`;
      }
    }

    this._cookieNames = {
      crossApp: 'park-crossApp',
      crossAppToken: 'park-crossAppToken',
      properties: `park-${propertiesName}`,
      token: 'park-fr',
      userData: 'park-userData',
      profile: 'park-profile',
    };

    this._local = checkIsLocal();
    this._enabled = false;
  }

  /**
   * Set enabled flag
   *
   * @param {boolean} isEnabled
   */
  setEnabled(isEnabled) {
    this._enabled = isEnabled;
  }

  /**
   * Get enabled flag
   *
   * @return {boolean}
   */
  isEnabled() {
    return this._enabled;
  }

  /**
   * Get from document cookies
   *
   * @param {string} name - cookie name
   * @param {Object} options - additional options on setting cookie
   * @return {string} or null
   */
  getCookie(name, options = {}) {
    if (!this._enabled) return;
    let cookieName = this._cookieNames[name] || name;
    if (options.userId) cookieName = `${cookieName}--${options.userId}`;
    const cookie = document.cookie.split(';').filter((item) => {
      return item.includes(`${cookieName}=`);
    });

    return cookie[0] && cookie[0].replace(`${cookieName}=`, '').trim() || null;
  }

  /**
   * Remove from cookies
   *
   * @param {string} name
   */
  removeCookie(name) {
    if (!this._enabled) return;
    const cookieName = this._cookieNames[name] || name;
    this.setCookie(cookieName, '', {}, -1);
  }

  /**
   * Set a wildcard domain cookie with an expiry of 1 year (12 months)
   *
   * @param {string} name - cookie name
   * @param {string} value - cookie value
   * @param {Object} options - additional options on setting cookie
   * @param {number} expires - days until expiry
   */
  setCookie(name, value, options = {}, expires = 365) {
    if (!this._enabled) return;
    const cookieList = [];
    let cookieName = this._cookieNames[name] || name;
    if (options.userId) cookieName = `${cookieName}--${options.userId}`;

    const today = new Date();
    expires = new Date(today.getTime() + expires * 24 * 60 * 60 * 1000);

    cookieList.push(`${cookieName}=${value}`);
    cookieList.push(`expires=${expires.toUTCString()}`);
    cookieList.push('path=/');
    cookieList.push('SameSite=None');
    cookieList.push('Secure');

    if (!this._local) {
      cookieList.push('domain=.nbc.com');
    }

    document.cookie = cookieList.join(';');
  }

  /**
   * Subscribe to cookie changes
   *
   * @param {string} name
   * @param {function} callback
   * @param {number} period
   *
   * @return {number} Interval id
   */
  subscribe(name, callback, period = 1000) {
    if (!this._enabled) return;
    let history = this.getCookie(name);

    return window.setInterval(() => {
      const current = this.getCookie(name);

      if (history !== current) {
        history = current;
        return current ? callback(current) : callback();
      }
    }, period);
  }

  /**
   * Unsubscribe from cookie changes
   *
   * @param {number} id
   */
  unsubscribe(id) {
    if (!this._enabled) return;
    window.clearInterval(id);
  }
}

export default IdentityCookie;