const {VM, VMScript} = require("vm2");
const crypto = require('crypto')
const fs = require('fs')
function readFile(name){
    var data = fs.readFileSync(name);
    return data.toString()
}

const vm = new VM({
    sandbox: {
        'crypto':crypto,
        'Blob':Blob,
        'URL':URL,
        'DOMException':DOMException,
        'FormData':FormData,
        'MessageChannel':MessageChannel,
        'ReadableStream':ReadableStream,
        'TextDecoderStream':TextDecoderStream,
        'TextEncoderStream':TextEncoderStream,
        'TransformStream':TransformStream,
        'URLSearchParams':URLSearchParams,
        'fetch':fetch,
        'setTimeout':setTimeout,
        'clearInterval':clearInterval,
    }
});

let envCode = readFile("env.js")
let srcCode = readFile("src.js")

let code = envCode + "\n\n\n" + srcCode

code += `

function getargs(){
 var xhr = new XMLHttpRequest();
    xhr.open('POST', "https://www.southwest.com/api/air-booking/v1/air-booking/page/air/booking/shopping", false);
    xhr.send('{"adultPassengersCount":"1","adultsCount":"1","departureDate":"2023-05-27","departureTimeOfDay":"ALL_DAY","destinationAirportCode":"LAS","fareType":"USD","int":"HOMEQBOMAIR","originationAirportCode":"BOS","passengerType":"ADULT","reset":"true","returnDate":"","returnTimeOfDay":"ALL_DAY","tripType":"oneway","application":"air-booking","site":"southwest"}')
    // console.log(xhr.headers)
    console.log('结束times', times)
    console.log(medias.join('|||'))

    return JSON.stringify(xhr.headers);
}


`
const script = new VMScript(code,`${__dirname}/debug.js`);
// 运行脚本文件
vm.run(script);
console.log('vm.getGlobal("XMLHttpRequest")', vm.getGlobal("getargs"))
var xhr = vm.getGlobal("getargs");
console.log(xhr())
