From 70f0755278e0a2bdb61c29623f27623b65473ab4 Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Wed, 13 Dec 2023 05:13:48 +0200 Subject: [PATCH] fix(Webhook Node): Do not create binary data when there is no data in the request (#8000) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://linear.app/n8n/issue/NODE-980/do-not-create-binary-data-for-webhooks-when-there-is-no-data-in-the related: https://github.com/n8n-io/n8n/pull/7804/files#r1422641833 --------- Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ --- .../nodes-base/nodes/Webhook/Webhook.node.ts | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/nodes-base/nodes/Webhook/Webhook.node.ts b/packages/nodes-base/nodes/Webhook/Webhook.node.ts index 69feb42d15..e2f42d25ae 100644 --- a/packages/nodes-base/nodes/Webhook/Webhook.node.ts +++ b/packages/nodes-base/nodes/Webhook/Webhook.node.ts @@ -1,6 +1,7 @@ /* eslint-disable n8n-nodes-base/node-execute-block-wrong-error-thrown */ import { pipeline } from 'stream/promises'; import { createWriteStream } from 'fs'; +import { stat } from 'fs/promises'; import type { IWebhookFunctions, ICredentialDataDecryptedObject, @@ -216,7 +217,6 @@ export class Webhook extends Node { const { data, files } = req.body; const returnItem: INodeExecutionData = { - binary: {}, json: { headers: req.headers, params: req.params, @@ -225,6 +225,10 @@ export class Webhook extends Node { }, }; + if (files?.length) { + returnItem.binary = {}; + } + let count = 0; for (const key of Object.keys(files)) { @@ -274,7 +278,6 @@ export class Webhook extends Node { await pipeline(req, createWriteStream(binaryFile.path)); const returnItem: INodeExecutionData = { - binary: {}, json: { headers: req.headers, params: req.params, @@ -283,20 +286,18 @@ export class Webhook extends Node { }, }; - const binaryPropertyName = (options.binaryPropertyName || 'data') as string; - const fileName = req.contentDisposition?.filename ?? uuid(); - const binaryData = await context.nodeHelpers.copyBinaryFile( - binaryFile.path, - fileName, - req.contentType ?? 'application/octet-stream', - ); - - if (!binaryData.data) { - return { workflowData: [[returnItem]] }; + const stats = await stat(binaryFile.path); + if (stats.size) { + const binaryPropertyName = (options.binaryPropertyName ?? 'data') as string; + const fileName = req.contentDisposition?.filename ?? uuid(); + const binaryData = await context.nodeHelpers.copyBinaryFile( + binaryFile.path, + fileName, + req.contentType ?? 'application/octet-stream', + ); + returnItem.binary = { [binaryPropertyName]: binaryData }; } - returnItem.binary![binaryPropertyName] = binaryData; - return { workflowData: [[returnItem]] }; } catch (error) { throw new NodeOperationError(context.getNode(), error as Error);