perf: async read file (#3531)

This commit is contained in:
Archer
2025-01-05 16:57:35 +08:00
committed by archer
parent 2bf1fce32a
commit 2066094047
7 changed files with 61 additions and 53 deletions

View File

@@ -1,5 +1,5 @@
import { initHttpAgent } from '@fastgpt/service/common/middle/httpAgent';
import { existsSync, readdirSync, readFileSync } from 'fs';
import fs, { existsSync, readdirSync } from 'fs';
import type { FastGPTFeConfigsType } from '@fastgpt/global/common/system/types/index.d';
import type { FastGPTConfigFileType } from '@fastgpt/global/common/system/types/index.d';
import { PluginSourceEnum } from '@fastgpt/global/core/plugin/constants';
@@ -13,7 +13,7 @@ import { defaultGroup, defaultTemplateTypes } from '@fastgpt/web/core/workflow/c
import { MongoPluginGroups } from '@fastgpt/service/core/app/plugin/pluginGroupSchema';
import { MongoTemplateTypes } from '@fastgpt/service/core/app/templates/templateTypeSchema';
export const readConfigData = (name: string) => {
export const readConfigData = async (name: string) => {
const splitName = name.split('.');
const devName = `${splitName[0]}.local.${splitName[1]}`;
@@ -30,7 +30,7 @@ export const readConfigData = (name: string) => {
return `/app/data/${name}`;
})();
const content = readFileSync(filename, 'utf-8');
const content = await fs.promises.readFile(filename, 'utf-8');
return content;
};
@@ -120,13 +120,13 @@ export async function initSystemConfig() {
});
}
function getSystemVersion() {
async function getSystemVersion() {
if (global.systemVersion) return;
try {
if (process.env.NODE_ENV === 'development') {
global.systemVersion = process.env.npm_package_version || '0.0.0';
} else {
const packageJson = json5.parse(readFileSync('/app/package.json', 'utf-8'));
const packageJson = json5.parse(await fs.promises.readFile('/app/package.json', 'utf-8'));
global.systemVersion = packageJson?.version;
}
@@ -138,7 +138,7 @@ function getSystemVersion() {
}
}
function getSystemPlugin() {
async function getSystemPlugin() {
if (global.communityPlugins && global.communityPlugins.length > 0) return;
const basePath =
@@ -149,15 +149,17 @@ function getSystemPlugin() {
const filterFiles = files.filter((item) => item.endsWith('.json'));
// read json file
const fileTemplates = filterFiles.map<SystemPluginTemplateItemType>((filename) => {
const content = readFileSync(`${basePath}/${filename}`, 'utf-8');
return {
...json5.parse(content),
originCost: 0,
currentCost: 0,
id: `${PluginSourceEnum.community}-${filename.replace('.json', '')}`
};
});
const fileTemplates = await Promise.all(
filterFiles.map<Promise<SystemPluginTemplateItemType>>(async (filename) => {
const content = await fs.promises.readFile(`${basePath}/${filename}`, 'utf-8');
return {
...json5.parse(content),
originCost: 0,
currentCost: 0,
id: `${PluginSourceEnum.community}-${filename.replace('.json', '')}`
};
})
);
fileTemplates.sort((a, b) => (b.weight || 0) - (a.weight || 0));

View File

@@ -21,7 +21,9 @@ export const getSystemPlugins = async (refresh = false) => {
global.systemPlugins = [];
}
global.systemPlugins = FastGPTProUrl ? await getCommercialPlugins() : getCommunityPlugins();
global.systemPlugins = FastGPTProUrl
? await getCommercialPlugins()
: await getCommunityPlugins();
addLog.info(`Load system plugin successfully: ${global.systemPlugins.length}`);