init.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import * as util from 'util';
  2. const timers = require('timers');
  3. type AnyFn = (...args: any[]) => any
  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 <T extends AnyFn> (func: T): T {
  11. return wrap(func, function (func) {
  12. return function (this: any, ...args: any[]) {
  13. process.activateUvLoop();
  14. return func.apply(this, args);
  15. };
  16. }) as T;
  17. };
  18. /**
  19. * Casts to any below for func are due to Typescript not supporting symbols
  20. * in index signatures
  21. *
  22. * Refs: https://github.com/Microsoft/TypeScript/issues/1863
  23. */
  24. function wrap <T extends AnyFn> (func: T, wrapper: (fn: AnyFn) => T) {
  25. const wrapped = wrapper(func);
  26. if ((func as any)[util.promisify.custom]) {
  27. (wrapped as any)[util.promisify.custom] = wrapper((func as any)[util.promisify.custom]);
  28. }
  29. return wrapped;
  30. }
  31. // process.nextTick and setImmediate make use of uv_check and uv_prepare to
  32. // run the callbacks, however since we only run uv loop on requests, the
  33. // callbacks wouldn't be called until something else activated the uv loop,
  34. // which would delay the callbacks for arbitrary long time. So we should
  35. // initiatively activate the uv loop once process.nextTick and setImmediate is
  36. // called.
  37. process.nextTick = wrapWithActivateUvLoop(process.nextTick);
  38. global.setImmediate = timers.setImmediate = wrapWithActivateUvLoop(timers.setImmediate);
  39. global.clearImmediate = timers.clearImmediate;
  40. // setTimeout needs to update the polling timeout of the event loop, when
  41. // called under Chromium's event loop the node's event loop won't get a chance
  42. // to update the timeout, so we have to force the node's event loop to
  43. // recalculate the timeout in the process.
  44. timers.setTimeout = wrapWithActivateUvLoop(timers.setTimeout);
  45. timers.setInterval = wrapWithActivateUvLoop(timers.setInterval);
  46. // Update the global version of the timer apis to use the above wrapper
  47. // only in the process that runs node event loop alongside chromium
  48. // event loop. We skip renderer with nodeIntegration here because node globals
  49. // are deleted in these processes, see renderer/init.js for reference.
  50. if (process.type === 'browser' ||
  51. process.type === 'utility') {
  52. global.setTimeout = timers.setTimeout;
  53. global.setInterval = timers.setInterval;
  54. }
  55. if (process.platform === 'win32') {
  56. // Always returns EOF for stdin stream.
  57. const { Readable } = require('stream');
  58. const stdin = new Readable();
  59. stdin.push(null);
  60. Object.defineProperty(process, 'stdin', {
  61. configurable: false,
  62. enumerable: true,
  63. get () {
  64. return stdin;
  65. }
  66. });
  67. }