ngdp-cache 0.4.3

Transparent caching layer with TTL support for all NGDP operations
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
//! Cached wrapper for TACT HTTP client
//!
//! This module provides a caching layer for TactClient that stores responses
//! in BPSV format with sequence number tracking similar to Ribbit.
//!
//! **Important**: This caches TACT protocol metadata responses (versions, CDN configs, BGDL),
//! NOT actual game content files. The TACT protocol provides:
//! - Version information about available game builds
//! - CDN server configuration (which servers host the content)
//! - Background download settings
//!
//! For caching actual CDN content files, a separate caching layer should be
//! implemented for the ngdp-cdn crate.
//!
//! # Example
//!
//! ```no_run
//! use ngdp_cache::cached_tact_client::CachedTactClient;
//! use tact_client::{Region, ProtocolVersion};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a cached client
//! let client = CachedTactClient::new(Region::US, ProtocolVersion::V1).await?;
//!
//! // Use it exactly like TactClient - caching is transparent
//! let versions = client.get_versions_parsed("wow").await?;
//! println!("Found {} versions", versions.len());
//!
//! // Subsequent calls use cache based on sequence numbers
//! let versions2 = client.get_versions_parsed("wow").await?;  // This will be from cache!
//! # Ok(())
//! # }
//! ```

use std::path::PathBuf;
use std::time::Duration;
use tracing::{debug, trace};

use tact_client::{CdnEntry, HttpClient, ProtocolVersion, Region, VersionEntry};

use crate::{Result, ensure_dir, get_cache_dir};

/// Default TTL for version responses (5 minutes)
const VERSIONS_CACHE_TTL: Duration = Duration::from_secs(5 * 60);

/// Default TTL for CDN and BGDL responses (30 minutes)
const CDN_CACHE_TTL: Duration = Duration::from_secs(30 * 60);

/// Metadata for cached responses
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct CacheMetadata {
    /// Unix timestamp when cached
    timestamp: u64,
    /// TTL in seconds
    ttl_seconds: u64,
    /// Region
    region: String,
    /// Protocol version (v1 or v2)
    protocol: String,
    /// Product name
    product: String,
    /// Endpoint type (versions, cdns, bgdl)
    endpoint: String,
    /// Sequence number from response
    sequence: Option<u64>,
    /// Response size in bytes
    response_size: usize,
}

/// Endpoint types for TACT protocol
#[derive(Debug, Clone, Copy, PartialEq)]
enum TactEndpoint {
    Versions,
    Cdns,
    Bgdl,
}

impl TactEndpoint {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Versions => "versions",
            Self::Cdns => "cdns",
            Self::Bgdl => "bgdl",
        }
    }

    fn ttl(&self) -> Duration {
        match self {
            Self::Versions => VERSIONS_CACHE_TTL,
            Self::Cdns | Self::Bgdl => CDN_CACHE_TTL,
        }
    }
}

/// A caching wrapper around TactClient
pub struct CachedTactClient {
    /// The underlying TACT HTTP client
    client: HttpClient,
    /// Base directory for cache
    cache_dir: PathBuf,
    /// Whether caching is enabled
    enabled: bool,
}

impl CachedTactClient {
    /// Create a new cached TACT client
    pub async fn new(region: Region, protocol: ProtocolVersion) -> Result<Self> {
        let client = HttpClient::new(region, protocol)?;
        let cache_dir = get_cache_dir()?.join("tact");
        ensure_dir(&cache_dir).await?;

        debug!(
            "Initialized cached TACT client for region {:?}, protocol {:?}",
            region, protocol
        );

        Ok(Self {
            client,
            cache_dir,
            enabled: true,
        })
    }

    /// Create a new cached client with custom cache directory
    pub async fn with_cache_dir(
        region: Region,
        protocol: ProtocolVersion,
        cache_dir: PathBuf,
    ) -> Result<Self> {
        let client = HttpClient::new(region, protocol)?;
        ensure_dir(&cache_dir).await?;

        Ok(Self {
            client,
            cache_dir,
            enabled: true,
        })
    }

    /// Create from an existing HTTP client
    pub async fn with_client(client: HttpClient) -> Result<Self> {
        let cache_dir = get_cache_dir()?.join("tact");
        ensure_dir(&cache_dir).await?;

        Ok(Self {
            client,
            cache_dir,
            enabled: true,
        })
    }

    /// Enable or disable caching
    pub fn set_caching_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }

    /// Get the cache path for an endpoint
    fn get_cache_path(
        &self,
        product: &str,
        endpoint: TactEndpoint,
        sequence: Option<u64>,
    ) -> PathBuf {
        let region = self.client.region().to_string();
        let protocol = match self.client.version() {
            ProtocolVersion::V1 => "v1",
            ProtocolVersion::V2 => "v2",
        };

        let seq = sequence.unwrap_or(0);
        let filename = format!("{}-{}.bpsv", endpoint.as_str(), seq);

        self.cache_dir
            .join(region)
            .join(protocol)
            .join(product)
            .join(filename)
    }

    /// Get the metadata path for an endpoint
    fn get_metadata_path(
        &self,
        product: &str,
        endpoint: TactEndpoint,
        sequence: Option<u64>,
    ) -> PathBuf {
        let mut path = self.get_cache_path(product, endpoint, sequence);
        path.set_extension("meta");
        path
    }

    /// Extract sequence number from TACT response data
    fn extract_sequence_number(&self, data: &str) -> Option<u64> {
        // Look for "## seqn = 12345" pattern
        for line in data.lines() {
            if line.starts_with("## seqn = ") {
                if let Some(seqn_str) = line.strip_prefix("## seqn = ") {
                    if let Ok(seqn) = seqn_str.trim().parse::<u64>() {
                        return Some(seqn);
                    }
                }
            }
        }
        None
    }

    /// Find the most recent valid cached file for an endpoint
    async fn find_cached_file(
        &self,
        product: &str,
        endpoint: TactEndpoint,
    ) -> Option<(PathBuf, u64)> {
        if !self.enabled {
            return None;
        }

        let region = self.client.region().to_string();
        let protocol = match self.client.version() {
            ProtocolVersion::V1 => "v1",
            ProtocolVersion::V2 => "v2",
        };

        let cache_subdir = self.cache_dir.join(&region).join(protocol).join(product);
        if tokio::fs::metadata(&cache_subdir).await.is_err() {
            return None;
        }

        let prefix = format!("{}-", endpoint.as_str());
        let ttl = endpoint.ttl();
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let mut best_file: Option<(PathBuf, u64)> = None;
        let mut best_seqn: u64 = 0;

        // Read directory and find matching files
        if let Ok(mut entries) = tokio::fs::read_dir(&cache_subdir).await {
            while let Some(entry) = entries.next_entry().await.ok()? {
                let path = entry.path();
                if let Some(filename) = path.file_name().and_then(|n| n.to_str()) {
                    // Check if this file matches our endpoint pattern
                    if filename.starts_with(&prefix) && filename.ends_with(".bpsv") {
                        // Extract sequence number from filename
                        if let Some(seqn_part) = filename
                            .strip_prefix(&prefix)
                            .and_then(|s| s.strip_suffix(".bpsv"))
                        {
                            if let Ok(seqn) = seqn_part.parse::<u64>() {
                                // Check if this file is still valid
                                let meta_path = path.with_extension("meta");
                                if let Ok(metadata_str) =
                                    tokio::fs::read_to_string(&meta_path).await
                                {
                                    if let Ok(metadata) =
                                        serde_json::from_str::<CacheMetadata>(&metadata_str)
                                    {
                                        if now.saturating_sub(metadata.timestamp) < ttl.as_secs()
                                            && seqn > best_seqn
                                        {
                                            best_file = Some((path.clone(), seqn));
                                            best_seqn = seqn;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        best_file
    }

    /// Write response to cache
    async fn write_to_cache(
        &self,
        product: &str,
        endpoint: TactEndpoint,
        data: &str,
    ) -> Result<()> {
        if !self.enabled {
            return Ok(());
        }

        // Extract sequence number from the response data
        let sequence = self.extract_sequence_number(data);

        let cache_path = self.get_cache_path(product, endpoint, sequence);
        let meta_path = self.get_metadata_path(product, endpoint, sequence);

        // Ensure parent directory exists
        if let Some(parent) = cache_path.parent() {
            ensure_dir(parent).await?;
        }

        // Write the response data
        trace!(
            "Writing {} bytes to TACT cache: {:?}",
            data.len(),
            cache_path
        );
        tokio::fs::write(&cache_path, data).await?;

        // Create and write metadata
        let metadata = CacheMetadata {
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            ttl_seconds: endpoint.ttl().as_secs(),
            region: self.client.region().to_string(),
            protocol: match self.client.version() {
                ProtocolVersion::V1 => "v1".to_string(),
                ProtocolVersion::V2 => "v2".to_string(),
            },
            product: product.to_string(),
            endpoint: endpoint.as_str().to_string(),
            sequence,
            response_size: data.len(),
        };

        let metadata_json = serde_json::to_string_pretty(&metadata)?;
        tokio::fs::write(&meta_path, metadata_json).await?;

        Ok(())
    }

    /// Read response from cache
    async fn read_from_cache(&self, product: &str, endpoint: TactEndpoint) -> Result<String> {
        if let Some((cache_path, _seqn)) = self.find_cached_file(product, endpoint).await {
            trace!("Reading from TACT cache: {:?}", cache_path);
            Ok(tokio::fs::read_to_string(&cache_path).await?)
        } else {
            Err(crate::Error::CacheEntryNotFound(format!(
                "No valid cache for {}/{}/{}",
                self.client.region(),
                product,
                endpoint.as_str()
            )))
        }
    }

    /// Get versions with caching
    pub async fn get_versions(&self, product: &str) -> Result<reqwest::Response> {
        // For raw Response objects, we need to use the underlying client directly
        // as we can't reconstruct Response from cached data
        Ok(self.client.get_versions(product).await?)
    }

    /// Get versions with parsed response and caching
    pub async fn get_versions_parsed(&self, product: &str) -> Result<Vec<VersionEntry>> {
        let endpoint = TactEndpoint::Versions;

        // Check cache first
        if self.enabled {
            if let Ok(cached_data) = self.read_from_cache(product, endpoint).await {
                debug!("Cache hit for TACT {}/{}", product, endpoint.as_str());
                // Parse the cached data
                return Ok(tact_client::parse_versions(&cached_data)?);
            }
        }

        // Cache miss - fetch from server
        debug!(
            "Cache miss for TACT {}/{}, fetching from server",
            product,
            endpoint.as_str()
        );
        let response = self.client.get_versions(product).await?;
        let text = response.text().await?;

        // Cache the response
        if let Err(e) = self.write_to_cache(product, endpoint, &text).await {
            debug!("Failed to write to TACT cache: {}", e);
        }

        // Parse and return
        Ok(tact_client::parse_versions(&text)?)
    }

    /// Get CDN configuration with caching
    ///
    /// **Important**: This returns CDN server configuration from the TACT `/cdns` endpoint,
    /// NOT actual CDN content. The TACT protocol has three metadata endpoints:
    /// - `/versions` - game version information
    /// - `/cdns` - CDN server configuration (which CDN servers to use)
    /// - `/bgdl` - background download configuration
    ///
    /// For actual CDN content caching, use the ngdp-cdn crate with its own caching layer.
    pub async fn get_cdns(&self, product: &str) -> Result<reqwest::Response> {
        // For raw Response objects, we need to use the underlying client directly
        Ok(self.client.get_cdns(product).await?)
    }

    /// Get CDN configuration with parsed response and caching
    ///
    /// **Important**: This returns CDN server configuration, NOT actual CDN content.
    /// See `get_cdns()` documentation for details.
    pub async fn get_cdns_parsed(&self, product: &str) -> Result<Vec<CdnEntry>> {
        let endpoint = TactEndpoint::Cdns;

        // Check cache first
        if self.enabled {
            if let Ok(cached_data) = self.read_from_cache(product, endpoint).await {
                debug!("Cache hit for TACT {}/{}", product, endpoint.as_str());
                // Parse the cached data
                return Ok(tact_client::parse_cdns(&cached_data)?);
            }
        }

        // Cache miss - fetch from server
        debug!(
            "Cache miss for TACT {}/{}, fetching from server",
            product,
            endpoint.as_str()
        );
        let response = self.client.get_cdns(product).await?;
        let text = response.text().await?;

        // Cache the response
        if let Err(e) = self.write_to_cache(product, endpoint, &text).await {
            debug!("Failed to write to TACT cache: {}", e);
        }

        // Parse and return
        Ok(tact_client::parse_cdns(&text)?)
    }

    /// Get BGDL with caching
    pub async fn get_bgdl(&self, product: &str) -> Result<reqwest::Response> {
        // For raw Response objects, we need to use the underlying client directly
        Ok(self.client.get_bgdl(product).await?)
    }

    /// Get BGDL with parsed response and caching
    pub async fn get_bgdl_parsed(
        &self,
        product: &str,
    ) -> Result<Vec<tact_client::response_types::BgdlEntry>> {
        let endpoint = TactEndpoint::Bgdl;

        // Check cache first
        if self.enabled {
            if let Ok(cached_data) = self.read_from_cache(product, endpoint).await {
                debug!("Cache hit for TACT {}/{}", product, endpoint.as_str());
                // Parse the cached data
                return Ok(tact_client::response_types::parse_bgdl(&cached_data)?);
            }
        }

        // Cache miss - fetch from server
        debug!(
            "Cache miss for TACT {}/{}, fetching from server",
            product,
            endpoint.as_str()
        );
        let response = self.client.get_bgdl(product).await?;
        let text = response.text().await?;

        // Cache the response
        if let Err(e) = self.write_to_cache(product, endpoint, &text).await {
            debug!("Failed to write to TACT cache: {}", e);
        }

        // Parse and return
        Ok(tact_client::response_types::parse_bgdl(&text)?)
    }

    /// Get raw response from any path with caching
    pub async fn get(&self, path: &str) -> Result<reqwest::Response> {
        // For custom paths, we don't cache as we can't determine the endpoint type
        Ok(self.client.get(path).await?)
    }

    /// Download a file from CDN (no caching for binary files)
    ///
    /// **Note**: This method downloads actual game content from CDN servers and does NOT
    /// cache the response. CDN content caching should be implemented in a separate layer
    /// (e.g., in ngdp-cdn crate) to handle binary data efficiently with proper storage
    /// in ~/.cache/ngdp/cdn/ instead of the TACT metadata cache.
    pub async fn download_file(
        &self,
        cdn_host: &str,
        path: &str,
        hash: &str,
    ) -> Result<reqwest::Response> {
        Ok(self.client.download_file(cdn_host, path, hash).await?)
    }

    /// Get the underlying HTTP client
    pub fn inner(&self) -> &HttpClient {
        &self.client
    }

    /// Get mutable access to the underlying HTTP client
    pub fn inner_mut(&mut self) -> &mut HttpClient {
        &mut self.client
    }

    /// Clear all cached responses
    pub async fn clear_cache(&self) -> Result<()> {
        debug!("Clearing all cached TACT responses");

        let region = self.client.region().to_string();
        let protocol = match self.client.version() {
            ProtocolVersion::V1 => "v1",
            ProtocolVersion::V2 => "v2",
        };

        let cache_subdir = self.cache_dir.join(region).join(protocol);
        if tokio::fs::metadata(&cache_subdir).await.is_ok() {
            clear_directory_recursively(&cache_subdir).await?;
        }

        Ok(())
    }

    /// Clear expired cache entries
    pub async fn clear_expired(&self) -> Result<()> {
        debug!("Clearing expired TACT cache entries");

        let region = self.client.region().to_string();
        let protocol = match self.client.version() {
            ProtocolVersion::V1 => "v1",
            ProtocolVersion::V2 => "v2",
        };

        let cache_subdir = self.cache_dir.join(region).join(protocol);
        if tokio::fs::metadata(&cache_subdir).await.is_ok() {
            clear_expired_in_directory(&cache_subdir).await?;
        }

        Ok(())
    }
}

/// Recursively clear all files in a directory
fn clear_directory_recursively(
    dir: &PathBuf,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + '_>> {
    Box::pin(async move {
        let mut entries = tokio::fs::read_dir(dir).await?;
        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            if let Ok(metadata) = tokio::fs::metadata(&path).await {
                if metadata.is_dir() {
                    clear_directory_recursively(&path).await?;
                } else {
                    tokio::fs::remove_file(&path).await?;
                }
            }
        }
        Ok(())
    })
}

/// Clear expired entries in a directory
fn clear_expired_in_directory(
    dir: &PathBuf,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + '_>> {
    Box::pin(async move {
        let mut entries = tokio::fs::read_dir(dir).await?;
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();

            if path.is_dir() {
                clear_expired_in_directory(&path).await?;
            } else if path.extension().and_then(|s| s.to_str()) == Some("meta") {
                // Check if this metadata file indicates an expired entry
                if let Ok(metadata_str) = tokio::fs::read_to_string(&path).await {
                    if let Ok(metadata) = serde_json::from_str::<CacheMetadata>(&metadata_str) {
                        if now.saturating_sub(metadata.timestamp) >= metadata.ttl_seconds {
                            // Remove both the data and metadata files
                            let data_path = path.with_extension("bpsv");
                            let _ = tokio::fs::remove_file(&data_path).await;
                            let _ = tokio::fs::remove_file(&path).await;
                            trace!("Removed expired TACT cache entry: {:?}", data_path);
                        }
                    }
                }
            }
        }

        Ok(())
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_endpoint_properties() {
        assert_eq!(TactEndpoint::Versions.as_str(), "versions");
        assert_eq!(TactEndpoint::Cdns.as_str(), "cdns");
        assert_eq!(TactEndpoint::Bgdl.as_str(), "bgdl");

        assert_eq!(TactEndpoint::Versions.ttl(), VERSIONS_CACHE_TTL);
        assert_eq!(TactEndpoint::Cdns.ttl(), CDN_CACHE_TTL);
        assert_eq!(TactEndpoint::Bgdl.ttl(), CDN_CACHE_TTL);
    }

    #[test]
    fn test_sequence_number_extraction() {
        tokio::runtime::Runtime::new().unwrap().block_on(async {
            let client = CachedTactClient::new(Region::US, ProtocolVersion::V1)
                .await
                .unwrap();

            // Test with sequence number
            let data_with_seqn = "Product!STRING:0|Seqn!DEC:4\n## seqn = 3020098\nwow|12345";
            assert_eq!(
                client.extract_sequence_number(data_with_seqn),
                Some(3020098)
            );

            // Test without sequence number
            let data_no_seqn = "Product!STRING:0|Seqn!DEC:4\nwow|12345";
            assert_eq!(client.extract_sequence_number(data_no_seqn), None);

            // Test with malformed sequence
            let data_bad_seqn = "## seqn = not_a_number\nwow|12345";
            assert_eq!(client.extract_sequence_number(data_bad_seqn), None);
        });
    }

    #[test]
    fn test_cache_path_generation() {
        tokio::runtime::Runtime::new().unwrap().block_on(async {
            let client = CachedTactClient::new(Region::US, ProtocolVersion::V1)
                .await
                .unwrap();

            let path = client.get_cache_path("wow", TactEndpoint::Versions, Some(12345));
            assert!(path.ends_with("us/v1/wow/versions-12345.bpsv"));

            let path_no_seq = client.get_cache_path("d3", TactEndpoint::Cdns, None);
            assert!(path_no_seq.ends_with("us/v1/d3/cdns-0.bpsv"));
        });
    }

    #[test]
    fn test_api_methods_compile() {
        // This test just verifies that all API methods compile correctly
        tokio::runtime::Runtime::new().unwrap().block_on(async {
            let client = CachedTactClient::new(Region::EU, ProtocolVersion::V2)
                .await
                .unwrap();

            // These would all compile and work in real usage:
            // let _ = client.get_versions_parsed("wow").await;
            // let _ = client.get_cdns_parsed("wow").await;
            // let _ = client.get_bgdl_parsed("wow").await;
            // let _ = client.clear_cache().await;
            // let _ = client.clear_expired().await;

            // Just verify the client was created
            assert_eq!(client.inner().region(), Region::EU);
            assert_eq!(client.inner().version(), ProtocolVersion::V2);
        });
    }
}