@eliware/common 15.0.0 → 15.2.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.
Files changed (193) hide show
  1. package/AGENTS.md +7 -5
  2. package/README.md +12 -12
  3. package/RELEASE_NOTES.md +54 -0
  4. package/docs/README.md +10 -4
  5. package/docs/api.md +35 -164
  6. package/docs/logging.md +66 -0
  7. package/docs/package.md +44 -30
  8. package/docs/paths.md +57 -0
  9. package/docs/process-errors.md +62 -0
  10. package/docs/redaction.md +115 -0
  11. package/docs/signals.md +67 -0
  12. package/docs/snowflake-ids.md +77 -0
  13. package/docs/troubleshooting.md +33 -29
  14. package/docs/usage.md +42 -138
  15. package/package.json +28 -8
  16. package/specs/README.md +6 -0
  17. package/specs/directives.yaml +11 -6
  18. package/specs/errors.yaml +23 -0
  19. package/specs/log.yaml +19 -0
  20. package/specs/path.yaml +18 -0
  21. package/specs/redact.yaml +20 -0
  22. package/specs/signals.yaml +16 -0
  23. package/specs/snowflake.yaml +18 -0
  24. package/src/errors/cleanup/abort-listener.mjs +19 -0
  25. package/src/errors/cleanup/attached-handlers.mjs +30 -0
  26. package/src/errors/cleanup/detached-state.mjs +8 -0
  27. package/src/errors/cleanup/finalize.mjs +5 -0
  28. package/src/errors/cleanup/listener.mjs +27 -0
  29. package/src/errors/cleanup.mjs +43 -0
  30. package/src/errors/constants.mjs +1 -0
  31. package/src/errors/event/handler.mjs +38 -0
  32. package/src/errors/event/one-shot.mjs +37 -0
  33. package/src/errors/event-handlers.mjs +33 -0
  34. package/src/errors/failure.mjs +10 -0
  35. package/src/errors/finalize.mjs +27 -0
  36. package/src/errors/handler-removal.mjs +18 -0
  37. package/src/errors/index.d.mts +28 -0
  38. package/src/errors/index.d.ts +28 -0
  39. package/src/errors/index.mjs +6 -0
  40. package/src/errors/logging/diagnostic.mjs +9 -0
  41. package/src/errors/logging/error-diagnostic.mjs +5 -0
  42. package/src/errors/logging/value-diagnostic.mjs +5 -0
  43. package/src/errors/logging.mjs +10 -0
  44. package/src/errors/registration/events.mjs +9 -0
  45. package/src/errors/registration/lifecycle.mjs +18 -0
  46. package/src/errors/registration/registry.mjs +5 -0
  47. package/src/errors/registration/setup-state.mjs +20 -0
  48. package/src/errors/registration/state.mjs +28 -0
  49. package/src/errors/registration.mjs +80 -0
  50. package/src/errors/setup/installation.mjs +52 -0
  51. package/src/errors/setup/rollback.mjs +35 -0
  52. package/src/errors/setup.mjs +7 -0
  53. package/src/errors/validation/events.mjs +17 -0
  54. package/src/errors/validation/logger.mjs +8 -0
  55. package/src/errors/validation/process-object.mjs +11 -0
  56. package/src/errors/validation/signal.mjs +9 -0
  57. package/src/index.d.ts +14 -14
  58. package/src/index.mjs +9 -7
  59. package/src/log/child-logger.mjs +5 -0
  60. package/src/log/configuration/build-logger.mjs +13 -0
  61. package/src/log/configuration/default-options.mjs +5 -0
  62. package/src/log/configuration/normalize-options.mjs +14 -0
  63. package/src/log/configuration/validate-options.mjs +16 -0
  64. package/src/log/create-logger.mjs +18 -0
  65. package/src/log/formats/json-record.mjs +19 -0
  66. package/src/log/formats/json.mjs +38 -0
  67. package/src/log/formats/message.mjs +13 -0
  68. package/src/log/formats/text.mjs +21 -0
  69. package/src/log/index.d.mts +74 -0
  70. package/src/log/index.d.ts +74 -0
  71. package/src/log/index.mjs +15 -0
  72. package/src/log/logger-levels.mjs +37 -0
  73. package/src/path/base.mjs +17 -0
  74. package/src/path/conversions.mjs +7 -0
  75. package/src/path/create-path-helpers.mjs +58 -0
  76. package/src/path/current-path.mjs +13 -0
  77. package/src/path/file-url-to-path.mjs +5 -0
  78. package/src/path/index.d.mts +66 -0
  79. package/src/path/index.d.ts +66 -0
  80. package/src/path/index.mjs +12 -0
  81. package/src/path/input-classification.mjs +11 -0
  82. package/src/path/operation/failure.mjs +12 -0
  83. package/src/path/operation/logging.mjs +24 -0
  84. package/src/redact/headers/inspect-header-input.mjs +17 -0
  85. package/src/redact/headers/redact/entries.mjs +33 -0
  86. package/src/redact/headers/redact/headers.mjs +12 -0
  87. package/src/redact/headers/valid-entries.mjs +15 -0
  88. package/src/redact/index.d.mts +49 -0
  89. package/src/redact/index.d.ts +49 -0
  90. package/src/redact/index.mjs +7 -0
  91. package/src/redact/inspection/add-truncation-marker.mjs +13 -0
  92. package/src/redact/inspection/copy-error-fields.mjs +20 -0
  93. package/src/redact/inspection/inspect/entries.mjs +15 -0
  94. package/src/redact/inspection/inspect/error-fields.mjs +44 -0
  95. package/src/redact/inspection/safe-entries.mjs +7 -0
  96. package/src/redact/limits/default-limits.mjs +6 -0
  97. package/src/redact/limits/normalize/output-limit.mjs +8 -0
  98. package/src/redact/limits/normalize/traversal-limits.mjs +12 -0
  99. package/src/redact/markers/default-marker.mjs +1 -0
  100. package/src/redact/policy/create/policy.mjs +9 -0
  101. package/src/redact/policy/create/readonly-set.mjs +31 -0
  102. package/src/redact/policy/default/key-names.mjs +23 -0
  103. package/src/redact/policy/default/policy.mjs +9 -0
  104. package/src/redact/policy/normalize/keys.mjs +13 -0
  105. package/src/redact/policy/normalize-policy.mjs +14 -0
  106. package/src/redact/serialization/safe-serialize.mjs +34 -0
  107. package/src/redact/serialization/serialize/array.mjs +13 -0
  108. package/src/redact/serialization/serialize/buffer.mjs +5 -0
  109. package/src/redact/serialization/serialize/error.mjs +12 -0
  110. package/src/redact/serialization/serialize/object.mjs +22 -0
  111. package/src/redact/serialization/serialize/primitive.mjs +21 -0
  112. package/src/redact/structured/redact/array/attempt.mjs +9 -0
  113. package/src/redact/structured/redact/array/index-name.mjs +12 -0
  114. package/src/redact/structured/redact/array/indices.mjs +18 -0
  115. package/src/redact/structured/redact/array/properties.mjs +60 -0
  116. package/src/redact/structured/redact/array/result.mjs +11 -0
  117. package/src/redact/structured/redact/array.mjs +26 -0
  118. package/src/redact/structured/redact/classify.mjs +13 -0
  119. package/src/redact/structured/redact/error-fields.mjs +6 -0
  120. package/src/redact/structured/redact/error.mjs +14 -0
  121. package/src/redact/structured/redact/object.mjs +23 -0
  122. package/src/redact/structured/redact/value.mjs +49 -0
  123. package/src/redact/text/apply/configured/rules.mjs +9 -0
  124. package/src/redact/text/apply/configured/text-redactions.mjs +11 -0
  125. package/src/redact/text/apply/redactions.mjs +17 -0
  126. package/src/redact/text/apply/text-rules.mjs +8 -0
  127. package/src/redact/text/boundary/detect-jwt.mjs +18 -0
  128. package/src/redact/text/boundary/finish.mjs +15 -0
  129. package/src/redact/text/boundary/input.mjs +10 -0
  130. package/src/redact/text/boundary/jwt.mjs +22 -0
  131. package/src/redact/text/boundary/prepare.mjs +16 -0
  132. package/src/redact/text/boundary/process.mjs +9 -0
  133. package/src/redact/text/boundary/redaction.mjs +20 -0
  134. package/src/redact/text/boundary/secrets.mjs +21 -0
  135. package/src/redact/text/limit-output.mjs +12 -0
  136. package/src/redact/text/patterns/secret-key-patterns.mjs +2 -0
  137. package/src/redact/text/redact-text.mjs +8 -0
  138. package/src/redact/text/replace-literal/matches.mjs +39 -0
  139. package/src/redact/text/replace-literal/secret.mjs +6 -0
  140. package/src/redact/text/replace-literal/secrets.mjs +18 -0
  141. package/src/redact/text/replace-literal/trie.mjs +36 -0
  142. package/src/redact/text/rules/built-in.mjs +8 -0
  143. package/src/redact/text/rules/redact/authorization.mjs +12 -0
  144. package/src/redact/text/rules/redact/jwt/pattern.mjs +2 -0
  145. package/src/redact/text/rules/redact/jwt.mjs +10 -0
  146. package/src/redact/text/rules/redact/key/configured-values.mjs +25 -0
  147. package/src/redact/text/rules/redact/key/material.mjs +9 -0
  148. package/src/redact/text/rules/redact/key/values.mjs +16 -0
  149. package/src/redact/text/rules/redact/provider-token.mjs +9 -0
  150. package/src/signals/index.d.mts +29 -0
  151. package/src/signals/index.d.ts +29 -0
  152. package/src/signals/index.mjs +1 -0
  153. package/src/signals/lifecycle/abort/aggregate-cleanup.mjs +21 -0
  154. package/src/signals/lifecycle/abort/cleanup.mjs +9 -0
  155. package/src/signals/lifecycle/abort/listener-registration.mjs +14 -0
  156. package/src/signals/lifecycle/abort/listener.mjs +16 -0
  157. package/src/signals/lifecycle/abort/registration-cleanup.mjs +14 -0
  158. package/src/signals/lifecycle/abort/registration.mjs +19 -0
  159. package/src/signals/lifecycle/before/exit-registration.mjs +36 -0
  160. package/src/signals/lifecycle/before/exit.mjs +33 -0
  161. package/src/signals/lifecycle/cleanup/process-handlers.mjs +27 -0
  162. package/src/signals/lifecycle/cleanup/registration.mjs +5 -0
  163. package/src/signals/lifecycle/cleanup.mjs +17 -0
  164. package/src/signals/lifecycle/defaults.mjs +1 -0
  165. package/src/signals/lifecycle/exit-policy.mjs +17 -0
  166. package/src/signals/lifecycle/handlers/rollback.mjs +14 -0
  167. package/src/signals/lifecycle/handlers.mjs +35 -0
  168. package/src/signals/lifecycle/hooks.mjs +19 -0
  169. package/src/signals/lifecycle/logging.mjs +11 -0
  170. package/src/signals/lifecycle/registration-policy.mjs +29 -0
  171. package/src/signals/lifecycle/registration.mjs +7 -0
  172. package/src/signals/lifecycle/shutdown.mjs +75 -0
  173. package/src/signals/lifecycle/validation/lifecycle-options.mjs +8 -0
  174. package/src/signals/lifecycle/validation/logger.mjs +9 -0
  175. package/src/signals/lifecycle/validation/options.mjs +19 -0
  176. package/src/signals/lifecycle/validation/registration-options.mjs +36 -0
  177. package/src/signals/lifecycle/validation/signals.mjs +9 -0
  178. package/src/signals/register.mjs +76 -0
  179. package/src/snowflake/constants.mjs +15 -0
  180. package/src/snowflake/generator-options.mjs +27 -0
  181. package/src/snowflake/generator.mjs +82 -0
  182. package/src/snowflake/index.d.mts +24 -0
  183. package/src/snowflake/index.d.ts +24 -0
  184. package/src/snowflake/index.mjs +8 -0
  185. package/src/snowflake/log-generation-failure.mjs +9 -0
  186. package/src/snowflake/logger-validation.mjs +10 -0
  187. package/src/snowflake/validation.mjs +18 -0
  188. package/src/errors.d.mts +0 -9
  189. package/src/errors.mjs +0 -4
  190. package/src/fs.mjs +0 -4
  191. package/src/path.d.mts +0 -12
  192. package/src/path.mjs +0 -22
  193. package/src/signals.mjs +0 -4
@@ -0,0 +1,74 @@
1
+ import type { SerializationOptions as RedactSerializationOptions } from "../redact/index.mjs";
2
+
3
+ /**
4
+ * Factory to create a Winston logger instance.
5
+ * @param options Optional logger options.
6
+ * @param options.level Log level (default: 'info'; invalid values throw a TypeError)
7
+ * @param options.transports Array of winston transports (default: Console)
8
+ * @param options.timestamp Add a timestamp to JSON records. The value must be a boolean.
9
+ * @returns winston.Logger
10
+ *
11
+ * Note: All logger methods accept either (message, metaObject) or (message, primitive/array),
12
+ * where primitives/arrays are wrapped as { value: ... }.
13
+ */
14
+ export declare function createLogger(options?: {
15
+ level?: string;
16
+ transports?: import("winston").transport[];
17
+ format?: "text" | "json";
18
+ timestamp?: boolean;
19
+ keys?: string[];
20
+ }): import("winston").Logger & {
21
+ debug(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
22
+ info(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
23
+ warn(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
24
+ error(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
25
+ };
26
+
27
+ export {
28
+ createPolicy,
29
+ defaultPolicy,
30
+ redactHeaders,
31
+ redactText,
32
+ redactValue,
33
+ replaceLiteralSecret,
34
+ } from "../redact/index.mjs";
35
+
36
+ /** Runtime-configured Winston levels are available dynamically; standard levels are typed explicitly above and custom levels remain runtime-discovered. */
37
+ export type LoggerWithConfiguredLevels = import("winston").Logger & { [level: string]: unknown };
38
+
39
+ /** Recursive JSON-safe result returned by safeSerialize. */
40
+ export type SafeSerializedValue =
41
+ null | boolean | number | string | SafeSerializedValue[] | { [key: string]: SafeSerializedValue };
42
+
43
+ export type {
44
+ RedactionPolicy,
45
+ SerializationOptions,
46
+ StructuredRedactionOptions,
47
+ TextRedactionOptions,
48
+ } from "../redact/index.mjs";
49
+
50
+ /** Options accepted by safeSerialize. This alias preserves the original @eliware/log type name. */
51
+ export type RedactionOptions = RedactSerializationOptions;
52
+
53
+ /**
54
+ * Safely serializes metadata without invoking toJSON.
55
+ * Error values include name, message, stack, and enumerable custom properties, subject to redaction and limits.
56
+ */
57
+ export declare function safeSerialize(
58
+ value: unknown,
59
+ options?: RedactionOptions,
60
+ ): SafeSerializedValue;
61
+
62
+ /**
63
+ * Default logger instance.
64
+ *
65
+ * All logger methods accept either (message, metaObject) or (message, primitive/array),
66
+ * where primitives/arrays are wrapped as { value: ... }.
67
+ */
68
+ export declare const log: import("winston").Logger & {
69
+ debug(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
70
+ info(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
71
+ warn(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
72
+ error(message: unknown, meta?: unknown, ...args: unknown[]): import("winston").Logger;
73
+ } & LoggerWithConfiguredLevels;
74
+ export default log;
@@ -0,0 +1,15 @@
1
+ import createLogger from "./create-logger.mjs";
2
+ export {
3
+ createPolicy,
4
+ defaultPolicy,
5
+ redactHeaders,
6
+ redactText,
7
+ redactValue,
8
+ replaceLiteralSecret,
9
+ safeSerialize,
10
+ } from "../redact/index.mjs";
11
+ export { createLogger };
12
+
13
+ const log = createLogger();
14
+ export default log;
15
+ export { log };
@@ -0,0 +1,37 @@
1
+ const patchLoggerLevels = (logger) => {
2
+ // Only configured Winston level methods use the primitive metadata rule.
3
+ const levels = new Set(Object.keys(logger.levels));
4
+ for (const method of levels) {
5
+ const original = logger[method];
6
+ if (typeof original !== "function") {
7
+ throw new TypeError(`Configured logger level "${method}" is not callable`);
8
+ }
9
+ }
10
+ return new Proxy(logger, {
11
+ get(target, property, receiver) {
12
+ const original = Reflect.get(target, property, receiver);
13
+ if (property === "child" && typeof original === "function") {
14
+ return createChildLoggerWrapper(original, target, patchLoggerLevels);
15
+ }
16
+ if (!levels.has(property) || typeof original !== "function") return original;
17
+ return function (message, meta) {
18
+ const context = this == null || this === globalThis ? target : this;
19
+ if (
20
+ arguments.length >= 2 &&
21
+ meta !== undefined &&
22
+ (typeof meta !== "object" || meta === null || Array.isArray(meta))
23
+ ) {
24
+ return original.apply(context, [
25
+ message,
26
+ { value: meta },
27
+ ...Array.from(arguments).slice(2),
28
+ ]);
29
+ }
30
+ return original.apply(context, arguments);
31
+ };
32
+ },
33
+ });
34
+ };
35
+
36
+ export default patchLoggerLevels;
37
+ import createChildLoggerWrapper from "./child-logger.mjs";
@@ -0,0 +1,17 @@
1
+ import { fileURLToPath } from "url";
2
+ import { dirname as pathDirname } from "path";
3
+
4
+ const getCurrentFilename = (metaOrDir) => {
5
+ if (typeof metaOrDir === "string") return metaOrDir;
6
+ if (metaOrDir && metaOrDir.url) return fileURLToPath(metaOrDir.url);
7
+ throw new Error(
8
+ "Cannot determine current filename: provide import.meta, __dirname, or run in Node.js environment with __filename.",
9
+ );
10
+ };
11
+
12
+ const getCurrentDirname = (metaOrDir, dirnameFn = pathDirname) => {
13
+ if (typeof metaOrDir === "string") return metaOrDir;
14
+ return dirnameFn(getCurrentFilename(metaOrDir));
15
+ };
16
+
17
+ export default { getCurrentFilename, getCurrentDirname };
@@ -0,0 +1,7 @@
1
+ import { pathToFileURL } from "url";
2
+ import __methods from "./current-path.mjs";
3
+ const { path } = __methods;
4
+
5
+ const pathUrl = (metaOrDir, ...segments) => pathToFileURL(path(metaOrDir, ...segments)).href;
6
+
7
+ export default pathUrl;
@@ -0,0 +1,58 @@
1
+ import { log as defaultLogger } from "../log/index.mjs";
2
+ import __methods from "./current-path.mjs";
3
+ const { path, relativePath, resolvePath } = __methods;
4
+ import pathUrl from "./conversions.mjs";
5
+ import fileUrlToPath from "./file-url-to-path.mjs";
6
+ import __methods1 from "./base.mjs";
7
+ const { getCurrentDirname, getCurrentFilename } = __methods1;
8
+ import inputClassification from "./input-classification.mjs";
9
+ import createOperationLogger from "./operation/logging.mjs";
10
+ const { classifyDirectoryInput, classifyFileUrlInput } = inputClassification;
11
+
12
+ const createPathHelpers = ({ logger = defaultLogger } = {}) => {
13
+ if (!logger || typeof logger.log !== "function") {
14
+ throw new TypeError("logger must provide a log(info) function");
15
+ }
16
+
17
+ const execute = createOperationLogger(logger);
18
+
19
+ return {
20
+ getCurrentFilename: (metaOrDir) =>
21
+ execute("getCurrentFilename", classifyDirectoryInput(metaOrDir), {}, () =>
22
+ getCurrentFilename(metaOrDir),
23
+ ),
24
+ getCurrentDirname: (metaOrDir, dirnameFn) =>
25
+ execute(
26
+ "getCurrentDirname",
27
+ classifyDirectoryInput(metaOrDir),
28
+ { customDirname: typeof dirnameFn === "function" },
29
+ () => getCurrentDirname(metaOrDir, dirnameFn),
30
+ ),
31
+ path: (metaOrDir, ...segments) =>
32
+ execute("path", classifyDirectoryInput(metaOrDir), { segmentCount: segments.length }, () =>
33
+ path(metaOrDir, ...segments),
34
+ ),
35
+ pathUrl: (metaOrDir, ...segments) =>
36
+ execute("pathUrl", classifyDirectoryInput(metaOrDir), { segmentCount: segments.length }, () =>
37
+ pathUrl(metaOrDir, ...segments),
38
+ ),
39
+ resolvePath: (metaOrDir, ...segments) =>
40
+ execute(
41
+ "resolvePath",
42
+ classifyDirectoryInput(metaOrDir),
43
+ { segmentCount: segments.length },
44
+ () => resolvePath(metaOrDir, ...segments),
45
+ ),
46
+ relativePath: (metaOrDir, ...segments) =>
47
+ execute(
48
+ "relativePath",
49
+ classifyDirectoryInput(metaOrDir),
50
+ { segmentCount: segments.length },
51
+ () => relativePath(metaOrDir, ...segments),
52
+ ),
53
+ fileUrlToPath: (fileUrl) =>
54
+ execute("fileUrlToPath", classifyFileUrlInput(fileUrl), {}, () => fileUrlToPath(fileUrl)),
55
+ };
56
+ };
57
+
58
+ export default createPathHelpers;
@@ -0,0 +1,13 @@
1
+ import { join as pathJoin, relative as pathRelative, resolve as pathResolve } from "path";
2
+ import __methods from "./base.mjs";
3
+ const { getCurrentDirname } = __methods;
4
+
5
+ const path = (metaOrDir, ...segments) => pathJoin(getCurrentDirname(metaOrDir), ...segments);
6
+ const resolvePath = (metaOrDir, ...segments) =>
7
+ pathResolve(getCurrentDirname(metaOrDir), ...segments);
8
+ const relativePath = (metaOrDir, ...segments) => {
9
+ const dirname = getCurrentDirname(metaOrDir);
10
+ return pathRelative(dirname, pathResolve(dirname, ...segments));
11
+ };
12
+
13
+ export default { path, resolvePath, relativePath };
@@ -0,0 +1,5 @@
1
+ import { fileURLToPath } from "url";
2
+
3
+ const fileUrlToPath = (fileUrl) => fileURLToPath(fileUrl);
4
+
5
+ export default fileUrlToPath;
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Returns the current filename from import.meta or the supplied directory string.
3
+ * @param metaOrDir import.meta (ESM) or a string directory (e.g. __dirname)
4
+ * @returns The module filename for import.meta, or the unchanged directory string.
5
+ */
6
+ export function getCurrentFilename(metaOrDir: ImportMeta | string): string;
7
+
8
+ /**
9
+ * Returns the current dirname from import.meta or the supplied directory string.
10
+ * @param metaOrDir import.meta (ESM) or a string directory (e.g. __dirname)
11
+ * @param dirnameFn Optional custom dirname function.
12
+ * @returns The directory path, absolute when the input is absolute.
13
+ */
14
+ export function getCurrentDirname(meta: ImportMeta, dirnameFn?: (path: string) => string): string;
15
+ export function getCurrentDirname(directory: string): string;
16
+
17
+ /**
18
+ * Joins the current dirname with provided segments.
19
+ * @param metaOrDir import.meta (ESM) or a string directory (e.g. __dirname)
20
+ * @param segments Path segments to join.
21
+ * @returns A path string using the host platform's native path behavior.
22
+ */
23
+ export declare const path: (metaOrDir: ImportMeta | string, ...segments: string[]) => string;
24
+
25
+ /**
26
+ * Converts a resolved path to a file URL href string for dynamic import compatibility.
27
+ * @param metaOrDir import.meta (ESM) or a string directory (e.g. __dirname)
28
+ * @param segments Path segments to join.
29
+ * @returns File URL href string.
30
+ */
31
+ export function pathUrl(metaOrDir: ImportMeta | string, ...segments: string[]): string;
32
+
33
+ export default path;
34
+
35
+ /** Resolves segments from the import.meta-derived directory. */
36
+ export function resolvePath(metaOrDir: ImportMeta | string, ...segments: string[]): string;
37
+
38
+ /** Returns a path relative to the import.meta-derived directory. */
39
+ export function relativePath(metaOrDir: ImportMeta | string, ...segments: string[]): string;
40
+
41
+ /** Converts a file URL string or URL object to a filesystem path. */
42
+ export function fileUrlToPath(fileUrl: string | URL): string;
43
+
44
+ /** A logger that accepts structured Winston-style log records. */
45
+ export type PathLogger = Pick<LoggerWithConfiguredLevels, "log">;
46
+
47
+ /** Options for creating path helpers with injected logging. */
48
+ export interface PathHelpersOptions {
49
+ logger?: PathLogger;
50
+ }
51
+
52
+ /** Path helper functions that write structured records to the supplied logger. */
53
+ export interface PathHelpers {
54
+ getCurrentFilename(metaOrDir: ImportMeta | string): string;
55
+ getCurrentDirname(meta: ImportMeta, dirnameFn?: (path: string) => string): string;
56
+ getCurrentDirname(directory: string): string;
57
+ path(metaOrDir: ImportMeta | string, ...segments: string[]): string;
58
+ pathUrl(metaOrDir: ImportMeta | string, ...segments: string[]): string;
59
+ resolvePath(metaOrDir: ImportMeta | string, ...segments: string[]): string;
60
+ relativePath(metaOrDir: ImportMeta | string, ...segments: string[]): string;
61
+ fileUrlToPath(fileUrl: string | URL): string;
62
+ }
63
+
64
+ /** Creates path helpers that write safe operation context to an injected logger. */
65
+ export function createPathHelpers(options?: PathHelpersOptions): PathHelpers;
66
+ import type { LoggerWithConfiguredLevels } from "../log/index.mjs";
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Returns the current filename from import.meta or the supplied directory string.
3
+ * @param metaOrDir import.meta (ESM) or a string directory (e.g. __dirname)
4
+ * @returns The module filename for import.meta, or the unchanged directory string.
5
+ */
6
+ export function getCurrentFilename(metaOrDir: ImportMeta | string): string;
7
+
8
+ /**
9
+ * Returns the current dirname from import.meta or the supplied directory string.
10
+ * @param metaOrDir import.meta (ESM) or a string directory (e.g. __dirname)
11
+ * @param dirnameFn Optional custom dirname function.
12
+ * @returns The directory path, absolute when the input is absolute.
13
+ */
14
+ export function getCurrentDirname(meta: ImportMeta, dirnameFn?: (path: string) => string): string;
15
+ export function getCurrentDirname(directory: string): string;
16
+
17
+ /**
18
+ * Joins the current dirname with provided segments.
19
+ * @param metaOrDir import.meta (ESM) or a string directory (e.g. __dirname)
20
+ * @param segments Path segments to join.
21
+ * @returns A path string using the host platform's native path behavior.
22
+ */
23
+ export declare const path: (metaOrDir: ImportMeta | string, ...segments: string[]) => string;
24
+
25
+ /**
26
+ * Converts a resolved path to a file URL href string for dynamic import compatibility.
27
+ * @param metaOrDir import.meta (ESM) or a string directory (e.g. __dirname)
28
+ * @param segments Path segments to join.
29
+ * @returns File URL href string.
30
+ */
31
+ export function pathUrl(metaOrDir: ImportMeta | string, ...segments: string[]): string;
32
+
33
+ export default path;
34
+
35
+ /** Resolves segments from the import.meta-derived directory. */
36
+ export function resolvePath(metaOrDir: ImportMeta | string, ...segments: string[]): string;
37
+
38
+ /** Returns a path relative to the import.meta-derived directory. */
39
+ export function relativePath(metaOrDir: ImportMeta | string, ...segments: string[]): string;
40
+
41
+ /** Converts a file URL string or URL object to a filesystem path. */
42
+ export function fileUrlToPath(fileUrl: string | URL): string;
43
+
44
+ /** A logger that accepts structured Winston-style log records. */
45
+ export type PathLogger = Pick<LoggerWithConfiguredLevels, "log">;
46
+
47
+ /** Options for creating path helpers with injected logging. */
48
+ export interface PathHelpersOptions {
49
+ logger?: PathLogger;
50
+ }
51
+
52
+ /** Path helper functions that write structured records to the supplied logger. */
53
+ export interface PathHelpers {
54
+ getCurrentFilename(metaOrDir: ImportMeta | string): string;
55
+ getCurrentDirname(meta: ImportMeta, dirnameFn?: (path: string) => string): string;
56
+ getCurrentDirname(directory: string): string;
57
+ path(metaOrDir: ImportMeta | string, ...segments: string[]): string;
58
+ pathUrl(metaOrDir: ImportMeta | string, ...segments: string[]): string;
59
+ resolvePath(metaOrDir: ImportMeta | string, ...segments: string[]): string;
60
+ relativePath(metaOrDir: ImportMeta | string, ...segments: string[]): string;
61
+ fileUrlToPath(fileUrl: string | URL): string;
62
+ }
63
+
64
+ /** Creates path helpers that write safe operation context to an injected logger. */
65
+ export function createPathHelpers(options?: PathHelpersOptions): PathHelpers;
66
+ import type { LoggerWithConfiguredLevels } from "../log/index.mjs";
@@ -0,0 +1,12 @@
1
+ import __methods from "./current-path.mjs";
2
+ export const path = __methods.path;
3
+ export const resolvePath = __methods.resolvePath;
4
+ export const relativePath = __methods.relativePath;
5
+ import __methods1 from "./base.mjs";
6
+ export const getCurrentFilename = __methods1.getCurrentFilename;
7
+ export const getCurrentDirname = __methods1.getCurrentDirname;
8
+ export { default as pathUrl } from "./conversions.mjs";
9
+ export { default as fileUrlToPath } from "./file-url-to-path.mjs";
10
+ export { default as createPathHelpers } from "./create-path-helpers.mjs";
11
+ import __methods2 from "./current-path.mjs";
12
+ export default __methods2.path;
@@ -0,0 +1,11 @@
1
+ function classifyDirectoryInput(input) {
2
+ return typeof input === "string" ? "directory" : "import-meta";
3
+ }
4
+
5
+ function classifyFileUrlInput(input) {
6
+ if (input instanceof URL) return "url-object";
7
+ if (typeof input === "string") return "url-string";
8
+ return "other";
9
+ }
10
+
11
+ export default { classifyDirectoryInput, classifyFileUrlInput };
@@ -0,0 +1,12 @@
1
+ function logOperationFailure(logger, operation, inputType, context, error) {
2
+ logger.log({
3
+ level: "error",
4
+ message: "Path operation failed",
5
+ operation,
6
+ inputType,
7
+ ...context,
8
+ });
9
+ throw error;
10
+ }
11
+
12
+ export default logOperationFailure;
@@ -0,0 +1,24 @@
1
+ import logOperationFailure from "./failure.mjs";
2
+
3
+ function createOperationLogger(logger) {
4
+ return (operation, inputType, context, action) => {
5
+ let result;
6
+ try {
7
+ result = action();
8
+ } catch (error) {
9
+ logOperationFailure(logger, operation, inputType, context, error);
10
+ }
11
+
12
+ logger.log({
13
+ level: "debug",
14
+ message: "Path operation completed",
15
+ operation,
16
+ inputType,
17
+ ...context,
18
+ resultType: typeof result,
19
+ });
20
+ return result;
21
+ };
22
+ }
23
+
24
+ export default createOperationLogger;
@@ -0,0 +1,17 @@
1
+ function inspectHeaderInput(headers) {
2
+ let isArray = false;
3
+ try {
4
+ isArray = Array.isArray(headers);
5
+ if (headers && !isArray) {
6
+ const entries = headers.entries;
7
+ if (typeof entries === "function")
8
+ return { type: "entries", entries: [...entries.call(headers)] };
9
+ }
10
+ if (isArray) return { type: "array", entries: headers };
11
+ return { type: "object", value: headers ?? {} };
12
+ } catch {
13
+ return { type: "entries", entries: [], failed: true };
14
+ }
15
+ }
16
+
17
+ export default inspectHeaderInput;
@@ -0,0 +1,33 @@
1
+ import redactValue from "../../structured/redact/value.mjs";
2
+ import validEntries from "../valid-entries.mjs";
3
+
4
+ function redactEntries(entries, type, options) {
5
+ const redactedEntries = [];
6
+ try {
7
+ for (const entry of entries) {
8
+ if (!validEntries([entry])) continue;
9
+ try {
10
+ const [key, value] = entry;
11
+ const normalizedKey = key.toLowerCase();
12
+ const record = {};
13
+ Object.defineProperty(record, normalizedKey, { value, enumerable: true });
14
+ redactedEntries.push([key, redactValue(record, options)[normalizedKey]]);
15
+ } catch {}
16
+ }
17
+ } catch {
18
+ return [];
19
+ }
20
+
21
+ if (type !== "entries") return redactedEntries;
22
+ const byName = new Map();
23
+ const redactedNames = new Set();
24
+ for (const [key, value] of redactedEntries) {
25
+ const name = key.toLowerCase();
26
+ byName.set(name, value);
27
+ if (value === "[REDACTED]") redactedNames.add(name);
28
+ }
29
+ for (const name of redactedNames) byName.set(name, "[REDACTED]");
30
+ return Object.fromEntries(byName);
31
+ }
32
+
33
+ export default redactEntries;
@@ -0,0 +1,12 @@
1
+ import redactValue from "../../structured/redact/value.mjs";
2
+ import inspectHeaderInput from "../inspect-header-input.mjs";
3
+ import redactEntries from "./entries.mjs";
4
+
5
+ function redactHeaders(headers, options = {}) {
6
+ const input = inspectHeaderInput(headers);
7
+ if (input.type === "object") return redactValue(input.value, options);
8
+ if (input.failed) return [];
9
+ return redactEntries(input.entries, input.type, options);
10
+ }
11
+
12
+ export default redactHeaders;
@@ -0,0 +1,15 @@
1
+ function validEntries(entries) {
2
+ try {
3
+ return entries.every(
4
+ (entry) =>
5
+ Array.isArray(entry) &&
6
+ entry.length === 2 &&
7
+ typeof entry[0] === "string" &&
8
+ typeof entry[1] === "string",
9
+ );
10
+ } catch {
11
+ return false;
12
+ }
13
+ }
14
+
15
+ export default validEntries;
@@ -0,0 +1,49 @@
1
+ export interface RedactionPolicy {
2
+ keys?: Iterable<string>;
3
+ redactKeys?: Iterable<string>;
4
+ marker?: string;
5
+ circularMarker?: string;
6
+ }
7
+
8
+ export interface SerializationOptions extends RedactionPolicy {
9
+ maxDepth?: number;
10
+ maxKeys?: number;
11
+ maxArray?: number;
12
+ maxString?: number;
13
+ }
14
+
15
+ export interface StructuredRedactionOptions extends RedactionPolicy {
16
+ maxDepth?: number;
17
+ maxKeys?: number;
18
+ maxArray?: number;
19
+ }
20
+
21
+ export interface TextRedactionOptions extends RedactionPolicy {
22
+ secrets?: Iterable<string>;
23
+ maxOutputLength?: number;
24
+ }
25
+
26
+ export declare const defaultPolicy: Readonly<{
27
+ keys: ReadonlySet<string>;
28
+ marker: "[REDACTED]";
29
+ }>;
30
+
31
+ export declare function createPolicy(options?: RedactionPolicy): Readonly<
32
+ RedactionPolicy & {
33
+ keys: ReadonlySet<string>;
34
+ marker: string;
35
+ }
36
+ >;
37
+
38
+ export declare function redactValue<T>(value: T, options?: StructuredRedactionOptions): unknown;
39
+ export declare function redactHeaders(
40
+ headers: unknown,
41
+ options?: StructuredRedactionOptions,
42
+ ): unknown;
43
+ export declare function redactText(value: unknown, options?: TextRedactionOptions): string;
44
+ export declare function replaceLiteralSecret(
45
+ value: unknown,
46
+ secret: unknown,
47
+ marker?: string,
48
+ ): string;
49
+ export declare function safeSerialize(value: unknown, options?: SerializationOptions): unknown;
@@ -0,0 +1,49 @@
1
+ export interface RedactionPolicy {
2
+ keys?: Iterable<string>;
3
+ redactKeys?: Iterable<string>;
4
+ marker?: string;
5
+ circularMarker?: string;
6
+ }
7
+
8
+ export interface SerializationOptions extends RedactionPolicy {
9
+ maxDepth?: number;
10
+ maxKeys?: number;
11
+ maxArray?: number;
12
+ maxString?: number;
13
+ }
14
+
15
+ export interface StructuredRedactionOptions extends RedactionPolicy {
16
+ maxDepth?: number;
17
+ maxKeys?: number;
18
+ maxArray?: number;
19
+ }
20
+
21
+ export interface TextRedactionOptions extends RedactionPolicy {
22
+ secrets?: Iterable<string>;
23
+ maxOutputLength?: number;
24
+ }
25
+
26
+ export declare const defaultPolicy: Readonly<{
27
+ keys: ReadonlySet<string>;
28
+ marker: "[REDACTED]";
29
+ }>;
30
+
31
+ export declare function createPolicy(options?: RedactionPolicy): Readonly<
32
+ RedactionPolicy & {
33
+ keys: ReadonlySet<string>;
34
+ marker: string;
35
+ }
36
+ >;
37
+
38
+ export declare function redactValue<T>(value: T, options?: StructuredRedactionOptions): unknown;
39
+ export declare function redactHeaders(
40
+ headers: unknown,
41
+ options?: StructuredRedactionOptions,
42
+ ): unknown;
43
+ export declare function redactText(value: unknown, options?: TextRedactionOptions): string;
44
+ export declare function replaceLiteralSecret(
45
+ value: unknown,
46
+ secret: unknown,
47
+ marker?: string,
48
+ ): string;
49
+ export declare function safeSerialize(value: unknown, options?: SerializationOptions): unknown;
@@ -0,0 +1,7 @@
1
+ export { default as createPolicy } from "./policy/create/policy.mjs";
2
+ export { defaultPolicy } from "./policy/default/policy.mjs";
3
+ export { default as redactValue } from "./structured/redact/value.mjs";
4
+ export { default as redactHeaders } from "./headers/redact/headers.mjs";
5
+ export { default as redactText } from "./text/redact-text.mjs";
6
+ export { default as replaceLiteralSecret } from "./text/replace-literal/secret.mjs";
7
+ export { default as safeSerialize } from "./serialization/safe-serialize.mjs";
@@ -0,0 +1,13 @@
1
+ function addTruncationMarker(output) {
2
+ let key = "__truncated";
3
+ let suffix = 0;
4
+ while (Object.hasOwn(output, key)) key = `__truncated_${++suffix}`;
5
+ Object.defineProperty(output, key, {
6
+ value: "[TRUNCATED]",
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ });
11
+ }
12
+
13
+ export default addTruncationMarker;
@@ -0,0 +1,20 @@
1
+ import addTruncationMarker from "./add-truncation-marker.mjs";
2
+ import inspectErrorFields from "./inspect/error-fields.mjs";
3
+
4
+ function copyErrorFields(error, maxKeys, mapValue) {
5
+ const inspected = inspectErrorFields(error, maxKeys);
6
+ if (inspected.failed) return {};
7
+ const output = {};
8
+ for (const [key, value] of inspected.fields) {
9
+ Object.defineProperty(output, key, {
10
+ value: mapValue(key, value),
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ });
15
+ }
16
+ if (inspected.truncated) addTruncationMarker(output);
17
+ return output;
18
+ }
19
+
20
+ export default copyErrorFields;