All files / src/modules PersistentStore.ts

92.3% Statements 24/26
100% Branches 9/9
77.77% Functions 7/9
92% Lines 23/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 654x   4x   4x   4x   3x   3x 3x   1x   1x             14x                         7x 7x 7x       10x       12x 12x   7x 7x       4x 4x       2x   2x 2x      
import { Subject } from '@wirewheel/cmp/src/entities/Subject'
 
export abstract class PersistentStore {
  static canSetCookies(): boolean {
    try {
      // Set the test cookie with a value of "test"
      document.cookie = 'test=test;SameSite=None;Secure'
      // Check if the cookie was successfully set by reading the cookies string
      const enabled = document.cookie.indexOf('test=test') >= 0
      // Deleting cookie
      document.cookie = 'test=test; expires=Thu, 01 Jan 1970 00:00:00 UTC; SameSite=None; Secure;'
      return enabled
    } catch (cause) {
      console.warn(cause)
      // Fallback if it cannot write a cookie
      return false
    }
  }
 
  protected abstract write(value: any): void
  protected abstract read(): any
 
  private parentDomain: string | undefined = undefined
 
  protected constructor() {}
 
  getParentDomain(): string | undefined {
    return this.parentDomain
  }
 
  setParentDomain(newParentDomain: string) {
    this.parentDomain = newParentDomain
  }
 
  getOrCreateSubject(): Subject {
    const subject = this.getSubject() || Subject.anonymous()
    this.setSubject(subject)
    return subject
  }
 
  setSubject(subject: Subject): void {
    this.write({ subject })
  }
 
  getSubject(): Subject | undefined {
    const store = this.read()
    if (!store.subject) return
 
    const { verifiedId, anonymousId } = store.subject
    return verifiedId ? Subject.verified(verifiedId) : Subject.anonymous(anonymousId)
  }
 
  getGPCConfirmationBySubjectId(subjectId: string) {
    const gpcConfirmation = this.read().privacyProtocols
    return gpcConfirmation && gpcConfirmation[subjectId]
  }
 
  setGPCConfirmationBySubjectId(subjectId: string, data: any) {
    const privacyProtocols: any = this.read().privacyProtocols || {}
 
    privacyProtocols[subjectId] = data
    this.write({ privacyProtocols })
  }
}