winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.94k stars 194 forks source link

Streaming types / APIs in standard library #7129

Open Chriscbr opened 2 days ago

Chriscbr commented 2 days ago

Use Case

Proposed Solution

Dedicated streaming APIs that don't require the entire contents of an API response or file or etc. to be loaded in memory at once.

(side note: maybe file streams and HTTP streams should be treated separately? I'm not sure)

Implementation Notes

References:

Component

SDK

Community Notes

Chriscbr commented 2 days ago

Related: https://github.com/winglang/wing/issues/5785

skyrpex commented 2 days ago

Use Case

  • Downloading a file from a bucket and saving it directly to disk
  • Downloading a file from an HTTP response, and performing processing on its output
  • Reading a file from disk as a stream, and processing it piece by piece (e.g. to count how many lines it has)
  • Streaming a response back from an API endpoint written by the user

(side note: maybe file streams and HTTP streams should be treated separately? I'm not sure)

I believe they are the same in the end but I'm no expert in that field so I can't really add much. You already shared Node's streams and the browser API's streams so we have good references there.

Chriscbr commented 2 days ago

Sharing another use case posted by a community member on our Discord:

I need to process a streaming response from a http request and forward the chunks to the client via websockets. Doing it using fetch I could do something like this:


const aiApiUrl = 'https://api.example.com/ai-stream';
const response = await fetch(aiApiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestBody)
});
if (!response.ok) {
  ws.send(JSON.stringify({ error: 'AI API request failed' }));
  return;
}

const reader = response.body
  .pipeThrough(new TextDecoderStream()) // Decode the text stream
  .getReader();

try {
  // Read the stream and forward each chunk via WebSocket
  while (true) {
    const { done, value } = await reader.read();
    if (done) {
      ws.send(JSON.stringify({ event: 'end' })); // Notify client that the stream ended
      break;
    }

    // Send the chunk to the WebSocket client
    ws.send(value);
  }
} catch (err) {
  console.error('Error during stream processing:', err);
  ws.send(JSON.stringify({ error: 'Stream processing error' }));
} finally {
  reader.releaseLock();
}


> How can I process a streaming response with Wing via the http library?
> If not any suggestions on how I can accomplish this?