astuyve / lambda-stream

Implements AWS Lambda Response Streaming in TypeScript for local development and testing
82 stars 9 forks source link

Issue running in production with SST #14

Closed Gr33nLight closed 8 months ago

Gr33nLight commented 8 months ago

My issue is that in production, my lambda code, runs and returns the entire body at once. I tested it with the following code, adding delays to check for response delivery. The lambda function is already set to RENSPONSE_STREAM and not BUFFER. I'm calling the lambda url directly, not passing through Api gateway as I know its not supported. I don't know whan else to try, please help.

lambda function:

import { streamifyResponse } from 'lambda-stream';

export const handler = streamifyResponse(async function (event, responseStream) {
  responseStream.setContentType('text/plain');
  responseStream.write(JSON.stringify({ a: '1' }));
  await new Promise((r) => setTimeout(r, 4000));
  responseStream.write(JSON.stringify({ b: '2' }));
  await new Promise((r) => setTimeout(r, 1000));
  responseStream.write(JSON.stringify({ c: '3' }));
  responseStream.end();
});

SST stack


   const api = new Function(stack, 'Chat', {
    timeout: 120,
    memorySize: 512,
    url: {
      streaming: true,
      cors: {
        allowHeaders: ['*'],
        ...
      },
    },
    handler: 'packages/functions/src/chat.handler',
    environment: {
     ...
    },
  }); 

This is the function url https://wphwlqy64yzmoyeuiokt7t6asy0cwqfb.lambda-url.eu-south-1.on.aws/