UNPKG

n8n

Version:

n8n Workflow Automation Tool

142 lines 7.45 kB
"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.LiveWebhooks = void 0; const typedi_1 = require("typedi"); const n8n_workflow_1 = require("n8n-workflow"); const workflow_repository_1 = require("../databases/repositories/workflow.repository"); const Logger_1 = require("../Logger"); const NodeTypes_1 = require("../NodeTypes"); const webhook_service_1 = require("../webhooks/webhook.service"); const webhook_not_found_error_1 = require("../errors/response-errors/webhook-not-found.error"); const not_found_error_1 = require("../errors/response-errors/not-found.error"); const WorkflowExecuteAdditionalData = __importStar(require("../WorkflowExecuteAdditionalData")); const WebhookHelpers = __importStar(require("../webhooks/WebhookHelpers")); const workflowStaticData_service_1 = require("../workflows/workflowStaticData.service"); let LiveWebhooks = class LiveWebhooks { constructor(logger, nodeTypes, webhookService, workflowRepository, workflowStaticDataService) { this.logger = logger; this.nodeTypes = nodeTypes; this.webhookService = webhookService; this.workflowRepository = workflowRepository; this.workflowStaticDataService = workflowStaticDataService; } async getWebhookMethods(path) { return await this.webhookService.getWebhookMethods(path); } async findAccessControlOptions(path, httpMethod) { var _a; const webhook = await this.findWebhook(path, httpMethod); const workflowData = await this.workflowRepository.findOne({ where: { id: webhook.workflowId }, select: ['nodes'], }); const nodes = workflowData === null || workflowData === void 0 ? void 0 : workflowData.nodes; const webhookNode = nodes === null || nodes === void 0 ? void 0 : 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 executeWebhook(request, response) { const httpMethod = request.method; const path = request.params.path; this.logger.debug(`Received webhook "${httpMethod}" for path "${path}"`); request.params = {}; const webhook = await this.findWebhook(path, httpMethod); if (webhook.isDynamic) { const pathElements = path.split('/').slice(1); webhook.webhookPath.split('/').forEach((ele, index) => { if (ele.startsWith(':')) { request.params[ele.slice(1)] = pathElements[index]; } }); } const workflowData = await this.workflowRepository.findOne({ where: { id: webhook.workflowId }, relations: { shared: { project: { projectRelations: true } } }, }); if (workflowData === null) { throw new not_found_error_1.NotFoundError(`Could not find workflow with id "${webhook.workflowId}"`); } const workflow = new n8n_workflow_1.Workflow({ id: webhook.workflowId, name: workflowData.name, nodes: workflowData.nodes, connections: workflowData.connections, active: workflowData.active, nodeTypes: this.nodeTypes, staticData: workflowData.staticData, settings: workflowData.settings, }); const additionalData = await WorkflowExecuteAdditionalData.getBase(); const webhookData = n8n_workflow_1.NodeHelpers.getNodeWebhooks(workflow, workflow.getNode(webhook.node), additionalData).find((w) => w.httpMethod === httpMethod && w.path === webhook.webhookPath); const workflowStartNode = workflow.getNode(webhookData.node); if (workflowStartNode === null) { throw new not_found_error_1.NotFoundError('Could not find node to process webhook.'); } return await new Promise((resolve, reject) => { const executionMode = 'webhook'; void WebhookHelpers.executeWebhook(workflow, webhookData, workflowData, workflowStartNode, executionMode, undefined, undefined, undefined, request, response, async (error, data) => { if (error !== null) { return reject(error); } await this.workflowStaticDataService.saveStaticData(workflow); resolve(data); }); }); } async findWebhook(path, httpMethod) { if (path.endsWith('/')) { path = path.slice(0, -1); } const webhook = await this.webhookService.findWebhook(httpMethod, path); if (webhook === null) { throw new webhook_not_found_error_1.WebhookNotFoundError({ path, httpMethod }, { hint: 'production' }); } return webhook; } }; exports.LiveWebhooks = LiveWebhooks; exports.LiveWebhooks = LiveWebhooks = __decorate([ (0, typedi_1.Service)(), __metadata("design:paramtypes", [Logger_1.Logger, NodeTypes_1.NodeTypes, webhook_service_1.WebhookService, workflow_repository_1.WorkflowRepository, workflowStaticData_service_1.WorkflowStaticDataService]) ], LiveWebhooks); //# sourceMappingURL=LiveWebhooks.js.map