Skip to content

Commit

Permalink
refactor(core): Move push message types to a new shared package (no-c…
Browse files Browse the repository at this point in the history
…hangelog) (#10742)
  • Loading branch information
netroy authored and riascho committed Sep 23, 2024
1 parent 11decea commit 3286796
Show file tree
Hide file tree
Showing 56 changed files with 480 additions and 663 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-documentation-urls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: pnpm install --frozen-lockfile

- name: Build relevant packages
run: pnpm --filter @n8n/client-oauth2 --filter @n8n/imap --filter n8n-workflow --filter n8n-core --filter n8n-nodes-base --filter @n8n/n8n-nodes-langchain build
run: pnpm build:nodes

- run: npm install --prefix=.github/scripts --no-package-lock

Expand Down
7 changes: 7 additions & 0 deletions packages/@n8n/api-types/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const sharedOptions = require('@n8n_io/eslint-config/shared');

/** @type {import('@types/eslint').ESLint.ConfigData} */
module.exports = {
extends: ['@n8n_io/eslint-config/base'],
...sharedOptions(__dirname),
};
3 changes: 3 additions & 0 deletions packages/@n8n/api-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## @n8n/api-types

This package contains types and schema definitions for the n8n internal API, so that these can be shared between the backend and the frontend code.
2 changes: 2 additions & 0 deletions packages/@n8n/api-types/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @type {import('jest').Config} */
module.exports = require('../../../jest.config');
24 changes: 24 additions & 0 deletions packages/@n8n/api-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@n8n/api-types",
"version": "0.1.0",
"scripts": {
"clean": "rimraf dist .turbo",
"dev": "pnpm watch",
"typecheck": "tsc --noEmit",
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write . --ignore-path ../../../.prettierignore",
"lint": "eslint .",
"lintfix": "eslint . --fix",
"watch": "tsc -p tsconfig.build.json --watch",
"test": "echo \"No tests yet\" && exit 0"
},
"main": "dist/index.js",
"module": "src/index.ts",
"types": "dist/index.d.ts",
"files": [
"dist/**/*"
],
"devDependencies": {
"n8n-workflow": "workspace:*"
}
}
2 changes: 2 additions & 0 deletions packages/@n8n/api-types/src/datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** Date time in the ISO 8601 format, e.g. 2024-10-31T00:00:00.123Z */
export type Iso8601DateTimeString = string;
7 changes: 7 additions & 0 deletions packages/@n8n/api-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type * from './push';
export type * from './scaling';
export type * from './datetime';
export type * from './user';

export type { Collaborator } from './push/collaboration';
export type { SendWorkerStatusMessage } from './push/worker';
17 changes: 17 additions & 0 deletions packages/@n8n/api-types/src/push/collaboration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Iso8601DateTimeString } from '../datetime';
import type { MinimalUser } from '../user';

export type Collaborator = {
user: MinimalUser;
lastSeen: Iso8601DateTimeString;
};

type CollaboratorsChanged = {
type: 'collaboratorsChanged';
data: {
workflowId: string;
collaborators: Collaborator[];
};
};

export type CollaborationPushMessage = CollaboratorsChanged;
9 changes: 9 additions & 0 deletions packages/@n8n/api-types/src/push/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type SendConsoleMessage = {
type: 'sendConsoleMessage';
data: {
source: string;
messages: unknown[];
};
};

export type DebugPushMessage = SendConsoleMessage;
53 changes: 53 additions & 0 deletions packages/@n8n/api-types/src/push/execution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { IRun, ITaskData, WorkflowExecuteMode } from 'n8n-workflow';

type ExecutionStarted = {
type: 'executionStarted';
data: {
executionId: string;
mode: WorkflowExecuteMode;
startedAt: Date;
workflowId: string;
workflowName?: string;
retryOf?: string;
};
};

type ExecutionFinished = {
type: 'executionFinished';
data: {
executionId: string;
data: IRun;
retryOf?: string;
};
};

type ExecutionRecovered = {
type: 'executionRecovered';
data: {
executionId: string;
};
};

type NodeExecuteBefore = {
type: 'nodeExecuteBefore';
data: {
executionId: string;
nodeName: string;
};
};

type NodeExecuteAfter = {
type: 'nodeExecuteAfter';
data: {
executionId: string;
nodeName: string;
data: ITaskData;
};
};

export type ExecutionPushMessage =
| ExecutionStarted
| ExecutionFinished
| ExecutionRecovered
| NodeExecuteBefore
| NodeExecuteAfter;
21 changes: 21 additions & 0 deletions packages/@n8n/api-types/src/push/hot-reload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type NodeTypeData = {
name: string;
version: number;
};

type ReloadNodeType = {
type: 'reloadNodeType';
data: NodeTypeData;
};

type RemoveNodeType = {
type: 'removeNodeType';
data: NodeTypeData;
};

type NodeDescriptionUpdated = {
type: 'nodeDescriptionUpdated';
data: {};
};

export type HotReloadPushMessage = ReloadNodeType | RemoveNodeType | NodeDescriptionUpdated;
20 changes: 20 additions & 0 deletions packages/@n8n/api-types/src/push/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ExecutionPushMessage } from './execution';
import type { WorkflowPushMessage } from './workflow';
import type { HotReloadPushMessage } from './hot-reload';
import type { WorkerPushMessage } from './worker';
import type { WebhookPushMessage } from './webhook';
import type { CollaborationPushMessage } from './collaboration';
import type { DebugPushMessage } from './debug';

export type PushMessage =
| ExecutionPushMessage
| WorkflowPushMessage
| HotReloadPushMessage
| WebhookPushMessage
| WorkerPushMessage
| CollaborationPushMessage
| DebugPushMessage;

export type PushType = PushMessage['type'];

export type PushPayload<T extends PushType> = Extract<PushMessage, { type: T }>['data'];
17 changes: 17 additions & 0 deletions packages/@n8n/api-types/src/push/webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type TestWebhookDeleted = {
type: 'testWebhookDeleted';
data: {
executionId?: string;
workflowId: string;
};
};

type TestWebhookReceived = {
type: 'testWebhookReceived';
data: {
executionId: string;
workflowId: string;
};
};

export type WebhookPushMessage = TestWebhookDeleted | TestWebhookReceived;
11 changes: 11 additions & 0 deletions packages/@n8n/api-types/src/push/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { WorkerStatus } from '../scaling';

export type SendWorkerStatusMessage = {
type: 'sendWorkerStatusMessage';
data: {
workerId: string;
status: WorkerStatus;
};
};

export type WorkerPushMessage = SendWorkerStatusMessage;
26 changes: 26 additions & 0 deletions packages/@n8n/api-types/src/push/workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type WorkflowActivated = {
type: 'workflowActivated';
data: {
workflowId: string;
};
};

type WorkflowFailedToActivate = {
type: 'workflowFailedToActivate';
data: {
workflowId: string;
errorMessage: string;
};
};

type WorkflowDeactivated = {
type: 'workflowDeactivated';
data: {
workflowId: string;
};
};

export type WorkflowPushMessage =
| WorkflowActivated
| WorkflowFailedToActivate
| WorkflowDeactivated;
30 changes: 30 additions & 0 deletions packages/@n8n/api-types/src/scaling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ExecutionStatus, WorkflowExecuteMode } from 'n8n-workflow';

export type RunningJobSummary = {
executionId: string;
workflowId: string;
workflowName: string;
mode: WorkflowExecuteMode;
startedAt: Date;
retryOf: string;
status: ExecutionStatus;
};

export type WorkerStatus = {
workerId: string;
runningJobsSummary: RunningJobSummary[];
freeMem: number;
totalMem: number;
uptime: number;
loadAvg: number[];
cpus: string;
arch: string;
platform: NodeJS.Platform;
hostname: string;
interfaces: Array<{
family: 'IPv4' | 'IPv6';
address: string;
internal: boolean;
}>;
version: string;
};
6 changes: 6 additions & 0 deletions packages/@n8n/api-types/src/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type MinimalUser = {
id: string;
email: string;
firstName: string;
lastName: string;
};
11 changes: 11 additions & 0 deletions packages/@n8n/api-types/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": ["./tsconfig.json", "../../../tsconfig.build.json"],
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/build.tsbuildinfo"
},
"include": ["src/**/*.ts"],
"exclude": ["test/**", "src/**/__tests__/**"]
}
10 changes: 10 additions & 0 deletions packages/@n8n/api-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"types": ["node", "jest"],
"baseUrl": "src",
"tsBuildInfoFile": "dist/typecheck.tsbuildinfo"
},
"include": ["src/**/*.ts", "test/**/*.ts"]
}
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@azure/identity": "^4.3.0",
"@azure/keyvault-secrets": "^4.8.0",
"@google-cloud/secret-manager": "^5.6.0",
"@n8n/api-types": "workspace:*",
"@n8n/client-oauth2": "workspace:*",
"@n8n/config": "workspace:*",
"@n8n/localtunnel": "3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/collaboration/collaboration.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { PushPayload } from '@n8n/api-types';
import type { Workflow } from 'n8n-workflow';
import { ApplicationError, ErrorReporterProxy } from 'n8n-workflow';
import { Service } from 'typedi';

import { CollaborationState } from '@/collaboration/collaboration.state';
import type { User } from '@/databases/entities/user';
import { UserRepository } from '@/databases/repositories/user.repository';
import type { ICollaboratorsChanged } from '@/interfaces';
import { Push } from '@/push';
import type { OnPushMessage } from '@/push/types';
import { AccessService } from '@/services/access.service';
Expand Down Expand Up @@ -92,7 +92,7 @@ export class CollaborationService {
user: user.toIUser(),
lastSeen: collaborators.find(({ userId }) => userId === user.id)!.lastSeen,
}));
const msgData: ICollaboratorsChanged = {
const msgData: PushPayload<'collaboratorsChanged'> = {
workflowId,
collaborators: activeCollaborators,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/collaboration/collaboration.state.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Iso8601DateTimeString } from '@n8n/api-types';
import type { Workflow } from 'n8n-workflow';
import { Service } from 'typedi';

import { Time } from '@/constants';
import type { User } from '@/databases/entities/user';
import type { Iso8601DateTimeString } from '@/interfaces';
import { CacheService } from '@/services/cache/cache.service';

type WorkflowCacheHash = Record<User['id'], Iso8601DateTimeString>;
Expand Down
11 changes: 6 additions & 5 deletions packages/cli/src/controllers/e2e.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PushPayload, PushType } from '@n8n/api-types';
import { Request } from 'express';
import Container from 'typedi';
import { v4 as uuid } from 'uuid';
Expand All @@ -10,7 +11,7 @@ import { SettingsRepository } from '@/databases/repositories/settings.repository
import { UserRepository } from '@/databases/repositories/user.repository';
import { Patch, Post, RestController } from '@/decorators';
import { MessageEventBus } from '@/eventbus/message-event-bus/message-event-bus';
import type { BooleanLicenseFeature, IPushDataType, NumericLicenseFeature } from '@/interfaces';
import type { BooleanLicenseFeature, NumericLicenseFeature } from '@/interfaces';
import { License } from '@/license';
import { Logger } from '@/logger';
import { MfaService } from '@/mfa/mfa.service';
Expand Down Expand Up @@ -56,13 +57,13 @@ type ResetRequest = Request<
}
>;

type PushRequest = Request<
type PushRequest<T extends PushType> = Request<
{},
{},
{
type: IPushDataType;
type: T;
pushRef: string;
data: object;
data: PushPayload<T>;
}
>;

Expand Down Expand Up @@ -132,7 +133,7 @@ export class E2EController {
}

@Post('/push', { skipAuth: true })
async pushSend(req: PushRequest) {
async pushSend(req: PushRequest<any>) {
this.push.broadcast(req.body.type, req.body.data);
}

Expand Down
Loading

0 comments on commit 3286796

Please sign in to comment.