(task: WorkerTask)
| 29 | instance = createInstance(); |
| 30 | |
| 31 | export default async function handler(task: WorkerTask): Promise<unknown> { |
| 32 | if (!instance) { |
| 33 | throw new Error('Worker instance not initialized'); |
| 34 | } |
| 35 | |
| 36 | const { fn, args = [] } = task; |
| 37 | |
| 38 | // Type guard to ensure the function exists |
| 39 | if (!(fn in instance)) { |
| 40 | throw new Error(`Unknown function: ${fn}`); |
| 41 | } |
| 42 | |
| 43 | const func = instance[fn]; |
| 44 | |
| 45 | if (typeof func !== 'function') { |
| 46 | throw new Error(`${fn} is not a function`); |
| 47 | } |
| 48 | |
| 49 | const result = await (func as (...args: unknown[]) => unknown)(...args); |
| 50 | |
| 51 | // Parse result if it's a string (most functions return JSON strings) |
| 52 | if (typeof result === 'string' && result.trim().startsWith('{')) { |
| 53 | const parsedResult = JSON.parse(result) as { id?: string }; |
| 54 | |
| 55 | // Free memory immediately if needed |
| 56 | if (parsedResult.id && 'freeMemory' in instance && typeof instance.freeMemory === 'function') { |
| 57 | (instance.freeMemory as (id: string) => void)(parsedResult.id); |
| 58 | delete parsedResult.id; |
| 59 | } |
| 60 | |
| 61 | return parsedResult; |
| 62 | } |
| 63 | |
| 64 | return result; |
| 65 | } |
nothing calls this directly
no outgoing calls
no test coverage detected