ResolveCache.js 12 KB
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
var fs = require('../util/fs');
var path = require('path');
var mout = require('mout');
var Q = require('q');
var mkdirp = require('mkdirp');
var rimraf = require('../util/rimraf');
var LRU = require('lru-cache');
var lockFile = require('lockfile');
var md5 = require('md5-hex');
var semver = require('../util/semver');
var readJson = require('../util/readJson');
var copy = require('../util/copy');

function ResolveCache(config) {
    // TODO: Make some config entries, such as:
    //       - Max MB
    //       - Max versions per source
    //       - Max MB per source
    //       - etc..
    this._config = config;
    this._dir = this._config.storage.packages;
    this._lockDir = this._config.storage.packages;

    mkdirp.sync(this._lockDir);

    // Cache is stored/retrieved statically to ensure singularity
    // among instances
    this._cache = this.constructor._cache.get(this._dir);
    if (!this._cache) {
        this._cache = new LRU({
            max: 100,
            maxAge: 60 * 5 * 1000  // 5 minutes
        });
        this.constructor._cache.set(this._dir, this._cache);
    }

    // Ensure dir is created
    mkdirp.sync(this._dir);
}

// -----------------

ResolveCache.prototype.retrieve = function (source, target) {
    var sourceId = md5(source);
    var dir = path.join(this._dir, sourceId);
    var that = this;

    target = target || '*';

    return this._getVersions(sourceId)
    .spread(function (versions) {
        var suitable;

        // If target is a semver, find a suitable version
        if (semver.validRange(target)) {
            suitable = semver.maxSatisfying(versions, target, true);

            if (suitable) {
                return suitable;
            }
        }

        // If target is '*' check if there's a cached '_wildcard'
        if (target === '*') {
            return mout.array.find(versions, function (version) {
                return version === '_wildcard';
            });
        }

        // Otherwise check if there's an exact match
        return mout.array.find(versions, function (version) {
            return version === target;
        });
    })
    .then(function (version) {
        var canonicalDir;

        if (!version) {
            return [];
        }

        // Resolve with canonical dir and package meta
        canonicalDir = path.join(dir, encodeURIComponent(version));
        return that._readPkgMeta(canonicalDir)
        .then(function (pkgMeta) {
            return [canonicalDir, pkgMeta];
        }, function () {
            // If there was an error, invalidate the in-memory cache,
            // delete the cached package and try again
            that._cache.del(sourceId);

            return Q.nfcall(rimraf, canonicalDir)
            .then(function () {
                return that.retrieve(source, target);
            });
        });
    });
};

ResolveCache.prototype.store = function (canonicalDir, pkgMeta) {
    var sourceId;
    var release;
    var dir;
    var pkgLock;
    var promise;
    var that = this;

    promise = pkgMeta ? Q.resolve(pkgMeta) : this._readPkgMeta(canonicalDir);

    return promise
    .then(function (pkgMeta) {
        sourceId = md5(pkgMeta._source);
        release = that._getPkgRelease(pkgMeta);
        dir = path.join(that._dir, sourceId, release);
        pkgLock = path.join(that._lockDir, sourceId + '-' + release + '.lock');

        // Check if destination directory exists to prevent issuing lock at all times
        return Q.nfcall(fs.stat, dir)
        .fail(function (err) {
            var lockParams = { wait: 250, retries: 25, stale: 60000 };
            return Q.nfcall(lockFile.lock, pkgLock, lockParams).then(function () {
                // Ensure other process didn't start copying files before lock was created
                return Q.nfcall(fs.stat, dir)
                .fail(function (err) {
                    // If stat fails, it is expected to return ENOENT
                    if (err.code !== 'ENOENT') {
                        throw err;
                    }

                    // Create missing directory and copy files there
                    return Q.nfcall(mkdirp, path.dirname(dir)).then(function () {
                        return Q.nfcall(fs.rename, canonicalDir, dir)
                        .fail(function (err) {
                            // If error is EXDEV it means that we are trying to rename
                            // across different drives, so we copy and remove it instead
                            if (err.code !== 'EXDEV') {
                                throw err;
                            }

                            return copy.copyDir(canonicalDir, dir);
                        });
                    });
                });
            }).finally(function () {
                lockFile.unlockSync(pkgLock);
            });
        }).finally(function () {
            // Ensure no tmp dir is left on disk.
            return Q.nfcall(rimraf, canonicalDir);
        });
    })
    .then(function () {
        var versions = that._cache.get(sourceId);

        // Add it to the in memory cache
        // and sort the versions afterwards
        if (versions && versions.indexOf(release) === -1) {
            versions.push(release);
            that._sortVersions(versions);
        }

        // Resolve with the final location
        return dir;
    });
};

ResolveCache.prototype.eliminate = function (pkgMeta) {
    var sourceId = md5(pkgMeta._source);
    var release = this._getPkgRelease(pkgMeta);
    var dir = path.join(this._dir, sourceId, release);
    var that = this;

    return Q.nfcall(rimraf, dir)
    .then(function () {
        var versions = that._cache.get(sourceId) || [];
        mout.array.remove(versions, release);

        // If this was the last package in the cache,
        // delete the parent folder (source)
        // For extra security, check against the file system
        // if this was really the last package
        if (!versions.length) {
            that._cache.del(sourceId);

            return that._getVersions(sourceId)
            .spread(function (versions) {
                if (!versions.length) {
                    // Do not keep in-memory cache if it's completely
                    // empty
                    that._cache.del(sourceId);

                    return Q.nfcall(rimraf, path.dirname(dir));
                }
            });
        }
    });
};

ResolveCache.prototype.clear = function () {
    return Q.nfcall(rimraf, this._dir)
    .then(function () {
        return Q.nfcall(fs.mkdir, this._dir);
    }.bind(this))
    .then(function () {
        this._cache.reset();
    }.bind(this));
};

ResolveCache.prototype.reset = function () {
    this._cache.reset();
    return this;
};

ResolveCache.prototype.versions = function (source) {
    var sourceId = md5(source);

    return this._getVersions(sourceId)
    .spread(function (versions) {
        return versions.filter(function (version) {
            return semver.valid(version);
        });
    });
};

ResolveCache.prototype.list = function () {
    var promises;
    var dirs = [];
    var that = this;

    // Get the list of directories
    return Q.nfcall(fs.readdir, this._dir)
    .then(function (sourceIds) {
        promises = sourceIds.map(function (sourceId) {
            return Q.nfcall(fs.readdir, path.join(that._dir, sourceId))
            .then(function (versions) {
                versions.forEach(function (version) {
                    var dir = path.join(that._dir, sourceId, version);
                    dirs.push(dir);
                });
            }, function (err) {
                // Ignore lurking files, e.g.: .DS_Store if the user
                // has navigated throughout the cache
                if (err.code === 'ENOTDIR' && err.path) {
                    return Q.nfcall(rimraf, err.path);
                }

                throw err;
            });
        });

        return Q.all(promises);
    })
    // Read every package meta
    .then(function () {
        promises = dirs.map(function (dir) {
            return that._readPkgMeta(dir)
            .then(function (pkgMeta) {
                return {
                    canonicalDir: dir,
                    pkgMeta: pkgMeta
                };
            }, function () {
                // If it fails to read, invalidate the in memory
                // cache for the source and delete the entry directory
                var sourceId = path.basename(path.dirname(dir));
                that._cache.del(sourceId);

                return Q.nfcall(rimraf, dir);
            });
        });

        return Q.all(promises);
    })
    // Sort by name ASC & release ASC
    .then(function (entries) {
        // Ignore falsy entries due to errors reading
        // package metas
        entries = entries.filter(function (entry) {
            return !!entry;
        });

        return entries.sort(function (entry1, entry2) {
            var pkgMeta1 = entry1.pkgMeta;
            var pkgMeta2 = entry2.pkgMeta;
            var comp = pkgMeta1.name.localeCompare(pkgMeta2.name);

            // Sort by name
            if (comp) {
                return comp;
            }

            // Sort by version
            if (pkgMeta1.version && pkgMeta2.version) {
                return semver.compare(pkgMeta1.version, pkgMeta2.version);
            }
            if (pkgMeta1.version) {
                return -1;
            }
            if (pkgMeta2.version) {
                return 1;
            }

            // Sort by target
            return pkgMeta1._target.localeCompare(pkgMeta2._target);
        });
    });
};

// ------------------------

ResolveCache.clearRuntimeCache = function () {
    // Note that _cache refers to the static _cache variable
    // that holds other caches per dir!
    // Do not confuse it with the instance cache

    // Clear cache of each directory
    this._cache.forEach(function (cache) {
        cache.reset();
    });

    // Clear root cache
    this._cache.reset();
};

// ------------------------

ResolveCache.prototype._getPkgRelease = function (pkgMeta) {
    var release = pkgMeta.version || (pkgMeta._target === '*' ? '_wildcard' : pkgMeta._target);

    // Encode some dangerous chars such as / and \
    release = encodeURIComponent(release);

    return release;
};

ResolveCache.prototype._readPkgMeta = function (dir) {
    var filename = path.join(dir, '.bower.json');

    return readJson(filename)
    .spread(function (json) {
        return json;
    });
};

ResolveCache.prototype._getVersions = function (sourceId) {
    var dir;
    var versions = this._cache.get(sourceId);
    var that = this;

    if (versions) {
        return Q.resolve([versions, true]);
    }

    dir = path.join(this._dir, sourceId);
    return Q.nfcall(fs.readdir, dir)
    .then(function (versions) {
        // Sort and cache in memory
        that._sortVersions(versions);
        versions = versions.map(decodeURIComponent);
        that._cache.set(sourceId, versions);
        return [versions, false];
    }, function (err) {
        // If the directory does not exists, resolve
        // as an empty array
        if (err.code === 'ENOENT') {
            versions = [];
            that._cache.set(sourceId, versions);
            return [versions, false];
        }

        throw err;
    });
};

ResolveCache.prototype._sortVersions = function (versions) {
    // Sort DESC
    versions.sort(function (version1, version2) {
        var validSemver1 = semver.valid(version1);
        var validSemver2 = semver.valid(version2);

        // If both are semvers, compare them
        if (validSemver1 && validSemver2) {
            return semver.rcompare(version1, version2);
        }

        // If one of them are semvers, give higher priority
        if (validSemver1) {
            return -1;
        }
        if (validSemver2) {
            return 1;
        }

        // Otherwise they are considered equal
        return 0;
    });
};

// ------------------------

ResolveCache._cache = new LRU({
    max: 5,
    maxAge: 60 * 30 * 1000  // 30 minutes
});

module.exports = ResolveCache;