@logtape/logtape 1.2.0-dev.347 → 1.2.0-dev.359
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/deno.json +1 -1
- package/dist/config.cjs +3 -2
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +3 -2
- package/dist/config.js.map +1 -1
- package/dist/logger.cjs +224 -4
- package/dist/logger.js +224 -4
- package/dist/logger.js.map +1 -1
- package/package.json +1 -1
- package/src/config.ts +11 -4
- package/src/logger.test.ts +456 -0
- package/src/logger.ts +305 -2
package/deno.json
CHANGED
package/dist/config.cjs
CHANGED
|
@@ -156,9 +156,10 @@ function configureInternal(config, allowAsync) {
|
|
|
156
156
|
else throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
157
157
|
if (Symbol.dispose in filter) disposables.add(filter);
|
|
158
158
|
}
|
|
159
|
-
if ("process" in globalThis && !("Deno" in globalThis)) {
|
|
159
|
+
if (typeof globalThis.EdgeRuntime !== "string" && "process" in globalThis && !("Deno" in globalThis)) {
|
|
160
160
|
const proc = globalThis.process;
|
|
161
|
-
|
|
161
|
+
const onMethod = proc?.["on"];
|
|
162
|
+
if (typeof onMethod === "function") onMethod.call(proc, "exit", allowAsync ? dispose : disposeSync);
|
|
162
163
|
} else addEventListener("unload", allowAsync ? dispose : disposeSync);
|
|
163
164
|
const meta = require_logger.LoggerImpl.getLogger(["logtape", "meta"]);
|
|
164
165
|
if (!metaConfigured) meta.sinks.push(require_sink.getConsoleSink());
|
package/dist/config.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.cts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;;EAUK,KAAE,EALrB,MAKqB,CALd,OAKc,EALL,IAKK,CAAA;EAA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;;;;;;;;iBA6EM,oEAGZ,OAAO,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDvB,wEACN,OAAO,SAAS;;;;;iBAyIV,SAAA,CAAA,GAAa;;;;iBAOP,KAAA,CAAA,GAAS;;;;;;iBAUf,SAAA,CAAA;;;;iBAgBM,OAAA,CAAA,GAAW;;;;;;iBAejB,WAAA,CAAA;;;;cAQH,WAAA,SAAoB,KAAK"}
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;;EAUK,KAAE,EALrB,MAKqB,CALd,OAKc,EALL,IAKK,CAAA;EAAU;;;;EAKjB,OAMqB,CAAA,EAXhC,MAWgC,CAXzB,SAWyB,EAXd,UAWc,CAAA;EAAM;AAAP;AAW3C;EAA6B,OAAA,EAjBlB,YAiBkB,CAjBL,OAiBK,EAjBI,SAiBJ,CAAA,EAAA;EAAA;;;AAoCL;EA6EF,mBAAS,CAAA,EA5HP,mBA4HO,CA5Ha,MA4Hb,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EAAA;;;EAGI,KAAzB,CAAA,EAAA,OAAA;;AAAoC;AAgD9C;;AACiB,UArKA,YAqKA,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAO;;AAAR;
|
|
1
|
+
{"version":3,"file":"config.d.ts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;;EAUK,KAAE,EALrB,MAKqB,CALd,OAKc,EALL,IAKK,CAAA;EAAU;;;;EAKjB,OAMqB,CAAA,EAXhC,MAWgC,CAXzB,SAWyB,EAXd,UAWc,CAAA;EAAM;AAAP;AAW3C;EAA6B,OAAA,EAjBlB,YAiBkB,CAjBL,OAiBK,EAjBI,SAiBJ,CAAA,EAAA;EAAA;;;AAoCL;EA6EF,mBAAS,CAAA,EA5HP,mBA4HO,CA5Ha,MA4Hb,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EAAA;;;EAGI,KAAzB,CAAA,EAAA,OAAA;;AAAoC;AAgD9C;;AACiB,UArKA,YAqKA,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAO;;AAAR;AAyIhB;EAOsB,QAAK,EAAA,MAAA,GAAI,MAAA,EAAO;EAUtB;AAgBhB;AAeA;EAQa,KAAA,CAAA,EAzVH,OAyVe,EAAA;;;;;;;;;;;;;;YAzUb;;;;;;gBAOI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6EM,oEAGZ,OAAO,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDvB,wEACN,OAAO,SAAS;;;;;iBAyIV,SAAA,CAAA,GAAa;;;;iBAOP,KAAA,CAAA,GAAS;;;;;;iBAUf,SAAA,CAAA;;;;iBAgBM,OAAA,CAAA,GAAW;;;;;;iBAejB,WAAA,CAAA;;;;cAQH,WAAA,SAAoB,KAAK"}
|
package/dist/config.js
CHANGED
|
@@ -156,9 +156,10 @@ function configureInternal(config, allowAsync) {
|
|
|
156
156
|
else throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
157
157
|
if (Symbol.dispose in filter) disposables.add(filter);
|
|
158
158
|
}
|
|
159
|
-
if ("process" in globalThis && !("Deno" in globalThis)) {
|
|
159
|
+
if (typeof globalThis.EdgeRuntime !== "string" && "process" in globalThis && !("Deno" in globalThis)) {
|
|
160
160
|
const proc = globalThis.process;
|
|
161
|
-
|
|
161
|
+
const onMethod = proc?.["on"];
|
|
162
|
+
if (typeof onMethod === "function") onMethod.call(proc, "exit", allowAsync ? dispose : disposeSync);
|
|
162
163
|
} else addEventListener("unload", allowAsync ? dispose : disposeSync);
|
|
163
164
|
const meta = LoggerImpl.getLogger(["logtape", "meta"]);
|
|
164
165
|
if (!metaConfigured) meta.sinks.push(getConsoleSink());
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","names":["currentConfig: Config<string, string> | null","strongRefs: Set<LoggerImpl>","disposables: Set<Disposable>","asyncDisposables: Set<AsyncDisposable>","cfg: LoggerConfig<TSinkId, TFilterId>","config: Config<TSinkId, TFilterId>","allowAsync: boolean","promises: PromiseLike<void>[]","message: string"],"sources":["../src/config.ts"],"sourcesContent":["import type { ContextLocalStorage } from \"./context.ts\";\nimport { type FilterLike, toFilter } from \"./filter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport { LoggerImpl } from \"./logger.ts\";\nimport { getConsoleSink, type Sink } from \"./sink.ts\";\n\n/**\n * A configuration for the loggers.\n */\nexport interface Config<TSinkId extends string, TFilterId extends string> {\n /**\n * The sinks to use. The keys are the sink identifiers, and the values are\n * {@link Sink}s.\n */\n sinks: Record<TSinkId, Sink>;\n /**\n * The filters to use. The keys are the filter identifiers, and the values\n * are either {@link Filter}s or {@link LogLevel}s.\n */\n filters?: Record<TFilterId, FilterLike>;\n\n /**\n * The loggers to configure.\n */\n loggers: LoggerConfig<TSinkId, TFilterId>[];\n\n /**\n * The context-local storage to use for implicit contexts.\n * @since 0.7.0\n */\n contextLocalStorage?: ContextLocalStorage<Record<string, unknown>>;\n\n /**\n * Whether to reset the configuration before applying this one.\n */\n reset?: boolean;\n}\n\n/**\n * A logger configuration.\n */\nexport interface LoggerConfig<\n TSinkId extends string,\n TFilterId extends string,\n> {\n /**\n * The category of the logger. If a string, it is equivalent to an array\n * with one element.\n */\n category: string | string[];\n\n /**\n * The sink identifiers to use.\n */\n sinks?: TSinkId[];\n\n /**\n * Whether to inherit the parent's sinks. If `inherit`, the parent's sinks\n * are used along with the specified sinks. If `override`, the parent's\n * sinks are not used, and only the specified sinks are used.\n *\n * The default is `inherit`.\n * @default `\"inherit\"\n * @since 0.6.0\n */\n parentSinks?: \"inherit\" | \"override\";\n\n /**\n * The filter identifiers to use.\n */\n filters?: TFilterId[];\n\n /**\n * The lowest log level to accept. If `null`, the logger will reject all\n * records.\n * @since 0.8.0\n */\n lowestLevel?: LogLevel | null;\n}\n\n/**\n * The current configuration, if any. Otherwise, `null`.\n */\nlet currentConfig: Config<string, string> | null = null;\n\n/**\n * Strong references to the loggers.\n * This is to prevent the loggers from being garbage collected so that their\n * sinks and filters are not removed.\n */\nconst strongRefs: Set<LoggerImpl> = new Set();\n\n/**\n * Disposables to dispose when resetting the configuration.\n */\nconst disposables: Set<Disposable> = new Set();\n\n/**\n * Async disposables to dispose when resetting the configuration.\n */\nconst asyncDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Check if a config is for the meta logger.\n */\nfunction isLoggerConfigMeta<TSinkId extends string, TFilterId extends string>(\n cfg: LoggerConfig<TSinkId, TFilterId>,\n): boolean {\n return cfg.category.length === 0 ||\n (cfg.category.length === 1 && cfg.category[0] === \"logtape\") ||\n (cfg.category.length === 2 &&\n cfg.category[0] === \"logtape\" &&\n cfg.category[1] === \"meta\");\n}\n\n/**\n * Configure the loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * @example\n * ```typescript\n * await configure({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * filters: {\n * slow: (log) =>\n * \"duration\" in log.properties &&\n * log.properties.duration as number > 1000,\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: [\"my-app\", \"sql\"],\n * filters: [\"slow\"],\n * lowestLevel: \"debug\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n */\nexport async function configure<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>): Promise<void> {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n await reset();\n try {\n configureInternal(config, true);\n } catch (e) {\n if (e instanceof ConfigError) await reset();\n throw e;\n }\n}\n\n/**\n * Configure sync loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * Also note that passing async sinks or filters will throw. If\n * necessary use {@link resetSync} or {@link disposeSync}.\n *\n * @example\n * ```typescript\n * configureSync({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n * @since 0.9.0\n */\nexport function configureSync<TSinkId extends string, TFilterId extends string>(\n config: Config<TSinkId, TFilterId>,\n): void {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n if (asyncDisposables.size > 0) {\n throw new ConfigError(\n \"Previously configured async disposables are still active. \" +\n \"Use configure() instead or explicitly dispose them using dispose().\",\n );\n }\n resetSync();\n try {\n configureInternal(config, false);\n } catch (e) {\n if (e instanceof ConfigError) resetSync();\n throw e;\n }\n}\n\nfunction configureInternal<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>, allowAsync: boolean): void {\n currentConfig = config;\n\n let metaConfigured = false;\n const configuredCategories = new Set<string>();\n\n for (const cfg of config.loggers) {\n if (isLoggerConfigMeta(cfg)) {\n metaConfigured = true;\n }\n\n // Check for duplicate logger categories\n const categoryKey = Array.isArray(cfg.category)\n ? JSON.stringify(cfg.category)\n : JSON.stringify([cfg.category]);\n if (configuredCategories.has(categoryKey)) {\n throw new ConfigError(\n `Duplicate logger configuration for category: ${categoryKey}. ` +\n `Each category can only be configured once.`,\n );\n }\n configuredCategories.add(categoryKey);\n\n const logger = LoggerImpl.getLogger(cfg.category);\n for (const sinkId of cfg.sinks ?? []) {\n const sink = config.sinks[sinkId];\n if (!sink) {\n throw new ConfigError(`Sink not found: ${sinkId}.`);\n }\n logger.sinks.push(sink);\n }\n logger.parentSinks = cfg.parentSinks ?? \"inherit\";\n if (cfg.lowestLevel !== undefined) {\n logger.lowestLevel = cfg.lowestLevel;\n }\n for (const filterId of cfg.filters ?? []) {\n const filter = config.filters?.[filterId];\n if (filter === undefined) {\n throw new ConfigError(`Filter not found: ${filterId}.`);\n }\n logger.filters.push(toFilter(filter));\n }\n strongRefs.add(logger);\n }\n\n LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;\n\n for (const sink of Object.values<Sink>(config.sinks)) {\n if (Symbol.asyncDispose in sink) {\n if (allowAsync) asyncDisposables.add(sink as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in sink) disposables.add(sink as Disposable);\n }\n\n for (const filter of Object.values<FilterLike>(config.filters ?? {})) {\n if (filter == null || typeof filter === \"string\") continue;\n if (Symbol.asyncDispose in filter) {\n if (allowAsync) asyncDisposables.add(filter as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in filter) disposables.add(filter as Disposable);\n }\n\n if (\"process\" in globalThis && !(\"Deno\" in globalThis)) {\n // deno-lint-ignore no-explicit-any\n const proc = (globalThis as any).process;\n if (proc?.on) {\n proc.on(\"exit\", allowAsync ? dispose : disposeSync);\n }\n } else {\n // @ts-ignore: It's fine to addEventListener() on the browser/Deno\n addEventListener(\"unload\", allowAsync ? dispose : disposeSync);\n }\n const meta = LoggerImpl.getLogger([\"logtape\", \"meta\"]);\n if (!metaConfigured) {\n meta.sinks.push(getConsoleSink());\n }\n\n meta.info(\n \"LogTape loggers are configured. Note that LogTape itself uses the meta \" +\n \"logger, which has category {metaLoggerCategory}. The meta logger \" +\n \"purposes to log internal errors such as sink exceptions. If you \" +\n \"are seeing this message, the meta logger is automatically configured. \" +\n \"It's recommended to configure the meta logger with a separate sink \" +\n \"so that you can easily notice if logging itself fails or is \" +\n \"misconfigured. To turn off this message, configure the meta logger \" +\n \"with higher log levels than {dismissLevel}. See also \" +\n \"<https://logtape.org/manual/categories#meta-logger>.\",\n { metaLoggerCategory: [\"logtape\", \"meta\"], dismissLevel: \"info\" },\n );\n}\n\n/**\n * Get the current configuration, if any. Otherwise, `null`.\n * @returns The current configuration, if any. Otherwise, `null`.\n */\nexport function getConfig(): Config<string, string> | null {\n return currentConfig;\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes.\n */\nexport async function reset(): Promise<void> {\n await dispose();\n resetInternal();\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes. Will not clear async\n * sinks, only use with sync sinks. Use {@link reset} if you have async sinks.\n * @since 0.9.0\n */\nexport function resetSync(): void {\n disposeSync();\n resetInternal();\n}\n\nfunction resetInternal(): void {\n const rootLogger = LoggerImpl.getLogger([]);\n rootLogger.resetDescendants();\n delete rootLogger.contextLocalStorage;\n strongRefs.clear();\n currentConfig = null;\n}\n\n/**\n * Dispose of the disposables.\n */\nexport async function dispose(): Promise<void> {\n disposeSync();\n const promises: PromiseLike<void>[] = [];\n for (const disposable of asyncDisposables) {\n promises.push(disposable[Symbol.asyncDispose]());\n asyncDisposables.delete(disposable);\n }\n await Promise.all(promises);\n}\n\n/**\n * Dispose of the sync disposables. Async disposables will be untouched,\n * use {@link dispose} if you have async sinks.\n * @since 0.9.0\n */\nexport function disposeSync(): void {\n for (const disposable of disposables) disposable[Symbol.dispose]();\n disposables.clear();\n}\n\n/**\n * A configuration error.\n */\nexport class ConfigError extends Error {\n /**\n * Constructs a new configuration error.\n * @param message The error message.\n */\n constructor(message: string) {\n super(message);\n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AO,WAAW,UAAU,CAAC,WAAW,MAAO,EAAC;AACtD,MAAK,eACH,MAAK,MAAM,KAAK,gBAAgB,CAAC;AAGnC,MAAK,KACH,mkBASA;EAAE,oBAAoB,CAAC,WAAW,MAAO;EAAE,cAAc;CAAQ,EAClE;AACF;;;;;AAMD,SAAgB,YAA2C;AACzD,QAAO;AACR;;;;AAKD,eAAsB,QAAuB;AAC3C,OAAM,SAAS;AACf,gBAAe;AAChB;;;;;;AAOD,SAAgB,YAAkB;AAChC,cAAa;AACb,gBAAe;AAChB;AAED,SAAS,gBAAsB;CAC7B,MAAM,aAAa,WAAW,UAAU,CAAE,EAAC;AAC3C,YAAW,kBAAkB;AAC7B,QAAO,WAAW;AAClB,YAAW,OAAO;AAClB,iBAAgB;AACjB;;;;AAKD,eAAsB,UAAyB;AAC7C,cAAa;CACb,MAAMC,WAAgC,CAAE;AACxC,MAAK,MAAM,cAAc,kBAAkB;AACzC,WAAS,KAAK,WAAW,OAAO,eAAe,CAAC;AAChD,mBAAiB,OAAO,WAAW;CACpC;AACD,OAAM,QAAQ,IAAI,SAAS;AAC5B;;;;;;AAOD,SAAgB,cAAoB;AAClC,MAAK,MAAM,cAAc,YAAa,YAAW,OAAO,UAAU;AAClE,aAAY,OAAO;AACpB;;;;AAKD,IAAa,cAAb,cAAiC,MAAM;;;;;CAKrC,YAAYC,SAAiB;AAC3B,QAAM,QAAQ;AACd,OAAK,OAAO;CACb;AACF"}
|
|
1
|
+
{"version":3,"file":"config.js","names":["currentConfig: Config<string, string> | null","strongRefs: Set<LoggerImpl>","disposables: Set<Disposable>","asyncDisposables: Set<AsyncDisposable>","cfg: LoggerConfig<TSinkId, TFilterId>","config: Config<TSinkId, TFilterId>","allowAsync: boolean","promises: PromiseLike<void>[]","message: string"],"sources":["../src/config.ts"],"sourcesContent":["import type { ContextLocalStorage } from \"./context.ts\";\nimport { type FilterLike, toFilter } from \"./filter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport { LoggerImpl } from \"./logger.ts\";\nimport { getConsoleSink, type Sink } from \"./sink.ts\";\n\n/**\n * A configuration for the loggers.\n */\nexport interface Config<TSinkId extends string, TFilterId extends string> {\n /**\n * The sinks to use. The keys are the sink identifiers, and the values are\n * {@link Sink}s.\n */\n sinks: Record<TSinkId, Sink>;\n /**\n * The filters to use. The keys are the filter identifiers, and the values\n * are either {@link Filter}s or {@link LogLevel}s.\n */\n filters?: Record<TFilterId, FilterLike>;\n\n /**\n * The loggers to configure.\n */\n loggers: LoggerConfig<TSinkId, TFilterId>[];\n\n /**\n * The context-local storage to use for implicit contexts.\n * @since 0.7.0\n */\n contextLocalStorage?: ContextLocalStorage<Record<string, unknown>>;\n\n /**\n * Whether to reset the configuration before applying this one.\n */\n reset?: boolean;\n}\n\n/**\n * A logger configuration.\n */\nexport interface LoggerConfig<\n TSinkId extends string,\n TFilterId extends string,\n> {\n /**\n * The category of the logger. If a string, it is equivalent to an array\n * with one element.\n */\n category: string | string[];\n\n /**\n * The sink identifiers to use.\n */\n sinks?: TSinkId[];\n\n /**\n * Whether to inherit the parent's sinks. If `inherit`, the parent's sinks\n * are used along with the specified sinks. If `override`, the parent's\n * sinks are not used, and only the specified sinks are used.\n *\n * The default is `inherit`.\n * @default `\"inherit\"\n * @since 0.6.0\n */\n parentSinks?: \"inherit\" | \"override\";\n\n /**\n * The filter identifiers to use.\n */\n filters?: TFilterId[];\n\n /**\n * The lowest log level to accept. If `null`, the logger will reject all\n * records.\n * @since 0.8.0\n */\n lowestLevel?: LogLevel | null;\n}\n\n/**\n * The current configuration, if any. Otherwise, `null`.\n */\nlet currentConfig: Config<string, string> | null = null;\n\n/**\n * Strong references to the loggers.\n * This is to prevent the loggers from being garbage collected so that their\n * sinks and filters are not removed.\n */\nconst strongRefs: Set<LoggerImpl> = new Set();\n\n/**\n * Disposables to dispose when resetting the configuration.\n */\nconst disposables: Set<Disposable> = new Set();\n\n/**\n * Async disposables to dispose when resetting the configuration.\n */\nconst asyncDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Check if a config is for the meta logger.\n */\nfunction isLoggerConfigMeta<TSinkId extends string, TFilterId extends string>(\n cfg: LoggerConfig<TSinkId, TFilterId>,\n): boolean {\n return cfg.category.length === 0 ||\n (cfg.category.length === 1 && cfg.category[0] === \"logtape\") ||\n (cfg.category.length === 2 &&\n cfg.category[0] === \"logtape\" &&\n cfg.category[1] === \"meta\");\n}\n\n/**\n * Configure the loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * @example\n * ```typescript\n * await configure({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * filters: {\n * slow: (log) =>\n * \"duration\" in log.properties &&\n * log.properties.duration as number > 1000,\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: [\"my-app\", \"sql\"],\n * filters: [\"slow\"],\n * lowestLevel: \"debug\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n */\nexport async function configure<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>): Promise<void> {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n await reset();\n try {\n configureInternal(config, true);\n } catch (e) {\n if (e instanceof ConfigError) await reset();\n throw e;\n }\n}\n\n/**\n * Configure sync loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * Also note that passing async sinks or filters will throw. If\n * necessary use {@link resetSync} or {@link disposeSync}.\n *\n * @example\n * ```typescript\n * configureSync({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n * @since 0.9.0\n */\nexport function configureSync<TSinkId extends string, TFilterId extends string>(\n config: Config<TSinkId, TFilterId>,\n): void {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n if (asyncDisposables.size > 0) {\n throw new ConfigError(\n \"Previously configured async disposables are still active. \" +\n \"Use configure() instead or explicitly dispose them using dispose().\",\n );\n }\n resetSync();\n try {\n configureInternal(config, false);\n } catch (e) {\n if (e instanceof ConfigError) resetSync();\n throw e;\n }\n}\n\nfunction configureInternal<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>, allowAsync: boolean): void {\n currentConfig = config;\n\n let metaConfigured = false;\n const configuredCategories = new Set<string>();\n\n for (const cfg of config.loggers) {\n if (isLoggerConfigMeta(cfg)) {\n metaConfigured = true;\n }\n\n // Check for duplicate logger categories\n const categoryKey = Array.isArray(cfg.category)\n ? JSON.stringify(cfg.category)\n : JSON.stringify([cfg.category]);\n if (configuredCategories.has(categoryKey)) {\n throw new ConfigError(\n `Duplicate logger configuration for category: ${categoryKey}. ` +\n `Each category can only be configured once.`,\n );\n }\n configuredCategories.add(categoryKey);\n\n const logger = LoggerImpl.getLogger(cfg.category);\n for (const sinkId of cfg.sinks ?? []) {\n const sink = config.sinks[sinkId];\n if (!sink) {\n throw new ConfigError(`Sink not found: ${sinkId}.`);\n }\n logger.sinks.push(sink);\n }\n logger.parentSinks = cfg.parentSinks ?? \"inherit\";\n if (cfg.lowestLevel !== undefined) {\n logger.lowestLevel = cfg.lowestLevel;\n }\n for (const filterId of cfg.filters ?? []) {\n const filter = config.filters?.[filterId];\n if (filter === undefined) {\n throw new ConfigError(`Filter not found: ${filterId}.`);\n }\n logger.filters.push(toFilter(filter));\n }\n strongRefs.add(logger);\n }\n\n LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;\n\n for (const sink of Object.values<Sink>(config.sinks)) {\n if (Symbol.asyncDispose in sink) {\n if (allowAsync) asyncDisposables.add(sink as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in sink) disposables.add(sink as Disposable);\n }\n\n for (const filter of Object.values<FilterLike>(config.filters ?? {})) {\n if (filter == null || typeof filter === \"string\") continue;\n if (Symbol.asyncDispose in filter) {\n if (allowAsync) asyncDisposables.add(filter as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in filter) disposables.add(filter as Disposable);\n }\n\n if (\n // deno-lint-ignore no-explicit-any\n typeof (globalThis as any).EdgeRuntime !== \"string\" &&\n \"process\" in globalThis &&\n !(\"Deno\" in globalThis)\n ) {\n // deno-lint-ignore no-explicit-any\n const proc = (globalThis as any).process;\n // Use bracket notation to avoid static analysis detection in Edge Runtime\n const onMethod = proc?.[\"on\"];\n if (typeof onMethod === \"function\") {\n onMethod.call(proc, \"exit\", allowAsync ? dispose : disposeSync);\n }\n } else {\n // @ts-ignore: It's fine to addEventListener() on the browser/Deno/Edge Runtime\n addEventListener(\"unload\", allowAsync ? dispose : disposeSync);\n }\n const meta = LoggerImpl.getLogger([\"logtape\", \"meta\"]);\n if (!metaConfigured) {\n meta.sinks.push(getConsoleSink());\n }\n\n meta.info(\n \"LogTape loggers are configured. Note that LogTape itself uses the meta \" +\n \"logger, which has category {metaLoggerCategory}. The meta logger \" +\n \"purposes to log internal errors such as sink exceptions. If you \" +\n \"are seeing this message, the meta logger is automatically configured. \" +\n \"It's recommended to configure the meta logger with a separate sink \" +\n \"so that you can easily notice if logging itself fails or is \" +\n \"misconfigured. To turn off this message, configure the meta logger \" +\n \"with higher log levels than {dismissLevel}. See also \" +\n \"<https://logtape.org/manual/categories#meta-logger>.\",\n { metaLoggerCategory: [\"logtape\", \"meta\"], dismissLevel: \"info\" },\n );\n}\n\n/**\n * Get the current configuration, if any. Otherwise, `null`.\n * @returns The current configuration, if any. Otherwise, `null`.\n */\nexport function getConfig(): Config<string, string> | null {\n return currentConfig;\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes.\n */\nexport async function reset(): Promise<void> {\n await dispose();\n resetInternal();\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes. Will not clear async\n * sinks, only use with sync sinks. Use {@link reset} if you have async sinks.\n * @since 0.9.0\n */\nexport function resetSync(): void {\n disposeSync();\n resetInternal();\n}\n\nfunction resetInternal(): void {\n const rootLogger = LoggerImpl.getLogger([]);\n rootLogger.resetDescendants();\n delete rootLogger.contextLocalStorage;\n strongRefs.clear();\n currentConfig = null;\n}\n\n/**\n * Dispose of the disposables.\n */\nexport async function dispose(): Promise<void> {\n disposeSync();\n const promises: PromiseLike<void>[] = [];\n for (const disposable of asyncDisposables) {\n promises.push(disposable[Symbol.asyncDispose]());\n asyncDisposables.delete(disposable);\n }\n await Promise.all(promises);\n}\n\n/**\n * Dispose of the sync disposables. Async disposables will be untouched,\n * use {@link dispose} if you have async sinks.\n * @since 0.9.0\n */\nexport function disposeSync(): void {\n for (const disposable of disposables) disposable[Symbol.dispose]();\n disposables.clear();\n}\n\n/**\n * A configuration error.\n */\nexport class ConfigError extends Error {\n /**\n * Constructs a new configuration error.\n * @param message The error message.\n */\n constructor(message: string) {\n super(message);\n this.name = \"ConfigureError\";\n }\n}\n"],"mappings":";;;;;;;;AAmFA,IAAIA,gBAA+C;;;;;;AAOnD,MAAMC,6BAA8B,IAAI;;;;AAKxC,MAAMC,8BAA+B,IAAI;;;;AAKzC,MAAMC,mCAAyC,IAAI;;;;AAKnD,SAAS,mBACPC,KACS;AACT,QAAO,IAAI,SAAS,WAAW,KAC5B,IAAI,SAAS,WAAW,KAAK,IAAI,SAAS,OAAO,aACjD,IAAI,SAAS,WAAW,KACvB,IAAI,SAAS,OAAO,aACpB,IAAI,SAAS,OAAO;AACzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AED,SAAS,gBAAsB;CAC7B,MAAM,aAAa,WAAW,UAAU,CAAE,EAAC;AAC3C,YAAW,kBAAkB;AAC7B,QAAO,WAAW;AAClB,YAAW,OAAO;AAClB,iBAAgB;AACjB;;;;AAKD,eAAsB,UAAyB;AAC7C,cAAa;CACb,MAAMC,WAAgC,CAAE;AACxC,MAAK,MAAM,cAAc,kBAAkB;AACzC,WAAS,KAAK,WAAW,OAAO,eAAe,CAAC;AAChD,mBAAiB,OAAO,WAAW;CACpC;AACD,OAAM,QAAQ,IAAI,SAAS;AAC5B;;;;;;AAOD,SAAgB,cAAoB;AAClC,MAAK,MAAM,cAAc,YAAa,YAAW,OAAO,UAAU;AAClE,aAAY,OAAO;AACpB;;;;AAKD,IAAa,cAAb,cAAiC,MAAM;;;;;CAKrC,YAAYC,SAAiB;AAC3B,QAAM,QAAQ;AACd,OAAK,OAAO;CACb;AACF"}
|
package/dist/logger.cjs
CHANGED
|
@@ -330,10 +330,227 @@ var LoggerCtx = class LoggerCtx {
|
|
|
330
330
|
*/
|
|
331
331
|
const metaLogger = LoggerImpl.getLogger(["logtape", "meta"]);
|
|
332
332
|
/**
|
|
333
|
+
* Check if a property access key contains nested access patterns.
|
|
334
|
+
* @param key The property key to check.
|
|
335
|
+
* @returns True if the key contains nested access patterns.
|
|
336
|
+
*/
|
|
337
|
+
function isNestedAccess(key) {
|
|
338
|
+
return key.includes(".") || key.includes("[") || key.includes("?.");
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Safely access an own property from an object, blocking prototype pollution.
|
|
342
|
+
*
|
|
343
|
+
* @param obj The object to access the property from.
|
|
344
|
+
* @param key The property key to access.
|
|
345
|
+
* @returns The property value or undefined if not accessible.
|
|
346
|
+
*/
|
|
347
|
+
function getOwnProperty(obj, key) {
|
|
348
|
+
if (key === "__proto__" || key === "prototype" || key === "constructor") return void 0;
|
|
349
|
+
if ((typeof obj === "object" || typeof obj === "function") && obj !== null) return Object.prototype.hasOwnProperty.call(obj, key) ? obj[key] : void 0;
|
|
350
|
+
return void 0;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Parse the next segment from a property path string.
|
|
354
|
+
*
|
|
355
|
+
* @param path The full property path string.
|
|
356
|
+
* @param fromIndex The index to start parsing from.
|
|
357
|
+
* @returns The parsed segment and next index, or null if parsing fails.
|
|
358
|
+
*/
|
|
359
|
+
function parseNextSegment(path, fromIndex) {
|
|
360
|
+
const len = path.length;
|
|
361
|
+
let i = fromIndex;
|
|
362
|
+
if (i >= len) return null;
|
|
363
|
+
let segment;
|
|
364
|
+
if (path[i] === "[") {
|
|
365
|
+
i++;
|
|
366
|
+
if (i >= len) return null;
|
|
367
|
+
if (path[i] === "\"" || path[i] === "'") {
|
|
368
|
+
const quote = path[i];
|
|
369
|
+
i++;
|
|
370
|
+
let segmentStr = "";
|
|
371
|
+
while (i < len && path[i] !== quote) if (path[i] === "\\") {
|
|
372
|
+
i++;
|
|
373
|
+
if (i < len) {
|
|
374
|
+
const escapeChar = path[i];
|
|
375
|
+
switch (escapeChar) {
|
|
376
|
+
case "n":
|
|
377
|
+
segmentStr += "\n";
|
|
378
|
+
break;
|
|
379
|
+
case "t":
|
|
380
|
+
segmentStr += " ";
|
|
381
|
+
break;
|
|
382
|
+
case "r":
|
|
383
|
+
segmentStr += "\r";
|
|
384
|
+
break;
|
|
385
|
+
case "b":
|
|
386
|
+
segmentStr += "\b";
|
|
387
|
+
break;
|
|
388
|
+
case "f":
|
|
389
|
+
segmentStr += "\f";
|
|
390
|
+
break;
|
|
391
|
+
case "v":
|
|
392
|
+
segmentStr += "\v";
|
|
393
|
+
break;
|
|
394
|
+
case "0":
|
|
395
|
+
segmentStr += "\0";
|
|
396
|
+
break;
|
|
397
|
+
case "\\":
|
|
398
|
+
segmentStr += "\\";
|
|
399
|
+
break;
|
|
400
|
+
case "\"":
|
|
401
|
+
segmentStr += "\"";
|
|
402
|
+
break;
|
|
403
|
+
case "'":
|
|
404
|
+
segmentStr += "'";
|
|
405
|
+
break;
|
|
406
|
+
case "u":
|
|
407
|
+
if (i + 4 < len) {
|
|
408
|
+
const hex = path.slice(i + 1, i + 5);
|
|
409
|
+
const codePoint = Number.parseInt(hex, 16);
|
|
410
|
+
if (!Number.isNaN(codePoint)) {
|
|
411
|
+
segmentStr += String.fromCharCode(codePoint);
|
|
412
|
+
i += 4;
|
|
413
|
+
} else segmentStr += escapeChar;
|
|
414
|
+
} else segmentStr += escapeChar;
|
|
415
|
+
break;
|
|
416
|
+
default: segmentStr += escapeChar;
|
|
417
|
+
}
|
|
418
|
+
i++;
|
|
419
|
+
}
|
|
420
|
+
} else {
|
|
421
|
+
segmentStr += path[i];
|
|
422
|
+
i++;
|
|
423
|
+
}
|
|
424
|
+
if (i >= len) return null;
|
|
425
|
+
segment = segmentStr;
|
|
426
|
+
i++;
|
|
427
|
+
} else {
|
|
428
|
+
const startIndex = i;
|
|
429
|
+
while (i < len && path[i] !== "]" && path[i] !== "'" && path[i] !== "\"") i++;
|
|
430
|
+
if (i >= len) return null;
|
|
431
|
+
const indexStr = path.slice(startIndex, i);
|
|
432
|
+
if (indexStr.length === 0) return null;
|
|
433
|
+
const indexNum = Number(indexStr);
|
|
434
|
+
segment = Number.isNaN(indexNum) ? indexStr : indexNum;
|
|
435
|
+
}
|
|
436
|
+
while (i < len && path[i] !== "]") i++;
|
|
437
|
+
if (i < len) i++;
|
|
438
|
+
} else {
|
|
439
|
+
const startIndex = i;
|
|
440
|
+
while (i < len && path[i] !== "." && path[i] !== "[" && path[i] !== "?" && path[i] !== "]") i++;
|
|
441
|
+
segment = path.slice(startIndex, i);
|
|
442
|
+
if (segment.length === 0) return null;
|
|
443
|
+
}
|
|
444
|
+
if (i < len && path[i] === ".") i++;
|
|
445
|
+
return {
|
|
446
|
+
segment,
|
|
447
|
+
nextIndex: i
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Access a property or index on an object or array.
|
|
452
|
+
*
|
|
453
|
+
* @param obj The object or array to access.
|
|
454
|
+
* @param segment The property key or array index.
|
|
455
|
+
* @returns The accessed value or undefined if not accessible.
|
|
456
|
+
*/
|
|
457
|
+
function accessProperty(obj, segment) {
|
|
458
|
+
if (typeof segment === "string") return getOwnProperty(obj, segment);
|
|
459
|
+
if (Array.isArray(obj) && segment >= 0 && segment < obj.length) return obj[segment];
|
|
460
|
+
return void 0;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Resolve a nested property path from an object.
|
|
464
|
+
*
|
|
465
|
+
* There are two types of property access patterns:
|
|
466
|
+
* 1. Array/index access: [0] or ["prop"]
|
|
467
|
+
* 2. Property access: prop or prop?.next
|
|
468
|
+
*
|
|
469
|
+
* @param obj The object to traverse.
|
|
470
|
+
* @param path The property path (e.g., "user.name", "users[0].email", "user['full-name']").
|
|
471
|
+
* @returns The resolved value or undefined if path doesn't exist.
|
|
472
|
+
*/
|
|
473
|
+
function resolvePropertyPath(obj, path) {
|
|
474
|
+
if (obj == null) return void 0;
|
|
475
|
+
if (path.length === 0 || path.endsWith(".")) return void 0;
|
|
476
|
+
let current = obj;
|
|
477
|
+
let i = 0;
|
|
478
|
+
const len = path.length;
|
|
479
|
+
while (i < len) {
|
|
480
|
+
const isOptional = path.slice(i, i + 2) === "?.";
|
|
481
|
+
if (isOptional) {
|
|
482
|
+
i += 2;
|
|
483
|
+
if (current == null) return void 0;
|
|
484
|
+
} else if (current == null) return void 0;
|
|
485
|
+
const result = parseNextSegment(path, i);
|
|
486
|
+
if (result === null) return void 0;
|
|
487
|
+
const { segment, nextIndex } = result;
|
|
488
|
+
i = nextIndex;
|
|
489
|
+
current = accessProperty(current, segment);
|
|
490
|
+
if (current === void 0) return void 0;
|
|
491
|
+
}
|
|
492
|
+
return current;
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
333
495
|
* Parse a message template into a message template array and a values array.
|
|
334
|
-
*
|
|
496
|
+
*
|
|
497
|
+
* Placeholders to be replaced with `values` are indicated by keys in curly braces
|
|
498
|
+
* (e.g., `{value}`). The system supports both simple property access and nested
|
|
499
|
+
* property access patterns:
|
|
500
|
+
*
|
|
501
|
+
* **Simple property access:**
|
|
502
|
+
* ```ts
|
|
503
|
+
* parseMessageTemplate("Hello, {user}!", { user: "foo" })
|
|
504
|
+
* // Returns: ["Hello, ", "foo", "!"]
|
|
505
|
+
* ```
|
|
506
|
+
*
|
|
507
|
+
* **Nested property access (dot notation):**
|
|
508
|
+
* ```ts
|
|
509
|
+
* parseMessageTemplate("Hello, {user.name}!", {
|
|
510
|
+
* user: { name: "foo", email: "foo@example.com" }
|
|
511
|
+
* })
|
|
512
|
+
* // Returns: ["Hello, ", "foo", "!"]
|
|
513
|
+
* ```
|
|
514
|
+
*
|
|
515
|
+
* **Array indexing:**
|
|
516
|
+
* ```ts
|
|
517
|
+
* parseMessageTemplate("First: {users[0]}", {
|
|
518
|
+
* users: ["foo", "bar", "baz"]
|
|
519
|
+
* })
|
|
520
|
+
* // Returns: ["First: ", "foo", ""]
|
|
521
|
+
* ```
|
|
522
|
+
*
|
|
523
|
+
* **Bracket notation for special property names:**
|
|
524
|
+
* ```ts
|
|
525
|
+
* parseMessageTemplate("Name: {user[\"full-name\"]}", {
|
|
526
|
+
* user: { "full-name": "foo bar" }
|
|
527
|
+
* })
|
|
528
|
+
* // Returns: ["Name: ", "foo bar", ""]
|
|
529
|
+
* ```
|
|
530
|
+
*
|
|
531
|
+
* **Optional chaining for safe navigation:**
|
|
532
|
+
* ```ts
|
|
533
|
+
* parseMessageTemplate("Email: {user?.profile?.email}", {
|
|
534
|
+
* user: { name: "foo" }
|
|
535
|
+
* })
|
|
536
|
+
* // Returns: ["Email: ", undefined, ""]
|
|
537
|
+
* ```
|
|
538
|
+
*
|
|
539
|
+
* **Wildcard patterns:**
|
|
540
|
+
* - `{*}` - Replaced with the entire properties object
|
|
541
|
+
* - `{ key-with-whitespace }` - Whitespace is trimmed when looking up keys
|
|
542
|
+
*
|
|
543
|
+
* **Escaping:**
|
|
544
|
+
* - `{{` and `}}` are escaped literal braces
|
|
545
|
+
*
|
|
546
|
+
* **Error handling:**
|
|
547
|
+
* - Non-existent paths return `undefined`
|
|
548
|
+
* - Malformed expressions resolve to `undefined` without throwing errors
|
|
549
|
+
* - Out of bounds array access returns `undefined`
|
|
550
|
+
*
|
|
551
|
+
* @param template The message template string containing placeholders.
|
|
335
552
|
* @param properties The values to replace placeholders with.
|
|
336
|
-
* @returns The message template array
|
|
553
|
+
* @returns The message template array with values interleaved between text segments.
|
|
337
554
|
*/
|
|
338
555
|
function parseMessageTemplate(template, properties) {
|
|
339
556
|
const length = template.length;
|
|
@@ -357,8 +574,11 @@ function parseMessageTemplate(template, properties) {
|
|
|
357
574
|
let prop;
|
|
358
575
|
const trimmedKey = key.trim();
|
|
359
576
|
if (trimmedKey === "*") prop = key in properties ? properties[key] : "*" in properties ? properties["*"] : properties;
|
|
360
|
-
else
|
|
361
|
-
|
|
577
|
+
else {
|
|
578
|
+
if (key !== trimmedKey) prop = key in properties ? properties[key] : properties[trimmedKey];
|
|
579
|
+
else prop = properties[key];
|
|
580
|
+
if (prop === void 0 && isNestedAccess(trimmedKey)) prop = resolvePropertyPath(properties, trimmedKey);
|
|
581
|
+
}
|
|
362
582
|
message.push(prop);
|
|
363
583
|
i = closeIndex;
|
|
364
584
|
startIndex = i + 1;
|
package/dist/logger.js
CHANGED
|
@@ -330,10 +330,227 @@ var LoggerCtx = class LoggerCtx {
|
|
|
330
330
|
*/
|
|
331
331
|
const metaLogger = LoggerImpl.getLogger(["logtape", "meta"]);
|
|
332
332
|
/**
|
|
333
|
+
* Check if a property access key contains nested access patterns.
|
|
334
|
+
* @param key The property key to check.
|
|
335
|
+
* @returns True if the key contains nested access patterns.
|
|
336
|
+
*/
|
|
337
|
+
function isNestedAccess(key) {
|
|
338
|
+
return key.includes(".") || key.includes("[") || key.includes("?.");
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Safely access an own property from an object, blocking prototype pollution.
|
|
342
|
+
*
|
|
343
|
+
* @param obj The object to access the property from.
|
|
344
|
+
* @param key The property key to access.
|
|
345
|
+
* @returns The property value or undefined if not accessible.
|
|
346
|
+
*/
|
|
347
|
+
function getOwnProperty(obj, key) {
|
|
348
|
+
if (key === "__proto__" || key === "prototype" || key === "constructor") return void 0;
|
|
349
|
+
if ((typeof obj === "object" || typeof obj === "function") && obj !== null) return Object.prototype.hasOwnProperty.call(obj, key) ? obj[key] : void 0;
|
|
350
|
+
return void 0;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Parse the next segment from a property path string.
|
|
354
|
+
*
|
|
355
|
+
* @param path The full property path string.
|
|
356
|
+
* @param fromIndex The index to start parsing from.
|
|
357
|
+
* @returns The parsed segment and next index, or null if parsing fails.
|
|
358
|
+
*/
|
|
359
|
+
function parseNextSegment(path, fromIndex) {
|
|
360
|
+
const len = path.length;
|
|
361
|
+
let i = fromIndex;
|
|
362
|
+
if (i >= len) return null;
|
|
363
|
+
let segment;
|
|
364
|
+
if (path[i] === "[") {
|
|
365
|
+
i++;
|
|
366
|
+
if (i >= len) return null;
|
|
367
|
+
if (path[i] === "\"" || path[i] === "'") {
|
|
368
|
+
const quote = path[i];
|
|
369
|
+
i++;
|
|
370
|
+
let segmentStr = "";
|
|
371
|
+
while (i < len && path[i] !== quote) if (path[i] === "\\") {
|
|
372
|
+
i++;
|
|
373
|
+
if (i < len) {
|
|
374
|
+
const escapeChar = path[i];
|
|
375
|
+
switch (escapeChar) {
|
|
376
|
+
case "n":
|
|
377
|
+
segmentStr += "\n";
|
|
378
|
+
break;
|
|
379
|
+
case "t":
|
|
380
|
+
segmentStr += " ";
|
|
381
|
+
break;
|
|
382
|
+
case "r":
|
|
383
|
+
segmentStr += "\r";
|
|
384
|
+
break;
|
|
385
|
+
case "b":
|
|
386
|
+
segmentStr += "\b";
|
|
387
|
+
break;
|
|
388
|
+
case "f":
|
|
389
|
+
segmentStr += "\f";
|
|
390
|
+
break;
|
|
391
|
+
case "v":
|
|
392
|
+
segmentStr += "\v";
|
|
393
|
+
break;
|
|
394
|
+
case "0":
|
|
395
|
+
segmentStr += "\0";
|
|
396
|
+
break;
|
|
397
|
+
case "\\":
|
|
398
|
+
segmentStr += "\\";
|
|
399
|
+
break;
|
|
400
|
+
case "\"":
|
|
401
|
+
segmentStr += "\"";
|
|
402
|
+
break;
|
|
403
|
+
case "'":
|
|
404
|
+
segmentStr += "'";
|
|
405
|
+
break;
|
|
406
|
+
case "u":
|
|
407
|
+
if (i + 4 < len) {
|
|
408
|
+
const hex = path.slice(i + 1, i + 5);
|
|
409
|
+
const codePoint = Number.parseInt(hex, 16);
|
|
410
|
+
if (!Number.isNaN(codePoint)) {
|
|
411
|
+
segmentStr += String.fromCharCode(codePoint);
|
|
412
|
+
i += 4;
|
|
413
|
+
} else segmentStr += escapeChar;
|
|
414
|
+
} else segmentStr += escapeChar;
|
|
415
|
+
break;
|
|
416
|
+
default: segmentStr += escapeChar;
|
|
417
|
+
}
|
|
418
|
+
i++;
|
|
419
|
+
}
|
|
420
|
+
} else {
|
|
421
|
+
segmentStr += path[i];
|
|
422
|
+
i++;
|
|
423
|
+
}
|
|
424
|
+
if (i >= len) return null;
|
|
425
|
+
segment = segmentStr;
|
|
426
|
+
i++;
|
|
427
|
+
} else {
|
|
428
|
+
const startIndex = i;
|
|
429
|
+
while (i < len && path[i] !== "]" && path[i] !== "'" && path[i] !== "\"") i++;
|
|
430
|
+
if (i >= len) return null;
|
|
431
|
+
const indexStr = path.slice(startIndex, i);
|
|
432
|
+
if (indexStr.length === 0) return null;
|
|
433
|
+
const indexNum = Number(indexStr);
|
|
434
|
+
segment = Number.isNaN(indexNum) ? indexStr : indexNum;
|
|
435
|
+
}
|
|
436
|
+
while (i < len && path[i] !== "]") i++;
|
|
437
|
+
if (i < len) i++;
|
|
438
|
+
} else {
|
|
439
|
+
const startIndex = i;
|
|
440
|
+
while (i < len && path[i] !== "." && path[i] !== "[" && path[i] !== "?" && path[i] !== "]") i++;
|
|
441
|
+
segment = path.slice(startIndex, i);
|
|
442
|
+
if (segment.length === 0) return null;
|
|
443
|
+
}
|
|
444
|
+
if (i < len && path[i] === ".") i++;
|
|
445
|
+
return {
|
|
446
|
+
segment,
|
|
447
|
+
nextIndex: i
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Access a property or index on an object or array.
|
|
452
|
+
*
|
|
453
|
+
* @param obj The object or array to access.
|
|
454
|
+
* @param segment The property key or array index.
|
|
455
|
+
* @returns The accessed value or undefined if not accessible.
|
|
456
|
+
*/
|
|
457
|
+
function accessProperty(obj, segment) {
|
|
458
|
+
if (typeof segment === "string") return getOwnProperty(obj, segment);
|
|
459
|
+
if (Array.isArray(obj) && segment >= 0 && segment < obj.length) return obj[segment];
|
|
460
|
+
return void 0;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Resolve a nested property path from an object.
|
|
464
|
+
*
|
|
465
|
+
* There are two types of property access patterns:
|
|
466
|
+
* 1. Array/index access: [0] or ["prop"]
|
|
467
|
+
* 2. Property access: prop or prop?.next
|
|
468
|
+
*
|
|
469
|
+
* @param obj The object to traverse.
|
|
470
|
+
* @param path The property path (e.g., "user.name", "users[0].email", "user['full-name']").
|
|
471
|
+
* @returns The resolved value or undefined if path doesn't exist.
|
|
472
|
+
*/
|
|
473
|
+
function resolvePropertyPath(obj, path) {
|
|
474
|
+
if (obj == null) return void 0;
|
|
475
|
+
if (path.length === 0 || path.endsWith(".")) return void 0;
|
|
476
|
+
let current = obj;
|
|
477
|
+
let i = 0;
|
|
478
|
+
const len = path.length;
|
|
479
|
+
while (i < len) {
|
|
480
|
+
const isOptional = path.slice(i, i + 2) === "?.";
|
|
481
|
+
if (isOptional) {
|
|
482
|
+
i += 2;
|
|
483
|
+
if (current == null) return void 0;
|
|
484
|
+
} else if (current == null) return void 0;
|
|
485
|
+
const result = parseNextSegment(path, i);
|
|
486
|
+
if (result === null) return void 0;
|
|
487
|
+
const { segment, nextIndex } = result;
|
|
488
|
+
i = nextIndex;
|
|
489
|
+
current = accessProperty(current, segment);
|
|
490
|
+
if (current === void 0) return void 0;
|
|
491
|
+
}
|
|
492
|
+
return current;
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
333
495
|
* Parse a message template into a message template array and a values array.
|
|
334
|
-
*
|
|
496
|
+
*
|
|
497
|
+
* Placeholders to be replaced with `values` are indicated by keys in curly braces
|
|
498
|
+
* (e.g., `{value}`). The system supports both simple property access and nested
|
|
499
|
+
* property access patterns:
|
|
500
|
+
*
|
|
501
|
+
* **Simple property access:**
|
|
502
|
+
* ```ts
|
|
503
|
+
* parseMessageTemplate("Hello, {user}!", { user: "foo" })
|
|
504
|
+
* // Returns: ["Hello, ", "foo", "!"]
|
|
505
|
+
* ```
|
|
506
|
+
*
|
|
507
|
+
* **Nested property access (dot notation):**
|
|
508
|
+
* ```ts
|
|
509
|
+
* parseMessageTemplate("Hello, {user.name}!", {
|
|
510
|
+
* user: { name: "foo", email: "foo@example.com" }
|
|
511
|
+
* })
|
|
512
|
+
* // Returns: ["Hello, ", "foo", "!"]
|
|
513
|
+
* ```
|
|
514
|
+
*
|
|
515
|
+
* **Array indexing:**
|
|
516
|
+
* ```ts
|
|
517
|
+
* parseMessageTemplate("First: {users[0]}", {
|
|
518
|
+
* users: ["foo", "bar", "baz"]
|
|
519
|
+
* })
|
|
520
|
+
* // Returns: ["First: ", "foo", ""]
|
|
521
|
+
* ```
|
|
522
|
+
*
|
|
523
|
+
* **Bracket notation for special property names:**
|
|
524
|
+
* ```ts
|
|
525
|
+
* parseMessageTemplate("Name: {user[\"full-name\"]}", {
|
|
526
|
+
* user: { "full-name": "foo bar" }
|
|
527
|
+
* })
|
|
528
|
+
* // Returns: ["Name: ", "foo bar", ""]
|
|
529
|
+
* ```
|
|
530
|
+
*
|
|
531
|
+
* **Optional chaining for safe navigation:**
|
|
532
|
+
* ```ts
|
|
533
|
+
* parseMessageTemplate("Email: {user?.profile?.email}", {
|
|
534
|
+
* user: { name: "foo" }
|
|
535
|
+
* })
|
|
536
|
+
* // Returns: ["Email: ", undefined, ""]
|
|
537
|
+
* ```
|
|
538
|
+
*
|
|
539
|
+
* **Wildcard patterns:**
|
|
540
|
+
* - `{*}` - Replaced with the entire properties object
|
|
541
|
+
* - `{ key-with-whitespace }` - Whitespace is trimmed when looking up keys
|
|
542
|
+
*
|
|
543
|
+
* **Escaping:**
|
|
544
|
+
* - `{{` and `}}` are escaped literal braces
|
|
545
|
+
*
|
|
546
|
+
* **Error handling:**
|
|
547
|
+
* - Non-existent paths return `undefined`
|
|
548
|
+
* - Malformed expressions resolve to `undefined` without throwing errors
|
|
549
|
+
* - Out of bounds array access returns `undefined`
|
|
550
|
+
*
|
|
551
|
+
* @param template The message template string containing placeholders.
|
|
335
552
|
* @param properties The values to replace placeholders with.
|
|
336
|
-
* @returns The message template array
|
|
553
|
+
* @returns The message template array with values interleaved between text segments.
|
|
337
554
|
*/
|
|
338
555
|
function parseMessageTemplate(template, properties) {
|
|
339
556
|
const length = template.length;
|
|
@@ -357,8 +574,11 @@ function parseMessageTemplate(template, properties) {
|
|
|
357
574
|
let prop;
|
|
358
575
|
const trimmedKey = key.trim();
|
|
359
576
|
if (trimmedKey === "*") prop = key in properties ? properties[key] : "*" in properties ? properties["*"] : properties;
|
|
360
|
-
else
|
|
361
|
-
|
|
577
|
+
else {
|
|
578
|
+
if (key !== trimmedKey) prop = key in properties ? properties[key] : properties[trimmedKey];
|
|
579
|
+
else prop = properties[key];
|
|
580
|
+
if (prop === void 0 && isNestedAccess(trimmedKey)) prop = resolvePropertyPath(properties, trimmedKey);
|
|
581
|
+
}
|
|
362
582
|
message.push(prop);
|
|
363
583
|
i = closeIndex;
|
|
364
584
|
startIndex = i + 1;
|