refactor(core): Improve community node repo code (#3767)

* 📘 Tighten `NodeRequest`

* :blue: Add `AuthAgent` type

*  Add constants

* 📘 Namespace npm types

* 🧪 Set up `createAuthAgent`

* 🧪 Refactor helpers tests

* 🧪 Refactor endpoints tests

*  Refactor CNR helpers

*  Return promises in `packageModel`

*  Refactor endpoints

* ✏️ Restore naming

*  Expose dependency `jest-mock`

* 📦 Update `package-lock.json`

* 📦 Update `package-lock.json`

* 👕 Fix lint

* 🚚 Rename namespace

* 🔥 Remove outdated comment

* 🐛 Fix `Promise` comparison

*  Undo `ResponseHelper` change

* ✏️ Document `ResponseError`

* 🎨 Fix formatting
This commit is contained in:
Iván Ovejero 2022-08-02 10:40:57 +02:00 committed by GitHub
parent ad8d662976
commit 7e578b7f4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1042 additions and 867 deletions

156
package-lock.json generated
View file

@ -171,6 +171,7 @@
"iso-639-1": "^2.1.3", "iso-639-1": "^2.1.3",
"jest": "^27.4.7", "jest": "^27.4.7",
"jest-environment-jsdom": "^27.5.1", "jest-environment-jsdom": "^27.5.1",
"jest-mock": "^28.1.3",
"jmespath": "^0.16.0", "jmespath": "^0.16.0",
"jquery": "^3.4.1", "jquery": "^3.4.1",
"jsdom": "19.0.0", "jsdom": "19.0.0",
@ -3496,6 +3497,18 @@
"url": "https://github.com/chalk/chalk?sponsor=1" "url": "https://github.com/chalk/chalk?sponsor=1"
} }
}, },
"node_modules/@jest/environment/node_modules/jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"dependencies": {
"@jest/types": "^27.5.1",
"@types/node": "*"
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
}
},
"node_modules/@jest/environment/node_modules/supports-color": { "node_modules/@jest/environment/node_modules/supports-color": {
"version": "7.2.0", "version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
@ -3561,6 +3574,18 @@
"url": "https://github.com/chalk/chalk?sponsor=1" "url": "https://github.com/chalk/chalk?sponsor=1"
} }
}, },
"node_modules/@jest/fake-timers/node_modules/jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"dependencies": {
"@jest/types": "^27.5.1",
"@types/node": "*"
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
}
},
"node_modules/@jest/fake-timers/node_modules/jest-util": { "node_modules/@jest/fake-timers/node_modules/jest-util": {
"version": "27.5.1", "version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz",
@ -39769,6 +39794,18 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/jest-environment-jsdom/node_modules/jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"dependencies": {
"@jest/types": "^27.5.1",
"@types/node": "*"
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
}
},
"node_modules/jest-environment-jsdom/node_modules/jest-util": { "node_modules/jest-environment-jsdom/node_modules/jest-util": {
"version": "27.5.1", "version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz",
@ -39984,6 +40021,18 @@
"url": "https://github.com/chalk/chalk?sponsor=1" "url": "https://github.com/chalk/chalk?sponsor=1"
} }
}, },
"node_modules/jest-environment-node/node_modules/jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"dependencies": {
"@jest/types": "^27.5.1",
"@types/node": "*"
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
}
},
"node_modules/jest-environment-node/node_modules/jest-util": { "node_modules/jest-environment-node/node_modules/jest-util": {
"version": "27.5.1", "version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz",
@ -40378,36 +40427,37 @@
} }
}, },
"node_modules/jest-mock": { "node_modules/jest-mock": {
"version": "27.5.1", "version": "28.1.3",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-28.1.3.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==", "integrity": "sha512-o3J2jr6dMMWYVH4Lh/NKmDXdosrsJgi4AviS8oXLujcjpCMBb1FMsblDnOXKZKfSiHLxYub1eS0IHuRXsio9eA==",
"dependencies": { "dependencies": {
"@jest/types": "^27.5.1", "@jest/types": "^28.1.3",
"@types/node": "*" "@types/node": "*"
}, },
"engines": { "engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" "node": "^12.13.0 || ^14.15.0 || ^16.10.0 || >=17.0.0"
} }
}, },
"node_modules/jest-mock/node_modules/@jest/types": { "node_modules/jest-mock/node_modules/@jest/types": {
"version": "27.5.1", "version": "28.1.3",
"resolved": "https://registry.npmjs.org/@jest/types/-/types-27.5.1.tgz", "resolved": "https://registry.npmjs.org/@jest/types/-/types-28.1.3.tgz",
"integrity": "sha512-Cx46iJ9QpwQTjIdq5VJu2QTMMs3QlEjI0x1QbBP5W1+nMzyc2XmimiRR/CbX9TO0cPTeUlxWMOu8mslYsJ8DEw==", "integrity": "sha512-RyjiyMUZrKz/c+zlMFO1pm70DcIlST8AeWTkoUdZevew44wcNZQHsEVOiCVtgVnlFFD82FPaXycys58cf2muVQ==",
"dependencies": { "dependencies": {
"@jest/schemas": "^28.1.3",
"@types/istanbul-lib-coverage": "^2.0.0", "@types/istanbul-lib-coverage": "^2.0.0",
"@types/istanbul-reports": "^3.0.0", "@types/istanbul-reports": "^3.0.0",
"@types/node": "*", "@types/node": "*",
"@types/yargs": "^16.0.0", "@types/yargs": "^17.0.8",
"chalk": "^4.0.0" "chalk": "^4.0.0"
}, },
"engines": { "engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" "node": "^12.13.0 || ^14.15.0 || ^16.10.0 || >=17.0.0"
} }
}, },
"node_modules/jest-mock/node_modules/@types/yargs": { "node_modules/jest-mock/node_modules/@types/yargs": {
"version": "16.0.4", "version": "17.0.10",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.10.tgz",
"integrity": "sha512-T8Yc9wt/5LbJyCaLiHPReJa0kApcIgJ7Bn735GjItUfh08Z1pJvu8QZqb9s+mMvKV6WUQRV7K2R46YbjMXTTJw==", "integrity": "sha512-gmEaFwpj/7f/ROdtIlci1R1VYU1J4j95m8T+Tj3iBgiBFKg1foE/PSl93bBd5T9LDXNPo8UlNN6W0qwD8O5OaA==",
"dependencies": { "dependencies": {
"@types/yargs-parser": "*" "@types/yargs-parser": "*"
} }
@ -41040,6 +41090,18 @@
"fsevents": "^2.3.2" "fsevents": "^2.3.2"
} }
}, },
"node_modules/jest-runtime/node_modules/jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"dependencies": {
"@jest/types": "^27.5.1",
"@types/node": "*"
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
}
},
"node_modules/jest-runtime/node_modules/jest-regex-util": { "node_modules/jest-runtime/node_modules/jest-regex-util": {
"version": "27.5.1", "version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-regex-util/-/jest-regex-util-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-regex-util/-/jest-regex-util-27.5.1.tgz",
@ -64044,6 +64106,15 @@
"supports-color": "^7.1.0" "supports-color": "^7.1.0"
} }
}, },
"jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"requires": {
"@jest/types": "^27.5.1",
"@types/node": "*"
}
},
"supports-color": { "supports-color": {
"version": "7.2.0", "version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
@ -64096,6 +64167,15 @@
"supports-color": "^7.1.0" "supports-color": "^7.1.0"
} }
}, },
"jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"requires": {
"@jest/types": "^27.5.1",
"@types/node": "*"
}
},
"jest-util": { "jest-util": {
"version": "27.5.1", "version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz",
@ -92256,6 +92336,15 @@
"safer-buffer": ">= 2.1.2 < 3" "safer-buffer": ">= 2.1.2 < 3"
} }
}, },
"jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"requires": {
"@jest/types": "^27.5.1",
"@types/node": "*"
}
},
"jest-util": { "jest-util": {
"version": "27.5.1", "version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz",
@ -93225,6 +93314,15 @@
"supports-color": "^7.1.0" "supports-color": "^7.1.0"
} }
}, },
"jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"requires": {
"@jest/types": "^27.5.1",
"@types/node": "*"
}
},
"jest-util": { "jest-util": {
"version": "27.5.1", "version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz",
@ -93533,30 +93631,31 @@
} }
}, },
"jest-mock": { "jest-mock": {
"version": "27.5.1", "version": "28.1.3",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-28.1.3.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==", "integrity": "sha512-o3J2jr6dMMWYVH4Lh/NKmDXdosrsJgi4AviS8oXLujcjpCMBb1FMsblDnOXKZKfSiHLxYub1eS0IHuRXsio9eA==",
"requires": { "requires": {
"@jest/types": "^27.5.1", "@jest/types": "^28.1.3",
"@types/node": "*" "@types/node": "*"
}, },
"dependencies": { "dependencies": {
"@jest/types": { "@jest/types": {
"version": "27.5.1", "version": "28.1.3",
"resolved": "https://registry.npmjs.org/@jest/types/-/types-27.5.1.tgz", "resolved": "https://registry.npmjs.org/@jest/types/-/types-28.1.3.tgz",
"integrity": "sha512-Cx46iJ9QpwQTjIdq5VJu2QTMMs3QlEjI0x1QbBP5W1+nMzyc2XmimiRR/CbX9TO0cPTeUlxWMOu8mslYsJ8DEw==", "integrity": "sha512-RyjiyMUZrKz/c+zlMFO1pm70DcIlST8AeWTkoUdZevew44wcNZQHsEVOiCVtgVnlFFD82FPaXycys58cf2muVQ==",
"requires": { "requires": {
"@jest/schemas": "^28.1.3",
"@types/istanbul-lib-coverage": "^2.0.0", "@types/istanbul-lib-coverage": "^2.0.0",
"@types/istanbul-reports": "^3.0.0", "@types/istanbul-reports": "^3.0.0",
"@types/node": "*", "@types/node": "*",
"@types/yargs": "^16.0.0", "@types/yargs": "^17.0.8",
"chalk": "^4.0.0" "chalk": "^4.0.0"
} }
}, },
"@types/yargs": { "@types/yargs": {
"version": "16.0.4", "version": "17.0.10",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.10.tgz",
"integrity": "sha512-T8Yc9wt/5LbJyCaLiHPReJa0kApcIgJ7Bn735GjItUfh08Z1pJvu8QZqb9s+mMvKV6WUQRV7K2R46YbjMXTTJw==", "integrity": "sha512-gmEaFwpj/7f/ROdtIlci1R1VYU1J4j95m8T+Tj3iBgiBFKg1foE/PSl93bBd5T9LDXNPo8UlNN6W0qwD8O5OaA==",
"requires": { "requires": {
"@types/yargs-parser": "*" "@types/yargs-parser": "*"
} }
@ -94046,6 +94145,15 @@
"walker": "^1.0.7" "walker": "^1.0.7"
} }
}, },
"jest-mock": {
"version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-27.5.1.tgz",
"integrity": "sha512-K4jKbY1d4ENhbrG2zuPWaQBvDly+iZ2yAW+T1fATN78hc0sInwn7wZB8XtlNnvHug5RMwV897Xm4LqmPM4e2Og==",
"requires": {
"@jest/types": "^27.5.1",
"@types/node": "*"
}
},
"jest-regex-util": { "jest-regex-util": {
"version": "27.5.1", "version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-regex-util/-/jest-regex-util-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-regex-util/-/jest-regex-util-27.5.1.tgz",

View file

@ -88,6 +88,7 @@
"@types/validator": "^13.7.0", "@types/validator": "^13.7.0",
"concurrently": "^5.1.0", "concurrently": "^5.1.0",
"jest": "^27.4.7", "jest": "^27.4.7",
"jest-mock": "^28.1.3",
"nodemon": "^2.0.2", "nodemon": "^2.0.2",
"run-script-os": "^1.0.7", "run-script-os": "^1.0.7",
"supertest": "^6.2.2", "supertest": "^6.2.2",
@ -96,9 +97,9 @@
"typescript": "~4.6.0" "typescript": "~4.6.0"
}, },
"dependencies": { "dependencies": {
"@oclif/core": "^1.9.3",
"@apidevtools/swagger-cli": "4.0.0", "@apidevtools/swagger-cli": "4.0.0",
"@oclif/command": "^1.5.18", "@oclif/command": "^1.5.18",
"@oclif/core": "^1.9.3",
"@oclif/errors": "^1.2.2", "@oclif/errors": "^1.2.2",
"@rudderstack/rudder-sdk-node": "1.0.6", "@rudderstack/rudder-sdk-node": "1.0.6",
"@types/json-diff": "^0.5.1", "@types/json-diff": "^0.5.1",

View file

@ -1,71 +1,89 @@
/* eslint-disable no-restricted-syntax */
/* eslint-disable import/no-cycle */ /* eslint-disable import/no-cycle */
/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
import { promisify } from 'util'; import { promisify } from 'util';
import { exec } from 'child_process'; import { exec } from 'child_process';
import { access as fsAccess, mkdir as fsMkdir } from 'fs/promises'; import { access as fsAccess, mkdir as fsMkdir } from 'fs/promises';
import axios from 'axios';
import { UserSettings } from 'n8n-core'; import { UserSettings } from 'n8n-core';
import { LoggerProxy, PublicInstalledPackage } from 'n8n-workflow'; import { LoggerProxy, PublicInstalledPackage } from 'n8n-workflow';
import axios from 'axios';
import { import {
NODE_PACKAGE_PREFIX, NODE_PACKAGE_PREFIX,
NPM_COMMAND_TOKENS, NPM_COMMAND_TOKENS,
NPM_PACKAGE_STATUS_GOOD, NPM_PACKAGE_STATUS_GOOD,
RESPONSE_ERROR_MESSAGES, RESPONSE_ERROR_MESSAGES,
UNKNOWN_FAILURE_REASON,
} from '../constants'; } from '../constants';
import { NpmPackageStatusCheck, NpmUpdatesAvailable, ParsedNpmPackageName } from '../Interfaces';
import { InstalledPackages } from '../databases/entities/InstalledPackages'; import { InstalledPackages } from '../databases/entities/InstalledPackages';
import config from '../../config'; import config from '../../config';
import type { CommunityPackages } from '../Interfaces';
const {
PACKAGE_NAME_NOT_PROVIDED,
DISK_IS_FULL,
PACKAGE_FAILED_TO_INSTALL,
PACKAGE_VERSION_NOT_FOUND,
PACKAGE_DOES_NOT_CONTAIN_NODES,
PACKAGE_NOT_FOUND,
} = RESPONSE_ERROR_MESSAGES;
const {
NPM_PACKAGE_NOT_FOUND_ERROR,
NPM_NO_VERSION_AVAILABLE,
NPM_DISK_NO_SPACE,
NPM_DISK_INSUFFICIENT_SPACE,
NPM_PACKAGE_VERSION_NOT_FOUND_ERROR,
} = NPM_COMMAND_TOKENS;
const execAsync = promisify(exec); const execAsync = promisify(exec);
export const parsePackageName = (originalString: string | undefined): ParsedNpmPackageName => { const INVALID_OR_SUSPICIOUS_PACKAGE_NAME = /[^0-9a-z@\-./]/;
if (!originalString) {
throw new Error('Package name was not provided');
}
if (new RegExp(/[^0-9a-z@\-./]/).test(originalString)) { export const parseNpmPackageName = (rawString?: string): CommunityPackages.ParsedPackageName => {
// Prevent any strings that are not valid npm package names or if (!rawString) throw new Error(PACKAGE_NAME_NOT_PROVIDED);
// could indicate malicous commands
if (INVALID_OR_SUSPICIOUS_PACKAGE_NAME.test(rawString))
throw new Error('Package name must be a single word'); throw new Error('Package name must be a single word');
}
const scope = originalString.includes('/') ? originalString.split('/')[0] : undefined; const scope = rawString.includes('/') ? rawString.split('/')[0] : undefined;
const packageNameWithoutScope = scope ? originalString.replace(`${scope}/`, '') : originalString; const packageNameWithoutScope = scope ? rawString.replace(`${scope}/`, '') : rawString;
if (!packageNameWithoutScope.startsWith(NODE_PACKAGE_PREFIX)) { if (!packageNameWithoutScope.startsWith(NODE_PACKAGE_PREFIX)) {
throw new Error('Package name must start with n8n-nodes-'); throw new Error(`Package name must start with ${NODE_PACKAGE_PREFIX}`);
} }
const version = packageNameWithoutScope.includes('@') const version = packageNameWithoutScope.includes('@')
? packageNameWithoutScope.split('@')[1] ? packageNameWithoutScope.split('@')[1]
: undefined; : undefined;
const packageName = version ? originalString.replace(`@${version}`, '') : originalString; const packageName = version ? rawString.replace(`@${version}`, '') : rawString;
return { return {
packageName, packageName,
scope, scope,
version, version,
originalString, rawString,
}; };
}; };
export const sanitizeNpmPackageName = parseNpmPackageName;
export const executeCommand = async ( export const executeCommand = async (
command: string, command: string,
options?: { options?: { doNotHandleError?: boolean },
doNotHandleError?: boolean;
},
): Promise<string> => { ): Promise<string> => {
const downloadFolder = UserSettings.getUserN8nFolderDowloadedNodesPath(); const downloadFolder = UserSettings.getUserN8nFolderDowloadedNodesPath();
// Make sure the node-download folder exists
try { try {
await fsAccess(downloadFolder); await fsAccess(downloadFolder);
// eslint-disable-next-line no-empty } catch (_) {
} catch (error) {
await fsMkdir(downloadFolder); await fsMkdir(downloadFolder);
} }
const execOptions = { const execOptions = {
cwd: downloadFolder, cwd: downloadFolder,
env: { env: {
@ -76,57 +94,48 @@ export const executeCommand = async (
try { try {
const commandResult = await execAsync(command, execOptions); const commandResult = await execAsync(command, execOptions);
return commandResult.stdout; return commandResult.stdout;
} catch (error) { } catch (error) {
if (options?.doNotHandleError) { if (options?.doNotHandleError) throw error;
throw error;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const errorMessage = error.message as string;
if ( const errorMessage = error instanceof Error ? error.message : UNKNOWN_FAILURE_REASON;
errorMessage.includes(NPM_COMMAND_TOKENS.NPM_PACKAGE_NOT_FOUND_ERROR) ||
errorMessage.includes(NPM_COMMAND_TOKENS.NPM_NO_VERSION_AVAILABLE)
) {
throw new Error(RESPONSE_ERROR_MESSAGES.PACKAGE_NOT_FOUND);
}
if (errorMessage.includes(NPM_COMMAND_TOKENS.NPM_PACKAGE_VERSION_NOT_FOUND_ERROR)) {
throw new Error(RESPONSE_ERROR_MESSAGES.PACKAGE_VERSION_NOT_FOUND);
}
if (
errorMessage.includes(NPM_COMMAND_TOKENS.NPM_DISK_NO_SPACE) ||
errorMessage.includes(NPM_COMMAND_TOKENS.NPM_DISK_INSUFFICIENT_SPACE)
) {
throw new Error(RESPONSE_ERROR_MESSAGES.DISK_IS_FULL);
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const map = {
LoggerProxy.warn('npm command failed; see message', { errorMessage }); [NPM_PACKAGE_NOT_FOUND_ERROR]: PACKAGE_NOT_FOUND,
[NPM_NO_VERSION_AVAILABLE]: PACKAGE_NOT_FOUND,
[NPM_PACKAGE_VERSION_NOT_FOUND_ERROR]: PACKAGE_VERSION_NOT_FOUND,
[NPM_DISK_NO_SPACE]: DISK_IS_FULL,
[NPM_DISK_INSUFFICIENT_SPACE]: DISK_IS_FULL,
};
throw new Error('Package could not be installed - check logs for details'); Object.entries(map).forEach(([npmMessage, n8nMessage]) => {
if (errorMessage.includes(npmMessage)) throw new Error(n8nMessage);
});
LoggerProxy.warn('npm command failed', { errorMessage });
throw new Error(PACKAGE_FAILED_TO_INSTALL);
} }
}; };
export function matchPackagesWithUpdates( export function matchPackagesWithUpdates(
installedPackages: InstalledPackages[], packages: InstalledPackages[],
availableUpdates?: NpmUpdatesAvailable, updates?: CommunityPackages.AvailableUpdates,
): PublicInstalledPackage[] { ): PublicInstalledPackage[] {
if (!availableUpdates) { if (!updates) return packages;
return installedPackages;
}
const hydratedPackageList = [] as PublicInstalledPackage[];
for (let i = 0; i < installedPackages.length; i++) { return packages.reduce<PublicInstalledPackage[]>((acc, cur) => {
const installedPackage = installedPackages[i]; const publicPackage: PublicInstalledPackage = { ...cur };
const publicPackage = { ...installedPackage } as PublicInstalledPackage;
if (availableUpdates[installedPackage.packageName]) { const update = updates[cur.packageName];
publicPackage.updateAvailable = availableUpdates[installedPackage.packageName].latest;
}
hydratedPackageList.push(publicPackage);
}
return hydratedPackageList; if (update) publicPackage.updateAvailable = update.latest;
acc.push(publicPackage);
return acc;
}, []);
} }
export function matchMissingPackages( export function matchMissingPackages(
@ -138,7 +147,7 @@ export function matchMissingPackages(
const missingPackagesList = missingPackageNames.map((missingPackageName: string) => { const missingPackagesList = missingPackageNames.map((missingPackageName: string) => {
// Strip away versions but maintain scope and package name // Strip away versions but maintain scope and package name
try { try {
const parsedPackageData = parsePackageName(missingPackageName); const parsedPackageData = parseNpmPackageName(missingPackageName);
return parsedPackageData.packageName; return parsedPackageData.packageName;
// eslint-disable-next-line no-empty // eslint-disable-next-line no-empty
@ -147,6 +156,7 @@ export function matchMissingPackages(
}); });
const hydratedPackageList = [] as PublicInstalledPackage[]; const hydratedPackageList = [] as PublicInstalledPackage[];
installedPackages.forEach((installedPackage) => { installedPackages.forEach((installedPackage) => {
const hydratedInstalledPackage = { ...installedPackage }; const hydratedInstalledPackage = { ...installedPackage };
if (missingPackagesList.includes(hydratedInstalledPackage.packageName)) { if (missingPackagesList.includes(hydratedInstalledPackage.packageName)) {
@ -158,47 +168,38 @@ export function matchMissingPackages(
return hydratedPackageList; return hydratedPackageList;
} }
export async function checkPackageStatus(packageName: string): Promise<NpmPackageStatusCheck> { export async function checkNpmPackageStatus(
// You can change this URL for testing - the default testing url below packageName: string,
// is a postman mock service ): Promise<CommunityPackages.PackageStatusCheck> {
const n8nBackendServiceUrl = 'https://api.n8n.io/api/package'; const N8N_BACKEND_SERVICE_URL = 'https://api.n8n.io/api/package';
try { try {
const output = await axios.post( const response = await axios.post<CommunityPackages.PackageStatusCheck>(
n8nBackendServiceUrl, N8N_BACKEND_SERVICE_URL,
{ name: packageName }, { name: packageName },
{ { method: 'POST' },
method: 'POST',
},
); );
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (response.data.status !== NPM_PACKAGE_STATUS_GOOD) return response.data;
if (output.data.status !== NPM_PACKAGE_STATUS_GOOD) {
return output.data as NpmPackageStatusCheck;
}
} catch (error) { } catch (error) {
// Do nothing if service is unreachable // Do nothing if service is unreachable
} }
return { status: NPM_PACKAGE_STATUS_GOOD }; return { status: NPM_PACKAGE_STATUS_GOOD };
} }
export function hasPackageLoadedSuccessfully(packageName: string): boolean { export function hasPackageLoaded(packageName: string): boolean {
try { const missingPackages = config.get('nodes.packagesMissing') as string | undefined;
const failedPackages = (config.get('nodes.packagesMissing') as string).split(' ');
const packageFailedToLoad = failedPackages.find( if (!missingPackages) return true;
return !missingPackages
.split(' ')
.some(
(packageNameAndVersion) => (packageNameAndVersion) =>
packageNameAndVersion.startsWith(packageName) && packageNameAndVersion.startsWith(packageName) &&
packageNameAndVersion.replace(packageName, '').startsWith('@'), packageNameAndVersion.replace(packageName, '').startsWith('@'),
); );
if (packageFailedToLoad) {
return false;
}
return true;
} catch (_error) {
// If key doesn't exist it means all packages loaded fine
return true;
}
} }
export function removePackageFromMissingList(packageName: string): void { export function removePackageFromMissingList(packageName: string): void {
@ -216,3 +217,17 @@ export function removePackageFromMissingList(packageName: string): void {
// Do nothing // Do nothing
} }
} }
export const isClientError = (error: Error): boolean => {
const clientErrors = [
PACKAGE_VERSION_NOT_FOUND,
PACKAGE_DOES_NOT_CONTAIN_NODES,
PACKAGE_NOT_FOUND,
];
return clientErrors.some((message) => error.message.includes(message));
};
export function isNpmError(error: unknown): error is { code: number; stdout: string } {
return typeof error === 'object' && error !== null && 'code' in error && 'stdout' in error;
}

View file

@ -4,24 +4,25 @@ import { Db } from '..';
import { InstalledNodes } from '../databases/entities/InstalledNodes'; import { InstalledNodes } from '../databases/entities/InstalledNodes';
import { InstalledPackages } from '../databases/entities/InstalledPackages'; import { InstalledPackages } from '../databases/entities/InstalledPackages';
export async function searchInstalledPackage( export async function findInstalledPackage(
packageName: string, packageName: string,
): Promise<InstalledPackages | undefined> { ): Promise<InstalledPackages | undefined> {
const installedPackage = await Db.collections.InstalledPackages.findOne(packageName, { return Db.collections.InstalledPackages.findOne(packageName, { relations: ['installedNodes'] });
relations: ['installedNodes'], }
});
return installedPackage; export async function isPackageInstalled(packageName: string): Promise<boolean> {
const installedPackage = await findInstalledPackage(packageName);
return installedPackage !== undefined;
} }
export async function getAllInstalledPackages(): Promise<InstalledPackages[]> { export async function getAllInstalledPackages(): Promise<InstalledPackages[]> {
const installedPackages = await Db.collections.InstalledPackages.find({ return Db.collections.InstalledPackages.find({ relations: ['installedNodes'] });
relations: ['installedNodes'],
});
return installedPackages;
} }
export async function removePackageFromDatabase(packageName: InstalledPackages): Promise<void> { export async function removePackageFromDatabase(
void (await Db.collections.InstalledPackages.remove(packageName)); packageName: InstalledPackages,
): Promise<InstalledPackages> {
return Db.collections.InstalledPackages.remove(packageName);
} }
export async function persistInstalledPackageData( export async function persistInstalledPackageData(

View file

@ -714,30 +714,32 @@ export interface IWorkflowExecuteProcess {
export type WhereClause = Record<string, { id: string }>; export type WhereClause = Record<string, { id: string }>;
/** ******************************** // ----------------------------------
* Commuinity nodes // community nodes
******************************** */ // ----------------------------------
export type ParsedNpmPackageName = { export namespace CommunityPackages {
packageName: string; export type ParsedPackageName = {
originalString: string; packageName: string;
scope?: string; rawString: string;
version?: string; scope?: string;
}; version?: string;
export type NpmUpdatesAvailable = {
[packageName: string]: {
current: string;
wanted: string;
latest: string;
location: string;
}; };
};
export type NpmPackageStatusCheck = { export type AvailableUpdates = {
status: 'OK' | 'Banned'; [packageName: string]: {
reason?: string; current: string;
}; wanted: string;
latest: string;
location: string;
};
};
export type PackageStatusCheck = {
status: 'OK' | 'Banned';
reason?: string;
};
}
// ---------------------------------- // ----------------------------------
// telemetry // telemetry

View file

@ -35,6 +35,8 @@ export class ResponseError extends Error {
/** /**
* Creates an instance of ResponseError. * Creates an instance of ResponseError.
* Must be used inside a block with `ResponseHelper.send()`.
*
* @param {string} message The error message * @param {string} message The error message
* @param {number} [errorCode] The error code which can be used by frontend to identify the actual error * @param {number} [errorCode] The error code which can be used by frontend to identify the actual error
* @param {number} [httpStatusCode] The HTTP status code the response should have * @param {number} [httpStatusCode] The HTTP status code the response should have

View file

@ -1,44 +1,44 @@
/* eslint-disable import/no-cycle */ /* eslint-disable import/no-cycle */
import express = require('express'); import express from 'express';
import { LoggerProxy, PublicInstalledPackage } from 'n8n-workflow'; import { PublicInstalledPackage } from 'n8n-workflow';
import { getLogger } from '../Logger';
import config from '../../config';
import { ResponseHelper, LoadNodesAndCredentials, Push, InternalHooksManager } from '..'; import { ResponseHelper, LoadNodesAndCredentials, Push, InternalHooksManager } from '..';
import { NodeRequest } from '../requests';
import { RESPONSE_ERROR_MESSAGES } from '../constants'; import { RESPONSE_ERROR_MESSAGES, UNKNOWN_FAILURE_REASON } from '../constants';
import { import {
matchMissingPackages, matchMissingPackages,
matchPackagesWithUpdates, matchPackagesWithUpdates,
executeCommand, executeCommand,
checkPackageStatus, checkNpmPackageStatus,
hasPackageLoadedSuccessfully, hasPackageLoaded,
removePackageFromMissingList, removePackageFromMissingList,
parsePackageName, parseNpmPackageName,
isClientError,
sanitizeNpmPackageName,
isNpmError,
} from '../CommunityNodes/helpers'; } from '../CommunityNodes/helpers';
import { getAllInstalledPackages, searchInstalledPackage } from '../CommunityNodes/packageModel'; import {
getAllInstalledPackages,
findInstalledPackage,
isPackageInstalled,
} from '../CommunityNodes/packageModel';
import { isAuthenticatedRequest } from '../UserManagement/UserManagementHelper'; import { isAuthenticatedRequest } from '../UserManagement/UserManagementHelper';
import config = require('../../config');
import { NpmUpdatesAvailable } from '../Interfaces'; import type { NodeRequest } from '../requests';
import type { CommunityPackages } from '../Interfaces';
import { InstalledPackages } from '../databases/entities/InstalledPackages';
const { PACKAGE_NOT_INSTALLED, PACKAGE_NAME_NOT_PROVIDED } = RESPONSE_ERROR_MESSAGES;
export const nodesController = express.Router(); export const nodesController = express.Router();
/**
* Initialize Logger if needed
*/
nodesController.use((req, res, next) => {
try {
LoggerProxy.getInstance();
} catch (error) {
LoggerProxy.init(getLogger());
}
next();
});
nodesController.use((req, res, next) => { nodesController.use((req, res, next) => {
if (!isAuthenticatedRequest(req) || req.user.globalRole.name !== 'owner') { if (!isAuthenticatedRequest(req) || req.user.globalRole.name !== 'owner') {
res.status(403).json({ status: 'error', message: 'Unauthorized' }); res.status(403).json({ status: 'error', message: 'Unauthorized' });
return; return;
} }
next(); next();
}); });
@ -50,246 +50,263 @@ nodesController.use((req, res, next) => {
}); });
return; return;
} }
next(); next();
}); });
/**
* POST /nodes
*
* Install an n8n community package
*/
nodesController.post( nodesController.post(
'/', '/',
ResponseHelper.send(async (req: NodeRequest.Post) => { ResponseHelper.send(async (req: NodeRequest.Post) => {
const { name } = req.body; const { name } = req.body;
let parsedPackageName;
try { if (!name) {
parsedPackageName = parsePackageName(name); throw new ResponseHelper.ResponseError(PACKAGE_NAME_NOT_PROVIDED, undefined, 400);
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
throw new ResponseHelper.ResponseError(error.message, undefined, 400);
} }
// Only install packages that haven't been installed let parsed: CommunityPackages.ParsedPackageName;
// or that have failed loading
const installedPackageInstalled = await searchInstalledPackage(parsedPackageName.packageName); try {
const loadedPackage = hasPackageLoadedSuccessfully(name); parsed = parseNpmPackageName(name);
if (installedPackageInstalled && loadedPackage) { } catch (error) {
throw new ResponseHelper.ResponseError( throw new ResponseHelper.ResponseError(
`Package "${parsedPackageName.packageName}" is already installed. For updating, click the corresponding button.`, error instanceof Error ? error.message : 'Failed to parse package name',
undefined, undefined,
400, 400,
); );
} }
const packageStatus = await checkPackageStatus(name); const isInstalled = await isPackageInstalled(parsed.packageName);
const hasLoaded = hasPackageLoaded(name);
if (isInstalled && hasLoaded) {
throw new ResponseHelper.ResponseError(
[
`Package "${parsed.packageName}" is already installed`,
'To update it, click the corresponding button in the UI',
].join('.'),
undefined,
400,
);
}
const packageStatus = await checkNpmPackageStatus(name);
if (packageStatus.status !== 'OK') { if (packageStatus.status !== 'OK') {
throw new ResponseHelper.ResponseError( throw new ResponseHelper.ResponseError(
`Package "${name}" has been banned from n8n's repository and will not be installed`, `Package "${name}" is banned so it cannot be installed`,
undefined, undefined,
400, 400,
); );
} }
let installedPackage: InstalledPackages;
try { try {
const installedPackage = await LoadNodesAndCredentials().loadNpmModule( installedPackage = await LoadNodesAndCredentials().loadNpmModule(
parsedPackageName.packageName, parsed.packageName,
parsedPackageName.version, parsed.version,
); );
if (!loadedPackage) {
removePackageFromMissingList(name);
}
// Inform the connected frontends that new nodes are available
installedPackage.installedNodes.forEach((nodeData) => {
const pushInstance = Push.getInstance();
pushInstance.send('reloadNodeType', {
name: nodeData.type,
version: nodeData.latestVersion,
});
});
void InternalHooksManager.getInstance().onCommunityPackageInstallFinished({
user_id: req.user.id,
input_string: name,
package_name: parsedPackageName.packageName,
success: true,
package_version: parsedPackageName.version,
package_node_names: installedPackage.installedNodes.map((nodeData) => nodeData.name),
package_author: installedPackage.authorName,
package_author_email: installedPackage.authorEmail,
});
return installedPackage;
} catch (error) { } catch (error) {
let statusCode = 500; const errorMessage = error instanceof Error ? error.message : UNKNOWN_FAILURE_REASON;
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
const errorMessage = error.message as string;
if (
errorMessage.includes(RESPONSE_ERROR_MESSAGES.PACKAGE_VERSION_NOT_FOUND) ||
errorMessage.includes(RESPONSE_ERROR_MESSAGES.PACKAGE_DOES_NOT_CONTAIN_NODES) ||
errorMessage.includes(RESPONSE_ERROR_MESSAGES.PACKAGE_NOT_FOUND)
) {
statusCode = 400;
}
void InternalHooksManager.getInstance().onCommunityPackageInstallFinished({ void InternalHooksManager.getInstance().onCommunityPackageInstallFinished({
user_id: req.user.id, user_id: req.user.id,
input_string: name, input_string: name,
package_name: parsedPackageName.packageName, package_name: parsed.packageName,
success: false, success: false,
package_version: parsedPackageName.version, package_version: parsed.version,
failure_reason: errorMessage, failure_reason: errorMessage,
}); });
throw new ResponseHelper.ResponseError(
`Error loading package "${name}": ${errorMessage}`, const message = [`Error loading package "${name}"`, errorMessage].join(':');
undefined,
statusCode, const clientError = error instanceof Error ? isClientError(error) : false;
);
throw new ResponseHelper.ResponseError(message, undefined, clientError ? 400 : 500);
} }
if (!hasLoaded) removePackageFromMissingList(name);
const pushInstance = Push.getInstance();
// broadcast to connected frontends that node list has been updated
installedPackage.installedNodes.forEach((node) => {
pushInstance.send('reloadNodeType', {
name: node.type,
version: node.latestVersion,
});
});
void InternalHooksManager.getInstance().onCommunityPackageInstallFinished({
user_id: req.user.id,
input_string: name,
package_name: parsed.packageName,
success: true,
package_version: parsed.version,
package_node_names: installedPackage.installedNodes.map((node) => node.name),
package_author: installedPackage.authorName,
package_author_email: installedPackage.authorEmail,
});
return installedPackage;
}), }),
); );
// Install new credentials/nodes from npm /**
* GET /nodes
*
* Retrieve list of installed n8n community packages
*/
nodesController.get( nodesController.get(
'/', '/',
ResponseHelper.send(async (): Promise<PublicInstalledPackage[]> => { ResponseHelper.send(async (): Promise<PublicInstalledPackage[]> => {
const packages = await getAllInstalledPackages(); const installedPackages = await getAllInstalledPackages();
if (packages.length === 0) { if (installedPackages.length === 0) return [];
return packages;
} let pendingUpdates: CommunityPackages.AvailableUpdates | undefined;
let pendingUpdates: NpmUpdatesAvailable | undefined;
try { try {
// Command succeeds when there are no updates. const command = ['npm', 'outdated', '--json'].join(' ');
// NPM handles this oddly. It exits with code 1 when there are updates. await executeCommand(command, { doNotHandleError: true });
// More here: https://github.com/npm/rfcs/issues/473
await executeCommand('npm outdated --json', { doNotHandleError: true });
} catch (error) { } catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access // when there are updates, npm exits with code 1
if (error.code === 1) { // when there are no updates, command succeeds
// Updates available // https://github.com/npm/rfcs/issues/473
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
pendingUpdates = JSON.parse(error.stdout); if (isNpmError(error) && error.code === 1) {
pendingUpdates = JSON.parse(error.stdout) as CommunityPackages.AvailableUpdates;
} }
} }
let hydratedPackages = matchPackagesWithUpdates(packages, pendingUpdates);
let hydratedPackages = matchPackagesWithUpdates(installedPackages, pendingUpdates);
try { try {
if (config.get('nodes.packagesMissing')) { const missingPackages = config.get('nodes.packagesMissing') as string | undefined;
// eslint-disable-next-line prettier/prettier, @typescript-eslint/no-unsafe-argument
hydratedPackages = matchMissingPackages(hydratedPackages, config.get('nodes.packagesMissing')); if (missingPackages) {
hydratedPackages = matchMissingPackages(hydratedPackages, missingPackages);
} }
} catch (error) { } catch (_) {
// Do nothing if setting is missing // Do nothing if setting is missing
} }
return hydratedPackages; return hydratedPackages;
}), }),
); );
// Uninstall credentials/nodes from npm /**
* DELETE /nodes
*
* Uninstall an installed n8n community package
*/
nodesController.delete( nodesController.delete(
'/', '/',
ResponseHelper.send(async (req: NodeRequest.Delete) => { ResponseHelper.send(async (req: NodeRequest.Delete) => {
const { name } = req.body; const { name } = req.body;
if (!name) { if (!name) {
throw new ResponseHelper.ResponseError( throw new ResponseHelper.ResponseError(PACKAGE_NAME_NOT_PROVIDED, undefined, 400);
RESPONSE_ERROR_MESSAGES.PACKAGE_NAME_NOT_PROVIDED,
undefined,
400,
);
}
// This function also sanitizes the package name by throwing errors.
parsePackageName(name);
const installedPackage = await searchInstalledPackage(name);
if (!installedPackage) {
throw new ResponseHelper.ResponseError(
RESPONSE_ERROR_MESSAGES.PACKAGE_NOT_INSTALLED,
undefined,
400,
);
} }
try { try {
void (await LoadNodesAndCredentials().removeNpmModule(name, installedPackage)); sanitizeNpmPackageName(name);
// Inform the connected frontends that the node list has been updated
installedPackage.installedNodes.forEach((installedNode) => {
const pushInstance = Push.getInstance();
pushInstance.send('removeNodeType', {
name: installedNode.type,
version: installedNode.latestVersion,
});
});
void InternalHooksManager.getInstance().onCommunityPackageDeleteFinished({
user_id: req.user.id,
package_name: name,
package_version: installedPackage.installedVersion,
package_node_names: installedPackage.installedNodes.map((nodeData) => nodeData.name),
package_author: installedPackage.authorName,
package_author_email: installedPackage.authorEmail,
});
} catch (error) { } catch (error) {
throw new ResponseHelper.ResponseError( const message = error instanceof Error ? error.message : UNKNOWN_FAILURE_REASON;
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-member-access
`Error removing package "${name}": ${error.message}`, throw new ResponseHelper.ResponseError(message, undefined, 400);
undefined,
500,
);
} }
const installedPackage = await findInstalledPackage(name);
if (!installedPackage) {
throw new ResponseHelper.ResponseError(PACKAGE_NOT_INSTALLED, undefined, 400);
}
try {
await LoadNodesAndCredentials().removeNpmModule(name, installedPackage);
} catch (error) {
const message = [
`Error removing package "${name}"`,
error instanceof Error ? error.message : UNKNOWN_FAILURE_REASON,
].join(':');
throw new ResponseHelper.ResponseError(message, undefined, 500);
}
const pushInstance = Push.getInstance();
// broadcast to connected frontends that node list has been updated
installedPackage.installedNodes.forEach((node) => {
pushInstance.send('removeNodeType', {
name: node.type,
version: node.latestVersion,
});
});
void InternalHooksManager.getInstance().onCommunityPackageDeleteFinished({
user_id: req.user.id,
package_name: name,
package_version: installedPackage.installedVersion,
package_node_names: installedPackage.installedNodes.map((node) => node.name),
package_author: installedPackage.authorName,
package_author_email: installedPackage.authorEmail,
});
}), }),
); );
// Update a package /**
* PATCH /nodes
*
* Update an installed n8n community package
*/
nodesController.patch( nodesController.patch(
'/', '/',
ResponseHelper.send(async (req: NodeRequest.Update) => { ResponseHelper.send(async (req: NodeRequest.Update) => {
const { name } = req.body; const { name } = req.body;
if (!name) { if (!name) {
throw new ResponseHelper.ResponseError( throw new ResponseHelper.ResponseError(PACKAGE_NAME_NOT_PROVIDED, undefined, 400);
RESPONSE_ERROR_MESSAGES.PACKAGE_NAME_NOT_PROVIDED,
undefined,
400,
);
} }
const parsedPackageData = parsePackageName(name); const previouslyInstalledPackage = await findInstalledPackage(name);
const packagePreviouslyInstalled = await searchInstalledPackage(name);
if (!packagePreviouslyInstalled) { if (!previouslyInstalledPackage) {
throw new ResponseHelper.ResponseError( throw new ResponseHelper.ResponseError(PACKAGE_NOT_INSTALLED, undefined, 400);
RESPONSE_ERROR_MESSAGES.PACKAGE_NOT_INSTALLED,
undefined,
400,
);
} }
try { try {
const newInstalledPackage = await LoadNodesAndCredentials().updateNpmModule( const newInstalledPackage = await LoadNodesAndCredentials().updateNpmModule(
parsedPackageData.packageName, parseNpmPackageName(name).packageName,
packagePreviouslyInstalled, previouslyInstalledPackage,
); );
const pushInstance = Push.getInstance(); const pushInstance = Push.getInstance();
// Inform the connected frontends that new nodes are available // broadcast to connected frontends that node list has been updated
packagePreviouslyInstalled.installedNodes.forEach((installedNode) => { previouslyInstalledPackage.installedNodes.forEach((node) => {
pushInstance.send('removeNodeType', { pushInstance.send('removeNodeType', {
name: installedNode.type, name: node.type,
version: installedNode.latestVersion, version: node.latestVersion,
}); });
}); });
newInstalledPackage.installedNodes.forEach((nodeData) => { newInstalledPackage.installedNodes.forEach((node) => {
pushInstance.send('reloadNodeType', { pushInstance.send('reloadNodeType', {
name: nodeData.name, name: node.name,
version: nodeData.latestVersion, version: node.latestVersion,
}); });
}); });
void InternalHooksManager.getInstance().onCommunityPackageUpdateFinished({ void InternalHooksManager.getInstance().onCommunityPackageUpdateFinished({
user_id: req.user.id, user_id: req.user.id,
package_name: name, package_name: name,
package_version_current: packagePreviouslyInstalled.installedVersion, package_version_current: previouslyInstalledPackage.installedVersion,
package_version_new: newInstalledPackage.installedVersion, package_version_new: newInstalledPackage.installedVersion,
package_node_names: newInstalledPackage.installedNodes.map((node) => node.name), package_node_names: newInstalledPackage.installedNodes.map((node) => node.name),
package_author: newInstalledPackage.authorName, package_author: newInstalledPackage.authorName,
@ -298,19 +315,20 @@ nodesController.patch(
return newInstalledPackage; return newInstalledPackage;
} catch (error) { } catch (error) {
packagePreviouslyInstalled.installedNodes.forEach((installedNode) => { previouslyInstalledPackage.installedNodes.forEach((node) => {
const pushInstance = Push.getInstance(); const pushInstance = Push.getInstance();
pushInstance.send('removeNodeType', { pushInstance.send('removeNodeType', {
name: installedNode.type, name: node.type,
version: installedNode.latestVersion, version: node.latestVersion,
}); });
}); });
throw new ResponseHelper.ResponseError(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-member-access const message = [
`Error updating package "${name}": ${error.message}`, `Error removing package "${name}"`,
undefined, error instanceof Error ? error.message : UNKNOWN_FAILURE_REASON,
500, ].join(':');
);
throw new ResponseHelper.ResponseError(message, undefined, 500);
} }
}), }),
); );

View file

@ -12,6 +12,7 @@ export const RESPONSE_ERROR_MESSAGES = {
PACKAGE_NAME_NOT_PROVIDED: 'Package name is required', PACKAGE_NAME_NOT_PROVIDED: 'Package name is required',
PACKAGE_NAME_NOT_VALID: `Package name is not valid - it must start with "${NODE_PACKAGE_PREFIX}"`, PACKAGE_NAME_NOT_VALID: `Package name is not valid - it must start with "${NODE_PACKAGE_PREFIX}"`,
PACKAGE_NOT_INSTALLED: 'This package is not installed - you must install it first', PACKAGE_NOT_INSTALLED: 'This package is not installed - you must install it first',
PACKAGE_FAILED_TO_INSTALL: 'Package could not be installed - check logs for details',
PACKAGE_NOT_FOUND: 'Package not found in npm', PACKAGE_NOT_FOUND: 'Package not found in npm',
PACKAGE_VERSION_NOT_FOUND: 'The specified package version was not found', PACKAGE_VERSION_NOT_FOUND: 'The specified package version was not found',
PACKAGE_DOES_NOT_CONTAIN_NODES: 'The specified package does not contain any nodes', PACKAGE_DOES_NOT_CONTAIN_NODES: 'The specified package does not contain any nodes',
@ -29,3 +30,5 @@ export const NPM_COMMAND_TOKENS = {
}; };
export const NPM_PACKAGE_STATUS_GOOD = 'OK'; export const NPM_PACKAGE_STATUS_GOOD = 'OK';
export const UNKNOWN_FAILURE_REASON = 'Unknown failure reason';

View file

@ -286,21 +286,21 @@ export type NodeParameterOptionsRequest = AuthenticatedRequest<
>; >;
// ---------------------------------- // ----------------------------------
// /tags // /tags
// ---------------------------------- // ----------------------------------
export declare namespace TagsRequest { export declare namespace TagsRequest {
type Delete = AuthenticatedRequest<{ id: string }>; type Delete = AuthenticatedRequest<{ id: string }>;
} }
export declare namespace NodeRequest { // ----------------------------------
type RequestBody = { // /nodes
name: string; // ----------------------------------
};
export declare namespace NodeRequest {
type GetAll = AuthenticatedRequest; type GetAll = AuthenticatedRequest;
type Post = AuthenticatedRequest<{}, {}, RequestBody>; type Post = AuthenticatedRequest<{}, {}, { name?: string }>;
type Delete = Post; type Delete = Post;

View file

@ -1,341 +1,333 @@
import { exec } from 'child_process'; import path from 'path';
import express from 'express'; import express from 'express';
import { mocked } from 'jest-mock';
import * as utils from './shared/utils'; import * as utils from './shared/utils';
import type { InstalledNodePayload, InstalledPackagePayload } from './shared/types';
import type { Role } from '../../src/databases/entities/Role';
import type { User } from '../../src/databases/entities/User';
import * as testDb from './shared/testDb'; import * as testDb from './shared/testDb';
import {
jest.mock('../../src/CommunityNodes/helpers', () => ({ executeCommand,
matchPackagesWithUpdates: jest.requireActual('../../src/CommunityNodes/helpers').matchPackagesWithUpdates, checkNpmPackageStatus,
parsePackageName: jest.requireActual('../../src/CommunityNodes/helpers').parsePackageName, hasPackageLoaded,
hasPackageLoadedSuccessfully: jest.fn(), removePackageFromMissingList,
searchInstalledPackage: jest.fn(), isNpmError,
executeCommand: jest.fn(), } from '../../src/CommunityNodes/helpers';
checkPackageStatus: jest.fn(), import { findInstalledPackage, isPackageInstalled } from '../../src/CommunityNodes/packageModel';
removePackageFromMissingList: jest.fn(),
}));
jest.mock('../../src/CommunityNodes/packageModel', () => ({
getAllInstalledPackages: jest.requireActual('../../src/CommunityNodes/packageModel').getAllInstalledPackages,
removePackageFromDatabase: jest.fn(),
searchInstalledPackage: jest.fn(),
}));
import { executeCommand, checkPackageStatus, hasPackageLoadedSuccessfully, removePackageFromMissingList } from '../../src/CommunityNodes/helpers';
import { getAllInstalledPackages, searchInstalledPackage, removePackageFromDatabase } from '../../src/CommunityNodes/packageModel';
import { CURRENT_PACKAGE_VERSION, UPDATED_PACKAGE_VERSION } from './shared/constants'; import { CURRENT_PACKAGE_VERSION, UPDATED_PACKAGE_VERSION } from './shared/constants';
import { installedPackagePayload } from './shared/utils'; import { LoadNodesAndCredentials } from '../../src/LoadNodesAndCredentials';
import { InstalledPackages } from '../../src/databases/entities/InstalledPackages';
import type { Role } from '../../src/databases/entities/Role';
import type { AuthAgent } from './shared/types';
import type { InstalledNodes } from '../../src/databases/entities/InstalledNodes';
jest.mock('../../src/telemetry'); jest.mock('../../src/telemetry');
jest.mock('../../src/LoadNodesAndCredentials', () => ({ jest.mock('../../src/CommunityNodes/helpers', () => {
LoadNodesAndCredentials: jest.fn(), return {
})); ...jest.requireActual('../../src/CommunityNodes/helpers'),
import { LoadNodesAndCredentials } from '../../src/LoadNodesAndCredentials'; checkNpmPackageStatus: jest.fn(),
executeCommand: jest.fn(),
hasPackageLoaded: jest.fn(),
isNpmError: jest.fn(),
removePackageFromMissingList: jest.fn(),
};
});
jest.mock('../../src/CommunityNodes/packageModel', () => {
return {
...jest.requireActual('../../src/CommunityNodes/packageModel'),
isPackageInstalled: jest.fn(),
findInstalledPackage: jest.fn(),
};
});
const mockedEmptyPackage = mocked(utils.emptyPackage);
let app: express.Application; let app: express.Application;
let testDbName = ''; let testDbName = '';
let globalOwnerRole: Role; let globalOwnerRole: Role;
let globalMemberRole: Role; let authAgent: AuthAgent;
let ownerShell: User;
beforeAll(async () => { beforeAll(async () => {
app = await utils.initTestServer({ endpointGroups: ['nodes'], applyAuth: true }); app = await utils.initTestServer({ endpointGroups: ['nodes'], applyAuth: true });
const initResult = await testDb.init(); const initResult = await testDb.init();
testDbName = initResult.testDbName; testDbName = initResult.testDbName;
utils.initConfigFile();
globalOwnerRole = await testDb.getGlobalOwnerRole(); globalOwnerRole = await testDb.getGlobalOwnerRole();
globalMemberRole = await testDb.getGlobalMemberRole();
ownerShell = await testDb.createUserShell(globalOwnerRole);
authAgent = utils.createAuthAgent(app);
utils.initConfigFile();
utils.initTestLogger(); utils.initTestLogger();
utils.initTestTelemetry(); utils.initTestTelemetry();
}); });
beforeEach(async () => { beforeEach(async () => {
await testDb.truncate(['InstalledNodes', 'InstalledPackages'], testDbName); await testDb.truncate(['InstalledNodes', 'InstalledPackages'], testDbName);
// @ts-ignore
executeCommand.mockReset(); mocked(executeCommand).mockReset();
// @ts-ignore mocked(findInstalledPackage).mockReset();
checkPackageStatus.mockReset();
// @ts-ignore
searchInstalledPackage.mockReset();
// @ts-ignore
hasPackageLoadedSuccessfully.mockReset();
}); });
afterAll(async () => { afterAll(async () => {
await testDb.terminate(testDbName); await testDb.terminate(testDbName);
}); });
test('GET /nodes should return empty list when no nodes are installed', async () => { /**
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); * GET /nodes
*/
const response = await authOwnerAgent.get('/nodes').send(); test('GET /nodes should respond 200 if no nodes are installed', async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
expect(response.statusCode).toBe(200); const {
expect(response.body.data).toHaveLength(0); statusCode,
body: { data },
} = await authAgent(ownerShell).get('/nodes');
expect(statusCode).toBe(200);
expect(data).toHaveLength(0);
}); });
test('GET /nodes should return list with installed package and node', async () => { test('GET /nodes should return list of one installed package and node', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); const ownerShell = await testDb.createUserShell(globalOwnerRole);
const installedPackage = await saveMockPackage(installedPackagePayload());
await saveMockNode(utils.installedNodePayload(installedPackage.packageName));
const response = await authOwnerAgent.get('/nodes').send(); const { packageName } = await testDb.saveInstalledPackage(utils.installedPackagePayload());
await testDb.saveInstalledNode(utils.installedNodePayload(packageName));
expect(response.statusCode).toBe(200); const {
expect(response.body.data).toHaveLength(1); statusCode,
expect(response.body.data[0].installedNodes).toHaveLength(1); body: { data },
} = await authAgent(ownerShell).get('/nodes');
expect(statusCode).toBe(200);
expect(data).toHaveLength(1);
expect(data[0].installedNodes).toHaveLength(1);
}); });
test('GET /nodes should return list with multiple installed package and node', async () => { test('GET /nodes should return list of multiple installed packages and nodes', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); const ownerShell = await testDb.createUserShell(globalOwnerRole);
const installedPackage1 = await saveMockPackage(installedPackagePayload());
await saveMockNode(utils.installedNodePayload(installedPackage1.packageName));
const installedPackage2 = await saveMockPackage(installedPackagePayload()); const first = await testDb.saveInstalledPackage(utils.installedPackagePayload());
await saveMockNode(utils.installedNodePayload(installedPackage2.packageName)); await testDb.saveInstalledNode(utils.installedNodePayload(first.packageName));
await saveMockNode(utils.installedNodePayload(installedPackage2.packageName));
const response = await authOwnerAgent.get('/nodes').send(); const second = await testDb.saveInstalledPackage(utils.installedPackagePayload());
await testDb.saveInstalledNode(utils.installedNodePayload(second.packageName));
await testDb.saveInstalledNode(utils.installedNodePayload(second.packageName));
expect(response.statusCode).toBe(200); const {
expect(response.body.data).toHaveLength(2); statusCode,
expect([...response.body.data[0].installedNodes, ...response.body.data[1].installedNodes]).toHaveLength(3); body: { data },
} = await authAgent(ownerShell).get('/nodes');
expect(statusCode).toBe(200);
expect(data).toHaveLength(2);
const allNodes = data.reduce(
(acc: InstalledNodes[], cur: InstalledPackages) => acc.concat(cur.installedNodes),
[],
);
expect(allNodes).toHaveLength(3);
}); });
test('GET /nodes should not check for updates when there are no packages installed', async () => { test('GET /nodes should not check for updates if no packages installed', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); const ownerShell = await testDb.createUserShell(globalOwnerRole);
await authOwnerAgent.get('/nodes').send(); await authAgent(ownerShell).get('/nodes');
expect(executeCommand).toHaveBeenCalledTimes(0); expect(mocked(executeCommand)).toHaveBeenCalledTimes(0);
}); });
test('GET /nodes should check for updates when there are packages installed', async () => { test('GET /nodes should check for updates if packages installed', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); const ownerShell = await testDb.createUserShell(globalOwnerRole);
const installedPackage = await saveMockPackage(installedPackagePayload());
await saveMockNode(utils.installedNodePayload(installedPackage.packageName));
await authOwnerAgent.get('/nodes').send(); const { packageName } = await testDb.saveInstalledPackage(utils.installedPackagePayload());
await testDb.saveInstalledNode(utils.installedNodePayload(packageName));
expect(executeCommand).toHaveBeenCalledWith('npm outdated --json', {"doNotHandleError": true}); await authAgent(ownerShell).get('/nodes');
expect(mocked(executeCommand)).toHaveBeenCalledWith('npm outdated --json', {
doNotHandleError: true,
});
}); });
test('GET /nodes should mention updates when available', async () => { test('GET /nodes should report package updates if available', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); const ownerShell = await testDb.createUserShell(globalOwnerRole);
const installedPackage = await saveMockPackage(installedPackagePayload());
await saveMockNode(utils.installedNodePayload(installedPackage.packageName));
// @ts-ignore const { packageName } = await testDb.saveInstalledPackage(utils.installedPackagePayload());
executeCommand.mockImplementation(() => { await testDb.saveInstalledNode(utils.installedNodePayload(packageName));
throw getNpmOutdatedError(installedPackage.packageName);
mocked(executeCommand).mockImplementationOnce(() => {
throw {
code: 1,
stdout: JSON.stringify({
[packageName]: {
current: CURRENT_PACKAGE_VERSION,
wanted: CURRENT_PACKAGE_VERSION,
latest: UPDATED_PACKAGE_VERSION,
location: path.join('node_modules', packageName),
},
}),
};
}); });
const response = await authOwnerAgent.get('/nodes').send(); mocked(isNpmError).mockReturnValueOnce(true);
expect(response.body.data[0].installedVersion).toBe(CURRENT_PACKAGE_VERSION);
expect(response.body.data[0].updateAvailable).toBe(UPDATED_PACKAGE_VERSION); const {
body: { data },
} = await authAgent(ownerShell).get('/nodes');
expect(data[0].installedVersion).toBe(CURRENT_PACKAGE_VERSION);
expect(data[0].updateAvailable).toBe(UPDATED_PACKAGE_VERSION);
}); });
// TEST POST ENDPOINT /**
* POST /nodes
*/
test('POST /nodes package name should not be empty', async () => { test('POST /nodes should reject if package name is missing', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); const ownerShell = await testDb.createUserShell(globalOwnerRole);
const response = await authOwnerAgent.post('/nodes').send();
const { statusCode } = await authAgent(ownerShell).post('/nodes');
expect(statusCode).toBe(400);
});
test('POST /nodes should reject if package is duplicate', async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
mocked(findInstalledPackage).mockResolvedValueOnce(new InstalledPackages());
mocked(isPackageInstalled).mockResolvedValueOnce(true);
mocked(hasPackageLoaded).mockReturnValueOnce(true);
const {
statusCode,
body: { message },
} = await authAgent(ownerShell).post('/nodes').send({
name: utils.installedPackagePayload().packageName,
});
expect(statusCode).toBe(400);
expect(message).toContain('already installed');
});
test('POST /nodes should allow installing packages that could not be loaded', async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
mocked(findInstalledPackage).mockResolvedValueOnce(new InstalledPackages());
mocked(hasPackageLoaded).mockReturnValueOnce(false);
mocked(checkNpmPackageStatus).mockResolvedValueOnce({ status: 'OK' });
jest
.spyOn(LoadNodesAndCredentials(), 'loadNpmModule')
.mockImplementationOnce(mockedEmptyPackage);
const { statusCode } = await authAgent(ownerShell).post('/nodes').send({
name: utils.installedPackagePayload().packageName,
});
expect(statusCode).toBe(200);
expect(mocked(removePackageFromMissingList)).toHaveBeenCalled();
});
test('POST /nodes should not install a banned package', async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
mocked(checkNpmPackageStatus).mockResolvedValueOnce({ status: 'Banned' });
const {
statusCode,
body: { message },
} = await authAgent(ownerShell).post('/nodes').send({
name: utils.installedPackagePayload().packageName,
});
expect(statusCode).toBe(400);
expect(message).toContain('banned');
});
/**
* DELETE /nodes
*/
test('DELETE /nodes should not delete if package name is empty', async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
const response = await authAgent(ownerShell).delete('/nodes');
expect(response.statusCode).toBe(400); expect(response.statusCode).toBe(400);
}); });
test('POST /nodes Should not install duplicate packages', async () => { test('DELETE /nodes should reject if package is not installed', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); const ownerShell = await testDb.createUserShell(globalOwnerRole);
const requestBody = {
name: installedPackagePayload().packageName, const {
}; statusCode,
// @ts-ignore body: { message },
searchInstalledPackage.mockImplementation(() => { } = await authAgent(ownerShell).delete('/nodes').send({
return true; name: utils.installedPackagePayload().packageName,
});
// @ts-ignore
hasPackageLoadedSuccessfully.mockImplementation(() => {
return true;
}); });
const response = await authOwnerAgent.post('/nodes').send(requestBody); expect(statusCode).toBe(400);
expect(response.status).toBe(400); expect(message).toContain('not installed');
expect(response.body.message).toContain('already installed');
}); });
test('POST /nodes Should allow installing packages that could not be loaded', async () => { test('DELETE /nodes should uninstall package', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); const ownerShell = await testDb.createUserShell(globalOwnerRole);
const requestBody = {
name: installedPackagePayload().packageName, const removeSpy = jest
}; .spyOn(LoadNodesAndCredentials(), 'removeNpmModule')
// @ts-ignore .mockImplementationOnce(jest.fn());
searchInstalledPackage.mockImplementation(() => {
return true; mocked(findInstalledPackage).mockImplementationOnce(mockedEmptyPackage);
});
// @ts-ignore const { statusCode } = await authAgent(ownerShell).delete('/nodes').send({
hasPackageLoadedSuccessfully.mockImplementation(() => { name: utils.installedPackagePayload().packageName,
return false;
}); });
// @ts-ignore expect(statusCode).toBe(200);
checkPackageStatus.mockImplementation(() => { expect(removeSpy).toHaveBeenCalledTimes(1);
return {status:'OK'};
});
// @ts-ignore
LoadNodesAndCredentials.mockImplementation(() => {
return {
loadNpmModule: () => {
return {
installedNodes: [],
};
},
};
});
const response = await authOwnerAgent.post('/nodes').send(requestBody);
expect(removePackageFromMissingList).toHaveBeenCalled();
expect(response.status).toBe(200);
}); });
test('POST /nodes package should not install banned package', async () => { /**
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); * PATCH /nodes
const installedPackage = installedPackagePayload(); */
const requestBody = {
name: installedPackage.packageName,
};
// @ts-ignore test('PATCH /nodes should reject if package name is empty', async () => {
checkPackageStatus.mockImplementation(() => { const ownerShell = await testDb.createUserShell(globalOwnerRole);
return {status:'Banned'};
});
const response = await authOwnerAgent.post('/nodes').send(requestBody);
expect(response.statusCode).toBe(400);
expect(response.body.message).toContain('banned');
});
// TEST DELETE ENDPOINT const response = await authAgent(ownerShell).patch('/nodes');
test('DELETE /nodes package name should not be empty', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const response = await authOwnerAgent.delete('/nodes').send();
expect(response.statusCode).toBe(400); expect(response.statusCode).toBe(400);
}); });
test('DELETE /nodes Should return error when package was not installed', async () => { test('PATCH /nodes reject if package is not installed', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell }); const ownerShell = await testDb.createUserShell(globalOwnerRole);
const requestBody = {
name: installedPackagePayload().packageName,
};
const response = await authOwnerAgent.delete('/nodes').send(requestBody); const {
expect(response.status).toBe(400); statusCode,
expect(response.body.message).toContain('not installed'); body: { message },
}); } = await authAgent(ownerShell).patch('/nodes').send({
name: utils.installedPackagePayload().packageName,
// Useful test ?
test('DELETE /nodes package should be uninstall all conditions are true', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const requestBody = {
name: installedPackagePayload().packageName,
};
// @ts-ignore
searchInstalledPackage.mockImplementation(() => {
return {
installedNodes: [],
};
}); });
const removeNpmModuleMock = jest.fn(); expect(statusCode).toBe(400);
// @ts-ignore expect(message).toContain('not installed');
LoadNodesAndCredentials.mockImplementation(() => { });
return {
removeNpmModule: removeNpmModuleMock, test('PATCH /nodes should update a package', async () => {
}; const ownerShell = await testDb.createUserShell(globalOwnerRole);
const updateSpy = jest
.spyOn(LoadNodesAndCredentials(), 'updateNpmModule')
.mockImplementationOnce(mockedEmptyPackage);
mocked(findInstalledPackage).mockImplementationOnce(mockedEmptyPackage);
await authAgent(ownerShell).patch('/nodes').send({
name: utils.installedPackagePayload().packageName,
}); });
const response = await authOwnerAgent.delete('/nodes').send(requestBody); expect(updateSpy).toHaveBeenCalledTimes(1);
expect(response.statusCode).toBe(200);
expect(removeNpmModuleMock).toHaveBeenCalledTimes(1);
}); });
// TEST PATCH ENDPOINT
test('PATCH /nodes package name should not be empty', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const response = await authOwnerAgent.patch('/nodes').send();
expect(response.statusCode).toBe(400);
});
test('PATCH /nodes Should return error when package was not installed', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const requestBody = {
name: installedPackagePayload().packageName,
};
const response = await authOwnerAgent.patch('/nodes').send(requestBody);
expect(response.status).toBe(400);
expect(response.body.message).toContain('not installed');
});
test('PATCH /nodes package should be updated if all conditions are true', async () => {
const authOwnerAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const requestBody = {
name: installedPackagePayload().packageName,
};
// @ts-ignore
searchInstalledPackage.mockImplementation(() => {
return {
installedNodes: [],
};
});
const updatedNpmModuleMock = jest.fn(() => ({
installedNodes: [],
}));
// @ts-ignore
LoadNodesAndCredentials.mockImplementation(() => {
return {
updateNpmModule: updatedNpmModuleMock,
};
});
const response = await authOwnerAgent.patch('/nodes').send(requestBody);
expect(updatedNpmModuleMock).toHaveBeenCalledTimes(1);
});
async function saveMockPackage(payload: InstalledPackagePayload) {
return await testDb.saveInstalledPackage(payload);
}
async function saveMockNode(payload: InstalledNodePayload) {
return await testDb.saveInstalledNode(payload);
}
function getNpmOutdatedError(packageName: string) {
const errorOutput = new Error('Something went wrong');
// @ts-ignore
errorOutput.code = 1;
// @ts-ignore
errorOutput.stdout = '{' +
`"${packageName}": {` +
`"current": "${CURRENT_PACKAGE_VERSION}",` +
`"wanted": "${CURRENT_PACKAGE_VERSION}",` +
`"latest": "${UPDATED_PACKAGE_VERSION}",` +
`"location": "node_modules/${packageName}"` +
'}' +
'}';
return errorOutput;
}

View file

@ -1,4 +1,6 @@
import type { ICredentialDataDecryptedObject, ICredentialNodeAccess } from 'n8n-workflow'; import type { ICredentialDataDecryptedObject, ICredentialNodeAccess } from 'n8n-workflow';
import type { SuperAgentTest } from 'supertest';
import type { ICredentialsDb, IDatabaseCollections } from '../../../src'; import type { ICredentialsDb, IDatabaseCollections } from '../../../src';
import type { CredentialsEntity } from '../../../src/databases/entities/CredentialsEntity'; import type { CredentialsEntity } from '../../../src/databases/entities/CredentialsEntity';
import type { User } from '../../../src/databases/entities/User'; import type { User } from '../../../src/databases/entities/User';
@ -10,6 +12,8 @@ export type MappingName = keyof typeof MAPPING_TABLES;
export type ApiPath = 'internal' | 'public'; export type ApiPath = 'internal' | 'public';
export type AuthAgent = (user: User) => SuperAgentTest;
type EndpointGroup = type EndpointGroup =
| 'me' | 'me'
| 'users' | 'users'
@ -49,12 +53,11 @@ export interface TriggerTime {
export type InstalledPackagePayload = { export type InstalledPackagePayload = {
packageName: string; packageName: string;
installedVersion: string; installedVersion: string;
} };
export type InstalledNodePayload = { export type InstalledNodePayload = {
name: string; name: string;
type: string; type: string;
latestVersion: string; latestVersion: string;
package: string; package: string;
} };

View file

@ -23,7 +23,12 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
import config from '../../../config'; import config from '../../../config';
import { AUTHLESS_ENDPOINTS, CURRENT_PACKAGE_VERSION, PUBLIC_API_REST_PATH_SEGMENT, REST_PATH_SEGMENT } from './constants'; import {
AUTHLESS_ENDPOINTS,
CURRENT_PACKAGE_VERSION,
PUBLIC_API_REST_PATH_SEGMENT,
REST_PATH_SEGMENT,
} from './constants';
import { AUTH_COOKIE_NAME, NODE_PACKAGE_PREFIX } from '../../../src/constants'; import { AUTH_COOKIE_NAME, NODE_PACKAGE_PREFIX } from '../../../src/constants';
import { addRoutes as authMiddleware } from '../../../src/UserManagement/routes'; import { addRoutes as authMiddleware } from '../../../src/UserManagement/routes';
import { import {
@ -56,6 +61,7 @@ import type { N8nApp } from '../../../src/UserManagement/Interfaces';
import { workflowsController } from '../../../src/api/workflows.api'; import { workflowsController } from '../../../src/api/workflows.api';
import { nodesController } from '../../../src/api/nodes.api'; import { nodesController } from '../../../src/api/nodes.api';
import { randomName } from './random'; import { randomName } from './random';
import { InstalledPackages } from '../../../src/databases/entities/InstalledPackages';
/** /**
* Initialize a test server. * Initialize a test server.
@ -101,7 +107,7 @@ export async function initTestServer({
credentials: { controller: credentialsController, path: 'credentials' }, credentials: { controller: credentialsController, path: 'credentials' },
workflows: { controller: workflowsController, path: 'workflows' }, workflows: { controller: workflowsController, path: 'workflows' },
nodes: { controller: nodesController, path: 'nodes' }, nodes: { controller: nodesController, path: 'nodes' },
publicApi: apiRouters publicApi: apiRouters,
}; };
for (const group of routerEndpoints) { for (const group of routerEndpoints) {
@ -139,6 +145,9 @@ export function initTestTelemetry() {
void InternalHooksManager.init('test-instance-id', 'test-version', mockNodeTypes); void InternalHooksManager.init('test-instance-id', 'test-version', mockNodeTypes);
} }
export const createAuthAgent = (app: express.Application) => (user: User) =>
createAgent(app, { auth: true, user });
/** /**
* Classify endpoint groups into `routerEndpoints` (newest, using `express.Router`), * Classify endpoint groups into `routerEndpoints` (newest, using `express.Router`),
* and `functionEndpoints` (legacy, namespaced inside a function). * and `functionEndpoints` (legacy, namespaced inside a function).
@ -893,7 +902,7 @@ export function getPostgresSchemaSection(
} }
// ---------------------------------- // ----------------------------------
// nodes // community nodes
// ---------------------------------- // ----------------------------------
export function installedPackagePayload(): InstalledPackagePayload { export function installedPackagePayload(): InstalledPackagePayload {
@ -912,3 +921,11 @@ export function installedNodePayload(packageName: string): InstalledNodePayload
package: packageName, package: packageName,
}; };
} }
export const emptyPackage = () => {
const installedPackage = new InstalledPackages();
installedPackage.installedNodes = [];
return Promise.resolve(installedPackage);
};

View file

@ -1,330 +1,343 @@
import { checkPackageStatus, matchPackagesWithUpdates, executeCommand, parsePackageName, matchMissingPackages, hasPackageLoadedSuccessfully, removePackageFromMissingList } from '../../src/CommunityNodes/helpers'; import { exec } from 'child_process';
import { NODE_PACKAGE_PREFIX, NPM_COMMAND_TOKENS, NPM_PACKAGE_STATUS_GOOD, RESPONSE_ERROR_MESSAGES } from '../../src/constants';
jest.mock('fs/promises');
import { access as fsAccess, mkdir as fsMkdir } from 'fs/promises'; import { access as fsAccess, mkdir as fsMkdir } from 'fs/promises';
jest.mock('child_process');
import { exec } from 'child_process';
import { InstalledPackages } from '../../src/databases/entities/InstalledPackages';
import { installedNodePayload, installedPackagePayload } from '../integration/shared/utils';
import { InstalledNodes } from '../../src/databases/entities/InstalledNodes';
import { NpmUpdatesAvailable } from '../../src/Interfaces';
import { randomName } from '../integration/shared/random';
import config from '../../config';
jest.mock('axios');
import axios from 'axios'; import axios from 'axios';
describe('CommunityNodesHelper', () => { import {
checkNpmPackageStatus,
matchPackagesWithUpdates,
executeCommand,
parseNpmPackageName,
matchMissingPackages,
hasPackageLoaded,
removePackageFromMissingList,
} from '../../src/CommunityNodes/helpers';
import {
NODE_PACKAGE_PREFIX,
NPM_COMMAND_TOKENS,
NPM_PACKAGE_STATUS_GOOD,
RESPONSE_ERROR_MESSAGES,
} from '../../src/constants';
import { InstalledPackages } from '../../src/databases/entities/InstalledPackages';
import { InstalledNodes } from '../../src/databases/entities/InstalledNodes';
import { randomName } from '../integration/shared/random';
import config from '../../config';
import { installedPackagePayload, installedNodePayload } from '../integration/shared/utils';
describe('parsePackageName', () => { import type { CommunityPackages } from '../../src/Interfaces';
it('Should fail with empty package name', () => {
expect(() => parsePackageName('')).toThrowError()
});
it('Should fail with invalid package prefix name', () => { jest.mock('fs/promises');
expect(() => parsePackageName('INVALID_PREFIX@123')).toThrowError() jest.mock('child_process');
}); jest.mock('axios');
it('Should parse valid package name', () => { describe('parsePackageName', () => {
const validPackageName = NODE_PACKAGE_PREFIX + 'cool-package-name'; test('Should fail with empty package name', () => {
const parsedPackageName = parsePackageName(validPackageName); expect(() => parseNpmPackageName('')).toThrowError();
expect(parsedPackageName.originalString).toBe(validPackageName);
expect(parsedPackageName.packageName).toBe(validPackageName);
expect(parsedPackageName.scope).toBeUndefined();
expect(parsedPackageName.version).toBeUndefined();
});
it('Should parse valid package name and version', () => {
const validPackageName = NODE_PACKAGE_PREFIX + 'cool-package-name';
const validPackageVersion = '0.1.1';
const fullPackageName = `${validPackageName}@${validPackageVersion}`;
const parsedPackageName = parsePackageName(fullPackageName);
expect(parsedPackageName.originalString).toBe(fullPackageName);
expect(parsedPackageName.packageName).toBe(validPackageName);
expect(parsedPackageName.scope).toBeUndefined();
expect(parsedPackageName.version).toBe(validPackageVersion);
});
it('Should parse valid package name, scope and version', () => {
const validPackageScope = '@n8n';
const validPackageName = NODE_PACKAGE_PREFIX + 'cool-package-name';
const validPackageVersion = '0.1.1';
const fullPackageName = `${validPackageScope}/${validPackageName}@${validPackageVersion}`;
const parsedPackageName = parsePackageName(fullPackageName);
expect(parsedPackageName.originalString).toBe(fullPackageName);
expect(parsedPackageName.packageName).toBe(`${validPackageScope}/${validPackageName}`);
expect(parsedPackageName.scope).toBe(validPackageScope);
expect(parsedPackageName.version).toBe(validPackageVersion);
});
}); });
describe('executeCommand', () => { test('Should fail with invalid package prefix name', () => {
expect(() => parseNpmPackageName('INVALID_PREFIX@123')).toThrowError();
beforeEach(() => {
// @ts-ignore
fsAccess.mockReset();
// @ts-ignore
fsMkdir.mockReset();
// @ts-ignore
exec.mockReset();
});
it('Should call command with valid options', async () => {
// @ts-ignore
exec.mockImplementation((...args) => {
expect(args[1].cwd).toBeDefined();
expect(args[1].env).toBeDefined();
// PATH or NODE_PATH may be undefined depending on environment so we don't check for these keys.
const callbackFunction = args[args.length - 1];
callbackFunction(null, { stdout: 'Done' });
});
await executeCommand('ls');
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toBeCalledTimes(0);
});
it ('Should make sure folder exists', async () => {
// @ts-ignore
exec.mockImplementation((...args) => {
const callbackFunction = args[args.length - 1];
callbackFunction(null, { stdout: 'Done' });
});
await executeCommand('ls');
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toBeCalledTimes(0);
});
it ('Should try to create folder if it does not exist', async () => {
// @ts-ignore
exec.mockImplementation((...args) => {
const callbackFunction = args[args.length - 1];
callbackFunction(null, { stdout: 'Done' });
});
// @ts-ignore
fsAccess.mockImplementation(() => {
throw new Error('Folder does not exist.');
});
await executeCommand('ls');
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toHaveBeenCalled();
});
it('Should throw especial error when package is not found', async() => {
// @ts-ignore
exec.mockImplementation((...args) => {
const callbackFunction = args[args.length - 1];
callbackFunction(new Error('Something went wrong - ' + NPM_COMMAND_TOKENS.NPM_PACKAGE_NOT_FOUND_ERROR + '. Aborting.'));
});
await expect(async () => await executeCommand('ls')).rejects.toThrow(RESPONSE_ERROR_MESSAGES.PACKAGE_NOT_FOUND);
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toHaveBeenCalledTimes(0);
});
}); });
test('Should parse valid package name', () => {
const validPackageName = NODE_PACKAGE_PREFIX + 'cool-package-name';
const parsed = parseNpmPackageName(validPackageName);
describe('crossInformationPackage', () => { expect(parsed.rawString).toBe(validPackageName);
expect(parsed.packageName).toBe(validPackageName);
it('Should return same list if availableUpdates is undefined', () => { expect(parsed.scope).toBeUndefined();
const fakePackages = generateListOfFakeInstalledPackages(); expect(parsed.version).toBeUndefined();
const crossedData = matchPackagesWithUpdates(fakePackages);
expect(crossedData).toEqual(fakePackages);
});
it ('Should correctly match update versions for packages', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const updates: NpmUpdatesAvailable = {
[fakePackages[0].packageName]: {
current: fakePackages[0].installedVersion,
wanted: fakePackages[0].installedVersion,
latest: '0.2.0',
location: fakePackages[0].packageName,
},
[fakePackages[1].packageName]: {
current: fakePackages[0].installedVersion,
wanted: fakePackages[0].installedVersion,
latest: '0.3.0',
location: fakePackages[0].packageName,
}
};
const crossedData = matchPackagesWithUpdates(fakePackages, updates);
// @ts-ignore
expect(crossedData[0].updateAvailable).toBe('0.2.0');
// @ts-ignore
expect(crossedData[1].updateAvailable).toBe('0.3.0');
});
it ('Should correctly match update versions for single package', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const updates: NpmUpdatesAvailable = {
[fakePackages[1].packageName]: {
current: fakePackages[0].installedVersion,
wanted: fakePackages[0].installedVersion,
latest: '0.3.0',
location: fakePackages[0].packageName,
}
};
const crossedData = matchPackagesWithUpdates(fakePackages, updates);
// @ts-ignore
expect(crossedData[0].updateAvailable).toBeUndefined();
// @ts-ignore
expect(crossedData[1].updateAvailable).toBe('0.3.0');
});
}); });
describe('matchMissingPackages', () => { test('Should parse valid package name and version', () => {
it('Should not match failed packages that do not exist', () => { const validPackageName = NODE_PACKAGE_PREFIX + 'cool-package-name';
const fakePackages = generateListOfFakeInstalledPackages(); const validPackageVersion = '0.1.1';
const notFoundPackageList = `${NODE_PACKAGE_PREFIX}very-long-name-that-should-never-be-generated@1.0.0 ${NODE_PACKAGE_PREFIX}another-very-long-name-that-never-is-seen`; const fullPackageName = `${validPackageName}@${validPackageVersion}`;
const matchedPackages = matchMissingPackages(fakePackages, notFoundPackageList); const parsed = parseNpmPackageName(fullPackageName);
expect(matchedPackages).toEqual(fakePackages); expect(parsed.rawString).toBe(fullPackageName);
expect(matchedPackages[0].failedLoading).toBeUndefined(); expect(parsed.packageName).toBe(validPackageName);
expect(matchedPackages[1].failedLoading).toBeUndefined(); expect(parsed.scope).toBeUndefined();
}); expect(parsed.version).toBe(validPackageVersion);
it('Should match failed packages that should be present', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const notFoundPackageList = `${NODE_PACKAGE_PREFIX}very-long-name-that-should-never-be-generated@1.0.0 ${fakePackages[0].packageName}@${fakePackages[0].installedVersion}`;
const matchedPackages = matchMissingPackages(fakePackages, notFoundPackageList);
expect(matchedPackages[0].failedLoading).toBe(true);
expect(matchedPackages[1].failedLoading).toBeUndefined();
});
it('Should match failed packages even if version is wrong', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const notFoundPackageList = `${NODE_PACKAGE_PREFIX}very-long-name-that-should-never-be-generated@1.0.0 ${fakePackages[0].packageName}@123.456.789`;
const matchedPackages = matchMissingPackages(fakePackages, notFoundPackageList);
expect(matchedPackages[0].failedLoading).toBe(true);
expect(matchedPackages[1].failedLoading).toBeUndefined();
});
}); });
describe('checkPackageStatus', () => { test('Should parse valid package name, scope and version', () => {
it('Should call axios.post', async () => { const validPackageScope = '@n8n';
const packageName = NODE_PACKAGE_PREFIX + randomName(); const validPackageName = NODE_PACKAGE_PREFIX + 'cool-package-name';
await checkPackageStatus(packageName); const validPackageVersion = '0.1.1';
expect(axios.post).toHaveBeenCalled(); const fullPackageName = `${validPackageScope}/${validPackageName}@${validPackageVersion}`;
}); const parsed = parseNpmPackageName(fullPackageName);
it('Should not fail if request fails', async () => { expect(parsed.rawString).toBe(fullPackageName);
const packageName = NODE_PACKAGE_PREFIX + randomName(); expect(parsed.packageName).toBe(`${validPackageScope}/${validPackageName}`);
axios.post = jest.fn(() => { expect(parsed.scope).toBe(validPackageScope);
throw new Error('Something went wrong'); expect(parsed.version).toBe(validPackageVersion);
}); });
const result = await checkPackageStatus(packageName); });
expect(result.status).toBe(NPM_PACKAGE_STATUS_GOOD);
});
it('Should warn if package is banned', async () => { describe('executeCommand', () => {
const packageName = NODE_PACKAGE_PREFIX + randomName(); beforeEach(() => {
// @ts-ignore // @ts-ignore
axios.post = jest.fn(() => { fsAccess.mockReset();
return { data: { status: 'Banned', reason: 'Not good' } }; // @ts-ignore
}); fsMkdir.mockReset();
const result = await checkPackageStatus(packageName); // @ts-ignore
expect(result.status).toBe('Banned'); exec.mockReset();
expect(result.reason).toBe('Not good');
});
}); });
describe('hasPackageLoadedSuccessfully', () => { test('Should call command with valid options', async () => {
it('Should return true when failed package list does not exist', () => { // @ts-ignore
config.set('nodes.packagesMissing', undefined); exec.mockImplementation((...args) => {
const result = hasPackageLoadedSuccessfully('package'); expect(args[1].cwd).toBeDefined();
expect(result).toBe(true); expect(args[1].env).toBeDefined();
// PATH or NODE_PATH may be undefined depending on environment so we don't check for these keys.
const callbackFunction = args[args.length - 1];
callbackFunction(null, { stdout: 'Done' });
}); });
it('Should return true when package is not in the list of missing packages', () => { await executeCommand('ls');
config.set('nodes.packagesMissing', 'packageA@0.1.0 packgeB@0.1.0'); expect(fsAccess).toHaveBeenCalled();
const result = hasPackageLoadedSuccessfully('packageC'); expect(exec).toHaveBeenCalled();
expect(result).toBe(true); expect(fsMkdir).toBeCalledTimes(0);
});
it('Should return false when package is in the list of missing packages', () => {
config.set('nodes.packagesMissing', 'packageA@0.1.0 packgeB@0.1.0');
const result = hasPackageLoadedSuccessfully('packageA');
expect(result).toBe(false);
});
}); });
describe('removePackageFromMissingList', () => { test('Should make sure folder exists', async () => {
it('Should do nothing if key does not exist', () => { // @ts-ignore
config.set('nodes.packagesMissing', undefined); exec.mockImplementation((...args) => {
const callbackFunction = args[args.length - 1];
removePackageFromMissingList('packageA'); callbackFunction(null, { stdout: 'Done' });
const packageList = config.get('nodes.packagesMissing');
expect(packageList).toBeUndefined();
}); });
it('Should remove only correct package from list', () => { await executeCommand('ls');
config.set('nodes.packagesMissing', 'packageA@0.1.0 packageB@0.2.0 packageBB@0.2.0'); expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toBeCalledTimes(0);
});
removePackageFromMissingList('packageB'); test('Should try to create folder if it does not exist', async () => {
// @ts-ignore
const packageList = config.get('nodes.packagesMissing'); exec.mockImplementation((...args) => {
expect(packageList).toBe('packageA@0.1.0 packageBB@0.2.0'); const callbackFunction = args[args.length - 1];
callbackFunction(null, { stdout: 'Done' });
}); });
// @ts-ignore
it('Should not remove if package is not in the list', () => { fsAccess.mockImplementation(() => {
const failedToLoadList = 'packageA@0.1.0 packageB@0.2.0 packageBB@0.2.0'; throw new Error('Folder does not exist.');
config.set('nodes.packagesMissing', failedToLoadList);
removePackageFromMissingList('packageC');
const packageList = config.get('nodes.packagesMissing');
expect(packageList).toBe(failedToLoadList);
}); });
await executeCommand('ls');
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toHaveBeenCalled();
});
test('Should throw especial error when package is not found', async () => {
// @ts-ignore
exec.mockImplementation((...args) => {
const callbackFunction = args[args.length - 1];
callbackFunction(
new Error(
'Something went wrong - ' +
NPM_COMMAND_TOKENS.NPM_PACKAGE_NOT_FOUND_ERROR +
'. Aborting.',
),
);
});
await expect(async () => await executeCommand('ls')).rejects.toThrow(
RESPONSE_ERROR_MESSAGES.PACKAGE_NOT_FOUND,
);
expect(fsAccess).toHaveBeenCalled();
expect(exec).toHaveBeenCalled();
expect(fsMkdir).toHaveBeenCalledTimes(0);
});
});
describe('crossInformationPackage', () => {
test('Should return same list if availableUpdates is undefined', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const crossedData = matchPackagesWithUpdates(fakePackages);
expect(crossedData).toEqual(fakePackages);
});
test('Should correctly match update versions for packages', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const updates: CommunityPackages.AvailableUpdates = {
[fakePackages[0].packageName]: {
current: fakePackages[0].installedVersion,
wanted: fakePackages[0].installedVersion,
latest: '0.2.0',
location: fakePackages[0].packageName,
},
[fakePackages[1].packageName]: {
current: fakePackages[0].installedVersion,
wanted: fakePackages[0].installedVersion,
latest: '0.3.0',
location: fakePackages[0].packageName,
},
};
const crossedData = matchPackagesWithUpdates(fakePackages, updates);
// @ts-ignore
expect(crossedData[0].updateAvailable).toBe('0.2.0');
// @ts-ignore
expect(crossedData[1].updateAvailable).toBe('0.3.0');
});
test('Should correctly match update versions for single package', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const updates: CommunityPackages.AvailableUpdates = {
[fakePackages[1].packageName]: {
current: fakePackages[0].installedVersion,
wanted: fakePackages[0].installedVersion,
latest: '0.3.0',
location: fakePackages[0].packageName,
},
};
const crossedData = matchPackagesWithUpdates(fakePackages, updates);
// @ts-ignore
expect(crossedData[0].updateAvailable).toBeUndefined();
// @ts-ignore
expect(crossedData[1].updateAvailable).toBe('0.3.0');
});
});
describe('matchMissingPackages', () => {
test('Should not match failed packages that do not exist', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const notFoundPackageList = `${NODE_PACKAGE_PREFIX}very-long-name-that-should-never-be-generated@1.0.0 ${NODE_PACKAGE_PREFIX}another-very-long-name-that-never-is-seen`;
const matchedPackages = matchMissingPackages(fakePackages, notFoundPackageList);
expect(matchedPackages).toEqual(fakePackages);
expect(matchedPackages[0].failedLoading).toBeUndefined();
expect(matchedPackages[1].failedLoading).toBeUndefined();
});
test('Should match failed packages that should be present', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const notFoundPackageList = `${NODE_PACKAGE_PREFIX}very-long-name-that-should-never-be-generated@1.0.0 ${fakePackages[0].packageName}@${fakePackages[0].installedVersion}`;
const matchedPackages = matchMissingPackages(fakePackages, notFoundPackageList);
expect(matchedPackages[0].failedLoading).toBe(true);
expect(matchedPackages[1].failedLoading).toBeUndefined();
});
test('Should match failed packages even if version is wrong', () => {
const fakePackages = generateListOfFakeInstalledPackages();
const notFoundPackageList = `${NODE_PACKAGE_PREFIX}very-long-name-that-should-never-be-generated@1.0.0 ${fakePackages[0].packageName}@123.456.789`;
const matchedPackages = matchMissingPackages(fakePackages, notFoundPackageList);
expect(matchedPackages[0].failedLoading).toBe(true);
expect(matchedPackages[1].failedLoading).toBeUndefined();
});
});
describe('checkNpmPackageStatus', () => {
test('Should call axios.post', async () => {
const packageName = NODE_PACKAGE_PREFIX + randomName();
await checkNpmPackageStatus(packageName);
expect(axios.post).toHaveBeenCalled();
});
test('Should not fail if request fails', async () => {
const packageName = NODE_PACKAGE_PREFIX + randomName();
axios.post = jest.fn(() => {
throw new Error('Something went wrong');
});
const result = await checkNpmPackageStatus(packageName);
expect(result.status).toBe(NPM_PACKAGE_STATUS_GOOD);
});
test('Should warn if package is banned', async () => {
const packageName = NODE_PACKAGE_PREFIX + randomName();
// @ts-ignore
axios.post = jest.fn(() => {
return { data: { status: 'Banned', reason: 'Not good' } };
});
const result = await checkNpmPackageStatus(packageName);
expect(result.status).toBe('Banned');
expect(result.reason).toBe('Not good');
});
});
describe('hasPackageLoadedSuccessfully', () => {
test('Should return true when failed package list does not exist', () => {
config.set('nodes.packagesMissing', undefined);
const result = hasPackageLoaded('package');
expect(result).toBe(true);
});
test('Should return true when package is not in the list of missing packages', () => {
config.set('nodes.packagesMissing', 'packageA@0.1.0 packgeB@0.1.0');
const result = hasPackageLoaded('packageC');
expect(result).toBe(true);
});
test('Should return false when package is in the list of missing packages', () => {
config.set('nodes.packagesMissing', 'packageA@0.1.0 packgeB@0.1.0');
const result = hasPackageLoaded('packageA');
expect(result).toBe(false);
});
});
describe('removePackageFromMissingList', () => {
test('Should do nothing if key does not exist', () => {
config.set('nodes.packagesMissing', undefined);
removePackageFromMissingList('packageA');
const packageList = config.get('nodes.packagesMissing');
expect(packageList).toBeUndefined();
});
test('Should remove only correct package from list', () => {
config.set('nodes.packagesMissing', 'packageA@0.1.0 packageB@0.2.0 packageBB@0.2.0');
removePackageFromMissingList('packageB');
const packageList = config.get('nodes.packagesMissing');
expect(packageList).toBe('packageA@0.1.0 packageBB@0.2.0');
});
test('Should not remove if package is not in the list', () => {
const failedToLoadList = 'packageA@0.1.0 packageB@0.2.0 packageBB@0.2.0';
config.set('nodes.packagesMissing', failedToLoadList);
removePackageFromMissingList('packageC');
const packageList = config.get('nodes.packagesMissing');
expect(packageList).toBe(failedToLoadList);
}); });
}); });
/** /**
* Generates a list with 2 packages, one with a single node and * Generate a list with 2 packages, one with a single node and another with 2 nodes
* another with 2 nodes
* @returns
*/ */
function generateListOfFakeInstalledPackages(): InstalledPackages[] { function generateListOfFakeInstalledPackages(): InstalledPackages[] {
const fakeInstalledPackage1 = new InstalledPackages(); const fakeInstalledPackage1 = new InstalledPackages();
Object.assign(fakeInstalledPackage1, installedPackagePayload()); Object.assign(fakeInstalledPackage1, installedPackagePayload());
const fakeInstalledNode1 = new InstalledNodes(); const fakeInstalledNode1 = new InstalledNodes();
Object.assign(fakeInstalledNode1, installedNodePayload(fakeInstalledPackage1.packageName)); Object.assign(fakeInstalledNode1, installedNodePayload(fakeInstalledPackage1.packageName));
fakeInstalledPackage1.installedNodes = [fakeInstalledNode1]; fakeInstalledPackage1.installedNodes = [fakeInstalledNode1];
const fakeInstalledPackage2 = new InstalledPackages(); const fakeInstalledPackage2 = new InstalledPackages();
Object.assign(fakeInstalledPackage2, installedPackagePayload()); Object.assign(fakeInstalledPackage2, installedPackagePayload());
const fakeInstalledNode2 = new InstalledNodes(); const fakeInstalledNode2 = new InstalledNodes();
Object.assign(fakeInstalledNode2, installedNodePayload(fakeInstalledPackage2.packageName)); Object.assign(fakeInstalledNode2, installedNodePayload(fakeInstalledPackage2.packageName));
const fakeInstalledNode3 = new InstalledNodes(); const fakeInstalledNode3 = new InstalledNodes();
Object.assign(fakeInstalledNode3, installedNodePayload(fakeInstalledPackage2.packageName)); Object.assign(fakeInstalledNode3, installedNodePayload(fakeInstalledPackage2.packageName));
fakeInstalledPackage2.installedNodes = [fakeInstalledNode2, fakeInstalledNode3]; fakeInstalledPackage2.installedNodes = [fakeInstalledNode2, fakeInstalledNode3];
return [fakeInstalledPackage1, fakeInstalledPackage2]; return [fakeInstalledPackage1, fakeInstalledPackage2];