downloadPrecompiledRustLib function

Future<String?> downloadPrecompiledRustLib({
  1. String? manifestDir,
  2. String? cacheDir,
})

Downloads a verified release cdylib into cacheDir and returns its path.

Returns null when precompiled binaries are not configured, not published for the current crate hash / platform, or signature verification fails.

Implementation

Future<String?> downloadPrecompiledRustLib({
  String? manifestDir,
  String? cacheDir,
}) async {
  final manifest = manifestDir ?? rustManifestDir();
  final config = _loadPrecompiledConfig(manifest);
  if (config == null) return null;

  final crateHash = _computeCrateHash(manifest);
  final triple = hostRustTriple();
  final libraryFile = precompiledRustLibFileName();
  final remoteName = precompiledRemoteFileName(triple, libraryFile);
  final signatureName = '$remoteName.sig';

  final store = cacheDir ??
      p.join(
        Directory.systemTemp.path,
        'rust_lib_flutter_alacritty_precompiled',
        crateHash,
      );
  Directory(store).createSync(recursive: true);
  final localPath = p.join(store, libraryFile);
  if (File(localPath).existsSync()) {
    return localPath;
  }

  final prefix = config.urlPrefix;
  final binaryUrl = Uri.parse('$prefix$crateHash/$remoteName');
  final signatureUrl = Uri.parse('$prefix$crateHash/$signatureName');

  final signatureResponse = await http.get(signatureUrl);
  if (signatureResponse.statusCode != 200) {
    return null;
  }

  final binaryResponse = await http.get(binaryUrl);
  if (binaryResponse.statusCode != 200) {
    return null;
  }

  if (!verify(
    config.publicKey,
    binaryResponse.bodyBytes,
    signatureResponse.bodyBytes,
  )) {
    return null;
  }

  await File(localPath).writeAsBytes(binaryResponse.bodyBytes);
  return localPath;
}