From 4df0eec70aaba2a8bd256de58c60460c9e9ada72 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sat, 30 Nov 2024 20:00:41 -0800 Subject: [PATCH] server: esmodule cleanups --- server/src/es/es-eval.ts | 2 +- server/src/plugin/plugin-remote-worker.ts | 32 +++++-------------- server/src/plugin/runtime/node-fork-worker.ts | 7 ++-- .../src/plugin/runtime/node-thread-worker.ts | 5 ++- 4 files changed, 16 insertions(+), 30 deletions(-) diff --git a/server/src/es/es-eval.ts b/server/src/es/es-eval.ts index 39b989842..e5426522f 100644 --- a/server/src/es/es-eval.ts +++ b/server/src/es/es-eval.ts @@ -1,5 +1,5 @@ export async function eseval(script: string) { // const dataUrl = `data:text/javascript,${encodeURIComponent(script)}`; - const module = await import(script); + const module = await import(script); return module; } diff --git a/server/src/plugin/plugin-remote-worker.ts b/server/src/plugin/plugin-remote-worker.ts index 30ed7fc72..91fd2274e 100644 --- a/server/src/plugin/plugin-remote-worker.ts +++ b/server/src/plugin/plugin-remote-worker.ts @@ -1,11 +1,13 @@ import { ForkWorker, ScryptedStatic, SystemManager } from '@scrypted/types'; import child_process from 'child_process'; import fs from 'fs'; +import { createRequire } from 'module'; import path from 'path'; import { install as installSourceMapSupport } from 'source-map-support'; import worker_threads from 'worker_threads'; import { utilizesClusterForkWorker } from '../cluster/cluster-labels'; import { setupCluster } from '../cluster/cluster-setup'; +import { eseval } from '../es/es-eval'; import { RpcMessage, RpcPeer } from '../rpc'; import { evalLocal } from '../rpc-peer-eval'; import { ClusterManagerImpl } from '../scrypted-cluster-main'; @@ -24,7 +26,6 @@ import { NodeThreadWorker } from './runtime/node-thread-worker'; import { prepareZip } from './runtime/node-worker-common'; import { getBuiltinRuntimeHosts } from './runtime/runtime-host'; import { RuntimeWorker, RuntimeWorkerOptions } from './runtime/runtime-worker'; -import { eseval } from '../es/es-eval'; const serverVersion = require('../../package.json').version; @@ -135,30 +136,12 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe const pluginConsole = getPluginConsole?.(); params.console = pluginConsole; + const pnp = getPluginNodePath(pluginId); + const pnpNodeModules = path.join(pnp, 'node_modules'); pluginConsole?.log('node modules', pnp); - params.require = (name: string) => { - if (name === 'realfs') { - return require('fs'); - } - try { - if (name.startsWith('.') && unzippedPath) { - try { - const c = path.join(unzippedPath, name); - const module = require(c); - return module; - } - catch (e) { - } - } - const module = require(name); - return module; - } - catch (e) { - const c = path.join(pnp, 'node_modules', name); - return require(c); - } - }; + params.require = createRequire(pnpNodeModules); + params.module = { exports: {}, }; @@ -360,8 +343,9 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe } try { + const isModule = packageJson.type === 'module'; const filename = zipOptions?.debug ? pluginMainNodeJs : pluginIdMainNodeJs; - if (packageJson.type === 'module') { + if (isModule) { const p = path.join(unzippedPath, mainNodejs); const module = await eseval(p); params.module.exports = module; diff --git a/server/src/plugin/runtime/node-fork-worker.ts b/server/src/plugin/runtime/node-fork-worker.ts index 9c5b1c7e4..5da809a72 100644 --- a/server/src/plugin/runtime/node-fork-worker.ts +++ b/server/src/plugin/runtime/node-fork-worker.ts @@ -1,10 +1,11 @@ import child_process from 'child_process'; import net from "net"; +import path from 'path'; +import { getScryptedClusterMode } from '../../cluster/cluster-setup'; import { RpcMessage, RpcPeer } from "../../rpc"; import { SidebandSocketSerializer } from "../socket-serializer"; import { ChildProcessWorker } from "./child-process-worker"; import { RuntimeWorkerOptions } from "./runtime-worker"; -import { getScryptedClusterMode } from '../../cluster/cluster-setup'; export const NODE_PLUGIN_CHILD_PROCESS = 'child'; export const NODE_PLUGIN_FORK_PROCESS = 'fork'; @@ -49,7 +50,9 @@ export class NodeForkWorker extends ChildProcessWorker { this.worker = child_process.fork(mainFilename, args, { stdio: ['pipe', 'pipe', 'pipe', 'ipc'], - env: Object.assign({}, process.env, env), + env: Object.assign({}, process.env, env, { + NODE_PATH: path.resolve(__dirname, '..', '..', '..', 'node_modules'), + }), serialization: 'advanced', execArgv, }); diff --git a/server/src/plugin/runtime/node-thread-worker.ts b/server/src/plugin/runtime/node-thread-worker.ts index 2b33f3454..71e9bd4d9 100644 --- a/server/src/plugin/runtime/node-thread-worker.ts +++ b/server/src/plugin/runtime/node-thread-worker.ts @@ -1,10 +1,9 @@ -import worker_threads from "worker_threads"; import { EventEmitter } from "events"; +import worker_threads from "worker_threads"; import { RpcMessage, RpcPeer, RpcSerializer } from "../../rpc"; -import { RuntimeWorker, RuntimeWorkerOptions } from "./runtime-worker"; import { BufferSerializer } from '../../rpc-buffer-serializer'; import { NODE_PLUGIN_THREAD_PROCESS } from "./node-fork-worker"; - +import { RuntimeWorker, RuntimeWorkerOptions } from "./runtime-worker"; class BufferTransfer implements RpcSerializer { bufferSerializer = new BufferSerializer();