serverless / event-gateway

React to any event with serverless functions across clouds
https://www.serverless.com/event-gateway
Apache License 2.0
1.65k stars 98 forks source link

Binary support for HTTP response #289

Open mthenw opened 6 years ago

mthenw commented 6 years ago

Current implementation support HTTP responses via special JSON object schema. Right now only string is supported as a response body. EG needs to support also binary responses. Requirements:

Possible solution isBinary flag and base64 encoding

Right now the supported object looks like this:

{
    "statusCode": 200,
    "headers": {
      "content-type": "text/html"
    },
    "body": "<hello/>"
}

The proposition is to support additional flag isBinary that will indicate that body is base64 encoded string that carries a binary payload. EG will decode it and return a binary response to the requester. We already pass a Base64 string to a function in case of binary input data so this solution is "symmetrical".

{
    "statusCode": 200,
    "headers": {
      "content-type": "text/html"
    },
    "body": "<base64 string>",
    "isBinary": true
}

Node.js Example

exports.image = (event, context, callback) => {
  fs.readFile("./image.jpg", { encoding: "base64" }, function(err, data) {
    callback(null, {
      statusCode: 200,
      headers: {
        "content-type": "image/jpeg"
      },
      isBinary: true,
      body: data
    });
  });
});

Python Example

def main(event, context):
    with open('./image.jpg', 'rb') as f:
        body = f.read().encode('base64')

    return {
        "statusCode": 200,
        "headers": {
            "content-type": "image/jpeg"
        },
        "isBinary": True,
        "body": body
   }

cc @brianneisler @DavidWells @nikgraf

xtuc commented 6 years ago

Instead of using a binary representation over HTTP I would use end to end binary stream.

mthenw commented 6 years ago

@xtuc the goal is to allow function (e.g. AWS Lambda) returning binary files. Also, it has to be easy to achieve with standard lib across different languages. Not sure if your proposed solutions accomplish that?

xtuc commented 6 years ago

Ok, sorry I didn't know it was to communicate with lambdas.

As I said, bson is really a simple packing technique, it should be implementable in every languages.

mthenw commented 6 years ago

@xtuc the problem is that it requires external libraries. I know that base64 is not perfect but at least it's "universal". In the future, we may support compression between EG <> FaaS.

mthenw commented 6 years ago

Optionally, instead of adding another field in the response object we can just detect binary payload based on returned content type.