<commit_before>/*jslint nomen: true */
/*global module, require, process */
(function () {

    "use strict";

    var APIBase, root;

    APIBase = function (URL) {
        if (typeof URL !== 'string') { throw new Error("URL must be a string!"); }
        var attr, self = this;

        self._ref = new root.Firebase(URL);
        self._attributes = [];
        self._online = false;
        self._authState = 0;
        self._pendingResolutions = [];
        self._methods = {};
        self._user = {};
        self._isServer = false;
        self._cleanUpInterval = 60000;
        self._context = {}; 

        for (attr in self) {
            self._attributes.push(attr);
        }

        self._ref.child('_meta/online').on('value', function (snapshot) {
            self._online = snapshot.val() ? true : false;
        });
    };

    APIBase.prototype.publish = function () {
        var deferred = this._defer(),
            self = this;

        self._isServer = true;

        this._ref.child('_meta/online').once('value', function (snapshot) {
            if (snapshot.val()) { throw "A APIBase server is already running at " + self._ref.toString(); }
            self._pendingResolutions.push(deferred.resolve.bind(deferred));
            self._progress();
        });

        deferred.promise.then(function () {
            var attr, methodName, methodQueue
            for (attr in self) {
                if (self._attributes.indexOf(attr) === -1) {
                    self._methods[attr] = true;
                }
            }

            self._ref.child('_meta/methods').set(self._methods);
            self._ref.child('_meta/methods')
                .onDisconnect().remove();

            self._ref.child('_meta/online').set(true);
            self._ref.child('_meta/online')
                .onDisconnect().set(false);

            for (methodName in self._methods) {
                methodQueue = self._ref.child('queue').child('request');
                methodQueue.child(methodName).on('child_added', self._handleQueueItem.bind(self));
                setInterval(self._cleanUp.bind(self, methodName), self._cleanUpInterval);
            }
            
            self._ref.child('queue').child('request').on('child_added', self._handleMethodType.bind(self));
            
            self._ref.child('_meta/online').on('value', function (snapshot) {
                var online = snapshot.val();
                if (online) {
                    self._log("APIBase is listening for requests...");   
                }
            });
        });
    };

    APIBase.prototype.get = function (methodName) {
        return this._createFunction(methodName);
    };

    APIBase.prototype.retrieve = function () {
        var API = {},
            deferred = this._defer(),
            self = this;

        this._ref.child('_meta/methods').once('value', function (snapshot) {
            snapshot.forEach(function (methodSnapshot) {
                var methodName = methodSnapshot.name();
                API[methodName] = self._createFunction(methodName);
            });
            self._pendingResolutions.push(deferred.resolve.bind(deferred, API));
            self._progress();
        });

        return deferred.promise;
    };

    APIBase.prototype.auth = function (token) {
        var self = this;
        self._authState = 1; // Auth in the progress
        self._ref.auth(token.toString(), function (err, data) {
            if (err) { throw err; }
            self._authState = 2; // Auth complete
            self._user = data.auth;
            
            self._progress();
        });
    };
    
    APIBase.prototype.context = function (contextObj) {
        this._context = contextObj; 
    };

    APIBase.prototype._progress = function () {
        var resId;
        if (this._authState !== 2) { return false; }
        for (resId = 0; resId < this._pendingResolutions.length; resId += 1) {<commit_msg>Adds check for existing auth<commit_after>/*jslint nomen: true */
/*global module, require, process */
(function () {

    "use strict";

    var APIBase, root;

    APIBase = function (URL) {
        if (typeof URL !== 'string') { throw new Error("URL must be a string!"); }
        var attr, self = this;

        self._ref = new root.Firebase(URL);
        self._attributes = [];
        self._online = false;
        self._authState = 0;
        self._pendingResolutions = [];
        self._methods = {};
        self._user = {};
        self._isServer = false;
        self._cleanUpInterval = 60000;
        self._context = {}; 

        for (attr in self) {
            self._attributes.push(attr);
        }

        self._ref.child('_meta/online').on('value', function (snapshot) {
            self._online = snapshot.val() ? true : false;
        });
    };

    APIBase.prototype.publish = function () {
        var deferred = this._defer(),
            self = this;

        self._isServer = true;

        this._ref.child('_meta/online').once('value', function (snapshot) {
            if (snapshot.val()) { throw "A APIBase server is already running at " + self._ref.toString(); }
            self._pendingResolutions.push(deferred.resolve.bind(deferred));
            self._progress();
        });

        deferred.promise.then(function () {
            var attr, methodName, methodQueue
            for (attr in self) {
                if (self._attributes.indexOf(attr) === -1) {
                    self._methods[attr] = true;
                }
            }

            self._ref.child('_meta/methods').set(self._methods);
            self._ref.child('_meta/methods')
                .onDisconnect().remove();

            self._ref.child('_meta/online').set(true);
            self._ref.child('_meta/online')
                .onDisconnect().set(false);

            for (methodName in self._methods) {
                methodQueue = self._ref.child('queue').child('request');
                methodQueue.child(methodName).on('child_added', self._handleQueueItem.bind(self));
                setInterval(self._cleanUp.bind(self, methodName), self._cleanUpInterval);
            }
            
            self._ref.child('queue').child('request').on('child_added', self._handleMethodType.bind(self));
            
            self._ref.child('_meta/online').on('value', function (snapshot) {
                var online = snapshot.val();
                if (online) {
                    self._log("APIBase is listening for requests...");   
                }
            });
        });
    };

    APIBase.prototype.get = function (methodName) {
        return this._createFunction(methodName);
    };

    APIBase.prototype.retrieve = function () {
        var API = {},
            deferred = this._defer(),
            self = this;

        this._ref.child('_meta/methods').once('value', function (snapshot) {
            snapshot.forEach(function (methodSnapshot) {
                var methodName = methodSnapshot.name();
                API[methodName] = self._createFunction(methodName);
            });
            self._pendingResolutions.push(deferred.resolve.bind(deferred, API));
            self._progress();
        });

        return deferred.promise;
    };

    APIBase.prototype.auth = function (token, overwriteExistingAuth) {
        var self = this;
        self._authState = 1; // Auth in the progress
        self._ref.root().child('.info/authenticated').on('value', function (snapshot) {
            var authenticated = snapshot.val(); 
            
            if (authenticated && !overwriteExistingAuth) return;
            
            self._ref.auth(token.toString(), function (err, data) {
                if (err) { throw err; }
                self._authState = 2; // Auth complete
                self._user = data.auth;

                self._progress();
            });
        });
    };
    
    APIBase.prototype.context = function (contextObj) {
        this._context = contextObj; 
    };

    APIBase.prototype._progress = function () {
        var resId;
        if (this._authState !== 2) { return false; }
        for (resId = 0; resId < this._pendingResolutions.length; resId += 1) {
{'fraction_non_alphanumeric': 0.1362417743114794, 'fraction_numerical': 0.002924689251767, 'mean_word_length': 1.7877038043478262, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '40576', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2180, 'n_words': 427}