Skip to content

Commit

Permalink
fix(Webhook Node): Do not create binary data when there is no data in…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
michael-radency and netroy committed Dec 13, 2023
1 parent 6112986 commit 70f0755
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions packages/nodes-base/nodes/Webhook/Webhook.node.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -225,6 +225,10 @@ export class Webhook extends Node {
},
};

if (files?.length) {
returnItem.binary = {};
}

let count = 0;

for (const key of Object.keys(files)) {
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 70f0755

Please sign in to comment.