@@ -57,6 +57,10 @@ pub use self::registry_url::RegistryUrl;
57
57
58
58
const DEFAULT_WAIT_INTERVAL : Duration = Duration :: from_secs ( 1 ) ;
59
59
60
+ /// For Bytecode Alliance projects, the default registry is set to `bytecodealliance.org`.
61
+ /// The `.well-known` config path may resolve to another domain where the registry is hosted.
62
+ pub const DEFAULT_REGISTRY : & str = "bytecodealliance.org" ;
63
+
60
64
/// A client for a Warg registry.
61
65
pub struct Client < R , C , N >
62
66
where
@@ -1358,6 +1362,39 @@ pub enum StorageLockResult<T> {
1358
1362
}
1359
1363
1360
1364
impl FileSystemClient {
1365
+ async fn storage_paths (
1366
+ url : Option < & str > ,
1367
+ config : & Config ,
1368
+ disable_interactive : bool ,
1369
+ ) -> Result < StoragePaths , ClientError > {
1370
+ let checking_url_for_well_known = RegistryUrl :: new (
1371
+ url. or ( config. home_url . as_deref ( ) )
1372
+ . unwrap_or ( DEFAULT_REGISTRY ) ,
1373
+ ) ?;
1374
+
1375
+ let url = if let Some ( warg_url) =
1376
+ api:: Client :: new ( checking_url_for_well_known. to_string ( ) , None ) ?
1377
+ . well_known_config ( )
1378
+ . await ?
1379
+ {
1380
+ if !disable_interactive && warg_url != checking_url_for_well_known {
1381
+ println ! (
1382
+ "Resolved `{well_known}` to registry hosted on `{registry}`" ,
1383
+ well_known = checking_url_for_well_known. registry_domain( ) ,
1384
+ registry = warg_url. registry_domain( ) ,
1385
+ ) ;
1386
+ }
1387
+ warg_url
1388
+ } else {
1389
+ RegistryUrl :: new (
1390
+ url. or ( config. home_url . as_deref ( ) )
1391
+ . ok_or ( ClientError :: NoHomeRegistryUrl ) ?,
1392
+ ) ?
1393
+ } ;
1394
+
1395
+ config. storage_paths_for_url ( url)
1396
+ }
1397
+
1361
1398
/// Attempts to create a client for the given registry URL.
1362
1399
///
1363
1400
/// If the URL is `None`, the home registry URL is used; if there is no home registry
@@ -1366,30 +1403,20 @@ impl FileSystemClient {
1366
1403
/// If a lock cannot be acquired for a storage directory, then
1367
1404
/// `NewClientResult::Blocked` is returned with the path to the
1368
1405
/// directory that could not be locked.
1369
- pub fn try_new_with_config (
1370
- url : Option < & str > ,
1406
+ pub async fn try_new_with_config (
1407
+ registry : Option < & str > ,
1371
1408
config : & Config ,
1372
1409
mut auth_token : Option < Secret < String > > ,
1373
1410
) -> Result < StorageLockResult < Self > , ClientError > {
1411
+ let disable_interactive =
1412
+ cfg ! ( not( feature = "cli-interactive" ) ) || config. disable_interactive ;
1413
+
1374
1414
let StoragePaths {
1375
1415
registry_url : url,
1376
1416
registries_dir,
1377
1417
content_dir,
1378
1418
namespace_map_path,
1379
- } = config. storage_paths_for_url ( url) ?;
1380
-
1381
- let ( packages, content, namespace_map) = match (
1382
- FileSystemRegistryStorage :: try_lock ( registries_dir. clone ( ) ) ?,
1383
- FileSystemContentStorage :: try_lock ( content_dir. clone ( ) ) ?,
1384
- FileSystemNamespaceMapStorage :: new ( namespace_map_path. clone ( ) ) ,
1385
- ) {
1386
- ( Some ( packages) , Some ( content) , namespace_map) => ( packages, content, namespace_map) ,
1387
- ( None , _, _) => return Ok ( StorageLockResult :: NotAcquired ( registries_dir) ) ,
1388
- ( _, None , _) => return Ok ( StorageLockResult :: NotAcquired ( content_dir) ) ,
1389
- } ;
1390
-
1391
- let disable_interactive =
1392
- cfg ! ( not( feature = "cli-interactive" ) ) || config. disable_interactive ;
1419
+ } = Self :: storage_paths ( registry, config, disable_interactive) . await ?;
1393
1420
1394
1421
let ( keyring_backend, keys) = if cfg ! ( feature = "keyring" ) {
1395
1422
( config. keyring_backend . clone ( ) , config. keys . clone ( ) )
@@ -1402,6 +1429,16 @@ impl FileSystemClient {
1402
1429
auth_token = crate :: keyring:: Keyring :: from_config ( config) ?. get_auth_token ( & url) ?
1403
1430
}
1404
1431
1432
+ let ( packages, content, namespace_map) = match (
1433
+ FileSystemRegistryStorage :: try_lock ( registries_dir. clone ( ) ) ?,
1434
+ FileSystemContentStorage :: try_lock ( content_dir. clone ( ) ) ?,
1435
+ FileSystemNamespaceMapStorage :: new ( namespace_map_path. clone ( ) ) ,
1436
+ ) {
1437
+ ( Some ( packages) , Some ( content) , namespace_map) => ( packages, content, namespace_map) ,
1438
+ ( None , _, _) => return Ok ( StorageLockResult :: NotAcquired ( registries_dir) ) ,
1439
+ ( _, None , _) => return Ok ( StorageLockResult :: NotAcquired ( content_dir) ) ,
1440
+ } ;
1441
+
1405
1442
Ok ( StorageLockResult :: Acquired ( Self :: new (
1406
1443
url. into_url ( ) ,
1407
1444
packages,
@@ -1427,10 +1464,11 @@ impl FileSystemClient {
1427
1464
///
1428
1465
/// Same as calling `try_new_with_config` with
1429
1466
/// `Config::from_default_file()?.unwrap_or_default()`.
1430
- pub fn try_new_with_default_config (
1467
+ pub async fn try_new_with_default_config (
1431
1468
url : Option < & str > ,
1432
1469
) -> Result < StorageLockResult < Self > , ClientError > {
1433
1470
Self :: try_new_with_config ( url, & Config :: from_default_file ( ) ?. unwrap_or_default ( ) , None )
1471
+ . await
1434
1472
}
1435
1473
1436
1474
/// Creates a client for the given registry URL.
@@ -1439,20 +1477,20 @@ impl FileSystemClient {
1439
1477
/// URL, an error is returned.
1440
1478
///
1441
1479
/// This method blocks if storage locks cannot be acquired.
1442
- pub fn new_with_config (
1443
- url : Option < & str > ,
1480
+ pub async fn new_with_config (
1481
+ registry : Option < & str > ,
1444
1482
config : & Config ,
1445
1483
mut auth_token : Option < Secret < String > > ,
1446
1484
) -> Result < Self , ClientError > {
1485
+ let disable_interactive =
1486
+ cfg ! ( not( feature = "cli-interactive" ) ) || config. disable_interactive ;
1487
+
1447
1488
let StoragePaths {
1448
- registry_url,
1489
+ registry_url : url ,
1449
1490
registries_dir,
1450
1491
content_dir,
1451
1492
namespace_map_path,
1452
- } = config. storage_paths_for_url ( url) ?;
1453
-
1454
- let disable_interactive =
1455
- cfg ! ( not( feature = "cli-interactive" ) ) || config. disable_interactive ;
1493
+ } = Self :: storage_paths ( registry, config, disable_interactive) . await ?;
1456
1494
1457
1495
let ( keyring_backend, keys) = if cfg ! ( feature = "keyring" ) {
1458
1496
( config. keyring_backend . clone ( ) , config. keys . clone ( ) )
@@ -1462,12 +1500,11 @@ impl FileSystemClient {
1462
1500
1463
1501
#[ cfg( feature = "keyring" ) ]
1464
1502
if auth_token. is_none ( ) && config. keyring_auth {
1465
- auth_token =
1466
- crate :: keyring:: Keyring :: from_config ( config) ?. get_auth_token ( & registry_url) ?
1503
+ auth_token = crate :: keyring:: Keyring :: from_config ( config) ?. get_auth_token ( & url) ?
1467
1504
}
1468
1505
1469
1506
Self :: new (
1470
- registry_url . into_url ( ) ,
1507
+ url . into_url ( ) ,
1471
1508
FileSystemRegistryStorage :: lock ( registries_dir) ?,
1472
1509
FileSystemContentStorage :: lock ( content_dir) ?,
1473
1510
FileSystemNamespaceMapStorage :: new ( namespace_map_path) ,
@@ -1489,8 +1526,8 @@ impl FileSystemClient {
1489
1526
///
1490
1527
/// Same as calling `new_with_config` with
1491
1528
/// `Config::from_default_file()?.unwrap_or_default()`.
1492
- pub fn new_with_default_config ( url : Option < & str > ) -> Result < Self , ClientError > {
1493
- Self :: new_with_config ( url, & Config :: from_default_file ( ) ?. unwrap_or_default ( ) , None )
1529
+ pub async fn new_with_default_config ( url : Option < & str > ) -> Result < Self , ClientError > {
1530
+ Self :: new_with_config ( url, & Config :: from_default_file ( ) ?. unwrap_or_default ( ) , None ) . await
1494
1531
}
1495
1532
}
1496
1533
0 commit comments