browser.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // shim for using process in browser
  2. var process = module.exports = {};
  3. // cached from whatever global is present so that test runners that stub it
  4. // don't break things. But we need to wrap it in a try catch in case it is
  5. // wrapped in strict mode code which doesn't define any globals. It's inside a
  6. // function because try/catches deoptimize in certain engines.
  7. var cachedSetTimeout;
  8. var cachedClearTimeout;
  9. function defaultSetTimout() {
  10. throw new Error('setTimeout has not been defined');
  11. }
  12. function defaultClearTimeout () {
  13. throw new Error('clearTimeout has not been defined');
  14. }
  15. (function () {
  16. try {
  17. if (typeof setTimeout === 'function') {
  18. cachedSetTimeout = setTimeout;
  19. } else {
  20. cachedSetTimeout = defaultSetTimout;
  21. }
  22. } catch (e) {
  23. cachedSetTimeout = defaultSetTimout;
  24. }
  25. try {
  26. if (typeof clearTimeout === 'function') {
  27. cachedClearTimeout = clearTimeout;
  28. } else {
  29. cachedClearTimeout = defaultClearTimeout;
  30. }
  31. } catch (e) {
  32. cachedClearTimeout = defaultClearTimeout;
  33. }
  34. } ())
  35. function runTimeout(fun) {
  36. if (cachedSetTimeout === setTimeout) {
  37. //normal enviroments in sane situations
  38. return setTimeout(fun, 0);
  39. }
  40. // if setTimeout wasn't available but was latter defined
  41. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  42. cachedSetTimeout = setTimeout;
  43. return setTimeout(fun, 0);
  44. }
  45. try {
  46. // when when somebody has screwed with setTimeout but no I.E. maddness
  47. return cachedSetTimeout(fun, 0);
  48. } catch(e){
  49. try {
  50. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  51. return cachedSetTimeout.call(null, fun, 0);
  52. } catch(e){
  53. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
  54. return cachedSetTimeout.call(this, fun, 0);
  55. }
  56. }
  57. }
  58. function runClearTimeout(marker) {
  59. if (cachedClearTimeout === clearTimeout) {
  60. //normal enviroments in sane situations
  61. return clearTimeout(marker);
  62. }
  63. // if clearTimeout wasn't available but was latter defined
  64. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  65. cachedClearTimeout = clearTimeout;
  66. return clearTimeout(marker);
  67. }
  68. try {
  69. // when when somebody has screwed with setTimeout but no I.E. maddness
  70. return cachedClearTimeout(marker);
  71. } catch (e){
  72. try {
  73. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  74. return cachedClearTimeout.call(null, marker);
  75. } catch (e){
  76. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
  77. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  78. return cachedClearTimeout.call(this, marker);
  79. }
  80. }
  81. }
  82. var queue = [];
  83. var draining = false;
  84. var currentQueue;
  85. var queueIndex = -1;
  86. function cleanUpNextTick() {
  87. if (!draining || !currentQueue) {
  88. return;
  89. }
  90. draining = false;
  91. if (currentQueue.length) {
  92. queue = currentQueue.concat(queue);
  93. } else {
  94. queueIndex = -1;
  95. }
  96. if (queue.length) {
  97. drainQueue();
  98. }
  99. }
  100. function drainQueue() {
  101. if (draining) {
  102. return;
  103. }
  104. var timeout = runTimeout(cleanUpNextTick);
  105. draining = true;
  106. var len = queue.length;
  107. while(len) {
  108. currentQueue = queue;
  109. queue = [];
  110. while (++queueIndex < len) {
  111. if (currentQueue) {
  112. currentQueue[queueIndex].run();
  113. }
  114. }
  115. queueIndex = -1;
  116. len = queue.length;
  117. }
  118. currentQueue = null;
  119. draining = false;
  120. runClearTimeout(timeout);
  121. }
  122. process.nextTick = function (fun) {
  123. var args = new Array(arguments.length - 1);
  124. if (arguments.length > 1) {
  125. for (var i = 1; i < arguments.length; i++) {
  126. args[i - 1] = arguments[i];
  127. }
  128. }
  129. queue.push(new Item(fun, args));
  130. if (queue.length === 1 && !draining) {
  131. runTimeout(drainQueue);
  132. }
  133. };
  134. // v8 likes predictible objects
  135. function Item(fun, array) {
  136. this.fun = fun;
  137. this.array = array;
  138. }
  139. Item.prototype.run = function () {
  140. this.fun.apply(null, this.array);
  141. };
  142. process.title = 'browser';
  143. process.browser = true;
  144. process.env = {};
  145. process.argv = [];
  146. process.version = ''; // empty string to avoid regexp issues
  147. process.versions = {};
  148. function noop() {}
  149. process.on = noop;
  150. process.addListener = noop;
  151. process.once = noop;
  152. process.off = noop;
  153. process.removeListener = noop;
  154. process.removeAllListeners = noop;
  155. process.emit = noop;
  156. process.prependListener = noop;
  157. process.prependOnceListener = noop;
  158. process.listeners = function (name) { return [] }
  159. process.binding = function (name) {
  160. throw new Error('process.binding is not supported');
  161. };
  162. process.cwd = function () { return '/' };
  163. process.chdir = function (dir) {
  164. throw new Error('process.chdir is not supported');
  165. };
  166. process.umask = function() { return 0; };