Skip to content

Commit 7527fdc

Browse files
committed
feat: add worker runtime context
1 parent 071586d commit 7527fdc

7 files changed

Lines changed: 60 additions & 8 deletions

File tree

src/client/components/CodeEditor/lib/sandbox.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,27 @@ interface RequestReturn {
5050
status: number;
5151
}
5252
53+
interface FetchContext {
54+
type: 'http' | 'cron' | 'manual' | 'test';
55+
request?: {
56+
method: string;
57+
url: string;
58+
headers: Record<string, string>;
59+
};
60+
}
61+
62+
interface Console {
63+
log(...data: any[]): void;
64+
info(...data: any[]): void;
65+
warn(...data: any[]): void;
66+
error(...data: any[]): void;
67+
}
68+
69+
declare const console: Console;
70+
5371
declare function request(config: AxiosConfig): Promise<RequestReturn>;
5472
5573
const request = async (config: AxiosConfig): Promise<RequestReturn> => {};
74+
75+
declare function fetch(params: Record<string, any>, ctx: FetchContext): Promise<string>;
5676
`;

src/client/components/CodeEditor/main.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ export const CodeEditor: React.FC<CodeEditorProps> = React.memo((props) => {
4242
editorRef.current = editor;
4343
monacoRef.current = monaco;
4444

45+
const tsDefaults = monaco.languages.typescript.typescriptDefaults;
46+
tsDefaults.setCompilerOptions({
47+
...tsDefaults.getCompilerOptions(),
48+
lib: ['es2021'],
49+
});
50+
51+
const jsDefaults = monaco.languages.typescript.javascriptDefaults;
52+
jsDefaults.setCompilerOptions({
53+
...jsDefaults.getCompilerOptions(),
54+
lib: ['es2021'],
55+
});
56+
4557
monaco.languages.typescript.javascriptDefaults.addExtraLib(
4658
sandboxGlobal,
4759
'global.ts'

src/client/components/worker/WorkerExecutionDetail.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export const WorkerExecutionDetail: React.FC<WorkerExecutionDetailProps> =
120120
{formatDate(log[1])}
121121
</span>
122122
</div>
123-
<span>
123+
<span className="overflow-x-auto">
124124
{drop(log, 2).map((item, i) => (
125125
<DataRender
126126
key={i}
@@ -137,7 +137,7 @@ export const WorkerExecutionDetail: React.FC<WorkerExecutionDetailProps> =
137137
<span className="opacity-60">
138138
{formatDate(log[1])}
139139
</span>
140-
<span>
140+
<span className="overflow-x-auto">
141141
{drop(log, 2).map((item, i) => (
142142
<DataRender
143143
key={i}

src/server/model/worker/cronRunner.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export class WorkerCronRunner {
3535
);
3636

3737
// Execute the worker
38-
const result = await execWorker(worker.code, worker.id);
38+
const result = await execWorker(worker.code, worker.id, undefined, {
39+
type: 'cron',
40+
});
3941

4042
logger.info(
4143
`[Worker Cron] Worker ${worker.name}(${worker.id}) executed successfully`

src/server/model/worker/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ import { isPlainObject } from 'lodash-es';
99
export async function execWorker(
1010
code: string,
1111
workerId?: string,
12-
requestPayload?: Record<string, any>
12+
requestPayload?: Record<string, any>,
13+
context?: Record<string, any>
1314
) {
1415
const requestPayloadString = isPlainObject(requestPayload)
1516
? JSON.stringify(requestPayload)
1617
: '{}';
18+
const contextString = isPlainObject(context) ? JSON.stringify(context) : '{}';
1719

1820
try {
1921
const { isolate, logger, result, usage } = await runCodeInIVM(`
2022
(async () => {
2123
${code}
2224
23-
return typeof fetch === 'function' ? fetch(${requestPayloadString}) : 'fetch is not defined';
25+
return typeof fetch === 'function' ? fetch(${requestPayloadString}, ${contextString}) : 'fetch is not defined';
2426
})()
2527
`);
2628

src/server/router/worker.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,19 @@ workerRouter.all(
5151
}
5252

5353
// Execute the worker
54-
const execution = await execWorker(worker.code, workerId, requestPayload);
54+
const execution = await execWorker(
55+
worker.code,
56+
workerId,
57+
requestPayload,
58+
{
59+
type: 'http',
60+
request: {
61+
method: req.method,
62+
url: req.url,
63+
headers: { ...req.headers },
64+
},
65+
}
66+
);
5567

5668
const response = execution.responsePayload;
5769

src/server/trpc/routers/worker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ export const workerRouter = router({
252252
throw new Error('Worker not found');
253253
}
254254

255-
const execution = await execWorker(worker.code, workerId);
255+
const execution = await execWorker(worker.code, workerId, undefined, {
256+
type: 'manual',
257+
});
256258

257259
await createAuditLog({
258260
workspaceId,
@@ -407,7 +409,9 @@ export const workerRouter = router({
407409
throw new Error('Function worker is not enabled');
408410
}
409411

410-
const execution = await execWorker(code);
412+
const execution = await execWorker(code, undefined, undefined, {
413+
type: 'test',
414+
});
411415

412416
return execution;
413417
}),

0 commit comments

Comments
 (0)