a zip code crypto-currency system good for red ONLY

CachedInputFileSystem.js 7.9KB

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