UNPKG

@ones-op/node-host

Version:
121 lines (120 loc) 6.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.pluginConfig = exports.platformTimeOutMillis = exports.platformPluginAbilityFetchUrl = exports.isTcpPlatformAddress = exports.isHttpPlatformAddress = exports.reWriteYamlLocal = exports.config = exports.serveModeStandalone = exports.serveModeSaas = void 0; const fs_1 = __importDefault(require("fs")); const minimist_1 = __importDefault(require("minimist")); const path_1 = __importDefault(require("path")); const yaml_1 = __importDefault(require("yaml")); const level_1 = require("./logger/level"); const utils_1 = require("./utils"); /* command params */ const argv = (0, minimist_1.default)(process.argv.slice(2)); const configPath = getAbsolutePath(argv.conf_path || 'config.yaml'); exports.serveModeSaas = 'saas'; exports.serveModeStandalone = 'standalone'; /* config.yml */ /* developer's local.yaml*/ const localConfig = yaml_1.default.parse(fs_1.default.readFileSync(configPath, 'utf8')); let log_level = argv.log_level || localConfig.local?.log_level || level_1.DEFAULT_LEVEL; if (!Object.keys(level_1.LEVELS).includes(log_level)) { console.error(`Illegal log_level type: ${log_level}`); log_level = level_1.DEFAULT_LEVEL; } exports.config = { log_level, plugin: { backend_node_modules: `./backend/node_modules`, backend_index: argv.plugin_backend_index || './backend/dist/index.js', workspace_path: getAbsolutePath(argv.plugin_workspace_path || localConfig.plugin?.workspace_path || './workspace'), app_id: localConfig.plugin?.app_id || argv.app_id || 'annoymous_appid', app_name: localConfig.plugin?.app_name || '', app_version: localConfig.plugin?.app_version || '1.0.0', instance_id: localConfig.plugin?.instance_id || localConfig.local?.instance_uuid || 'annoymous_instanceid', organization_uuid: localConfig.plugin?.organization_uuid || localConfig.local?.organization_uuid || '', team_uuid: localConfig.plugin?.team_uuid || localConfig.local?.team_uuid || '', path_to_plugin: argv.path_to_plugin || localConfig.plugin?.runtime_path || '/data/plugin/upload', scope: localConfig.plugin?.scope || 0, scope_uuid: localConfig.plugin?.scope_uuid || '', }, platform: { address: argv.platform_address || localConfig.platform.address, report_log_interval: localConfig.platform.report_log_interval < 0 ? 0 : 3, baseURL: argv.base_url || localConfig.platform.baseURL, }, host: { id: argv.host_id || localConfig.local?.id || localConfig.runtime?.id || '', name: argv.host_name || localConfig.local?.name || '', version: argv.host_version || localConfig.local?.version || '', timeout_sec: argv.host_timeout_sec || localConfig.runtime?.timeout_sec || localConfig.local?.timeout_sec || 60, listen_port: argv.host_http_port || localConfig.runtime?.http_port || 80, debug_mode: argv.host_debug_mode || localConfig.local?.debug_mode || false, serve_mode: argv.host_serve_mode || localConfig.runtime?.serve_mode || exports.serveModeStandalone, language: argv.host_language || localConfig.local?.language || 'nodejs', language_version: argv.host_languageversion || localConfig.local?.languageversion || '16.13.1', min_system_version: argv.host_min_system_version || localConfig.local?.min_system_version || '3.5.0', sub_version: argv.host_sub_version || localConfig.local?.sub_version || '1.0.0', log_file_path: getAbsolutePath(argv.log_file_path || './ones-plugin-node-host.log'), openapi_service_address: argv.openapi_service_address || localConfig.runtime?.openapi_service_address || 'http://open-api-service:80', otlp_export_grpc_addr: argv.otlp_export_grpc_addr || localConfig.runtime?.otlp_export_grpc_addr || '', }, // cli config cli: { debug_mode: argv.tags === 'local', }, local: { ...localConfig.local, web_service_ip: localConfig.local?.web_service_ip || '127.0.0.1', }, }; /** * pkg 打包,如果需要动态引入外部文件,需要使用 process.cwd() 来获取当前工作目录 * 并且需要避免使用 __dirname,因为 __dirname 会导致文件被打包进二进制文件中 * https://github.com/vercel/pkg#snapshot-filesystem */ function getAbsolutePath(p) { return path_1.default.isAbsolute(p) ? p : path_1.default.join(process.cwd(), p); } const reWriteYamlLocal = (localConfig) => { const doc = yaml_1.default.parseDocument(fs_1.default.readFileSync(configPath, 'utf8')); doc.set('local', { ...exports.config.local, ...localConfig, }); return fs_1.default.writeFileSync(configPath, doc.toString()); }; exports.reWriteYamlLocal = reWriteYamlLocal; // 目前支持的 node-host 访问平台的地址类型 var platformAddressTypeEnum; (function (platformAddressTypeEnum) { platformAddressTypeEnum[platformAddressTypeEnum["Tcp"] = 0] = "Tcp"; platformAddressTypeEnum[platformAddressTypeEnum["Http"] = 1] = "Http"; })(platformAddressTypeEnum || (platformAddressTypeEnum = {})); const getPlatformAddressType = () => { if (exports.config.platform.address.startsWith('http')) { return platformAddressTypeEnum.Http; } return platformAddressTypeEnum.Tcp; }; const platformAddressType = getPlatformAddressType(); // 访问平台的地址类型是否 Http exports.isHttpPlatformAddress = platformAddressType === platformAddressTypeEnum.Http; // 访问平台的地址类型是否 Tcp exports.isTcpPlatformAddress = platformAddressType === platformAddressTypeEnum.Tcp; // 当前 node-host 访问平台能力的Url exports.platformPluginAbilityFetchUrl = exports.config.platform.address.replace(/\/+$/g, '') + '/plugin_ability_fetch'; const getPlatformTimeOutMillis = () => { return exports.config.host.timeout_sec * 1000; }; // 调用平台的超时时间,单位毫秒(ms) exports.platformTimeOutMillis = getPlatformTimeOutMillis(); const { code, path: packageDir } = (0, utils_1.getCodeInfo)(); const pluginYamlPath = path_1.default.join(packageDir, 'config/plugin.yaml'); exports.pluginConfig = yaml_1.default.parse(fs_1.default.readFileSync(pluginYamlPath, 'utf8'));