Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): drop the pending capability call when it cannot be posted

Yudhi Armyndharis98c45fb13356
diff --git a/src/core/plugins/sandbox/worker-capability.spec.ts b/src/core/plugins/sandbox/worker-capability.spec.ts
index ec8867ce1..c530d5e78 100644
--- a/src/core/plugins/sandbox/worker-capability.spec.ts
+++ b/src/core/plugins/sandbox/worker-capability.spec.ts
@@ -15,6 +15,18 @@ describe('WorkerCapabilityClient', () => {
await expect(pending).resolves.toEqual({ messageId: 'm' });
});
+ it('rejects without keeping a pending entry when the call cannot be posted', async () => {
+ // postMessage throws DataCloneError synchronously for an uncloneable arg (a Promise, a function).
+ const client = new WorkerCapabilityClient(() => {
+ throw new Error('could not be cloned');
+ });
+
+ await expect(client.call('messages.sendText', ['s', 'c', Promise.resolve('x')])).rejects.toThrow(
+ 'could not be cloned',
+ );
+ expect((client as unknown as { pending: Map<number, unknown> }).pending.size).toBe(0);
+ });
+
it('rejects on an error cap-result', async () => {
const sent: WorkerToHostMessage[] = [];
const client = new WorkerCapabilityClient(m => sent.push(m));
diff --git a/src/core/plugins/sandbox/worker-capability.ts b/src/core/plugins/sandbox/worker-capability.ts
index 493b634f8..3ad046fea 100644
--- a/src/core/plugins/sandbox/worker-capability.ts
+++ b/src/core/plugins/sandbox/worker-capability.ts
@@ -16,9 +16,11 @@ export class WorkerCapabilityClient {
call(verb: string, args: unknown[]): Promise<unknown> {
const id = this.nextId++;
return new Promise((resolve, reject) => {
- this.pending.set(id, { resolve, reject });
const inFlight = hookConfigStore.getStore()?.inFlight;
+ // Post before registering: an uncloneable arg throws here (rejecting this promise), and an entry
+ // registered first would never be removed, since no cap-result can come back for it.
this.post(inFlight ? { kind: 'cap', id, verb, args, inFlight } : { kind: 'cap', id, verb, args });
+ this.pending.set(id, { resolve, reject });
});
}