Skip to content

Commit c723f62

Browse files
committed
feat: add worker typescript support
1 parent c8a9814 commit c723f62

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

src/client/components/CodeEditor/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export const CodeEditor: React.FC<CodeEditorProps> = React.memo((props) => {
126126
readOnly,
127127
automaticLayout: true,
128128
scrollBeyondLastLine: false,
129-
wordWrap: 'on',
129+
wordWrap: 'off',
130130
wrappingIndent: 'indent',
131131
formatOnPaste: true,
132132
formatOnType: true,

src/client/components/worker/WorkerEditForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const formSchema = z
6767
}
6868
);
6969

70-
const defaultCode = `async function fetch(payload, ctx) {
70+
const defaultCode = `async function fetch(payload: Record<string, any>, ctx: FetchContext) {
7171
return 'Hello, World!';
7272
}
7373
`;

src/server/utils/vm/index.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,17 @@ describe('runCodeInIVM', () => {
187187
expect(result.logger[0][5]).toEqual({ key: 'value' });
188188
expect(result.result).toBe('complete');
189189
});
190+
191+
test.only('should support typescript code', async () => {
192+
const code = `
193+
(async () => {
194+
const a: number = 1;
195+
const b: number = 2;
196+
197+
return a + b;
198+
})()
199+
`;
200+
const result = await runCodeInIVM(code);
201+
expect(result.result).toBe(3);
202+
});
190203
});

src/server/utils/vm/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { buildSandbox, environmentScript } from './sandbox.js';
33
import { env } from '../env.js';
44
import { runCodeInVM2 } from './sandbox-vm2.js';
55
import { logger } from '../logger.js';
6+
import { transformTypescriptCode } from './utils.js';
67

78
if (env.sandbox.useVM2) {
89
logger.warn(
@@ -52,11 +53,12 @@ export async function runCodeInVM(_code: string): Promise<{
5253
export async function runCodeInIVM(_code: string) {
5354
const start = Date.now();
5455
const isolate = new ivm.Isolate({ memoryLimit: env.sandbox.memoryLimit });
56+
const transformedCode = await transformTypescriptCode(_code);
5557

5658
// avoid end comment with line break
5759
const code = `${environmentScript}
5860
59-
${_code}`;
61+
${transformedCode}`;
6062

6163
const [context, script] = await Promise.all([
6264
isolate.createContext(),

src/server/utils/vm/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { transform as swcTransform } from '@swc/core';
2+
3+
/**
4+
* Transform Typescript code to ES6 code
5+
*/
6+
export async function transformTypescriptCode(sourceCode: string) {
7+
const res = await swcTransform(sourceCode, {
8+
jsc: {
9+
parser: {
10+
syntax: 'typescript',
11+
tsx: false,
12+
},
13+
target: 'esnext',
14+
},
15+
module: {
16+
type: 'es6',
17+
},
18+
minify: false,
19+
});
20+
21+
return res.code;
22+
}

0 commit comments

Comments
 (0)