-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathcode-tool-worker.ts
More file actions
328 lines (299 loc) · 10.7 KB
/
code-tool-worker.ts
File metadata and controls
328 lines (299 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import path from 'node:path';
import util from 'node:util';
import Fuse from 'fuse.js';
import ts from 'typescript';
import { WorkerOutput } from './code-tool-types';
import { ImageKit, ClientOptions } from '@imagekit/nodejs';
async function tseval(code: string) {
return import('data:application/typescript;charset=utf-8;base64,' + Buffer.from(code).toString('base64'));
}
function getRunFunctionSource(code: string): {
type: 'declaration' | 'expression';
client: string | undefined;
code: string;
} | null {
const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
const printer = ts.createPrinter();
for (const statement of sourceFile.statements) {
// Check for top-level function declarations
if (ts.isFunctionDeclaration(statement)) {
if (statement.name?.text === 'run') {
return {
type: 'declaration',
client: statement.parameters[0]?.name.getText(),
code: printer.printNode(ts.EmitHint.Unspecified, statement.body!, sourceFile),
};
}
}
// Check for variable declarations: const run = () => {} or const run = function() {}
if (ts.isVariableStatement(statement)) {
for (const declaration of statement.declarationList.declarations) {
if (
ts.isIdentifier(declaration.name) &&
declaration.name.text === 'run' &&
// Check if it's initialized with a function
declaration.initializer &&
(ts.isFunctionExpression(declaration.initializer) || ts.isArrowFunction(declaration.initializer))
) {
return {
type: 'expression',
client: declaration.initializer.parameters[0]?.name.getText(),
code: printer.printNode(ts.EmitHint.Unspecified, declaration.initializer, sourceFile),
};
}
}
}
}
return null;
}
function getTSDiagnostics(code: string): string[] {
const functionSource = getRunFunctionSource(code)!;
const codeWithImport = [
'import { ImageKit } from "@imagekit/nodejs";',
functionSource.type === 'declaration' ?
`async function run(${functionSource.client}: ImageKit)`
: `const run: (${functionSource.client}: ImageKit) => Promise<unknown> =`,
functionSource.code,
].join('\n');
const sourcePath = path.resolve('code.ts');
const ast = ts.createSourceFile(sourcePath, codeWithImport, ts.ScriptTarget.Latest, true);
const options = ts.getDefaultCompilerOptions();
options.target = ts.ScriptTarget.Latest;
options.module = ts.ModuleKind.NodeNext;
options.moduleResolution = ts.ModuleResolutionKind.NodeNext;
const host = ts.createCompilerHost(options, true);
const newHost: typeof host = {
...host,
getSourceFile: (...args) => {
if (path.resolve(args[0]) === sourcePath) {
return ast;
}
return host.getSourceFile(...args);
},
readFile: (...args) => {
if (path.resolve(args[0]) === sourcePath) {
return codeWithImport;
}
return host.readFile(...args);
},
fileExists: (...args) => {
if (path.resolve(args[0]) === sourcePath) {
return true;
}
return host.fileExists(...args);
},
};
const program = ts.createProgram({
options,
rootNames: [sourcePath],
host: newHost,
});
const diagnostics = ts.getPreEmitDiagnostics(program, ast);
return diagnostics.map((d) => {
const message = ts.flattenDiagnosticMessageText(d.messageText, '\n');
if (!d.file || !d.start) return `- ${message}`;
const { line: lineNumber } = ts.getLineAndCharacterOfPosition(d.file, d.start);
const line = codeWithImport.split('\n').at(lineNumber)?.trim();
return line ? `- ${message}\n ${line}` : `- ${message}`;
});
}
const fuse = new Fuse(
[
'client.customMetadataFields.create',
'client.customMetadataFields.delete',
'client.customMetadataFields.list',
'client.customMetadataFields.update',
'client.files.copy',
'client.files.delete',
'client.files.get',
'client.files.move',
'client.files.rename',
'client.files.update',
'client.files.upload',
'client.files.bulk.addTags',
'client.files.bulk.delete',
'client.files.bulk.removeAITags',
'client.files.bulk.removeTags',
'client.files.versions.delete',
'client.files.versions.get',
'client.files.versions.list',
'client.files.versions.restore',
'client.files.metadata.get',
'client.files.metadata.getFromURL',
'client.savedExtensions.create',
'client.savedExtensions.delete',
'client.savedExtensions.get',
'client.savedExtensions.list',
'client.savedExtensions.update',
'client.assets.list',
'client.cache.invalidation.create',
'client.cache.invalidation.get',
'client.folders.copy',
'client.folders.create',
'client.folders.delete',
'client.folders.move',
'client.folders.rename',
'client.folders.job.get',
'client.accounts.usage.get',
'client.accounts.origins.create',
'client.accounts.origins.delete',
'client.accounts.origins.get',
'client.accounts.origins.list',
'client.accounts.origins.update',
'client.accounts.urlEndpoints.create',
'client.accounts.urlEndpoints.delete',
'client.accounts.urlEndpoints.get',
'client.accounts.urlEndpoints.list',
'client.accounts.urlEndpoints.update',
'client.beta.v2.files.upload',
'client.webhooks.unsafeUnwrap',
'client.webhooks.unwrap',
],
{ threshold: 1, shouldSort: true },
);
function getMethodSuggestions(fullyQualifiedMethodName: string): string[] {
return fuse
.search(fullyQualifiedMethodName)
.map(({ item }) => item)
.slice(0, 5);
}
const proxyToObj = new WeakMap<any, any>();
const objToProxy = new WeakMap<any, any>();
type ClientProxyConfig = {
path: string[];
isBelievedBad?: boolean;
};
function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T {
let proxy: T = objToProxy.get(obj);
if (!proxy) {
proxy = new Proxy(obj, {
get(target, prop, receiver) {
const propPath = [...path, String(prop)];
const value = Reflect.get(target, prop, receiver);
if (isBelievedBad || (!(prop in target) && value === undefined)) {
// If we're accessing a path that doesn't exist, it will probably eventually error.
// Let's proxy it and mark it bad so that we can control the error message.
// We proxy an empty class so that an invocation or construction attempt is possible.
return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true });
}
if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
return makeSdkProxy(value, { path: propPath, isBelievedBad });
}
return value;
},
apply(target, thisArg, args) {
if (isBelievedBad || typeof target !== 'function') {
const fullyQualifiedMethodName = path.join('.');
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
throw new Error(
`${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`,
);
}
return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
},
construct(target, args, newTarget) {
if (isBelievedBad || typeof target !== 'function') {
const fullyQualifiedMethodName = path.join('.');
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
throw new Error(
`${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`,
);
}
return Reflect.construct(target, args, newTarget);
},
});
objToProxy.set(obj, proxy);
proxyToObj.set(proxy, obj);
}
return proxy;
}
function parseError(code: string, error: unknown): string | undefined {
if (!(error instanceof Error)) return;
const cause = error.cause instanceof Error ? `: ${error.cause.message}` : '';
const message = error.name ? `${error.name}: ${error.message}${cause}` : `${error.message}${cause}`;
try {
// Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
// -1 for the zero-based indexing
const line =
lineNumber &&
code
.split('\n')
.at(parseInt(lineNumber, 10) - 1)
?.trim();
return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
} catch {
return message;
}
}
const fetch = async (req: Request): Promise<Response> => {
const { opts, code } = (await req.json()) as { opts: ClientOptions; code: string };
const runFunctionSource = code ? getRunFunctionSource(code) : null;
if (!runFunctionSource) {
const message =
code ?
'The code is missing a top-level `run` function.'
: 'The code argument is missing. Provide one containing a top-level `run` function.';
return Response.json(
{
is_error: true,
result: `${message} Write code within this template:\n\n\`\`\`\nasync function run(client) {\n // Fill this out\n}\n\`\`\``,
log_lines: [],
err_lines: [],
} satisfies WorkerOutput,
{ status: 400, statusText: 'Code execution error' },
);
}
const diagnostics = getTSDiagnostics(code);
if (diagnostics.length > 0) {
return Response.json(
{
is_error: true,
result: `The code contains TypeScript diagnostics:\n${diagnostics.join('\n')}`,
log_lines: [],
err_lines: [],
} satisfies WorkerOutput,
{ status: 400, statusText: 'Code execution error' },
);
}
const client = new ImageKit({
...opts,
});
const log_lines: string[] = [];
const err_lines: string[] = [];
const originalConsole = globalThis.console;
globalThis.console = {
...originalConsole,
log: (...args: unknown[]) => {
log_lines.push(util.format(...args));
},
error: (...args: unknown[]) => {
err_lines.push(util.format(...args));
},
};
try {
let run_ = async (client: any) => {};
run_ = (await tseval(`${code}\nexport default run;`)).default;
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
return Response.json({
is_error: false,
result,
log_lines,
err_lines,
} satisfies WorkerOutput);
} catch (e) {
return Response.json(
{
is_error: true,
result: parseError(code, e),
log_lines,
err_lines,
} satisfies WorkerOutput,
{ status: 400, statusText: 'Code execution error' },
);
} finally {
globalThis.console = originalConsole;
}
};
export default { fetch };