@builder.io/sdk-svelte 2.0.16 → 2.0.21
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.
- package/lib/browser/components/awaiter.svelte +16 -0
- package/lib/browser/components/awaiter.svelte.d.ts +23 -0
- package/lib/browser/components/block/components/interactive-element.svelte +14 -4
- package/lib/browser/components/content/components/enable-editor.svelte +9 -46
- package/lib/browser/components/content/content.svelte +25 -2
- package/lib/browser/components/content/content.svelte.d.ts +2 -2
- package/lib/browser/constants/sdk-version.d.ts +1 -1
- package/lib/browser/constants/sdk-version.js +1 -1
- package/lib/browser/context/component-reference-types.d.ts +17 -0
- package/lib/browser/context/component-reference-types.js +1 -0
- package/lib/browser/context/types.d.ts +2 -1
- package/lib/browser/functions/evaluate/browser-runtime/browser.d.ts +0 -6
- package/lib/browser/functions/evaluate/browser-runtime/browser.js +1 -30
- package/lib/browser/functions/evaluate/edge-runtime/edge-runtime.js +73 -34
- package/lib/browser/functions/evaluate/helpers.d.ts +5 -0
- package/lib/browser/functions/evaluate/helpers.js +29 -0
- package/lib/browser/functions/evaluate/node-runtime/node-runtime.js +1 -1
- package/lib/edge/components/awaiter.svelte +16 -0
- package/lib/edge/components/awaiter.svelte.d.ts +23 -0
- package/lib/edge/components/block/components/interactive-element.svelte +14 -4
- package/lib/edge/components/content/components/enable-editor.svelte +9 -46
- package/lib/edge/components/content/content.svelte +25 -2
- package/lib/edge/components/content/content.svelte.d.ts +2 -2
- package/lib/edge/constants/sdk-version.d.ts +1 -1
- package/lib/edge/constants/sdk-version.js +1 -1
- package/lib/edge/context/component-reference-types.d.ts +17 -0
- package/lib/edge/context/component-reference-types.js +1 -0
- package/lib/edge/context/types.d.ts +2 -1
- package/lib/edge/functions/evaluate/browser-runtime/browser.d.ts +0 -6
- package/lib/edge/functions/evaluate/browser-runtime/browser.js +1 -30
- package/lib/edge/functions/evaluate/edge-runtime/edge-runtime.js +73 -34
- package/lib/edge/functions/evaluate/helpers.d.ts +5 -0
- package/lib/edge/functions/evaluate/helpers.js +29 -0
- package/lib/edge/functions/evaluate/node-runtime/node-runtime.js +1 -1
- package/lib/node/components/awaiter.svelte +16 -0
- package/lib/node/components/awaiter.svelte.d.ts +23 -0
- package/lib/node/components/block/components/interactive-element.svelte +14 -4
- package/lib/node/components/content/components/enable-editor.svelte +9 -46
- package/lib/node/components/content/content.svelte +25 -2
- package/lib/node/components/content/content.svelte.d.ts +2 -2
- package/lib/node/constants/sdk-version.d.ts +1 -1
- package/lib/node/constants/sdk-version.js +1 -1
- package/lib/node/context/component-reference-types.d.ts +17 -0
- package/lib/node/context/component-reference-types.js +1 -0
- package/lib/node/context/types.d.ts +2 -1
- package/lib/node/functions/evaluate/browser-runtime/browser.d.ts +0 -6
- package/lib/node/functions/evaluate/browser-runtime/browser.js +1 -30
- package/lib/node/functions/evaluate/edge-runtime/edge-runtime.js +73 -34
- package/lib/node/functions/evaluate/helpers.d.ts +5 -0
- package/lib/node/functions/evaluate/helpers.js +29 -0
- package/lib/node/functions/evaluate/node-runtime/node-runtime.js +1 -1
- package/package.json +1 -1
- package/src/components/awaiter.svelte +20 -0
- package/src/components/block/components/interactive-element.svelte +14 -4
- package/src/components/content/components/enable-editor.svelte +9 -52
- package/src/components/content/content.svelte +28 -2
- package/src/constants/sdk-version.ts +1 -1
- package/src/context/component-reference-types.ts +18 -0
- package/src/context/types.ts +2 -1
- package/src/functions/evaluate/browser-runtime/browser.ts +1 -39
- package/src/functions/evaluate/edge-runtime/edge-runtime.ts +73 -35
- package/src/functions/evaluate/helpers.ts +37 -0
- package/src/functions/evaluate/node-runtime/node-runtime.ts +1 -1
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { BuilderRenderState } from '../../../context/types';
|
|
2
1
|
import type { ExecutorArgs } from '../helpers';
|
|
3
|
-
import { getFunctionArguments } from '../helpers';
|
|
2
|
+
import { flattenState, getFunctionArguments } from '../helpers';
|
|
4
3
|
export const runInBrowser = ({
|
|
5
4
|
code,
|
|
6
5
|
builder,
|
|
@@ -21,41 +20,4 @@ export const runInBrowser = ({
|
|
|
21
20
|
})
|
|
22
21
|
});
|
|
23
22
|
return new Function(...functionArgs.map(([name]) => name), code)(...functionArgs.map(([, value]) => value));
|
|
24
|
-
};
|
|
25
|
-
export function flattenState({
|
|
26
|
-
rootState,
|
|
27
|
-
localState,
|
|
28
|
-
rootSetState
|
|
29
|
-
}: {
|
|
30
|
-
rootState: Record<string | symbol, any>;
|
|
31
|
-
localState: Record<string | symbol, any> | undefined;
|
|
32
|
-
rootSetState: ((rootState: BuilderRenderState) => void) | undefined;
|
|
33
|
-
}): BuilderRenderState {
|
|
34
|
-
return new Proxy(rootState, {
|
|
35
|
-
get: (target, prop) => {
|
|
36
|
-
if (localState && prop in localState) {
|
|
37
|
-
return localState[prop];
|
|
38
|
-
}
|
|
39
|
-
const val = target[prop];
|
|
40
|
-
if (typeof val === 'object' && val !== null) {
|
|
41
|
-
return flattenState({
|
|
42
|
-
rootState: val,
|
|
43
|
-
localState: undefined,
|
|
44
|
-
rootSetState: rootSetState ? subState => {
|
|
45
|
-
target[prop] = subState;
|
|
46
|
-
rootSetState(target);
|
|
47
|
-
} : undefined
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
return val;
|
|
51
|
-
},
|
|
52
|
-
set: (target, prop, value) => {
|
|
53
|
-
if (localState && prop in localState) {
|
|
54
|
-
throw new Error('Writing to local state is not allowed as it is read-only.');
|
|
55
|
-
}
|
|
56
|
-
target[prop] = value;
|
|
57
|
-
rootSetState?.(target);
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
23
|
}
|
|
@@ -1,26 +1,66 @@
|
|
|
1
1
|
import { logger } from '../../../helpers/logger';
|
|
2
|
-
import { set } from '../../set';
|
|
3
2
|
import type { ExecutorArgs } from '../helpers';
|
|
4
|
-
import { getFunctionArguments } from '../helpers';
|
|
3
|
+
import { flattenState, getFunctionArguments } from '../helpers';
|
|
5
4
|
import Interpreter from './acorn-interpreter';
|
|
6
|
-
const processCode = (code: string) => {
|
|
7
|
-
return code.split('\n').map(line => {
|
|
8
|
-
const trimmed = line.trim();
|
|
9
|
-
|
|
10
|
-
// this async wrapper doesn't work in JS-interpreter, so we drop it.
|
|
11
|
-
if (line.includes('__awaiter')) return undefined;
|
|
12
5
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
6
|
+
/**
|
|
7
|
+
* https://stackoverflow.com/a/46503625
|
|
8
|
+
*/
|
|
9
|
+
function patchInterpreter() {
|
|
10
|
+
const originalGetProperty = Interpreter.prototype.getProperty;
|
|
11
|
+
const originalSetProperty = Interpreter.prototype.setProperty;
|
|
12
|
+
function newGetProperty(this: typeof Interpreter, obj: any, name: any) {
|
|
13
|
+
if (obj == null || !obj._connected) {
|
|
14
|
+
return originalGetProperty.call(this, obj, name);
|
|
15
|
+
}
|
|
16
|
+
const value = obj._connected[name];
|
|
17
|
+
if (Array.isArray(value)) {
|
|
18
|
+
return this.nativeToPseudo(value);
|
|
19
|
+
}
|
|
20
|
+
if (typeof value === 'object') {
|
|
21
|
+
// if the value is an object itself, create another connected object
|
|
22
|
+
return this.createConnectedObject(value);
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function newSetProperty(this: typeof Interpreter, obj: any, name: any, value: any, opt_descriptor: any) {
|
|
27
|
+
if (obj == null || !obj._connected) {
|
|
28
|
+
return originalSetProperty.call(this, obj, name, value, opt_descriptor);
|
|
29
|
+
}
|
|
30
|
+
obj._connected[name] = this.pseudoToNative(value);
|
|
31
|
+
}
|
|
32
|
+
const getKeys: string[] = [];
|
|
33
|
+
const setKeys: string[] = [];
|
|
34
|
+
for (const key of Object.keys(Interpreter.prototype)) {
|
|
35
|
+
if (Interpreter.prototype[key] === originalGetProperty) {
|
|
36
|
+
getKeys.push(key);
|
|
37
|
+
}
|
|
38
|
+
if (Interpreter.prototype[key] === originalSetProperty) {
|
|
39
|
+
setKeys.push(key);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
for (const key of getKeys) {
|
|
43
|
+
Interpreter.prototype[key] = newGetProperty;
|
|
44
|
+
}
|
|
45
|
+
for (const key of setKeys) {
|
|
46
|
+
Interpreter.prototype[key] = newSetProperty;
|
|
47
|
+
}
|
|
48
|
+
Interpreter.prototype.createConnectedObject = function (obj: any) {
|
|
49
|
+
const connectedObject = this.createObject(this.OBJECT);
|
|
50
|
+
connectedObject._connected = obj;
|
|
51
|
+
return connectedObject;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
patchInterpreter();
|
|
55
|
+
const processCode = (code: string) => {
|
|
56
|
+
return code
|
|
57
|
+
// strip async/await polyfill: remove anything before 'function main()'
|
|
58
|
+
.replace(/^.*?function main\(\)/, `
|
|
59
|
+
var __awaiter = function (e, t, n, r) {return r()},
|
|
60
|
+
__generator = function (e, t) { return t() };
|
|
61
|
+
function main()`)
|
|
62
|
+
// replace ?. with .
|
|
63
|
+
.replace(/\?\./g, '.');
|
|
24
64
|
};
|
|
25
65
|
const getJSONValName = (val: string) => val + 'JSON';
|
|
26
66
|
export const runInEdge = ({
|
|
@@ -32,10 +72,11 @@ export const runInEdge = ({
|
|
|
32
72
|
rootSetState,
|
|
33
73
|
code
|
|
34
74
|
}: ExecutorArgs) => {
|
|
35
|
-
const state = {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
75
|
+
const state = flattenState({
|
|
76
|
+
rootState,
|
|
77
|
+
localState,
|
|
78
|
+
rootSetState
|
|
79
|
+
});
|
|
39
80
|
const properties = getFunctionArguments({
|
|
40
81
|
builder,
|
|
41
82
|
context,
|
|
@@ -48,6 +89,9 @@ export const runInEdge = ({
|
|
|
48
89
|
*/
|
|
49
90
|
const prependedCode = properties.map(([key]) => {
|
|
50
91
|
const jsonValName = getJSONValName(key);
|
|
92
|
+
if (key === 'state') {
|
|
93
|
+
return ``;
|
|
94
|
+
}
|
|
51
95
|
return `var ${key} = ${jsonValName} === undefined ? undefined : JSON.parse(${jsonValName});`;
|
|
52
96
|
}).join('\n');
|
|
53
97
|
const cleanedCode = processCode(code);
|
|
@@ -63,24 +107,18 @@ function theFunction() {
|
|
|
63
107
|
}
|
|
64
108
|
theFunction();
|
|
65
109
|
`;
|
|
66
|
-
const setRootState = (prop: string, value: any) => {
|
|
67
|
-
const newState = set(state, prop, value);
|
|
68
|
-
rootSetState?.(newState);
|
|
69
|
-
};
|
|
70
110
|
const initFunc = function (interpreter: any, globalObject: any) {
|
|
71
111
|
/**
|
|
72
112
|
* serialize all function args to JSON strings
|
|
73
113
|
*/
|
|
74
114
|
properties.forEach(([key, val]) => {
|
|
75
|
-
|
|
76
|
-
|
|
115
|
+
if (key === 'state') {
|
|
116
|
+
interpreter.setProperty(globalObject, key, interpreter.createConnectedObject(val), interpreter.READONLY_DESCRIPTOR);
|
|
117
|
+
} else {
|
|
118
|
+
const jsonVal = JSON.stringify(val);
|
|
119
|
+
interpreter.setProperty(globalObject, getJSONValName(key), jsonVal);
|
|
120
|
+
}
|
|
77
121
|
});
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Add a JavaScript function "setRootState" to the interpreter's global object, that will be called whenever a
|
|
81
|
-
* state property is set. This function will update the state object.
|
|
82
|
-
*/
|
|
83
|
-
interpreter.setProperty(globalObject, 'setRootState', interpreter.createNativeFunction(setRootState));
|
|
84
122
|
};
|
|
85
123
|
const myInterpreter = new Interpreter(transformed, initFunc);
|
|
86
124
|
myInterpreter.run();
|
|
@@ -53,4 +53,41 @@ export const parseCode = (code: string, {
|
|
|
53
53
|
isExpression && !(code.includes(';') || code.includes(' return ') || code.trim().startsWith('return '));
|
|
54
54
|
const useCode = useReturn ? `return (${code});` : code;
|
|
55
55
|
return useCode;
|
|
56
|
+
};
|
|
57
|
+
export function flattenState({
|
|
58
|
+
rootState,
|
|
59
|
+
localState,
|
|
60
|
+
rootSetState
|
|
61
|
+
}: {
|
|
62
|
+
rootState: Record<string | symbol, any>;
|
|
63
|
+
localState: Record<string | symbol, any> | undefined;
|
|
64
|
+
rootSetState: ((rootState: BuilderRenderState) => void) | undefined;
|
|
65
|
+
}): BuilderRenderState {
|
|
66
|
+
return new Proxy(rootState, {
|
|
67
|
+
get: (target, prop) => {
|
|
68
|
+
if (localState && prop in localState) {
|
|
69
|
+
return localState[prop];
|
|
70
|
+
}
|
|
71
|
+
const val = target[prop];
|
|
72
|
+
if (typeof val === 'object' && val !== null) {
|
|
73
|
+
return flattenState({
|
|
74
|
+
rootState: val,
|
|
75
|
+
localState: undefined,
|
|
76
|
+
rootSetState: rootSetState ? subState => {
|
|
77
|
+
target[prop] = subState;
|
|
78
|
+
rootSetState(target);
|
|
79
|
+
} : undefined
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return val;
|
|
83
|
+
},
|
|
84
|
+
set: (target, prop, value) => {
|
|
85
|
+
if (localState && prop in localState) {
|
|
86
|
+
throw new Error('Writing to local state is not allowed as it is read-only.');
|
|
87
|
+
}
|
|
88
|
+
target[prop] = value;
|
|
89
|
+
rootSetState?.(target);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
56
93
|
}
|
|
@@ -80,7 +80,7 @@ export const setIvm = (ivm: IsolatedVMImport, options: IsolateOptions = {}) => {
|
|
|
80
80
|
};
|
|
81
81
|
|
|
82
82
|
// only mention the script for SDKs that have it.
|
|
83
|
-
const SHOULD_MENTION_INITIALIZE_SCRIPT = SDK_NAME === '@builder.io/sdk-react-nextjs' || SDK_NAME === '@builder.io/sdk-react' || SDK_NAME === '@builder.io/sdk-qwik';
|
|
83
|
+
const SHOULD_MENTION_INITIALIZE_SCRIPT = SDK_NAME === '@builder.io/sdk-react-nextjs' || SDK_NAME === '@builder.io/sdk-react' || SDK_NAME === '@builder.io/sdk-qwik' || SDK_NAME === '@builder.io/sdk-vue';
|
|
84
84
|
const getIvm = (): IsolatedVMImport => {
|
|
85
85
|
try {
|
|
86
86
|
if (IVM_INSTANCE) return IVM_INSTANCE;
|