Files
Automata/packages/workflow/src/TelemetryHelpers.ts
Alex Grozav 27e2ce0470 feat: migrate editor-ui to Vite.js and various DX improvements (N8N-2277) (#4061)
* feat: Added vite.js dependencies.

* chore: Removed tests folder to follow same structure as design-system

* chore: Removed unused testing config.

* chore: Created vite.js index.html

* refactor: Updated scss structure and imports.

* refactor: Updated workflow building.

* fix: Cleared up all workflow dependency cycles. Added proper package.json imports config.

* feat: Got a working build using Vite. Need to fix issues next.

* fix: Progress! Getting process.env error.

* fix: Changed process.env to import.meta.env.

* fix: Fixed circular imports that used require(). Fixed monaco editor.

* chore: Removed commented code.

* chore: Cleaned up package.json

* feat: Made necessary changes to replace base path in css files.

* feat: Serve CSS files for `editor-ui` Vite migration (#4069)

 Serve CSS files for Vite migration

* chore: Fixed package-lock.json.

* fix: Fixed build after centralized tsconfig update.

* fix: Removed lodash-es replacement.

* fix: Commented out vitest test command.

* style: Fixed linting issues.

* fix: Added lodash-es hotfix back.

* chore: Updated package-lock.json

* refactor: Renamed all n8n scss variables to no longer be defined as private.

* feat(editor): add application-wide el-button replacement.

* fix(editor): Fix import in page alert after merge.

* chore(editor): update package-lock.json.

* fix: Case sensitive lodash-es replacement for vue-agile.

* fix: add alias for lodash-es camelcase import.

* fix: add patch-package support for fixing quill

* feat: add patch-package on postinstall

* fix: update quill patch path.

* refactor: rename quill patch

* fix: update quill version.

* fix: update quill patch

* fix: fix linting rules after installing eslint in design-system

* fix: update date picker button to have primary color

* test: update callout component snapshots

* fix(editor): fix linting issues in editor after enabling eslint

* fix(cli): add /assets/* to auth ignore endpoints in server

* chore: update package-lock.json

* chore: update package-lock.json

* fix(editor): fix linting issues

* feat: add vite-legacy support

* fix: update workflow package interface imports to type imports.

* chore: update package-lock.json

* fix(editor) fix importing translations other than english

* fix(editor): remove test command until vitest is added

* fix: increase memory allocation for vite build

* fix: add patch-package patches to n8n-custom docker build

* fix: add performance and load time improvements

* fix: add proper typing to setNodeType

* chore: update package-lock.json

* style: use generic type for reduce in setNodeType

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
2022-09-23 17:14:28 +03:00

233 lines
6.8 KiB
TypeScript

import {
IConnection,
INode,
INodeNameIndex,
INodesGraph,
INodeGraphItem,
INodesGraphResult,
IWorkflowBase,
INodeTypes,
INodeType,
} from './Interfaces';
const STICKY_NODE_TYPE = 'n8n-nodes-base.stickyNote';
export function getNodeTypeForName(workflow: IWorkflowBase, nodeName: string): INode | undefined {
return workflow.nodes.find((node) => node.name === nodeName);
}
export function isNumber(value: unknown): value is number {
return typeof value === 'number';
}
function getStickyDimensions(note: INode, stickyType: INodeType | undefined) {
const heightProperty = stickyType?.description?.properties.find(
(property) => property.name === 'height',
);
const widthProperty = stickyType?.description?.properties.find(
(property) => property.name === 'width',
);
const defaultHeight =
heightProperty && isNumber(heightProperty?.default) ? heightProperty.default : 0;
const defaultWidth =
widthProperty && isNumber(widthProperty?.default) ? widthProperty.default : 0;
const height: number = isNumber(note.parameters.height) ? note.parameters.height : defaultHeight;
const width: number = isNumber(note.parameters.width) ? note.parameters.width : defaultWidth;
return {
height,
width,
};
}
type XYPosition = [number, number];
function areOverlapping(
topLeft: XYPosition,
bottomRight: XYPosition,
targetPos: XYPosition,
): boolean {
return (
targetPos[0] > topLeft[0] &&
targetPos[1] > topLeft[1] &&
targetPos[0] < bottomRight[0] &&
targetPos[1] < bottomRight[1]
);
}
const URL_PARTS_REGEX = /(?<protocolPlusDomain>.*?\..*?)(?<pathname>\/.*)/;
export function getDomainBase(raw: string, urlParts = URL_PARTS_REGEX): string {
try {
const url = new URL(raw);
return [url.protocol, url.hostname].join('//');
} catch (_) {
const match = urlParts.exec(raw);
if (!match?.groups?.protocolPlusDomain) return '';
return match.groups.protocolPlusDomain;
}
}
function isSensitive(segment: string) {
if (/^v\d+$/.test(segment)) return false;
return /%40/.test(segment) || /\d/.test(segment) || /^[0-9A-F]{8}/i.test(segment);
}
export const ANONYMIZATION_CHARACTER = '*';
function sanitizeRoute(raw: string, check = isSensitive, char = ANONYMIZATION_CHARACTER) {
return raw
.split('/')
.map((segment) => (check(segment) ? char.repeat(segment.length) : segment))
.join('/');
}
/**
* Return pathname plus query string from URL, anonymizing IDs in route and query params.
*/
export function getDomainPath(raw: string, urlParts = URL_PARTS_REGEX): string {
try {
const url = new URL(raw);
if (!url.hostname) throw new Error('Malformed URL');
return sanitizeRoute(url.pathname);
} catch (_) {
const match = urlParts.exec(raw);
if (!match?.groups?.pathname) return '';
// discard query string
const route = match.groups.pathname.split('?').shift() as string;
return sanitizeRoute(route);
}
}
export function generateNodesGraph(
workflow: IWorkflowBase,
nodeTypes: INodeTypes,
options?: {
sourceInstanceId?: string;
nodeIdMap?: { [curr: string]: string };
},
): INodesGraphResult {
const nodesGraph: INodesGraph = {
node_types: [],
node_connections: [],
nodes: {},
notes: {},
is_pinned: Object.keys(workflow.pinData ?? {}).length > 0,
};
const nodeNameAndIndex: INodeNameIndex = {};
const webhookNodeNames: string[] = [];
try {
const notes = workflow.nodes.filter((node) => node.type === STICKY_NODE_TYPE);
const otherNodes = workflow.nodes.filter((node) => node.type !== STICKY_NODE_TYPE);
notes.forEach((stickyNote: INode, index: number) => {
const stickyType = nodeTypes.getByNameAndVersion(STICKY_NODE_TYPE, stickyNote.typeVersion);
const { height, width } = getStickyDimensions(stickyNote, stickyType);
const topLeft = stickyNote.position;
const bottomRight: [number, number] = [topLeft[0] + width, topLeft[1] + height];
const overlapping = Boolean(
otherNodes.find((node) => areOverlapping(topLeft, bottomRight, node.position)),
);
nodesGraph.notes[index] = {
overlapping,
position: topLeft,
height,
width,
};
});
otherNodes.forEach((node: INode, index: number) => {
nodesGraph.node_types.push(node.type);
const nodeItem: INodeGraphItem = {
id: node.id,
type: node.type,
position: node.position,
};
if (options?.sourceInstanceId) {
nodeItem.src_instance_id = options.sourceInstanceId;
}
if (node.id && options?.nodeIdMap && options.nodeIdMap[node.id]) {
nodeItem.src_node_id = options.nodeIdMap[node.id];
}
if (node.type === 'n8n-nodes-base.httpRequest' && node.typeVersion === 1) {
try {
nodeItem.domain = new URL(node.parameters.url as string).hostname;
} catch (_) {
nodeItem.domain = getDomainBase(node.parameters.url as string);
}
} else if (node.type === 'n8n-nodes-base.httpRequest' && node.typeVersion === 2) {
const { authentication } = node.parameters as { authentication: string };
nodeItem.credential_type = {
none: 'none',
genericCredentialType: node.parameters.genericAuthType as string,
existingCredentialType: node.parameters.nodeCredentialType as string,
}[authentication];
nodeItem.credential_set = node.credentials
? Object.keys(node.credentials).length > 0
: false;
const { url } = node.parameters as { url: string };
nodeItem.domain_base = getDomainBase(url);
nodeItem.domain_path = getDomainPath(url);
nodeItem.method = node.parameters.requestMethod as string;
} else if (node.type === 'n8n-nodes-base.webhook') {
webhookNodeNames.push(node.name);
} else {
const nodeType = nodeTypes.getByNameAndVersion(node.type);
nodeType?.description?.properties?.forEach((property) => {
if (
property.name === 'operation' ||
property.name === 'resource' ||
property.name === 'mode'
) {
nodeItem[property.name] = property.default ? property.default.toString() : undefined;
}
});
nodeItem.operation = node.parameters.operation?.toString() ?? nodeItem.operation;
nodeItem.resource = node.parameters.resource?.toString() ?? nodeItem.resource;
nodeItem.mode = node.parameters.mode?.toString() ?? nodeItem.mode;
}
nodesGraph.nodes[`${index}`] = nodeItem;
nodeNameAndIndex[node.name] = index.toString();
});
const getGraphConnectionItem = (startNode: string, connectionItem: IConnection) => {
return { start: nodeNameAndIndex[startNode], end: nodeNameAndIndex[connectionItem.node] };
};
Object.keys(workflow.connections).forEach((nodeName) => {
const connections = workflow.connections[nodeName];
connections.main.forEach((element) => {
element.forEach((element2) => {
nodesGraph.node_connections.push(getGraphConnectionItem(nodeName, element2));
});
});
});
} catch (e) {
return { nodeGraph: nodesGraph, nameIndices: nodeNameAndIndex, webhookNodeNames };
}
return { nodeGraph: nodesGraph, nameIndices: nodeNameAndIndex, webhookNodeNames };
}