@@ -30,7 +30,6 @@ async function setupIsolate() {
3030 const context = await isolate . createContext ( ) ;
3131 const jail = context . global ;
3232 await jail . set ( 'global' , jail . derefInto ( ) ) ;
33-
3433 // Define a SafeBuffer object
3534 const ___internal = {
3635 b64decode : ( str ) => Buffer . from ( str , 'base64' ) . toString ( 'utf8' ) ,
@@ -62,45 +61,66 @@ export async function runJs(code: string) {
6261
6362 if ( ! code . endsWith ( ';' ) ) code += ';' ;
6463
65- let scriptCode = '' ;
6664 const { isolate, context, jail } = await setupIsolate ( ) ;
6765 const remoteUrls = await extractFetchUrls ( code ) ;
6866 for ( const url of remoteUrls ) {
6967 const remoteCode = await fetchCodeFromCDN ( url ) ;
70- context . eval ( `${ remoteCode } ` ) ;
68+ await context . eval ( `${ remoteCode } ` ) ;
7169 }
72- const randomId = Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) ;
73- const resId = `res ${ randomId } ` ;
74- scriptCode = `
75- var ${ resId } ;
76- ${ code } ;
77-
78- ${ resId } = JSON.stringify(_output);
79- ${ resId } ;
80- ` ;
81- const script : any = await isolate . compileScript ( scriptCode ) . catch ( ( err ) => {
70+
71+ const executionCode = `
72+ (async () => {
73+ ${ code }
74+ globalThis.__finalResult = result ;
75+ })();
76+ ` ;
77+
78+ // Execute the original code
79+ const executeScript = await isolate . compileScript ( executionCode ) . catch ( ( err ) => {
8280 console . error ( err ) ;
8381 return { error : 'Compile Error - ' + err . message } ;
8482 } ) ;
85- if ( script ?. error ) {
86- throw new Error ( script . error ) ;
83+ if ( ' error' in executeScript ) {
84+ throw new Error ( executeScript . error ) ;
8785 }
8886
89- const rawResult = await script . run ( context ) . catch ( ( err ) => {
87+ await executeScript . run ( context ) . catch ( ( err ) => {
9088 console . error ( err ) ;
91- return { error : 'Run Error - ' + err . message } ;
89+ throw new Error ( 'Run Error - ' + err . message ) ;
9290 } ) ;
91+
92+ // Try to get the result from the global variable first, then fallback to 'result'
93+ let rawResult = await context . eval ( 'globalThis.__finalResult' ) . catch ( ( err ) => {
94+ console . error ( 'Failed to get __finalResult:' , err ) ;
95+ return null ;
96+ } ) ;
97+
9398 if ( rawResult ?. error ) {
9499 throw new Error ( rawResult . error ) ;
95100 }
96-
97- // Transfer the result out of the isolate and parse it
98- //const serializedResult = rawResult.copySync();
99- const Output = JSON . parse ( rawResult ) ;
100-
101- return Output ;
101+ return { Output : rawResult } ;
102102 } catch ( error ) {
103103 console . error ( error ) ;
104104 throw new Error ( error . message ) ;
105105 }
106106}
107+
108+ function getParametersString ( parameters : string [ ] , inputs : Record < string , any > ) {
109+ let params = [ ] ;
110+ for ( const parameter of parameters ) {
111+ if ( typeof inputs [ parameter ] === 'string' ) {
112+ params . push ( `'${ inputs [ parameter ] } '` ) ;
113+ } else {
114+ params . push ( `${ inputs [ parameter ] } ;` ) ;
115+ }
116+ }
117+ return params . join ( ',' ) ;
118+ }
119+
120+ export function generateExecutableCode ( code : string , parameters : string [ ] , inputs : Record < string , any > ) {
121+ const executableCode = `
122+ ${ code }
123+ const result = await main(${ getParametersString ( parameters , inputs ) } );
124+ `
125+ return executableCode ;
126+ }
0 commit comments