decompressBytes function

Future<Uint8List> decompressBytes(
  1. Uint8List input, {
  2. int maxDecompressedBytes = _defaultMaxDecompressedBytes,
})

Decompresses a GZip compressed byte array into a byte array.

Implementation

Future<Uint8List> decompressBytes(
  Uint8List input, {
  int maxDecompressedBytes = _defaultMaxDecompressedBytes,
}) async {
  if (maxDecompressedBytes < 0) {
    throw FormatException(
      'GZip decompressed size exceeds limit of $maxDecompressedBytes bytes.',
    );
  }
  final blob = web.Blob([input.toJS].toJS);
  final response = web.Response(blob);

  final decompressionStream = DecompressionStream('gzip');
  final decompressedStream = response.body!.pipeThrough(decompressionStream);

  final decompressedResponse = web.Response(decompressedStream);
  final arrayBuffer = await decompressedResponse.arrayBuffer().toDart;
  final bytes = arrayBuffer.toDart.asUint8List();
  if (bytes.length > maxDecompressedBytes) {
    throw FormatException(
      'GZip decompressed size exceeds limit of $maxDecompressedBytes bytes.',
    );
  }
  return bytes;
}