perf: improver code editor request return type and test modal style

This commit is contained in:
moonrailgun 2024-01-03 00:15:39 +08:00
parent b751222b39
commit 4f35463f89
6 changed files with 26 additions and 4 deletions

View File

@ -44,5 +44,11 @@ interface AxiosConfig {
} | false; } | false;
} }
const request = async (config: AxiosConfig) => {} interface RequestReturn {
headers: Record<string, string>;
data: any;
status: number;
}
const request = async (config: AxiosConfig): Promise<RequestReturn> => {}
`; `;

View File

@ -26,6 +26,9 @@ export const CodeEditor: React.FC<CodeEditorProps> = React.memo((props) => {
theme={theme} theme={theme}
defaultLanguage="javascript" defaultLanguage="javascript"
value={props.value} value={props.value}
options={{
tabSize: 2,
}}
onChange={(val) => props.onChange?.(val ?? '')} onChange={(val) => props.onChange?.(val ?? '')}
beforeMount={handleEditorWillMount} beforeMount={handleEditorWillMount}
/> />

View File

@ -27,6 +27,7 @@ export const MonitorCustom: React.FC = React.memo(() => {
centered: true, centered: true,
maskClosable: true, maskClosable: true,
title: 'Run Completed', title: 'Run Completed',
width: 'clamp(320px, 60vw, 860px)',
content: ( content: (
<div> <div>
{logger.map(([type, time, ...args]) => ( {logger.map(([type, time, ...args]) => (
@ -43,7 +44,7 @@ export const MonitorCustom: React.FC = React.memo(() => {
{dayjs(time).format('HH:mm:ss')} {dayjs(time).format('HH:mm:ss')}
</span> </span>
{args.join(' ')} <span className="break-all">{args.join(' ')}</span>
</div> </div>
))} ))}

View File

@ -27,7 +27,12 @@ export async function runCodeInVM(_code: string) {
const start = Date.now(); const start = Date.now();
const isolate = new ivm.Isolate({ memoryLimit: env.sandboxMemoryLimit }); const isolate = new ivm.Isolate({ memoryLimit: env.sandboxMemoryLimit });
const code = `${environmentScript}\n\n;(async () => {${_code}})()`; // avoid end comment with line break
const code = `${environmentScript}
;(async () => {
${_code}
})()`;
const [context, script] = await Promise.all([ const [context, script] = await Promise.all([
isolate.createContext(), isolate.createContext(),

View File

@ -187,7 +187,13 @@ export const monitorRouter = router({
}) })
) )
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
return runCodeInVM(input.code); const res = await runCodeInVM(input.code);
return {
logger: res.logger,
result: res.result ?? -1,
usage: res.usage,
};
}), }),
data: workspaceProcedure data: workspaceProcedure
.meta( .meta(

View File

@ -155,6 +155,7 @@ export function buildSandbox(context: Context, globals: SandboxGlobals = {}) {
const result = await axios.request(config); const result = await axios.request(config);
return makeTransferable({ return makeTransferable({
headers: { ...result.headers },
data: result.data, data: result.data,
status: result.status, status: result.status,
}); });