From 246213ba5d0b17f43a0693f8353fdb289248bd86 Mon Sep 17 00:00:00 2001 From: ricardo Date: Fri, 20 Mar 2020 14:53:51 -0400 Subject: [PATCH 1/3] :zap: added support to binary data --- packages/nodes-base/nodes/Webhook.node.ts | 68 +++++++++++++++++------ 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/packages/nodes-base/nodes/Webhook.node.ts b/packages/nodes-base/nodes/Webhook.node.ts index a05bf1ccc..3f5a3822a 100644 --- a/packages/nodes-base/nodes/Webhook.node.ts +++ b/packages/nodes-base/nodes/Webhook.node.ts @@ -211,6 +211,28 @@ export class Webhook implements INodeType { }, description: 'Name of the binary property to return', }, + { + displayName: 'Binary Data', + name: 'binaryData', + type: 'boolean', + default: false, + description: 'If the data to upload should be taken from binary field.', + }, + { + displayName: 'Binary Property', + name: 'binaryPropertyName', + type: 'string', + default: 'data', + required: true, + displayOptions: { + show: { + binaryData: [ + true, + ], + }, + }, + description: 'Name of the binary property which contains
the data for the file to be uploaded.', + }, { displayName: 'Options', name: 'options', @@ -253,13 +275,6 @@ export class Webhook implements INodeType { default: 'data', description: 'Name of the property to return the data of instead of the whole JSON.', }, - { - displayName: 'Raw Body', - name: 'rawBody', - type: 'boolean', - default: false, - description: 'Raw body (binary)', - }, ], }, ], @@ -268,7 +283,7 @@ export class Webhook implements INodeType { async webhook(this: IWebhookFunctions): Promise { const authentication = this.getNodeParameter('authentication', 0) as string; - const options = this.getNodeParameter('options', 0) as IDataObject; + const binaryData = this.getNodeParameter('binaryData', 0) as boolean; const req = this.getRequestObject(); const resp = this.getResponseObject(); const headers = this.getHeaderData(); @@ -345,6 +360,34 @@ export class Webhook implements INodeType { }); } + if (binaryData) { + return new Promise((resolve, reject) => { + const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0) as string; + const data: Buffer[] = []; + + req.on('data', (chunk) => { + data.push(chunk); + }); + + req.on('end', () => { + const returnData: IDataObject[] = [{}]; + set(returnData[0], `binary[${binaryPropertyName}]`, { + data: Buffer.concat(data).toString('base64'), + mimeType: req.headers['content-type'], + }); + return resolve({ + workflowData: [ + returnData as INodeExecutionData[], + ], + }); + }); + + req.on('error', (err) => { + throw new Error(err.message); + }); + }); + } + const response: INodeExecutionData = { json: { body: this.getBodyData(), @@ -352,15 +395,6 @@ export class Webhook implements INodeType { query: this.getQueryData(), }, }; - if (options.rawBody) { - response.binary = { - data: { - // @ts-ignore - data: req.rawBody.toString('base64'), - mimeType, - } - }; - } return { workflowData: [ From b3fe74a655fd6aac260dd820fcfa55ecce0d991b Mon Sep 17 00:00:00 2001 From: ricardo Date: Fri, 20 Mar 2020 18:44:01 -0400 Subject: [PATCH 2/3] :zap: added back rawBody field --- packages/nodes-base/nodes/Webhook.node.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/nodes-base/nodes/Webhook.node.ts b/packages/nodes-base/nodes/Webhook.node.ts index 3f5a3822a..2f5e63301 100644 --- a/packages/nodes-base/nodes/Webhook.node.ts +++ b/packages/nodes-base/nodes/Webhook.node.ts @@ -275,14 +275,21 @@ export class Webhook implements INodeType { default: 'data', description: 'Name of the property to return the data of instead of the whole JSON.', }, + { + displayName: 'Raw Body', + name: 'rawBody', + type: 'boolean', + default: false, + description: 'Raw body (binary)', + }, ], }, ], }; - async webhook(this: IWebhookFunctions): Promise { const authentication = this.getNodeParameter('authentication', 0) as string; + const options = this.getNodeParameter('options', 0) as IDataObject; const binaryData = this.getNodeParameter('binaryData', 0) as boolean; const req = this.getRequestObject(); const resp = this.getResponseObject(); @@ -396,6 +403,16 @@ export class Webhook implements INodeType { }, }; + if (options.rawBody) { + response.binary = { + data: { + // @ts-ignore + data: req.rawBody.toString('base64'), + mimeType, + } + }; + } + return { workflowData: [ [ From 1534335b589a5b3fb1e937ba8642cf3c99cb4b9f Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sat, 21 Mar 2020 23:39:40 +0100 Subject: [PATCH 3/3] :zap: Improve Webhook receive binary functionality --- packages/nodes-base/nodes/Webhook.node.ts | 84 ++++++++++++++--------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/packages/nodes-base/nodes/Webhook.node.ts b/packages/nodes-base/nodes/Webhook.node.ts index da5fe5fe3..00e56a13f 100644 --- a/packages/nodes-base/nodes/Webhook.node.ts +++ b/packages/nodes-base/nodes/Webhook.node.ts @@ -212,28 +212,6 @@ export class Webhook implements INodeType { }, description: 'Name of the binary property to return', }, - { - displayName: 'Binary Data', - name: 'binaryData', - type: 'boolean', - default: false, - description: 'If the data to upload should be taken from binary field.', - }, - { - displayName: 'Binary Property', - name: 'binaryPropertyName', - type: 'string', - default: 'data', - required: true, - displayOptions: { - show: { - binaryData: [ - true, - ], - }, - }, - description: 'Name of the binary property which contains
the data for the file to be uploaded.', - }, { displayName: 'Options', name: 'options', @@ -241,6 +219,35 @@ export class Webhook implements INodeType { placeholder: 'Add Option', default: {}, options: [ + { + displayName: 'Binary Data', + name: 'binaryData', + type: 'boolean', + displayOptions: { + show: { + '/httpMethod': [ + 'POST', + ], + }, + }, + default: false, + description: 'Set to true if webhook will receive binary data.', + }, + { + displayName: 'Binary Property', + name: 'binaryPropertyName', + type: 'string', + default: 'data', + required: true, + displayOptions: { + show: { + binaryData: [ + true, + ], + }, + }, + description: 'Name of the binary property to which to
write the data of the received file.', + }, { displayName: 'Response Content-Type', name: 'responseContentType', @@ -280,6 +287,13 @@ export class Webhook implements INodeType { displayName: 'Raw Body', name: 'rawBody', type: 'boolean', + displayOptions: { + hide: { + binaryData: [ + true, + ], + }, + }, default: false, description: 'Raw body (binary)', }, @@ -289,9 +303,8 @@ export class Webhook implements INodeType { }; async webhook(this: IWebhookFunctions): Promise { - const authentication = this.getNodeParameter('authentication', 0) as string; - const options = this.getNodeParameter('options', 0) as IDataObject; - const binaryData = this.getNodeParameter('binaryData', 0) as boolean; + const authentication = this.getNodeParameter('authentication') as string; + const options = this.getNodeParameter('options', {}) as IDataObject; const req = this.getRequestObject(); const resp = this.getResponseObject(); const headers = this.getHeaderData(); @@ -369,24 +382,33 @@ export class Webhook implements INodeType { }); } - if (binaryData) { + if (options.binaryData === true) { return new Promise((resolve, reject) => { - const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0) as string; + const binaryPropertyName = options.binaryPropertyName || 'data'; const data: Buffer[] = []; req.on('data', (chunk) => { data.push(chunk); }); - req.on('end', () => { - const returnData: IDataObject[] = [{}]; + req.on('end', async () => { + const returnItem: INodeExecutionData = { + binary: {}, + json: {}, + }; + + const returnData: IDataObject[] = [{ json: {} }]; set(returnData[0], `binary[${binaryPropertyName}]`, { - data: Buffer.concat(data).toString('base64'), + data: Buffer.concat(data).toString(BINARY_ENCODING), mimeType: req.headers['content-type'], }); + returnItem.binary![binaryPropertyName as string] = await this.helpers.prepareBinaryData(Buffer.concat(data)); + return resolve({ workflowData: [ - returnData as INodeExecutionData[], + [ + returnItem + ] ], }); });