init.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const timers = require('timers')
  2. process.atomBinding = require('./atom-binding-setup')(process.binding, process.type)
  3. // setImmediate and process.nextTick makes use of uv_check and uv_prepare to
  4. // run the callbacks, however since we only run uv loop on requests, the
  5. // callbacks wouldn't be called until something else activated the uv loop,
  6. // which would delay the callbacks for arbitrary long time. So we should
  7. // initiatively activate the uv loop once setImmediate and process.nextTick is
  8. // called.
  9. var wrapWithActivateUvLoop = function (func) {
  10. return function () {
  11. process.activateUvLoop()
  12. return func.apply(this, arguments)
  13. }
  14. }
  15. process.nextTick = wrapWithActivateUvLoop(process.nextTick)
  16. global.setImmediate = wrapWithActivateUvLoop(timers.setImmediate)
  17. global.clearImmediate = timers.clearImmediate
  18. if (process.type === 'browser') {
  19. // setTimeout needs to update the polling timeout of the event loop, when
  20. // called under Chromium's event loop the node's event loop won't get a chance
  21. // to update the timeout, so we have to force the node's event loop to
  22. // recalculate the timeout in browser process.
  23. global.setTimeout = wrapWithActivateUvLoop(timers.setTimeout)
  24. global.setInterval = wrapWithActivateUvLoop(timers.setInterval)
  25. }
  26. if (process.platform === 'win32') {
  27. // Always returns EOF for stdin stream.
  28. const {Readable} = require('stream')
  29. const stdin = new Readable()
  30. stdin.push(null)
  31. process.__defineGetter__('stdin', function () {
  32. return stdin
  33. })
  34. // If we're running as a Windows Store app, __dirname will be set
  35. // to C:/Program Files/WindowsApps.
  36. //
  37. // Nobody else get's to install there, changing the path is forbidden
  38. // We can therefore say that we're running as appx
  39. if (__dirname.includes('\\WindowsApps\\')) {
  40. process.windowsStore = true
  41. }
  42. }