<commit_before>/**
 * Insert css when first loaded
 */

require('./lib/insert-css');

/**
 * Module dependencies.
 */

var bonzo = require('bonzo');
var _ = require('underscore');
var debug = require('debug')('auth0-lock');
var Auth0 = require('auth0-js');
var $ = require('./lib/bonzo-augmented');
var EventEmitter = require('events').EventEmitter;

var strategies = require('./lib/strategies');
var template = require('./lib/html/main.ejs');

var HeaderView = require('./lib/header');

var SigninPanel = require('./lib/mode-signin');
var SignupPanel = require('./lib/mode-signup');
var ResetPanel = require('./lib/mode-reset');
var LoggedinPanel = require('./lib/mode-loggedin');
var KerberosPanel = require('./lib/mode-kerberos');
var LoadingPanel = require('./lib/mode-loading');
var OptionsManager = require('./lib/options-manager');

//browser incompatibilities fixes
var placeholderSupported = require('./lib/supports-placeholder');
var has_animations = require('./lib/supports-animation');
var ocreate = require('./lib/object-create');
var stop = require('./lib/stop-event');
var utils = require('./lib/utils');
var bind = require('./lib/bind');

/**
 * Expose `Auth0Lock` constructor
 */

module.exports = Auth0Lock;

/**
 * Create `Auth0Lock` instance
 * resolving `options`.
 *
 * @param {String} clientID
 * @param {String} domain
 * @param {Object} options
 *     - cdn
 *     - assetsUrl
 * @return {Auth0Lock}
 * @constructor
 */

function Auth0Lock (clientID, domain, options) {
  if (!(this instanceof Auth0Lock)) {
    return new Auth0Lock(clientID, domain, options);
  }

  // validate required options
  if ('string' !== typeof clientID) throw new Error('`ClientID` required as first parameter.');
  if ('string' !== typeof domain) throw new Error('`domain` required as second parameter.');

  // Initiate `EventEmitter`
  EventEmitter.call(this);

  // Instance properties and options
  this.$options = _.extend({}, options);

  // Save clientID and domain in $options
  this.$options.clientID = clientID;
  this.$options.domain = domain;

  // Holds copy for all suppported strategies
  this.$strategies = strategies;

  // Holds auth0-js' instance
  this.$auth0 = new Auth0({
    clientID: this.$options.clientID,
    domain: this.$options.domain,
    useCordovaSocialPlugins: this.$options.useCordovaSocialPlugins
  });

  // use domain as assetsUrl if no assetsUrl provided
  // and domain is not *.auth0.com. Fallback to S3 url
  this.$options.assetsUrl = this.$options.assetsUrl || (this.isAuth0Domain() ? 'https://cdn.auth0.com/' : 'https://' + this.$options.domain + '/');

  // This cdn is only used for the "loading" image
  this.$options.cdn = this.$options.cdn || (this.isAuth0Domain() ? 'https://d19p4zemcycm7a.cloudfront.net/w2/' : 'https://' + this.$options.domain + '/w2/');

  // Holds SSO Data for return user experience
  this.$ssoData = null;

  // Holds widget's DOM `$container` ref
  this.$container = null;

  // holds client's connections configuration
  // retrieved from S3 or CDN/assetsUrl provided
  this.$client = {};
  this.getClientConfiguration(bind(this.setClientConfiguration, this));
}

/**
 * Expose current `Auth0Lock`'s version
 */

Auth0Lock.version = require('package.version');

/**
 * Inherit from `EventEmitter`
 */

Auth0Lock.prototype = ocreate(EventEmitter.prototype);

/**
 * Get client configuration.
 * XXX: Why not use jsonp? that woudld allow the
 * global namespace definition to be optional...
 *
 * @param {Function} done
 * @return {Auth0Lock}
 * @private
 */

Auth0Lock.prototype.getClientConfiguration = function (done) {
  var self = this;

  // Save callback to be called once
  // client configuration gets loaded
  if ('function' === typeof done) {
    this.once('client loaded', function (client) {
      done(client);
    });
  }

  // If not loading, check for already stored
  // in a previous widget instantiation
  global.window.Auth0 = global.window.Auth0 || { clients: {}, script_tags: {} };

  if (!global.window.Auth0.clients) {
    global.window.Auth0.clients = {};
    global.window.Auth0.script_tags = {};
  }

  var clients = global.window.Auth0.clients;
  var client = clients[this.$options.clientID];
  if (client) return this.emit('client loaded', client);

  // check if loading state
  // and then await for response
  // no need to monkey-patch again
  if (this.loadState) return;
  this.loadState = true;

  // Monkey patch Auth.setClient to load client
  var setClient = global.window.Auth0.setClient || function setClient() {};
  global.window.Auth0.setClient = function (client) {
    setClient.apply(window.Auth0, arguments);

    // If not this client, return
    if (self.$options.clientID !== client.id) return;

    // store the client
    clients[self.$options.clientID] = client;

    // notify initialized and pass the client with it
    self.emit('client loaded', client);
  };

  var script = global.window.Auth0.script_tags[this.$options.clientID];

  if (!script) {
    // Load client from assets url
    var script = document.createElement('script');
    script.src = this.$options.assetsUrl + 'client/' + this.$options.clientID + '.js' + '?t' + (+new Date());

    // Save script reference for other intances using the same clientID
    global.window.Auth0.script_tags[this.$options.clientID] = script;

    // Insert script in DOM head
    var firstScript = document.getElementsByTagName('script')[0];
    firstScript.parentNode.insertBefore(script, firstScript);
  }

  // Handle load and error for client config
  script.addEventListener('load', bind(this.onclientloadsuccess, this));
  script.addEventListener('error', bind(this.onclientloaderror, this));
  this.timeout = setTimeout(bind(this.onclientloaderror, this), 5000);
};

/**
 * Handle success for script load of client's configuration
 *
 * @private
 */

Auth0Lock.prototype.onclientloadsuccess = function() {

  // clear error timeout
  clearTimeout(this.timeout);<commit_msg>Remove unnecessary point of failure in trying to grab<commit_after>/**
 * Insert css when first loaded
 */

require('./lib/insert-css');

/**
 * Module dependencies.
 */

var bonzo = require('bonzo');
var _ = require('underscore');
var debug = require('debug')('auth0-lock');
var Auth0 = require('auth0-js');
var $ = require('./lib/bonzo-augmented');
var EventEmitter = require('events').EventEmitter;

var strategies = require('./lib/strategies');
var template = require('./lib/html/main.ejs');

var HeaderView = require('./lib/header');

var SigninPanel = require('./lib/mode-signin');
var SignupPanel = require('./lib/mode-signup');
var ResetPanel = require('./lib/mode-reset');
var LoggedinPanel = require('./lib/mode-loggedin');
var KerberosPanel = require('./lib/mode-kerberos');
var LoadingPanel = require('./lib/mode-loading');
var OptionsManager = require('./lib/options-manager');

//browser incompatibilities fixes
var placeholderSupported = require('./lib/supports-placeholder');
var has_animations = require('./lib/supports-animation');
var ocreate = require('./lib/object-create');
var stop = require('./lib/stop-event');
var utils = require('./lib/utils');
var bind = require('./lib/bind');

/**
 * Expose `Auth0Lock` constructor
 */

module.exports = Auth0Lock;

/**
 * Create `Auth0Lock` instance
 * resolving `options`.
 *
 * @param {String} clientID
 * @param {String} domain
 * @param {Object} options
 *     - cdn
 *     - assetsUrl
 * @return {Auth0Lock}
 * @constructor
 */

function Auth0Lock (clientID, domain, options) {
  if (!(this instanceof Auth0Lock)) {
    return new Auth0Lock(clientID, domain, options);
  }

  // validate required options
  if ('string' !== typeof clientID) throw new Error('`ClientID` required as first parameter.');
  if ('string' !== typeof domain) throw new Error('`domain` required as second parameter.');

  // Initiate `EventEmitter`
  EventEmitter.call(this);

  // Instance properties and options
  this.$options = _.extend({}, options);

  // Save clientID and domain in $options
  this.$options.clientID = clientID;
  this.$options.domain = domain;

  // Holds copy for all suppported strategies
  this.$strategies = strategies;

  // Holds auth0-js' instance
  this.$auth0 = new Auth0({
    clientID: this.$options.clientID,
    domain: this.$options.domain,
    useCordovaSocialPlugins: this.$options.useCordovaSocialPlugins
  });

  // use domain as assetsUrl if no assetsUrl provided
  // and domain is not *.auth0.com. Fallback to S3 url
  this.$options.assetsUrl = this.$options.assetsUrl || (this.isAuth0Domain() ? 'https://cdn.auth0.com/' : 'https://' + this.$options.domain + '/');

  // This cdn is only used for the "loading" image
  this.$options.cdn = this.$options.cdn || (this.isAuth0Domain() ? 'https://d19p4zemcycm7a.cloudfront.net/w2/' : 'https://' + this.$options.domain + '/w2/');

  // Holds SSO Data for return user experience
  this.$ssoData = null;

  // Holds widget's DOM `$container` ref
  this.$container = null;

  // holds client's connections configuration
  // retrieved from S3 or CDN/assetsUrl provided
  this.$client = {};
  this.getClientConfiguration(bind(this.setClientConfiguration, this));
}

/**
 * Expose current `Auth0Lock`'s version
 */

Auth0Lock.version = require('package.version');

/**
 * Inherit from `EventEmitter`
 */

Auth0Lock.prototype = ocreate(EventEmitter.prototype);

/**
 * Get client configuration.
 * XXX: Why not use jsonp? that woudld allow the
 * global namespace definition to be optional...
 *
 * @param {Function} done
 * @return {Auth0Lock}
 * @private
 */

Auth0Lock.prototype.getClientConfiguration = function (done) {
  var self = this;

  // Save callback to be called once
  // client configuration gets loaded
  if ('function' === typeof done) {
    this.once('client loaded', function (client) {
      done(client);
    });
  }

  // If not loading, check for already stored
  // in a previous widget instantiation
  global.window.Auth0 = global.window.Auth0 || { clients: {}, script_tags: {} };

  if (!global.window.Auth0.clients) {
    global.window.Auth0.clients = {};
    global.window.Auth0.script_tags = {};
  }

  var clients = global.window.Auth0.clients;
  var client = clients[this.$options.clientID];
  if (client) return this.emit('client loaded', client);

  // check if loading state
  // and then await for response
  // no need to monkey-patch again
  if (this.loadState) return;
  this.loadState = true;

  // Monkey patch Auth.setClient to load client
  var setClient = global.window.Auth0.setClient || function setClient() {};
  global.window.Auth0.setClient = function (client) {
    setClient.apply(window.Auth0, arguments);

    // If not this client, return
    if (self.$options.clientID !== client.id) return;

    // store the client
    clients[self.$options.clientID] = client;

    // notify initialized and pass the client with it
    self.emit('client loaded', client);
  };

  var script = global.window.Auth0.script_tags[this.$options.clientID];

  if (!script) {
    // Load client from assets url
    var script = document.createElement('script');
    script.src = this.$options.assetsUrl + 'client/' + this.$options.clientID + '.js' + '?t' + (+new Date());

    // Save script reference for other intances using the same clientID
    global.window.Auth0.script_tags[this.$options.clientID] = script;

    // Insert script in DOM head
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  // Handle load and error for client config
  script.addEventListener('load', bind(this.onclientloadsuccess, this));
  script.addEventListener('error', bind(this.onclientloaderror, this));
  this.timeout = setTimeout(bind(this.onclientloaderror, this), 5000);
};

/**
 * Handle success for script load of client's configuration
 *
 * @private
 */

Auth0Lock.prototype.onclientloadsuccess = function() {

  // clear error timeout
  clearTimeout(this.timeout);
{'fraction_non_alphanumeric': 0.14888254074945387, 'fraction_numerical': 0.007897832297092925, 'mean_word_length': 3.789939637826962, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, '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': '3531949', 'n_tokens_mistral': 3584, 'n_tokens_neox': 3514, 'n_words': 951}