fix(Webhook Node): Do not create binary data when there is no data in the request (#8000)

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: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Michael Kret 2023-12-13 05:13:48 +02:00 committed by GitHub
parent 61129863f1
commit 70f0755278
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
/* eslint-disable n8n-nodes-base/node-execute-block-wrong-error-thrown */ /* eslint-disable n8n-nodes-base/node-execute-block-wrong-error-thrown */
import { pipeline } from 'stream/promises'; import { pipeline } from 'stream/promises';
import { createWriteStream } from 'fs'; import { createWriteStream } from 'fs';
import { stat } from 'fs/promises';
import type { import type {
IWebhookFunctions, IWebhookFunctions,
ICredentialDataDecryptedObject, ICredentialDataDecryptedObject,
@ -216,7 +217,6 @@ export class Webhook extends Node {
const { data, files } = req.body; const { data, files } = req.body;
const returnItem: INodeExecutionData = { const returnItem: INodeExecutionData = {
binary: {},
json: { json: {
headers: req.headers, headers: req.headers,
params: req.params, params: req.params,
@ -225,6 +225,10 @@ export class Webhook extends Node {
}, },
}; };
if (files?.length) {
returnItem.binary = {};
}
let count = 0; let count = 0;
for (const key of Object.keys(files)) { for (const key of Object.keys(files)) {
@ -274,7 +278,6 @@ export class Webhook extends Node {
await pipeline(req, createWriteStream(binaryFile.path)); await pipeline(req, createWriteStream(binaryFile.path));
const returnItem: INodeExecutionData = { const returnItem: INodeExecutionData = {
binary: {},
json: { json: {
headers: req.headers, headers: req.headers,
params: req.params, params: req.params,
@ -283,20 +286,18 @@ export class Webhook extends Node {
}, },
}; };
const binaryPropertyName = (options.binaryPropertyName || 'data') as string; const stats = await stat(binaryFile.path);
if (stats.size) {
const binaryPropertyName = (options.binaryPropertyName ?? 'data') as string;
const fileName = req.contentDisposition?.filename ?? uuid(); const fileName = req.contentDisposition?.filename ?? uuid();
const binaryData = await context.nodeHelpers.copyBinaryFile( const binaryData = await context.nodeHelpers.copyBinaryFile(
binaryFile.path, binaryFile.path,
fileName, fileName,
req.contentType ?? 'application/octet-stream', req.contentType ?? 'application/octet-stream',
); );
returnItem.binary = { [binaryPropertyName]: binaryData };
if (!binaryData.data) {
return { workflowData: [[returnItem]] };
} }
returnItem.binary![binaryPropertyName] = binaryData;
return { workflowData: [[returnItem]] }; return { workflowData: [[returnItem]] };
} catch (error) { } catch (error) {
throw new NodeOperationError(context.getNode(), error as Error); throw new NodeOperationError(context.getNode(), error as Error);