salesforce / near-membrane

JavaScript Near Membrane Library that powers Lightning Locker Service
113 stars 27 forks source link

Errors and sourcemaps inside Virtualenvironment #427

Closed academo closed 1 year ago

academo commented 1 year ago

hello.

I am experimenting with near-membrane to isolate plugins executions.

I am using near-membrane-dom to load the plugins code, I create a virtual environment, load the code in a string and use evalute to get the code executed.

  const response = await fetch(pathToPluginJsCode);
  const code = await response.text();
   const env = createVirtualEnvironment(window, {
      // distortions are interceptors to modify the behavior of objects when
      // the code inside the sandbox tries to access them
      distortionCallback(v) {
          // pluginDistortionMap is the distortion definitions, not 
          return pluginDistortionMap.get(v) ?? v;
     },
    endowments: Object.getOwnPropertyDescriptors({
        // plugins normally run inside system js. I'm using this as mechanism to
        // resolve their deps and execute the code
        define(deps: string[], jsModule: () =>  void) => {
             const realDeps = resolveDeps(deps);
             const pluginExport = code.apply(null, realDeps);
             pluginExport.execute(); // the logic is a bit more complex but not relevant for this case.
        },
      }),
);

Generally speaking, is there a way to get what line of code generated the error inside the virtual environment? or maybe there's another way to evaluate the plugin's code that doesn't rely on a string so meaningful errors can be generated? There is little to none documentation and I feel force to ask here

Thanks in advance and amazing work in this library.

academo commented 1 year ago

I kept experimenting with this and the only way I found to make sourcemaps work was to make sure the sourceMappingURL has either an absolute url or relative to the website hostname. If the sourcemap is (as normally is) relative to the script you are trying to execute it won't load it.