pulyaevskiy / firebase-functions-interop

Firebase Functions Interop Library for Dart.
BSD 3-Clause "New" or "Revised" License
191 stars 52 forks source link

Unable to send/receive binary data (e.g. Buffer, Uint8List) in HTTPS function #29

Closed paulreimer closed 5 years ago

paulreimer commented 5 years ago

This works with node_interop and Google Cloud Function (binary data is returned in the response):

Uint8List responseBytes = ...;
res
  ..statusCode = 200 // OK
  ..write(Buffer.from(responseBytes))
  ..end();

But with firebase-functions-interop it returns the toString representation of a List<int> of the binary data:

Uint8List responseBytes = ...;
request.response
  ..statusCode = 200 // OK
  ..write(Buffer.from(responseBytes))
  ..close();

Produces the output:

[24, 0, 0, 0, 65, 67, 77, 46, 16,...]
pulyaevskiy commented 5 years ago

The ‘write’ method is supposed to only write string values. Everything that is not a ‘String’, gets converted with ‘toString()’ call. This contract comes from the Dart’s IOSink class, actually, node_io simply tries to follow it. Docs: https://api.dartlang.org/dev/2.0.0-dev.63.0/dart-io/IOSink/write.html

To add byte data there is ‘add’ method.

Hope this helps.

pulyaevskiy commented 5 years ago

Oh, the first example shouldn’t work either. This is strange since firebase functions use the same node_io implementation.

paulreimer commented 5 years ago

Well, oddly the first example does work! Maybe the Buffer.from(...) is affecting things? But using add also works in both cases (so it does resolve this issue for me!).

In fact I no longer need the package:node_interop:buffer.dart dependency and I can add my Uint8List bytes directly.

Thanks so much for the help!

pulyaevskiy commented 5 years ago

Glad it works for you now.

Wondering if you can provide a more complete version of the first example? Would be nice to figure out what's going on there.