<commit_before>import _ from 'underscore';
import { checkParamOrThrow } from 'apify-client/build/utils';

// We use weak maps here so that the content gets discarted after page gets closed.
const pageInterceptRequestHandlersMap = new WeakMap(); // Maps page to an array of request interception handlers.
const pageInterceptRequestMasterHandlerMap = new WeakMap(); // Maps page to master request interception handler.

/**
 * Executes an array for given intercept request handlers for a given request object.
 *
 * @param {Request} request Puppeteer's Request object.
 * @param {Array} interceptRequestHandlers An array of intercept request handlers.
 * @ignore
 */
const handleRequest = (request, interceptRequestHandlers) => {
    let wasAborted = false;
    let wasResponded = false;
    let wasContinued = false;
    const accumulatedOverrides = {
        headers: request.headers(),
    };

    const originalContinue = request.continue.bind(request);
    request.continue = (overrides = {}) => {
        wasContinued = true;
        const headers = Object.assign({}, accumulatedOverrides.headers, overrides.headers);
        Object.assign(accumulatedOverrides, overrides, { headers });
    };

    request.abort = _.wrap(request.abort.bind(request), (abort, ...args) => {
        wasAborted = true;

        return abort(...args);
    });

    request.respond = _.wrap(request.respond.bind(request), (respond, ...args) => {
        wasResponded = true;

        return respond(...args);
    });<commit_msg>Fix request interception for POST requests<commit_after>import _ from 'underscore';
import { checkParamOrThrow } from 'apify-client/build/utils';

// We use weak maps here so that the content gets discarted after page gets closed.
const pageInterceptRequestHandlersMap = new WeakMap(); // Maps page to an array of request interception handlers.
const pageInterceptRequestMasterHandlerMap = new WeakMap(); // Maps page to master request interception handler.

/**
 * Executes an array for given intercept request handlers for a given request object.
 *
 * @param {Request} request Puppeteer's Request object.
 * @param {Array} interceptRequestHandlers An array of intercept request handlers.
 * @ignore
 */
const handleRequest = (request, interceptRequestHandlers) => {
    // If there are no intercept handlers, it means that request interception is not enabled (anymore)
    // and therefore .abort() .respond() and .continue() would throw and crash the process.
    if (!interceptRequestHandlers.length) return;

    let wasAborted = false;
    let wasResponded = false;
    let wasContinued = false;
    const accumulatedOverrides = {
        headers: request.headers(),
    };

    const originalContinue = request.continue.bind(request);
    request.continue = (overrides = {}) => {
        wasContinued = true;
        const headers = Object.assign({}, accumulatedOverrides.headers, overrides.headers);
        Object.assign(accumulatedOverrides, overrides, { headers });
    };

    request.abort = _.wrap(request.abort.bind(request), (abort, ...args) => {
        wasAborted = true;

        return abort(...args);
    });

    request.respond = _.wrap(request.respond.bind(request), (respond, ...args) => {
        wasResponded = true;

        return respond(...args);
    });
{'fraction_non_alphanumeric': 0.11040339702760085, 'fraction_numerical': 0.0, 'mean_word_length': 3.4931880108991824, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4213019', 'n_tokens_mistral': 903, 'n_tokens_neox': 867, 'n_words': 281}