@builder.io/sdk-svelte 1.0.20 → 1.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/constants/sdk-version.d.ts +1 -1
- package/lib/browser/constants/sdk-version.js +1 -1
- package/lib/browser/functions/evaluate/node-runtime/init.d.ts +10 -0
- package/lib/browser/functions/evaluate/node-runtime/init.js +26 -0
- package/lib/browser/functions/evaluate/node-runtime/node-runtime.d.ts +6 -0
- package/lib/browser/functions/evaluate/node-runtime/node-runtime.js +32 -6
- package/lib/browser/functions/get-content/types.d.ts +3 -3
- package/lib/browser/helpers/logger.d.ts +1 -0
- package/lib/browser/helpers/logger.js +1 -1
- package/lib/edge/constants/sdk-version.d.ts +1 -1
- package/lib/edge/constants/sdk-version.js +1 -1
- package/lib/edge/functions/evaluate/node-runtime/init.d.ts +10 -0
- package/lib/edge/functions/evaluate/node-runtime/init.js +26 -0
- package/lib/edge/functions/evaluate/node-runtime/node-runtime.d.ts +6 -0
- package/lib/edge/functions/evaluate/node-runtime/node-runtime.js +32 -6
- package/lib/edge/functions/get-content/types.d.ts +3 -3
- package/lib/edge/helpers/logger.d.ts +1 -0
- package/lib/edge/helpers/logger.js +1 -1
- package/lib/node/constants/sdk-version.d.ts +1 -1
- package/lib/node/constants/sdk-version.js +1 -1
- package/lib/node/functions/evaluate/node-runtime/init.d.ts +10 -0
- package/lib/node/functions/evaluate/node-runtime/init.js +26 -0
- package/lib/node/functions/evaluate/node-runtime/node-runtime.d.ts +6 -0
- package/lib/node/functions/evaluate/node-runtime/node-runtime.js +32 -6
- package/lib/node/functions/get-content/types.d.ts +3 -3
- package/lib/node/helpers/logger.d.ts +1 -0
- package/lib/node/helpers/logger.js +1 -1
- package/package.json +1 -1
- package/src/constants/sdk-version.ts +1 -1
- package/src/functions/evaluate/node-runtime/init.ts +27 -0
- package/src/functions/evaluate/node-runtime/node-runtime.ts +30 -6
- package/src/functions/get-content/types.ts +3 -3
- package/src/helpers/logger.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "1.0.
|
|
1
|
+
export declare const SDK_VERSION = "1.0.21";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "1.0.
|
|
1
|
+
export const SDK_VERSION = "1.0.21";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function initializes the SDK on a Node server. It handles importing the
|
|
3
|
+
* `isolated-vm` package which is needed for dynamic bindings.
|
|
4
|
+
*
|
|
5
|
+
* NOTE: this function cannot be called on the client. You must call this function
|
|
6
|
+
* from a server-only location, such as:
|
|
7
|
+
* - The NextJS Pages router's `_document.tsx`
|
|
8
|
+
* - Your Remix route's `loader`
|
|
9
|
+
*/
|
|
10
|
+
export declare const initializeNodeRuntime: () => void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file:
|
|
3
|
+
* - imports `isolated-vm`, which can only be made from a file that never runs
|
|
4
|
+
* on the client (e.g. Next's `_document.tsx`)
|
|
5
|
+
* - stores the ivm instance in a global variable using `setIvm`
|
|
6
|
+
*
|
|
7
|
+
* This is needed for when bundlers/meta-frameworks are not able to reliably
|
|
8
|
+
* import the `isolated-vm` package using our `safeDynamicRequire` trick.
|
|
9
|
+
*
|
|
10
|
+
* The `isolated-vm` import must exist in this separate file, or it will end up
|
|
11
|
+
* in the SDK's main entry point, causing errors for users.
|
|
12
|
+
*/
|
|
13
|
+
import ivm from 'isolated-vm';
|
|
14
|
+
import { setIvm } from './node-runtime';
|
|
15
|
+
/**
|
|
16
|
+
* This function initializes the SDK on a Node server. It handles importing the
|
|
17
|
+
* `isolated-vm` package which is needed for dynamic bindings.
|
|
18
|
+
*
|
|
19
|
+
* NOTE: this function cannot be called on the client. You must call this function
|
|
20
|
+
* from a server-only location, such as:
|
|
21
|
+
* - The NextJS Pages router's `_document.tsx`
|
|
22
|
+
* - Your Remix route's `loader`
|
|
23
|
+
*/
|
|
24
|
+
export const initializeNodeRuntime = () => {
|
|
25
|
+
setIvm(ivm);
|
|
26
|
+
};
|
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import type { ExecutorArgs } from '../helpers';
|
|
2
|
+
/**
|
|
3
|
+
* Set the `isolated-vm` instance to be used by the node runtime.
|
|
4
|
+
* This is useful for environments that are not able to rely on our
|
|
5
|
+
* `safeDynamicRequire` trick to import the `isolated-vm` package.
|
|
6
|
+
*/
|
|
7
|
+
export declare const setIvm: (ivm: typeof import('isolated-vm')) => void;
|
|
2
8
|
export declare const runInNode: ({ code, builder, context, event, localState, rootSetState, rootState }: ExecutorArgs) => any;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MSG_PREFIX, logger } from '../../../helpers/logger';
|
|
1
2
|
import { fastClone } from '../../fast-clone';
|
|
2
3
|
import { set } from '../../set';
|
|
3
4
|
import { getFunctionArguments } from '../helpers';
|
|
@@ -53,19 +54,44 @@ if (typeof output === 'object' && output !== null) {
|
|
|
53
54
|
output;
|
|
54
55
|
`;
|
|
55
56
|
};
|
|
57
|
+
let IVM_INSTANCE = null;
|
|
58
|
+
/**
|
|
59
|
+
* Set the `isolated-vm` instance to be used by the node runtime.
|
|
60
|
+
* This is useful for environments that are not able to rely on our
|
|
61
|
+
* `safeDynamicRequire` trick to import the `isolated-vm` package.
|
|
62
|
+
*/
|
|
63
|
+
export const setIvm = (ivm) => {
|
|
64
|
+
IVM_INSTANCE = ivm;
|
|
65
|
+
};
|
|
66
|
+
const getIvm = () => {
|
|
67
|
+
try {
|
|
68
|
+
if (IVM_INSTANCE)
|
|
69
|
+
return IVM_INSTANCE;
|
|
70
|
+
const dynRequiredIvm = safeDynamicRequire('isolated-vm');
|
|
71
|
+
if (dynRequiredIvm)
|
|
72
|
+
return dynRequiredIvm;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
logger.error('isolated-vm import error.', error);
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`${MSG_PREFIX}could not import \`isolated-vm\` module for safe script execution on Node server.
|
|
78
|
+
|
|
79
|
+
In certain Node environments, the SDK requires additional initialization steps. This can be achieved by
|
|
80
|
+
importing and calling \`initializeNodeRuntime()\` from "@builder.io/sdk-react/node/init". This must be done in
|
|
81
|
+
a server-only execution path within your application.
|
|
82
|
+
|
|
83
|
+
Please see the documentation for more information: https://builder.io/c/docs/integration-tips#enabling-data-bindings-in-node-environments
|
|
84
|
+
`);
|
|
85
|
+
};
|
|
56
86
|
const getIsolateContext = () => {
|
|
57
|
-
|
|
58
|
-
// return Builder.serverContext;
|
|
59
|
-
// }
|
|
60
|
-
// Builder.setServerContext(isolate.createContextSync());
|
|
61
|
-
const ivm = safeDynamicRequire('isolated-vm');
|
|
87
|
+
const ivm = getIvm();
|
|
62
88
|
const isolate = new ivm.Isolate({
|
|
63
89
|
memoryLimit: 128
|
|
64
90
|
});
|
|
65
91
|
return isolate.createContextSync();
|
|
66
92
|
};
|
|
67
93
|
export const runInNode = ({ code, builder, context, event, localState, rootSetState, rootState }) => {
|
|
68
|
-
const ivm =
|
|
94
|
+
const ivm = getIvm();
|
|
69
95
|
const state = fastClone({
|
|
70
96
|
...rootState,
|
|
71
97
|
...localState
|
|
@@ -131,9 +131,9 @@ export interface GetContentOptions {
|
|
|
131
131
|
/**
|
|
132
132
|
* Optional override of the `fetch` function. (Defaults to global `fetch`)
|
|
133
133
|
*/
|
|
134
|
-
fetch?:
|
|
134
|
+
fetch?: (input: string, init?: object) => Promise<any>;
|
|
135
135
|
/**
|
|
136
|
-
* Optional fetch options to be passed to the `fetch` function.
|
|
136
|
+
* Optional fetch options to be passed as the second argument to the `fetch` function.
|
|
137
137
|
*/
|
|
138
|
-
fetchOptions?:
|
|
138
|
+
fetchOptions?: object;
|
|
139
139
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "1.0.
|
|
1
|
+
export declare const SDK_VERSION = "1.0.21";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "1.0.
|
|
1
|
+
export const SDK_VERSION = "1.0.21";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function initializes the SDK on a Node server. It handles importing the
|
|
3
|
+
* `isolated-vm` package which is needed for dynamic bindings.
|
|
4
|
+
*
|
|
5
|
+
* NOTE: this function cannot be called on the client. You must call this function
|
|
6
|
+
* from a server-only location, such as:
|
|
7
|
+
* - The NextJS Pages router's `_document.tsx`
|
|
8
|
+
* - Your Remix route's `loader`
|
|
9
|
+
*/
|
|
10
|
+
export declare const initializeNodeRuntime: () => void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file:
|
|
3
|
+
* - imports `isolated-vm`, which can only be made from a file that never runs
|
|
4
|
+
* on the client (e.g. Next's `_document.tsx`)
|
|
5
|
+
* - stores the ivm instance in a global variable using `setIvm`
|
|
6
|
+
*
|
|
7
|
+
* This is needed for when bundlers/meta-frameworks are not able to reliably
|
|
8
|
+
* import the `isolated-vm` package using our `safeDynamicRequire` trick.
|
|
9
|
+
*
|
|
10
|
+
* The `isolated-vm` import must exist in this separate file, or it will end up
|
|
11
|
+
* in the SDK's main entry point, causing errors for users.
|
|
12
|
+
*/
|
|
13
|
+
import ivm from 'isolated-vm';
|
|
14
|
+
import { setIvm } from './node-runtime';
|
|
15
|
+
/**
|
|
16
|
+
* This function initializes the SDK on a Node server. It handles importing the
|
|
17
|
+
* `isolated-vm` package which is needed for dynamic bindings.
|
|
18
|
+
*
|
|
19
|
+
* NOTE: this function cannot be called on the client. You must call this function
|
|
20
|
+
* from a server-only location, such as:
|
|
21
|
+
* - The NextJS Pages router's `_document.tsx`
|
|
22
|
+
* - Your Remix route's `loader`
|
|
23
|
+
*/
|
|
24
|
+
export const initializeNodeRuntime = () => {
|
|
25
|
+
setIvm(ivm);
|
|
26
|
+
};
|
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import type { ExecutorArgs } from '../helpers';
|
|
2
|
+
/**
|
|
3
|
+
* Set the `isolated-vm` instance to be used by the node runtime.
|
|
4
|
+
* This is useful for environments that are not able to rely on our
|
|
5
|
+
* `safeDynamicRequire` trick to import the `isolated-vm` package.
|
|
6
|
+
*/
|
|
7
|
+
export declare const setIvm: (ivm: typeof import('isolated-vm')) => void;
|
|
2
8
|
export declare const runInNode: ({ code, builder, context, event, localState, rootSetState, rootState }: ExecutorArgs) => any;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MSG_PREFIX, logger } from '../../../helpers/logger';
|
|
1
2
|
import { fastClone } from '../../fast-clone';
|
|
2
3
|
import { set } from '../../set';
|
|
3
4
|
import { getFunctionArguments } from '../helpers';
|
|
@@ -53,19 +54,44 @@ if (typeof output === 'object' && output !== null) {
|
|
|
53
54
|
output;
|
|
54
55
|
`;
|
|
55
56
|
};
|
|
57
|
+
let IVM_INSTANCE = null;
|
|
58
|
+
/**
|
|
59
|
+
* Set the `isolated-vm` instance to be used by the node runtime.
|
|
60
|
+
* This is useful for environments that are not able to rely on our
|
|
61
|
+
* `safeDynamicRequire` trick to import the `isolated-vm` package.
|
|
62
|
+
*/
|
|
63
|
+
export const setIvm = (ivm) => {
|
|
64
|
+
IVM_INSTANCE = ivm;
|
|
65
|
+
};
|
|
66
|
+
const getIvm = () => {
|
|
67
|
+
try {
|
|
68
|
+
if (IVM_INSTANCE)
|
|
69
|
+
return IVM_INSTANCE;
|
|
70
|
+
const dynRequiredIvm = safeDynamicRequire('isolated-vm');
|
|
71
|
+
if (dynRequiredIvm)
|
|
72
|
+
return dynRequiredIvm;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
logger.error('isolated-vm import error.', error);
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`${MSG_PREFIX}could not import \`isolated-vm\` module for safe script execution on Node server.
|
|
78
|
+
|
|
79
|
+
In certain Node environments, the SDK requires additional initialization steps. This can be achieved by
|
|
80
|
+
importing and calling \`initializeNodeRuntime()\` from "@builder.io/sdk-react/node/init". This must be done in
|
|
81
|
+
a server-only execution path within your application.
|
|
82
|
+
|
|
83
|
+
Please see the documentation for more information: https://builder.io/c/docs/integration-tips#enabling-data-bindings-in-node-environments
|
|
84
|
+
`);
|
|
85
|
+
};
|
|
56
86
|
const getIsolateContext = () => {
|
|
57
|
-
|
|
58
|
-
// return Builder.serverContext;
|
|
59
|
-
// }
|
|
60
|
-
// Builder.setServerContext(isolate.createContextSync());
|
|
61
|
-
const ivm = safeDynamicRequire('isolated-vm');
|
|
87
|
+
const ivm = getIvm();
|
|
62
88
|
const isolate = new ivm.Isolate({
|
|
63
89
|
memoryLimit: 128
|
|
64
90
|
});
|
|
65
91
|
return isolate.createContextSync();
|
|
66
92
|
};
|
|
67
93
|
export const runInNode = ({ code, builder, context, event, localState, rootSetState, rootState }) => {
|
|
68
|
-
const ivm =
|
|
94
|
+
const ivm = getIvm();
|
|
69
95
|
const state = fastClone({
|
|
70
96
|
...rootState,
|
|
71
97
|
...localState
|
|
@@ -131,9 +131,9 @@ export interface GetContentOptions {
|
|
|
131
131
|
/**
|
|
132
132
|
* Optional override of the `fetch` function. (Defaults to global `fetch`)
|
|
133
133
|
*/
|
|
134
|
-
fetch?:
|
|
134
|
+
fetch?: (input: string, init?: object) => Promise<any>;
|
|
135
135
|
/**
|
|
136
|
-
* Optional fetch options to be passed to the `fetch` function.
|
|
136
|
+
* Optional fetch options to be passed as the second argument to the `fetch` function.
|
|
137
137
|
*/
|
|
138
|
-
fetchOptions?:
|
|
138
|
+
fetchOptions?: object;
|
|
139
139
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "1.0.
|
|
1
|
+
export declare const SDK_VERSION = "1.0.21";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "1.0.
|
|
1
|
+
export const SDK_VERSION = "1.0.21";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function initializes the SDK on a Node server. It handles importing the
|
|
3
|
+
* `isolated-vm` package which is needed for dynamic bindings.
|
|
4
|
+
*
|
|
5
|
+
* NOTE: this function cannot be called on the client. You must call this function
|
|
6
|
+
* from a server-only location, such as:
|
|
7
|
+
* - The NextJS Pages router's `_document.tsx`
|
|
8
|
+
* - Your Remix route's `loader`
|
|
9
|
+
*/
|
|
10
|
+
export declare const initializeNodeRuntime: () => void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file:
|
|
3
|
+
* - imports `isolated-vm`, which can only be made from a file that never runs
|
|
4
|
+
* on the client (e.g. Next's `_document.tsx`)
|
|
5
|
+
* - stores the ivm instance in a global variable using `setIvm`
|
|
6
|
+
*
|
|
7
|
+
* This is needed for when bundlers/meta-frameworks are not able to reliably
|
|
8
|
+
* import the `isolated-vm` package using our `safeDynamicRequire` trick.
|
|
9
|
+
*
|
|
10
|
+
* The `isolated-vm` import must exist in this separate file, or it will end up
|
|
11
|
+
* in the SDK's main entry point, causing errors for users.
|
|
12
|
+
*/
|
|
13
|
+
import ivm from 'isolated-vm';
|
|
14
|
+
import { setIvm } from './node-runtime';
|
|
15
|
+
/**
|
|
16
|
+
* This function initializes the SDK on a Node server. It handles importing the
|
|
17
|
+
* `isolated-vm` package which is needed for dynamic bindings.
|
|
18
|
+
*
|
|
19
|
+
* NOTE: this function cannot be called on the client. You must call this function
|
|
20
|
+
* from a server-only location, such as:
|
|
21
|
+
* - The NextJS Pages router's `_document.tsx`
|
|
22
|
+
* - Your Remix route's `loader`
|
|
23
|
+
*/
|
|
24
|
+
export const initializeNodeRuntime = () => {
|
|
25
|
+
setIvm(ivm);
|
|
26
|
+
};
|
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import type { ExecutorArgs } from '../helpers';
|
|
2
|
+
/**
|
|
3
|
+
* Set the `isolated-vm` instance to be used by the node runtime.
|
|
4
|
+
* This is useful for environments that are not able to rely on our
|
|
5
|
+
* `safeDynamicRequire` trick to import the `isolated-vm` package.
|
|
6
|
+
*/
|
|
7
|
+
export declare const setIvm: (ivm: typeof import('isolated-vm')) => void;
|
|
2
8
|
export declare const runInNode: ({ code, builder, context, event, localState, rootSetState, rootState }: ExecutorArgs) => any;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MSG_PREFIX, logger } from '../../../helpers/logger';
|
|
1
2
|
import { fastClone } from '../../fast-clone';
|
|
2
3
|
import { set } from '../../set';
|
|
3
4
|
import { getFunctionArguments } from '../helpers';
|
|
@@ -53,19 +54,44 @@ if (typeof output === 'object' && output !== null) {
|
|
|
53
54
|
output;
|
|
54
55
|
`;
|
|
55
56
|
};
|
|
57
|
+
let IVM_INSTANCE = null;
|
|
58
|
+
/**
|
|
59
|
+
* Set the `isolated-vm` instance to be used by the node runtime.
|
|
60
|
+
* This is useful for environments that are not able to rely on our
|
|
61
|
+
* `safeDynamicRequire` trick to import the `isolated-vm` package.
|
|
62
|
+
*/
|
|
63
|
+
export const setIvm = (ivm) => {
|
|
64
|
+
IVM_INSTANCE = ivm;
|
|
65
|
+
};
|
|
66
|
+
const getIvm = () => {
|
|
67
|
+
try {
|
|
68
|
+
if (IVM_INSTANCE)
|
|
69
|
+
return IVM_INSTANCE;
|
|
70
|
+
const dynRequiredIvm = safeDynamicRequire('isolated-vm');
|
|
71
|
+
if (dynRequiredIvm)
|
|
72
|
+
return dynRequiredIvm;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
logger.error('isolated-vm import error.', error);
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`${MSG_PREFIX}could not import \`isolated-vm\` module for safe script execution on Node server.
|
|
78
|
+
|
|
79
|
+
In certain Node environments, the SDK requires additional initialization steps. This can be achieved by
|
|
80
|
+
importing and calling \`initializeNodeRuntime()\` from "@builder.io/sdk-react/node/init". This must be done in
|
|
81
|
+
a server-only execution path within your application.
|
|
82
|
+
|
|
83
|
+
Please see the documentation for more information: https://builder.io/c/docs/integration-tips#enabling-data-bindings-in-node-environments
|
|
84
|
+
`);
|
|
85
|
+
};
|
|
56
86
|
const getIsolateContext = () => {
|
|
57
|
-
|
|
58
|
-
// return Builder.serverContext;
|
|
59
|
-
// }
|
|
60
|
-
// Builder.setServerContext(isolate.createContextSync());
|
|
61
|
-
const ivm = safeDynamicRequire('isolated-vm');
|
|
87
|
+
const ivm = getIvm();
|
|
62
88
|
const isolate = new ivm.Isolate({
|
|
63
89
|
memoryLimit: 128
|
|
64
90
|
});
|
|
65
91
|
return isolate.createContextSync();
|
|
66
92
|
};
|
|
67
93
|
export const runInNode = ({ code, builder, context, event, localState, rootSetState, rootState }) => {
|
|
68
|
-
const ivm =
|
|
94
|
+
const ivm = getIvm();
|
|
69
95
|
const state = fastClone({
|
|
70
96
|
...rootState,
|
|
71
97
|
...localState
|
|
@@ -131,9 +131,9 @@ export interface GetContentOptions {
|
|
|
131
131
|
/**
|
|
132
132
|
* Optional override of the `fetch` function. (Defaults to global `fetch`)
|
|
133
133
|
*/
|
|
134
|
-
fetch?:
|
|
134
|
+
fetch?: (input: string, init?: object) => Promise<any>;
|
|
135
135
|
/**
|
|
136
|
-
* Optional fetch options to be passed to the `fetch` function.
|
|
136
|
+
* Optional fetch options to be passed as the second argument to the `fetch` function.
|
|
137
137
|
*/
|
|
138
|
-
fetchOptions?:
|
|
138
|
+
fetchOptions?: object;
|
|
139
139
|
}
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "1.0.
|
|
1
|
+
export const SDK_VERSION = "1.0.21"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file:
|
|
3
|
+
* - imports `isolated-vm`, which can only be made from a file that never runs
|
|
4
|
+
* on the client (e.g. Next's `_document.tsx`)
|
|
5
|
+
* - stores the ivm instance in a global variable using `setIvm`
|
|
6
|
+
*
|
|
7
|
+
* This is needed for when bundlers/meta-frameworks are not able to reliably
|
|
8
|
+
* import the `isolated-vm` package using our `safeDynamicRequire` trick.
|
|
9
|
+
*
|
|
10
|
+
* The `isolated-vm` import must exist in this separate file, or it will end up
|
|
11
|
+
* in the SDK's main entry point, causing errors for users.
|
|
12
|
+
*/
|
|
13
|
+
import ivm from 'isolated-vm';
|
|
14
|
+
import { setIvm } from './node-runtime';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This function initializes the SDK on a Node server. It handles importing the
|
|
18
|
+
* `isolated-vm` package which is needed for dynamic bindings.
|
|
19
|
+
*
|
|
20
|
+
* NOTE: this function cannot be called on the client. You must call this function
|
|
21
|
+
* from a server-only location, such as:
|
|
22
|
+
* - The NextJS Pages router's `_document.tsx`
|
|
23
|
+
* - Your Remix route's `loader`
|
|
24
|
+
*/
|
|
25
|
+
export const initializeNodeRuntime = () => {
|
|
26
|
+
setIvm(ivm);
|
|
27
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MSG_PREFIX, logger } from '../../../helpers/logger';
|
|
1
2
|
import { fastClone } from '../../fast-clone';
|
|
2
3
|
import { set } from '../../set';
|
|
3
4
|
import type { BuilderGlobals, ExecutorArgs, FunctionArguments } from '../helpers';
|
|
@@ -62,12 +63,35 @@ if (typeof output === 'object' && output !== null) {
|
|
|
62
63
|
output;
|
|
63
64
|
`;
|
|
64
65
|
};
|
|
66
|
+
let IVM_INSTANCE: typeof import('isolated-vm') | null = null;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Set the `isolated-vm` instance to be used by the node runtime.
|
|
70
|
+
* This is useful for environments that are not able to rely on our
|
|
71
|
+
* `safeDynamicRequire` trick to import the `isolated-vm` package.
|
|
72
|
+
*/
|
|
73
|
+
export const setIvm = (ivm: typeof import('isolated-vm')) => {
|
|
74
|
+
IVM_INSTANCE = ivm;
|
|
75
|
+
};
|
|
76
|
+
const getIvm = (): typeof import('isolated-vm') => {
|
|
77
|
+
try {
|
|
78
|
+
if (IVM_INSTANCE) return IVM_INSTANCE;
|
|
79
|
+
const dynRequiredIvm = safeDynamicRequire('isolated-vm');
|
|
80
|
+
if (dynRequiredIvm) return dynRequiredIvm;
|
|
81
|
+
} catch (error) {
|
|
82
|
+
logger.error('isolated-vm import error.', error);
|
|
83
|
+
}
|
|
84
|
+
throw new Error(`${MSG_PREFIX}could not import \`isolated-vm\` module for safe script execution on Node server.
|
|
85
|
+
|
|
86
|
+
In certain Node environments, the SDK requires additional initialization steps. This can be achieved by
|
|
87
|
+
importing and calling \`initializeNodeRuntime()\` from "@builder.io/sdk-react/node/init". This must be done in
|
|
88
|
+
a server-only execution path within your application.
|
|
89
|
+
|
|
90
|
+
Please see the documentation for more information: https://builder.io/c/docs/integration-tips#enabling-data-bindings-in-node-environments
|
|
91
|
+
`);
|
|
92
|
+
};
|
|
65
93
|
const getIsolateContext = () => {
|
|
66
|
-
|
|
67
|
-
// return Builder.serverContext;
|
|
68
|
-
// }
|
|
69
|
-
// Builder.setServerContext(isolate.createContextSync());
|
|
70
|
-
const ivm: typeof import('isolated-vm') = safeDynamicRequire('isolated-vm');
|
|
94
|
+
const ivm = getIvm();
|
|
71
95
|
const isolate = new ivm.Isolate({
|
|
72
96
|
memoryLimit: 128
|
|
73
97
|
});
|
|
@@ -82,7 +106,7 @@ export const runInNode = ({
|
|
|
82
106
|
rootSetState,
|
|
83
107
|
rootState
|
|
84
108
|
}: ExecutorArgs) => {
|
|
85
|
-
const ivm
|
|
109
|
+
const ivm = getIvm();
|
|
86
110
|
const state = fastClone({
|
|
87
111
|
...rootState,
|
|
88
112
|
...localState
|
|
@@ -148,10 +148,10 @@ export interface GetContentOptions {
|
|
|
148
148
|
/**
|
|
149
149
|
* Optional override of the `fetch` function. (Defaults to global `fetch`)
|
|
150
150
|
*/
|
|
151
|
-
fetch?:
|
|
151
|
+
fetch?: (input: string, init?: object) => Promise<any>;
|
|
152
152
|
|
|
153
153
|
/**
|
|
154
|
-
* Optional fetch options to be passed to the `fetch` function.
|
|
154
|
+
* Optional fetch options to be passed as the second argument to the `fetch` function.
|
|
155
155
|
*/
|
|
156
|
-
fetchOptions?:
|
|
156
|
+
fetchOptions?: object;
|
|
157
157
|
}
|
package/src/helpers/logger.ts
CHANGED