<commit_before>'use strict';

var retry = require('retry');
var deepExtend = require('deep-extend');

var errorCodes = [
    'EADDRINFO',
    'ETIMEDOUT',
    'ECONNRESET'
];

function requestReplay(request, options) {
    var originalEmit = request.emit;
    var operation;
    var attempts = 0;

    // Default options
    options = deepExtend({
        errorCodes: errorCodes,
        retries: 5,
        factor: 3,
        minTimeout: 2000,
        maxTimeout: 35000,
        randomize: true
    }, options || {});

    // Init retry
    operation = retry.operation(options);
    operation.attempt(function () {
        if (attempts) {
            request.start();
        }

        attempts++;
    });

    // Increase maxListeners because start() adds a new listener each time
    request._maxListeners += options.retries + 1;

    // Monkey patch emit to catch errors and retry
    request.emit = function (name, error) {
        // If not an error, pass-through
        if (name !== 'error') {
            return originalEmit.apply(this, arguments);
        }

        // If not a retry error code, pass-through
        if (options.errorCodes.indexOf(error.code) === -1) {
            return originalEmit.call(this, name, error);
        }

        // Retry
        if (operation.retry(error)) {
            this.emit('replay', attempts - 1, error);
            return 0;
        }

        // No more retries available, error out
        error.replays = attempts - 1;
        return originalEmit.call(this, name, error);
    };

    return request;
}

module.exports = requestReplay;
<commit_msg>Add ESOCKETTIMEDOUT as error code.<commit_after>'use strict';

var retry = require('retry');
var deepExtend = require('deep-extend');

var errorCodes = [
    'EADDRINFO',
    'ETIMEDOUT',
    'ECONNRESET',
    'ESOCKETTIMEDOUT'
];

function requestReplay(request, options) {
    var originalEmit = request.emit;
    var operation;
    var attempts = 0;

    // Default options
    options = deepExtend({
        errorCodes: errorCodes,
        retries: 5,
        factor: 3,
        minTimeout: 2000,
        maxTimeout: 35000,
        randomize: true
    }, options || {});

    // Init retry
    operation = retry.operation(options);
    operation.attempt(function () {
        if (attempts) {
            request.start();
        }

        attempts++;
    });

    // Increase maxListeners because start() adds a new listener each time
    request._maxListeners += options.retries + 1;

    // Monkey patch emit to catch errors and retry
    request.emit = function (name, error) {
        // If not an error, pass-through
        if (name !== 'error') {
            return originalEmit.apply(this, arguments);
        }

        // If not a retry error code, pass-through
        if (options.errorCodes.indexOf(error.code) === -1) {
            return originalEmit.call(this, name, error);
        }

        // Retry
        if (operation.retry(error)) {
            this.emit('replay', attempts - 1, error);
            return 0;
        }

        // No more retries available, error out
        error.replays = attempts - 1;
        return originalEmit.call(this, name, error);
    };

    return request;
}

module.exports = requestReplay;

{'fraction_non_alphanumeric': 0.11732186732186732, 'fraction_numerical': 0.010442260442260442, 'mean_word_length': 2.063969896519285, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4636037', 'n_tokens_mistral': 964, 'n_tokens_neox': 935, 'n_words': 257}