@h-ai/core 0.1.0-alpha.18 → 0.1.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/dist/browser.d.ts +19 -4
- package/dist/browser.js +68 -10
- package/dist/browser.js.map +1 -1
- package/dist/{chunk-RDAHW7NP.js → chunk-3E75CCDQ.js} +157 -5
- package/dist/chunk-3E75CCDQ.js.map +1 -0
- package/dist/{core-types-aT8pmvMq.d.ts → core-function-config.node-Baqu9RZ3.d.ts} +245 -3
- package/dist/node.d.ts +4 -205
- package/dist/node.js +16 -9
- package/dist/node.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-RDAHW7NP.js.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
1
|
+
import { z, ZodType } from 'zod';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @h-ai/core — 核心配置 Schema
|
|
@@ -465,7 +465,7 @@ declare const i18n: {
|
|
|
465
465
|
setGlobalLocale: typeof setGlobalLocale;
|
|
466
466
|
getGlobalLocale: typeof getGlobalLocale;
|
|
467
467
|
createMessageGetter: typeof createMessageGetter;
|
|
468
|
-
coreM: (key: "$schema" | "core_errorUnknown" | "core_errorNetwork" | "core_errorTimeout" | "core_errorNotFound" | "core_errorUnauthorized" | "core_errorForbidden" | "core_errorValidation" | "core_errorInternal" | "core_actionConfirm" | "core_actionCancel" | "core_actionSave" | "core_actionDelete" | "core_actionEdit" | "core_actionCreate" | "core_actionUpdate" | "core_actionSearch" | "core_actionReset" | "core_actionSubmit" | "core_actionLoading" | "core_actionRetry" | "core_statusSuccess" | "core_statusFailed" | "core_statusPending" | "core_statusProcessing" | "core_statusCompleted" | "core_statusCancelled" | "core_validationRequired" | "core_validationMinLength" | "core_validationMaxLength" | "core_validationEmail" | "core_validationUrl" | "core_validationNumber" | "core_validationInteger" | "core_validationPositive" | "core_validationPattern" | "core_timeJustNow" | "core_timeSecondsAgo" | "core_timeMinutesAgo" | "core_timeHoursAgo" | "core_timeDaysAgo" | "core_timeWeeksAgo" | "core_timeMonthsAgo" | "core_timeYearsAgo" | "core_browserFeatureUnsupported" | "core_configEnvVarMissing" | "core_configFileNotExist" | "core_configParseFailed" | "core_configValidationFailed" | "core_configNotLoaded", options?: MessageOptions) => string;
|
|
468
|
+
coreM: (key: "$schema" | "core_errorUnknown" | "core_errorNetwork" | "core_errorTimeout" | "core_errorNotFound" | "core_errorUnauthorized" | "core_errorForbidden" | "core_errorValidation" | "core_errorInternal" | "core_actionConfirm" | "core_actionCancel" | "core_actionSave" | "core_actionDelete" | "core_actionEdit" | "core_actionCreate" | "core_actionUpdate" | "core_actionSearch" | "core_actionReset" | "core_actionSubmit" | "core_actionLoading" | "core_actionRetry" | "core_statusSuccess" | "core_statusFailed" | "core_statusPending" | "core_statusProcessing" | "core_statusCompleted" | "core_statusCancelled" | "core_validationRequired" | "core_validationMinLength" | "core_validationMaxLength" | "core_validationEmail" | "core_validationUrl" | "core_validationNumber" | "core_validationInteger" | "core_validationPositive" | "core_validationPattern" | "core_timeJustNow" | "core_timeSecondsAgo" | "core_timeMinutesAgo" | "core_timeHoursAgo" | "core_timeDaysAgo" | "core_timeWeeksAgo" | "core_timeMonthsAgo" | "core_timeYearsAgo" | "core_browserFeatureUnsupported" | "core_configEnvVarMissing" | "core_configFileNotExist" | "core_configParseFailed" | "core_configValidationFailed" | "core_configNotLoaded" | "core_configUnsupportedInBrowser", options?: MessageOptions) => string;
|
|
469
469
|
};
|
|
470
470
|
/** i18n 子工具类型 */
|
|
471
471
|
type I18nFn = typeof i18n;
|
|
@@ -902,6 +902,45 @@ declare const object: {
|
|
|
902
902
|
/** object 子工具类型 */
|
|
903
903
|
type ObjectFn = typeof object;
|
|
904
904
|
|
|
905
|
+
/** 敏感字段匹配器 */
|
|
906
|
+
type SensitiveKeyMatcher = RegExp | string[] | ((key: string) => boolean);
|
|
907
|
+
/** 敏感字段脱敏选项 */
|
|
908
|
+
interface SanitizeSensitiveFieldsOptions {
|
|
909
|
+
/** 自定义敏感字段匹配规则 */
|
|
910
|
+
matcher?: SensitiveKeyMatcher;
|
|
911
|
+
/** 自定义脱敏替换值 */
|
|
912
|
+
replacement?: unknown;
|
|
913
|
+
}
|
|
914
|
+
/** 判断键名是否属于敏感字段 */
|
|
915
|
+
declare function isSensitiveKey(key: string, matcher?: SensitiveKeyMatcher): boolean;
|
|
916
|
+
/**
|
|
917
|
+
* 递归脱敏单个值。
|
|
918
|
+
*
|
|
919
|
+
* @param value - 原始值
|
|
920
|
+
* @param key - 当前字段名;根节点传 null
|
|
921
|
+
* @param options - 脱敏选项
|
|
922
|
+
* @param cache - 循环引用 / 重复引用缓存
|
|
923
|
+
*/
|
|
924
|
+
declare function sanitizeSensitiveValue(value: unknown, key: string | null, options?: SanitizeSensitiveFieldsOptions, cache?: WeakMap<object, unknown>): unknown;
|
|
925
|
+
/**
|
|
926
|
+
* 对对象中的敏感字段执行递归脱敏,并自动处理 URL/endpoint 字段中的内嵌凭证。
|
|
927
|
+
*
|
|
928
|
+
* @param value - 原始对象/数组/值
|
|
929
|
+
* @param options - 脱敏选项
|
|
930
|
+
* @returns 脱敏后的新值
|
|
931
|
+
*/
|
|
932
|
+
declare function sanitizeSensitiveFields<T>(value: T, options?: SanitizeSensitiveFieldsOptions): T;
|
|
933
|
+
/** 脱敏工具对象。 */
|
|
934
|
+
declare const sanitize: {
|
|
935
|
+
defaultSensitiveKeyRegex: RegExp;
|
|
936
|
+
defaultSensitiveReplacement: string;
|
|
937
|
+
isSensitiveKey: typeof isSensitiveKey;
|
|
938
|
+
sanitizeSensitiveValue: typeof sanitizeSensitiveValue;
|
|
939
|
+
sanitizeSensitiveFields: typeof sanitizeSensitiveFields;
|
|
940
|
+
};
|
|
941
|
+
/** sanitize 子工具类型 */
|
|
942
|
+
type SanitizeFn = typeof sanitize;
|
|
943
|
+
|
|
905
944
|
/**
|
|
906
945
|
* @h-ai/core — 字符串操作工具
|
|
907
946
|
* @module core-util-string
|
|
@@ -1690,6 +1729,8 @@ interface CoreFunctions {
|
|
|
1690
1729
|
readonly typeUtils: TypeUtilFn;
|
|
1691
1730
|
/** 对象操作工具 */
|
|
1692
1731
|
readonly object: ObjectFn;
|
|
1732
|
+
/** 脱敏工具 */
|
|
1733
|
+
readonly sanitize: SanitizeFn;
|
|
1693
1734
|
/** 字符串操作工具 */
|
|
1694
1735
|
readonly string: StringFn;
|
|
1695
1736
|
/** 数组操作工具 */
|
|
@@ -1704,4 +1745,205 @@ interface CoreFunctions {
|
|
|
1704
1745
|
readonly module: ModuleFn;
|
|
1705
1746
|
}
|
|
1706
1747
|
|
|
1707
|
-
|
|
1748
|
+
/**
|
|
1749
|
+
* @h-ai/core — 配置管理(Node.js 专用)
|
|
1750
|
+
*
|
|
1751
|
+
* 提供 YAML 配置文件加载、环境变量插值、缓存管理。
|
|
1752
|
+
* @module core-function-config.node
|
|
1753
|
+
*/
|
|
1754
|
+
|
|
1755
|
+
/**
|
|
1756
|
+
* 监听回调类型。
|
|
1757
|
+
*
|
|
1758
|
+
* @template T - 配置数据类型
|
|
1759
|
+
*
|
|
1760
|
+
* @example
|
|
1761
|
+
* ```ts
|
|
1762
|
+
* const callback: WatchCallback<AppConfig> = (cfg, error) => {
|
|
1763
|
+
* if (error) { core.logger.error('Config reload failed', { error }); return }
|
|
1764
|
+
* // 使用更新后的 cfg
|
|
1765
|
+
* }
|
|
1766
|
+
* ```
|
|
1767
|
+
*/
|
|
1768
|
+
type WatchCallback<T = unknown> = (config: T | null, error?: HaiError) => void;
|
|
1769
|
+
/**
|
|
1770
|
+
* 配置管理对象。
|
|
1771
|
+
*
|
|
1772
|
+
* @example
|
|
1773
|
+
* ```ts
|
|
1774
|
+
* const result = config.load('app', './config/app.yml')
|
|
1775
|
+
* if (result.success) {
|
|
1776
|
+
* const cfg = config.get('app')
|
|
1777
|
+
* }
|
|
1778
|
+
* ```
|
|
1779
|
+
*/
|
|
1780
|
+
declare const config: {
|
|
1781
|
+
/**
|
|
1782
|
+
* 加载配置到缓存。
|
|
1783
|
+
*
|
|
1784
|
+
* 加载 YAML 文件并可选地用 Zod Schema 校验,成功后写入缓存。
|
|
1785
|
+
*
|
|
1786
|
+
* @param name - 配置名称(缓存 key)
|
|
1787
|
+
* @param filePath - YAML 文件路径
|
|
1788
|
+
* @param schema - 可选 Zod Schema(不传则跳过校验)
|
|
1789
|
+
* @returns 成功时返回解析后的配置数据;失败时返回 HaiError
|
|
1790
|
+
*
|
|
1791
|
+
* @example
|
|
1792
|
+
* ```ts
|
|
1793
|
+
* const result = config.load('core', './config/_core.yml', CoreConfigSchema)
|
|
1794
|
+
* if (result.success) {
|
|
1795
|
+
* // result.data 为校验后的配置
|
|
1796
|
+
* }
|
|
1797
|
+
* ```
|
|
1798
|
+
*/
|
|
1799
|
+
load<T>(name: string, filePath: string, schema?: ZodType<T>): HaiResult<T>;
|
|
1800
|
+
/**
|
|
1801
|
+
* 验证已加载的配置数据。
|
|
1802
|
+
*
|
|
1803
|
+
* 对缓存中的配置数据重新用 Schema 校验,校验通过后更新缓存。
|
|
1804
|
+
*
|
|
1805
|
+
* @param name - 配置名称
|
|
1806
|
+
* @param schema - Zod 验证模式
|
|
1807
|
+
* @returns 校验结果;未加载时返回 NOT_LOADED,格式错误返回 VALIDATION_ERROR
|
|
1808
|
+
*
|
|
1809
|
+
* @example
|
|
1810
|
+
* ```ts
|
|
1811
|
+
* const result = config.validate('app', AppSchema)
|
|
1812
|
+
* if (!result.success) {
|
|
1813
|
+
* // result.error.code 可能为 NOT_LOADED 或 VALIDATION_ERROR
|
|
1814
|
+
* }
|
|
1815
|
+
* ```
|
|
1816
|
+
*/
|
|
1817
|
+
validate<T>(name: string, schema: ZodType<T>): HaiResult<T>;
|
|
1818
|
+
/**
|
|
1819
|
+
* 获取已加载的配置。
|
|
1820
|
+
*
|
|
1821
|
+
* @param name - 配置名称
|
|
1822
|
+
* @returns 配置数据;未加载时返回 undefined
|
|
1823
|
+
*
|
|
1824
|
+
* @example
|
|
1825
|
+
* ```ts
|
|
1826
|
+
* const cfg = config.get<CoreConfig>('core')
|
|
1827
|
+
* if (cfg) {
|
|
1828
|
+
* // 使用 cfg
|
|
1829
|
+
* }
|
|
1830
|
+
* ```
|
|
1831
|
+
*/
|
|
1832
|
+
get<T>(name: string): T | undefined;
|
|
1833
|
+
/**
|
|
1834
|
+
* 获取配置,不存在时抛出错误。
|
|
1835
|
+
*
|
|
1836
|
+
* @param name - 配置名称
|
|
1837
|
+
* @returns 配置数据
|
|
1838
|
+
* @throws 配置未加载时抛出 Error
|
|
1839
|
+
*
|
|
1840
|
+
* @example
|
|
1841
|
+
* ```ts
|
|
1842
|
+
* try {
|
|
1843
|
+
* const cfg = config.getOrThrow<CoreConfig>('core')
|
|
1844
|
+
* } catch (e) {
|
|
1845
|
+
* // 配置未加载
|
|
1846
|
+
* }
|
|
1847
|
+
* ```
|
|
1848
|
+
*/
|
|
1849
|
+
getOrThrow<T>(name: string): T;
|
|
1850
|
+
/**
|
|
1851
|
+
* 重新加载配置。
|
|
1852
|
+
*
|
|
1853
|
+
* 从磁盘重新读取配置文件并更新缓存,同时通知所有 watch 回调。
|
|
1854
|
+
*
|
|
1855
|
+
* @param name - 配置名称
|
|
1856
|
+
* @returns 重载结果;未加载时返回 NOT_LOADED
|
|
1857
|
+
*
|
|
1858
|
+
* @example
|
|
1859
|
+
* ```ts
|
|
1860
|
+
* const result = config.reload('app')
|
|
1861
|
+
* ```
|
|
1862
|
+
*/
|
|
1863
|
+
reload(name: string): HaiResult<unknown>;
|
|
1864
|
+
/**
|
|
1865
|
+
* 检查配置是否已加载。
|
|
1866
|
+
*
|
|
1867
|
+
* @param name - 配置名称
|
|
1868
|
+
* @returns 是否已加载到缓存
|
|
1869
|
+
*
|
|
1870
|
+
* @example
|
|
1871
|
+
* ```ts
|
|
1872
|
+
* if (config.has('db')) {
|
|
1873
|
+
* const dbCfg = config.get('db')
|
|
1874
|
+
* }
|
|
1875
|
+
* ```
|
|
1876
|
+
*/
|
|
1877
|
+
has(name: string): boolean;
|
|
1878
|
+
/**
|
|
1879
|
+
* 清除配置缓存(同时停止对应监听)。
|
|
1880
|
+
*
|
|
1881
|
+
* @param name - 配置名称;不传则清除全部
|
|
1882
|
+
*
|
|
1883
|
+
* @example
|
|
1884
|
+
* ```ts
|
|
1885
|
+
* config.clear('app') // 清除单个
|
|
1886
|
+
* config.clear() // 清除全部
|
|
1887
|
+
* ```
|
|
1888
|
+
*/
|
|
1889
|
+
clear(name?: string): void;
|
|
1890
|
+
/**
|
|
1891
|
+
* 获取所有已加载的配置名称。
|
|
1892
|
+
*
|
|
1893
|
+
* @returns 配置名称数组
|
|
1894
|
+
*
|
|
1895
|
+
* @example
|
|
1896
|
+
* ```ts
|
|
1897
|
+
* const names = config.keys() // ['core', 'db', 'app']
|
|
1898
|
+
* ```
|
|
1899
|
+
*/
|
|
1900
|
+
keys(): string[];
|
|
1901
|
+
/**
|
|
1902
|
+
* 监听配置文件变更并自动重载。
|
|
1903
|
+
*
|
|
1904
|
+
* 文件变更时自动重新加载并调用回调。配置未加载时立即回调 NOT_LOADED 错误。
|
|
1905
|
+
*
|
|
1906
|
+
* @param name - 配置名称
|
|
1907
|
+
* @param callback - 配置变更回调,接收新配置或错误
|
|
1908
|
+
* @returns 取消监听函数
|
|
1909
|
+
*
|
|
1910
|
+
* @example
|
|
1911
|
+
* ```ts
|
|
1912
|
+
* const unwatch = config.watch('app', (cfg, error) => {
|
|
1913
|
+
* if (error) { core.logger.error('reload failed', { error }); return }
|
|
1914
|
+
* core.logger.info('config updated', { cfg })
|
|
1915
|
+
* })
|
|
1916
|
+
* // 取消监听
|
|
1917
|
+
* unwatch()
|
|
1918
|
+
* ```
|
|
1919
|
+
*/
|
|
1920
|
+
watch<T = unknown>(name: string, callback: WatchCallback<T>): () => void;
|
|
1921
|
+
/**
|
|
1922
|
+
* 停止配置文件监听。
|
|
1923
|
+
*
|
|
1924
|
+
* @param name - 配置名称;不传则停止所有监听
|
|
1925
|
+
*
|
|
1926
|
+
* @example
|
|
1927
|
+
* ```ts
|
|
1928
|
+
* config.unwatch('app') // 停止单个
|
|
1929
|
+
* config.unwatch() // 停止全部
|
|
1930
|
+
* ```
|
|
1931
|
+
*/
|
|
1932
|
+
unwatch(name?: string): void;
|
|
1933
|
+
/**
|
|
1934
|
+
* 检查是否正在监听某个配置。
|
|
1935
|
+
*
|
|
1936
|
+
* @param name - 配置名称
|
|
1937
|
+
* @returns 是否有活跃的 watcher
|
|
1938
|
+
*
|
|
1939
|
+
* @example
|
|
1940
|
+
* ```ts
|
|
1941
|
+
* if (config.isWatching('app')) {
|
|
1942
|
+
* config.unwatch('app')
|
|
1943
|
+
* }
|
|
1944
|
+
* ```
|
|
1945
|
+
*/
|
|
1946
|
+
isWatching(name: string): boolean;
|
|
1947
|
+
};
|
|
1948
|
+
|
|
1949
|
+
export { type TimeFn as $, type MessageDictionary as A, type MessageOptions as B, type CoreFunctions as C, type PaginationOptions as D, type Env as E, type PaginationOptionsInput as F, type ZodValidationFn as G, HaiCommonError as H, type IdConfig as I, type ZodValidationMessageKey as J, err as K, type Locale as L, type MatchHandlers as M, ok as N, type I18nFn as O, type PaginatedResult as P, zodValidation as Q, type IdFn as R, type ObjectFn as S, type TypeUtilFn as T, type SanitizeFn as U, type ValidationFormError as V, type WatchCallback as W, type StringFn as X, type ArrayFn as Y, type ZodMessageGetter as Z, type AsyncFn as _, type CoreOptions as a, type ErrorFn as a0, type ModuleFn as a1, type CoreConfig as b, config as c, CoreConfigSchema as d, type CoreLogger as e, EnvSchema as f, type ErrorInfo as g, type ErrorInfoValue as h, HaiConfigError as i, type HaiError as j, type HaiErrorDef as k, type HaiResult as l, IdConfigSchema as m, type InterpolationParams as n, type LocaleInfo as o, type LocaleMessages as p, type LogContext as q, type LogFormat as r, LogFormatSchema as s, type LogLevel as t, LogLevelSchema as u, type Logger as v, type LoggerFunctions as w, type LoggerOptions as x, type LoggingConfig as y, LoggingConfigSchema as z };
|
package/dist/node.d.ts
CHANGED
|
@@ -1,207 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @h-ai/core — 配置管理(Node.js 专用)
|
|
7
|
-
*
|
|
8
|
-
* 提供 YAML 配置文件加载、环境变量插值、缓存管理。
|
|
9
|
-
* @module core-function-config
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 监听回调类型。
|
|
14
|
-
*
|
|
15
|
-
* @template T - 配置数据类型
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```ts
|
|
19
|
-
* const callback: WatchCallback<AppConfig> = (cfg, error) => {
|
|
20
|
-
* if (error) { core.logger.error('Config reload failed', { error }); return }
|
|
21
|
-
* // 使用更新后的 cfg
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
type WatchCallback<T = unknown> = (config: T | null, error?: HaiError) => void;
|
|
26
|
-
/**
|
|
27
|
-
* 配置管理对象。
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```ts
|
|
31
|
-
* const result = config.load('app', './config/app.yml')
|
|
32
|
-
* if (result.success) {
|
|
33
|
-
* const cfg = config.get('app')
|
|
34
|
-
* }
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
declare const config: {
|
|
38
|
-
/**
|
|
39
|
-
* 加载配置到缓存。
|
|
40
|
-
*
|
|
41
|
-
* 加载 YAML 文件并可选地用 Zod Schema 校验,成功后写入缓存。
|
|
42
|
-
*
|
|
43
|
-
* @param name - 配置名称(缓存 key)
|
|
44
|
-
* @param filePath - YAML 文件路径
|
|
45
|
-
* @param schema - 可选 Zod Schema(不传则跳过校验)
|
|
46
|
-
* @returns 成功时返回解析后的配置数据;失败时返回 HaiError
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```ts
|
|
50
|
-
* const result = config.load('core', './config/_core.yml', CoreConfigSchema)
|
|
51
|
-
* if (result.success) {
|
|
52
|
-
* // result.data 为校验后的配置
|
|
53
|
-
* }
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
load<T>(name: string, filePath: string, schema?: ZodType<T>): HaiResult<T>;
|
|
57
|
-
/**
|
|
58
|
-
* 验证已加载的配置数据。
|
|
59
|
-
*
|
|
60
|
-
* 对缓存中的配置数据重新用 Schema 校验,校验通过后更新缓存。
|
|
61
|
-
*
|
|
62
|
-
* @param name - 配置名称
|
|
63
|
-
* @param schema - Zod 验证模式
|
|
64
|
-
* @returns 校验结果;未加载时返回 NOT_LOADED,格式错误返回 VALIDATION_ERROR
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```ts
|
|
68
|
-
* const result = config.validate('app', AppSchema)
|
|
69
|
-
* if (!result.success) {
|
|
70
|
-
* // result.error.code 可能为 NOT_LOADED 或 VALIDATION_ERROR
|
|
71
|
-
* }
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
validate<T>(name: string, schema: ZodType<T>): HaiResult<T>;
|
|
75
|
-
/**
|
|
76
|
-
* 获取已加载的配置。
|
|
77
|
-
*
|
|
78
|
-
* @param name - 配置名称
|
|
79
|
-
* @returns 配置数据;未加载时返回 undefined
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```ts
|
|
83
|
-
* const cfg = config.get<CoreConfig>('core')
|
|
84
|
-
* if (cfg) {
|
|
85
|
-
* // 使用 cfg
|
|
86
|
-
* }
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
get<T>(name: string): T | undefined;
|
|
90
|
-
/**
|
|
91
|
-
* 获取配置,不存在时抛出错误。
|
|
92
|
-
*
|
|
93
|
-
* @param name - 配置名称
|
|
94
|
-
* @returns 配置数据
|
|
95
|
-
* @throws 配置未加载时抛出 Error
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* ```ts
|
|
99
|
-
* try {
|
|
100
|
-
* const cfg = config.getOrThrow<CoreConfig>('core')
|
|
101
|
-
* } catch (e) {
|
|
102
|
-
* // 配置未加载
|
|
103
|
-
* }
|
|
104
|
-
* ```
|
|
105
|
-
*/
|
|
106
|
-
getOrThrow<T>(name: string): T;
|
|
107
|
-
/**
|
|
108
|
-
* 重新加载配置。
|
|
109
|
-
*
|
|
110
|
-
* 从磁盘重新读取配置文件并更新缓存,同时通知所有 watch 回调。
|
|
111
|
-
*
|
|
112
|
-
* @param name - 配置名称
|
|
113
|
-
* @returns 重载结果;未加载时返回 NOT_LOADED
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```ts
|
|
117
|
-
* const result = config.reload('app')
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
reload(name: string): HaiResult<unknown>;
|
|
121
|
-
/**
|
|
122
|
-
* 检查配置是否已加载。
|
|
123
|
-
*
|
|
124
|
-
* @param name - 配置名称
|
|
125
|
-
* @returns 是否已加载到缓存
|
|
126
|
-
*
|
|
127
|
-
* @example
|
|
128
|
-
* ```ts
|
|
129
|
-
* if (config.has('db')) {
|
|
130
|
-
* const dbCfg = config.get('db')
|
|
131
|
-
* }
|
|
132
|
-
* ```
|
|
133
|
-
*/
|
|
134
|
-
has(name: string): boolean;
|
|
135
|
-
/**
|
|
136
|
-
* 清除配置缓存(同时停止对应监听)。
|
|
137
|
-
*
|
|
138
|
-
* @param name - 配置名称;不传则清除全部
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* ```ts
|
|
142
|
-
* config.clear('app') // 清除单个
|
|
143
|
-
* config.clear() // 清除全部
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
|
-
clear(name?: string): void;
|
|
147
|
-
/**
|
|
148
|
-
* 获取所有已加载的配置名称。
|
|
149
|
-
*
|
|
150
|
-
* @returns 配置名称数组
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```ts
|
|
154
|
-
* const names = config.keys() // ['core', 'db', 'app']
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
|
-
keys(): string[];
|
|
158
|
-
/**
|
|
159
|
-
* 监听配置文件变更并自动重载。
|
|
160
|
-
*
|
|
161
|
-
* 文件变更时自动重新加载并调用回调。配置未加载时立即回调 NOT_LOADED 错误。
|
|
162
|
-
*
|
|
163
|
-
* @param name - 配置名称
|
|
164
|
-
* @param callback - 配置变更回调,接收新配置或错误
|
|
165
|
-
* @returns 取消监听函数
|
|
166
|
-
*
|
|
167
|
-
* @example
|
|
168
|
-
* ```ts
|
|
169
|
-
* const unwatch = config.watch('app', (cfg, error) => {
|
|
170
|
-
* if (error) { core.logger.error('reload failed', { error }); return }
|
|
171
|
-
* core.logger.info('config updated', { cfg })
|
|
172
|
-
* })
|
|
173
|
-
* // 取消监听
|
|
174
|
-
* unwatch()
|
|
175
|
-
* ```
|
|
176
|
-
*/
|
|
177
|
-
watch<T = unknown>(name: string, callback: WatchCallback<T>): () => void;
|
|
178
|
-
/**
|
|
179
|
-
* 停止配置文件监听。
|
|
180
|
-
*
|
|
181
|
-
* @param name - 配置名称;不传则停止所有监听
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* ```ts
|
|
185
|
-
* config.unwatch('app') // 停止单个
|
|
186
|
-
* config.unwatch() // 停止全部
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
unwatch(name?: string): void;
|
|
190
|
-
/**
|
|
191
|
-
* 检查是否正在监听某个配置。
|
|
192
|
-
*
|
|
193
|
-
* @param name - 配置名称
|
|
194
|
-
* @returns 是否有活跃的 watcher
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```ts
|
|
198
|
-
* if (config.isWatching('app')) {
|
|
199
|
-
* config.unwatch('app')
|
|
200
|
-
* }
|
|
201
|
-
* ```
|
|
202
|
-
*/
|
|
203
|
-
isWatching(name: string): boolean;
|
|
204
|
-
};
|
|
1
|
+
import { C as CoreFunctions, c as config, a as CoreOptions } from './core-function-config.node-Baqu9RZ3.js';
|
|
2
|
+
export { b as CoreConfig, d as CoreConfigSchema, e as CoreLogger, E as Env, f as EnvSchema, g as ErrorInfo, h as ErrorInfoValue, H as HaiCommonError, i as HaiConfigError, j as HaiError, k as HaiErrorDef, l as HaiResult, I as IdConfig, m as IdConfigSchema, n as InterpolationParams, L as Locale, o as LocaleInfo, p as LocaleMessages, q as LogContext, r as LogFormat, s as LogFormatSchema, t as LogLevel, u as LogLevelSchema, v as Logger, w as LoggerFunctions, x as LoggerOptions, y as LoggingConfig, z as LoggingConfigSchema, M as MatchHandlers, A as MessageDictionary, B as MessageOptions, P as PaginatedResult, D as PaginationOptions, F as PaginationOptionsInput, V as ValidationFormError, Z as ZodMessageGetter, G as ZodValidationFn, J as ZodValidationMessageKey, K as err, N as ok } from './core-function-config.node-Baqu9RZ3.js';
|
|
3
|
+
import 'zod';
|
|
205
4
|
|
|
206
5
|
/**
|
|
207
6
|
* @h-ai/core — Core 服务聚合(Node.js)
|
|
@@ -241,4 +40,4 @@ declare const core: NodeCoreFunctions;
|
|
|
241
40
|
*/
|
|
242
41
|
declare function initCore(options?: CoreOptions): void;
|
|
243
42
|
|
|
244
|
-
export { CoreFunctions, CoreOptions,
|
|
43
|
+
export { CoreFunctions, CoreOptions, core };
|
package/dist/node.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { createCore, CoreConfigSchema, i18n, err, HaiConfigError, ok, typeUtils } from './chunk-
|
|
2
|
-
export { CoreConfigSchema, EnvSchema, HaiCommonError, HaiConfigError, IdConfigSchema, LogFormatSchema, LogLevelSchema, LoggingConfigSchema, err, ok } from './chunk-
|
|
1
|
+
import { createCore, CoreConfigSchema, i18n, err, HaiConfigError, ok, sanitize, typeUtils } from './chunk-3E75CCDQ.js';
|
|
2
|
+
export { CoreConfigSchema, EnvSchema, HaiCommonError, HaiConfigError, IdConfigSchema, LogFormatSchema, LogLevelSchema, LoggingConfigSchema, err, ok } from './chunk-3E75CCDQ.js';
|
|
3
3
|
import { existsSync, readdirSync, watch, readFileSync } from 'fs';
|
|
4
4
|
import { join } from 'path';
|
|
5
5
|
import process2 from 'process';
|
|
@@ -465,28 +465,35 @@ function setLogLevel(level) {
|
|
|
465
465
|
function getLogLevel() {
|
|
466
466
|
return globalLevel;
|
|
467
467
|
}
|
|
468
|
+
function sanitizeLogContext(ctx) {
|
|
469
|
+
if (!ctx || Object.keys(ctx).length === 0) {
|
|
470
|
+
return ctx;
|
|
471
|
+
}
|
|
472
|
+
return sanitize.sanitizeSensitiveFields(ctx);
|
|
473
|
+
}
|
|
468
474
|
function wrapPino(pinoLogger, context) {
|
|
469
475
|
return {
|
|
470
476
|
trace(message, ctx) {
|
|
471
|
-
pinoLogger.trace({ ...context, ...ctx }, message);
|
|
477
|
+
pinoLogger.trace(sanitizeLogContext({ ...context, ...ctx }) ?? {}, message);
|
|
472
478
|
},
|
|
473
479
|
debug(message, ctx) {
|
|
474
|
-
pinoLogger.debug({ ...context, ...ctx }, message);
|
|
480
|
+
pinoLogger.debug(sanitizeLogContext({ ...context, ...ctx }) ?? {}, message);
|
|
475
481
|
},
|
|
476
482
|
info(message, ctx) {
|
|
477
|
-
pinoLogger.info({ ...context, ...ctx }, message);
|
|
483
|
+
pinoLogger.info(sanitizeLogContext({ ...context, ...ctx }) ?? {}, message);
|
|
478
484
|
},
|
|
479
485
|
warn(message, ctx) {
|
|
480
|
-
pinoLogger.warn({ ...context, ...ctx }, message);
|
|
486
|
+
pinoLogger.warn(sanitizeLogContext({ ...context, ...ctx }) ?? {}, message);
|
|
481
487
|
},
|
|
482
488
|
error(message, ctx) {
|
|
483
|
-
pinoLogger.error({ ...context, ...ctx }, message);
|
|
489
|
+
pinoLogger.error(sanitizeLogContext({ ...context, ...ctx }) ?? {}, message);
|
|
484
490
|
},
|
|
485
491
|
fatal(message, ctx) {
|
|
486
|
-
pinoLogger.fatal({ ...context, ...ctx }, message);
|
|
492
|
+
pinoLogger.fatal(sanitizeLogContext({ ...context, ...ctx }) ?? {}, message);
|
|
487
493
|
},
|
|
488
494
|
child(childContext) {
|
|
489
|
-
|
|
495
|
+
const sanitizedChildContext = sanitizeLogContext(childContext) ?? {};
|
|
496
|
+
return wrapPino(pinoLogger.child(sanitizedChildContext), { ...context, ...sanitizedChildContext });
|
|
490
497
|
}
|
|
491
498
|
};
|
|
492
499
|
}
|