a zip code crypto-currency system good for red ONLY

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const path = require("path");
  7. const Tapable = require("tapable");
  8. const Compilation = require("./Compilation");
  9. const Stats = require("./Stats");
  10. const NormalModuleFactory = require("./NormalModuleFactory");
  11. const ContextModuleFactory = require("./ContextModuleFactory");
  12. const makePathsRelative = require("./util/identifier").makePathsRelative;
  13. class Watching {
  14. constructor(compiler, watchOptions, handler) {
  15. this.startTime = null;
  16. this.invalid = false;
  17. this.handler = handler;
  18. this.callbacks = [];
  19. this.closed = false;
  20. if(typeof watchOptions === "number") {
  21. this.watchOptions = {
  22. aggregateTimeout: watchOptions
  23. };
  24. } else if(watchOptions && typeof watchOptions === "object") {
  25. this.watchOptions = Object.assign({}, watchOptions);
  26. } else {
  27. this.watchOptions = {};
  28. }
  29. this.watchOptions.aggregateTimeout = this.watchOptions.aggregateTimeout || 200;
  30. this.compiler = compiler;
  31. this.running = true;
  32. this.compiler.readRecords(err => {
  33. if(err) return this._done(err);
  34. this._go();
  35. });
  36. }
  37. _go() {
  38. this.startTime = Date.now();
  39. this.running = true;
  40. this.invalid = false;
  41. this.compiler.applyPluginsAsync("watch-run", this, err => {
  42. if(err) return this._done(err);
  43. const onCompiled = (err, compilation) => {
  44. if(err) return this._done(err);
  45. if(this.invalid) return this._done();
  46. if(this.compiler.applyPluginsBailResult("should-emit", compilation) === false) {
  47. return this._done(null, compilation);
  48. }
  49. this.compiler.emitAssets(compilation, err => {
  50. if(err) return this._done(err);
  51. if(this.invalid) return this._done();
  52. this.compiler.emitRecords(err => {
  53. if(err) return this._done(err);
  54. if(compilation.applyPluginsBailResult("need-additional-pass")) {
  55. compilation.needAdditionalPass = true;
  56. const stats = new Stats(compilation);
  57. stats.startTime = this.startTime;
  58. stats.endTime = Date.now();
  59. this.compiler.applyPlugins("done", stats);
  60. this.compiler.applyPluginsAsync("additional-pass", err => {
  61. if(err) return this._done(err);
  62. this.compiler.compile(onCompiled);
  63. });
  64. return;
  65. }
  66. return this._done(null, compilation);
  67. });
  68. });
  69. };
  70. this.compiler.compile(onCompiled);
  71. });
  72. }
  73. _getStats(compilation) {
  74. const stats = new Stats(compilation);
  75. stats.startTime = this.startTime;
  76. stats.endTime = Date.now();
  77. return stats;
  78. }
  79. _done(err, compilation) {
  80. this.running = false;
  81. if(this.invalid) return this._go();
  82. const stats = compilation ? this._getStats(compilation) : null;
  83. if(err) {
  84. this.compiler.applyPlugins("failed", err);
  85. this.handler(err, stats);
  86. return;
  87. }
  88. this.compiler.applyPlugins("done", stats);
  89. this.handler(null, stats);
  90. if(!this.closed) {
  91. this.watch(compilation.fileDependencies, compilation.contextDependencies, compilation.missingDependencies);
  92. }
  93. this.callbacks.forEach(cb => cb());
  94. this.callbacks.length = 0;
  95. }
  96. watch(files, dirs, missing) {
  97. this.pausedWatcher = null;
  98. this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchOptions, (err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) => {
  99. this.pausedWatcher = this.watcher;
  100. this.watcher = null;
  101. if(err) return this.handler(err);
  102. this.compiler.fileTimestamps = fileTimestamps;
  103. this.compiler.contextTimestamps = contextTimestamps;
  104. this.invalidate();
  105. }, (fileName, changeTime) => {
  106. this.compiler.applyPlugins("invalid", fileName, changeTime);
  107. });
  108. }
  109. invalidate(callback) {
  110. if(callback) {
  111. this.callbacks.push(callback);
  112. }
  113. if(this.watcher) {
  114. this.pausedWatcher = this.watcher;
  115. this.watcher.pause();
  116. this.watcher = null;
  117. }
  118. if(this.running) {
  119. this.invalid = true;
  120. return false;
  121. } else {
  122. this._go();
  123. }
  124. }
  125. close(callback) {
  126. if(callback === undefined) callback = function() {};
  127. this.closed = true;
  128. if(this.watcher) {
  129. this.watcher.close();
  130. this.watcher = null;
  131. }
  132. if(this.pausedWatcher) {
  133. this.pausedWatcher.close();
  134. this.pausedWatcher = null;
  135. }
  136. if(this.running) {
  137. this.invalid = true;
  138. this._done = () => {
  139. this.compiler.applyPlugins("watch-close");
  140. callback();
  141. };
  142. } else {
  143. this.compiler.applyPlugins("watch-close");
  144. callback();
  145. }
  146. }
  147. }
  148. class Compiler extends Tapable {
  149. constructor() {
  150. super();
  151. this.outputPath = "";
  152. this.outputFileSystem = null;
  153. this.inputFileSystem = null;
  154. this.recordsInputPath = null;
  155. this.recordsOutputPath = null;
  156. this.records = {};
  157. this.fileTimestamps = {};
  158. this.contextTimestamps = {};
  159. this.resolvers = {
  160. normal: null,
  161. loader: null,
  162. context: null
  163. };
  164. let deprecationReported = false;
  165. this.parser = {
  166. plugin: (hook, fn) => {
  167. if(!deprecationReported) {
  168. console.warn("webpack: Using compiler.parser is deprecated.\n" +
  169. "Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.plugin(/* ... */); });\n}); instead. " +
  170. "It was called " + new Error().stack.split("\n")[2].trim() + ".");
  171. deprecationReported = true;
  172. }
  173. this.plugin("compilation", (compilation, data) => {
  174. data.normalModuleFactory.plugin("parser", parser => {
  175. parser.plugin(hook, fn);
  176. });
  177. });
  178. },
  179. apply: () => {
  180. const args = arguments;
  181. if(!deprecationReported) {
  182. console.warn("webpack: Using compiler.parser is deprecated.\n" +
  183. "Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.apply(/* ... */); });\n}); instead. " +
  184. "It was called " + new Error().stack.split("\n")[2].trim() + ".");
  185. deprecationReported = true;
  186. }
  187. this.plugin("compilation", (compilation, data) => {
  188. data.normalModuleFactory.plugin("parser", parser => {
  189. parser.apply.apply(parser, args);
  190. });
  191. });
  192. }
  193. };
  194. this.options = {};
  195. }
  196. watch(watchOptions, handler) {
  197. this.fileTimestamps = {};
  198. this.contextTimestamps = {};
  199. const watching = new Watching(this, watchOptions, handler);
  200. return watching;
  201. }
  202. run(callback) {
  203. const startTime = Date.now();
  204. const onCompiled = (err, compilation) => {
  205. if(err) return callback(err);
  206. if(this.applyPluginsBailResult("should-emit", compilation) === false) {
  207. const stats = new Stats(compilation);
  208. stats.startTime = startTime;
  209. stats.endTime = Date.now();
  210. this.applyPlugins("done", stats);
  211. return callback(null, stats);
  212. }
  213. this.emitAssets(compilation, err => {
  214. if(err) return callback(err);
  215. if(compilation.applyPluginsBailResult("need-additional-pass")) {
  216. compilation.needAdditionalPass = true;
  217. const stats = new Stats(compilation);
  218. stats.startTime = startTime;
  219. stats.endTime = Date.now();
  220. this.applyPlugins("done", stats);
  221. this.applyPluginsAsync("additional-pass", err => {
  222. if(err) return callback(err);
  223. this.compile(onCompiled);
  224. });
  225. return;
  226. }
  227. this.emitRecords(err => {
  228. if(err) return callback(err);
  229. const stats = new Stats(compilation);
  230. stats.startTime = startTime;
  231. stats.endTime = Date.now();
  232. this.applyPlugins("done", stats);
  233. return callback(null, stats);
  234. });
  235. });
  236. };
  237. this.applyPluginsAsync("before-run", this, err => {
  238. if(err) return callback(err);
  239. this.applyPluginsAsync("run", this, err => {
  240. if(err) return callback(err);
  241. this.readRecords(err => {
  242. if(err) return callback(err);
  243. this.compile(onCompiled);
  244. });
  245. });
  246. });
  247. }
  248. runAsChild(callback) {
  249. this.compile((err, compilation) => {
  250. if(err) return callback(err);
  251. this.parentCompilation.children.push(compilation);
  252. Object.keys(compilation.assets).forEach(name => {
  253. this.parentCompilation.assets[name] = compilation.assets[name];
  254. });
  255. const entries = Object.keys(compilation.entrypoints).map(name => {
  256. return compilation.entrypoints[name].chunks;
  257. }).reduce((array, chunks) => {
  258. return array.concat(chunks);
  259. }, []);
  260. return callback(null, entries, compilation);
  261. });
  262. }
  263. purgeInputFileSystem() {
  264. if(this.inputFileSystem && this.inputFileSystem.purge)
  265. this.inputFileSystem.purge();
  266. }
  267. emitAssets(compilation, callback) {
  268. let outputPath;
  269. const emitFiles = (err) => {
  270. if(err) return callback(err);
  271. require("async").forEach(Object.keys(compilation.assets), (file, callback) => {
  272. let targetFile = file;
  273. const queryStringIdx = targetFile.indexOf("?");
  274. if(queryStringIdx >= 0) {
  275. targetFile = targetFile.substr(0, queryStringIdx);
  276. }
  277. const writeOut = (err) => {
  278. if(err) return callback(err);
  279. const targetPath = this.outputFileSystem.join(outputPath, targetFile);
  280. const source = compilation.assets[file];
  281. if(source.existsAt === targetPath) {
  282. source.emitted = false;
  283. return callback();
  284. }
  285. let content = source.source();
  286. if(!Buffer.isBuffer(content)) {
  287. content = new Buffer(content, "utf8"); // eslint-disable-line
  288. }
  289. source.existsAt = targetPath;
  290. source.emitted = true;
  291. this.outputFileSystem.writeFile(targetPath, content, callback);
  292. };
  293. if(targetFile.match(/\/|\\/)) {
  294. const dir = path.dirname(targetFile);
  295. this.outputFileSystem.mkdirp(this.outputFileSystem.join(outputPath, dir), writeOut);
  296. } else writeOut();
  297. }, err => {
  298. if(err) return callback(err);
  299. afterEmit.call(this);
  300. });
  301. };
  302. this.applyPluginsAsync("emit", compilation, err => {
  303. if(err) return callback(err);
  304. outputPath = compilation.getPath(this.outputPath);
  305. this.outputFileSystem.mkdirp(outputPath, emitFiles);
  306. });
  307. function afterEmit() {
  308. this.applyPluginsAsyncSeries1("after-emit", compilation, err => {
  309. if(err) return callback(err);
  310. return callback();
  311. });
  312. }
  313. }
  314. emitRecords(callback) {
  315. if(!this.recordsOutputPath) return callback();
  316. const idx1 = this.recordsOutputPath.lastIndexOf("/");
  317. const idx2 = this.recordsOutputPath.lastIndexOf("\\");
  318. let recordsOutputPathDirectory = null;
  319. if(idx1 > idx2) recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx1);
  320. if(idx1 < idx2) recordsOutputPathDirectory = this.recordsOutputPath.substr(0, idx2);
  321. if(!recordsOutputPathDirectory) return writeFile.call(this);
  322. this.outputFileSystem.mkdirp(recordsOutputPathDirectory, err => {
  323. if(err) return callback(err);
  324. writeFile.call(this);
  325. });
  326. function writeFile() {
  327. this.outputFileSystem.writeFile(this.recordsOutputPath, JSON.stringify(this.records, undefined, 2), callback);
  328. }
  329. }
  330. readRecords(callback) {
  331. if(!this.recordsInputPath) {
  332. this.records = {};
  333. return callback();
  334. }
  335. this.inputFileSystem.stat(this.recordsInputPath, err => {
  336. // It doesn't exist
  337. // We can ignore this.
  338. if(err) return callback();
  339. this.inputFileSystem.readFile(this.recordsInputPath, (err, content) => {
  340. if(err) return callback(err);
  341. try {
  342. this.records = JSON.parse(content.toString("utf-8"));
  343. } catch(e) {
  344. e.message = "Cannot parse records: " + e.message;
  345. return callback(e);
  346. }
  347. return callback();
  348. });
  349. });
  350. }
  351. createChildCompiler(compilation, compilerName, compilerIndex, outputOptions, plugins) {
  352. const childCompiler = new Compiler();
  353. if(Array.isArray(plugins)) {
  354. plugins.forEach(plugin => childCompiler.apply(plugin));
  355. }
  356. for(const name in this._plugins) {
  357. if(["make", "compile", "emit", "after-emit", "invalid", "done", "this-compilation"].indexOf(name) < 0)
  358. childCompiler._plugins[name] = this._plugins[name].slice();
  359. }
  360. childCompiler.name = compilerName;
  361. childCompiler.outputPath = this.outputPath;
  362. childCompiler.inputFileSystem = this.inputFileSystem;
  363. childCompiler.outputFileSystem = null;
  364. childCompiler.resolvers = this.resolvers;
  365. childCompiler.fileTimestamps = this.fileTimestamps;
  366. childCompiler.contextTimestamps = this.contextTimestamps;
  367. const relativeCompilerName = makePathsRelative(this.context, compilerName);
  368. if(!this.records[relativeCompilerName]) this.records[relativeCompilerName] = [];
  369. if(this.records[relativeCompilerName][compilerIndex])
  370. childCompiler.records = this.records[relativeCompilerName][compilerIndex];
  371. else
  372. this.records[relativeCompilerName].push(childCompiler.records = {});
  373. childCompiler.options = Object.create(this.options);
  374. childCompiler.options.output = Object.create(childCompiler.options.output);
  375. for(const name in outputOptions) {
  376. childCompiler.options.output[name] = outputOptions[name];
  377. }
  378. childCompiler.parentCompilation = compilation;
  379. compilation.applyPlugins("child-compiler", childCompiler, compilerName, compilerIndex);
  380. return childCompiler;
  381. }
  382. isChild() {
  383. return !!this.parentCompilation;
  384. }
  385. createCompilation() {
  386. return new Compilation(this);
  387. }
  388. newCompilation(params) {
  389. const compilation = this.createCompilation();
  390. compilation.fileTimestamps = this.fileTimestamps;
  391. compilation.contextTimestamps = this.contextTimestamps;
  392. compilation.name = this.name;
  393. compilation.records = this.records;
  394. compilation.compilationDependencies = params.compilationDependencies;
  395. this.applyPlugins("this-compilation", compilation, params);
  396. this.applyPlugins("compilation", compilation, params);
  397. return compilation;
  398. }
  399. createNormalModuleFactory() {
  400. const normalModuleFactory = new NormalModuleFactory(this.options.context, this.resolvers, this.options.module || {});
  401. this.applyPlugins("normal-module-factory", normalModuleFactory);
  402. return normalModuleFactory;
  403. }
  404. createContextModuleFactory() {
  405. const contextModuleFactory = new ContextModuleFactory(this.resolvers, this.inputFileSystem);
  406. this.applyPlugins("context-module-factory", contextModuleFactory);
  407. return contextModuleFactory;
  408. }
  409. newCompilationParams() {
  410. const params = {
  411. normalModuleFactory: this.createNormalModuleFactory(),
  412. contextModuleFactory: this.createContextModuleFactory(),
  413. compilationDependencies: []
  414. };
  415. return params;
  416. }
  417. compile(callback) {
  418. const params = this.newCompilationParams();
  419. this.applyPluginsAsync("before-compile", params, err => {
  420. if(err) return callback(err);
  421. this.applyPlugins("compile", params);
  422. const compilation = this.newCompilation(params);
  423. this.applyPluginsParallel("make", compilation, err => {
  424. if(err) return callback(err);
  425. compilation.finish();
  426. compilation.seal(err => {
  427. if(err) return callback(err);
  428. this.applyPluginsAsync("after-compile", compilation, err => {
  429. if(err) return callback(err);
  430. return callback(null, compilation);
  431. });
  432. });
  433. });
  434. });
  435. }
  436. }
  437. Compiler.Watching = Watching;
  438. module.exports = Compiler;