mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-11 07:04:06 -08:00
0da338f9b5
* feat(editor): Usage and plan page (#4793) feat(editor): usage and plan page * feat(editor): Update Usage and plan page (#4842) * feat(editor): usage and plan store * feat(editor): usage and plan page updates * feat(editor): usage and plan add buttons and alert * tes(editor): usage and plan store * tes(editor): usage remove refresh button and add link to view plans * tes(editor): usage use info tip * tes(editor): usage info style * feat(editor): Get quotas data (#4866) feat(editor): get quotas data * feat(editor): In-app experience (#4875) * feat: Add license quotas endpoint * feat: Add trigger count to workflow activation process * refactor: Get quotas from db * feat: Add license information * ✨ - finalised GET /license endpoint * 🔨 - getActiveTriggerCount return 0 instead of null * 🐛 - ignore manualTrigger when counting active triggers * ✨ - add activation endpoint * ✨ - added renew endpoint * 🔨 - added return type interfaces * 🔨 - handle license errors where methods are called * 🔨 - rename function to match name from lib * feat(editor): usage add plans buttons logic * 🚨 - testing new License methods * feat(editor): usage add more business logic * chore(editor): code formatting * 🚨 - added license api tests * fix(editor): usage store * fix(editor): usage update translations * feat(editor): usage add license activation modal * feat(editor): usage change subscription app url * feat(editor): usage add contact us link * feat(editor): usage fix modal width * ✨ - Add renewal tracking metric * ✨ - add license data to pulse event * 🔨 - set default triggercount on entity model * ✨ - add db migrations for mysql and postgres * fix(editor): Usage api call data processing and error handling * fix(editor): Usage fix activation query key * 🚨 - add initDb to telemetry tests * 🔨 - move getlicensedata to licenseservice * 🔨 - return 403 instead of 404 to non owners * 🔨 - move owner checking to middleware * 🐛 - fixed incorrectly returned error from middleware * 🐛 - using mock instead of test db for pulse tests * fix(editor): Usage fix activation and add success messages * fix(editor): Usage should not renew activation right after activation * 🚨 - skipping failing pulse tests for now * fix(editor): Usage add telemetry calls and apply design review outcomes * feat(editor): Hide usage page according to BE flag * feat(editor): Usage modify key activation flow * feat(editor): Usage change subscription app url * feat(editor): Usage add telemetry for manage plan * feat(editor): Usage extend link url query params * feat(editor): Usage add line chart if there is a workflow limit * feat(editor): Usage remove query after key activation redirection * fix(editor): Usage handle limit exceeded workflow chart, add focus to input when modal opened * fix(editor): Usage activation can return router promise when removing query * fix(editor): Usage and plan design review * 🐛 - fix renew endpoint hanging issue * 🐛 - fix license activation bug * fix(editor): Usage proper translation for plans and/or editions * fix(editor): Usage apply David's review results * fix(editor): Usage page set as default and first under Settings * fix(editor): Usage open subscription app in new tab * fix(editor): Usage page having key query param a plan links * test: Fix broken test * fix(editor): Usage page address review * 🧪 Flush promises on telemetry tests * ⚡ Extract helper with `setImmediate` * 🔥 Remove leftovers * ⚡ Use Adi's helper * refactor: Comment broken tests * refactor: add Tenant id to settings * feat: add environment to license endpoints * refactor: Move license environment to general settings * fix: fix routing bug * fix(editor): Usage page some code review changes and formatting * fix(editor): Usage page remove direct usage of reusable translation keys * fix(editor): Usage page async await instead of then * fix(editor): Usage page show some content only if network requests in component mounted were successful * chore(editor): code formatting * fix(editor): Usage checking license environment * feat(editor): Improve license activation error messages (no-changelog) (#4958) * fix(editor): Usage changing activation error title * remove unnecessary import * fix(editor): Usage refactor notification showing * fix(editor): Usage using notification directly in store actions Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: freyamade <freya@n8n.io> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> Co-authored-by: Mutasem <mutdmour@gmail.com> Co-authored-by: Cornelius Suermann <cornelius@n8n.io> * fix(editor): Usage change mounted lifecycle logic * fix(editor): Usage return after successful activation in mounted * fix: remove console log * test: fix tests related to settings (#4979) Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: freyamade <freya@n8n.io> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> Co-authored-by: Mutasem <mutdmour@gmail.com> Co-authored-by: Cornelius Suermann <cornelius@n8n.io> Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com>
745 lines
20 KiB
TypeScript
745 lines
20 KiB
TypeScript
import { randomBytes } from 'crypto';
|
|
import { existsSync } from 'fs';
|
|
|
|
import bodyParser from 'body-parser';
|
|
import { CronJob } from 'cron';
|
|
import express from 'express';
|
|
import set from 'lodash.set';
|
|
import { BinaryDataManager, UserSettings } from 'n8n-core';
|
|
import {
|
|
ICredentialType,
|
|
IDataObject,
|
|
IExecuteFunctions,
|
|
INode,
|
|
INodeExecutionData,
|
|
INodeParameters,
|
|
INodesAndCredentials,
|
|
ITriggerFunctions,
|
|
ITriggerResponse,
|
|
LoggerProxy,
|
|
NodeHelpers,
|
|
toCronExpression,
|
|
TriggerTime,
|
|
} from 'n8n-workflow';
|
|
import type { N8nApp } from '@/UserManagement/Interfaces';
|
|
import superagent from 'superagent';
|
|
import request from 'supertest';
|
|
import { URL } from 'url';
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
import config from '@/config';
|
|
import * as Db from '@/Db';
|
|
import { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
|
import { CredentialTypes } from '@/CredentialTypes';
|
|
import { ExternalHooks } from '@/ExternalHooks';
|
|
import { InternalHooksManager } from '@/InternalHooksManager';
|
|
import { NodeTypes } from '@/NodeTypes';
|
|
import * as ActiveWorkflowRunner from '@/ActiveWorkflowRunner';
|
|
import { meNamespace as meEndpoints } from '@/UserManagement/routes/me';
|
|
import { usersNamespace as usersEndpoints } from '@/UserManagement/routes/users';
|
|
import { authenticationMethods as authEndpoints } from '@/UserManagement/routes/auth';
|
|
import { ownerNamespace as ownerEndpoints } from '@/UserManagement/routes/owner';
|
|
import { passwordResetNamespace as passwordResetEndpoints } from '@/UserManagement/routes/passwordReset';
|
|
import { nodesController } from '@/api/nodes.api';
|
|
import { workflowsController } from '@/workflows/workflows.controller';
|
|
import { AUTH_COOKIE_NAME, NODE_PACKAGE_PREFIX } from '@/constants';
|
|
import { credentialsController } from '@/credentials/credentials.controller';
|
|
import { InstalledPackages } from '@db/entities/InstalledPackages';
|
|
import type { User } from '@db/entities/User';
|
|
import { getLogger } from '@/Logger';
|
|
import { loadPublicApiVersions } from '@/PublicApi/';
|
|
import { issueJWT } from '@/UserManagement/auth/jwt';
|
|
import { addRoutes as authMiddleware } from '@/UserManagement/routes';
|
|
import {
|
|
AUTHLESS_ENDPOINTS,
|
|
COMMUNITY_NODE_VERSION,
|
|
COMMUNITY_PACKAGE_VERSION,
|
|
PUBLIC_API_REST_PATH_SEGMENT,
|
|
REST_PATH_SEGMENT,
|
|
} from './constants';
|
|
import { randomName } from './random';
|
|
import type {
|
|
ApiPath,
|
|
EndpointGroup,
|
|
InstalledNodePayload,
|
|
InstalledPackagePayload,
|
|
PostgresSchemaSection,
|
|
} from './types';
|
|
import { licenseController } from '@/license/license.controller';
|
|
|
|
const loadNodesAndCredentials: INodesAndCredentials = {
|
|
loaded: { nodes: {}, credentials: {} },
|
|
known: { nodes: {}, credentials: {} },
|
|
};
|
|
|
|
const mockNodeTypes = NodeTypes(loadNodesAndCredentials);
|
|
CredentialTypes(loadNodesAndCredentials);
|
|
|
|
/**
|
|
* Initialize a test server.
|
|
*
|
|
* @param applyAuth Whether to apply auth middleware to test server.
|
|
* @param endpointGroups Groups of endpoints to apply to test server.
|
|
*/
|
|
export async function initTestServer({
|
|
applyAuth,
|
|
endpointGroups,
|
|
}: {
|
|
applyAuth: boolean;
|
|
endpointGroups?: EndpointGroup[];
|
|
}) {
|
|
const testServer = {
|
|
app: express(),
|
|
restEndpoint: REST_PATH_SEGMENT,
|
|
publicApiEndpoint: PUBLIC_API_REST_PATH_SEGMENT,
|
|
externalHooks: {},
|
|
};
|
|
|
|
testServer.app.use(bodyParser.json());
|
|
testServer.app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
config.set('userManagement.jwtSecret', 'My JWT secret');
|
|
config.set('userManagement.isInstanceOwnerSetUp', false);
|
|
|
|
if (applyAuth) {
|
|
authMiddleware.apply(testServer, [AUTHLESS_ENDPOINTS, REST_PATH_SEGMENT]);
|
|
}
|
|
|
|
if (!endpointGroups) return testServer.app;
|
|
|
|
if (
|
|
endpointGroups.includes('credentials') ||
|
|
endpointGroups.includes('me') ||
|
|
endpointGroups.includes('users') ||
|
|
endpointGroups.includes('passwordReset')
|
|
) {
|
|
testServer.externalHooks = ExternalHooks();
|
|
}
|
|
|
|
const [routerEndpoints, functionEndpoints] = classifyEndpointGroups(endpointGroups);
|
|
|
|
if (routerEndpoints.length) {
|
|
const { apiRouters } = await loadPublicApiVersions(testServer.publicApiEndpoint);
|
|
const map: Record<string, express.Router | express.Router[] | any> = {
|
|
credentials: { controller: credentialsController, path: 'credentials' },
|
|
workflows: { controller: workflowsController, path: 'workflows' },
|
|
nodes: { controller: nodesController, path: 'nodes' },
|
|
license: { controller: licenseController, path: 'license' },
|
|
publicApi: apiRouters,
|
|
};
|
|
|
|
for (const group of routerEndpoints) {
|
|
if (group === 'publicApi') {
|
|
testServer.app.use(...(map[group] as express.Router[]));
|
|
} else {
|
|
testServer.app.use(`/${testServer.restEndpoint}/${map[group].path}`, map[group].controller);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (functionEndpoints.length) {
|
|
const map: Record<string, (this: N8nApp) => void> = {
|
|
me: meEndpoints,
|
|
users: usersEndpoints,
|
|
auth: authEndpoints,
|
|
owner: ownerEndpoints,
|
|
passwordReset: passwordResetEndpoints,
|
|
};
|
|
|
|
for (const group of functionEndpoints) {
|
|
map[group].apply(testServer);
|
|
}
|
|
}
|
|
|
|
return testServer.app;
|
|
}
|
|
|
|
/**
|
|
* Pre-requisite: Mock the telemetry module before calling.
|
|
*/
|
|
export function initTestTelemetry() {
|
|
void InternalHooksManager.init('test-instance-id', 'test-version', mockNodeTypes);
|
|
}
|
|
|
|
/**
|
|
* Classify endpoint groups into `routerEndpoints` (newest, using `express.Router`),
|
|
* and `functionEndpoints` (legacy, namespaced inside a function).
|
|
*/
|
|
const classifyEndpointGroups = (endpointGroups: string[]) => {
|
|
const routerEndpoints: string[] = [];
|
|
const functionEndpoints: string[] = [];
|
|
|
|
const ROUTER_GROUP = ['credentials', 'nodes', 'workflows', 'publicApi', 'license'];
|
|
|
|
endpointGroups.forEach((group) =>
|
|
(ROUTER_GROUP.includes(group) ? routerEndpoints : functionEndpoints).push(group),
|
|
);
|
|
|
|
return [routerEndpoints, functionEndpoints];
|
|
};
|
|
|
|
// ----------------------------------
|
|
// initializers
|
|
// ----------------------------------
|
|
|
|
/**
|
|
* Initialize node types.
|
|
*/
|
|
export async function initActiveWorkflowRunner(): Promise<ActiveWorkflowRunner.ActiveWorkflowRunner> {
|
|
const workflowRunner = ActiveWorkflowRunner.getInstance();
|
|
workflowRunner.init();
|
|
return workflowRunner;
|
|
}
|
|
|
|
export function gitHubCredentialType(): ICredentialType {
|
|
return {
|
|
name: 'githubApi',
|
|
displayName: 'Github API',
|
|
documentationUrl: 'github',
|
|
properties: [
|
|
{
|
|
displayName: 'Github Server',
|
|
name: 'server',
|
|
type: 'string',
|
|
default: 'https://api.github.com',
|
|
description: 'The server to connect to. Only has to be set if Github Enterprise is used.',
|
|
},
|
|
{
|
|
displayName: 'User',
|
|
name: 'user',
|
|
type: 'string',
|
|
default: '',
|
|
},
|
|
{
|
|
displayName: 'Access Token',
|
|
name: 'accessToken',
|
|
type: 'string',
|
|
default: '',
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Initialize node types.
|
|
*/
|
|
export async function initCredentialsTypes(): Promise<void> {
|
|
loadNodesAndCredentials.loaded.credentials = {
|
|
githubApi: {
|
|
type: gitHubCredentialType(),
|
|
sourcePath: '',
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Initialize node types.
|
|
*/
|
|
export async function initNodeTypes() {
|
|
loadNodesAndCredentials.loaded.nodes = {
|
|
'n8n-nodes-base.start': {
|
|
sourcePath: '',
|
|
type: {
|
|
description: {
|
|
displayName: 'Start',
|
|
name: 'start',
|
|
group: ['input'],
|
|
version: 1,
|
|
description: 'Starts the workflow execution from this node',
|
|
defaults: {
|
|
name: 'Start',
|
|
color: '#553399',
|
|
},
|
|
inputs: [],
|
|
outputs: ['main'],
|
|
properties: [],
|
|
},
|
|
execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
|
|
return this.prepareOutputData(items);
|
|
},
|
|
},
|
|
},
|
|
'n8n-nodes-base.cron': {
|
|
sourcePath: '',
|
|
type: {
|
|
description: {
|
|
displayName: 'Cron',
|
|
name: 'cron',
|
|
icon: 'fa:calendar',
|
|
group: ['trigger', 'schedule'],
|
|
version: 1,
|
|
description: 'Triggers the workflow at a specific time',
|
|
eventTriggerDescription: '',
|
|
activationMessage:
|
|
'Your cron trigger will now trigger executions on the schedule you have defined.',
|
|
defaults: {
|
|
name: 'Cron',
|
|
color: '#00FF00',
|
|
},
|
|
inputs: [],
|
|
outputs: ['main'],
|
|
properties: [
|
|
{
|
|
displayName: 'Trigger Times',
|
|
name: 'triggerTimes',
|
|
type: 'fixedCollection',
|
|
typeOptions: {
|
|
multipleValues: true,
|
|
multipleValueButtonText: 'Add Time',
|
|
},
|
|
default: {},
|
|
description: 'Triggers for the workflow',
|
|
placeholder: 'Add Cron Time',
|
|
options: NodeHelpers.cronNodeOptions,
|
|
},
|
|
],
|
|
},
|
|
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
|
|
const triggerTimes = this.getNodeParameter('triggerTimes') as unknown as {
|
|
item: TriggerTime[];
|
|
};
|
|
|
|
// Get all the trigger times
|
|
const cronTimes = (triggerTimes.item || []).map(toCronExpression);
|
|
|
|
// The trigger function to execute when the cron-time got reached
|
|
// or when manually triggered
|
|
const executeTrigger = () => {
|
|
this.emit([this.helpers.returnJsonArray([{}])]);
|
|
};
|
|
|
|
const timezone = this.getTimezone();
|
|
|
|
// Start the cron-jobs
|
|
const cronJobs = cronTimes.map(
|
|
(cronTime) => new CronJob(cronTime, executeTrigger, undefined, true, timezone),
|
|
);
|
|
|
|
// Stop the cron-jobs
|
|
async function closeFunction() {
|
|
for (const cronJob of cronJobs) {
|
|
cronJob.stop();
|
|
}
|
|
}
|
|
|
|
async function manualTriggerFunction() {
|
|
executeTrigger();
|
|
}
|
|
|
|
return {
|
|
closeFunction,
|
|
manualTriggerFunction,
|
|
};
|
|
},
|
|
},
|
|
},
|
|
'n8n-nodes-base.set': {
|
|
sourcePath: '',
|
|
type: {
|
|
description: {
|
|
displayName: 'Set',
|
|
name: 'set',
|
|
icon: 'fa:pen',
|
|
group: ['input'],
|
|
version: 1,
|
|
description: 'Sets values on items and optionally remove other values',
|
|
defaults: {
|
|
name: 'Set',
|
|
color: '#0000FF',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
properties: [
|
|
{
|
|
displayName: 'Keep Only Set',
|
|
name: 'keepOnlySet',
|
|
type: 'boolean',
|
|
default: false,
|
|
description:
|
|
'If only the values set on this node should be kept and all others removed.',
|
|
},
|
|
{
|
|
displayName: 'Values to Set',
|
|
name: 'values',
|
|
placeholder: 'Add Value',
|
|
type: 'fixedCollection',
|
|
typeOptions: {
|
|
multipleValues: true,
|
|
sortable: true,
|
|
},
|
|
description: 'The value to set.',
|
|
default: {},
|
|
options: [
|
|
{
|
|
name: 'boolean',
|
|
displayName: 'Boolean',
|
|
values: [
|
|
{
|
|
displayName: 'Name',
|
|
name: 'name',
|
|
type: 'string',
|
|
default: 'propertyName',
|
|
description:
|
|
'Name of the property to write data to. Supports dot-notation. Example: "data.person[0].name"',
|
|
},
|
|
{
|
|
displayName: 'Value',
|
|
name: 'value',
|
|
type: 'boolean',
|
|
default: false,
|
|
description: 'The boolean value to write in the property.',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'number',
|
|
displayName: 'Number',
|
|
values: [
|
|
{
|
|
displayName: 'Name',
|
|
name: 'name',
|
|
type: 'string',
|
|
default: 'propertyName',
|
|
description:
|
|
'Name of the property to write data to. Supports dot-notation. Example: "data.person[0].name"',
|
|
},
|
|
{
|
|
displayName: 'Value',
|
|
name: 'value',
|
|
type: 'number',
|
|
default: 0,
|
|
description: 'The number value to write in the property.',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'string',
|
|
displayName: 'String',
|
|
values: [
|
|
{
|
|
displayName: 'Name',
|
|
name: 'name',
|
|
type: 'string',
|
|
default: 'propertyName',
|
|
description:
|
|
'Name of the property to write data to. Supports dot-notation. Example: "data.person[0].name"',
|
|
},
|
|
{
|
|
displayName: 'Value',
|
|
name: 'value',
|
|
type: 'string',
|
|
default: '',
|
|
description: 'The string value to write in the property.',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
|
|
{
|
|
displayName: 'Options',
|
|
name: 'options',
|
|
type: 'collection',
|
|
placeholder: 'Add Option',
|
|
default: {},
|
|
options: [
|
|
{
|
|
displayName: 'Dot Notation',
|
|
name: 'dotNotation',
|
|
type: 'boolean',
|
|
default: true,
|
|
description: `<p>By default, dot-notation is used in property names. This means that "a.b" will set the property "b" underneath "a" so { "a": { "b": value} }.<p></p>If that is not intended this can be deactivated, it will then set { "a.b": value } instead.</p>
|
|
`,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
const items = this.getInputData();
|
|
|
|
if (items.length === 0) {
|
|
items.push({ json: {} });
|
|
}
|
|
|
|
const returnData: INodeExecutionData[] = [];
|
|
|
|
let item: INodeExecutionData;
|
|
let keepOnlySet: boolean;
|
|
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
|
keepOnlySet = this.getNodeParameter('keepOnlySet', itemIndex, false) as boolean;
|
|
item = items[itemIndex];
|
|
const options = this.getNodeParameter('options', itemIndex, {}) as IDataObject;
|
|
|
|
const newItem: INodeExecutionData = {
|
|
json: {},
|
|
};
|
|
|
|
if (keepOnlySet !== true) {
|
|
if (item.binary !== undefined) {
|
|
// Create a shallow copy of the binary data so that the old
|
|
// data references which do not get changed still stay behind
|
|
// but the incoming data does not get changed.
|
|
newItem.binary = {};
|
|
Object.assign(newItem.binary, item.binary);
|
|
}
|
|
|
|
newItem.json = JSON.parse(JSON.stringify(item.json));
|
|
}
|
|
|
|
// Add boolean values
|
|
(this.getNodeParameter('values.boolean', itemIndex, []) as INodeParameters[]).forEach(
|
|
(setItem) => {
|
|
if (options.dotNotation === false) {
|
|
newItem.json[setItem.name as string] = !!setItem.value;
|
|
} else {
|
|
set(newItem.json, setItem.name as string, !!setItem.value);
|
|
}
|
|
},
|
|
);
|
|
|
|
// Add number values
|
|
(this.getNodeParameter('values.number', itemIndex, []) as INodeParameters[]).forEach(
|
|
(setItem) => {
|
|
if (options.dotNotation === false) {
|
|
newItem.json[setItem.name as string] = setItem.value;
|
|
} else {
|
|
set(newItem.json, setItem.name as string, setItem.value);
|
|
}
|
|
},
|
|
);
|
|
|
|
// Add string values
|
|
(this.getNodeParameter('values.string', itemIndex, []) as INodeParameters[]).forEach(
|
|
(setItem) => {
|
|
if (options.dotNotation === false) {
|
|
newItem.json[setItem.name as string] = setItem.value;
|
|
} else {
|
|
set(newItem.json, setItem.name as string, setItem.value);
|
|
}
|
|
},
|
|
);
|
|
|
|
returnData.push(newItem);
|
|
}
|
|
|
|
return this.prepareOutputData(returnData);
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Initialize a logger for test runs.
|
|
*/
|
|
export function initTestLogger() {
|
|
LoggerProxy.init(getLogger());
|
|
}
|
|
|
|
/**
|
|
* Initialize a BinaryManager for test runs.
|
|
*/
|
|
export async function initBinaryManager() {
|
|
const binaryDataConfig = config.getEnv('binaryDataManager');
|
|
await BinaryDataManager.init(binaryDataConfig);
|
|
}
|
|
|
|
/**
|
|
* Initialize a user settings config file if non-existent.
|
|
*/
|
|
export function initConfigFile() {
|
|
const settingsPath = UserSettings.getUserSettingsPath();
|
|
|
|
if (!existsSync(settingsPath)) {
|
|
const userSettings = { encryptionKey: randomBytes(24).toString('base64') };
|
|
UserSettings.writeUserSettings(userSettings, settingsPath);
|
|
}
|
|
}
|
|
|
|
// ----------------------------------
|
|
// request agent
|
|
// ----------------------------------
|
|
|
|
/**
|
|
* Create a request agent, optionally with an auth cookie.
|
|
*/
|
|
export function createAgent(
|
|
app: express.Application,
|
|
options?: { auth: boolean; user: User; apiPath?: ApiPath; version?: string | number },
|
|
) {
|
|
const agent = request.agent(app);
|
|
|
|
if (options?.apiPath === undefined || options?.apiPath === 'internal') {
|
|
agent.use(prefix(REST_PATH_SEGMENT));
|
|
if (options?.auth && options?.user) {
|
|
const { token } = issueJWT(options.user);
|
|
agent.jar.setCookie(`${AUTH_COOKIE_NAME}=${token}`);
|
|
}
|
|
}
|
|
|
|
if (options?.apiPath === 'public') {
|
|
agent.use(prefix(`${PUBLIC_API_REST_PATH_SEGMENT}/v${options?.version}`));
|
|
|
|
if (options?.auth && options?.user.apiKey) {
|
|
agent.set({ 'X-N8N-API-KEY': options.user.apiKey });
|
|
}
|
|
}
|
|
|
|
return agent;
|
|
}
|
|
|
|
export function createAuthAgent(app: express.Application) {
|
|
return (user: User) => createAgent(app, { auth: true, user });
|
|
}
|
|
|
|
/**
|
|
* Plugin to prefix a path segment into a request URL pathname.
|
|
*
|
|
* Example: http://127.0.0.1:62100/me/password → http://127.0.0.1:62100/rest/me/password
|
|
*/
|
|
export function prefix(pathSegment: string) {
|
|
return function (request: superagent.SuperAgentRequest) {
|
|
const url = new URL(request.url);
|
|
|
|
// enforce consistency at call sites
|
|
if (url.pathname[0] !== '/') {
|
|
throw new Error('Pathname must start with a forward slash');
|
|
}
|
|
|
|
url.pathname = pathSegment + url.pathname;
|
|
request.url = url.toString();
|
|
return request;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Extract the value (token) of the auth cookie in a response.
|
|
*/
|
|
export function getAuthToken(response: request.Response, authCookieName = AUTH_COOKIE_NAME) {
|
|
const cookies: string[] = response.headers['set-cookie'];
|
|
|
|
if (!cookies) return undefined;
|
|
|
|
const authCookie = cookies.find((c) => c.startsWith(`${authCookieName}=`));
|
|
|
|
if (!authCookie) return undefined;
|
|
|
|
const match = authCookie.match(new RegExp(`(^| )${authCookieName}=(?<token>[^;]+)`));
|
|
|
|
if (!match || !match.groups) return undefined;
|
|
|
|
return match.groups.token;
|
|
}
|
|
|
|
// ----------------------------------
|
|
// settings
|
|
// ----------------------------------
|
|
|
|
export async function isInstanceOwnerSetUp() {
|
|
const { value } = await Db.collections.Settings.findOneOrFail({
|
|
key: 'userManagement.isInstanceOwnerSetUp',
|
|
});
|
|
|
|
return Boolean(value);
|
|
}
|
|
|
|
// ----------------------------------
|
|
// misc
|
|
// ----------------------------------
|
|
|
|
/**
|
|
* Categorize array items into two groups based on whether they pass a test.
|
|
*/
|
|
export const categorize = <T>(arr: T[], test: (str: T) => boolean) => {
|
|
return arr.reduce<{ pass: T[]; fail: T[] }>(
|
|
(acc, cur) => {
|
|
test(cur) ? acc.pass.push(cur) : acc.fail.push(cur);
|
|
|
|
return acc;
|
|
},
|
|
{ pass: [], fail: [] },
|
|
);
|
|
};
|
|
|
|
export function getPostgresSchemaSection(
|
|
schema = config.getSchema(),
|
|
): PostgresSchemaSection | null {
|
|
for (const [key, value] of Object.entries(schema)) {
|
|
if (key === 'postgresdb') {
|
|
return value._cvtProperties;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// ----------------------------------
|
|
// community nodes
|
|
// ----------------------------------
|
|
|
|
export function installedPackagePayload(): InstalledPackagePayload {
|
|
return {
|
|
packageName: NODE_PACKAGE_PREFIX + randomName(),
|
|
installedVersion: COMMUNITY_PACKAGE_VERSION.CURRENT,
|
|
};
|
|
}
|
|
|
|
export function installedNodePayload(packageName: string): InstalledNodePayload {
|
|
const nodeName = randomName();
|
|
return {
|
|
name: nodeName,
|
|
type: nodeName,
|
|
latestVersion: COMMUNITY_NODE_VERSION.CURRENT,
|
|
package: packageName,
|
|
};
|
|
}
|
|
|
|
export const emptyPackage = () => {
|
|
const installedPackage = new InstalledPackages();
|
|
installedPackage.installedNodes = [];
|
|
|
|
return Promise.resolve(installedPackage);
|
|
};
|
|
|
|
// ----------------------------------
|
|
// workflow
|
|
// ----------------------------------
|
|
|
|
export function makeWorkflow(options?: {
|
|
withPinData: boolean;
|
|
withCredential?: { id: string; name: string };
|
|
}) {
|
|
const workflow = new WorkflowEntity();
|
|
|
|
const node: INode = {
|
|
id: uuid(),
|
|
name: 'Cron',
|
|
type: 'n8n-nodes-base.cron',
|
|
parameters: {},
|
|
typeVersion: 1,
|
|
position: [740, 240],
|
|
};
|
|
|
|
if (options?.withCredential) {
|
|
node.credentials = {
|
|
spotifyApi: options.withCredential,
|
|
};
|
|
}
|
|
|
|
workflow.name = 'My Workflow';
|
|
workflow.active = false;
|
|
workflow.connections = {};
|
|
workflow.nodes = [node];
|
|
|
|
if (options?.withPinData) {
|
|
workflow.pinData = MOCK_PINDATA;
|
|
}
|
|
|
|
return workflow;
|
|
}
|
|
|
|
export const MOCK_PINDATA = { Spotify: [{ json: { myKey: 'myValue' } }] };
|