MCPcopy
hub / github.com/alibaba/lightproxy / spawnModule

Function spawnModule

src/main/api.ts:26–96  ·  view source on GitHub ↗
(props: any)

Source from the content-addressed store, hash-verified

24}
25
26async function spawnModule(props: any) {
27 const { moduleId, env = {} } = props as SwpanModuleProp;
28 logger.info('spawn module', moduleId);
29 const boardcastPort = (await BoardcastManager.getInstance()).getPort();
30
31 logger.info('boardcast port', boardcastPort);
32
33 const nodeModulePath = path.join(LIGHTPROXY_FILES_DIR, `/node/node_modules/`);
34 const modulePath = encodeURIComponent(path.join(nodeModulePath, `${moduleId}/index.js`));
35
36 const nodeExe = SYSTEM_IS_MACOS
37 ? path.join(LIGHTPROXY_FILES_DIR, './node/node-mac')
38 : path.join(LIGHTPROXY_FILES_DIR, './node/node-win.exe');
39
40 const nodeScript = `
41const cp = require('child_process');
42const originSpwan = cp.spawn;
43
44// @ts-ignore
45cp.spawn = function(cmd, argv, options) {
46 if (cmd === 'node' || cmd === 'node.exe') {
47 cmd = process.execPath;
48 options = options || {};
49 options.env = options.env || {};
50 options.env.ELECTRON_RUN_AS_NODE = '1';
51 }
52
53 return originSpwan.call(this, cmd, argv, options);
54};
55require(decodeURIComponent('${modulePath}'));`;
56 const startProcess = () => {
57 const child = spwan(
58 nodeExe,
59 [
60 '-e',
61 `const code = decodeURIComponent("${encodeURIComponent(nodeScript)}");console.log(code);eval(code);`,
62 '--tls-min-v1.0',
63 '--max-http-header-size=256000',
64 '--http-parser=legacy',
65 ],
66 {
67 env: {
68 ...process.env,
69 ...env,
70 ELECTRON_RUN_MODULE: moduleId,
71 LIGHTPROXY_BOARDCASR_PORT: boardcastPort,
72 USER_DATA: app.getPath('appData'),
73 NODE_PATH: nodeModulePath,
74 ELECTRON_RUN_AS_NODE: 1,
75 NODE_SKIP_PLATFORM_CHECK: 1,
76 },
77 },
78 );
79
80 exitHook(() => {
81 child?.kill();
82 });
83

Callers

nothing calls this directly

Calls 3

startProcessFunction · 0.85
getPortMethod · 0.80
getInstanceMethod · 0.80

Tested by

no test coverage detected