4.8.3 (#1654)
* Milvus (#1644) * feat: support regx * 4.8.3 test and fix (#1648) * perf: version tip * feat: sandbox support log * fix: debug component render * fix: share page header * fix: input guide auth * fix: iso viewport * remove file * fix: route url * feat: add debug timout * perf: reference select support trigger * perf: session code * perf: theme * perf: load milvus
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { RunCodeDto } from 'src/sandbox/dto/create-sandbox.dto';
|
||||
import { RunCodeDto, RunCodeResponse } from 'src/sandbox/dto/create-sandbox.dto';
|
||||
import { parentPort } from 'worker_threads';
|
||||
import { workerResponse } from './utils';
|
||||
|
||||
@@ -6,19 +6,33 @@ import { workerResponse } from './utils';
|
||||
const ivm = require('isolated-vm');
|
||||
|
||||
parentPort?.on('message', ({ code, variables = {} }: RunCodeDto) => {
|
||||
const resolve = (data: any) => workerResponse({ parentPort, type: 'success', data });
|
||||
const resolve = (data: RunCodeResponse) => workerResponse({ parentPort, type: 'success', data });
|
||||
const reject = (error: any) => workerResponse({ parentPort, type: 'error', data: error });
|
||||
|
||||
const isolate = new ivm.Isolate({ memoryLimit: 32 });
|
||||
const context = isolate.createContextSync();
|
||||
const jail = context.global;
|
||||
|
||||
// custom log function
|
||||
// custom function
|
||||
const logData = [];
|
||||
const CustomLogStr = 'CUSTOM_LOG';
|
||||
code = code.replace(/console\.log/g, `${CustomLogStr}`);
|
||||
jail.setSync(CustomLogStr, function (...args) {
|
||||
logData.push(
|
||||
args
|
||||
.map((item) => (typeof item === 'object' ? JSON.stringify(item, null, 2) : item))
|
||||
.join(', ')
|
||||
);
|
||||
});
|
||||
|
||||
jail.setSync('responseData', function (args: any): any {
|
||||
if (typeof args === 'object') {
|
||||
resolve(args);
|
||||
resolve({
|
||||
codeReturn: args,
|
||||
log: logData.join('\n')
|
||||
});
|
||||
} else {
|
||||
reject('Not an invalid response');
|
||||
reject('Not an invalid response, must return an object');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user