Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CachedInputFileSystem.js 7.4KB

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