All files / backend/dist/services/workspace file-cache-token.js

0% Statements 0/21
0% Branches 0/6
0% Functions 0/7
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                     
/**
 * File Cache Token
 *
 * Creates a cache token based on file modification times.
 * Used to invalidate configuration caches when files change.
 */
import fs from 'fs';
/**
 * FileCacheToken generates a unique token based on file modification times
 * Used for cache invalidation when source files change
 */
export class FileCacheToken {
    files;
    token;
    isBuilt;
    constructor(files) {
        this.files = files;
        this.token = null;
        this.isBuilt = false;
    }
    /**
     * Build the cache token from file modification times
     */
    async build() {
        if (this.isBuilt) {
            return this;
        }
        const signatures = [];
        const promises = (this.files || []).map((file) => new Promise((resolve, reject) => {
            fs.stat(file, (err, stats) => {
                if (err)
                    return reject(err);
                signatures.push(`${file}>${stats.mtime.getTime()}`);
                resolve();
            });
        }));
        await Promise.all(promises);
        this.token = signatures.sort().join('|');
        this.isBuilt = true;
        this.files = null;
        return this;
    }
    /**
     * Check if this token matches another token
     * @param other - Another FileCacheToken to compare against
     * @returns true if tokens match, false otherwise
     */
    async match(other) {
        await Promise.all([this.build(), other.build()]);
        return this.token === other.token;
    }
    /**
     * Get the token value (after building)
     */
    getToken() {
        return this.token;
    }
}