@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,15 @@
1
+ import limitOutput from "../limit-output.mjs";
2
+
3
+ function finishBoundaryRedaction(output, boundaryResult, inputTruncated, maxLength, options) {
4
+ if (boundaryResult.redacted) {
5
+ const marker = "[REDACTED][TRUNCATED]";
6
+ const prefix = output.slice(0, output.indexOf(boundaryResult.marker));
7
+ return limitOutput(
8
+ `${prefix.slice(0, Math.max(0, maxLength - marker.length))}${marker}`,
9
+ options.maxOutputLength,
10
+ );
11
+ }
12
+ return limitOutput(inputTruncated ? `${output}[TRUNCATED]` : output, options.maxOutputLength);
13
+ }
14
+
15
+ export default finishBoundaryRedaction;
@@ -0,0 +1,10 @@
1
+ import normalizeOutputLimit from "../../limits/normalize/output-limit.mjs";
2
+
3
+ function prepareBoundaryInput(value, configuredLimit) {
4
+ return {
5
+ input: String(value ?? ""),
6
+ maxLength: normalizeOutputLimit(configuredLimit),
7
+ };
8
+ }
9
+
10
+ export default prepareBoundaryInput;
@@ -0,0 +1,22 @@
1
+ const boundedJwtPattern = /\beyJ[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]*(?:\.[A-Za-z0-9_-]*)?/gu;
2
+
3
+ export const boundedJwtMarker = "\u0000bounded-jwt-redacted\u0000";
4
+
5
+ function redactJwtAtBoundary(value, boundary, inputContinues = false) {
6
+ let redacted = false;
7
+ const output = value.replace(boundedJwtPattern, (match, offset) => {
8
+ const segments = match.match(/\./g).length;
9
+ const incompleteAtEnd =
10
+ inputContinues &&
11
+ segments === 1 &&
12
+ offset < boundary &&
13
+ offset + match.length === value.length;
14
+ if (segments < 2 && !incompleteAtEnd) return match;
15
+ if (offset >= boundary || offset + match.length < boundary) return match;
16
+ redacted = true;
17
+ return boundedJwtMarker;
18
+ });
19
+ return { output, redacted };
20
+ }
21
+
22
+ export default redactJwtAtBoundary;
@@ -0,0 +1,16 @@
1
+ import prepareBoundaryInput from "./input.mjs";
2
+ import prepareBoundarySecrets from "./secrets.mjs";
3
+ import createBoundaryRedaction from "./redaction.mjs";
4
+
5
+ function prepareBoundaryRedaction(value, options) {
6
+ const { input, maxLength } = prepareBoundaryInput(value, options.maxOutputLength);
7
+ const { secrets, lookahead } = prepareBoundarySecrets(options.secrets);
8
+ const processingLimit = maxLength + lookahead;
9
+ return {
10
+ secrets,
11
+ processingLimit,
12
+ boundary: createBoundaryRedaction(input, maxLength, processingLimit),
13
+ };
14
+ }
15
+
16
+ export default prepareBoundaryRedaction;
@@ -0,0 +1,9 @@
1
+ import prepareBoundaryRedaction from "./prepare.mjs";
2
+
3
+ function processBoundaryRedaction(value, options, processInput) {
4
+ const { secrets, processingLimit, boundary } = prepareBoundaryRedaction(value, options);
5
+ const output = processInput(boundary.input, secrets, processingLimit);
6
+ return boundary.finish(output, options);
7
+ }
8
+
9
+ export default processBoundaryRedaction;
@@ -0,0 +1,20 @@
1
+ import detectBoundaryJwt from "./detect-jwt.mjs";
2
+ import finishBoundaryRedaction from "./finish.mjs";
3
+
4
+ function createBoundaryRedaction(input, maxLength, processingLimit) {
5
+ const boundaryResult = detectBoundaryJwt(input, maxLength, processingLimit);
6
+
7
+ return {
8
+ input: boundaryResult.output,
9
+ finish: (output, options) =>
10
+ finishBoundaryRedaction(
11
+ output,
12
+ boundaryResult,
13
+ boundaryResult.inputTruncated,
14
+ maxLength,
15
+ options,
16
+ ),
17
+ };
18
+ }
19
+
20
+ export default createBoundaryRedaction;
@@ -0,0 +1,21 @@
1
+ const MAX_SECRET_COUNT = 100;
2
+ const MAX_SECRET_CHARACTERS = 10_000;
3
+
4
+ function prepareBoundarySecrets(configuredSecrets) {
5
+ const source =
6
+ typeof configuredSecrets === "string" ? [configuredSecrets] : (configuredSecrets ?? []);
7
+ const secrets = [];
8
+ let totalCharacters = 0;
9
+ for (const value of source) {
10
+ if (secrets.length === MAX_SECRET_COUNT)
11
+ throw new RangeError("secrets must contain no more than 100 values");
12
+ const secret = String(value);
13
+ totalCharacters += secret.length;
14
+ if (totalCharacters > MAX_SECRET_CHARACTERS)
15
+ throw new RangeError("secrets must contain no more than 10000 total characters");
16
+ secrets.push(secret);
17
+ }
18
+ return { secrets, lookahead: Math.max(0, ...secrets.map((secret) => secret.length)) };
19
+ }
20
+
21
+ export default prepareBoundarySecrets;
@@ -0,0 +1,12 @@
1
+ import normalizeOutputLimit from "../limits/normalize/output-limit.mjs";
2
+
3
+ function limitOutput(value, configuredMaxLength) {
4
+ const maxLength = normalizeOutputLimit(configuredMaxLength);
5
+ if (value.length <= maxLength) return value;
6
+
7
+ const marker = "[TRUNCATED]";
8
+ if (maxLength < marker.length) return maxLength === 0 ? "" : "…";
9
+ return `${value.slice(0, maxLength - marker.length)}${marker}`;
10
+ }
11
+
12
+ export default limitOutput;
@@ -0,0 +1,2 @@
1
+ export const SECRET_KEY_PATTERN =
2
+ "(?:api[_-]?key|access[_-]?key|client[_-]?secret|credential|password|passwd|secret|token|authorization)";
@@ -0,0 +1,8 @@
1
+ import createTextRedactor from "./apply/redactions.mjs";
2
+ import processBoundaryRedaction from "./boundary/process.mjs";
3
+
4
+ function redactText(value, options = {}) {
5
+ return processBoundaryRedaction(value, options, createTextRedactor(options));
6
+ }
7
+
8
+ export default redactText;
@@ -0,0 +1,39 @@
1
+ import createSecretTrie from "./trie.mjs";
2
+
3
+ function matchSecretRanges(text, secrets) {
4
+ const trie = createSecretTrie(secrets);
5
+ if (trie.children.size === 0) return [];
6
+
7
+ const matchEnds = new Int32Array(text.length);
8
+ matchEnds.fill(-1);
9
+ let node = trie;
10
+ for (let index = 0; index < text.length; index++) {
11
+ const character = text[index];
12
+ while (node !== trie && !node.children.has(character)) node = node.failure;
13
+ node = node.children.get(character) ?? trie;
14
+ if (node.matchLength === 0) continue;
15
+
16
+ const end = index + 1;
17
+ const start = end - node.matchLength;
18
+ matchEnds[start] = Math.max(matchEnds[start], end);
19
+ }
20
+
21
+ const ranges = [];
22
+ let rangeStart = -1;
23
+ let rangeEnd = -1;
24
+ for (let start = 0; start < matchEnds.length; start++) {
25
+ const end = matchEnds[start];
26
+ if (end === -1) continue;
27
+ if (rangeStart !== -1 && start < rangeEnd) {
28
+ rangeEnd = Math.max(rangeEnd, end);
29
+ continue;
30
+ }
31
+ if (rangeStart !== -1) ranges.push([rangeStart, rangeEnd]);
32
+ rangeStart = start;
33
+ rangeEnd = end;
34
+ }
35
+ if (rangeStart !== -1) ranges.push([rangeStart, rangeEnd]);
36
+ return ranges;
37
+ }
38
+
39
+ export default matchSecretRanges;
@@ -0,0 +1,6 @@
1
+ function replaceLiteralSecret(value, secret, marker = "[REDACTED]") {
2
+ const text = String(value ?? "");
3
+ return secret ? text.split(String(secret)).join(marker) : text;
4
+ }
5
+
6
+ export default replaceLiteralSecret;
@@ -0,0 +1,18 @@
1
+ import matchSecretRanges from "./matches.mjs";
2
+
3
+ function replaceLiteralSecrets(value, secrets, marker = "[REDACTED]") {
4
+ const text = String(value ?? "");
5
+ const secretValues = typeof secrets === "string" ? [secrets] : [...(secrets ?? [])];
6
+ const ranges = matchSecretRanges(text, secretValues.map(String).filter(Boolean));
7
+ if (ranges.length === 0) return text;
8
+
9
+ let output = "";
10
+ let cursor = 0;
11
+ for (const [start, end] of ranges) {
12
+ output += text.slice(cursor, start) + marker;
13
+ cursor = end;
14
+ }
15
+ return output + text.slice(cursor);
16
+ }
17
+
18
+ export default replaceLiteralSecrets;
@@ -0,0 +1,36 @@
1
+ function createSecretTrie(secrets) {
2
+ const root = { children: new Map(), failure: null, matchLength: 0, terminalLength: 0 };
3
+ for (const secret of secrets) {
4
+ let node = root;
5
+ for (let index = 0; index < secret.length; index++) {
6
+ const character = secret[index];
7
+ if (!node.children.has(character))
8
+ node.children.set(character, {
9
+ children: new Map(),
10
+ failure: null,
11
+ matchLength: 0,
12
+ terminalLength: 0,
13
+ });
14
+ node = node.children.get(character);
15
+ }
16
+ node.terminalLength = secret.length;
17
+ }
18
+
19
+ root.failure = root;
20
+ const queue = [...root.children.values()];
21
+ for (const child of queue) child.failure = root;
22
+
23
+ for (let index = 0; index < queue.length; index++) {
24
+ const node = queue[index];
25
+ node.matchLength = Math.max(node.terminalLength, node.failure.matchLength);
26
+ for (const [character, child] of node.children) {
27
+ let fallback = node.failure;
28
+ while (fallback !== root && !fallback.children.has(character)) fallback = fallback.failure;
29
+ child.failure = fallback.children.get(character) ?? root;
30
+ queue.push(child);
31
+ }
32
+ }
33
+ return root;
34
+ }
35
+
36
+ export default createSecretTrie;
@@ -0,0 +1,8 @@
1
+ import redactAuthorization from "./redact/authorization.mjs";
2
+ import redactJwt from "./redact/jwt.mjs";
3
+ import redactKeyValues from "./redact/key/values.mjs";
4
+ import redactProviderToken from "./redact/provider-token.mjs";
5
+
6
+ const builtInRules = [redactAuthorization, redactKeyValues, redactProviderToken, redactJwt];
7
+
8
+ export default builtInRules;
@@ -0,0 +1,12 @@
1
+ import applyTextRules from "../../apply/text-rules.mjs";
2
+
3
+ const rules = [
4
+ [/(authorization\s*:\s*)(?:bearer\s+)?[^\s,]+/giu, "$1[REDACTED]"],
5
+ [/(\bBearer\s+)[A-Za-z0-9._~+/=-]+/giu, "$1[REDACTED]"],
6
+ ];
7
+
8
+ function redactAuthorization(value) {
9
+ return applyTextRules(value, rules);
10
+ }
11
+
12
+ export default redactAuthorization;
@@ -0,0 +1,2 @@
1
+ export const JWT_PATTERN =
2
+ /\beyJ[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]*(?![A-Za-z0-9_-])/gu;
@@ -0,0 +1,10 @@
1
+ import applyTextRules from "../../apply/text-rules.mjs";
2
+ import { JWT_PATTERN } from "./jwt/pattern.mjs";
3
+
4
+ const rules = [[JWT_PATTERN, "[REDACTED]"]];
5
+
6
+ function redactJwt(value) {
7
+ return applyTextRules(value, rules);
8
+ }
9
+
10
+ export default redactJwt;
@@ -0,0 +1,25 @@
1
+ import applyTextRules from "../../../apply/text-rules.mjs";
2
+
3
+ function redactConfiguredKeyValues(value, keys, marker = "[REDACTED]") {
4
+ const alternatives = [...keys]
5
+ .filter(Boolean)
6
+ .map((key) => String(key).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
7
+ .sort((left, right) => right.length - left.length);
8
+ if (alternatives.length === 0) return String(value ?? "");
9
+
10
+ const keyPattern = `(?:${alternatives.join("|")})`;
11
+ const valuePattern = `(?:"[^"]*"|'[^']*'|[^\\s,};&]+)`;
12
+ const rules = [
13
+ [
14
+ new RegExp(`([?&]${keyPattern}=)(?:"[^"]*"|'[^']*'|[^&#\\s]+)`, "giu"),
15
+ (_match, prefix) => `${prefix}${marker}`,
16
+ ],
17
+ [
18
+ new RegExp(`(?<![?&])(${keyPattern}\\s*[=:]\\s*)${valuePattern}`, "giu"),
19
+ (_match, prefix) => `${prefix}${marker}`,
20
+ ],
21
+ ];
22
+ return applyTextRules(value, rules);
23
+ }
24
+
25
+ export default redactConfiguredKeyValues;
@@ -0,0 +1,9 @@
1
+ import applyTextRules from "../../../apply/text-rules.mjs";
2
+
3
+ const rules = [[/-----BEGIN [^-]+-----[\s\S]*?(?:-----END [^-]+-----|$)/gu, "[REDACTED]"]];
4
+
5
+ function redactKeyMaterial(value) {
6
+ return applyTextRules(value, rules);
7
+ }
8
+
9
+ export default redactKeyMaterial;
@@ -0,0 +1,16 @@
1
+ import applyTextRules from "../../../apply/text-rules.mjs";
2
+ import { SECRET_KEY_PATTERN } from "../../../patterns/secret-key-patterns.mjs";
3
+
4
+ const rules = [
5
+ [new RegExp(`([?&]${SECRET_KEY_PATTERN}=)[^&#\\s]+`, "giu"), "$1[REDACTED]"],
6
+ [
7
+ new RegExp(`((?:${SECRET_KEY_PATTERN})\\s*[=:]\\s*)(?:"[^"]*"|'[^']*'|[^\\s,};&]+)`, "giu"),
8
+ "$1[REDACTED]",
9
+ ],
10
+ ];
11
+
12
+ function redactKeyValues(value) {
13
+ return applyTextRules(value, rules);
14
+ }
15
+
16
+ export default redactKeyValues;
@@ -0,0 +1,9 @@
1
+ import applyTextRules from "../../apply/text-rules.mjs";
2
+
3
+ const rules = [[/\b(?:ghp|gho|ghs|github_pat|npm_|pypi-)[A-Za-z0-9_-]+/gu, "[REDACTED]"]];
4
+
5
+ function redactProviderToken(value) {
6
+ return applyTextRules(value, rules);
7
+ }
8
+
9
+ export default redactProviderToken;
@@ -0,0 +1,29 @@
1
+ export type NodeSignal = string;
2
+ export interface ProcessLike {
3
+ on(event: string, listener: (...args: any[]) => void): unknown;
4
+ off(event: string, listener: (...args: any[]) => void): unknown;
5
+ listeners(event: string): Array<(...args: any[]) => void>;
6
+ exit?(code?: number): unknown;
7
+ }
8
+ export interface SignalsLogger {
9
+ debug(message: string, meta?: unknown): unknown;
10
+ warn(message: string, meta?: unknown): unknown;
11
+ error(message: string, meta?: unknown): unknown;
12
+ }
13
+ export interface RegisterSignalsOptions {
14
+ processObj?: ProcessLike;
15
+ log?: SignalsLogger;
16
+ signals?: NodeSignal[];
17
+ shutdownHook?: (label: string) => void | Promise<void>;
18
+ exitCode?: number;
19
+ exit?: boolean;
20
+ signal?: AbortSignal;
21
+ }
22
+ export interface SignalsRegistration {
23
+ readonly removed: boolean;
24
+ shutdown(label?: string): Promise<void>;
25
+ getShuttingDown(): boolean;
26
+ removeHandlers(): void;
27
+ }
28
+ export declare function registerSignals(options?: RegisterSignalsOptions): SignalsRegistration;
29
+ export default registerSignals;
@@ -0,0 +1,29 @@
1
+ export type NodeSignal = string;
2
+ export interface ProcessLike {
3
+ on(event: string, listener: (...args: any[]) => void): unknown;
4
+ off(event: string, listener: (...args: any[]) => void): unknown;
5
+ listeners(event: string): Array<(...args: any[]) => void>;
6
+ exit?(code?: number): unknown;
7
+ }
8
+ export interface SignalsLogger {
9
+ debug(message: string, meta?: unknown): unknown;
10
+ warn(message: string, meta?: unknown): unknown;
11
+ error(message: string, meta?: unknown): unknown;
12
+ }
13
+ export interface RegisterSignalsOptions {
14
+ processObj?: ProcessLike;
15
+ log?: SignalsLogger;
16
+ signals?: NodeSignal[];
17
+ shutdownHook?: (label: string) => void | Promise<void>;
18
+ exitCode?: number;
19
+ exit?: boolean;
20
+ signal?: AbortSignal;
21
+ }
22
+ export interface SignalsRegistration {
23
+ readonly removed: boolean;
24
+ shutdown(label?: string): Promise<void>;
25
+ getShuttingDown(): boolean;
26
+ removeHandlers(): void;
27
+ }
28
+ export declare function registerSignals(options?: RegisterSignalsOptions): SignalsRegistration;
29
+ export default registerSignals;
@@ -0,0 +1 @@
1
+ export { default, default as registerSignals } from "./register.mjs";
@@ -0,0 +1,21 @@
1
+ function aggregateCleanup(removeAbortListener, removeHandlers) {
2
+ let cleanupError;
3
+ let hasCleanupError = false;
4
+ try {
5
+ removeAbortListener();
6
+ } catch (error) {
7
+ cleanupError = error;
8
+ hasCleanupError = true;
9
+ }
10
+ try {
11
+ removeHandlers();
12
+ } catch (error) {
13
+ if (!hasCleanupError) {
14
+ cleanupError = error;
15
+ hasCleanupError = true;
16
+ }
17
+ }
18
+ if (hasCleanupError) throw cleanupError;
19
+ }
20
+
21
+ export default aggregateCleanup;
@@ -0,0 +1,9 @@
1
+ const createAbortCleanup = ({ signal }) => ({
2
+ remove: (abortHandler) => {
3
+ if (signal && abortHandler && typeof signal.removeEventListener === "function") {
4
+ signal.removeEventListener("abort", abortHandler);
5
+ }
6
+ },
7
+ });
8
+
9
+ export default createAbortCleanup;
@@ -0,0 +1,14 @@
1
+ import createAbortCleanup from "./cleanup.mjs";
2
+ import createAbortListener from "./listener.mjs";
3
+
4
+ function createAbortListenerRegistration({ signal, onAbort }) {
5
+ const abortCleanup = createAbortCleanup({ signal });
6
+ const abortListener = createAbortListener({ signal, onAbort });
7
+
8
+ return {
9
+ attach: abortListener.attach,
10
+ remove: () => abortCleanup.remove(abortListener.getHandler()),
11
+ };
12
+ }
13
+
14
+ export default createAbortListenerRegistration;
@@ -0,0 +1,16 @@
1
+ const createAbortListener = ({ signal, onAbort }) => {
2
+ let abortHandler;
3
+
4
+ const attach = () => {
5
+ if (!signal) return;
6
+ if (signal.aborted) onAbort();
7
+ else {
8
+ abortHandler = onAbort;
9
+ signal.addEventListener("abort", abortHandler, { once: true });
10
+ }
11
+ };
12
+
13
+ return { attach, getHandler: () => abortHandler };
14
+ };
15
+
16
+ export default createAbortListener;
@@ -0,0 +1,14 @@
1
+ import aggregateCleanup from "./aggregate-cleanup.mjs";
2
+
3
+ function createRegistrationCleanup({ removeAbortListener, removeHandlers }) {
4
+ let removed = false;
5
+ const cleanup = () => {
6
+ if (removed) return;
7
+ aggregateCleanup(removeAbortListener, removeHandlers);
8
+ removed = true;
9
+ };
10
+ cleanup.getRemoved = () => removed;
11
+ return cleanup;
12
+ }
13
+
14
+ export default createRegistrationCleanup;
@@ -0,0 +1,19 @@
1
+ import createAbortListenerRegistration from "./listener-registration.mjs";
2
+ import createRegistrationCleanup from "./registration-cleanup.mjs";
3
+
4
+ const createAbortRegistration = ({ signal, removeHandlers }) => {
5
+ let listenerRegistration;
6
+ const cleanup = createRegistrationCleanup({
7
+ removeAbortListener: () => listenerRegistration.remove(),
8
+ removeHandlers,
9
+ });
10
+ listenerRegistration = createAbortListenerRegistration({ signal, onAbort: cleanup });
11
+
12
+ return {
13
+ attach: listenerRegistration.attach,
14
+ removeHandlers: cleanup,
15
+ getRemoved: cleanup.getRemoved,
16
+ };
17
+ };
18
+
19
+ export default createAbortRegistration;
@@ -0,0 +1,36 @@
1
+ import __methods from "./exit.mjs";
2
+ const { createBeforeExit } = __methods;
3
+
4
+ function createBeforeExitRegistration({
5
+ isActive,
6
+ isShuttingDown,
7
+ getShutdownPromise,
8
+ setShuttingDown,
9
+ setShutdownPromise,
10
+ logSafely,
11
+ runHooks,
12
+ onHooksFinished,
13
+ }) {
14
+ const beginShutdown = () => {
15
+ let resolve;
16
+ let reject;
17
+ setShuttingDown(true);
18
+ const promise = new Promise((resolvePromise, rejectPromise) => {
19
+ resolve = resolvePromise;
20
+ reject = rejectPromise;
21
+ });
22
+ setShutdownPromise(promise);
23
+ return { promise, resolve, reject };
24
+ };
25
+ return createBeforeExit({
26
+ isActive,
27
+ isShuttingDown,
28
+ getShutdownPromise,
29
+ beginShutdown,
30
+ logSafely,
31
+ runHooks,
32
+ onHooksFinished,
33
+ });
34
+ }
35
+
36
+ export default createBeforeExitRegistration;
@@ -0,0 +1,33 @@
1
+ const withEventLoopKeepAlive = async (operation) => {
2
+ const timer = setInterval(() => {}, 2 ** 31 - 1);
3
+ try {
4
+ return await operation();
5
+ } finally {
6
+ clearInterval(timer);
7
+ }
8
+ };
9
+
10
+ const createBeforeExit = ({
11
+ isActive,
12
+ isShuttingDown,
13
+ getShutdownPromise,
14
+ beginShutdown,
15
+ logSafely,
16
+ runHooks,
17
+ onHooksFinished,
18
+ }) => {
19
+ return (code) => {
20
+ if (!isActive()) return undefined;
21
+ if (isShuttingDown()) return getShutdownPromise();
22
+ logSafely("debug", `Process exiting (code ${code}). Running shutdown hooks...`);
23
+ const shutdown = beginShutdown();
24
+ withEventLoopKeepAlive(() => runHooks("beforeExit"))
25
+ .then(() => {
26
+ onHooksFinished();
27
+ })
28
+ .then(shutdown.resolve, shutdown.reject);
29
+ return shutdown.promise;
30
+ };
31
+ };
32
+
33
+ export default { withEventLoopKeepAlive, createBeforeExit };
@@ -0,0 +1,27 @@
1
+ import clearRegistration from "./registration.mjs";
2
+
3
+ function removeProcessHandlers({
4
+ processObj,
5
+ listeners,
6
+ onBeforeExit,
7
+ deactivate,
8
+ deleteRegistration,
9
+ }) {
10
+ deactivate();
11
+ let removalError;
12
+ const handlers = [...listeners, ["beforeExit", onBeforeExit]];
13
+ for (const [name, listener] of handlers) {
14
+ try {
15
+ processObj.off(name, listener);
16
+ if (processObj.listeners(name).includes(listener)) {
17
+ throw new Error(`processObj.off did not remove the ${name} listener`);
18
+ }
19
+ } catch (error) {
20
+ removalError ??= error;
21
+ }
22
+ }
23
+ if (removalError) throw removalError;
24
+ clearRegistration(processObj, deleteRegistration);
25
+ }
26
+
27
+ export default removeProcessHandlers;
@@ -0,0 +1,5 @@
1
+ function clearRegistration(processObj, deleteRegistration) {
2
+ deleteRegistration(processObj);
3
+ }
4
+
5
+ export default clearRegistration;
@@ -0,0 +1,17 @@
1
+ import __methods from "./registration.mjs";
2
+ const { deleteRegistration } = __methods;
3
+ import removeProcessHandlers from "./cleanup/process-handlers.mjs";
4
+
5
+ const createCleanup = ({ processObj, listeners, onBeforeExit, deactivate }) => {
6
+ let removed = false;
7
+
8
+ const removeHandlers = () => {
9
+ if (removed) return;
10
+ removeProcessHandlers({ processObj, listeners, onBeforeExit, deactivate, deleteRegistration });
11
+ removed = true;
12
+ };
13
+
14
+ return { removeHandlers };
15
+ };
16
+
17
+ export default createCleanup;
@@ -0,0 +1 @@
1
+ export const defaultSignals = ["SIGTERM", "SIGINT", "SIGHUP"];