|
| 1 | +import crypto from 'node:crypto' |
| 2 | +import fs from 'node:fs/promises' |
| 3 | +import { logger } from '../utils.js' |
| 4 | + |
| 5 | +export default class TrackCacheManager { |
| 6 | + constructor(nodelink) { |
| 7 | + this.nodelink = nodelink |
| 8 | + this.key = crypto.scryptSync( |
| 9 | + nodelink.options.server.password, |
| 10 | + 'nodelink-track-salt', |
| 11 | + 32 |
| 12 | + ) |
| 13 | + this.filePath = './.cache/tracks.bin' |
| 14 | + this.cache = new Map() |
| 15 | + this._saveTimeout = null |
| 16 | + } |
| 17 | + |
| 18 | + async load() { |
| 19 | + try { |
| 20 | + const data = await fs.readFile(this.filePath) |
| 21 | + if (data.length < 32) return |
| 22 | + |
| 23 | + const iv = data.subarray(0, 16) |
| 24 | + const tag = data.subarray(16, 32) |
| 25 | + const encrypted = data.subarray(32) |
| 26 | + |
| 27 | + const decipher = crypto.createDecipheriv('aes-256-gcm', this.key, iv) |
| 28 | + decipher.setAuthTag(tag) |
| 29 | + |
| 30 | + const decrypted = |
| 31 | + decipher.update(encrypted, 'binary', 'utf8') + decipher.final('utf8') |
| 32 | + const obj = JSON.parse(decrypted) |
| 33 | + |
| 34 | + this.cache = new Map(Object.entries(obj)) |
| 35 | + |
| 36 | + const now = Date.now() |
| 37 | + let expiredCount = 0 |
| 38 | + for (const [key, entry] of this.cache.entries()) { |
| 39 | + if (entry.expiresAt && now > entry.expiresAt) { |
| 40 | + this.cache.delete(key) |
| 41 | + expiredCount++ |
| 42 | + } |
| 43 | + } |
| 44 | + if (expiredCount > 0) this.save() |
| 45 | + |
| 46 | + logger('debug', 'TrackCache', `Loaded ${this.cache.size} cached tracks from disk.`) |
| 47 | + } catch (e) { |
| 48 | + if (e.code !== 'ENOENT') { |
| 49 | + logger('error', 'TrackCache', `Failed to load track cache: ${e.message}`) |
| 50 | + } |
| 51 | + this.cache = new Map() |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + async save() { |
| 56 | + if (this._saveTimeout) return |
| 57 | + |
| 58 | + this._saveTimeout = setTimeout(async () => { |
| 59 | + this._saveTimeout = null |
| 60 | + await this.forceSave() |
| 61 | + }, 5000) |
| 62 | + } |
| 63 | + |
| 64 | + async forceSave() { |
| 65 | + try { |
| 66 | + const plainText = JSON.stringify(Object.fromEntries(this.cache)) |
| 67 | + const iv = crypto.randomBytes(16) |
| 68 | + const cipher = crypto.createCipheriv('aes-256-gcm', this.key, iv) |
| 69 | + |
| 70 | + const encrypted = Buffer.concat([ |
| 71 | + cipher.update(plainText, 'utf8'), |
| 72 | + cipher.final() |
| 73 | + ]) |
| 74 | + const tag = cipher.getAuthTag() |
| 75 | + |
| 76 | + await fs.mkdir('./.cache', { recursive: true }) |
| 77 | + await fs.writeFile(this.filePath, Buffer.concat([iv, tag, encrypted])) |
| 78 | + } catch (e) { |
| 79 | + logger('error', 'TrackCache', `Failed to save track cache: ${e.message}`) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + get(source, identifier) { |
| 84 | + const key = `${source}:${identifier}` |
| 85 | + const entry = this.cache.get(key) |
| 86 | + if (!entry) return null |
| 87 | + if (entry.expiresAt && Date.now() > entry.expiresAt) { |
| 88 | + this.cache.delete(key) |
| 89 | + this.save() |
| 90 | + return null |
| 91 | + } |
| 92 | + return entry.value |
| 93 | + } |
| 94 | + |
| 95 | + set(source, identifier, value, ttlMs = 1000 * 60 * 60 * 6) { |
| 96 | + const key = `${source}:${identifier}` |
| 97 | + this.cache.set(key, { |
| 98 | + value, |
| 99 | + expiresAt: Date.now() + ttlMs |
| 100 | + }) |
| 101 | + this.save() |
| 102 | + } |
| 103 | +} |
0 commit comments