Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WorkerGlobalScope.fetch doesn't serialize TypedArrays properly #48938

Open
jbrownsw opened this issue May 2, 2022 · 0 comments
Open

WorkerGlobalScope.fetch doesn't serialize TypedArrays properly #48938

jbrownsw opened this issue May 2, 2022 · 0 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. library-html web-libraries Issues impacting dart:html, etc., libraries

Comments

@jbrownsw
Copy link

jbrownsw commented May 2, 2022

  • Dart SDK version: 2.16.2
  • Platform: Windows 11
  • Browser: Chrome 100.0.4896.127

I was attempting to implement a fetch based transport for the grpc-dart package as opposed to xhr due to the following issue:
grpc/grpc-dart#511

My initial implementation was calling WorkerGlobalScope.fetch, but I could never get the body right. I have a Uint8List that I wanted in the body which should just map to a javascript Uint8Array, but instead it would convert it to just a comma separated string of ints. I suspect it has to do with convertDartToNative_Dictionary which fetch calls internally.
https://github.com/jbrownsw/grpc-dart/blob/26b5dbb8dbbaccea04bca7cc866a4035a1b503d3/lib/src/client/transport/fetch_transport.dart

My workaround was just to define the interop init type here and instead of calling wgs.fetch I use callMethod:
jbrownsw/grpc-dart@e627db5

A minimal(ish) sample would be something like the following:

// ignore_for_file: avoid_web_libraries_in_flutter
import 'dart:html';
import 'dart:typed_data';
import 'dart:js_util' as js_util;

void main() async {
  final data = Uint8List.fromList([
    // Don't format me
    0, 0, 0, 0, 65, 10, 59, 81, 117, 103, 110, 87, 117, 50, 67, 49, 114, 50,
    73, 88, 57, 107, 54, 56, 118, 106, 65, 73, 65, 58, 52, 100, 101, 48, 55,
    48, 55, 50, 45, 97, 51, 54, 54, 45, 52, 49, 51, 48, 45, 98, 101, 98, 54,
    45, 97, 55, 50, 55, 98, 101, 55, 55, 51, 100, 102, 54, 16, 128, 130, 104
  ]);
  final headers = <String, String>{
    'Content-Type': 'application/grpc-web+proto',
    'X-User-Agent': 'grpc-web-dart/0.1',
    'X-Grpc-Web': '1',
  };

  final init = <String, dynamic>{
    'cache': 'default',
    'credentials': 'omit',
    'integrity': '',
    'keepalive': true,
    'method': 'POST',
    'mode': 'cors',
    'redirect': 'follow',
    'referrerPolicy': 'origin',
    'body': data,
    'headers': js_util.jsify(headers),
    'data': data,
  };

  final wgs = WorkerGlobalScope.instance;
  final response = await wgs.fetch('https://reqbin.com/echo/post/json', init);
}

I'm then using chrome dev tools to monitor the network traffic and I'm expecting to see a 70 byte message being sent. However, instead I see a content-length 222 message because it converts data to a comma separated list of ints (essentially just toString()).

@lrhn lrhn added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label May 3, 2022
@rileyporter rileyporter added web-libraries Issues impacting dart:html, etc., libraries library-html labels May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. library-html web-libraries Issues impacting dart:html, etc., libraries
Projects
None yet
Development

No branches or pull requests

3 participants