@builder.io/react 3.0.7 → 3.0.8-0

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.
Files changed (34) hide show
  1. package/dist/builder-react-lite.cjs.js +1 -1
  2. package/dist/builder-react-lite.cjs.js.map +1 -1
  3. package/dist/builder-react-lite.esm.js +1 -1
  4. package/dist/builder-react-lite.esm.js.map +1 -1
  5. package/dist/builder-react.browser.js +2 -2
  6. package/dist/builder-react.browser.js.map +1 -1
  7. package/dist/builder-react.cjs.js +1 -1
  8. package/dist/builder-react.cjs.js.map +1 -1
  9. package/dist/builder-react.es5.js +1 -1
  10. package/dist/builder-react.es5.js.map +1 -1
  11. package/dist/builder-react.unpkg.js +2 -2
  12. package/dist/builder-react.unpkg.js.map +1 -1
  13. package/dist/lib/package.json +3 -3
  14. package/dist/lib/rollup.config.js +2 -2
  15. package/dist/lib/rollup.config.js.map +1 -1
  16. package/dist/lib/src/blocks/Video.js +7 -1
  17. package/dist/lib/src/blocks/Video.js.map +1 -1
  18. package/dist/lib/src/components/builder-component.component.js +2 -3
  19. package/dist/lib/src/components/builder-component.component.js.map +1 -1
  20. package/dist/lib/src/functions/is-debug.js +8 -0
  21. package/dist/lib/src/functions/is-debug.js.map +1 -0
  22. package/dist/lib/src/functions/string-to-function.js +19 -22
  23. package/dist/lib/src/functions/string-to-function.js.map +1 -1
  24. package/dist/lib/src/functions/try-eval.js +2 -3
  25. package/dist/lib/src/functions/try-eval.js.map +1 -1
  26. package/dist/types/src/components/builder-content.component.d.ts +0 -67
  27. package/dist/types/src/functions/is-debug.d.ts +1 -0
  28. package/package.json +5 -4
  29. package/rollup.config.ts +2 -2
  30. package/src/blocks/Video.tsx +8 -0
  31. package/src/components/builder-component.component.tsx +2 -5
  32. package/src/functions/is-debug.ts +5 -0
  33. package/src/functions/string-to-function.ts +48 -20
  34. package/src/functions/try-eval.tsx +2 -5
@@ -1,5 +1,6 @@
1
1
  import { Builder, builder } from '@builder.io/sdk';
2
2
  import { safeDynamicRequire } from './safe-dynamic-require';
3
+ import { isDebug } from './is-debug';
3
4
 
4
5
  const fnCache: { [key: string]: BuilderEvanFunction | undefined } = {};
5
6
 
@@ -117,21 +118,12 @@ export function stringToFunction(
117
118
  // browser bundler's like rollup and webpack. Our rollup plugin strips these comments only
118
119
  // for the server build
119
120
  // TODO: cache these for better performancs with new VmScript
120
- // tslint:disable:comment-format
121
- const { VM } = safeDynamicRequire('vm2') as typeof import('vm2');
122
- const [state, event, _block, _builder, _Device, _update, _Builder, context] = args;
123
-
124
- return new VM({
125
- timeout: 100,
126
- sandbox: {
127
- ...state,
128
- ...{ state },
129
- ...{ context },
130
- ...{ builder: api },
131
- event,
132
- },
133
- }).run(str.replace(/(^|;)return /, '$1'));
134
- // tslint:enable:comment-format
121
+ const isolateContext = getIsolateContext();
122
+ const ivm = safeDynamicRequire('isolated-vm') as typeof import('isolated-vm');
123
+ return isolateContext.evalClosureSync(
124
+ makeFn(str, useReturn),
125
+ args.map(arg => (typeof arg === 'object' ? new ivm.Reference(arg) : null))
126
+ );
135
127
  }
136
128
  } catch (error: any) {
137
129
  if (Builder.isBrowser) {
@@ -143,11 +135,7 @@ export function stringToFunction(
143
135
  error.stack || error
144
136
  );
145
137
  } else {
146
- if (
147
- typeof process !== 'undefined' &&
148
- typeof process.env !== 'undefined' &&
149
- process.env.DEBUG
150
- ) {
138
+ if (isDebug()) {
151
139
  console.debug(
152
140
  'Builder custom code error:',
153
141
  error.message || error,
@@ -170,3 +158,43 @@ export function stringToFunction(
170
158
 
171
159
  return final;
172
160
  }
161
+
162
+ const makeFn = (code: string, useReturn: boolean) => {
163
+ const names = ['state', 'event', 'block', 'builder', 'Device', 'update', 'Builder', 'context'];
164
+ return `
165
+ const refToProxy = (obj) => {
166
+ if (typeof obj !== 'object' || obj === null) {
167
+ return obj;
168
+ }
169
+ return new Proxy({}, {
170
+ get(target, key) {
171
+ const val = obj.getSync(key);
172
+ if (typeof val?.getSync === 'function') {
173
+ return refToProxy(val);
174
+ }
175
+ return val;
176
+ },
177
+ set(target, key, value) {
178
+ obj.setSync(key, value);
179
+ },
180
+ deleteProperty(target, key) {
181
+ obj.deleteSync(key);
182
+ }
183
+ })
184
+ }
185
+ `.concat(names.map((arg, index) => `var ${arg} = refToProxy($${index});`).join('\n')).concat(`
186
+ var ctx = context;
187
+ ${useReturn ? `return (${code});` : code};
188
+ `);
189
+ };
190
+
191
+ const getIsolateContext = () => {
192
+ const _Builder = Builder as any;
193
+ if (_Builder.serverContext) {
194
+ return _Builder.serverContext;
195
+ }
196
+ const ivm = safeDynamicRequire('isolated-vm') as typeof import('isolated-vm');
197
+ const isolate = new ivm.Isolate({ memoryLimit: 128 });
198
+ _Builder.serverContext = isolate.createContextSync();
199
+ return _Builder.serverContext;
200
+ };
@@ -1,6 +1,7 @@
1
1
  'use client';
2
2
  import { Builder } from '@builder.io/sdk';
3
3
  import { safeDynamicRequire } from './safe-dynamic-require';
4
+ import { isDebug } from './is-debug';
4
5
 
5
6
  export const tryEval = (str?: string, data: any = {}, errors?: Error[]): any => {
6
7
  const value = str;
@@ -70,11 +71,7 @@ export const tryEval = (str?: string, data: any = {}, errors?: Error[]): any =>
70
71
  if (Builder.isBrowser) {
71
72
  console.warn('Builder custom code error:', error.message, 'in', str, error.stack);
72
73
  } else {
73
- if (
74
- typeof process !== 'undefined' &&
75
- typeof process.env !== 'undefined' &&
76
- process.env.DEBUG
77
- ) {
74
+ if (isDebug()) {
78
75
  console.debug('Builder custom code error:', error.message, 'in', str, error.stack);
79
76
  }
80
77
  // Add to req.options.errors to return to client