<commit_before>/*
 * Copyright 2012 <NAME>.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var promise = require("../modules/promise.js");
var noderError = require("../modules/noderError.js");
var HttpRequestObject = global.XMLHttpRequest;
var newHttpRequestObject;

if (HttpRequestObject) {
    newHttpRequestObject = function() {
        return new HttpRequestObject();
    };
} else {
    HttpRequestObject = global.ActiveXObject;
    newHttpRequestObject = function() {
        return new HttpRequestObject("Microsoft.XMLHTTP");
    };
}

var createCallback = function(url, xhr, deferred) {
    return function() {
        if (xhr && xhr.readyState == 4) {
            var error = (xhr.status != 200);
            if (error) {
                deferred.reject(noderError('XMLHttpRequest', [url, xhr]));
            } else {
                deferred.resolve(xhr.responseText, xhr);
            }
            // clean the closure:
            url = xhr = deferred = null;
        }
    };
};

module.exports = function(url, options) {
    options = options || {};
    var deferred = promise();
    var xhr = newHttpRequestObject();
    var headers = options.headers || {};
    xhr.open(options.method || 'GET', url, !options.sync);
    for (var key in headers) {
        if (headers.hasOwnProperty(key)) {
            xhr.setRequestHeader(key, headers[key]);
        }
    }
    xhr.onreadystatechange = createCallback(url, xhr, deferred);
    // Note that, on IE, onreadystatechange can be called during the call to send
    xhr.send(options.data);
    return deferred.promise();
};<commit_msg>No longer use onreadystatechange for synchronous requests<commit_after>/*
 * Copyright 2012 <NAME>.a.s.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var promise = require("../modules/promise.js");
var noderError = require("../modules/noderError.js");
var HttpRequestObject = global.XMLHttpRequest;
var newHttpRequestObject;

if (HttpRequestObject) {
    newHttpRequestObject = function() {
        return new HttpRequestObject();
    };
} else {
    HttpRequestObject = global.ActiveXObject;
    newHttpRequestObject = function() {
        return new HttpRequestObject("Microsoft.XMLHTTP");
    };
}

var createCallback = function(url, xhr, deferred) {
    return function() {
        if (xhr && xhr.readyState == 4) {
            var error = (xhr.status != 200);
            if (error) {
                deferred.reject(noderError('XMLHttpRequest', [url, xhr]));
            } else {
                deferred.resolve(xhr.responseText, xhr);
            }
            // clean the closure:
            url = xhr = deferred = null;
            return true;
        }
    };
};

module.exports = function(url, options) {
    options = options || {};
    var deferred = promise();
    var xhr = newHttpRequestObject();
    var headers = options.headers || {};
    xhr.open(options.method || 'GET', url, !options.sync);
    for (var key in headers) {
        if (headers.hasOwnProperty(key)) {
            xhr.setRequestHeader(key, headers[key]);
        }
    }
    xhr.send(options.data);
    var checkState = createCallback(url, xhr, deferred);
    if (!checkState()) {
        // only set onreadystatechange if it is useful
        // (i.e. the response is not available synchronously)
        xhr.onreadystatechange = checkState;
    }
    return deferred.promise();
};
{'fraction_non_alphanumeric': 0.11384335154826958, 'fraction_numerical': 0.00546448087431694, 'mean_word_length': 2.757912745936698, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5084656', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1198, 'n_words': 384}