-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Temporary blocking of vm2 of js interpreter
close #12
- Loading branch information
Showing
4 changed files
with
56 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
import { createJavaScriptInterpreter } from "./javaScriptInterpreter"; | ||
import { expect, it } from 'vitest'; | ||
// import { createJavaScriptInterpreter } from "./javaScriptInterpreter"; | ||
// import { expect, it } from 'vitest'; | ||
|
||
it('should interpret JavaScript code correctly', () => { | ||
const [javaScriptInterpreter] = createJavaScriptInterpreter(); | ||
const result = javaScriptInterpreter({ | ||
code: ` | ||
1 + 1 | ||
`, | ||
}); | ||
expect(result).toBe(2); | ||
}); | ||
// it('should interpret JavaScript code correctly', () => { | ||
// const [javaScriptInterpreter] = createJavaScriptInterpreter(); | ||
// const result = javaScriptInterpreter({ | ||
// code: ` | ||
// 1 + 1 | ||
// `, | ||
// }); | ||
// expect(result).toBe(2); | ||
// }); | ||
|
||
it('should timeout for long running scripts', () => { | ||
const [javaScriptInterpreter] = createJavaScriptInterpreter(); | ||
const result = javaScriptInterpreter({ | ||
code: ` | ||
while (true) {} | ||
`, | ||
}); | ||
// it('should timeout for long running scripts', () => { | ||
// const [javaScriptInterpreter] = createJavaScriptInterpreter(); | ||
// const result = javaScriptInterpreter({ | ||
// code: ` | ||
// while (true) {} | ||
// `, | ||
// }); | ||
|
||
expect(result).toBe("Failed to execute script: Script execution timed out after 5000ms") | ||
}); | ||
// expect(result).toBe("Failed to execute script: Script execution timed out after 5000ms") | ||
// }); | ||
|
||
it('should not have access to Node.js environment', () => { | ||
const [javaScriptInterpreter] = createJavaScriptInterpreter(); | ||
const result = javaScriptInterpreter({ | ||
code: ` | ||
process.exit(1) | ||
`, | ||
}); | ||
// it('should not have access to Node.js environment', () => { | ||
// const [javaScriptInterpreter] = createJavaScriptInterpreter(); | ||
// const result = javaScriptInterpreter({ | ||
// code: ` | ||
// process.exit(1) | ||
// `, | ||
// }); | ||
|
||
expect(result).toBe("Failed to execute script: process is not defined") | ||
}); | ||
// expect(result).toBe("Failed to execute script: process is not defined") | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
import { z } from "zod"; | ||
import { VM } from "vm2"; | ||
import { Tool } from "./tool"; | ||
// import { z } from "zod"; | ||
// import { VM } from "vm2"; | ||
// import { Tool } from "./tool"; | ||
|
||
function createJavaScriptInterpreter() { | ||
const paramsSchema = z.object({ | ||
code: z.string(), | ||
}); | ||
const name = "javaScriptInterpreter"; | ||
const description = "Useful for running JavaScript code in sandbox. Input is a string of JavaScript code, output is the result of the code."; | ||
// function createJavaScriptInterpreter() { | ||
// const paramsSchema = z.object({ | ||
// code: z.string(), | ||
// }); | ||
// const name = "javaScriptInterpreter"; | ||
// const description = "Useful for running JavaScript code in sandbox. Input is a string of JavaScript code, output is the result of the code."; | ||
|
||
const execute = (params: z.infer<typeof paramsSchema>) => { | ||
const { code } = params; | ||
const vm = new VM({ | ||
timeout: 5000, | ||
sandbox: {}, | ||
}); | ||
try { | ||
return vm.run(code); | ||
} catch (error) { | ||
return `Failed to execute script: ${error.message}`; | ||
} | ||
}; | ||
// const execute = (params: z.infer<typeof paramsSchema>) => { | ||
// const { code } = params; | ||
// const vm = new VM({ | ||
// timeout: 5000, | ||
// sandbox: {}, | ||
// }); | ||
// try { | ||
// return vm.run(code); | ||
// } catch (error) { | ||
// return `Failed to execute script: ${error.message}`; | ||
// } | ||
// }; | ||
|
||
return new Tool<typeof paramsSchema, z.ZodType<any, any>>(paramsSchema, name, description, execute).tool; | ||
} | ||
// return new Tool<typeof paramsSchema, z.ZodType<any, any>>(paramsSchema, name, description, execute).tool; | ||
// } | ||
|
||
export { createJavaScriptInterpreter }; | ||
// export { createJavaScriptInterpreter }; |