fix(core): Prevent backend from loading duplicate copies of nodes packages (#10979)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-09-26 20:28:57 +02:00
committed by GitHub
parent 1944b46fd4
commit 4584f22a9b
2 changed files with 24 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
import glob from 'fast-glob';
import { readFile } from 'fs/promises';
import { readFileSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import type {
CodexData,
DocumentationLink,
@@ -350,18 +351,11 @@ export class CustomDirectoryLoader extends DirectoryLoader {
* e.g. /nodes-base or community packages.
*/
export class PackageDirectoryLoader extends DirectoryLoader {
packageName = '';
packageJson: n8n.PackageJson = this.readJSONSync('package.json');
packageJson!: n8n.PackageJson;
async readPackageJson() {
this.packageJson = await this.readJSON('package.json');
this.packageName = this.packageJson.name;
}
packageName = this.packageJson.name;
override async loadAll() {
await this.readPackageJson();
const { n8n } = this.packageJson;
if (!n8n) return;
@@ -391,6 +385,17 @@ export class PackageDirectoryLoader extends DirectoryLoader {
});
}
protected readJSONSync<T>(file: string): T {
const filePath = this.resolvePath(file);
const fileString = readFileSync(filePath, 'utf8');
try {
return jsonParse<T>(fileString);
} catch (error) {
throw new ApplicationError('Failed to parse JSON', { extra: { filePath } });
}
}
protected async readJSON<T>(file: string): Promise<T> {
const filePath = this.resolvePath(file);
const fileString = await readFile(filePath, 'utf8');
@@ -408,8 +413,6 @@ export class PackageDirectoryLoader extends DirectoryLoader {
*/
export class LazyPackageDirectoryLoader extends PackageDirectoryLoader {
override async loadAll() {
await this.readPackageJson();
try {
const knownNodes: typeof this.known.nodes = await this.readJSON('dist/known/nodes.json');
for (const nodeName in knownNodes) {