Next.js Discord

Discord Forum

Vercel Edge function for API Route Handler has unlimited duration with streaming?

Unanswered
Mugger Crocodile posted this in #help-forum
Open in Discord
Mugger CrocodileOP
Suppose I have a long running job which need to process my JSONLine file line by line and store it to database, the question is if I am sending the response back within 25 seconds, does that mean I can process my file indefinitely until it complete?

export function POST( request:Request ){
  const body = await request.json()
  const url = body.record.url
  //I just run the function here?
  processJsonLines(url)

  return new Response("File processing has been initiated.", {
    status: 202, // HTTP status code 202 Accepted
    headers: { "Content-Type": "text/plain" },
  })
  
}


Below is the function which will run for maybe potentially 30mins
async function processJsonLines(url) {
  const response = await fetch(url);
  const reader = response.body.getReader();
  let { value: chunk, done: readerDone } = await reader.read();
  let chunkStr = '';

  while (!readerDone) {
    chunkStr += new TextDecoder('utf-8').decode(chunk);
    let lines = chunkStr.split('\n');
    chunkStr = lines.pop(); // last line might be incomplete

    for (let line of lines) {
      // process the line
      if (line) {
        Doing Supabase row insert here
        const {data, error} = await supabase.insert().....

      }
    }

    const { value, done } = await reader.read();
    chunk = value;
    readerDone = done;
  }

  // Process any remaining line
  if (chunkStr) {
    const json = JSON.parse(chunkStr);
    // handle the JSON object
  }
}

0 Replies