init.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import timers = require('timers');
  2. import * as util from 'util';
  3. import type * as stream from 'stream';
  4. type AnyFn = (...args: any[]) => any
  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 <T extends AnyFn> (func: T): T {
  12. return wrap(func, function (func) {
  13. return function (this: any, ...args: any[]) {
  14. process.activateUvLoop();
  15. return func.apply(this, args);
  16. };
  17. }) as T;
  18. };
  19. /**
  20. * Casts to any below for func are due to Typescript not supporting symbols
  21. * in index signatures
  22. *
  23. * Refs: https://github.com/Microsoft/TypeScript/issues/1863
  24. */
  25. function wrap <T extends AnyFn> (func: T, wrapper: (fn: AnyFn) => T) {
  26. const wrapped = wrapper(func);
  27. if ((func as any)[util.promisify.custom]) {
  28. (wrapped as any)[util.promisify.custom] = wrapper((func as any)[util.promisify.custom]);
  29. }
  30. return wrapped;
  31. }
  32. // process.nextTick and setImmediate make use of uv_check and uv_prepare to
  33. // run the callbacks, however since we only run uv loop on requests, the
  34. // callbacks wouldn't be called until something else activated the uv loop,
  35. // which would delay the callbacks for arbitrary long time. So we should
  36. // initiatively activate the uv loop once process.nextTick and setImmediate is
  37. // called.
  38. process.nextTick = wrapWithActivateUvLoop(process.nextTick);
  39. global.setImmediate = timers.setImmediate = wrapWithActivateUvLoop(timers.setImmediate);
  40. global.clearImmediate = timers.clearImmediate;
  41. // setTimeout needs to update the polling timeout of the event loop, when
  42. // called under Chromium's event loop the node's event loop won't get a chance
  43. // to update the timeout, so we have to force the node's event loop to
  44. // recalculate the timeout in the process.
  45. timers.setTimeout = wrapWithActivateUvLoop(timers.setTimeout);
  46. timers.setInterval = wrapWithActivateUvLoop(timers.setInterval);
  47. // Update the global version of the timer apis to use the above wrapper
  48. // only in the process that runs node event loop alongside chromium
  49. // event loop. We skip renderer with nodeIntegration here because node globals
  50. // are deleted in these processes, see renderer/init.js for reference.
  51. if (process.type === 'browser' ||
  52. process.type === 'utility') {
  53. global.setTimeout = timers.setTimeout;
  54. global.setInterval = timers.setInterval;
  55. }
  56. if (process.platform === 'win32') {
  57. // Always returns EOF for stdin stream.
  58. const { Readable } = require('stream') as typeof stream;
  59. const stdin = new Readable();
  60. stdin.push(null);
  61. Object.defineProperty(process, 'stdin', {
  62. configurable: false,
  63. enumerable: true,
  64. get () {
  65. return stdin;
  66. }
  67. });
  68. }
  69. const Module = require('module') as NodeJS.ModuleInternal;
  70. // Make a fake Electron module that we will insert into the module cache
  71. const makeElectronModule = (name: string) => {
  72. const electronModule = new Module('electron', null);
  73. electronModule.id = 'electron';
  74. electronModule.loaded = true;
  75. electronModule.filename = name;
  76. Object.defineProperty(electronModule, 'exports', {
  77. get: () => require('electron')
  78. });
  79. Module._cache[name] = electronModule;
  80. };
  81. makeElectronModule('electron');
  82. makeElectronModule('electron/common');
  83. if (process.type === 'browser') {
  84. makeElectronModule('electron/main');
  85. }
  86. if (process.type === 'renderer') {
  87. makeElectronModule('electron/renderer');
  88. }
  89. const originalResolveFilename = Module._resolveFilename;
  90. // 'electron/main', 'electron/renderer' and 'electron/common' are module aliases
  91. // of the 'electron' module for TypeScript purposes, i.e., the types for
  92. // 'electron/main' consist of only main process modules, etc. It is intentional
  93. // that these can be `require()`-ed from both the main process as well as the
  94. // renderer process regardless of the names, they're superficial for TypeScript
  95. // only.
  96. const electronModuleNames = new Set(['electron', 'electron/main', 'electron/renderer', 'electron/common']);
  97. Module._resolveFilename = function (request, parent, isMain, options) {
  98. if (electronModuleNames.has(request)) {
  99. return 'electron';
  100. } else {
  101. return originalResolveFilename(request, parent, isMain, options);
  102. }
  103. };