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

@@ -1235,6 +1235,24 @@ export interface ITriggerResponse {
export type WebhookSetupMethodNames = 'checkExists' | 'create' | 'delete';
export namespace MultiPartFormData {
export interface File {
filepath: string;
mimetype?: string;
originalFilename?: string;
newFilename: string;
}
export type Request = express.Request<
{},
{},
{
data: Record<string, string | string[]>;
files: Record<string, File | File[]>;
}
>;
}
export interface INodeType {
description: INodeTypeDescription;
execute?(
@@ -1492,7 +1510,7 @@ export interface INodeHookDescription {
}
export interface IWebhookData {
httpMethod: WebhookHttpMethod;
httpMethod: IHttpRequestMethods;
node: string;
path: string;
webhookDescription: IWebhookDescription;
@@ -1502,8 +1520,8 @@ export interface IWebhookData {
}
export interface IWebhookDescription {
[key: string]: WebhookHttpMethod | WebhookResponseMode | boolean | string | undefined;
httpMethod: WebhookHttpMethod | string;
[key: string]: IHttpRequestMethods | WebhookResponseMode | boolean | string | undefined;
httpMethod: IHttpRequestMethods | string;
isFullPath?: boolean;
name: 'default' | 'setup';
path: string;
@@ -1555,8 +1573,6 @@ export interface IWorkflowMetadata {
active: boolean;
}
export type WebhookHttpMethod = 'DELETE' | 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'OPTIONS';
export interface IWebhookResponseData {
workflowData?: INodeExecutionData[][];
webhookResponse?: any;
@@ -1811,6 +1827,10 @@ export interface WorkflowTestData {
[key: string]: any[][];
};
};
trigger?: {
mode: WorkflowExecuteMode;
input: INodeExecutionData;
};
}
export type LogTypes = 'debug' | 'verbose' | 'info' | 'warn' | 'error';

View File

@@ -32,7 +32,7 @@ import type {
IWebhookData,
IWorkflowExecuteAdditionalData,
NodeParameterValue,
WebhookHttpMethod,
IHttpRequestMethods,
FieldType,
INodePropertyOptions,
ResourceMapperValue,
@@ -870,8 +870,6 @@ export async function prepareOutputData(
/**
* Returns all the webhooks which should be created for the give node
*
*
*/
export function getNodeWebhooks(
workflow: Workflow,
@@ -968,7 +966,7 @@ export function getNodeWebhooks(
}
returnData.push({
httpMethod: httpMethod.toString() as WebhookHttpMethod,
httpMethod: httpMethod.toString() as IHttpRequestMethods,
node: node.name,
path,
webhookDescription,

View File

@@ -50,6 +50,11 @@ export type { DocMetadata, NativeDoc } from './Extensions';
declare module 'http' {
export interface IncomingMessage {
contentType?: string;
encoding: BufferEncoding;
contentDisposition?: { type: string; filename?: string };
rawBody: Buffer;
readRawBody(): Promise<void>;
_body: boolean;
}
}