T
T
tordroid2019-01-30 19:44:36
JavaScript
tordroid, 2019-01-30 19:44:36

How to combine vue-server-renderer and vm2?

Hello, I need to do server rendering for vue.js, while I need to execute untrusted code from .vue components.
In order to be safe, I want to take out server rendering in sandbox vm2
But nothing comes out.
I'm trying to do something like this:

const fs = require('fs');
const express = require('express');
const { createBundleRenderer } = require('vue-server-renderer');
const {NodeVM} = require('vm2');

const app = express();

app.get('*', (req, res) => {
  let bundleRenderer = createBundleRenderer(
    require('./dist/vue-ssr-bundle.json'),
    {
      template: fs.readFileSync('./public/index.html', 'utf-8')
    }
  );

  const vm = new NodeVM({
    sandbox: {
      bundleRenderer,
      res,
      req
    },
    require: {
      external: true,
    }
  })
  vm.run(`
    bundleRenderer
      .renderToStream({url: req.path})
      .pipe(res);
  `, 'server.js')
});

app.listen(8090);

But I am getting an error:
[Vue warn]: Error in beforeCreate hook: "TypeError: 'defineProperty' on proxy: trap returned truish for adding property 'styles'  that is incompatible with the existing property in the proxy target"

Maybe someone can tell me how to do something like this?
Or maybe there is some other way to execute untrusted code on the server?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question