feat: sandbox support str to base64 function;feat: compiance text ui (#2702)

* feat: sandbox strToBase64 function

* feat: compiance text ui
This commit is contained in:
Archer
2024-09-13 17:54:22 +08:00
committed by GitHub
parent 9de8849193
commit 092bb9ab17
7 changed files with 21 additions and 21 deletions

View File

@@ -0,0 +1,5 @@
export const strToBase64 = (str: string, prefix: string) => {
const base64_string = Buffer.from(str, 'utf-8').toString('base64');
return `${prefix}${base64_string}`;
};

View File

@@ -2,13 +2,18 @@ import { RunCodeDto, RunCodeResponse } from 'src/sandbox/dto/create-sandbox.dto'
import IsolatedVM, { ExternalCopy, Isolate, Reference } from 'isolated-vm';
import { countToken } from './jsFn/tiktoken';
import { timeDelay } from './jsFn/delay';
import { strToBase64 } from './jsFn/str2Base64';
const CustomLogStr = 'CUSTOM_LOG';
/*
Rewrite code to add custom functions: Promise function; Log.
*/
Rewrite code to add custom functions: Promise function; Log.
*/
function getFnCode(code: string) {
// rewrite log
code = code.replace(/console\.log/g, `${CustomLogStr}`);
// Promise function rewrite
const rewriteSystemFn = `
const thisDelay = (...args) => global_delay.applySyncPromise(undefined,args)
`;
@@ -16,9 +21,6 @@ function getFnCode(code: string) {
// rewrite delay
code = code.replace(/delay\((.*)\)/g, `thisDelay($1)`);
// rewrite log
code = code.replace(/console\.log/g, `${CustomLogStr}`);
const runCode = `
(async() => {
try {
@@ -35,11 +37,12 @@ function getFnCode(code: string) {
return runCode;
}
// Register global function
function registerSystemFn(jail: IsolatedVM.Reference<Record<string | number | symbol, any>>) {
return Promise.all([
// delay
jail.set('global_delay', new Reference(timeDelay)),
jail.set('countToken', countToken)
jail.set('countToken', countToken),
jail.set('strToBase64', strToBase64)
]);
}