Code Monkey home page Code Monkey logo

Comments (3)

willfarrell avatar willfarrell commented on August 17, 2024 1

Directly to Function URL or through API Gateway?

Empty body cause the Lambda to respond with 200 instead if 301

This is an AWS issue, Middy doesn't override statusCodes within core. Please open a support ticket with AWS, and point them here.

If AWS signals that they won't fix, we could change https://github.com/middyjs/middy/blob/main/packages/core/index.js#L58 to default to as a workaround. I'll have to think on If it should always or only when statusCode is 204, 300 - 399, or others.

PS I recall 3xx statusCodes used to return 500s when it was first released. So, they may be willing to fix.

from middy.

mdenkinger avatar mdenkinger commented on August 17, 2024

@willfarrell Thanks for checking. It’s through a Function URL. I’ll raise a case a AWS and then update this issue with the response.

from middy.

mdenkinger avatar mdenkinger commented on August 17, 2024

While working on the AWS case, I dug into the middy code more closely. I think I found a potential problem in how middy handles strings with no content. Basically, when there's no content in the iterator generator does not yield and then the pipeline then does not process the HttpResponseStream. As far as I can see.

To fix this, we can make a simple change. By ensuring the generator always yields content (even if it's just once with no content), we can resolve the issue.

use while (position <= length) instead of while (position < length).

Here's a sample code without middy, but with the algorithm to generate a string response in a stream-like manner.

import util from 'util';
import stream from 'stream';

const {Readable} = stream;
const pipeline = util.promisify(stream.pipeline);


export const handler = awslambda.streamifyResponse(async (event, responseStream, _context) => {

    responseStream = awslambda.HttpResponseStream.from(responseStream, {
        statusCode: 301, headers: {
            location: 'https://example.com'
        }
    });

    let handlerBody = null;
    handlerBody = handlerBody ?? ''

    // Source @datastream/core (MIT)
    let handlerStream
    if (handlerBody._readableState) {
        handlerStream = handlerBody
        console.log('its a stream')
    } else if (typeof handlerBody === 'string') {
        function* iterator(input) {
            const size = 16384 // 16 * 1024 // Node.js default
            let position = 0
            const length = input.length

            while (position <= length) {
                yield input.substring(position, position + size)
                position += size
            }
        }
        console.log('its a string')
        handlerStream = Readable.from(iterator(handlerBody))
    }

    if (!handlerStream) {
        throw new Error('handler response not a ReadableStream')
    }
    await pipeline(handlerStream, responseStream);
});

I had some trouble setting up ava tests on my computer, so I couldn't create a pull request yet. 😞

from middy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.