init.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import * as path from 'path';
  2. const Module = require('module') as NodeJS.ModuleInternal;
  3. // We modified the original process.argv to let node.js load the
  4. // init.js, we need to restore it here.
  5. process.argv.splice(1, 1);
  6. // Import common settings.
  7. require('@electron/internal/common/init');
  8. // Process command line arguments.
  9. const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line');
  10. // Export node bindings to global.
  11. const { makeRequireFunction } = __non_webpack_require__('internal/modules/helpers');
  12. global.module = new Module('electron/js2c/worker_init');
  13. global.require = makeRequireFunction(global.module);
  14. // See WebWorkerObserver::WorkerScriptReadyForEvaluation.
  15. if ((globalThis as any).blinkfetch) {
  16. const keys = ['fetch', 'Response', 'FormData', 'Request', 'Headers'];
  17. for (const key of keys) {
  18. (globalThis as any)[key] = (globalThis as any)[`blink${key}`];
  19. }
  20. }
  21. // Set the __filename to the path of html file if it is file: protocol.
  22. // NB. 'self' isn't defined in an AudioWorklet.
  23. if (typeof self !== 'undefined' && self.location.protocol === 'file:') {
  24. const pathname = process.platform === 'win32' && self?.location.pathname[0] === '/' ? self?.location.pathname.substr(1) : self?.location.pathname;
  25. global.__filename = path.normalize(decodeURIComponent(pathname));
  26. global.__dirname = path.dirname(global.__filename);
  27. // Set module's filename so relative require can work as expected.
  28. global.module.filename = global.__filename;
  29. // Also search for module under the html file.
  30. global.module.paths = Module._nodeModulePaths(global.__dirname);
  31. } else {
  32. // For backwards compatibility we fake these two paths here
  33. global.__filename = path.join(process.resourcesPath, 'electron.asar', 'worker', 'init.js');
  34. global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'worker');
  35. const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
  36. if (appPath) {
  37. // Search for module under the app directory.
  38. global.module.paths = Module._nodeModulePaths(appPath);
  39. }
  40. }