Closed thekid closed 1 year ago
https://docs.aws.amazon.com/lambda/latest/dg/response-streaming-tutorial.html#response-streaming-tutorial-create-function-cfn shows how HTTP headers can be returned:
exports.handler = awslambda.streamifyResponse(
async (event, responseStream, _context) => {
// Metadata is a JSON serializable JS object. Its shape is not defined here.
const metadata = {
statusCode: 200,
headers: {
"Content-Type": "application/json",
"CustomHeader": "outerspace"
}
};
// Assign to the responseStream parameter to prevent accidental reuse of the non-wrapped stream.
responseStream = awslambda.HttpResponseStream.from(responseStream, metadata);
responseStream.write("...");
responseStream.end();
}
);
This seems to be implemented by using a special content type, packing the meta data as JSON and delimiting it from the rest of the payload with 8 NUL bytes.
Content-Type: application/vnd.awslambda.http-integration-response
{"statusCode":200,"headers:{"Content-Type":"application/json","CustomHeader":"outerspace"}}
\0\0\0\0\0\0\0\0
...
See https://gist.github.com/serg06/0653a4c690a7f5854f8038e3ebe62a64
use com\amazon\aws\lambda\Handler;
use text\json\{Json, StreamOutput};
class Web extends Handler {
public function target(): callable {
return function($event, $stream, $context) {
$stream->use('application/vnd.awslambda.http-integration-response');
$stream->write('{"statusCode":200,"headers":{"Content-Type":"application/json"}}');
$stream->write("\x00\x00\x00\x00\x00\x00\x00\x00");
Json::write(['event' => $event], new StreamOutput($stream));
};
}
}
✅ Verified xp-forge/lambda-ws
is fully compatible with these changes.
However, changing the invoke mode on existing lambdas built with this library will yield incorrect results, displaying the meta information instead of using it for the request:
Mode BUFFERED:
Mode RESPONSE_STREAM:
This is nothing special to the XP Framework, it will happen with any NodeJS lambda, too!
As there seems to be no way to detect the invoke mode at runtime except for using the AWS lambda management APIs (which would be quite slow!), and because the other way around (a function using streaming on the inside but configured to use BUFFERED mode) will not breaks, our path forward should most probably be the following:
xp-forge/lambda-ws
to make use of streaming
This pull request implements feature request #22, AWS Lambda response streaming as announced by AWS in April 2023. Its documentation states:
To use the stream, return a
function(var, Stream, Context)
from the handler's target() method instead of afunction(var, Context)
.Example code
The transmit() method can be used to stream from input streams, e.g. from a file, or a socket. Response stream payloads have a soft limit of 20 MB as compared to the 6 MB limit for buffered responses.
Stream API
Deployment
Invocation
TODO
xp lambda run