init.js 2.0 KB

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