tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

`Buffer` response is serialized as a json

Open jeremyVignelles opened this issue 3 years ago • 4 comments

I have an API that returns a buffer, in the same way as it's done here:

https://github.com/lukeautry/tsoa/blob/b3dca8593770fa8d698ed745ed96932b8e8b2d13/tests/fixtures/controllers/getController.ts#L182

However, the response I receive looks like this:

{"type":"Buffer","data":[116,101,115,116,98,117,102,102,101,114]}

Sorting

  • I'm submitting a ...

    • [X] bug report
    • [ ] feature request
    • [ ] support request
  • I confirm that I

    • [X] used the search to make sure that a similar issue hasn't already been submit

Expected Behavior

The return data should read exactly testbuffer

Current Behavior

The buffer is passed though JSON.stringify and the resulting data is JSON.

Possible Workaround

Use the Readable type instead, which doesn't show the issue:

    const readable = new Readable();
    readable._read = () => ({});
    readable.push(Buffer.from('testbuffer'));
    readable.push(null);
    return readable;

Steps to Reproduce

https://github.com/lukeautry/tsoa/blob/b3dca8593770fa8d698ed745ed96932b8e8b2d13/tests/integration/express-server.spec.ts#L170-L181

Test the res.text returned from the HandleBufferType API entrypoint the same way it's done in the HandleStreamType entrypoint.

I didn't test that myself, but that should show the issue.

Context (Environment)

Version of the library: 3.6.1 Version of NodeJS: 16.11.1

  • Confirm you were using yarn not npm: [ ] (Using npm, but probably unrelated)

Breaking change?

The buffer result will no longer return a JSON. If someone is relying on the broken behavior, it will break for them.

jeremyVignelles avatar Nov 08 '21 12:11 jeremyVignelles

Take a look around this:

https://github.com/lukeautry/tsoa/blob/ea976ee5cac9edba0a2fbc9eb8d2729c4237f491/packages/cli/src/routeGeneration/templates/express.hbs#L199

WoH avatar Nov 22 '21 16:11 WoH

@WoH is this still an active issue? If so I'd love to take this up. Any starting guidance would be awesome

satya-nutella avatar Nov 22 '21 20:11 satya-nutella

I guess it's still an issue as I didn't work on it and @WoH pointed to the right direction just today. I would really like to see it solved 🙂

jeremyVignelles avatar Nov 22 '21 20:11 jeremyVignelles

@meehawk I referenced the function that needs to be changed. If the data is a buffer or a typed array, tsoa should not call res.json with the data, but instead res.send (and default to a binary content type, or try to determine it*)

* file-type comes to mind

WoH avatar Dec 28 '21 16:12 WoH