abner / flutter_js

A Javascript engine to use with flutter. It uses quickjs on Android and JavascriptCore on IOS
MIT License
464 stars 117 forks source link

Allow xhr to return raw response without UTF-8 decoding in flutter_js #137

Open zhouxiaofu opened 1 year ago

zhouxiaofu commented 1 year ago

Description:

Hello maintainers,

I'm currently using flutter_js for my project and I've encountered a scenario where the default UTF-8 decoding of the xhr response isn't suitable for my needs. Specifically, some of the content I'm fetching might be in GBK encoding, and I'm handling the conversion using iconv on the JS side.

Current behavior:

String responseText = utf8.decode(response.bodyBytes);

This automatic decoding poses a challenge when dealing with non-UTF-8 encoded content.

Error:

E/flutter (21984): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Missing extension byte (at offset 102)
E/flutter (21984): #0      _Utf8Decoder.convertSingle (dart:convert-patch/convert_patch.dart:1747:7)
E/flutter (21984): #1      Utf8Decoder.convert (dart:convert/utf.dart:351:42)
E/flutter (21984): #2      Utf8Codec.decode (dart:convert/utf.dart:63:20)
E/flutter (21984): #3      JavascriptRuntimeXhrExtension.enableXhr.<anonymous closure>.<anonymous closure> (package:flutter_js/extensions/xhr.dart:326:36)
E/flutter (21984): <asynchronous suspension>

Feature Request:

It would be great if there was an option or configuration to get the raw response from xhr, without any decoding, allowing the JS to handle the decoding (if necessary) based on the content type or other factors.

Use Case:

In my scenario, I'm dealing with content that could be either UTF-8 or GBK encoded. Having the raw bytes allows me to use libraries like iconv in the JS environment to handle decoding correctly based on the actual encoding of the content.

Thank you for considering this request. I believe it would make flutter_js more flexible in handling various content types and encodings.

abner commented 1 year ago

I will take a look on it