Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): report an uncloneable hook result instead of crashing
Yudhi Armyndharis6e798622b341
diff --git a/src/core/plugins/sandbox/worker-hooks.spec.ts b/src/core/plugins/sandbox/worker-hooks.spec.tsindex 5d66d6de5..e65bf4652 100644--- a/src/core/plugins/sandbox/worker-hooks.spec.ts+++ b/src/core/plugins/sandbox/worker-hooks.spec.ts@@ -116,6 +116,25 @@ describe('WorkerHookRegistry', () => {expect(sent.find(m => m.kind === 'hook-result')).toMatchObject({ continue: true, data: { n: 2 }, error: 'boom' });});+ it('reports an uncloneable handler result as an error instead of rejecting the dispatch', async () => {+ // parentPort.postMessage structured-clones the result; a rejection here would be unhandled in the+ // worker (the bootstrap calls handleHook with void) and kill it.+ const sent: WorkerToHostMessage[] = [];+ const post = (m: WorkerToHostMessage): void => {+ sent.push(structuredClone(m));+ };+ const reg = new WorkerHookRegistry(post);+ reg.register('e', ({ data }) => Promise.resolve({ continue: true, data: { ...(data as object), fn: () => 1 } }));++ await expect(+ reg.handleHook({ kind: 'hook', id: 1, event: 'e', data: { x: 1 }, source: 's' }),+ ).resolves.toBeUndefined();++ const result = sent.find(m => m.kind === 'hook-result');+ expect(result).toMatchObject({ id: 1, continue: true, error: expect.stringContaining('could not be sent') });+ expect(result).not.toHaveProperty('data'); // the host keeps the original data+ });+it('reports only the FIRST handler error and stringifies non-Error throws', async () => {const { sent, post } = collect();const reg = new WorkerHookRegistry(post);diff --git a/src/core/plugins/sandbox/worker-hooks.ts b/src/core/plugins/sandbox/worker-hooks.tsindex 29494cdc4..0380e16d1 100644--- a/src/core/plugins/sandbox/worker-hooks.ts+++ b/src/core/plugins/sandbox/worker-hooks.ts@@ -114,6 +114,18 @@ export class WorkerHookRegistry {data,};if (firstError !== undefined) result.error = firstError;- this.post(result);+ try {+ this.post(result);+ } catch (error) {+ // An uncloneable result (a missing await, a function property) throws DataCloneError here. Left+ // unhandled it would kill the worker, so report it like a handler failure; without `data` the host+ // keeps the original data.+ this.post({+ kind: 'hook-result',+ id: message.id,+ continue: shouldContinue,+ error: firstError ?? `hook result could not be sent: ${error instanceof Error ? error.message : String(error)}`,+ });+ }}}