@ubean/shared 0.5.1 → 0.6.0-beta.1

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/dist/index.d.ts CHANGED
@@ -399,6 +399,18 @@ export interface EnvValidationError {
399
399
  export declare function defineEnv<S extends EnvSchema>(config: EnvConfig<S>): DefineEnvResult<S>;
400
400
  export declare function setRuntimeEnv(env: Record<string, unknown>): void;
401
401
  export declare function useRuntimeEnv<T = string>(key: string, defaultValue?: T): T;
402
+ /**
403
+ * 当前是否运行在 Node 系运行时(Node / Bun / Deno 的 node 兼容层)上。
404
+ *
405
+ * 用途:**把 Node 专用能力挡在动态 import 里**。Cloudflare Workers 这类运行时没有 `node:fs`
406
+ * (即使开 `nodejs_compat` 也不支持),而打包器会把「静态 import 的 node 内建」留在产物里 ——
407
+ * workerd 在**模块实例化**阶段就会失败(`No such module "node:fs/promises"`),根本轮不到分支
408
+ * 判断。因此判据必须是运行时的,且 `import()` 必须写在分支内部(动态 import 只在执行时才解析)。
409
+ *
410
+ * 判定用 `process.versions.node` 而不是 `typeof process`:bundler 常会把 `process` 替身注入浏览器
411
+ * 产物,只看 `process` 会在 worker/browser 里误判为真。
412
+ */
413
+ export declare function isNodeRuntime(): boolean;
402
414
  //#endregion
403
415
  //#region src/path.d.ts
404
416
  /**
package/dist/index.js CHANGED
@@ -252,6 +252,21 @@ function setRuntimeEnv(env) {
252
252
  function useRuntimeEnv(key, defaultValue) {
253
253
  return _runtimeEnv[key] ?? defaultValue;
254
254
  }
255
+ /**
256
+ * 当前是否运行在 Node 系运行时(Node / Bun / Deno 的 node 兼容层)上。
257
+ *
258
+ * 用途:**把 Node 专用能力挡在动态 import 里**。Cloudflare Workers 这类运行时没有 `node:fs`
259
+ * (即使开 `nodejs_compat` 也不支持),而打包器会把「静态 import 的 node 内建」留在产物里 ——
260
+ * workerd 在**模块实例化**阶段就会失败(`No such module "node:fs/promises"`),根本轮不到分支
261
+ * 判断。因此判据必须是运行时的,且 `import()` 必须写在分支内部(动态 import 只在执行时才解析)。
262
+ *
263
+ * 判定用 `process.versions.node` 而不是 `typeof process`:bundler 常会把 `process` 替身注入浏览器
264
+ * 产物,只看 `process` 会在 worker/browser 里误判为真。
265
+ */
266
+ function isNodeRuntime() {
267
+ const proc = globalThis.process;
268
+ return typeof proc?.versions?.node === "string" || typeof proc?.versions?.bun === "string";
269
+ }
255
270
  //#endregion
256
271
  //#region src/string.ts
257
272
  /**
@@ -336,4 +351,4 @@ function matchAnyGlob(route, patterns) {
336
351
  return patterns.some((p) => matchGlob(route, p));
337
352
  }
338
353
  //#endregion
339
- export { ACTION_BRAND, ActionError, UbeanError, capitalize, createError, defineEnv, errorToResponse, fail, getBasename, getDirname, getExtension, getStem, isActionFailure, isServerAction, isUbeanError, matchAnyGlob, matchGlob, normalizePath, pathToTitle, setRuntimeEnv, useRuntimeEnv };
354
+ export { ACTION_BRAND, ActionError, UbeanError, capitalize, createError, defineEnv, errorToResponse, fail, getBasename, getDirname, getExtension, getStem, isActionFailure, isNodeRuntime, isServerAction, isUbeanError, matchAnyGlob, matchGlob, normalizePath, pathToTitle, setRuntimeEnv, useRuntimeEnv };
@@ -1,4 +1,4 @@
1
- import { n as getLogger } from "../logger-C9j4b6yh.js";
1
+ import { n as getLogger } from "../logger-NMOHjTCB.js";
2
2
  //#region src/logger/hono.ts
3
3
  /**
4
4
  * 判断路径是否应被跳过。
@@ -1,4 +1,5 @@
1
1
  import { i as UbeanLoggerOptions, n as RequestLoggerOptions, r as UbeanLogger, t as LogLevelName } from "../types-CFy62PZV.js";
2
+ import { TLogLevel } from "tslog";
2
3
  //#region src/logger/logger.d.ts
3
4
  /**
4
5
  * 创建一个 ubean 命名的 tslog logger(默认 name 为 `ubean`)。
@@ -45,5 +46,22 @@ export declare function getLogger(scope?: string, options?: UbeanLoggerOptions):
45
46
  * ```
46
47
  */
47
48
  export declare function setDebugLogging(enabled: boolean): void;
49
+ /**
50
+ * 设置全局日志级别(对已创建与后续创建的所有 logger 实例生效)。
51
+ *
52
+ * 供 CLI 在加载 `logging.level` 配置后调用,覆盖 `LOG_LEVEL`/`TSLOG_LEVEL` 环境
53
+ * 变量的既有效果。传入 `undefined` 为空操作(不覆盖环境变量语义)。
54
+ *
55
+ * 注意:必须走 tslog 实例的 `setMinLevel()` 方法 —— 它会把级别名归一化为数字 id
56
+ * (`tslog` 内部用数值比较过滤);直接改写 `settings.minLevel` 字符串会让比较失真。
57
+ * tslog 的级别名解析大小写不敏感,这里统一大写仅为通过其类型签名。
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const config = await loadUbeanConfig(cwd);
62
+ * if (config.logging.level) setMinLevel(config.logging.level);
63
+ * ```
64
+ */
65
+ export declare function setMinLevel(level: LogLevelName | TLogLevel | undefined): void;
48
66
  //#endregion
49
67
  export type { LogLevelName, RequestLoggerOptions, UbeanLogger, UbeanLoggerOptions };
@@ -1,2 +1,2 @@
1
- import { i as setDebugLogging, n as getLogger, r as logger, t as createUbeanLogger } from "../logger-C9j4b6yh.js";
2
- export { createUbeanLogger, getLogger, logger, setDebugLogging };
1
+ import { a as setMinLevel, i as setDebugLogging, n as getLogger, r as logger, t as createUbeanLogger } from "../logger-NMOHjTCB.js";
2
+ export { createUbeanLogger, getLogger, logger, setDebugLogging, setMinLevel };
@@ -15,6 +15,11 @@ const DEBUG_PRETTY_TEMPLATE = "{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}} {{log
15
15
  const DEFAULT_MIN_LEVEL = "INFO";
16
16
  /** 是否处于 debug 模式(输出详细模板) */
17
17
  let debugMode = false;
18
+ /**
19
+ * 配置文件显式指定的日志级别(`logging.level`,归一化为大写)。
20
+ * 优先级:logger 显式 `minLevel` > 此配置 > `LOG_LEVEL`/`TSLOG_LEVEL` 环境变量 > `info`。
21
+ */
22
+ let configuredMinLevel;
18
23
  /** 已创建的 logger 实例注册表,`setDebugLogging` 切换模板时统一更新(含子 logger) */
19
24
  const instances = /* @__PURE__ */ new Set();
20
25
  function registerInstance(instance) {
@@ -79,7 +84,7 @@ function getLogLevelFromEnv() {
79
84
  * ```
80
85
  */
81
86
  function createUbeanLogger(options = {}) {
82
- const minLevel = options.minLevel ?? getLogLevelFromEnv() ?? DEFAULT_MIN_LEVEL;
87
+ const minLevel = options.minLevel ?? configuredMinLevel ?? getLogLevelFromEnv() ?? DEFAULT_MIN_LEVEL;
83
88
  return registerInstance(new Logger({
84
89
  name: DEFAULT_NAME,
85
90
  ...options,
@@ -130,5 +135,27 @@ function setDebugLogging(enabled) {
130
135
  debugMode = enabled;
131
136
  for (const instance of instances) applyDebugMode(instance);
132
137
  }
138
+ /**
139
+ * 设置全局日志级别(对已创建与后续创建的所有 logger 实例生效)。
140
+ *
141
+ * 供 CLI 在加载 `logging.level` 配置后调用,覆盖 `LOG_LEVEL`/`TSLOG_LEVEL` 环境
142
+ * 变量的既有效果。传入 `undefined` 为空操作(不覆盖环境变量语义)。
143
+ *
144
+ * 注意:必须走 tslog 实例的 `setMinLevel()` 方法 —— 它会把级别名归一化为数字 id
145
+ * (`tslog` 内部用数值比较过滤);直接改写 `settings.minLevel` 字符串会让比较失真。
146
+ * tslog 的级别名解析大小写不敏感,这里统一大写仅为通过其类型签名。
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * const config = await loadUbeanConfig(cwd);
151
+ * if (config.logging.level) setMinLevel(config.logging.level);
152
+ * ```
153
+ */
154
+ function setMinLevel(level) {
155
+ if (!level || typeof level !== "string") return;
156
+ configuredMinLevel = level.toUpperCase();
157
+ const name = level.toUpperCase();
158
+ for (const instance of instances) instance.setMinLevel(name);
159
+ }
133
160
  //#endregion
134
- export { setDebugLogging as i, getLogger as n, logger as r, createUbeanLogger as t };
161
+ export { setMinLevel as a, setDebugLogging as i, getLogger as n, logger as r, createUbeanLogger as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ubean/shared",
3
- "version": "0.5.1",
3
+ "version": "0.6.0-beta.1",
4
4
  "description": "Shared protocol types, errors, env and universal utilities for ubean",
5
5
  "files": [
6
6
  "dist"