You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
7.3 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class Storage {
  7. constructor(duration) {
  8. this.duration = duration;
  9. this.running = new Map();
  10. this.data = new Map();
  11. this.levels = [];
  12. if (duration > 0) {
  13. this.levels.push(
  14. new Set(),
  15. new Set(),
  16. new Set(),
  17. new Set(),
  18. new Set(),
  19. new Set(),
  20. new Set(),
  21. new Set(),
  22. new Set()
  23. );
  24. for (let i = 8000; i < duration; i += 500) this.levels.push(new Set());
  25. }
  26. this.count = 0;
  27. this.interval = null;
  28. this.needTickCheck = false;
  29. this.nextTick = null;
  30. this.passive = true;
  31. this.tick = this.tick.bind(this);
  32. }
  33. ensureTick() {
  34. if (!this.interval && this.duration > 0 && !this.nextTick)
  35. this.interval = setInterval(
  36. this.tick,
  37. Math.floor(this.duration / this.levels.length)
  38. );
  39. }
  40. finished(name, err, result) {
  41. const callbacks = this.running.get(name);
  42. this.running.delete(name);
  43. if (this.duration > 0) {
  44. this.data.set(name, [err, result]);
  45. const levelData = this.levels[0];
  46. this.count -= levelData.size;
  47. levelData.add(name);
  48. this.count += levelData.size;
  49. this.ensureTick();
  50. }
  51. for (let i = 0; i < callbacks.length; i++) {
  52. callbacks[i](err, result);
  53. }
  54. }
  55. finishedSync(name, err, result) {
  56. if (this.duration > 0) {
  57. this.data.set(name, [err, result]);
  58. const levelData = this.levels[0];
  59. this.count -= levelData.size;
  60. levelData.add(name);
  61. this.count += levelData.size;
  62. this.ensureTick();
  63. }
  64. }
  65. provide(name, provider, callback) {
  66. if (typeof name !== "string") {
  67. callback(new TypeError("path must be a string"));
  68. return;
  69. }
  70. let running = this.running.get(name);
  71. if (running) {
  72. running.push(callback);
  73. return;
  74. }
  75. if (this.duration > 0) {
  76. this.checkTicks();
  77. const data = this.data.get(name);
  78. if (data) {
  79. return process.nextTick(() => {
  80. callback.apply(null, data);
  81. });
  82. }
  83. }
  84. this.running.set(name, (running = [callback]));
  85. provider(name, (err, result) => {
  86. this.finished(name, err, result);
  87. });
  88. }
  89. provideSync(name, provider) {
  90. if (typeof name !== "string") {
  91. throw new TypeError("path must be a string");
  92. }
  93. if (this.duration > 0) {
  94. this.checkTicks();
  95. const data = this.data.get(name);
  96. if (data) {
  97. if (data[0]) throw data[0];
  98. return data[1];
  99. }
  100. }
  101. let result;
  102. try {
  103. result = provider(name);
  104. } catch (e) {
  105. this.finishedSync(name, e);
  106. throw e;
  107. }
  108. this.finishedSync(name, null, result);
  109. return result;
  110. }
  111. tick() {
  112. const decay = this.levels.pop();
  113. for (let item of decay) {
  114. this.data.delete(item);
  115. }
  116. this.count -= decay.size;
  117. decay.clear();
  118. this.levels.unshift(decay);
  119. if (this.count === 0) {
  120. clearInterval(this.interval);
  121. this.interval = null;
  122. this.nextTick = null;
  123. return true;
  124. } else if (this.nextTick) {
  125. this.nextTick += Math.floor(this.duration / this.levels.length);
  126. const time = new Date().getTime();
  127. if (this.nextTick > time) {
  128. this.nextTick = null;
  129. this.interval = setInterval(
  130. this.tick,
  131. Math.floor(this.duration / this.levels.length)
  132. );
  133. return true;
  134. }
  135. } else if (this.passive) {
  136. clearInterval(this.interval);
  137. this.interval = null;
  138. this.nextTick =
  139. new Date().getTime() + Math.floor(this.duration / this.levels.length);
  140. } else {
  141. this.passive = true;
  142. }
  143. }
  144. checkTicks() {
  145. this.passive = false;
  146. if (this.nextTick) {
  147. while (!this.tick());
  148. }
  149. }
  150. purge(what) {
  151. if (!what) {
  152. this.count = 0;
  153. clearInterval(this.interval);
  154. this.nextTick = null;
  155. this.data.clear();
  156. this.levels.forEach(level => {
  157. level.clear();
  158. });
  159. } else if (typeof what === "string") {
  160. for (let key of this.data.keys()) {
  161. if (key.startsWith(what)) this.data.delete(key);
  162. }
  163. } else {
  164. for (let i = what.length - 1; i >= 0; i--) {
  165. this.purge(what[i]);
  166. }
  167. }
  168. }
  169. }
  170. module.exports = class CachedInputFileSystem {
  171. constructor(fileSystem, duration) {
  172. this.fileSystem = fileSystem;
  173. this._statStorage = new Storage(duration);
  174. this._readdirStorage = new Storage(duration);
  175. this._readFileStorage = new Storage(duration);
  176. this._readJsonStorage = new Storage(duration);
  177. this._readlinkStorage = new Storage(duration);
  178. this._stat = this.fileSystem.stat
  179. ? this.fileSystem.stat.bind(this.fileSystem)
  180. : null;
  181. if (!this._stat) this.stat = null;
  182. this._statSync = this.fileSystem.statSync
  183. ? this.fileSystem.statSync.bind(this.fileSystem)
  184. : null;
  185. if (!this._statSync) this.statSync = null;
  186. this._readdir = this.fileSystem.readdir
  187. ? this.fileSystem.readdir.bind(this.fileSystem)
  188. : null;
  189. if (!this._readdir) this.readdir = null;
  190. this._readdirSync = this.fileSystem.readdirSync
  191. ? this.fileSystem.readdirSync.bind(this.fileSystem)
  192. : null;
  193. if (!this._readdirSync) this.readdirSync = null;
  194. this._readFile = this.fileSystem.readFile
  195. ? this.fileSystem.readFile.bind(this.fileSystem)
  196. : null;
  197. if (!this._readFile) this.readFile = null;
  198. this._readFileSync = this.fileSystem.readFileSync
  199. ? this.fileSystem.readFileSync.bind(this.fileSystem)
  200. : null;
  201. if (!this._readFileSync) this.readFileSync = null;
  202. if (this.fileSystem.readJson) {
  203. this._readJson = this.fileSystem.readJson.bind(this.fileSystem);
  204. } else if (this.readFile) {
  205. this._readJson = (path, callback) => {
  206. this.readFile(path, (err, buffer) => {
  207. if (err) return callback(err);
  208. let data;
  209. try {
  210. data = JSON.parse(buffer.toString("utf-8"));
  211. } catch (e) {
  212. return callback(e);
  213. }
  214. callback(null, data);
  215. });
  216. };
  217. } else {
  218. this.readJson = null;
  219. }
  220. if (this.fileSystem.readJsonSync) {
  221. this._readJsonSync = this.fileSystem.readJsonSync.bind(this.fileSystem);
  222. } else if (this.readFileSync) {
  223. this._readJsonSync = path => {
  224. const buffer = this.readFileSync(path);
  225. const data = JSON.parse(buffer.toString("utf-8"));
  226. return data;
  227. };
  228. } else {
  229. this.readJsonSync = null;
  230. }
  231. this._readlink = this.fileSystem.readlink
  232. ? this.fileSystem.readlink.bind(this.fileSystem)
  233. : null;
  234. if (!this._readlink) this.readlink = null;
  235. this._readlinkSync = this.fileSystem.readlinkSync
  236. ? this.fileSystem.readlinkSync.bind(this.fileSystem)
  237. : null;
  238. if (!this._readlinkSync) this.readlinkSync = null;
  239. }
  240. stat(path, callback) {
  241. this._statStorage.provide(path, this._stat, callback);
  242. }
  243. readdir(path, callback) {
  244. this._readdirStorage.provide(path, this._readdir, callback);
  245. }
  246. readFile(path, callback) {
  247. this._readFileStorage.provide(path, this._readFile, callback);
  248. }
  249. readJson(path, callback) {
  250. this._readJsonStorage.provide(path, this._readJson, callback);
  251. }
  252. readlink(path, callback) {
  253. this._readlinkStorage.provide(path, this._readlink, callback);
  254. }
  255. statSync(path) {
  256. return this._statStorage.provideSync(path, this._statSync);
  257. }
  258. readdirSync(path) {
  259. return this._readdirStorage.provideSync(path, this._readdirSync);
  260. }
  261. readFileSync(path) {
  262. return this._readFileStorage.provideSync(path, this._readFileSync);
  263. }
  264. readJsonSync(path) {
  265. return this._readJsonStorage.provideSync(path, this._readJsonSync);
  266. }
  267. readlinkSync(path) {
  268. return this._readlinkStorage.provideSync(path, this._readlinkSync);
  269. }
  270. purge(what) {
  271. this._statStorage.purge(what);
  272. this._readdirStorage.purge(what);
  273. this._readFileStorage.purge(what);
  274. this._readlinkStorage.purge(what);
  275. this._readJsonStorage.purge(what);
  276. }
  277. };