tauri-plugin-cache 0.1.6

Advanced disk caching solution for Tauri applications. Provides compression, TTL management, memory caching, automatic cleanup, and cross-platform support. Enhances data access performance and storage optimization.
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
import SwiftRs
import Tauri
import UIKit
import Foundation
import Compression

// MARK: - Custom Error Type

enum CachePluginError: Error, LocalizedError {
    case invalidArgs(String)
    case operationFailed(String)
    
    var errorDescription: String? {
        switch self {
        case .invalidArgs(let message):
            return "Invalid arguments: \(message)"
        case .operationFailed(let message):
            return "Operation failed: \(message)"
        }
    }
}

// MARK: - Structures and Models

class ConfigureRequest: Decodable {
    let default_compression: Bool?
    let compression_level: Int?
    let compression_threshold: Int?
    let compression_method: String?
}

class CompressionConfig: Decodable {
    let enabled: Bool
    let level: Int
    let threshold: Int
    let method: String
}

// Non-generic version for parsing set arguments
class SetRequestArgs: Decodable {
    let key: String
    let value: AnyCodableValue
    let options: SetItemOptions?
}

// Helper to handle any JSON value
struct AnyCodableValue: Codable {
    let value: Any
    
    init(_ value: Any) {
        self.value = value
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        
        if container.decodeNil() {
            value = NSNull()
        } else if let bool = try? container.decode(Bool.self) {
            value = bool
        } else if let int = try? container.decode(Int.self) {
            value = int
        } else if let double = try? container.decode(Double.self) {
            value = double
        } else if let string = try? container.decode(String.self) {
            value = string
        } else if let array = try? container.decode([AnyCodableValue].self) {
            value = array.map { $0.value }
        } else if let dict = try? container.decode([String: AnyCodableValue].self) {
            value = dict.mapValues { $0.value }
        } else {
            throw DecodingError.dataCorruptedError(in: container, debugDescription: "Cannot decode value")
        }
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        
        switch value {
        case is NSNull:
            try container.encodeNil()
        case let bool as Bool:
            try container.encode(bool)
        case let int as Int:
            try container.encode(int)
        case let double as Double:
            try container.encode(double)
        case let string as String:
            try container.encode(string)
        case let array as [Any]:
            try container.encode(array.map { AnyCodableValue($0) })
        case let dict as [String: Any]:
            try container.encode(dict.mapValues { AnyCodableValue($0) })
        default:
            throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: container.codingPath, debugDescription: "Cannot encode value"))
        }
    }
}

class SetItemOptions: Decodable {
    let ttl: TimeInterval?
    let compress: Bool?
    let compressionMethod: String?
}

class GetRequest: Decodable {
    let key: String
}

class HasRequest: Decodable {
    let key: String
}

class RemoveRequest: Decodable {
    let key: String
}

class EmptyResponse: Encodable {
}

class BooleanResponse: Encodable {
    let value: Bool
    
    init(value: Bool) {
        self.value = value
    }
}

class CacheStats: Encodable {
    let totalSize: Int
    let activeSize: Int
    
    init(totalSize: Int, activeSize: Int) {
        self.totalSize = totalSize
        self.activeSize = activeSize
    }
}

// Helper class to return optional values (can be nil)
class OptionalValueResponse: Encodable {
    let value: String?
    
    init(_ value: String?) {
        self.value = value
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        if let value = value {
            try container.encode(value)
        } else {
            try container.encodeNil()
        }
    }
}

// MARK: - Cache Plugin

class CachePlugin: Plugin {
    private let cacheDirectory: URL
    private var defaultCompression = true
    private var compressionLevel = 6
    private var compressionThreshold = 1024 // 1KB
    private var compressionMethod = "zlib"
    
    private let fileManager = FileManager.default
    private let syncQueue = DispatchQueue(label: "app.tauri.plugin.cache.sync")
    
    override init() {
        // Create cache directory
        let appCacheDir = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
        cacheDirectory = appCacheDir.appendingPathComponent("tauri_cache", isDirectory: true)
        
        super.init()
        
        // Create directory if it doesn't exist
        if !fileManager.fileExists(atPath: cacheDirectory.path) {
            try? fileManager.createDirectory(at: cacheDirectory, withIntermediateDirectories: true)
        }
        
        print("Cache plugin initialized at \(cacheDirectory.path)")
    }
    
    // MARK: - API Methods
    
    @objc public func configure(_ invoke: Invoke) throws {
        let args = try invoke.parseArgs(ConfigureRequest.self)
        
        if let defaultCompression = args.default_compression {
            self.defaultCompression = defaultCompression
        }
        
        if let compressionLevel = args.compression_level {
            self.compressionLevel = compressionLevel
        }
        
        if let compressionThreshold = args.compression_threshold {
            self.compressionThreshold = compressionThreshold
        }
        
        if let compressionMethod = args.compression_method {
            self.compressionMethod = compressionMethod
        }
        
        print("Configure: compression=\(defaultCompression), level=\(compressionLevel), method=\(compressionMethod)")
        invoke.resolve()
    }
    
    @objc public func updateCompressionConfig(_ invoke: Invoke) throws {
        let config = try invoke.parseArgs(CompressionConfig.self)
        self.defaultCompression = config.enabled
        self.compressionLevel = config.level
        self.compressionThreshold = config.threshold
        self.compressionMethod = config.method
        
        print("Updated compression config: enabled=\(defaultCompression), level=\(compressionLevel), method=\(compressionMethod)")
        invoke.resolve()
    }
    
    @objc public func set(_ invoke: Invoke) throws {
        // Parse arguments using the proper Tauri API
        let args = try invoke.parseArgs(SetRequestArgs.self)
        let key = args.key
        
        // Extract options
        var ttl: TimeInterval? = nil
        var shouldCompress = self.defaultCompression
        var compressionMethodToUse = self.compressionMethod
        
        if let options = args.options {
            ttl = options.ttl
            if let compress = options.compress {
                shouldCompress = compress
            }
            if let method = options.compressionMethod {
                compressionMethodToUse = method
            }
        }
        
        // Store as JSON data
        let valueData = try JSONEncoder().encode(args.value)
        
        // Apply compression
        let finalData: Data
        var isCompressed = false
        var methodUsed: UInt8 = 0
        
        if shouldCompress && valueData.count > compressionThreshold {
            // Choose compression method
            if compressionMethodToUse.lowercased() == "lzma2" {
                if let compressed = compressWithLZMA(data: valueData) {
                    finalData = compressed
                    isCompressed = true
                    methodUsed = 2
                } else {
                    // Fall back to Zlib
                    if let compressed = compressWithZlib(data: valueData) {
                        finalData = compressed
                        isCompressed = true
                        methodUsed = 1
                    } else {
                        finalData = valueData
                    }
                }
            } else {
                if let compressed = compressWithZlib(data: valueData) {
                    finalData = compressed
                    isCompressed = true
                    methodUsed = 1
                } else {
                    finalData = valueData
                }
            }
        } else {
            finalData = valueData
        }
        
        // Build data with compression header if compressed
        var storedData: Data
        if isCompressed {
            storedData = Data()
            storedData.append(1) // Compression indicator
            storedData.append(methodUsed) // Method marker
            storedData.append(finalData)
        } else {
            storedData = finalData
        }
        
        // Create cache entry
        var cacheEntry: [String: Any] = [
            "value": storedData.base64EncodedString(),
            "is_compressed": isCompressed
        ]
        
        // Add expiration time
        if let ttl = ttl {
            let expiresAt = Date().timeIntervalSince1970 + ttl
            cacheEntry["expires_at"] = expiresAt
        }
        
        // Save as JSON
        let entryData = try JSONSerialization.data(withJSONObject: cacheEntry)
        
        // Save the file
        let fileURL = cacheDirectory.appendingPathComponent(key)
        syncQueue.sync {
            do {
                try entryData.write(to: fileURL)
                print("Cache item saved to \(fileURL.path)")
            } catch {
                print("Failed to write cache file: \(error)")
            }
        }
        
        invoke.resolve(EmptyResponse())
    }
    
    @objc public func get(_ invoke: Invoke) throws {
        let args = try invoke.parseArgs(GetRequest.self)
        let key = args.key
        
        let fileURL = cacheDirectory.appendingPathComponent(key)
        
        // Check if file exists
        guard fileManager.fileExists(atPath: fileURL.path) else {
            invoke.resolve(OptionalValueResponse(nil))
            return
        }
        
        // Read the file
        let data: Data
        do {
            data = try Data(contentsOf: fileURL)
        } catch {
            print("Failed to read cache file: \(error)")
            invoke.resolve(OptionalValueResponse(nil))
            return
        }
        
        // Parse as JSON
        guard let entryDict = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
            invoke.resolve(OptionalValueResponse(nil))
            return
        }
        
        // Check expiration time
        if let expiresAt = entryDict["expires_at"] as? TimeInterval {
            let now = Date().timeIntervalSince1970
            if now > expiresAt {
                // Item expired, delete it
                try? fileManager.removeItem(at: fileURL)
                invoke.resolve(OptionalValueResponse(nil))
                return
            }
        }
        
        // Extract value
        guard let valueBase64 = entryDict["value"] as? String,
              let valueData = Data(base64Encoded: valueBase64) else {
            invoke.resolve(OptionalValueResponse(nil))
            return
        }
        
        // Check if compressed
        let isCompressed = entryDict["is_compressed"] as? Bool ?? false
        
        let finalData: Data
        if isCompressed {
            do {
                finalData = try decompressData(valueData)
            } catch {
                print("Failed to decompress data: \(error)")
                invoke.resolve(OptionalValueResponse(nil))
                return
            }
        } else {
            finalData = valueData
        }
        
        // Parse JSON data
        if let jsonObject = try? JSONSerialization.jsonObject(with: finalData),
           let jsonString = String(data: try JSONSerialization.data(withJSONObject: jsonObject), encoding: .utf8) {
            invoke.resolve(OptionalValueResponse(jsonString))
        } else if let stringValue = String(data: finalData, encoding: .utf8) {
            // Accept as direct string
            invoke.resolve(OptionalValueResponse(stringValue))
        } else {
            invoke.resolve(OptionalValueResponse(nil))
        }
    }
    
    @objc public func has(_ invoke: Invoke) throws {
        let args = try invoke.parseArgs(HasRequest.self)
        let key = args.key
        
        let fileURL = cacheDirectory.appendingPathComponent(key)
        
        // Check if file exists
        guard fileManager.fileExists(atPath: fileURL.path) else {
            invoke.resolve(BooleanResponse(value: false))
            return
        }
        
        // Read file and check validity
        do {
            let data = try Data(contentsOf: fileURL)
            guard let entryDict = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
                invoke.resolve(BooleanResponse(value: false))
                return
            }
            
            // Check expiration time
            if let expiresAt = entryDict["expires_at"] as? TimeInterval {
                let now = Date().timeIntervalSince1970
                if now > expiresAt {
                    // Item expired, delete it
                    try? fileManager.removeItem(at: fileURL)
                    invoke.resolve(BooleanResponse(value: false))
                    return
                }
            }
            
            invoke.resolve(BooleanResponse(value: true))
        } catch {
            // Could not read file
            invoke.resolve(BooleanResponse(value: false))
        }
    }
    
    @objc public func remove(_ invoke: Invoke) throws {
        let args = try invoke.parseArgs(RemoveRequest.self)
        let key = args.key
        
        let fileURL = cacheDirectory.appendingPathComponent(key)
        
        if fileManager.fileExists(atPath: fileURL.path) {
            do {
                try fileManager.removeItem(at: fileURL)
                print("Cache item removed: \(key)")
            } catch {
                print("Failed to remove cache item: \(error)")
            }
        }
        
        invoke.resolve(EmptyResponse())
    }
    
    @objc public func clear(_ invoke: Invoke) throws {
        do {
            let contents = try fileManager.contentsOfDirectory(at: cacheDirectory, includingPropertiesForKeys: nil)
            for fileURL in contents {
                try fileManager.removeItem(at: fileURL)
            }
            print("Removed \(contents.count) cache items")
        } catch {
            print("Failed to clear cache: \(error)")
        }
        
        invoke.resolve(EmptyResponse())
    }
    
    @objc public func stats(_ invoke: Invoke) throws {
        var totalSize = 0
        var activeSize = 0
        let now = Date().timeIntervalSince1970
        
        do {
            let contents = try fileManager.contentsOfDirectory(at: cacheDirectory, includingPropertiesForKeys: nil)
            totalSize = contents.count
            
            for fileURL in contents {
                do {
                    let data = try Data(contentsOf: fileURL)
                    if let entryDict = try JSONSerialization.jsonObject(with: data) as? [String: Any] {
                        if let expiresAt = entryDict["expires_at"] as? TimeInterval {
                            if now <= expiresAt {
                                activeSize += 1
                            }
                        } else {
                            activeSize += 1
                        }
                    }
                } catch {
                    // Could not read file, skip
                }
            }
        } catch {
            print("Failed to get stats: \(error)")
        }
        
        invoke.resolve(CacheStats(totalSize: totalSize, activeSize: activeSize))
    }
    
    // MARK: - Compression Methods using Apple's Compression framework
    
    /// Compress data using Zlib (via Apple's Compression framework)
    private func compressWithZlib(data: Data) -> Data? {
        return compressData(data, algorithm: COMPRESSION_ZLIB)
    }
    
    /// Compress data using LZMA (via Apple's Compression framework)
    /// Note: Apple's Compression framework uses LZMA, not LZMA2, but provides similar compression ratios
    private func compressWithLZMA(data: Data) -> Data? {
        return compressData(data, algorithm: COMPRESSION_LZMA)
    }
    
    /// Generic compression using Apple's Compression framework
    private func compressData(_ data: Data, algorithm: compression_algorithm) -> Data? {
        // Calculate destination buffer size (worst case: slightly larger than source)
        let destinationBufferSize = data.count + 1024
        let destinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: destinationBufferSize)
        defer { destinationBuffer.deallocate() }
        
        let sourceBuffer = [UInt8](data)
        
        let compressedSize = compression_encode_buffer(
            destinationBuffer,
            destinationBufferSize,
            sourceBuffer,
            data.count,
            nil,
            algorithm
        )
        
        guard compressedSize > 0 else {
            print("Compression failed for algorithm: \(algorithm)")
            return nil
        }
        
        let compressedData = Data(bytes: destinationBuffer, count: compressedSize)
        print("Compressed \(data.count) bytes to \(compressedSize) bytes using algorithm \(algorithm)")
        return compressedData
    }
    
    /// Decompress data based on the compression header
    private func decompressData(_ data: Data) throws -> Data {
        guard data.count >= 2 else {
            throw CachePluginError.invalidArgs("Invalid compressed data")
        }
        
        // First byte is compression indicator
        let isCompressed = data[0] == 1
        
        if !isCompressed {
            // Uncompressed data
            return data.subdata(in: 2..<data.count)
        }
        
        // Second byte is compression method
        let method = data[1]
        let compressedData = data.subdata(in: 2..<data.count)
        
        let algorithm: compression_algorithm
        switch method {
        case 1:
            algorithm = COMPRESSION_ZLIB
        case 2:
            algorithm = COMPRESSION_LZMA
        default:
            throw CachePluginError.invalidArgs("Unknown compression method: \(method)")
        }
        
        guard let decompressedData = decompressData(compressedData, algorithm: algorithm) else {
            throw CachePluginError.operationFailed("Failed to decompress data")
        }
        
        return decompressedData
    }
    
    /// Generic decompression using Apple's Compression framework
    private func decompressData(_ data: Data, algorithm: compression_algorithm) -> Data? {
        // Estimate decompressed size (start with 4x, grow if needed)
        var destinationBufferSize = data.count * 4
        var destinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: destinationBufferSize)
        
        let sourceBuffer = [UInt8](data)
        
        var decompressedSize = compression_decode_buffer(
            destinationBuffer,
            destinationBufferSize,
            sourceBuffer,
            data.count,
            nil,
            algorithm
        )
        
        // If buffer was too small, try with larger buffer
        if decompressedSize == 0 || decompressedSize == destinationBufferSize {
            destinationBuffer.deallocate()
            destinationBufferSize = data.count * 16
            destinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: destinationBufferSize)
            
            decompressedSize = compression_decode_buffer(
                destinationBuffer,
                destinationBufferSize,
                sourceBuffer,
                data.count,
                nil,
                algorithm
            )
        }
        
        defer { destinationBuffer.deallocate() }
        
        guard decompressedSize > 0 else {
            print("Decompression failed for algorithm: \(algorithm)")
            return nil
        }
        
        let decompressedData = Data(bytes: destinationBuffer, count: decompressedSize)
        print("Decompressed \(data.count) bytes to \(decompressedSize) bytes using algorithm \(algorithm)")
        return decompressedData
    }
}

// MARK: - Plugin Init Function

@_cdecl("init_plugin_cache")
func initPlugin() -> Plugin {
    return CachePlugin()
}