@builder.io/react 3.0.7-3 → 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/CHANGELOG.md +3 -0
  2. package/dist/builder-react-lite.cjs.js +1 -1
  3. package/dist/builder-react-lite.cjs.js.map +1 -1
  4. package/dist/builder-react-lite.esm.js +1 -1
  5. package/dist/builder-react-lite.esm.js.map +1 -1
  6. package/dist/builder-react.browser.js +2 -2
  7. package/dist/builder-react.browser.js.map +1 -1
  8. package/dist/builder-react.cjs.js +1 -1
  9. package/dist/builder-react.cjs.js.map +1 -1
  10. package/dist/builder-react.es5.js +1 -1
  11. package/dist/builder-react.es5.js.map +1 -1
  12. package/dist/builder-react.unpkg.js +2 -2
  13. package/dist/builder-react.unpkg.js.map +1 -1
  14. package/dist/lib/package.json +4 -5
  15. package/dist/lib/rollup.config.js +2 -2
  16. package/dist/lib/rollup.config.js.map +1 -1
  17. package/dist/lib/src/blocks/Video.js +7 -1
  18. package/dist/lib/src/blocks/Video.js.map +1 -1
  19. package/dist/lib/src/components/builder-component.component.js +4 -4
  20. package/dist/lib/src/components/builder-component.component.js.map +1 -1
  21. package/dist/lib/src/functions/is-debug.js +8 -0
  22. package/dist/lib/src/functions/is-debug.js.map +1 -0
  23. package/dist/lib/src/functions/string-to-function.js +21 -23
  24. package/dist/lib/src/functions/string-to-function.js.map +1 -1
  25. package/dist/lib/src/functions/try-eval.js +4 -4
  26. package/dist/lib/src/functions/try-eval.js.map +1 -1
  27. package/dist/types/src/functions/is-debug.d.ts +1 -0
  28. package/package.json +4 -4
  29. package/rollup.config.ts +2 -2
  30. package/src/blocks/Video.tsx +8 -0
  31. package/src/components/builder-component.component.tsx +10 -10
  32. package/src/functions/is-debug.ts +5 -0
  33. package/src/functions/string-to-function.ts +56 -25
  34. package/src/functions/try-eval.tsx +4 -4
@@ -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,16 +135,15 @@ export function stringToFunction(
143
135
  error.stack || error
144
136
  );
145
137
  } else {
146
- // TODO(SK): test with replacing the below with typeof process !== undefined
147
- // if (process?.env?.DEBUG) {
148
- console.debug(
149
- 'Builder custom code error:',
150
- error.message || error,
151
- 'in',
152
- str,
153
- error.stack || error
154
- );
155
- // }
138
+ if (isDebug()) {
139
+ console.debug(
140
+ 'Builder custom code error:',
141
+ error.message || error,
142
+ 'in',
143
+ str,
144
+ error.stack || error
145
+ );
146
+ }
156
147
  }
157
148
  if (errors) {
158
149
  errors.push(error);
@@ -167,3 +158,43 @@ export function stringToFunction(
167
158
 
168
159
  return final;
169
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,10 +71,9 @@ 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
- // TODO(SK): test with replacing the below with typeof process !== undefined
74
- // if (process?.env?.DEBUG) {
75
- console.debug('Builder custom code error:', error.message, 'in', str, error.stack);
76
- // }
74
+ if (isDebug()) {
75
+ console.debug('Builder custom code error:', error.message, 'in', str, error.stack);
76
+ }
77
77
  // Add to req.options.errors to return to client
78
78
  }
79
79
  }