n8n
Version:
n8n Workflow Automation Tool
294 lines • 13.9 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestWebhooks = void 0;
const typedi_1 = require("typedi");
const n8n_workflow_1 = require("n8n-workflow");
const push_1 = require("../push");
const NodeTypes_1 = require("../NodeTypes");
const WebhookHelpers = __importStar(require("../webhooks/WebhookHelpers"));
const constants_1 = require("../constants");
const not_found_error_1 = require("../errors/response-errors/not-found.error");
const workflow_missing_id_error_1 = require("../errors/workflow-missing-id.error");
const webhook_not_found_error_1 = require("../errors/response-errors/webhook-not-found.error");
const NodeExecuteFunctions = __importStar(require("n8n-core"));
const utils_1 = require("../utils");
const test_webhook_registrations_service_1 = require("../webhooks/test-webhook-registrations.service");
const orchestration_service_1 = require("../services/orchestration.service");
const WorkflowExecuteAdditionalData = __importStar(require("../WorkflowExecuteAdditionalData"));
let TestWebhooks = class TestWebhooks {
constructor(push, nodeTypes, registrations, orchestrationService) {
this.push = push;
this.nodeTypes = nodeTypes;
this.registrations = registrations;
this.orchestrationService = orchestrationService;
this.timeouts = {};
}
async executeWebhook(request, response) {
const httpMethod = request.method;
let path = (0, utils_1.removeTrailingSlash)(request.params.path);
request.params = {};
let webhook = await this.getActiveWebhook(httpMethod, path);
if (!webhook) {
const [webhookId, ...segments] = path.split('/');
webhook = await this.getActiveWebhook(httpMethod, segments.join('/'), webhookId);
if (!webhook)
throw new webhook_not_found_error_1.WebhookNotFoundError({
path,
httpMethod,
webhookMethods: await this.getWebhookMethods(path),
});
path = webhook.path;
path.split('/').forEach((segment, index) => {
if (segment.startsWith(':')) {
request.params[segment.slice(1)] = segments[index];
}
});
}
const key = this.registrations.toKey(webhook);
const registration = await this.registrations.get(key);
if (!registration) {
throw new webhook_not_found_error_1.WebhookNotFoundError({
path,
httpMethod,
webhookMethods: await this.getWebhookMethods(path),
});
}
const { destinationNode, pushRef, workflowEntity, webhook: testWebhook } = registration;
const workflow = this.toWorkflow(workflowEntity);
if (testWebhook.staticData)
workflow.setTestStaticData(testWebhook.staticData);
const workflowStartNode = workflow.getNode(webhook.node);
if (workflowStartNode === null) {
throw new not_found_error_1.NotFoundError('Could not find node to process webhook.');
}
return await new Promise(async (resolve, reject) => {
try {
const executionMode = 'manual';
const executionId = await WebhookHelpers.executeWebhook(workflow, webhook, workflowEntity, workflowStartNode, executionMode, pushRef, undefined, undefined, request, response, (error, data) => {
if (error !== null)
reject(error);
else
resolve(data);
}, destinationNode);
if (executionId === undefined)
return;
if (pushRef !== undefined) {
this.push.send('testWebhookReceived', { workflowId: webhook === null || webhook === void 0 ? void 0 : webhook.workflowId, executionId }, pushRef);
}
}
catch { }
if (this.orchestrationService.isMultiMainSetupEnabled &&
pushRef &&
!this.push.getBackend().hasPushRef(pushRef)) {
const payload = { webhookKey: key, workflowEntity, pushRef };
void this.orchestrationService.publish('clear-test-webhooks', payload);
return;
}
this.clearTimeout(key);
await this.deactivateWebhooks(workflow);
});
}
clearTimeout(key) {
const timeout = this.timeouts[key];
if (timeout)
clearTimeout(timeout);
}
async getWebhookMethods(path) {
const allKeys = await this.registrations.getAllKeys();
const webhookMethods = allKeys
.filter((key) => key.includes(path))
.map((key) => key.split('|')[0]);
if (!webhookMethods.length)
throw new webhook_not_found_error_1.WebhookNotFoundError({ path });
return webhookMethods;
}
async findAccessControlOptions(path, httpMethod) {
var _a;
const allKeys = await this.registrations.getAllKeys();
const webhookKey = allKeys.find((key) => key.includes(path) && key.startsWith(httpMethod));
if (!webhookKey)
return;
const registration = await this.registrations.get(webhookKey);
if (!registration)
return;
const { workflowEntity } = registration;
const workflow = this.toWorkflow(workflowEntity);
const webhookNode = Object.values(workflow.nodes).find(({ type, parameters, typeVersion }) => {
var _a;
return (parameters === null || parameters === void 0 ? void 0 : parameters.path) === path &&
((_a = parameters === null || parameters === void 0 ? void 0 : parameters.httpMethod) !== null && _a !== void 0 ? _a : 'GET') === httpMethod &&
'webhook' in this.nodeTypes.getByNameAndVersion(type, typeVersion);
});
return (_a = webhookNode === null || webhookNode === void 0 ? void 0 : webhookNode.parameters) === null || _a === void 0 ? void 0 : _a.options;
}
async needsWebhook(userId, workflowEntity, additionalData, runData, pushRef, destinationNode) {
if (!workflowEntity.id)
throw new workflow_missing_id_error_1.WorkflowMissingIdError(workflowEntity);
const workflow = this.toWorkflow(workflowEntity);
const webhooks = WebhookHelpers.getWorkflowWebhooks(workflow, additionalData, destinationNode, true);
if (!webhooks.some((w) => w.webhookDescription.restartWebhook !== true)) {
return false;
}
const timeout = setTimeout(async () => await this.cancelWebhook(workflow.id), constants_1.TEST_WEBHOOK_TIMEOUT);
for (const webhook of webhooks) {
const key = this.registrations.toKey(webhook);
const registrationByKey = await this.registrations.get(key);
if (runData && webhook.node in runData) {
return false;
}
if (registrationByKey &&
!webhook.webhookId &&
!registrationByKey.webhook.isTest &&
registrationByKey.webhook.userId !== userId &&
registrationByKey.webhook.workflowId !== workflow.id) {
throw new n8n_workflow_1.WebhookPathTakenError(webhook.node);
}
webhook.path = (0, utils_1.removeTrailingSlash)(webhook.path);
webhook.isTest = true;
const { workflowExecuteAdditionalData: _, ...cacheableWebhook } = webhook;
cacheableWebhook.userId = userId;
const registration = {
pushRef,
workflowEntity,
destinationNode,
webhook: cacheableWebhook,
};
try {
await this.registrations.register(registration);
await workflow.createWebhookIfNotExists(webhook, NodeExecuteFunctions, 'manual', 'manual');
cacheableWebhook.staticData = workflow.staticData;
await this.registrations.register(registration);
this.timeouts[key] = timeout;
}
catch (error) {
await this.deactivateWebhooks(workflow);
delete this.timeouts[key];
throw error;
}
}
return true;
}
async cancelWebhook(workflowId) {
let foundWebhook = false;
const allWebhookKeys = await this.registrations.getAllKeys();
for (const key of allWebhookKeys) {
const registration = await this.registrations.get(key);
if (!registration)
continue;
const { pushRef, workflowEntity } = registration;
const workflow = this.toWorkflow(workflowEntity);
if (workflowEntity.id !== workflowId)
continue;
this.clearTimeout(key);
if (pushRef !== undefined) {
try {
this.push.send('testWebhookDeleted', { workflowId }, pushRef);
}
catch {
}
}
if (!foundWebhook) {
void this.deactivateWebhooks(workflow);
}
foundWebhook = true;
}
return foundWebhook;
}
async getActiveWebhook(httpMethod, path, webhookId) {
const key = this.registrations.toKey({ httpMethod, path, webhookId });
let webhook;
let maxMatches = 0;
const pathElementsSet = new Set(path.split('/'));
const registration = await this.registrations.get(key);
if (!registration)
return;
const { webhook: dynamicWebhook } = registration;
const staticElements = dynamicWebhook.path.split('/').filter((ele) => !ele.startsWith(':'));
const allStaticExist = staticElements.every((staticEle) => pathElementsSet.has(staticEle));
if (allStaticExist && staticElements.length > maxMatches) {
maxMatches = staticElements.length;
webhook = dynamicWebhook;
}
else if (staticElements.length === 0 && !webhook) {
webhook = dynamicWebhook;
}
return webhook;
}
async deactivateWebhooks(workflow) {
const allRegistrations = await this.registrations.getAllRegistrations();
if (!allRegistrations.length)
return;
const webhooksByWorkflow = allRegistrations.reduce((acc, cur) => {
const { workflowId } = cur.webhook;
acc[workflowId] || (acc[workflowId] = []);
acc[workflowId].push(cur.webhook);
return acc;
}, {});
const webhooks = webhooksByWorkflow[workflow.id];
if (!webhooks)
return;
for (const webhook of webhooks) {
const { userId, staticData } = webhook;
if (userId) {
webhook.workflowExecuteAdditionalData = await WorkflowExecuteAdditionalData.getBase(userId);
}
if (staticData)
workflow.staticData = staticData;
await workflow.deleteWebhook(webhook, NodeExecuteFunctions, 'internal', 'update');
}
await this.registrations.deregisterAll();
}
toWorkflow(workflowEntity) {
return new n8n_workflow_1.Workflow({
id: workflowEntity.id,
name: workflowEntity.name,
nodes: workflowEntity.nodes,
connections: workflowEntity.connections,
active: false,
nodeTypes: this.nodeTypes,
staticData: {},
settings: workflowEntity.settings,
});
}
};
exports.TestWebhooks = TestWebhooks;
exports.TestWebhooks = TestWebhooks = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [push_1.Push,
NodeTypes_1.NodeTypes,
test_webhook_registrations_service_1.TestWebhookRegistrationsService,
orchestration_service_1.OrchestrationService])
], TestWebhooks);
//# sourceMappingURL=TestWebhooks.js.map