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;
}
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}
defaultLanguage="javascript"
value={props.value}
options={{
tabSize: 2,
}}
onChange={(val) => props.onChange?.(val ?? '')}
beforeMount={handleEditorWillMount}
/>

View File

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

View File

@ -27,7 +27,12 @@ export async function runCodeInVM(_code: string) {
const start = Date.now();
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([
isolate.createContext(),

View File

@ -187,7 +187,13 @@ export const monitorRouter = router({
})
)
.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
.meta(

View File

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