Skip to content

Commit

Permalink
Feature: React gen2 SDK: setIvm export (#3665)
Browse files Browse the repository at this point in the history
## Description
  • Loading branch information
samijaber authored Nov 1, 2024
1 parent 45cea15 commit 800457c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-sloths-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/sdk-react': patch
---

Feature: add `/node/setIvm` export path. It provides a `setIvm` function that can be used to provide the SDK with an instance of the `isolated-vm` module. This is needed in Node.js environments for data bindings to work when `/node/init` can't be used.
Empty file added .yarn/versions/57f7f014.yml
Empty file.
8 changes: 7 additions & 1 deletion packages/sdks/output/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
"import": "./lib/node/init.mjs",
"require": "./lib/node/init.cjs"
},
"./node/setIvm": {
"types": "./types/functions/evaluate/node-runtime/setIvm.d.ts",
"import": "./lib/node/setIvm.mjs",
"require": "./lib/node/setIvm.cjs"
},
"./edge": {
"types": "./types/index.d.ts",
"import": "./lib/edge/edge-entry.mjs",
Expand Down Expand Up @@ -148,5 +153,6 @@
]
}
}
}
},
"stableVersion": "2.0.23"
}
1 change: 1 addition & 0 deletions packages/sdks/output/react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export default defineConfig({
...(SDK_ENV === 'node'
? {
init: './src/functions/evaluate/node-runtime/init.ts',
setIvm: './src/functions/evaluate/node-runtime/setIvm.ts',
}
: {}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ let IVM_CONTEXT: Context | null = null;
* `safeDynamicRequire` trick to import the `isolated-vm` package.
*/
export const setIvm = (ivm: IsolatedVMImport, options: IsolateOptions = {}) => {
IVM_INSTANCE = ivm;
if (IVM_INSTANCE) return;

IVM_INSTANCE = ivm;
setIsolateContext(options);
};

Expand Down Expand Up @@ -121,6 +122,8 @@ const getIvm = (): IsolatedVMImport => {
};

function setIsolateContext(options: IsolateOptions = { memoryLimit: 128 }) {
if (IVM_CONTEXT) return IVM_CONTEXT;

const ivm = getIvm();
const isolate = new ivm.Isolate(options);
const context = isolate.createContextSync();
Expand All @@ -143,11 +146,7 @@ function setIsolateContext(options: IsolateOptions = { memoryLimit: 128 }) {
}

const getIsolateContext = () => {
if (IVM_CONTEXT) return IVM_CONTEXT;

const context = setIsolateContext();

return context;
return setIsolateContext();
};

export const runInNode = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { setIvm } from './node-runtime.js';

0 comments on commit 800457c

Please sign in to comment.