initializeBoundaries function

Future<void> initializeBoundaries([
  1. String url = boundariesDefaultUrl
])

Fetches url then installBoundaries. Prefer bytes when you already have them.

Defaults to boundariesDefaultUrl. Wrong length (often an HTML 404) throws BoundariesInitException; well-sized but corrupt bytes throw IndexFormatException.

Implementation

Future<void> initializeBoundaries([String url = boundariesDefaultUrl]) async {
  try {
    final response = await window.fetch(url.toJS).toDart;
    if (!response.ok) {
      throw BoundariesInitException(
        'Request failed with status: ${response.status}',
      );
    }
    final jsBytes = await response.bytes().toDart;
    final bytes = Uint8List.fromList(jsBytes.toDart);
    if (bytes.length != boundariesBinLength) {
      throw BoundariesInitException(
        'Fetched ${bytes.length} bytes from $url, expected '
        '$boundariesBinLength ($boundariesBinName)',
      );
    }
    installBoundaries(bytes);
  } on BoundariesInitException {
    rethrow;
  } on IndexFormatException {
    rethrow;
  } catch (e) {
    throw BoundariesInitException(e.toString());
  }
}