async function handleRequest(request) { let closest = null if (request.method ===…
- User
async function handleRequest(request) { let closest = null if (request.method === "POST") { let { urls, chat_id } = await request.json(); // Destructure URLs and chat_id from the request body // let fetchPromises = urls.map(url => fetch(url, { method: 'HEAD' })); // let fetchPromises = urls.map(url => fetch(url, { method: 'HEAD', cf: { cacheTtl: -1 } }));
// let responses = await Promise.all(fetchPromises); // let sizes = await Promise.all(responses.map(async (response, index) => { // const size = parseInt(response.headers.get('Content-Length'), 10); // return { url: urls[index], size }; // })); let sizes = null; sizes = []; for (const url of urls) { // Loop through each URL try { const response = await fetch(url, { method: 'HEAD', cf: { cacheTtl: -1 } }); const size = parseInt(response.headers.get('Content-Length'), 10); // Retrieve the size sizes.push({ url, size }); // Add the URL and size to the sizes array } catch (error) { console.error(`Failed to fetch ${url}: ${error}`); // Handle errors or simply continue to the next URL continue; } } // console.log(sizes) // adding batch request start // const batchSize = 4; // Define how many URLs to process at a time // let sizes = await batchFetch(urls, batchSize); // Use the batchFetch function // console.log(sizes); // adding batch request end let validSizes = sizes.filter(item => !isNaN(item.size) && item.size < (50 * 1024 * 1024)); // console.log(validSizes); if (validSizes.length === 0) { return new Response(JSON.stringify({ chat_id, error: "No files under 50MB found" }), { status: 404 }); } closest = validSizes.reduce((prev, curr) => (50 * 1024 * 1024 - curr.size < 50 * 1024 * 1024 - prev.size) ? curr : prev); console.log("closest url is...") console.log(closest); // Assuming you found the closest URL… if (closest.url) { // Call the second worker (replace 'your_other_worker_url' with the actual URL) // const otherWorkerUrl = 'https://jsondownloadsendforytvideo-ff42.dhanasekarramesh77.workers.dev'; // await callOtherWorker(otherWorkerUrl, downloadurl, chat_id); await sendVideoToTelegram(chat_id, closest.url) // console.log("other worker url"); // console.log(otherWorkerUrl); console.log("closest found url"); console.log(closest.url); console.log("chat_id"); console.log(chat_id); let downloadurl = null downloadurl = closest.url return new Response(JSON.stringify({ chat_id, downloadurl }), { status: 200, headers: { 'Content-Type': 'application/json' } }); } else { return new Response('No suitable URL found.', { status: 404 }); }}
// return new Response(JSON.stringify({ chat_id, closestUrl: closest.url, closestSize: closest.size }), // { status: 200, headers: { 'Content-Type': 'application/json' } }); // } return new Response("This Worker expects a POST request with a JSON object containing 'urls' and 'chat_id'.", { status: 400 }); - Assistant
- User
- Assistant
- User
ok further tweak this code to immediately return the response and keep all the background fetching functions run until completion
- Assistant
- User
nope. your code instantly returns response but none of the background functions are actually running.
- Assistant
- User
Nope I have previously used another code which returns response immediately and uses event.waitUntil to keep the functions running the background
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.