n8n
Version:
n8n Workflow Automation Tool
289 lines • 14.6 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 __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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Start = void 0;
const typedi_1 = require("typedi");
const core_1 = require("@oclif/core");
const path_1 = __importDefault(require("path"));
const promises_1 = require("fs/promises");
const fs_1 = require("fs");
const promises_2 = require("stream/promises");
const replacestream_1 = __importDefault(require("replacestream"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const n8n_workflow_1 = require("n8n-workflow");
const config_1 = __importDefault(require("../config"));
const ActiveExecutions_1 = require("../ActiveExecutions");
const ActiveWorkflowManager_1 = require("../ActiveWorkflowManager");
const Server_1 = require("../Server");
const constants_1 = require("../constants");
const MessageEventBus_1 = require("../eventbus/MessageEventBus/MessageEventBus");
const InternalHooks_1 = require("../InternalHooks");
const License_1 = require("../License");
const orchestration_service_1 = require("../services/orchestration.service");
const orchestration_handler_main_service_1 = require("../services/orchestration/main/orchestration.handler.main.service");
const pruning_service_1 = require("../services/pruning.service");
const url_service_1 = require("../services/url.service");
const settings_repository_1 = require("../databases/repositories/settings.repository");
const execution_repository_1 = require("../databases/repositories/execution.repository");
const feature_not_licensed_error_1 = require("../errors/feature-not-licensed.error");
const WaitTracker_1 = require("../WaitTracker");
const BaseCommand_1 = require("./BaseCommand");
const open = require('open');
class Start extends BaseCommand_1.BaseCommand {
constructor(argv, cmdConfig) {
super(argv, cmdConfig);
this.server = typedi_1.Container.get(Server_1.Server);
this.setInstanceType('main');
this.setInstanceQueueModeId();
}
openBrowser() {
const editorUrl = typedi_1.Container.get(url_service_1.UrlService).baseUrl;
open(editorUrl, { wait: true }).catch(() => {
this.logger.info(`\nWas not able to open URL in browser. Please open manually by visiting:\n${editorUrl}\n`);
});
}
async stopProcess() {
var _a;
this.logger.info('\nStopping n8n...');
try {
this.activeWorkflowManager.removeAllQueuedWorkflowActivations();
typedi_1.Container.get(WaitTracker_1.WaitTracker).stopTracking();
await ((_a = this.externalHooks) === null || _a === void 0 ? void 0 : _a.run('n8n.stop', []));
await this.activeWorkflowManager.removeAllTriggerAndPollerBasedWorkflows();
if (typedi_1.Container.get(orchestration_service_1.OrchestrationService).isMultiMainSetupEnabled) {
await typedi_1.Container.get(orchestration_service_1.OrchestrationService).shutdown();
}
await typedi_1.Container.get(InternalHooks_1.InternalHooks).onN8nStop();
await typedi_1.Container.get(ActiveExecutions_1.ActiveExecutions).shutdown();
await typedi_1.Container.get(MessageEventBus_1.MessageEventBus).close();
}
catch (error) {
await this.exitWithCrash('There was an error shutting down n8n.', error);
}
await this.exitSuccessFully();
}
async generateStaticAssets() {
const n8nPath = config_1.default.getEnv('path');
const restEndpoint = config_1.default.getEnv('endpoints.rest');
const hooksUrls = config_1.default.getEnv('externalFrontendHooksUrls');
let scriptsString = '';
if (hooksUrls) {
scriptsString = hooksUrls.split(';').reduce((acc, curr) => {
return `${acc}<script src="${curr}"></script>`;
}, '');
}
const closingTitleTag = '</title>';
const { staticCacheDir } = this.instanceSettings;
const compileFile = async (fileName) => {
const filePath = path_1.default.join(constants_1.EDITOR_UI_DIST_DIR, fileName);
if (/(index\.html)|.*\.(js|css)/.test(filePath) && (0, fs_1.existsSync)(filePath)) {
const destFile = path_1.default.join(staticCacheDir, fileName);
await (0, promises_1.mkdir)(path_1.default.dirname(destFile), { recursive: true });
const streams = [
(0, fs_1.createReadStream)(filePath, 'utf-8'),
(0, replacestream_1.default)('/{{BASE_PATH}}/', n8nPath, { ignoreCase: false }),
(0, replacestream_1.default)('/%7B%7BBASE_PATH%7D%7D/', n8nPath, { ignoreCase: false }),
(0, replacestream_1.default)('/%257B%257BBASE_PATH%257D%257D/', n8nPath, { ignoreCase: false }),
(0, replacestream_1.default)('/static/', n8nPath + 'static/', { ignoreCase: false }),
];
if (filePath.endsWith('index.html')) {
streams.push((0, replacestream_1.default)('{{REST_ENDPOINT}}', restEndpoint, { ignoreCase: false }), (0, replacestream_1.default)(closingTitleTag, closingTitleTag + scriptsString, {
ignoreCase: false,
}));
}
streams.push((0, fs_1.createWriteStream)(destFile, 'utf-8'));
return await (0, promises_2.pipeline)(streams);
}
};
await compileFile('index.html');
const files = await (0, fast_glob_1.default)('**/*.{css,js}', { cwd: constants_1.EDITOR_UI_DIST_DIR });
await Promise.all(files.map(compileFile));
}
async init() {
await this.initCrashJournal();
this.logger.info('Initializing n8n process');
if (config_1.default.getEnv('executions.mode') === 'queue') {
this.logger.debug('Main Instance running in queue mode');
this.logger.debug(`Queue mode id: ${this.queueModeId}`);
}
await super.init();
this.activeWorkflowManager = typedi_1.Container.get(ActiveWorkflowManager_1.ActiveWorkflowManager);
await this.initLicense();
await this.initOrchestration();
this.logger.debug('Orchestration init complete');
typedi_1.Container.get(WaitTracker_1.WaitTracker).init();
this.logger.debug('Wait tracker init complete');
await this.initBinaryDataService();
this.logger.debug('Binary data service init complete');
await this.initExternalHooks();
this.logger.debug('External hooks init complete');
await this.initExternalSecrets();
this.logger.debug('External secrets init complete');
this.initWorkflowHistory();
this.logger.debug('Workflow history init complete');
if (!config_1.default.getEnv('endpoints.disableUi')) {
await this.generateStaticAssets();
}
}
async initOrchestration() {
if (config_1.default.getEnv('executions.mode') !== 'queue')
return;
if (config_1.default.getEnv('multiMainSetup.enabled') &&
!typedi_1.Container.get(License_1.License).isMultipleMainInstancesLicensed()) {
throw new feature_not_licensed_error_1.FeatureNotLicensedError(constants_1.LICENSE_FEATURES.MULTIPLE_MAIN_INSTANCES);
}
const orchestrationService = typedi_1.Container.get(orchestration_service_1.OrchestrationService);
await orchestrationService.init();
await typedi_1.Container.get(orchestration_handler_main_service_1.OrchestrationHandlerMainService).init();
if (!orchestrationService.isMultiMainSetupEnabled)
return;
orchestrationService.multiMainSetup
.on('leader-stepdown', async () => {
await this.license.reinit();
await this.activeWorkflowManager.removeAllTriggerAndPollerBasedWorkflows();
})
.on('leader-takeover', async () => {
await this.license.reinit();
await this.activeWorkflowManager.addAllTriggerAndPollerBasedWorkflows();
});
}
async run() {
var _a, _b;
const { flags } = await this.parse(Start);
const databaseSettings = await typedi_1.Container.get(settings_repository_1.SettingsRepository).findBy({
loadOnStartup: true,
});
databaseSettings.forEach((setting) => {
config_1.default.set(setting.key, (0, n8n_workflow_1.jsonParse)(setting.value, { fallbackValue: setting.value }));
});
const areCommunityPackagesEnabled = config_1.default.getEnv('nodes.communityPackages.enabled');
if (areCommunityPackagesEnabled) {
const { CommunityPackagesService } = await Promise.resolve().then(() => __importStar(require('../services/communityPackages.service')));
await typedi_1.Container.get(CommunityPackagesService).setMissingPackages({
reinstallMissingPackages: flags.reinstallMissingPackages,
});
}
const dbType = config_1.default.getEnv('database.type');
if (dbType === 'sqlite') {
const shouldRunVacuum = config_1.default.getEnv('database.sqlite.executeVacuumOnStartup');
if (shouldRunVacuum) {
await typedi_1.Container.get(execution_repository_1.ExecutionRepository).query('VACUUM;');
}
}
if (flags.tunnel) {
this.log('\nWaiting for tunnel ...');
let tunnelSubdomain = (_b = (_a = process.env.N8N_TUNNEL_SUBDOMAIN) !== null && _a !== void 0 ? _a : this.instanceSettings.tunnelSubdomain) !== null && _b !== void 0 ? _b : '';
if (tunnelSubdomain === '') {
const availableCharacters = 'abcdefghijklmnopqrstuvwxyz0123456789';
tunnelSubdomain = Array.from({ length: 24 })
.map(() => availableCharacters.charAt(Math.floor(Math.random() * availableCharacters.length)))
.join('');
this.instanceSettings.update({ tunnelSubdomain });
}
const { default: localtunnel } = await Promise.resolve().then(() => __importStar(require('@n8n/localtunnel')));
const port = config_1.default.getEnv('port');
const webhookTunnel = await localtunnel(port, {
host: 'https://hooks.n8n.cloud',
subdomain: tunnelSubdomain,
});
process.env.WEBHOOK_URL = `${webhookTunnel.url}/`;
this.log(`Tunnel URL: ${process.env.WEBHOOK_URL}\n`);
this.log('IMPORTANT! Do not share with anybody as it would give people access to your n8n instance!');
}
await this.server.start();
await this.initPruning();
await this.activeWorkflowManager.init();
const editorUrl = typedi_1.Container.get(url_service_1.UrlService).baseUrl;
this.log(`\nEditor is now accessible via:\n${editorUrl}`);
if (Boolean(process.stdout.isTTY) && process.stdin.setRawMode) {
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
if (flags.open) {
this.openBrowser();
}
this.log('\nPress "o" to open in Browser.');
process.stdin.on('data', (key) => {
if (key === 'o') {
this.openBrowser();
}
else if (key.charCodeAt(0) === 3) {
void this.stopProcess();
}
else {
if (key.charCodeAt(0) === 13) {
process.stdout.write('\n');
}
else {
process.stdout.write(key);
}
}
});
}
}
async initPruning() {
this.pruningService = typedi_1.Container.get(pruning_service_1.PruningService);
this.pruningService.startPruning();
if (config_1.default.getEnv('executions.mode') !== 'queue')
return;
const orchestrationService = typedi_1.Container.get(orchestration_service_1.OrchestrationService);
await orchestrationService.init();
if (!orchestrationService.isMultiMainSetupEnabled)
return;
orchestrationService.multiMainSetup
.on('leader-stepdown', () => this.pruningService.stopPruning())
.on('leader-takeover', () => this.pruningService.startPruning());
}
async catch(error) {
if (error.stack)
this.logger.error(error.stack);
await this.exitWithCrash('Exiting due to an error.', error);
}
}
exports.Start = Start;
Start.description = 'Starts n8n. Makes Web-UI available and starts active workflows';
Start.examples = [
'$ n8n start',
'$ n8n start --tunnel',
'$ n8n start -o',
'$ n8n start --tunnel -o',
];
Start.flags = {
help: core_1.Flags.help({ char: 'h' }),
open: core_1.Flags.boolean({
char: 'o',
description: 'opens the UI automatically in browser',
}),
tunnel: core_1.Flags.boolean({
description: 'runs the webhooks via a hooks.n8n.cloud tunnel server. Use only for testing and development!',
}),
reinstallMissingPackages: core_1.Flags.boolean({
description: 'Attempts to self heal n8n if packages with nodes are missing. Might drastically increase startup times.',
}),
};
//# sourceMappingURL=start.js.map