init.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict'
  2. const timers = require('timers')
  3. const util = require('util')
  4. process.atomBinding = require('@electron/internal/common/atom-binding-setup')(process.binding, process.type)
  5. // setImmediate and process.nextTick makes use of uv_check and uv_prepare to
  6. // run the callbacks, however since we only run uv loop on requests, the
  7. // callbacks wouldn't be called until something else activated the uv loop,
  8. // which would delay the callbacks for arbitrary long time. So we should
  9. // initiatively activate the uv loop once setImmediate and process.nextTick is
  10. // called.
  11. const wrapWithActivateUvLoop = function (func) {
  12. return wrap(func, function (func) {
  13. return function () {
  14. process.activateUvLoop()
  15. return func.apply(this, arguments)
  16. }
  17. })
  18. }
  19. function wrap (func, wrapper) {
  20. const wrapped = wrapper(func)
  21. if (func[util.promisify.custom]) {
  22. wrapped[util.promisify.custom] = wrapper(func[util.promisify.custom])
  23. }
  24. return wrapped
  25. }
  26. process.nextTick = wrapWithActivateUvLoop(process.nextTick)
  27. global.setImmediate = timers.setImmediate = wrapWithActivateUvLoop(timers.setImmediate)
  28. global.clearImmediate = timers.clearImmediate
  29. // setTimeout needs to update the polling timeout of the event loop, when
  30. // called under Chromium's event loop the node's event loop won't get a chance
  31. // to update the timeout, so we have to force the node's event loop to
  32. // recalculate the timeout in browser process.
  33. timers.setTimeout = wrapWithActivateUvLoop(timers.setTimeout)
  34. timers.setInterval = wrapWithActivateUvLoop(timers.setInterval)
  35. // Only override the global setTimeout/setInterval impls in the browser process
  36. if (process.type === 'browser') {
  37. global.setTimeout = timers.setTimeout
  38. global.setInterval = timers.setInterval
  39. }
  40. if (process.platform === 'win32') {
  41. // Always returns EOF for stdin stream.
  42. const { Readable } = require('stream')
  43. const stdin = new Readable()
  44. stdin.push(null)
  45. process.__defineGetter__('stdin', function () {
  46. return stdin
  47. })
  48. }