initializeBoundaries function
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 for $url failed with status: ${response.status}',
);
}
// arrayBuffer(), not bytes(): older Safari and Chrome lack bytes().
final buffer = await response.arrayBuffer().toDart;
final bytes = buffer.toDart.asUint8List();
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());
}
}