UNPKG

5.07 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3// See LICENSE in the project root for license information.
4var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5 if (k2 === undefined) k2 = k;
6 var desc = Object.getOwnPropertyDescriptor(m, k);
7 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8 desc = { enumerable: true, get: function() { return m[k]; } };
9 }
10 Object.defineProperty(o, k2, desc);
11}) : (function(o, m, k, k2) {
12 if (k2 === undefined) k2 = k;
13 o[k2] = m[k];
14}));
15var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16 Object.defineProperty(o, "default", { enumerable: true, value: v });
17}) : function(o, v) {
18 o["default"] = v;
19});
20var __importStar = (this && this.__importStar) || function (mod) {
21 if (mod && mod.__esModule) return mod;
22 var result = {};
23 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24 __setModuleDefault(result, mod);
25 return result;
26};
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.ConfigCache = void 0;
29const tsdoc_config_1 = require("@microsoft/tsdoc-config");
30const path = __importStar(require("path"));
31const Debug_1 = require("./Debug");
32// How often to check for modified input files. If a file's modification timestamp has changed, then we will
33// evict the cache entry immediately.
34const CACHE_CHECK_INTERVAL_MS = 3 * 1000;
35// Evict old entries from the cache after this much time, regardless of whether the file was detected as being
36// modified or not.
37const CACHE_EXPIRE_MS = 20 * 1000;
38// If this many objects accumulate in the cache, then it is cleared to avoid a memory leak.
39const CACHE_MAX_SIZE = 100;
40class ConfigCache {
41 /**
42 * Node.js equivalent of performance.now().
43 */
44 static _getTimeInMs() {
45 const [seconds, nanoseconds] = process.hrtime();
46 return seconds * 1000 + nanoseconds / 1000000;
47 }
48 static getForSourceFile(sourceFilePath) {
49 const sourceFileFolder = path.dirname(path.resolve(sourceFilePath));
50 // First, determine the file to be loaded. If not found, the configFilePath will be an empty string.
51 const configFilePath = tsdoc_config_1.TSDocConfigFile.findConfigPathForFolder(sourceFileFolder);
52 // If configFilePath is an empty string, then we'll use the folder of sourceFilePath as our cache key
53 // (instead of an empty string)
54 const cacheKey = configFilePath || sourceFileFolder + '/';
55 Debug_1.Debug.log(`Cache key: "${cacheKey}"`);
56 const nowMs = ConfigCache._getTimeInMs();
57 let cachedConfig = undefined;
58 // Do we have a cached object?
59 cachedConfig = ConfigCache._cachedConfigs.get(cacheKey);
60 if (cachedConfig) {
61 Debug_1.Debug.log('Cache hit');
62 // Is the cached object still valid?
63 const loadAgeMs = nowMs - cachedConfig.loadTimeMs;
64 const lastCheckAgeMs = nowMs - cachedConfig.lastCheckTimeMs;
65 if (loadAgeMs > CACHE_EXPIRE_MS || loadAgeMs < 0) {
66 Debug_1.Debug.log('Evicting because item is expired');
67 cachedConfig = undefined;
68 ConfigCache._cachedConfigs.delete(cacheKey);
69 }
70 else if (lastCheckAgeMs > CACHE_CHECK_INTERVAL_MS || lastCheckAgeMs < 0) {
71 Debug_1.Debug.log('Checking for modifications');
72 cachedConfig.lastCheckTimeMs = nowMs;
73 if (cachedConfig.configFile.checkForModifiedFiles()) {
74 // Invalidate the cache because it failed to load completely
75 Debug_1.Debug.log('Evicting because item was modified');
76 cachedConfig = undefined;
77 ConfigCache._cachedConfigs.delete(cacheKey);
78 }
79 }
80 }
81 // Load the object
82 if (!cachedConfig) {
83 if (ConfigCache._cachedConfigs.size > CACHE_MAX_SIZE) {
84 Debug_1.Debug.log('Clearing cache');
85 ConfigCache._cachedConfigs.clear(); // avoid a memory leak
86 }
87 const configFile = tsdoc_config_1.TSDocConfigFile.loadFile(configFilePath);
88 if (configFile.fileNotFound) {
89 Debug_1.Debug.log(`File not found: "${configFilePath}"`);
90 }
91 else {
92 Debug_1.Debug.log(`Loaded: "${configFilePath}"`);
93 }
94 cachedConfig = {
95 configFile,
96 lastCheckTimeMs: nowMs,
97 loadTimeMs: nowMs
98 };
99 ConfigCache._cachedConfigs.set(cacheKey, cachedConfig);
100 }
101 return cachedConfig.configFile;
102 }
103}
104exports.ConfigCache = ConfigCache;
105// findConfigPathForFolder() result --> loaded tsdoc.json configuration
106ConfigCache._cachedConfigs = new Map();
107//# sourceMappingURL=ConfigCache.js.map
\No newline at end of file