init.js 2.0 KB

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