refactor(core): Parse Webhook request bodies on-demand (#6394)

Also,
1. Consistent CORS support ~on all three webhook types~ waiting webhooks never supported CORS. I'll fix that in another PR
2. [Fixes binary-data handling when request body is text, json, or xml](https://linear.app/n8n/issue/NODE-505/webhook-binary-data-handling-fails-for-textplain-files).
3. Reduced number of middleware that each request has to go through.
4. Removed the need to maintain webhook endpoints in the auth-exception list.
5. Skip all middlewares (apart from `compression`) on Webhook routes. 
6. move `multipart/form-data` support out of individual nodes
7. upgrade `formidable`
8. fix the filenames on binary-data in webhooks nodes
9. add unit tests and integration tests for webhook request handling, and increase test coverage
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-08-01 17:32:30 +02:00
committed by GitHub
parent 369a2e9796
commit 31d8f478ee
29 changed files with 905 additions and 604 deletions

View File

@@ -1,7 +1,7 @@
import { Service } from 'typedi';
import type {
IWebhookData,
WebhookHttpMethod,
IHttpRequestMethods,
Workflow,
WorkflowActivateMode,
WorkflowExecuteMode,
@@ -102,7 +102,7 @@ export class ActiveWebhooks {
*
* @param {(string | undefined)} webhookId
*/
get(httpMethod: WebhookHttpMethod, path: string, webhookId?: string): IWebhookData | undefined {
get(httpMethod: IHttpRequestMethods, path: string, webhookId?: string): IWebhookData | undefined {
const webhookKey = this.getWebhookKey(httpMethod, path, webhookId);
if (this.webhookUrls[webhookKey] === undefined) {
return undefined;
@@ -133,17 +133,10 @@ export class ActiveWebhooks {
/**
* Gets all request methods associated with a single webhook
*/
getWebhookMethods(path: string): string[] {
const methods: string[] = [];
Object.keys(this.webhookUrls)
getWebhookMethods(path: string): IHttpRequestMethods[] {
return Object.keys(this.webhookUrls)
.filter((key) => key.includes(path))
.map((key) => {
methods.push(key.split('|')[0]);
});
return methods;
.map((key) => key.split('|')[0] as IHttpRequestMethods);
}
/**
@@ -159,7 +152,7 @@ export class ActiveWebhooks {
*
* @param {(string | undefined)} webhookId
*/
getWebhookKey(httpMethod: WebhookHttpMethod, path: string, webhookId?: string): string {
getWebhookKey(httpMethod: IHttpRequestMethods, path: string, webhookId?: string): string {
if (webhookId) {
if (path.startsWith(webhookId)) {
const cutFromIndex = path.indexOf('/') + 1;