modules/utils.js
/**
 * Call back function that will poll for a report 
 * until success or failure
 * status reported back via scan_progress_callback 
 **/
Call back function that will poll for a report
until success or failure
status reported back via scan_progress_callback
Call back function that will poll for a report until success or failure status reported back via scan_progress_callback
function poll_for_report(security_api, api_key, report_id, scan_progress_callback, polling_interval = 500) {
    const max_interval = 60 * 1000 * 5; 
    if (polling_interval > max_interval) {
        console.assert(false, `timed out waiting for ${report_id}`);
    }
    const headers = getSecplugsAPIHeaders(api_key);
    let poll_request_url = security_api + `/${report_id}`;
    fetch(poll_request_url, { method: "GET", headers: headers })
        .then(response => {
            console.assert(response.ok);
            response.json()
                .then(json_response => {
                    var scan_status = map_json_response_2_scan_status(json_response);
                    scan_progress_callback && scan_progress_callback(scan_status);
                    if (json_response['status'] == 'pending') {
                        setTimeout(() => {
                                poll_for_report(
                                    report_id,
                                    security_api,
                                    api_key,
                                    scan_progress_callback,
                                    polling_interval * 2);
                            },
                            polling_interval
                        );
                    }
                });
        });
}
function poll_for_report ( security_api , api_key , report_id , scan_progress_callback , polling_interval = 500 ) { const max_interval = 60 * 1000 * 5 ; if ( polling_interval > max_interval ) { console . assert ( false , ` ${ report_id } ` ) ; } const headers = getSecplugsAPIHeaders ( api_key ) ; let poll_request_url = security_api + ` ${ report_id } ` ; fetch ( poll_request_url , { method : "GET" , headers : headers } ) . then ( response => { console . assert ( response . ok ) ; response . json ( ) . then ( json_response => { var scan_status = map_json_response_2_scan_status ( json_response ) ; scan_progress_callback && scan_progress_callback ( scan_status ) ; if ( json_response [ 'status' ] == 'pending' ) { setTimeout ( ( ) => { poll_for_report ( report_id , security_api , api_key , scan_progress_callback , polling_interval * 2 ) ; } , polling_interval ) ; } } ) ; } ) ; }
Call back function that will poll for a report
 until success or failure
 status reported back via scan_progress_callback
Call back function that will poll for a report until success or failure status reported back via scan_progress_callback
// Trace timeout when interval is too large // 5 mins // Get the headers // Build the url to poll for the report // Trace error // Load json // Check status and report back // Reschedule if still pending // Call with exponential back off polling_interval