a zip code crypto-currency system good for red ONLY

node.ts 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import './events';
  9. import './fs';
  10. import {findEventTasks} from '../common/events';
  11. import {patchTimer} from '../common/timers';
  12. import {ArraySlice, bindArguments, isMix, patchMacroTask, patchMethod, patchMicroTask, patchOnProperties} from '../common/utils';
  13. const set = 'set';
  14. const clear = 'clear';
  15. Zone.__load_patch('node_util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  16. api.patchOnProperties = patchOnProperties;
  17. api.patchMethod = patchMethod;
  18. api.bindArguments = bindArguments;
  19. });
  20. Zone.__load_patch('node_timers', (global: any, Zone: ZoneType) => {
  21. // Timers
  22. let globalUseTimeoutFromTimer = false;
  23. try {
  24. const timers = require('timers');
  25. let globalEqualTimersTimeout = global.setTimeout === timers.setTimeout;
  26. if (!globalEqualTimersTimeout && !isMix) {
  27. // 1. if isMix, then we are in mix environment such as Electron
  28. // we should only patch timers.setTimeout because global.setTimeout
  29. // have been patched
  30. // 2. if global.setTimeout not equal timers.setTimeout, check
  31. // whether global.setTimeout use timers.setTimeout or not
  32. const originSetTimeout = timers.setTimeout;
  33. timers.setTimeout = function() {
  34. globalUseTimeoutFromTimer = true;
  35. return originSetTimeout.apply(this, arguments);
  36. };
  37. const detectTimeout = global.setTimeout(() => {}, 100);
  38. clearTimeout(detectTimeout);
  39. timers.setTimeout = originSetTimeout;
  40. }
  41. patchTimer(timers, set, clear, 'Timeout');
  42. patchTimer(timers, set, clear, 'Interval');
  43. patchTimer(timers, set, clear, 'Immediate');
  44. } catch (error) {
  45. // timers module not exists, for example, when we using nativeScript
  46. // timers is not available
  47. }
  48. if (isMix) {
  49. // if we are in mix environment, such as Electron,
  50. // the global.setTimeout has already been patched,
  51. // so we just patch timers.setTimeout
  52. return;
  53. }
  54. if (!globalUseTimeoutFromTimer) {
  55. // 1. global setTimeout equals timers setTimeout
  56. // 2. or global don't use timers setTimeout(maybe some other library patch setTimeout)
  57. // 3. or load timers module error happens, we should patch global setTimeout
  58. patchTimer(global, set, clear, 'Timeout');
  59. patchTimer(global, set, clear, 'Interval');
  60. patchTimer(global, set, clear, 'Immediate');
  61. } else {
  62. // global use timers setTimeout, but not equals
  63. // this happens when use nodejs v0.10.x, global setTimeout will
  64. // use a lazy load version of timers setTimeout
  65. // we should not double patch timer's setTimeout
  66. // so we only store the __symbol__ for consistency
  67. global[Zone.__symbol__('setTimeout')] = global.setTimeout;
  68. global[Zone.__symbol__('setInterval')] = global.setInterval;
  69. global[Zone.__symbol__('setImmediate')] = global.setImmediate;
  70. }
  71. });
  72. // patch process related methods
  73. Zone.__load_patch('nextTick', () => {
  74. // patch nextTick as microTask
  75. patchMicroTask(process, 'nextTick', (self: any, args: any[]) => {
  76. return {
  77. name: 'process.nextTick',
  78. args: args,
  79. cbIdx: (args.length > 0 && typeof args[0] === 'function') ? 0 : -1,
  80. target: process
  81. };
  82. });
  83. });
  84. Zone.__load_patch(
  85. 'handleUnhandledPromiseRejection', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  86. (Zone as any)[api.symbol('unhandledPromiseRejectionHandler')] =
  87. findProcessPromiseRejectionHandler('unhandledRejection');
  88. (Zone as any)[api.symbol('rejectionHandledHandler')] =
  89. findProcessPromiseRejectionHandler('rejectionHandled');
  90. // handle unhandled promise rejection
  91. function findProcessPromiseRejectionHandler(evtName: string) {
  92. return function(e: any) {
  93. const eventTasks = findEventTasks(process, evtName);
  94. eventTasks.forEach(eventTask => {
  95. // process has added unhandledrejection event listener
  96. // trigger the event listener
  97. if (evtName === 'unhandledRejection') {
  98. eventTask.invoke(e.rejection, e.promise);
  99. } else if (evtName === 'rejectionHandled') {
  100. eventTask.invoke(e.promise);
  101. }
  102. });
  103. };
  104. }
  105. });
  106. // Crypto
  107. Zone.__load_patch('crypto', () => {
  108. let crypto: any;
  109. try {
  110. crypto = require('crypto');
  111. } catch (err) {
  112. }
  113. // use the generic patchMacroTask to patch crypto
  114. if (crypto) {
  115. const methodNames = ['randomBytes', 'pbkdf2'];
  116. methodNames.forEach(name => {
  117. patchMacroTask(crypto, name, (self: any, args: any[]) => {
  118. return {
  119. name: 'crypto.' + name,
  120. args: args,
  121. cbIdx: (args.length > 0 && typeof args[args.length - 1] === 'function') ?
  122. args.length - 1 :
  123. -1,
  124. target: crypto
  125. };
  126. });
  127. });
  128. }
  129. });
  130. Zone.__load_patch('console', (global: any, Zone: ZoneType) => {
  131. const consoleMethods =
  132. ['dir', 'log', 'info', 'error', 'warn', 'assert', 'debug', 'timeEnd', 'trace'];
  133. consoleMethods.forEach((m: string) => {
  134. const originalMethod = (console as any)[Zone.__symbol__(m)] = (console as any)[m];
  135. if (originalMethod) {
  136. (console as any)[m] = function() {
  137. const args = ArraySlice.call(arguments);
  138. if (Zone.current === Zone.root) {
  139. return originalMethod.apply(this, args);
  140. } else {
  141. return Zone.root.run(originalMethod, this, args);
  142. }
  143. };
  144. }
  145. });
  146. });