a zip code crypto-currency system good for red ONLY

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const compareLocations = require("./compareLocations");
  8. const SortableSet = require("./util/SortableSet");
  9. let debugId = 1000;
  10. const sortById = (a, b) => {
  11. if(a.id < b.id) return -1;
  12. if(b.id < a.id) return 1;
  13. return 0;
  14. };
  15. const sortByIdentifier = (a, b) => {
  16. if(a.identifier() > b.identifier()) return 1;
  17. if(a.identifier() < b.identifier()) return -1;
  18. return 0;
  19. };
  20. class Chunk {
  21. constructor(name, module, loc) {
  22. this.id = null;
  23. this.ids = null;
  24. this.debugId = debugId++;
  25. this.name = name;
  26. this._modules = new SortableSet(undefined, sortByIdentifier);
  27. this.entrypoints = [];
  28. this.chunks = [];
  29. this.parents = [];
  30. this.blocks = [];
  31. this.origins = [];
  32. this.files = [];
  33. this.rendered = false;
  34. if(module) {
  35. this.origins.push({
  36. module,
  37. loc,
  38. name
  39. });
  40. }
  41. }
  42. get entry() {
  43. throw new Error("Chunk.entry was removed. Use hasRuntime()");
  44. }
  45. set entry(data) {
  46. throw new Error("Chunk.entry was removed. Use hasRuntime()");
  47. }
  48. get initial() {
  49. throw new Error("Chunk.initial was removed. Use isInitial()");
  50. }
  51. set initial(data) {
  52. throw new Error("Chunk.initial was removed. Use isInitial()");
  53. }
  54. hasRuntime() {
  55. if(this.entrypoints.length === 0) return false;
  56. return this.entrypoints[0].chunks[0] === this;
  57. }
  58. isInitial() {
  59. return this.entrypoints.length > 0;
  60. }
  61. hasEntryModule() {
  62. return !!this.entryModule;
  63. }
  64. addToCollection(collection, item) {
  65. if(item === this) {
  66. return false;
  67. }
  68. if(collection.indexOf(item) > -1) {
  69. return false;
  70. }
  71. collection.push(item);
  72. return true;
  73. }
  74. addChunk(chunk) {
  75. return this.addToCollection(this.chunks, chunk);
  76. }
  77. addParent(parentChunk) {
  78. return this.addToCollection(this.parents, parentChunk);
  79. }
  80. addModule(module) {
  81. if(!this._modules.has(module)) {
  82. this._modules.add(module);
  83. return true;
  84. }
  85. return false;
  86. }
  87. addBlock(block) {
  88. return this.addToCollection(this.blocks, block);
  89. }
  90. removeModule(module) {
  91. if(this._modules.delete(module)) {
  92. module.removeChunk(this);
  93. return true;
  94. }
  95. return false;
  96. }
  97. removeChunk(chunk) {
  98. const idx = this.chunks.indexOf(chunk);
  99. if(idx >= 0) {
  100. this.chunks.splice(idx, 1);
  101. chunk.removeParent(this);
  102. return true;
  103. }
  104. return false;
  105. }
  106. removeParent(chunk) {
  107. const idx = this.parents.indexOf(chunk);
  108. if(idx >= 0) {
  109. this.parents.splice(idx, 1);
  110. chunk.removeChunk(this);
  111. return true;
  112. }
  113. return false;
  114. }
  115. addOrigin(module, loc) {
  116. this.origins.push({
  117. module,
  118. loc,
  119. name: this.name
  120. });
  121. }
  122. setModules(modules) {
  123. this._modules = new SortableSet(modules, sortByIdentifier);
  124. }
  125. getNumberOfModules() {
  126. return this._modules.size;
  127. }
  128. get modulesIterable() {
  129. return this._modules;
  130. }
  131. forEachModule(fn) {
  132. this._modules.forEach(fn);
  133. }
  134. mapModules(fn) {
  135. return Array.from(this._modules, fn);
  136. }
  137. compareTo(otherChunk) {
  138. this._modules.sort();
  139. otherChunk._modules.sort();
  140. if(this._modules.size > otherChunk._modules.size) return -1;
  141. if(this._modules.size < otherChunk._modules.size) return 1;
  142. const a = this._modules[Symbol.iterator]();
  143. const b = otherChunk._modules[Symbol.iterator]();
  144. while(true) { // eslint-disable-line
  145. const aItem = a.next();
  146. const bItem = b.next();
  147. if(aItem.done) return 0;
  148. const aModuleIdentifier = aItem.value.identifier();
  149. const bModuleIdentifier = bItem.value.identifier();
  150. if(aModuleIdentifier > bModuleIdentifier) return -1;
  151. if(aModuleIdentifier < bModuleIdentifier) return 1;
  152. }
  153. }
  154. containsModule(module) {
  155. return this._modules.has(module);
  156. }
  157. getModules() {
  158. return Array.from(this._modules);
  159. }
  160. getModulesIdent() {
  161. this._modules.sort();
  162. let str = "";
  163. this._modules.forEach(m => {
  164. str += m.identifier() + "#";
  165. });
  166. return str;
  167. }
  168. remove(reason) {
  169. // cleanup modules
  170. // Array.from is used here to create a clone, because removeChunk modifies this._modules
  171. Array.from(this._modules).forEach(module => {
  172. module.removeChunk(this);
  173. });
  174. // cleanup parents
  175. this.parents.forEach(parentChunk => {
  176. // remove this chunk from its parents
  177. const idx = parentChunk.chunks.indexOf(this);
  178. if(idx >= 0) {
  179. parentChunk.chunks.splice(idx, 1);
  180. }
  181. // cleanup "sub chunks"
  182. this.chunks.forEach(chunk => {
  183. /**
  184. * remove this chunk as "intermediary" and connect
  185. * it "sub chunks" and parents directly
  186. */
  187. // add parent to each "sub chunk"
  188. chunk.addParent(parentChunk);
  189. // add "sub chunk" to parent
  190. parentChunk.addChunk(chunk);
  191. });
  192. });
  193. /**
  194. * we need to iterate again over the chunks
  195. * to remove this from the chunks parents.
  196. * This can not be done in the above loop
  197. * as it is not garuanteed that `this.parents` contains anything.
  198. */
  199. this.chunks.forEach(chunk => {
  200. // remove this as parent of every "sub chunk"
  201. const idx = chunk.parents.indexOf(this);
  202. if(idx >= 0) {
  203. chunk.parents.splice(idx, 1);
  204. }
  205. });
  206. // cleanup blocks
  207. this.blocks.forEach(block => {
  208. const idx = block.chunks.indexOf(this);
  209. if(idx >= 0) {
  210. block.chunks.splice(idx, 1);
  211. if(block.chunks.length === 0) {
  212. block.chunks = null;
  213. block.chunkReason = reason;
  214. }
  215. }
  216. });
  217. }
  218. moveModule(module, otherChunk) {
  219. module.removeChunk(this);
  220. module.addChunk(otherChunk);
  221. otherChunk.addModule(module);
  222. module.rewriteChunkInReasons(this, [otherChunk]);
  223. }
  224. replaceChunk(oldChunk, newChunk) {
  225. const idx = this.chunks.indexOf(oldChunk);
  226. if(idx >= 0) {
  227. this.chunks.splice(idx, 1);
  228. }
  229. if(this !== newChunk && newChunk.addParent(this)) {
  230. this.addChunk(newChunk);
  231. }
  232. }
  233. replaceParentChunk(oldParentChunk, newParentChunk) {
  234. const idx = this.parents.indexOf(oldParentChunk);
  235. if(idx >= 0) {
  236. this.parents.splice(idx, 1);
  237. }
  238. if(this !== newParentChunk && newParentChunk.addChunk(this)) {
  239. this.addParent(newParentChunk);
  240. }
  241. }
  242. integrate(otherChunk, reason) {
  243. if(!this.canBeIntegrated(otherChunk)) {
  244. return false;
  245. }
  246. // Array.from is used here to create a clone, because moveModule modifies otherChunk._modules
  247. const otherChunkModules = Array.from(otherChunk._modules);
  248. otherChunkModules.forEach(module => otherChunk.moveModule(module, this));
  249. otherChunk._modules.clear();
  250. otherChunk.parents.forEach(parentChunk => parentChunk.replaceChunk(otherChunk, this));
  251. otherChunk.parents.length = 0;
  252. otherChunk.chunks.forEach(chunk => chunk.replaceParentChunk(otherChunk, this));
  253. otherChunk.chunks.length = 0;
  254. otherChunk.blocks.forEach(b => {
  255. b.chunks = b.chunks ? b.chunks.map(c => {
  256. return c === otherChunk ? this : c;
  257. }) : [this];
  258. b.chunkReason = reason;
  259. this.addBlock(b);
  260. });
  261. otherChunk.blocks.length = 0;
  262. otherChunk.origins.forEach(origin => {
  263. this.origins.push(origin);
  264. });
  265. this.blocks.forEach(b => {
  266. b.chunkReason = reason;
  267. });
  268. this.origins.forEach(origin => {
  269. if(!origin.reasons) {
  270. origin.reasons = [reason];
  271. } else if(origin.reasons[0] !== reason) {
  272. origin.reasons.unshift(reason);
  273. }
  274. });
  275. this.chunks = this.chunks.filter(chunk => {
  276. return chunk !== otherChunk && chunk !== this;
  277. });
  278. this.parents = this.parents.filter(parentChunk => {
  279. return parentChunk !== otherChunk && parentChunk !== this;
  280. });
  281. return true;
  282. }
  283. split(newChunk) {
  284. this.blocks.forEach(block => {
  285. newChunk.blocks.push(block);
  286. block.chunks.push(newChunk);
  287. });
  288. this.chunks.forEach(chunk => {
  289. newChunk.chunks.push(chunk);
  290. chunk.parents.push(newChunk);
  291. });
  292. this.parents.forEach(parentChunk => {
  293. parentChunk.chunks.push(newChunk);
  294. newChunk.parents.push(parentChunk);
  295. });
  296. this.entrypoints.forEach(entrypoint => {
  297. entrypoint.insertChunk(newChunk, this);
  298. });
  299. }
  300. isEmpty() {
  301. return this._modules.size === 0;
  302. }
  303. updateHash(hash) {
  304. hash.update(`${this.id} `);
  305. hash.update(this.ids ? this.ids.join(",") : "");
  306. hash.update(`${this.name || ""} `);
  307. this._modules.forEach(m => m.updateHash(hash));
  308. }
  309. canBeIntegrated(otherChunk) {
  310. if(otherChunk.isInitial()) {
  311. return false;
  312. }
  313. if(this.isInitial()) {
  314. if(otherChunk.parents.length !== 1 || otherChunk.parents[0] !== this) {
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. addMultiplierAndOverhead(size, options) {
  321. const overhead = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
  322. const multiplicator = this.isInitial() ? (options.entryChunkMultiplicator || 10) : 1;
  323. return size * multiplicator + overhead;
  324. }
  325. modulesSize() {
  326. let count = 0;
  327. for(const module of this._modules) {
  328. count += module.size();
  329. }
  330. return count;
  331. }
  332. size(options) {
  333. return this.addMultiplierAndOverhead(this.modulesSize(), options);
  334. }
  335. integratedSize(otherChunk, options) {
  336. // Chunk if it's possible to integrate this chunk
  337. if(!this.canBeIntegrated(otherChunk)) {
  338. return false;
  339. }
  340. let integratedModulesSize = this.modulesSize();
  341. // only count modules that do not exist in this chunk!
  342. for(const otherModule of otherChunk._modules) {
  343. if(!this._modules.has(otherModule)) {
  344. integratedModulesSize += otherModule.size();
  345. }
  346. }
  347. return this.addMultiplierAndOverhead(integratedModulesSize, options);
  348. }
  349. getChunkMaps(includeEntries, realHash) {
  350. const chunksProcessed = [];
  351. const chunkHashMap = {};
  352. const chunkNameMap = {};
  353. (function addChunk(chunk) {
  354. if(chunksProcessed.indexOf(chunk) >= 0) return;
  355. chunksProcessed.push(chunk);
  356. if(!chunk.hasRuntime() || includeEntries) {
  357. chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
  358. if(chunk.name)
  359. chunkNameMap[chunk.id] = chunk.name;
  360. }
  361. chunk.chunks.forEach(addChunk);
  362. }(this));
  363. return {
  364. hash: chunkHashMap,
  365. name: chunkNameMap
  366. };
  367. }
  368. sortModules(sortByFn) {
  369. this._modules.sortWith(sortByFn || sortById);
  370. }
  371. sortItems() {
  372. this.sortModules();
  373. this.origins.sort((a, b) => {
  374. const aIdent = a.module.identifier();
  375. const bIdent = b.module.identifier();
  376. if(aIdent < bIdent) return -1;
  377. if(aIdent > bIdent) return 1;
  378. return compareLocations(a.loc, b.loc);
  379. });
  380. this.origins.forEach(origin => {
  381. if(origin.reasons)
  382. origin.reasons.sort();
  383. });
  384. this.parents.sort(sortById);
  385. this.chunks.sort(sortById);
  386. }
  387. toString() {
  388. return `Chunk[${Array.from(this._modules).join()}]`;
  389. }
  390. checkConstraints() {
  391. const chunk = this;
  392. chunk.chunks.forEach((child, idx) => {
  393. if(chunk.chunks.indexOf(child) !== idx)
  394. throw new Error(`checkConstraints: duplicate child in chunk ${chunk.debugId} ${child.debugId}`);
  395. if(child.parents.indexOf(chunk) < 0)
  396. throw new Error(`checkConstraints: child missing parent ${chunk.debugId} -> ${child.debugId}`);
  397. });
  398. chunk.parents.forEach((parentChunk, idx) => {
  399. if(chunk.parents.indexOf(parentChunk) !== idx)
  400. throw new Error(`checkConstraints: duplicate parent in chunk ${chunk.debugId} ${parentChunk.debugId}`);
  401. if(parentChunk.chunks.indexOf(chunk) < 0)
  402. throw new Error(`checkConstraints: parent missing child ${parentChunk.debugId} <- ${chunk.debugId}`);
  403. });
  404. }
  405. }
  406. Object.defineProperty(Chunk.prototype, "modules", {
  407. configurable: false,
  408. get: util.deprecate(function() {
  409. return Array.from(this._modules);
  410. }, "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead."),
  411. set: util.deprecate(function(value) {
  412. this.setModules(value);
  413. }, "Chunk.modules is deprecated. Use Chunk.addModule/removeModule instead.")
  414. });
  415. module.exports = Chunk;