@builder.io/react 3.0.13-0 → 3.0.13-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
  import { Builder } from '@builder.io/sdk';
3
3
  import { safeDynamicRequire } from './safe-dynamic-require';
4
4
  import { isDebug } from './is-debug';
5
+ import { getIsolateContext, makeFn } from './string-to-function';
5
6
 
6
7
  export const tryEval = (str?: string, data: any = {}, errors?: Error[]): any => {
7
8
  const value = str;
@@ -52,16 +53,16 @@ export const tryEval = (str?: string, data: any = {}, errors?: Error[]): any =>
52
53
  // Below is a hack to get certain code to *only* load in the server build, to not screw with
53
54
  // browser bundler's like rollup and webpack. Our rollup plugin strips these comments only
54
55
  // for the server build
55
- // tslint:disable:comment-format
56
- const { VM } = safeDynamicRequire('vm2');
57
- return new VM({
58
- sandbox: {
59
- ...data,
60
- ...{ state: data },
61
- },
62
- // TODO: convert reutrn to module.exports on server
63
- }).run(value.replace(/(^|;)return /, '$1'));
64
- // tslint:enable:comment-format
56
+ const ivm = safeDynamicRequire('isolated-vm');
57
+ const context = getIsolateContext();
58
+ const fnString = makeFn(str!, useReturn, ['state']);
59
+ const resultStr = context.evalClosureSync(fnString, [new ivm.Reference(data || {})]);
60
+ try {
61
+ // returning objects throw errors in isolated vm, so we stringify it and parse it back
62
+ return JSON.parse(resultStr);
63
+ } catch (_error: any) {
64
+ return resultStr;
65
+ }
65
66
  }
66
67
  } catch (error: any) {
67
68
  if (errors) {