aube-registry 1.36.0

npm registry HTTP client for Aube
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
use super::body::{
    TIMEOUT_RETRY_CAP, check_body_cap, is_retriable_status, read_body_capped,
    read_body_capped_streaming_sha512, retry_after_from,
};
use super::cache::cached_is_fresh;
use super::{RegistryClient, encoded_name};
use crate::{Error, NetworkMode};

impl RegistryClient {
    pub(super) fn registry_url_for(&self, name: &str) -> &str {
        self.config.registry_for(name)
    }

    pub(super) fn force_cache(&self) -> bool {
        matches!(
            self.network_mode,
            NetworkMode::PreferOffline | NetworkMode::Offline
        )
    }

    pub(super) fn trust_cached_packument(
        &self,
        fetched_at: u64,
        max_age_secs: Option<u64>,
    ) -> bool {
        self.force_cache() || cached_is_fresh(fetched_at, max_age_secs)
    }

    /// Build `{registry}/{encoded_name}` — the packument route. Scoped
    /// packages have their `/` encoded as `%2F` so intermediate proxies
    /// that route on path segments (Artifactory's npm remote is the
    /// known offender) don't reject the request with 406. npm-cli and
    /// pnpm encode the same way.
    pub(super) fn packument_url(&self, name: &str) -> (String, &str) {
        let registry_url = self.registry_url_for(name);
        let url = format!(
            "{}/{}",
            registry_url.trim_end_matches('/'),
            encoded_name(name),
        );
        (url, registry_url)
    }

    pub(super) fn authed_get_for_package(
        &self,
        url: &str,
        registry_url: &str,
        package_name: &str,
    ) -> reqwest::RequestBuilder {
        self.authed_request_for_package(reqwest::Method::GET, url, registry_url, package_name)
    }

    /// Build an HTTP request using this registry's configured TLS client
    /// and auth fallback order: bearer token, tokenHelper, then basic auth.
    pub fn authed_request(
        &self,
        method: reqwest::Method,
        url: &str,
        registry_url: &str,
    ) -> reqwest::RequestBuilder {
        self.authed(
            self.http_for(registry_url).request(method, url),
            registry_url,
        )
    }

    pub fn authed_request_for_package(
        &self,
        method: reqwest::Method,
        url: &str,
        registry_url: &str,
        package_name: &str,
    ) -> reqwest::RequestBuilder {
        self.authed_for_package(
            self.http_for_package(registry_url, package_name)
                .request(method, url),
            registry_url,
            package_name,
        )
    }

    /// Build an HTTP request using the TLS/proxy client selected for this
    /// registry, but leave authentication to the caller. Publish uses this
    /// for npm Trusted Publishing exchange tokens so an old `.npmrc` token
    /// cannot be sent alongside the short-lived OIDC-derived bearer token.
    pub fn request(
        &self,
        method: reqwest::Method,
        url: &str,
        registry_url: &str,
    ) -> reqwest::RequestBuilder {
        self.http_for(registry_url).request(method, url)
    }

    pub fn has_resolved_auth_for(&self, registry_url: &str) -> bool {
        self.registry_auth_token_for(registry_url).is_some()
            || self.config.basic_auth_for(registry_url).is_some()
    }

    pub fn has_resolved_auth_for_package(&self, registry_url: &str, package_name: &str) -> bool {
        self.registry_auth_token_for_package(registry_url, Some(package_name))
            .is_some()
            || self
                .config
                .basic_auth_for_package(registry_url, package_name)
                .is_some()
    }

    /// Attach auth headers to any `RequestBuilder` keyed off the registry
    /// that owns `registry_url`. Shared between the GET helpers and the
    /// dist-tag / deprecate PUT calls so every write request picks up the
    /// same token/basic-auth resolution as reads. Future token-type
    /// changes (e.g. web-flow refresh) only have to be made here.
    pub(super) fn authed(
        &self,
        req: reqwest::RequestBuilder,
        registry_url: &str,
    ) -> reqwest::RequestBuilder {
        if let Some(token) = self.registry_auth_token_for(registry_url) {
            req.bearer_auth(token)
        } else if let Some(auth) = self.config.basic_auth_for(registry_url) {
            req.header("Authorization", format!("Basic {auth}"))
        } else {
            req
        }
    }

    fn registry_auth_token_for(&self, registry_url: &str) -> Option<String> {
        self.registry_auth_token_for_package(registry_url, None)
    }

    pub(super) fn authed_for_package(
        &self,
        req: reqwest::RequestBuilder,
        registry_url: &str,
        package_name: &str,
    ) -> reqwest::RequestBuilder {
        if let Some(token) = self.registry_auth_token_for_package(registry_url, Some(package_name))
        {
            req.bearer_auth(token)
        } else if let Some(auth) = self
            .config
            .basic_auth_for_package(registry_url, package_name)
        {
            req.header("Authorization", format!("Basic {auth}"))
        } else {
            req
        }
    }

    fn registry_auth_token_for_package(
        &self,
        registry_url: &str,
        package_name: Option<&str>,
    ) -> Option<String> {
        let cache_key = match package_name {
            Some(name) => format!("{registry_url}\0{name}"),
            None => registry_url.to_string(),
        };
        // Fast path: memoized result. Hit on the second-and-later
        // request to the same registry URL within one process.
        if let Ok(cache) = self.auth_token_by_url.lock()
            && let Some(cached) = cache.get(&cache_key)
        {
            return cached.clone();
        }
        let auth_config = match package_name {
            Some(name) => self.config.registry_config_for_package(registry_url, name),
            None => self.config.registry_config_for(registry_url),
        };
        let resolved = if let Some(auth) = auth_config {
            if let Some(token) = auth.auth_token.as_ref() {
                Some(token.to_string())
            } else if let Some(helper) = auth.token_helper.as_deref() {
                self.cached_token_helper_result(helper)
            } else {
                None
            }
        } else {
            None
        };
        if let Ok(mut cache) = self.auth_token_by_url.lock() {
            cache.insert(cache_key, resolved.clone());
        }
        resolved
    }

    /// Cache key is the helper command itself, not the registry URL:
    /// `run_token_helper` spawns the helper as a subprocess that returns
    /// a token determined entirely by the command, with no URL input.
    /// Keying by URL would defeat the cache for tarball fetches (each
    /// tarball has a unique path) and re-spawn the helper hundreds of
    /// times during a large install.
    fn cached_token_helper_result(&self, helper: &str) -> Option<String> {
        {
            let cache = self.token_helper_cache.lock().ok()?;
            if let Some(token) = cache.get(helper) {
                return token.clone();
            }
        }
        let token = crate::config::run_token_helper(helper);
        if let Ok(mut cache) = self.token_helper_cache.lock() {
            cache.insert(helper.to_string(), token.clone());
        }
        token
    }

    pub(super) fn http_for(&self, registry_url: &str) -> &reqwest::Client {
        let uri_key = crate::config::registry_uri_key_pub(registry_url);
        crate::config::lookup_by_uri_prefix(&self.http_by_uri, &uri_key).unwrap_or(&self.http)
    }

    pub(super) fn http_for_package(
        &self,
        registry_url: &str,
        package_name: &str,
    ) -> &reqwest::Client {
        if let Some((prefix, scope, _)) = self
            .config
            .scoped_tls_config_for_package(registry_url, package_name)
            && let Some(client) = self
                .http_by_uri_scope
                .get(prefix)
                .and_then(|by_scope| by_scope.get(scope))
        {
            return client;
        }
        self.http_for(registry_url)
    }

    /// Pick the right HTTP client for tarball body downloads. The
    /// default registry uses the dedicated h1 client. Per-uri
    /// authed registries (corporate Artifactory, GitHub Packages)
    /// fall through to their h2 client because they're rare and
    /// keeping a parallel h1 map for them is not worth the
    /// complexity until measurement shows it matters.
    pub(super) fn http_tarball_for(&self, registry_url: &str) -> &reqwest::Client {
        let uri_key = crate::config::registry_uri_key_pub(registry_url);
        crate::config::lookup_by_uri_prefix(&self.http_by_uri, &uri_key)
            .unwrap_or(&self.http_tarball)
    }

    pub(super) fn http_tarball_for_package(
        &self,
        registry_url: &str,
        package_name: &str,
    ) -> &reqwest::Client {
        if let Some((prefix, scope, _)) = self
            .config
            .scoped_tls_config_for_package(registry_url, package_name)
            && let Some(client) = self
                .http_by_uri_scope
                .get(prefix)
                .and_then(|by_scope| by_scope.get(scope))
        {
            return client;
        }
        self.http_tarball_for(registry_url)
    }

    /// Authed RequestBuilder routed through the tarball-specific
    /// client. Mirrors [`Self::authed_get`] but picks
    /// [`Self::http_tarball_for`] instead of [`Self::http_for`].
    pub(super) fn authed_tarball_get(
        &self,
        url: &str,
        registry_url: &str,
    ) -> reqwest::RequestBuilder {
        if let Some(package_name) = package_name_from_tarball_url(url) {
            let req = self
                .http_tarball_for_package(registry_url, &package_name)
                .request(reqwest::Method::GET, url);
            self.authed_for_package(req, registry_url, &package_name)
        } else {
            let req = self
                .http_tarball_for(registry_url)
                .request(reqwest::Method::GET, url);
            self.authed(req, registry_url)
        }
    }

    /// Same as [`Self::send_with_retry`] but also returns wall-clock
    /// elapsed from the first `.send()` to the returned response. Used
    /// by metadata call sites to compare against `fetchWarnTimeoutMs`
    /// without double-timing the retry backoff from caller code.
    pub(super) async fn send_with_retry_timed<F>(
        &self,
        build: F,
    ) -> Result<(reqwest::Response, std::time::Duration), reqwest::Error>
    where
        F: Fn() -> reqwest::RequestBuilder,
    {
        let started = std::time::Instant::now();
        let max_attempts = self.fetch_policy.retries.saturating_add(1);
        for attempt in 0..max_attempts {
            let is_last = attempt + 1 >= max_attempts;
            match build().send().await {
                Ok(resp) => {
                    let status = resp.status();
                    // Retry on 5xx server errors and 429 rate-limit.
                    // Everything else — 2xx/3xx successes and 4xx
                    // client errors the caller needs to see (404,
                    // 401, 403) — is returned verbatim.
                    if !is_retriable_status(status) || is_last {
                        return Ok((resp, started.elapsed()));
                    }
                    // 429 may carry a `Retry-After` header; honor it
                    // (seconds form) so a rate-limited registry gets
                    // the wait it asked for instead of our default
                    // exponential backoff. `make-fetch-happen` does
                    // the same. HTTP-date form is rare for npm and
                    // `chrono` isn't a dep — parse as u64 seconds or
                    // fall back to the computed backoff.
                    let wait = retry_after_from(&resp)
                        .unwrap_or_else(|| self.fetch_policy.backoff_for_attempt(attempt + 1));
                    drop(resp);
                    // Surfaces at WARN so users see retry activity in
                    // the install output. The final failure still
                    // propagates up as a user-facing error if every
                    // attempt fails.
                    tracing::warn!(
                        attempt = attempt + 1,
                        max_attempts,
                        backoff_ms = wait.as_millis() as u64,
                        status = status.as_u16(),
                        code = aube_codes::warnings::WARN_AUBE_HTTP_RETRY_TRANSIENT,
                        "retrying HTTP request after transient failure",
                    );
                    tokio::time::sleep(wait).await;
                }
                Err(e) => {
                    if is_last {
                        return Err(e);
                    }
                    let wait = self.fetch_policy.backoff_for_attempt(attempt + 1);
                    tracing::warn!(
                        attempt = attempt + 1,
                        max_attempts,
                        backoff_ms = wait.as_millis() as u64,
                        error = %e,
                        code = aube_codes::warnings::WARN_AUBE_HTTP_RETRY_TRANSPORT,
                        "retrying HTTP request after transport error",
                    );
                    tokio::time::sleep(wait).await;
                }
            }
        }
        // `FetchPolicy::retries` is `u32`, so `max_attempts =
        // retries + 1` is always ≥ 1 and the loop runs at least once;
        // every path inside the loop either returns or continues. An
        // exit past this point is a structural bug, not a runtime
        // input the caller can provoke.
        unreachable!("retry loop exited without returning; max_attempts was {max_attempts}")
    }

    /// Metadata-request wrapper around [`Self::send_with_retry_timed`]
    /// that records a slow-metadata entry when total wall-clock
    /// (including any retry backoff) exceeds `fetchWarnTimeoutMs`. `0`
    /// disables the recording, matching pnpm's convention and the
    /// default in `settings.toml`.
    ///
    /// Per-event detail goes into [`crate::slow_metadata`], not the
    /// log stream — the install pipeline emits one summary warning
    /// after resolve via [`crate::slow_metadata::flush_summary`].
    ///
    /// Not used by tarball downloads — `fetchMinSpeedKiBps` is the
    /// tarball-side observability knob, and the two warnings are
    /// semantically distinct (headers latency vs. body throughput).
    pub(super) async fn send_metadata_with_retry<F>(
        &self,
        label: &str,
        build: F,
    ) -> Result<reqwest::Response, reqwest::Error>
    where
        F: Fn() -> reqwest::RequestBuilder,
    {
        let (resp, elapsed) = self.send_with_retry_timed(build).await?;
        let threshold = self.fetch_policy.warn_timeout_ms;
        let elapsed_ms = elapsed.as_millis() as u64;
        if threshold > 0 && elapsed_ms > threshold {
            crate::slow_metadata::record(label, elapsed_ms, threshold);
        }
        Ok(resp)
    }

    pub(super) fn maybe_record_slow_metadata(&self, label: &str, started: std::time::Instant) {
        let threshold = self.fetch_policy.warn_timeout_ms;
        let elapsed_ms = started.elapsed().as_millis() as u64;
        if threshold > 0 && elapsed_ms > threshold {
            crate::slow_metadata::record(label, elapsed_ms, threshold);
        }
    }

    /// Streaming variant of `retry_bytes_body_read`. Returns the body
    /// bytes along with a SHA-512 digest computed incrementally during
    /// the chunk read loop. Same retry semantics as the buffered path.
    /// Used by `fetch_tarball_bytes_streaming_sha512` so callers can
    /// skip the post-buffer hash pass.
    pub(super) async fn retry_bytes_body_read_streaming_sha512<F>(
        &self,
        label: &str,
        cap: u64,
        build: F,
    ) -> Result<(bytes::Bytes, [u8; 64], std::time::Duration), Error>
    where
        F: Fn() -> reqwest::RequestBuilder,
    {
        let max_attempts = self.fetch_policy.retries.saturating_add(1);
        let mut timeout_retries: u32 = 0;
        for attempt in 0..max_attempts {
            let is_last = attempt + 1 >= max_attempts;
            match build().send().await {
                Ok(resp) if is_retriable_status(resp.status()) && !is_last => {
                    let wait = retry_after_from(&resp)
                        .unwrap_or_else(|| self.fetch_policy.backoff_for_attempt(attempt + 1));
                    tracing::warn!(
                        attempt = attempt + 1,
                        max_attempts,
                        backoff_ms = wait.as_millis() as u64,
                        status = resp.status().as_u16(),
                        label,
                        code = aube_codes::warnings::WARN_AUBE_HTTP_RETRY_TRANSIENT,
                        "retrying HTTP request after transient failure",
                    );
                    tokio::time::sleep(wait).await;
                }
                Ok(resp) => {
                    let resp = resp.error_for_status()?;
                    check_body_cap(&resp, cap, label)?;
                    let started = std::time::Instant::now();
                    match read_body_capped_streaming_sha512(resp, cap, label).await {
                        Ok((bytes, sha512)) => return Ok((bytes, sha512, started.elapsed())),
                        Err(err) if !is_last => {
                            let is_timeout = matches!(&err, Error::Http(e) if e.is_timeout());
                            if is_timeout && timeout_retries >= TIMEOUT_RETRY_CAP {
                                return Err(err);
                            }
                            if is_timeout {
                                timeout_retries += 1;
                            }
                            let wait = self.fetch_policy.backoff_for_attempt(attempt + 1);
                            tracing::warn!(
                                attempt = attempt + 1,
                                max_attempts,
                                backoff_ms = wait.as_millis() as u64,
                                error = %err,
                                label,
                                code = aube_codes::warnings::WARN_AUBE_HTTP_RETRY_BODY_READ,
                                "retrying HTTP request after response body read error",
                            );
                            tokio::time::sleep(wait).await;
                        }
                        Err(err) => return Err(err),
                    }
                }
                Err(err) if !is_last => {
                    if err.is_timeout() {
                        if timeout_retries >= TIMEOUT_RETRY_CAP {
                            return Err(Error::Http(err));
                        }
                        timeout_retries += 1;
                    }
                    let wait = self.fetch_policy.backoff_for_attempt(attempt + 1);
                    tracing::warn!(
                        attempt = attempt + 1,
                        max_attempts,
                        backoff_ms = wait.as_millis() as u64,
                        error = %err,
                        label,
                        code = aube_codes::warnings::WARN_AUBE_HTTP_RETRY_TRANSPORT,
                        "retrying HTTP request after transport error",
                    );
                    tokio::time::sleep(wait).await;
                }
                Err(err) => return Err(Error::Http(err)),
            }
        }
        unreachable!("retry loop exited without returning; max_attempts was {max_attempts}")
    }

    pub(super) async fn retry_bytes_body_read<F>(
        &self,
        label: &str,
        cap: u64,
        build: F,
    ) -> Result<(bytes::Bytes, std::time::Duration), Error>
    where
        F: Fn() -> reqwest::RequestBuilder,
    {
        let max_attempts = self.fetch_policy.retries.saturating_add(1);
        let mut timeout_retries: u32 = 0;
        for attempt in 0..max_attempts {
            let is_last = attempt + 1 >= max_attempts;
            match build().send().await {
                Ok(resp) if is_retriable_status(resp.status()) && !is_last => {
                    let wait = retry_after_from(&resp)
                        .unwrap_or_else(|| self.fetch_policy.backoff_for_attempt(attempt + 1));
                    tracing::warn!(
                        attempt = attempt + 1,
                        max_attempts,
                        backoff_ms = wait.as_millis() as u64,
                        status = resp.status().as_u16(),
                        label,
                        code = aube_codes::warnings::WARN_AUBE_HTTP_RETRY_TRANSIENT,
                        "retrying HTTP request after transient failure",
                    );
                    tokio::time::sleep(wait).await;
                }
                Ok(resp) => {
                    let resp = resp.error_for_status()?;
                    check_body_cap(&resp, cap, label)?;
                    let started = std::time::Instant::now();
                    match read_body_capped(resp, cap, label).await {
                        Ok(bytes) => return Ok((bytes, started.elapsed())),
                        Err(err) if !is_last => {
                            let is_timeout = matches!(&err, Error::Http(e) if e.is_timeout());
                            if is_timeout && timeout_retries >= TIMEOUT_RETRY_CAP {
                                return Err(err);
                            }
                            if is_timeout {
                                timeout_retries += 1;
                            }
                            let wait = self.fetch_policy.backoff_for_attempt(attempt + 1);
                            tracing::warn!(
                                attempt = attempt + 1,
                                max_attempts,
                                backoff_ms = wait.as_millis() as u64,
                                error = %err,
                                label,
                                code = aube_codes::warnings::WARN_AUBE_HTTP_RETRY_BODY_READ,
                                "retrying HTTP request after response body read error",
                            );
                            tokio::time::sleep(wait).await;
                        }
                        Err(err) => return Err(err),
                    }
                }
                Err(err) if !is_last => {
                    if err.is_timeout() {
                        if timeout_retries >= TIMEOUT_RETRY_CAP {
                            return Err(Error::Http(err));
                        }
                        timeout_retries += 1;
                    }
                    let wait = self.fetch_policy.backoff_for_attempt(attempt + 1);
                    tracing::warn!(
                        attempt = attempt + 1,
                        max_attempts,
                        backoff_ms = wait.as_millis() as u64,
                        error = %err,
                        label,
                        code = aube_codes::warnings::WARN_AUBE_HTTP_RETRY_TRANSPORT,
                        "retrying HTTP request after transport error",
                    );
                    tokio::time::sleep(wait).await;
                }
                Err(err) => return Err(Error::Http(err)),
            }
        }
        unreachable!("retry loop exited without returning; max_attempts was {max_attempts}")
    }
}

fn package_name_from_tarball_url(url: &str) -> Option<String> {
    let parsed = reqwest::Url::parse(url).ok()?;
    let segments: Vec<&str> = parsed.path_segments()?.collect();
    if let Some(dash_idx) = segments.iter().position(|segment| *segment == "-")
        && dash_idx >= 1
    {
        let name = segments[dash_idx - 1];
        if let Some((scope, name)) = name.split_once("%2F").or_else(|| name.split_once("%2f"))
            && scope.starts_with('@')
        {
            return Some(format!("{scope}/{name}"));
        }
        if dash_idx >= 2 && segments[dash_idx - 2].starts_with('@') {
            return Some(format!("{}/{}", segments[dash_idx - 2], name));
        }
        return Some(name.to_string());
    }
    for (idx, segment) in segments.iter().enumerate() {
        if let Some((scope, name)) = segment
            .split_once("%2F")
            .or_else(|| segment.split_once("%2f"))
            && scope.starts_with('@')
        {
            return Some(format!("{scope}/{name}"));
        }
        if segment.starts_with('@') {
            let name = segments.get(idx + 1)?;
            return Some(format!("{segment}/{name}"));
        }
    }
    segments.first().map(|segment| (*segment).to_string())
}

#[cfg(test)]
mod tests {
    use super::{RegistryClient, package_name_from_tarball_url};
    use crate::config::{AuthConfig, NpmConfig};

    #[test]
    fn package_name_from_tarball_url_handles_scoped_paths() {
        assert_eq!(
            package_name_from_tarball_url("https://registry.example.com/@org/pkg/-/pkg-1.0.0.tgz")
                .as_deref(),
            Some("@org/pkg")
        );
        assert_eq!(
            package_name_from_tarball_url(
                "https://registry.example.com/@org%2Fpkg/-/pkg-1.0.0.tgz"
            )
            .as_deref(),
            Some("@org/pkg")
        );
        assert_eq!(
            package_name_from_tarball_url(
                "https://registry.example.com/npm/@org/pkg/-/pkg-1.0.0.tgz"
            )
            .as_deref(),
            Some("@org/pkg")
        );
        assert_eq!(
            package_name_from_tarball_url("https://registry.example.com/lodash/-/lodash-1.0.0.tgz")
                .as_deref(),
            Some("lodash")
        );
        assert_eq!(
            package_name_from_tarball_url(
                "https://registry.example.com/npm/lodash/-/lodash-1.0.0.tgz"
            )
            .as_deref(),
            Some("lodash")
        );
    }

    #[test]
    fn http_for_package_uses_scoped_tls_client() {
        let mut config = NpmConfig {
            registry: "https://registry.example.com/".to_string(),
            ..Default::default()
        };
        let mut scoped = AuthConfig::default();
        scoped.tls.cafile = Some(std::path::PathBuf::from("org-a-ca.pem"));
        config
            .scoped_auth_by_uri
            .entry("//registry.example.com/".to_string())
            .or_default()
            .insert("@org-a".to_string(), scoped);
        let client = RegistryClient::from_config(config);

        let default_client = client.http_for("https://registry.example.com/") as *const _;
        let org_client =
            client.http_for_package("https://registry.example.com/", "@org-a/pkg") as *const _;
        let other_client =
            client.http_for_package("https://registry.example.com/", "@org-b/pkg") as *const _;

        assert_ne!(org_client, default_client);
        assert_eq!(other_client, default_client);
    }

    #[test]
    fn tarball_request_uses_scoped_auth_for_path_registry() {
        let mut config = NpmConfig::default();
        let registry_auth = AuthConfig {
            auth_token: Some("registry-token".to_string()),
            ..Default::default()
        };
        config
            .auth_by_uri
            .insert("//registry.example.com/".to_string(), registry_auth);

        let scoped_auth = AuthConfig {
            auth_token: Some("scoped-token".to_string()),
            ..Default::default()
        };
        config
            .scoped_auth_by_uri
            .entry("//registry.example.com/npm".to_string())
            .or_default()
            .insert("@myorg".to_string(), scoped_auth);
        let client = RegistryClient::from_config(config);

        let req = client
            .authed_tarball_get(
                "https://registry.example.com/npm/@myorg/pkg/-/pkg-1.0.0.tgz",
                "https://registry.example.com/npm/@myorg/pkg/-/pkg-1.0.0.tgz",
            )
            .build()
            .unwrap();
        assert_eq!(
            req.headers().get(reqwest::header::AUTHORIZATION),
            Some(&reqwest::header::HeaderValue::from_static(
                "Bearer scoped-token"
            )),
        );

        let req = client
            .authed_tarball_get(
                "https://registry.example.com/npm-release/@myorg/pkg/-/pkg-1.0.0.tgz",
                "https://registry.example.com/npm-release/@myorg/pkg/-/pkg-1.0.0.tgz",
            )
            .build()
            .unwrap();
        assert_eq!(
            req.headers().get(reqwest::header::AUTHORIZATION),
            Some(&reqwest::header::HeaderValue::from_static(
                "Bearer registry-token"
            )),
        );
    }
}