Skip to content

Commit e02fef2

Browse files
committed
fix(core): Webhooks responding with binary data should not prematurely end the response stream
1 parent 7bda92c commit e02fef2

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

packages/cli/src/WebhookHelpers.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type {
3030
IWebhookResponseData,
3131
IWorkflowDataProxyAdditionalKeys,
3232
IWorkflowExecuteAdditionalData,
33+
WebhookResponseMode,
3334
Workflow,
3435
WorkflowExecuteMode,
3536
} from 'n8n-workflow';
@@ -272,7 +273,7 @@ export async function executeWebhook(
272273
additionalKeys,
273274
undefined,
274275
'onReceived',
275-
);
276+
) as WebhookResponseMode;
276277
const responseCode = workflow.expression.getSimpleParameterValue(
277278
workflowStartNode,
278279
webhookData.webhookDescription.responseCode as string,
@@ -291,7 +292,7 @@ export async function executeWebhook(
291292
'firstEntryJson',
292293
);
293294

294-
if (!['onReceived', 'lastNode', 'responseNode'].includes(responseMode as string)) {
295+
if (!['onReceived', 'lastNode', 'responseNode'].includes(responseMode)) {
295296
// If the mode is not known we error. Is probably best like that instead of using
296297
// the default that people know as early as possible (probably already testing phase)
297298
// that something does not resolve properly.
@@ -562,11 +563,11 @@ export async function executeWebhook(
562563
if (binaryData?.id) {
563564
res.header(response.headers);
564565
const stream = await Container.get(BinaryDataService).getAsStream(binaryData.id);
565-
await pipeline(stream, res);
566+
await pipeline(stream, res, { end: false });
566567
responseCallback(null, { noWebhookResponse: true });
567568
} else if (Buffer.isBuffer(response.body)) {
568569
res.header(response.headers);
569-
res.end(response.body);
570+
res.write(response.body);
570571
responseCallback(null, { noWebhookResponse: true });
571572
} else {
572573
// TODO: This probably needs some more changes depending on the options on the
@@ -595,6 +596,7 @@ export async function executeWebhook(
595596
});
596597
}
597598

599+
res.end();
598600
didSendResponse = true;
599601
})
600602
.catch(async (error) => {
@@ -659,17 +661,9 @@ export async function executeWebhook(
659661
return data;
660662
}
661663

662-
if (responseMode === 'responseNode') {
663-
if (!didSendResponse) {
664-
// Return an error if no Webhook-Response node did send any data
665-
responseCallback(null, {
666-
data: {
667-
message: 'Workflow executed successfully',
668-
},
669-
responseCode,
670-
});
671-
didSendResponse = true;
672-
}
664+
// in `responseNode` mode `responseCallback` is called by `responsePromise`
665+
if (responseMode === 'responseNode' && responsePromise) {
666+
await Promise.allSettled([responsePromise.promise()]);
673667
return undefined;
674668
}
675669

@@ -795,14 +789,15 @@ export async function executeWebhook(
795789
res.setHeader('Content-Type', binaryData.mimeType);
796790
if (binaryData.id) {
797791
const stream = await Container.get(BinaryDataService).getAsStream(binaryData.id);
798-
await pipeline(stream, res);
792+
await pipeline(stream, res, { end: false });
799793
} else {
800-
res.end(Buffer.from(binaryData.data, BINARY_ENCODING));
794+
res.write(Buffer.from(binaryData.data, BINARY_ENCODING));
801795
}
802796

803797
responseCallback(null, {
804798
noWebhookResponse: true,
805799
});
800+
res.end();
806801
}
807802
} else if (responseData === 'noData') {
808803
// Return without data

packages/workflow/src/Interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ export interface IWebhookResponseData {
18341834
}
18351835

18361836
export type WebhookResponseData = 'allEntries' | 'firstEntryJson' | 'firstEntryBinary' | 'noData';
1837-
export type WebhookResponseMode = 'onReceived' | 'lastNode';
1837+
export type WebhookResponseMode = 'onReceived' | 'lastNode' | 'responseNode';
18381838

18391839
export interface INodeTypes {
18401840
getByName(nodeType: string): INodeType | IVersionedNodeType;

0 commit comments

Comments
 (0)