init.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const timers = require('timers')
  2. const util = require('util')
  3. process.atomBinding = require('./atom-binding-setup')(process.binding, process.type)
  4. // setImmediate and process.nextTick makes use of uv_check and uv_prepare to
  5. // run the callbacks, however since we only run uv loop on requests, the
  6. // callbacks wouldn't be called until something else activated the uv loop,
  7. // which would delay the callbacks for arbitrary long time. So we should
  8. // initiatively activate the uv loop once setImmediate and process.nextTick is
  9. // called.
  10. const wrapWithActivateUvLoop = function (func) {
  11. return wrap(func, function (func) {
  12. return function () {
  13. process.activateUvLoop()
  14. return func.apply(this, arguments)
  15. }
  16. })
  17. }
  18. function wrap (func, wrapper) {
  19. const wrapped = wrapper(func)
  20. if (func[util.promisify.custom]) {
  21. wrapped[util.promisify.custom] = wrapper(func[util.promisify.custom])
  22. }
  23. return wrapped
  24. }
  25. process.nextTick = wrapWithActivateUvLoop(process.nextTick)
  26. global.setImmediate = wrapWithActivateUvLoop(timers.setImmediate)
  27. global.clearImmediate = timers.clearImmediate
  28. if (process.type === 'browser') {
  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. global.setTimeout = wrapWithActivateUvLoop(timers.setTimeout)
  34. global.setInterval = wrapWithActivateUvLoop(timers.setInterval)
  35. }
  36. if (process.platform === 'win32') {
  37. // Always returns EOF for stdin stream.
  38. const {Readable} = require('stream')
  39. const stdin = new Readable()
  40. stdin.push(null)
  41. process.__defineGetter__('stdin', function () {
  42. return stdin
  43. })
  44. // If we're running as a Windows Store app, __dirname will be set
  45. // to C:/Program Files/WindowsApps.
  46. //
  47. // Nobody else get's to install there, changing the path is forbidden
  48. // We can therefore say that we're running as appx
  49. if (__dirname.includes('\\WindowsApps\\')) {
  50. process.windowsStore = true
  51. }
  52. }