nodeJS make code run in isolated vm context Or make a specific global variable

1.2k Views Asked by At

In a project that I'm working on now, I need to have different context for each client that is connected to TCP server... Code that I will run on VM in the isolated context

 var sbox = { Client : require("./Client"),  server: {clients:[]}, socket:socket };
      vm.createContext(sbox)
      vm.runInNewContext(`
      let client = new Client(socket);
      server.clients.push(client);
      `, sbox)

To make it a little bit more clear what I need to do:

-The main component of the app is a core and every single part of the ap extends core...

-The core needs to have access to session object ( right now as a parameter )

-On each connection that appears to the server, I need to create a new session that will handle Client data like login, socket, sessionID and so on... right now I have to pas session by a parameter to every single new instance that inherits from core...

OR

somehow create a global variable that will live only in the instance of new Client and it would handle session object that should be recursively accessible for everything that I would invoke inside client instance.

//client module
const d = require('./sth');
function client(socket){
    glob_ses = new Clien_stession(socket);
    let a = new b();
    b.start();
}
// end of module 

//sth module
function client(socket){
    let login = glob_ses.getLogin
}
client.prototype.start() = ()=>{
    //start app that makes use of the login or some other staff that session will handle
}
// end of module 


server.connection = net.createServer((socket) => {
// code
const client = require('./client');
let C = new client(socket)
server.client.push({clientAdr:`${socket.remoteAddress}:${socket.remotePort}`,clientIsn: C});
//code
});
0

There are 0 best solutions below