<commit_before>// @flow
/* global RequestInfo, Response */
import EventEmitter from 'events'
import { authnFetch } from './authn-fetch'
import { openIdpSelector, startPopupServer } from './popup'
import type { Session } from './session'
import { getSession, saveSession, clearSession } from './session'
import type { AsyncStorage } from './storage'
import { defaultStorage } from './storage'
import { currentUrlNoParams } from './url-util'
import * as WebIdOidc from './webid-oidc'

export type loginOptions = {
  callbackUri: ?string,
  popupUri: ?string,
  storage: AsyncStorage
}

const defaultLoginOptions = (): loginOptions => {
  const url = currentUrlNoParams()
  return {
    callbackUri: url ? url.split('#')[0] : null,
    popupUri: null,
    storage: defaultStorage()
  }
}

export default class SolidAuthClient extends EventEmitter {
  fetch(url: RequestInfo, options?: Object): Promise<Response> {
    return authnFetch(defaultStorage())(url, options)
  }

  async login(idp: string, options: loginOptions): Promise<?Session> {
    options = { ...defaultLoginOptions(), ...options }
    const webIdOidcLogin = await WebIdOidc.login(idp, options)
    return webIdOidcLogin
  }

  async popupLogin(options: loginOptions): Promise<?Session> {
    if (!options.popupUri) {
      throw new Error('Must provide options.popupUri')
    }
    if (!/https?:/.test(options.popupUri)) {
      options.popupUri = new URL(
        options.popupUri || '',
        window.location
      ).toString()
    }
    if (!options.callbackUri) {
      options.callbackUri = options.popupUri
    }
    options = { ...defaultLoginOptions(), ...options }
    const childWindow = openIdpSelector(options)
    const session = await startPopupServer(
      options.storage,
      childWindow,
      options
    )
    return session
  }<commit_msg>Emit login and session events with popup.<commit_after>// @flow
/* global RequestInfo, Response */
import EventEmitter from 'events'
import { authnFetch } from './authn-fetch'
import { openIdpSelector, startPopupServer } from './popup'
import type { Session } from './session'
import { getSession, saveSession, clearSession } from './session'
import type { AsyncStorage } from './storage'
import { defaultStorage } from './storage'
import { currentUrlNoParams } from './url-util'
import * as WebIdOidc from './webid-oidc'

export type loginOptions = {
  callbackUri: ?string,
  popupUri: ?string,
  storage: AsyncStorage
}

const defaultLoginOptions = (): loginOptions => {
  const url = currentUrlNoParams()
  return {
    callbackUri: url ? url.split('#')[0] : null,
    popupUri: null,
    storage: defaultStorage()
  }
}

export default class SolidAuthClient extends EventEmitter {
  fetch(url: RequestInfo, options?: Object): Promise<Response> {
    return authnFetch(defaultStorage())(url, options)
  }

  async login(idp: string, options: loginOptions): Promise<?Session> {
    options = { ...defaultLoginOptions(), ...options }
    const webIdOidcLogin = await WebIdOidc.login(idp, options)
    return webIdOidcLogin
  }

  async popupLogin(options: loginOptions): Promise<?Session> {
    if (!options.popupUri) {
      throw new Error('Must provide options.popupUri')
    }
    if (!/https?:/.test(options.popupUri)) {
      options.popupUri = new URL(
        options.popupUri || '',
        window.location
      ).toString()
    }
    if (!options.callbackUri) {
      options.callbackUri = options.popupUri
    }
    options = { ...defaultLoginOptions(), ...options }
    const childWindow = openIdpSelector(options)
    const session = await startPopupServer(
      options.storage,
      childWindow,
      options
    )
    this.emit('login', session)
    this.emit('session', session)
    return session
  }
{'fraction_non_alphanumeric': 0.12599893446989877, 'fraction_numerical': 0.0005327650506126798, 'mean_word_length': 3.5132211538461537, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4299574', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1076, 'n_words': 288}