process.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // shim for using process in browser
  2. // based off https://github.com/defunctzombie/node-process/blob/master/browser.js
  3. function defaultSetTimout() {
  4. throw new Error('setTimeout has not been defined')
  5. }
  6. function defaultClearTimeout() {
  7. throw new Error('clearTimeout has not been defined')
  8. }
  9. var cachedSetTimeout = defaultSetTimout
  10. var cachedClearTimeout = defaultClearTimeout
  11. if (typeof global.setTimeout === 'function') {
  12. cachedSetTimeout = setTimeout
  13. }
  14. if (typeof global.clearTimeout === 'function') {
  15. cachedClearTimeout = clearTimeout
  16. }
  17. function runTimeout(fun) {
  18. if (cachedSetTimeout === setTimeout) {
  19. //normal enviroments in sane situations
  20. return setTimeout(fun, 0)
  21. }
  22. // if setTimeout wasn't available but was latter defined
  23. if (
  24. (cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) &&
  25. setTimeout
  26. ) {
  27. cachedSetTimeout = setTimeout
  28. return setTimeout(fun, 0)
  29. }
  30. try {
  31. // when when somebody has screwed with setTimeout but no I.E. maddness
  32. return cachedSetTimeout(fun, 0)
  33. } catch (e) {
  34. try {
  35. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  36. return cachedSetTimeout.call(null, fun, 0)
  37. } catch (e) {
  38. // 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
  39. return cachedSetTimeout.call(this, fun, 0)
  40. }
  41. }
  42. }
  43. function runClearTimeout(marker) {
  44. if (cachedClearTimeout === clearTimeout) {
  45. //normal enviroments in sane situations
  46. return clearTimeout(marker)
  47. }
  48. // if clearTimeout wasn't available but was latter defined
  49. if (
  50. (cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) &&
  51. clearTimeout
  52. ) {
  53. cachedClearTimeout = clearTimeout
  54. return clearTimeout(marker)
  55. }
  56. try {
  57. // when when somebody has screwed with setTimeout but no I.E. maddness
  58. return cachedClearTimeout(marker)
  59. } catch (e) {
  60. try {
  61. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  62. return cachedClearTimeout.call(null, marker)
  63. } catch (e) {
  64. // 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.
  65. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  66. return cachedClearTimeout.call(this, marker)
  67. }
  68. }
  69. }
  70. var queue = []
  71. var draining = false
  72. var currentQueue
  73. var queueIndex = -1
  74. function cleanUpNextTick() {
  75. if (!draining || !currentQueue) {
  76. return
  77. }
  78. draining = false
  79. if (currentQueue.length) {
  80. queue = currentQueue.concat(queue)
  81. } else {
  82. queueIndex = -1
  83. }
  84. if (queue.length) {
  85. drainQueue()
  86. }
  87. }
  88. function drainQueue() {
  89. if (draining) {
  90. return
  91. }
  92. var timeout = runTimeout(cleanUpNextTick)
  93. draining = true
  94. var len = queue.length
  95. while (len) {
  96. currentQueue = queue
  97. queue = []
  98. while (++queueIndex < len) {
  99. if (currentQueue) {
  100. currentQueue[queueIndex].run()
  101. }
  102. }
  103. queueIndex = -1
  104. len = queue.length
  105. }
  106. currentQueue = null
  107. draining = false
  108. runClearTimeout(timeout)
  109. }
  110. function nextTick(fun) {
  111. var args = new Array(arguments.length - 1)
  112. if (arguments.length > 1) {
  113. for (var i = 1; i < arguments.length; i++) {
  114. args[i - 1] = arguments[i]
  115. }
  116. }
  117. queue.push(new Item(fun, args))
  118. if (queue.length === 1 && !draining) {
  119. runTimeout(drainQueue)
  120. }
  121. }
  122. // v8 likes predictible objects
  123. function Item(fun, array) {
  124. this.fun = fun
  125. this.array = array
  126. }
  127. Item.prototype.run = function() {
  128. this.fun.apply(null, this.array)
  129. }
  130. var title = 'browser'
  131. var platform = 'browser'
  132. var browser = true
  133. var env = {}
  134. var argv = []
  135. var version = '' // empty string to avoid regexp issues
  136. var versions = {}
  137. var release = {}
  138. var config = {}
  139. function noop() {}
  140. var on = noop
  141. var addListener = noop
  142. var once = noop
  143. var off = noop
  144. var removeListener = noop
  145. var removeAllListeners = noop
  146. var emit = noop
  147. function binding(name) {
  148. throw new Error('process.binding is not supported')
  149. }
  150. function cwd() {
  151. return '/'
  152. }
  153. function chdir(dir) {
  154. throw new Error('process.chdir is not supported')
  155. }
  156. function umask() {
  157. return 0
  158. }
  159. // from https://github.com/kumavis/browser-process-hrtime/blob/master/index.js
  160. var performance = global.performance || {}
  161. var performanceNow =
  162. performance.now ||
  163. performance.mozNow ||
  164. performance.msNow ||
  165. performance.oNow ||
  166. performance.webkitNow ||
  167. function() {
  168. return new Date().getTime()
  169. }
  170. // generate timestamp or delta
  171. // see http://nodejs.org/api/process.html#process_process_hrtime
  172. function hrtime(previousTimestamp) {
  173. var clocktime = performanceNow.call(performance) * 1e-3
  174. var seconds = Math.floor(clocktime)
  175. var nanoseconds = Math.floor((clocktime % 1) * 1e9)
  176. if (previousTimestamp) {
  177. seconds = seconds - previousTimestamp[0]
  178. nanoseconds = nanoseconds - previousTimestamp[1]
  179. if (nanoseconds < 0) {
  180. seconds--
  181. nanoseconds += 1e9
  182. }
  183. }
  184. return [seconds, nanoseconds]
  185. }
  186. var startTime = new Date()
  187. function uptime() {
  188. var currentTime = new Date()
  189. var dif = currentTime - startTime
  190. return dif / 1000
  191. }
  192. export var process = {
  193. nextTick: nextTick,
  194. title: title,
  195. browser: browser,
  196. env: env,
  197. argv: argv,
  198. version: version,
  199. versions: versions,
  200. on: on,
  201. addListener: addListener,
  202. once: once,
  203. off: off,
  204. removeListener: removeListener,
  205. removeAllListeners: removeAllListeners,
  206. emit: emit,
  207. binding: binding,
  208. cwd: cwd,
  209. chdir: chdir,
  210. umask: umask,
  211. hrtime: hrtime,
  212. platform: platform,
  213. release: release,
  214. config: config,
  215. uptime: uptime,
  216. }
  217. // replace process.env.VAR with define
  218. const defines = {}
  219. Object.keys(defines).forEach((key) => {
  220. const segs = key.split('.')
  221. let target = process
  222. for (let i = 0; i < segs.length; i++) {
  223. const seg = segs[i]
  224. if (i === segs.length - 1) {
  225. target[seg] = defines[key]
  226. } else {
  227. target = target[seg] || (target[seg] = {})
  228. }
  229. }
  230. })