init.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { ParentPort } from '@electron/internal/utility/parent-port';
  2. const Module = require('module');
  3. const v8Util = process._linkedBinding('electron_common_v8_util');
  4. const entryScript: string = v8Util.getHiddenValue(process, '_serviceStartupScript');
  5. // We modified the original process.argv to let node.js load the init.js,
  6. // we need to restore it here.
  7. process.argv.splice(1, 1, entryScript);
  8. // Clear search paths.
  9. require('../common/reset-search-paths');
  10. // Import common settings.
  11. require('@electron/internal/common/init');
  12. const parentPort: ParentPort = new ParentPort();
  13. Object.defineProperty(process, 'parentPort', {
  14. enumerable: true,
  15. writable: false,
  16. value: parentPort
  17. });
  18. // Based on third_party/electron_node/lib/internal/worker/io.js
  19. parentPort.on('newListener', (name: string) => {
  20. if (name === 'message' && parentPort.listenerCount('message') === 0) {
  21. parentPort.start();
  22. }
  23. });
  24. parentPort.on('removeListener', (name: string) => {
  25. if (name === 'message' && parentPort.listenerCount('message') === 0) {
  26. parentPort.pause();
  27. }
  28. });
  29. // Finally load entry script.
  30. process._firstFileName = Module._resolveFilename(entryScript, null, false);
  31. Module._load(entryScript, Module, true);