All files scope.ts

100% Statements 74/74
85.29% Branches 29/34
100% Functions 21/21
100% Lines 72/72
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195            3x   49x     49x     49x     49x     49x     49x     49x           3x 5x       3x 3x           30x 30x 16x 16x 2x 2x   2x               3x 3x 3x               3x 4x 4x             3x 4x 4x             3x 13x 13x             3x 4x 4x             3x 12x 12x 12x       3x 1x       3x 11x       3x 1x       3x 1x       3x 1x       3x 1x 1x 1x 1x 1x 1x               3x 4x       4x                   3x 3x 3x   3x 2x   3x 2x   3x 1x       3x     3x 1x           3x   3x  
import { Breadcrumb, SentryEvent, User } from '@sentry/types';
 
/**
 * Holds additional event information. {@link Scope.applyToEvent} will be
 * called by the client before an event will be sent.
 */
export class Scope {
  /** Flag if notifiying is happening. */
  protected notifyingListeners: boolean = false;
 
  /** Callback for client to receive scope changes. */
  protected scopeListeners: Array<(scope: Scope) => void> = [];
 
  /** Callback list that will be called after {@link applyToEvent}. */
  protected eventProcessors: Array<(scope: SentryEvent) => void> = [];
 
  /** Array of breadcrumbs. */
  protected breadcrumbs: Breadcrumb[] = [];
 
  /** User */
  protected user: User = {};
 
  /** Tags */
  protected tags: { [key: string]: string } = {};
 
  /** Extra */
  protected extra: { [key: string]: any } = {};
 
  /** Fingerprint */
  protected fingerprint?: string[];
 
  /** Add internal on change listener. */
  public addScopeListener(callback: (scope: Scope) => void): void {
    this.scopeListeners.push(callback);
  }
 
  /** Add new event processor that will be called after {@link applyToEvent}. */
  public addEventProcessor(callback: (scope: SentryEvent) => void): void {
    this.eventProcessors.push(callback);
  }
 
  /**
   * This will be called on every set call.
   */
  protected notifyScopeListeners(): void {
    if (!this.notifyingListeners) {
      this.notifyingListeners = true;
      setTimeout(() => {
        this.scopeListeners.forEach(callback => {
          callback(this);
        });
        this.notifyingListeners = false;
      }, 0);
    }
  }
 
  /**
   * This will be called after {@link applyToEvent} is finished.
   */
  protected notifyEventProcessors(event: SentryEvent): void {
    this.eventProcessors.forEach(callback => {
      callback(event);
    });
  }
 
  /**
   * Updates user context information for future events.
   * @param user User context object to merge into current context.
   */
  public setUser(user: User): void {
    this.user = user;
    this.notifyScopeListeners();
  }
 
  /**
   * Updates tags context information for future events.
   * @param tags Tags context object to merge into current context.
   */
  public setTag(key: string, value: string): void {
    this.tags = { ...this.tags, [key]: value };
    this.notifyScopeListeners();
  }
 
  /**
   * Updates extra context information for future events.
   * @param extra Extra context object to merge into current context.
   */
  public setExtra(key: string, extra: any): void {
    this.extra = { ...this.extra, [key]: extra };
    this.notifyScopeListeners();
  }
 
  /**
   * Sets the fingerprint on the scope to send with the events.
   * @param fingerprint
   */
  public setFingerprint(fingerprint: string[]): void {
    this.fingerprint = fingerprint;
    this.notifyScopeListeners();
  }
 
  /**
   * Inherit values from the parent scope.
   * @param scope
   */
  public static clone(scope?: Scope): Scope {
    const newScope = new Scope();
    Object.assign(newScope, scope);
    return newScope;
  }
 
  /** Returns tags. */
  public getTags(): { [key: string]: string } {
    return this.tags;
  }
 
  /** Returns extra. */
  public getExtra(): { [key: string]: any } {
    return this.extra;
  }
 
  /** Returns extra. */
  public getUser(): User {
    return this.user;
  }
 
  /** Returns fingerprint. */
  public getFingerprint(): string[] | undefined {
    return this.fingerprint;
  }
 
  /** Returns breadcrumbs. */
  public getBreadcrumbs(): Breadcrumb[] {
    return this.breadcrumbs;
  }
 
  /** Clears the current scope and resets its properties. */
  public clear(): void {
    this.breadcrumbs = [];
    this.tags = {};
    this.extra = {};
    this.user = {};
    this.fingerprint = undefined;
    this.notifyScopeListeners();
  }
 
  /**
   * Sets the breadcrumbs in the scope
   * @param breadcrumbs Breadcrumb
   * @param maxBreadcrumbs number of max breadcrumbs to merged into event.
   */
  public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): void {
    this.breadcrumbs =
      maxBreadcrumbs !== undefined && maxBreadcrumbs >= 0
        ? [...this.breadcrumbs, breadcrumb].slice(-maxBreadcrumbs)
        : [...this.breadcrumbs, breadcrumb];
    this.notifyScopeListeners();
  }
 
  /**
   * Applies the current context and fingerprint to the event.
   * Note that breadcrumbs will be added by the client.
   * Also if the event has already breadcrumbs on it, we do not merge them.
   * @param event SentryEvent
   * @param maxBreadcrumbs number of max breadcrumbs to merged into event.
   */
  public applyToEvent(event: SentryEvent, maxBreadcrumbs?: number): void {
    Eif (this.extra && Object.keys(this.extra).length) {
      event.extra = { ...this.extra, ...event.extra };
    }
    if (this.tags && Object.keys(this.tags).length) {
      event.tags = { ...this.tags, ...event.tags };
    }
    if (this.user && Object.keys(this.user).length) {
      event.user = { ...this.user, ...event.user };
    }
    if (this.fingerprint && event.fingerprint === undefined) {
      event.fingerprint = this.fingerprint;
    }
    // We only want to set breadcrumbs in the event if there are none
    const hasNoBreadcrumbs =
      !event.breadcrumbs ||
      event.breadcrumbs.length === 0 ||
      (event.breadcrumbs.values && event.breadcrumbs.values.length === 0);
    if (hasNoBreadcrumbs && this.breadcrumbs.length > 0) {
      event.breadcrumbs =
        maxBreadcrumbs !== undefined && maxBreadcrumbs >= 0
          ? this.breadcrumbs.slice(-maxBreadcrumbs)
          : this.breadcrumbs;
    }
 
    this.notifyEventProcessors(event);
  }
}