n8n
Version:
n8n Workflow Automation Tool
122 lines • 6.34 kB
JavaScript
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.WaitingWebhooks = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const typedi_1 = require("typedi");
const WebhookHelpers = __importStar(require("../webhooks/WebhookHelpers"));
const NodeTypes_1 = require("../NodeTypes");
const WorkflowExecuteAdditionalData = __importStar(require("../WorkflowExecuteAdditionalData"));
const execution_repository_1 = require("../databases/repositories/execution.repository");
const Logger_1 = require("../Logger");
const conflict_error_1 = require("../errors/response-errors/conflict.error");
const not_found_error_1 = require("../errors/response-errors/not-found.error");
let WaitingWebhooks = class WaitingWebhooks {
constructor(logger, nodeTypes, executionRepository) {
this.logger = logger;
this.nodeTypes = nodeTypes;
this.executionRepository = executionRepository;
this.includeForms = false;
}
logReceivedWebhook(method, executionId) {
this.logger.debug(`Received waiting-webhook "${method}" for execution "${executionId}"`);
}
disableNode(execution, _method) {
execution.data.executionData.nodeExecutionStack[0].node.disabled = true;
}
async executeWebhook(req, res) {
const { path: executionId, suffix } = req.params;
this.logReceivedWebhook(req.method, executionId);
req.params = {};
const execution = await this.executionRepository.findSingleExecution(executionId, {
includeData: true,
unflattenData: true,
});
if (!execution) {
throw new not_found_error_1.NotFoundError(`The execution "${executionId} does not exist.`);
}
if (execution.status === 'running') {
throw new conflict_error_1.ConflictError(`The execution "${executionId} is running already.`);
}
if (execution.finished || execution.data.resultData.error) {
throw new conflict_error_1.ConflictError(`The execution "${executionId} has finished already.`);
}
const lastNodeExecuted = execution.data.resultData.lastNodeExecuted;
this.disableNode(execution, req.method);
execution.data.waitTill = undefined;
execution.data.resultData.runData[lastNodeExecuted].pop();
const { workflowData } = execution;
const workflow = new n8n_workflow_1.Workflow({
id: workflowData.id,
name: workflowData.name,
nodes: workflowData.nodes,
connections: workflowData.connections,
active: workflowData.active,
nodeTypes: this.nodeTypes,
staticData: workflowData.staticData,
settings: workflowData.settings,
});
const workflowStartNode = workflow.getNode(lastNodeExecuted);
if (workflowStartNode === null) {
throw new not_found_error_1.NotFoundError('Could not find node to process webhook.');
}
const additionalData = await WorkflowExecuteAdditionalData.getBase();
const webhookData = n8n_workflow_1.NodeHelpers.getNodeWebhooks(workflow, workflowStartNode, additionalData).find((webhook) => webhook.httpMethod === req.method &&
webhook.path === (suffix !== null && suffix !== void 0 ? suffix : '') &&
webhook.webhookDescription.restartWebhook === true &&
(webhook.webhookDescription.isForm || false) === this.includeForms);
if (webhookData === undefined) {
const errorMessage = `The workflow for execution "${executionId}" does not contain a waiting webhook with a matching path/method.`;
throw new not_found_error_1.NotFoundError(errorMessage);
}
const runExecutionData = execution.data;
return await new Promise((resolve, reject) => {
const executionMode = 'webhook';
void WebhookHelpers.executeWebhook(workflow, webhookData, workflowData, workflowStartNode, executionMode, undefined, runExecutionData, execution.id, req, res, (error, data) => {
if (error !== null) {
return reject(error);
}
resolve(data);
});
});
}
};
exports.WaitingWebhooks = WaitingWebhooks;
exports.WaitingWebhooks = WaitingWebhooks = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [Logger_1.Logger,
NodeTypes_1.NodeTypes,
execution_repository_1.ExecutionRepository])
], WaitingWebhooks);
//# sourceMappingURL=WaitingWebhooks.js.map
;