isr-engine 3.0.0 → 3.1.0
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 +8 -0
- package/dist/Logger-DqBS_3v8.cjs +184 -0
- package/dist/Logger-DqBS_3v8.cjs.map +1 -0
- package/dist/Logger-bg-doGwZ.js +174 -0
- package/dist/Logger-bg-doGwZ.js.map +1 -0
- package/dist/{abVariantContext-BcnxBn4V.cjs → abVariantContext-BvAMuhjj.cjs} +194 -122
- package/dist/abVariantContext-BvAMuhjj.cjs.map +1 -0
- package/dist/{abVariantContext-BmZZi1YZ.js → abVariantContext-Ud3aZ1yk.js} +117 -118
- package/dist/abVariantContext-Ud3aZ1yk.js.map +1 -0
- package/dist/cli/cli.cjs +1 -1
- package/dist/cli/cli.js +1 -1
- package/dist/config/normalizeEngineConfig.d.ts.map +1 -1
- package/dist/{createCacheInspectorMiddleware-BY6MX88X.js → createCacheInspectorMiddleware-Bjku5v62.js} +10 -5
- package/dist/createCacheInspectorMiddleware-Bjku5v62.js.map +1 -0
- package/dist/{createCacheInspectorMiddleware-DZr98Zfn.cjs → createCacheInspectorMiddleware-DG1_VLBS.cjs} +12 -5
- package/dist/createCacheInspectorMiddleware-DG1_VLBS.cjs.map +1 -0
- package/dist/{createISRApp-CS6XfjHl.js → createISRApp-DYVU8ydT.js} +402 -68
- package/dist/createISRApp-DYVU8ydT.js.map +1 -0
- package/dist/{createISRApp-CHiiWF3P.cjs → createISRApp-HX6X8UNb.cjs} +433 -92
- package/dist/createISRApp-HX6X8UNb.cjs.map +1 -0
- package/dist/{createImagePlugin-Ddo5t3dJ.js → createImagePlugin-BZJHOvN9.js} +67 -21
- package/dist/createImagePlugin-BZJHOvN9.js.map +1 -0
- package/dist/{createImagePlugin-BtJzr0t-.cjs → createImagePlugin-Ca4t_WhM.cjs} +73 -21
- package/dist/createImagePlugin-Ca4t_WhM.cjs.map +1 -0
- package/dist/defaults/runtime/browserObservability.d.ts.map +1 -1
- package/dist/defaults/runtime/defineSiteHooks.cjs +2 -2
- package/dist/defaults/runtime/defineSiteHooks.js +2 -2
- package/dist/{defineSiteHooks-BeVy-qPH.js → defineSiteHooks-CV4tqHvB.js} +11 -5
- package/dist/{defineSiteHooks-BeVy-qPH.js.map → defineSiteHooks-CV4tqHvB.js.map} +1 -1
- package/dist/{defineSiteHooks-C3v3WLS8.cjs → defineSiteHooks-w2IrF5-D.cjs} +11 -5
- package/dist/{defineSiteHooks-C3v3WLS8.cjs.map → defineSiteHooks-w2IrF5-D.cjs.map} +1 -1
- package/dist/engine/ISREngine.d.ts.map +1 -1
- package/dist/{i18n-CyJIoYJT.cjs → i18n-CS5QhOO0.cjs} +7 -30
- package/dist/i18n-CS5QhOO0.cjs.map +1 -0
- package/dist/{i18n-Dobe0Wxn.js → i18n-CnXYAgQE.js} +8 -25
- package/dist/i18n-CnXYAgQE.js.map +1 -0
- package/dist/isr/ISRRoutes.d.ts +3 -1
- package/dist/isr/ISRRoutes.d.ts.map +1 -1
- package/dist/isr-engine.cjs +576 -94
- package/dist/isr-engine.cjs.map +1 -1
- package/dist/isr-engine.js +571 -89
- package/dist/isr-engine.js.map +1 -1
- package/dist/logger/Logger.d.ts +6 -2
- package/dist/logger/Logger.d.ts.map +1 -1
- package/dist/logger/logRecord.d.ts +11 -0
- package/dist/logger/logRecord.d.ts.map +1 -0
- package/dist/logger/runtimeLogger.d.ts +6 -0
- package/dist/logger/runtimeLogger.d.ts.map +1 -0
- package/dist/middlewares/LocaleRedirect.d.ts.map +1 -1
- package/dist/plugin/createCacheInspectorMiddleware.d.ts.map +1 -1
- package/dist/plugin/createFontPlugin.d.ts +1 -1
- package/dist/plugin/createFontPlugin.d.ts.map +1 -1
- package/dist/plugin/googleFontAssets.d.ts +23 -0
- package/dist/plugin/googleFontAssets.d.ts.map +1 -0
- package/dist/plugin/isrCacheMiddleware.d.ts +5 -0
- package/dist/plugin/isrCacheMiddleware.d.ts.map +1 -1
- package/dist/plugin/remoteImageSource.d.ts +3 -3
- package/dist/plugin/remoteImageSource.d.ts.map +1 -1
- package/dist/rsc/ServerActions.d.ts.map +1 -1
- package/dist/rsc/cacheTag.d.ts.map +1 -1
- package/dist/rsc/index.cjs +8 -9
- package/dist/rsc/index.js +2 -3
- package/dist/rsc/revalidate.d.ts.map +1 -1
- package/dist/{rsc-BSxvkPHd.js → rsc-BJzB9s8U.js} +4 -6
- package/dist/rsc-BJzB9s8U.js.map +1 -0
- package/dist/{rsc-NB-Uka7j.cjs → rsc-CYT6B6bf.cjs} +5 -7
- package/dist/rsc-CYT6B6bf.cjs.map +1 -0
- package/dist/server/manager.d.ts.map +1 -1
- package/dist/server/opsConfig.d.ts +7 -1
- package/dist/server/opsConfig.d.ts.map +1 -1
- package/dist/server/productionRuntime.d.ts +2 -3
- package/dist/server/productionRuntime.d.ts.map +1 -1
- package/dist/server/requestLimits.d.ts +36 -0
- package/dist/server/requestLimits.d.ts.map +1 -0
- package/dist/server/safeRedirect.d.ts +6 -0
- package/dist/server/safeRedirect.d.ts.map +1 -0
- package/dist/types/ISRConfig.d.ts +17 -0
- package/dist/types/ISRConfig.d.ts.map +1 -1
- package/dist/utils/cookie.d.ts.map +1 -1
- package/package.json +3 -1
- package/src/defaults/runtime/browserObservability.ts +27 -9
- package/src/defaults/runtime/defineServerEntry.tsx +2 -2
- package/src/defaults/runtime/defineSiteHooks.ts +12 -7
- package/src/types/ISRConfig.ts +19 -0
- package/src/utils/cookie.ts +5 -3
- package/dist/RequestContext-BVQ-rOmV.js +0 -58
- package/dist/RequestContext-BVQ-rOmV.js.map +0 -1
- package/dist/RequestContext-DgGEAxcW.cjs +0 -105
- package/dist/RequestContext-DgGEAxcW.cjs.map +0 -1
- package/dist/abVariantContext-BcnxBn4V.cjs.map +0 -1
- package/dist/abVariantContext-BmZZi1YZ.js.map +0 -1
- package/dist/createCacheInspectorMiddleware-BY6MX88X.js.map +0 -1
- package/dist/createCacheInspectorMiddleware-DZr98Zfn.cjs.map +0 -1
- package/dist/createISRApp-CHiiWF3P.cjs.map +0 -1
- package/dist/createISRApp-CS6XfjHl.js.map +0 -1
- package/dist/createImagePlugin-BtJzr0t-.cjs.map +0 -1
- package/dist/createImagePlugin-Ddo5t3dJ.js.map +0 -1
- package/dist/i18n-CyJIoYJT.cjs.map +0 -1
- package/dist/i18n-Dobe0Wxn.js.map +0 -1
- package/dist/rsc-BSxvkPHd.js.map +0 -1
- package/dist/rsc-NB-Uka7j.cjs.map +0 -1
package/README.md
CHANGED
|
@@ -130,12 +130,18 @@ TTL 与 SWR 单位为秒。`routes` 控制渲染策略;页面路由可使用 `
|
|
|
130
130
|
|
|
131
131
|
`Logger.spin()` 返回 Ora 10 对象。`prefixText`、`suffixText` 可为字符串或生成函数;`color` 接受颜色名、`false` 或 `undefined`;自定义 spinner 的 `interval` 必须是正安全整数。
|
|
132
132
|
|
|
133
|
+
`Logger.getInstance({ logFile })` 的文件输出为 JSON Lines,每行独立保存时间、级别、请求上下文、参数与错误;采集程序应按 JSON 解析,原有纯文本文件可在升级前轮转。
|
|
134
|
+
|
|
133
135
|
## 运维与部署
|
|
134
136
|
|
|
135
137
|
部署保留完整 `dist/`、应用依赖及 `ssr.config.*`;配置编译需要应用根目录可写的 `.isr-cache/`。容器按需设置 `HOST=0.0.0.0`,反向代理后显式配置 `server.trustProxy` 的可信来源。
|
|
136
138
|
|
|
137
139
|
`/health` 默认公开。生产 `/metrics` 默认关闭;非公开 metrics/inventory 未配置 `server.ops.authToken` 会自动关闭。启用后通过 `Authorization: Bearer <token>` 或配置的 token header 访问;设为 `public: true` 会绕过 token 校验。
|
|
138
140
|
|
|
141
|
+
引擎运维接口、静态 HTML 文件读取和 SEO 路由分别使用进程内请求预算;超额返回 `429` 与 `Retry-After`。通过 `server.requestLimits.{ops,staticHtml,seo}` 配置 `windowMs`、单客户端 `maxRequests`、总量 `globalMaxRequests` 与容量 `maxClients`,均须为正整数。默认每分钟单客户端/总量分别为 `120/1200`、`600/12000`、`120/1200`;缓存 HIT 与应用 SSR/API 不计入这些预算。
|
|
142
|
+
|
|
143
|
+
Google 字体自托管只接受指定 Google HTTPS 来源,验证下载内容后交给 Vite 输出;`outDir` 相对构建输出目录,开发环境从内存提供资源。下载失败会使该优化任务失败,构建不再改写应用 `public/`。
|
|
144
|
+
|
|
139
145
|
```bash
|
|
140
146
|
curl -fsS http://localhost:3000/health
|
|
141
147
|
curl -fsS -H "x-isr-admin-token: $ISR_OPS_TOKEN" \
|
|
@@ -165,4 +171,6 @@ curl -fsS -H "x-isr-admin-token: $ISR_OPS_TOKEN" \
|
|
|
165
171
|
|
|
166
172
|
`main` 的自动发布流程通过验证后由 semantic-release 计算版本,以 npm OIDC 发布到 `registry.npmjs.org`;配置见 [发布工作流](./.github/workflows/publish.yml) 与 [release.config.mjs](./release.config.mjs)。npm 端需绑定对应仓库和工作流的 Trusted Publisher。
|
|
167
173
|
|
|
174
|
+
发布必须通过依赖审计和 CodeQL `security-extended` 扫描;任何扫描结果或缺失的扫描证据都会阻止发布,原始审计与 SARIF 结果保留为工作流产物。
|
|
175
|
+
|
|
168
176
|
许可证声明为 **UNLICENSED**,以 `package.json` 为准。
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
const require_rolldown_runtime = require("./rolldown-runtime-DakpK96I.cjs");
|
|
2
|
+
const require_abVariantContext = require("./abVariantContext-BvAMuhjj.cjs");
|
|
3
|
+
let chalk = require("chalk");
|
|
4
|
+
chalk = require_rolldown_runtime.__toESM(chalk, 1);
|
|
5
|
+
let ora = require("ora");
|
|
6
|
+
ora = require_rolldown_runtime.__toESM(ora, 1);
|
|
7
|
+
let path = require("path");
|
|
8
|
+
path = require_rolldown_runtime.__toESM(path, 1);
|
|
9
|
+
let pino = require("pino");
|
|
10
|
+
pino = require_rolldown_runtime.__toESM(pino, 1);
|
|
11
|
+
//#region src/logger/Logger.ts
|
|
12
|
+
var LogLevel = /* @__PURE__ */ function(LogLevel) {
|
|
13
|
+
LogLevel["VERBOSE"] = "verbose";
|
|
14
|
+
LogLevel["DEBUG"] = "debug";
|
|
15
|
+
LogLevel["INFO"] = "info";
|
|
16
|
+
LogLevel["WARN"] = "warn";
|
|
17
|
+
LogLevel["ERROR"] = "error";
|
|
18
|
+
LogLevel["SUCCESS"] = "success";
|
|
19
|
+
return LogLevel;
|
|
20
|
+
}({});
|
|
21
|
+
var Logger = class Logger {
|
|
22
|
+
static instance;
|
|
23
|
+
verboseMode = false;
|
|
24
|
+
logFile = null;
|
|
25
|
+
fileLogger = null;
|
|
26
|
+
fileDestination = null;
|
|
27
|
+
spinner = null;
|
|
28
|
+
static getInstance(options = {}) {
|
|
29
|
+
if (!Logger.instance) {
|
|
30
|
+
Logger.instance = new Logger();
|
|
31
|
+
require_abVariantContext.setOriginLogger(Logger.instance);
|
|
32
|
+
}
|
|
33
|
+
if (options.verbose !== void 0) Logger.instance.verboseMode = options.verbose;
|
|
34
|
+
if (options.logFile !== void 0) Logger.instance.configureFile(options.logFile);
|
|
35
|
+
return Logger.instance;
|
|
36
|
+
}
|
|
37
|
+
/** File destinations are operator configuration; request data is only a record field. */
|
|
38
|
+
configureFile(logFile) {
|
|
39
|
+
if (typeof logFile !== "string" || /[\u0000-\u001f\u007f]/.test(logFile)) throw new TypeError("logFile must be a path without control characters");
|
|
40
|
+
const resolved = logFile ? path.default.resolve(logFile) : null;
|
|
41
|
+
if (resolved === this.logFile) return;
|
|
42
|
+
this.closeFileDestination();
|
|
43
|
+
if (!resolved) return;
|
|
44
|
+
try {
|
|
45
|
+
const destination = pino.default.destination({
|
|
46
|
+
dest: resolved,
|
|
47
|
+
mkdir: true,
|
|
48
|
+
sync: true
|
|
49
|
+
});
|
|
50
|
+
destination.on("error", (error) => {
|
|
51
|
+
if (this.fileDestination === destination) this.closeFileDestination();
|
|
52
|
+
this.reportFileError(error);
|
|
53
|
+
});
|
|
54
|
+
destination.once("close", () => {
|
|
55
|
+
if (this.fileDestination !== destination) return;
|
|
56
|
+
this.fileLogger = null;
|
|
57
|
+
this.fileDestination = null;
|
|
58
|
+
this.logFile = null;
|
|
59
|
+
});
|
|
60
|
+
this.fileDestination = destination;
|
|
61
|
+
this.fileLogger = (0, pino.default)({
|
|
62
|
+
base: null,
|
|
63
|
+
timestamp: false,
|
|
64
|
+
level: "trace",
|
|
65
|
+
hooks: { streamWrite: require_abVariantContext.escapeSupplementalLogControls }
|
|
66
|
+
}, destination);
|
|
67
|
+
this.logFile = resolved;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
this.closeFileDestination();
|
|
70
|
+
this.reportFileError(error);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
closeFileDestination() {
|
|
74
|
+
const destination = this.fileDestination;
|
|
75
|
+
this.fileLogger = null;
|
|
76
|
+
this.fileDestination = null;
|
|
77
|
+
this.logFile = null;
|
|
78
|
+
if (!destination) return;
|
|
79
|
+
try {
|
|
80
|
+
destination.flushSync();
|
|
81
|
+
destination.end();
|
|
82
|
+
} catch (error) {
|
|
83
|
+
destination.destroy();
|
|
84
|
+
this.reportFileError(error);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
reportFileError(error) {
|
|
88
|
+
try {
|
|
89
|
+
process.stderr.write(`Failed to write to log file: ${require_abVariantContext.encodeLogField(require_abVariantContext.snapshotLogArgument(error))}\n`);
|
|
90
|
+
} catch {}
|
|
91
|
+
}
|
|
92
|
+
getLevelColor(level) {
|
|
93
|
+
switch (level) {
|
|
94
|
+
case "error": return chalk.default.red.bold;
|
|
95
|
+
case "warn": return chalk.default.yellow.bold;
|
|
96
|
+
case "success": return chalk.default.green.bold;
|
|
97
|
+
case "info": return chalk.default.blue.bold;
|
|
98
|
+
case "debug": return chalk.default.magenta;
|
|
99
|
+
case "verbose": return chalk.default.gray;
|
|
100
|
+
default: return chalk.default.white;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
log(level, ...args) {
|
|
104
|
+
if (!Object.values(LogLevel).includes(level)) throw new TypeError("Unknown log level");
|
|
105
|
+
if (level === "verbose" && !this.verboseMode) return;
|
|
106
|
+
if (level === "debug" && !this.verboseMode) return;
|
|
107
|
+
if (this.spinner?.isSpinning) this.spinner.stop();
|
|
108
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
109
|
+
const traceId = require_abVariantContext.snapshotLogArgument(require_abVariantContext.getTraceId());
|
|
110
|
+
const requestId = require_abVariantContext.snapshotLogArgument(require_abVariantContext.getRequestId());
|
|
111
|
+
const values = args.map(require_abVariantContext.snapshotLogArgument);
|
|
112
|
+
const errors = [];
|
|
113
|
+
const message = values.map((value, index) => {
|
|
114
|
+
if (args[index] instanceof Error) {
|
|
115
|
+
errors.push({
|
|
116
|
+
argument: index,
|
|
117
|
+
error: value
|
|
118
|
+
});
|
|
119
|
+
if (value && typeof value === "object" && !Array.isArray(value)) return value.stack || value.message || JSON.stringify(value, null, 2);
|
|
120
|
+
}
|
|
121
|
+
return typeof value === "string" ? value : JSON.stringify(value, null, 2);
|
|
122
|
+
}).join(" ");
|
|
123
|
+
const levelTag = this.getLevelColor(level)(`[${level.toUpperCase()}]`);
|
|
124
|
+
const traceTag = chalk.default.cyan(`traceId=${require_abVariantContext.encodeLogField(traceId)}`);
|
|
125
|
+
const requestTag = requestId === "unknown" ? "" : chalk.default.blue(`requestId=${require_abVariantContext.encodeLogField(requestId)}`);
|
|
126
|
+
console.log(`${chalk.default.gray(timestamp)} ${levelTag} ${traceTag} ${requestTag} message=${require_abVariantContext.encodeLogField(message)}`);
|
|
127
|
+
if (this.fileLogger) {
|
|
128
|
+
const transportLevel = level === "verbose" ? "trace" : level === "success" ? "info" : level;
|
|
129
|
+
try {
|
|
130
|
+
this.fileLogger[transportLevel]({
|
|
131
|
+
timestamp,
|
|
132
|
+
severity: level,
|
|
133
|
+
traceId,
|
|
134
|
+
requestId,
|
|
135
|
+
args: values,
|
|
136
|
+
errors
|
|
137
|
+
});
|
|
138
|
+
} catch (error) {
|
|
139
|
+
this.closeFileDestination();
|
|
140
|
+
this.reportFileError(error);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (this.spinner && !this.spinner.isSpinning) this.spinner.start();
|
|
144
|
+
}
|
|
145
|
+
error(...args) {
|
|
146
|
+
this.log("error", ...args);
|
|
147
|
+
}
|
|
148
|
+
warn(...args) {
|
|
149
|
+
this.log("warn", ...args);
|
|
150
|
+
}
|
|
151
|
+
info(...args) {
|
|
152
|
+
this.log("info", ...args);
|
|
153
|
+
}
|
|
154
|
+
success(...args) {
|
|
155
|
+
this.log("success", ...args);
|
|
156
|
+
}
|
|
157
|
+
debug(...args) {
|
|
158
|
+
this.log("debug", ...args);
|
|
159
|
+
}
|
|
160
|
+
verbose(...args) {
|
|
161
|
+
this.log("verbose", ...args);
|
|
162
|
+
}
|
|
163
|
+
spin(message) {
|
|
164
|
+
if (this.spinner) this.spinner.succeed();
|
|
165
|
+
this.spinner = (0, ora.default)(require_abVariantContext.encodeSpinnerMessage(message)).start();
|
|
166
|
+
return this.spinner;
|
|
167
|
+
}
|
|
168
|
+
stopSpinner(message, success = true) {
|
|
169
|
+
if (this.spinner) {
|
|
170
|
+
if (success) this.spinner.succeed(message === void 0 ? void 0 : require_abVariantContext.encodeSpinnerMessage(message));
|
|
171
|
+
else this.spinner.fail(message === void 0 ? void 0 : require_abVariantContext.encodeSpinnerMessage(message));
|
|
172
|
+
this.spinner = null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
//#endregion
|
|
177
|
+
Object.defineProperty(exports, "Logger", {
|
|
178
|
+
enumerable: true,
|
|
179
|
+
get: function() {
|
|
180
|
+
return Logger;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
//# sourceMappingURL=Logger-DqBS_3v8.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Logger-DqBS_3v8.cjs","names":[],"sources":["../src/logger/Logger.ts"],"sourcesContent":["import chalk from 'chalk';\nimport ora, { Ora } from 'ora';\nimport path from 'path';\nimport pino, { type Logger as PinoLogger } from 'pino';\nimport { getTraceId, getRequestId } from '../context/RequestContext';\nimport {\n encodeLogField,\n encodeSpinnerMessage,\n escapeSupplementalLogControls,\n snapshotLogArgument,\n type LogValue,\n} from './logRecord';\nimport { setOriginLogger } from './runtimeLogger';\n\nexport enum LogLevel {\n VERBOSE = 'verbose',\n DEBUG = 'debug',\n INFO = 'info',\n WARN = 'warn',\n ERROR = 'error',\n SUCCESS = 'success',\n}\n\nexport interface LoggerOptions {\n verbose?: boolean;\n logFile?: string;\n}\n\nexport class Logger {\n private static instance: Logger;\n private verboseMode: boolean = false;\n private logFile: string | null = null;\n private fileLogger: PinoLogger | null = null;\n private fileDestination: ReturnType<typeof pino.destination> | null = null;\n private spinner: Ora | null = null;\n\n public static getInstance(options: LoggerOptions = {}): Logger {\n if (!Logger.instance) {\n Logger.instance = new Logger();\n setOriginLogger(Logger.instance);\n }\n\n if (options.verbose !== undefined) {\n Logger.instance.verboseMode = options.verbose;\n }\n\n if (options.logFile !== undefined) {\n Logger.instance.configureFile(options.logFile);\n }\n\n return Logger.instance;\n }\n\n /** File destinations are operator configuration; request data is only a record field. */\n private configureFile(logFile: string): void {\n // eslint-disable-next-line no-control-regex\n if (typeof logFile !== 'string' || /[\\u0000-\\u001f\\u007f]/.test(logFile)) {\n throw new TypeError('logFile must be a path without control characters');\n }\n const resolved = logFile ? path.resolve(logFile) : null;\n if (resolved === this.logFile) return;\n this.closeFileDestination();\n if (!resolved) return;\n\n try {\n const destination = pino.destination({ dest: resolved, mkdir: true, sync: true });\n destination.on('error', error => {\n if (this.fileDestination === destination) this.closeFileDestination();\n this.reportFileError(error);\n });\n destination.once('close', () => {\n if (this.fileDestination !== destination) return;\n this.fileLogger = null;\n this.fileDestination = null;\n this.logFile = null;\n });\n this.fileDestination = destination;\n this.fileLogger = pino(\n {\n base: null,\n timestamp: false,\n level: 'trace',\n hooks: { streamWrite: escapeSupplementalLogControls },\n },\n destination\n );\n this.logFile = resolved;\n } catch (error) {\n this.closeFileDestination();\n this.reportFileError(error);\n }\n }\n\n private closeFileDestination(): void {\n const destination = this.fileDestination;\n this.fileLogger = null;\n this.fileDestination = null;\n this.logFile = null;\n if (!destination) return;\n try {\n destination.flushSync();\n destination.end();\n } catch (error) {\n destination.destroy();\n this.reportFileError(error);\n }\n }\n\n private reportFileError(error: unknown): void {\n try {\n process.stderr.write(\n `Failed to write to log file: ${encodeLogField(snapshotLogArgument(error))}\\n`\n );\n } catch {\n // Logging failure must never recurse or prevent serving a request.\n }\n }\n\n private getLevelColor(level: LogLevel) {\n switch (level) {\n case LogLevel.ERROR:\n return chalk.red.bold;\n case LogLevel.WARN:\n return chalk.yellow.bold;\n case LogLevel.SUCCESS:\n return chalk.green.bold;\n case LogLevel.INFO:\n return chalk.blue.bold;\n case LogLevel.DEBUG:\n return chalk.magenta;\n case LogLevel.VERBOSE:\n return chalk.gray;\n default:\n return chalk.white;\n }\n }\n\n public log(level: LogLevel, ...args: unknown[]) {\n if (!Object.values(LogLevel).includes(level)) {\n throw new TypeError('Unknown log level');\n }\n\n if (level === LogLevel.VERBOSE && !this.verboseMode) return;\n if (level === LogLevel.DEBUG && !this.verboseMode) return;\n\n if (this.spinner?.isSpinning) this.spinner.stop();\n\n const timestamp = new Date().toISOString();\n const traceId = snapshotLogArgument(getTraceId());\n const requestId = snapshotLogArgument(getRequestId());\n const values = args.map(snapshotLogArgument);\n const errors: Array<{ argument: number; error: LogValue }> = [];\n const message = values\n .map((value, index) => {\n if (args[index] instanceof Error) {\n errors.push({ argument: index, error: value });\n if (value && typeof value === 'object' && !Array.isArray(value)) {\n return value.stack || value.message || JSON.stringify(value, null, 2);\n }\n }\n return typeof value === 'string' ? value : JSON.stringify(value, null, 2);\n })\n .join(' ');\n\n // Encode each field before adding only the logger's own ANSI color sequences.\n const levelTag = this.getLevelColor(level)(`[${level.toUpperCase()}]`);\n const traceTag = chalk.cyan(`traceId=${encodeLogField(traceId)}`);\n const requestTag =\n requestId === 'unknown' ? '' : chalk.blue(`requestId=${encodeLogField(requestId)}`);\n console.log(\n `${chalk.gray(timestamp)} ${levelTag} ${traceTag} ${requestTag} message=${encodeLogField(message)}`\n );\n\n if (this.fileLogger) {\n const transportLevel =\n level === LogLevel.VERBOSE ? 'trace' : level === LogLevel.SUCCESS ? 'info' : level;\n try {\n this.fileLogger[transportLevel]({\n timestamp,\n severity: level,\n traceId,\n requestId,\n args: values,\n errors,\n });\n } catch (error) {\n this.closeFileDestination();\n this.reportFileError(error);\n }\n }\n\n if (this.spinner && !this.spinner.isSpinning) {\n this.spinner.start();\n }\n }\n\n public error(...args: unknown[]) {\n this.log(LogLevel.ERROR, ...args);\n }\n\n public warn(...args: unknown[]) {\n this.log(LogLevel.WARN, ...args);\n }\n\n public info(...args: unknown[]) {\n this.log(LogLevel.INFO, ...args);\n }\n\n public success(...args: unknown[]) {\n this.log(LogLevel.SUCCESS, ...args);\n }\n\n public debug(...args: unknown[]) {\n this.log(LogLevel.DEBUG, ...args);\n }\n\n public verbose(...args: unknown[]) {\n this.log(LogLevel.VERBOSE, ...args);\n }\n\n public spin(message: string): Ora {\n if (this.spinner) {\n this.spinner.succeed();\n }\n this.spinner = ora(encodeSpinnerMessage(message)).start();\n return this.spinner;\n }\n\n public stopSpinner(message?: string, success: boolean = true) {\n if (this.spinner) {\n if (success) {\n this.spinner.succeed(message === undefined ? undefined : encodeSpinnerMessage(message));\n } else {\n this.spinner.fail(message === undefined ? undefined : encodeSpinnerMessage(message));\n }\n this.spinner = null;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;AAcA,IAAY,WAAL,yBAAA,UAAA;CACL,SAAA,aAAA;CACA,SAAA,WAAA;CACA,SAAA,UAAA;CACA,SAAA,UAAA;CACA,SAAA,WAAA;CACA,SAAA,aAAA;;AACF,EAAA,CAAA,CAAA;AAOA,IAAa,SAAb,MAAa,OAAO;CAClB,OAAe;CACf,cAA+B;CAC/B,UAAiC;CACjC,aAAwC;CACxC,kBAAsE;CACtE,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IAAI;CAClC;CAEA,QAAe,GAAG,MAAiB;EACjC,KAAK,IAAA,WAAsB,GAAG,IAAI;CACpC;CAEA,KAAY,SAAsB;EAChC,IAAI,KAAK,SACP,KAAK,QAAQ,QAAQ;EAEvB,KAAK,WAAA,GAAA,IAAA,QAAA,CAAc,yBAAA,qBAAqB,OAAO,CAAC,CAAC,CAAC,MAAM;EACxD,OAAO,KAAK;CACd;CAEA,YAAmB,SAAkB,UAAmB,MAAM;EAC5D,IAAI,KAAK,SAAS;GAChB,IAAI,SACF,KAAK,QAAQ,QAAQ,YAAY,KAAA,IAAY,KAAA,IAAY,yBAAA,qBAAqB,OAAO,CAAC;QAEtF,KAAK,QAAQ,KAAK,YAAY,KAAA,IAAY,KAAA,IAAY,yBAAA,qBAAqB,OAAO,CAAC;GAErF,KAAK,UAAU;EACjB;CACF;AACF"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { C as getRequestId, _ as escapeSupplementalLogControls, g as encodeSpinnerMessage, h as encodeLogField, m as setOriginLogger, v as snapshotLogArgument, w as getTraceId } from "./abVariantContext-Ud3aZ1yk.js";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
import ora from "ora";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import pino from "pino";
|
|
6
|
+
//#region src/logger/Logger.ts
|
|
7
|
+
var LogLevel = /* @__PURE__ */ function(LogLevel) {
|
|
8
|
+
LogLevel["VERBOSE"] = "verbose";
|
|
9
|
+
LogLevel["DEBUG"] = "debug";
|
|
10
|
+
LogLevel["INFO"] = "info";
|
|
11
|
+
LogLevel["WARN"] = "warn";
|
|
12
|
+
LogLevel["ERROR"] = "error";
|
|
13
|
+
LogLevel["SUCCESS"] = "success";
|
|
14
|
+
return LogLevel;
|
|
15
|
+
}({});
|
|
16
|
+
var Logger = class Logger {
|
|
17
|
+
static instance;
|
|
18
|
+
verboseMode = false;
|
|
19
|
+
logFile = null;
|
|
20
|
+
fileLogger = null;
|
|
21
|
+
fileDestination = null;
|
|
22
|
+
spinner = null;
|
|
23
|
+
static getInstance(options = {}) {
|
|
24
|
+
if (!Logger.instance) {
|
|
25
|
+
Logger.instance = new Logger();
|
|
26
|
+
setOriginLogger(Logger.instance);
|
|
27
|
+
}
|
|
28
|
+
if (options.verbose !== void 0) Logger.instance.verboseMode = options.verbose;
|
|
29
|
+
if (options.logFile !== void 0) Logger.instance.configureFile(options.logFile);
|
|
30
|
+
return Logger.instance;
|
|
31
|
+
}
|
|
32
|
+
/** File destinations are operator configuration; request data is only a record field. */
|
|
33
|
+
configureFile(logFile) {
|
|
34
|
+
if (typeof logFile !== "string" || /[\u0000-\u001f\u007f]/.test(logFile)) throw new TypeError("logFile must be a path without control characters");
|
|
35
|
+
const resolved = logFile ? path.resolve(logFile) : null;
|
|
36
|
+
if (resolved === this.logFile) return;
|
|
37
|
+
this.closeFileDestination();
|
|
38
|
+
if (!resolved) return;
|
|
39
|
+
try {
|
|
40
|
+
const destination = pino.destination({
|
|
41
|
+
dest: resolved,
|
|
42
|
+
mkdir: true,
|
|
43
|
+
sync: true
|
|
44
|
+
});
|
|
45
|
+
destination.on("error", (error) => {
|
|
46
|
+
if (this.fileDestination === destination) this.closeFileDestination();
|
|
47
|
+
this.reportFileError(error);
|
|
48
|
+
});
|
|
49
|
+
destination.once("close", () => {
|
|
50
|
+
if (this.fileDestination !== destination) return;
|
|
51
|
+
this.fileLogger = null;
|
|
52
|
+
this.fileDestination = null;
|
|
53
|
+
this.logFile = null;
|
|
54
|
+
});
|
|
55
|
+
this.fileDestination = destination;
|
|
56
|
+
this.fileLogger = pino({
|
|
57
|
+
base: null,
|
|
58
|
+
timestamp: false,
|
|
59
|
+
level: "trace",
|
|
60
|
+
hooks: { streamWrite: escapeSupplementalLogControls }
|
|
61
|
+
}, destination);
|
|
62
|
+
this.logFile = resolved;
|
|
63
|
+
} catch (error) {
|
|
64
|
+
this.closeFileDestination();
|
|
65
|
+
this.reportFileError(error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
closeFileDestination() {
|
|
69
|
+
const destination = this.fileDestination;
|
|
70
|
+
this.fileLogger = null;
|
|
71
|
+
this.fileDestination = null;
|
|
72
|
+
this.logFile = null;
|
|
73
|
+
if (!destination) return;
|
|
74
|
+
try {
|
|
75
|
+
destination.flushSync();
|
|
76
|
+
destination.end();
|
|
77
|
+
} catch (error) {
|
|
78
|
+
destination.destroy();
|
|
79
|
+
this.reportFileError(error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
reportFileError(error) {
|
|
83
|
+
try {
|
|
84
|
+
process.stderr.write(`Failed to write to log file: ${encodeLogField(snapshotLogArgument(error))}\n`);
|
|
85
|
+
} catch {}
|
|
86
|
+
}
|
|
87
|
+
getLevelColor(level) {
|
|
88
|
+
switch (level) {
|
|
89
|
+
case "error": return chalk.red.bold;
|
|
90
|
+
case "warn": return chalk.yellow.bold;
|
|
91
|
+
case "success": return chalk.green.bold;
|
|
92
|
+
case "info": return chalk.blue.bold;
|
|
93
|
+
case "debug": return chalk.magenta;
|
|
94
|
+
case "verbose": return chalk.gray;
|
|
95
|
+
default: return chalk.white;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
log(level, ...args) {
|
|
99
|
+
if (!Object.values(LogLevel).includes(level)) throw new TypeError("Unknown log level");
|
|
100
|
+
if (level === "verbose" && !this.verboseMode) return;
|
|
101
|
+
if (level === "debug" && !this.verboseMode) return;
|
|
102
|
+
if (this.spinner?.isSpinning) this.spinner.stop();
|
|
103
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
104
|
+
const traceId = snapshotLogArgument(getTraceId());
|
|
105
|
+
const requestId = snapshotLogArgument(getRequestId());
|
|
106
|
+
const values = args.map(snapshotLogArgument);
|
|
107
|
+
const errors = [];
|
|
108
|
+
const message = values.map((value, index) => {
|
|
109
|
+
if (args[index] instanceof Error) {
|
|
110
|
+
errors.push({
|
|
111
|
+
argument: index,
|
|
112
|
+
error: value
|
|
113
|
+
});
|
|
114
|
+
if (value && typeof value === "object" && !Array.isArray(value)) return value.stack || value.message || JSON.stringify(value, null, 2);
|
|
115
|
+
}
|
|
116
|
+
return typeof value === "string" ? value : JSON.stringify(value, null, 2);
|
|
117
|
+
}).join(" ");
|
|
118
|
+
const levelTag = this.getLevelColor(level)(`[${level.toUpperCase()}]`);
|
|
119
|
+
const traceTag = chalk.cyan(`traceId=${encodeLogField(traceId)}`);
|
|
120
|
+
const requestTag = requestId === "unknown" ? "" : chalk.blue(`requestId=${encodeLogField(requestId)}`);
|
|
121
|
+
console.log(`${chalk.gray(timestamp)} ${levelTag} ${traceTag} ${requestTag} message=${encodeLogField(message)}`);
|
|
122
|
+
if (this.fileLogger) {
|
|
123
|
+
const transportLevel = level === "verbose" ? "trace" : level === "success" ? "info" : level;
|
|
124
|
+
try {
|
|
125
|
+
this.fileLogger[transportLevel]({
|
|
126
|
+
timestamp,
|
|
127
|
+
severity: level,
|
|
128
|
+
traceId,
|
|
129
|
+
requestId,
|
|
130
|
+
args: values,
|
|
131
|
+
errors
|
|
132
|
+
});
|
|
133
|
+
} catch (error) {
|
|
134
|
+
this.closeFileDestination();
|
|
135
|
+
this.reportFileError(error);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (this.spinner && !this.spinner.isSpinning) this.spinner.start();
|
|
139
|
+
}
|
|
140
|
+
error(...args) {
|
|
141
|
+
this.log("error", ...args);
|
|
142
|
+
}
|
|
143
|
+
warn(...args) {
|
|
144
|
+
this.log("warn", ...args);
|
|
145
|
+
}
|
|
146
|
+
info(...args) {
|
|
147
|
+
this.log("info", ...args);
|
|
148
|
+
}
|
|
149
|
+
success(...args) {
|
|
150
|
+
this.log("success", ...args);
|
|
151
|
+
}
|
|
152
|
+
debug(...args) {
|
|
153
|
+
this.log("debug", ...args);
|
|
154
|
+
}
|
|
155
|
+
verbose(...args) {
|
|
156
|
+
this.log("verbose", ...args);
|
|
157
|
+
}
|
|
158
|
+
spin(message) {
|
|
159
|
+
if (this.spinner) this.spinner.succeed();
|
|
160
|
+
this.spinner = ora(encodeSpinnerMessage(message)).start();
|
|
161
|
+
return this.spinner;
|
|
162
|
+
}
|
|
163
|
+
stopSpinner(message, success = true) {
|
|
164
|
+
if (this.spinner) {
|
|
165
|
+
if (success) this.spinner.succeed(message === void 0 ? void 0 : encodeSpinnerMessage(message));
|
|
166
|
+
else this.spinner.fail(message === void 0 ? void 0 : encodeSpinnerMessage(message));
|
|
167
|
+
this.spinner = null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
//#endregion
|
|
172
|
+
export { Logger as t };
|
|
173
|
+
|
|
174
|
+
//# sourceMappingURL=Logger-bg-doGwZ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Logger-bg-doGwZ.js","names":[],"sources":["../src/logger/Logger.ts"],"sourcesContent":["import chalk from 'chalk';\nimport ora, { Ora } from 'ora';\nimport path from 'path';\nimport pino, { type Logger as PinoLogger } from 'pino';\nimport { getTraceId, getRequestId } from '../context/RequestContext';\nimport {\n encodeLogField,\n encodeSpinnerMessage,\n escapeSupplementalLogControls,\n snapshotLogArgument,\n type LogValue,\n} from './logRecord';\nimport { setOriginLogger } from './runtimeLogger';\n\nexport enum LogLevel {\n VERBOSE = 'verbose',\n DEBUG = 'debug',\n INFO = 'info',\n WARN = 'warn',\n ERROR = 'error',\n SUCCESS = 'success',\n}\n\nexport interface LoggerOptions {\n verbose?: boolean;\n logFile?: string;\n}\n\nexport class Logger {\n private static instance: Logger;\n private verboseMode: boolean = false;\n private logFile: string | null = null;\n private fileLogger: PinoLogger | null = null;\n private fileDestination: ReturnType<typeof pino.destination> | null = null;\n private spinner: Ora | null = null;\n\n public static getInstance(options: LoggerOptions = {}): Logger {\n if (!Logger.instance) {\n Logger.instance = new Logger();\n setOriginLogger(Logger.instance);\n }\n\n if (options.verbose !== undefined) {\n Logger.instance.verboseMode = options.verbose;\n }\n\n if (options.logFile !== undefined) {\n Logger.instance.configureFile(options.logFile);\n }\n\n return Logger.instance;\n }\n\n /** File destinations are operator configuration; request data is only a record field. */\n private configureFile(logFile: string): void {\n // eslint-disable-next-line no-control-regex\n if (typeof logFile !== 'string' || /[\\u0000-\\u001f\\u007f]/.test(logFile)) {\n throw new TypeError('logFile must be a path without control characters');\n }\n const resolved = logFile ? path.resolve(logFile) : null;\n if (resolved === this.logFile) return;\n this.closeFileDestination();\n if (!resolved) return;\n\n try {\n const destination = pino.destination({ dest: resolved, mkdir: true, sync: true });\n destination.on('error', error => {\n if (this.fileDestination === destination) this.closeFileDestination();\n this.reportFileError(error);\n });\n destination.once('close', () => {\n if (this.fileDestination !== destination) return;\n this.fileLogger = null;\n this.fileDestination = null;\n this.logFile = null;\n });\n this.fileDestination = destination;\n this.fileLogger = pino(\n {\n base: null,\n timestamp: false,\n level: 'trace',\n hooks: { streamWrite: escapeSupplementalLogControls },\n },\n destination\n );\n this.logFile = resolved;\n } catch (error) {\n this.closeFileDestination();\n this.reportFileError(error);\n }\n }\n\n private closeFileDestination(): void {\n const destination = this.fileDestination;\n this.fileLogger = null;\n this.fileDestination = null;\n this.logFile = null;\n if (!destination) return;\n try {\n destination.flushSync();\n destination.end();\n } catch (error) {\n destination.destroy();\n this.reportFileError(error);\n }\n }\n\n private reportFileError(error: unknown): void {\n try {\n process.stderr.write(\n `Failed to write to log file: ${encodeLogField(snapshotLogArgument(error))}\\n`\n );\n } catch {\n // Logging failure must never recurse or prevent serving a request.\n }\n }\n\n private getLevelColor(level: LogLevel) {\n switch (level) {\n case LogLevel.ERROR:\n return chalk.red.bold;\n case LogLevel.WARN:\n return chalk.yellow.bold;\n case LogLevel.SUCCESS:\n return chalk.green.bold;\n case LogLevel.INFO:\n return chalk.blue.bold;\n case LogLevel.DEBUG:\n return chalk.magenta;\n case LogLevel.VERBOSE:\n return chalk.gray;\n default:\n return chalk.white;\n }\n }\n\n public log(level: LogLevel, ...args: unknown[]) {\n if (!Object.values(LogLevel).includes(level)) {\n throw new TypeError('Unknown log level');\n }\n\n if (level === LogLevel.VERBOSE && !this.verboseMode) return;\n if (level === LogLevel.DEBUG && !this.verboseMode) return;\n\n if (this.spinner?.isSpinning) this.spinner.stop();\n\n const timestamp = new Date().toISOString();\n const traceId = snapshotLogArgument(getTraceId());\n const requestId = snapshotLogArgument(getRequestId());\n const values = args.map(snapshotLogArgument);\n const errors: Array<{ argument: number; error: LogValue }> = [];\n const message = values\n .map((value, index) => {\n if (args[index] instanceof Error) {\n errors.push({ argument: index, error: value });\n if (value && typeof value === 'object' && !Array.isArray(value)) {\n return value.stack || value.message || JSON.stringify(value, null, 2);\n }\n }\n return typeof value === 'string' ? value : JSON.stringify(value, null, 2);\n })\n .join(' ');\n\n // Encode each field before adding only the logger's own ANSI color sequences.\n const levelTag = this.getLevelColor(level)(`[${level.toUpperCase()}]`);\n const traceTag = chalk.cyan(`traceId=${encodeLogField(traceId)}`);\n const requestTag =\n requestId === 'unknown' ? '' : chalk.blue(`requestId=${encodeLogField(requestId)}`);\n console.log(\n `${chalk.gray(timestamp)} ${levelTag} ${traceTag} ${requestTag} message=${encodeLogField(message)}`\n );\n\n if (this.fileLogger) {\n const transportLevel =\n level === LogLevel.VERBOSE ? 'trace' : level === LogLevel.SUCCESS ? 'info' : level;\n try {\n this.fileLogger[transportLevel]({\n timestamp,\n severity: level,\n traceId,\n requestId,\n args: values,\n errors,\n });\n } catch (error) {\n this.closeFileDestination();\n this.reportFileError(error);\n }\n }\n\n if (this.spinner && !this.spinner.isSpinning) {\n this.spinner.start();\n }\n }\n\n public error(...args: unknown[]) {\n this.log(LogLevel.ERROR, ...args);\n }\n\n public warn(...args: unknown[]) {\n this.log(LogLevel.WARN, ...args);\n }\n\n public info(...args: unknown[]) {\n this.log(LogLevel.INFO, ...args);\n }\n\n public success(...args: unknown[]) {\n this.log(LogLevel.SUCCESS, ...args);\n }\n\n public debug(...args: unknown[]) {\n this.log(LogLevel.DEBUG, ...args);\n }\n\n public verbose(...args: unknown[]) {\n this.log(LogLevel.VERBOSE, ...args);\n }\n\n public spin(message: string): Ora {\n if (this.spinner) {\n this.spinner.succeed();\n }\n this.spinner = ora(encodeSpinnerMessage(message)).start();\n return this.spinner;\n }\n\n public stopSpinner(message?: string, success: boolean = true) {\n if (this.spinner) {\n if (success) {\n this.spinner.succeed(message === undefined ? undefined : encodeSpinnerMessage(message));\n } else {\n this.spinner.fail(message === undefined ? undefined : encodeSpinnerMessage(message));\n }\n this.spinner = null;\n }\n }\n}\n"],"mappings":";;;;;;AAcA,IAAY,WAAL,yBAAA,UAAA;CACL,SAAA,aAAA;CACA,SAAA,WAAA;CACA,SAAA,UAAA;CACA,SAAA,UAAA;CACA,SAAA,WAAA;CACA,SAAA,aAAA;;AACF,EAAA,CAAA,CAAA;AAOA,IAAa,SAAb,MAAa,OAAO;CAClB,OAAe;CACf,cAA+B;CAC/B,UAAiC;CACjC,aAAwC;CACxC,kBAAsE;CACtE,UAA8B;CAE9B,OAAc,YAAY,UAAyB,CAAC,GAAW;EAC7D,IAAI,CAAC,OAAO,UAAU;GACpB,OAAO,WAAW,IAAI,OAAO;GAC7B,gBAAgB,OAAO,QAAQ;EACjC;EAEA,IAAI,QAAQ,YAAY,KAAA,GACtB,OAAO,SAAS,cAAc,QAAQ;EAGxC,IAAI,QAAQ,YAAY,KAAA,GACtB,OAAO,SAAS,cAAc,QAAQ,OAAO;EAG/C,OAAO,OAAO;CAChB;;CAGA,cAAsB,SAAuB;EAE3C,IAAI,OAAO,YAAY,YAAY,wBAAwB,KAAK,OAAO,GACrE,MAAM,IAAI,UAAU,mDAAmD;EAEzE,MAAM,WAAW,UAAU,KAAK,QAAQ,OAAO,IAAI;EACnD,IAAI,aAAa,KAAK,SAAS;EAC/B,KAAK,qBAAqB;EAC1B,IAAI,CAAC,UAAU;EAEf,IAAI;GACF,MAAM,cAAc,KAAK,YAAY;IAAE,MAAM;IAAU,OAAO;IAAM,MAAM;GAAK,CAAC;GAChF,YAAY,GAAG,UAAS,UAAS;IA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