Buffered image serves fine in express app but not in lamda function

In the local normal node/express app I create an image using ffmpeg and write it to the response object like this:

      ffmpeg.stdout.on('end', (data) => {
        function getSum(total, chunk) {
            return total + chunk.length;
        }
        const length = chunks.reduce(getSum, 0)
        const buffer = Buffer.concat(chunks, length);
        res.end(buffer)
      })

And it works perfectly :slight_smile: - the browser window is filled with my image.

Since there is no response object as such in serverless I assume I must just put it in the callback like this:

  ffmpeg.stdout.on('end', (data) => {
    function getSum(total, chunk) {
        return total + chunk.length;
    }
    const length = chunks.reduce(getSum, 0)
    const buffer = Buffer.concat(chunks, length);
    console.log("BUFFER", buffer)
    const response = {
      statusCode: 200,
      body: buffer  // "<h1>Hello</h1>"
    };

    callback(null, buffer);
  })

However, while it doesn’t crash when I run it in the terminal (in the browser I get {"message": "Internal server error"}) - the output seems to be empty:

Kjetils-Mac-mini:thumbs kjetilge$ sls invoke -f thumb
{
    "type": "Buffer",
    "data": []
}

How can this be ? Suggestion are welcome!

Update:
I’m now using SAM and getting better debugging in general.
When I now put the feed the buffer til callback I get this:

2018/03/18 00:47:28 Mounting /Users/kjetilge/Documents/GitHub/thumbs as /var/task:ro inside runtime container
START RequestId: ce4f93ad-828e-1f2f-0274-dd2ec4ea64b5 Version: $LATEST
2018-03-17T23:47:37.087Z	ce4f93ad-828e-1f2f-0274-dd2ec4ea64b5	length 82784
2018-03-17T23:47:37.090Z	ce4f93ad-828e-1f2f-0274-dd2ec4ea64b5	
My BUFFER:
 <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff fe 00 10 4c 61 76 63 35 37 2e 39 36 2e 31 30 31 00 ff db 00 43 00 08 0e 0e 10 0e 10 13 ... >
END RequestId: ce4f93ad-828e-1f2f-0274-dd2ec4ea64b5
REPORT RequestId: ce4f93ad-828e-1f2f-0274-dd2ec4ea64b5	Duration: 2460.49 ms	Billed Duration: 2500 ms	Memory Size: 128 MB	Max Memory Used: 32 MB	
2018/03/18 00:47:31 Function returned an invalid response (must include one of: body, headers or statusCode in the response object): %!s(<nil>)

At least the buffer isn’t empty anymore, but it still don’t work…

So I tried something new: using the buffer as an image source:

  ffmpeg.stdout.on('end', (data) => {
    function getSum(total, chunk) {
        return total + chunk.length;
    }
    const length = chunks.reduce(getSum, 0)
    console.log("length", length)
    const buffer = Buffer.concat(chunks, length);
    console.log("\nMy BUFFER:\n", buffer)
    const response = {
      statusCode: 200,
      encoding: null,
      body: "<h1>Hello <img src=" +buffer+ "</h1>"
    };

    callback(null, response);
  })

Now I get something interesting like this:

Hello <img src=�����JFIF���������Lavc57.96.101����C� ! !!###**((112<<H�����������������a���������a���!1QA2q�"aR�B����#3��4r$�bC�����1!A2aQ"q�3�#�B�����8a�"��������?��U+�h�����/�=~��m����گ/ɡ����/�E�e�Q��@qF�r���<��:"aD99Կ���lH����OO�oD�Fe���A�_W�j�eN�_@�|ݙ�N�|P� $R'�����!�^Ha�_BP]JG�)�TU-&��྄M/�������J/���ax ,Xu#u��zx#Z �i��� �Ī�,S;��п]����!����������W�_���a���8�j������ܶ�c�����eh�n,����3f�]T�3[�2�-fE�v�c4�4!tkR��)�풻�X������-`�����W��(X�*x T�)���P��"�qUy"�5�!

… and a lot more of the same…
I guess someone knows what this is ?

nobody has response of this problem?
I cant solve this is stuck!

I have the same problem