autotel 3.4.4 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-O4JZUCUE.js → chunk-66YJ66GG.js} +5 -158
- package/dist/chunk-66YJ66GG.js.map +1 -0
- package/dist/{chunk-Z6HRSM2Y.cjs → chunk-B7SWBE4P.cjs} +5 -5
- package/dist/{chunk-Z6HRSM2Y.cjs.map → chunk-B7SWBE4P.cjs.map} +1 -1
- package/dist/{chunk-DQEHQNQE.js → chunk-D4TM63S3.js} +3 -3
- package/dist/{chunk-DQEHQNQE.js.map → chunk-D4TM63S3.js.map} +1 -1
- package/dist/chunk-DCEDJQGG.js +28 -0
- package/dist/chunk-DCEDJQGG.js.map +1 -0
- package/dist/chunk-E6TERL5O.cjs +23 -0
- package/dist/chunk-E6TERL5O.cjs.map +1 -0
- package/dist/chunk-EE6CPXKH.cjs +164 -0
- package/dist/chunk-EE6CPXKH.cjs.map +1 -0
- package/dist/{chunk-GBFTC7Q7.cjs → chunk-EOFB7XCL.cjs} +6 -6
- package/dist/{chunk-GBFTC7Q7.cjs.map → chunk-EOFB7XCL.cjs.map} +1 -1
- package/dist/{chunk-Z7PW3KHL.cjs → chunk-FMTHVSYY.cjs} +4 -163
- package/dist/chunk-FMTHVSYY.cjs.map +1 -0
- package/dist/{chunk-VG2ABKJX.cjs → chunk-KYXZS3EA.cjs} +7 -7
- package/dist/{chunk-VG2ABKJX.cjs.map → chunk-KYXZS3EA.cjs.map} +1 -1
- package/dist/chunk-LVIPBYFE.js +157 -0
- package/dist/chunk-LVIPBYFE.js.map +1 -0
- package/dist/{chunk-NVGPMGI4.js → chunk-N25JDZSC.js} +3 -3
- package/dist/{chunk-NVGPMGI4.js.map → chunk-N25JDZSC.js.map} +1 -1
- package/dist/{chunk-AC5GNZKB.cjs → chunk-NENU7E6V.cjs} +5 -5
- package/dist/{chunk-AC5GNZKB.cjs.map → chunk-NENU7E6V.cjs.map} +1 -1
- package/dist/{chunk-URHPSJW2.js → chunk-QF7ARNUM.js} +3 -3
- package/dist/{chunk-URHPSJW2.js.map → chunk-QF7ARNUM.js.map} +1 -1
- package/dist/chunk-T5WRA76K.cjs +32 -0
- package/dist/chunk-T5WRA76K.cjs.map +1 -0
- package/dist/{chunk-YWCESU4Y.js → chunk-T7JO2TCP.js} +3 -3
- package/dist/{chunk-YWCESU4Y.js.map → chunk-T7JO2TCP.js.map} +1 -1
- package/dist/{chunk-O7JOKRN2.js → chunk-UIKYE2QZ.js} +3 -3
- package/dist/{chunk-O7JOKRN2.js.map → chunk-UIKYE2QZ.js.map} +1 -1
- package/dist/chunk-UNPLAVE7.js +21 -0
- package/dist/chunk-UNPLAVE7.js.map +1 -0
- package/dist/{chunk-FGNDN2FD.cjs → chunk-V7UBMJAB.cjs} +18 -18
- package/dist/{chunk-FGNDN2FD.cjs.map → chunk-V7UBMJAB.cjs.map} +1 -1
- package/dist/correlation-id.cjs +10 -9
- package/dist/correlation-id.js +2 -1
- package/dist/decorators.cjs +4 -3
- package/dist/decorators.cjs.map +1 -1
- package/dist/decorators.js +3 -2
- package/dist/decorators.js.map +1 -1
- package/dist/define-event-BL6Li7CM.d.ts +23 -0
- package/dist/define-event-ClP3T1Jx.d.cts +23 -0
- package/dist/event.cjs +6 -5
- package/dist/event.js +3 -2
- package/dist/functional.cjs +11 -10
- package/dist/functional.js +3 -2
- package/dist/http.cjs +3 -2
- package/dist/http.cjs.map +1 -1
- package/dist/http.js +2 -1
- package/dist/http.js.map +1 -1
- package/dist/index.cjs +91 -102
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -21
- package/dist/index.d.ts +2 -21
- package/dist/index.js +17 -27
- package/dist/index.js.map +1 -1
- package/dist/messaging.cjs +7 -6
- package/dist/messaging.js +4 -3
- package/dist/security-schema.cjs +69 -0
- package/dist/security-schema.cjs.map +1 -0
- package/dist/security-schema.d.cts +67 -0
- package/dist/security-schema.d.ts +67 -0
- package/dist/security-schema.js +59 -0
- package/dist/security-schema.js.map +1 -0
- package/dist/semantic-helpers.cjs +8 -7
- package/dist/semantic-helpers.js +4 -3
- package/dist/validate.cjs +138 -0
- package/dist/validate.cjs.map +1 -0
- package/dist/validate.d.cts +129 -0
- package/dist/validate.d.ts +129 -0
- package/dist/validate.js +133 -0
- package/dist/validate.js.map +1 -0
- package/dist/validation-attributes.cjs +20 -0
- package/dist/validation-attributes.cjs.map +1 -0
- package/dist/validation-attributes.d.cts +40 -0
- package/dist/validation-attributes.d.ts +40 -0
- package/dist/validation-attributes.js +3 -0
- package/dist/validation-attributes.js.map +1 -0
- package/dist/webhook.cjs +5 -4
- package/dist/webhook.cjs.map +1 -1
- package/dist/webhook.js +3 -2
- package/dist/webhook.js.map +1 -1
- package/dist/workflow-distributed.cjs +5 -4
- package/dist/workflow-distributed.cjs.map +1 -1
- package/dist/workflow-distributed.js +3 -2
- package/dist/workflow-distributed.js.map +1 -1
- package/dist/workflow.cjs +8 -7
- package/dist/workflow.js +4 -3
- package/package.json +23 -8
- package/src/define-event.ts +2 -21
- package/src/security-schema.test.ts +45 -0
- package/src/security-schema.ts +107 -0
- package/src/stable-hash.ts +27 -0
- package/src/validate.test.ts +285 -0
- package/src/validate.ts +301 -0
- package/src/validation-attributes.ts +43 -0
- package/dist/chunk-O4JZUCUE.js.map +0 -1
- package/dist/chunk-Z7PW3KHL.cjs.map +0 -1
package/dist/validate.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { VALIDATION_ATTR, VALIDATION_ISSUE_CAP, VALIDATION_METRICS } from './chunk-DCEDJQGG.js';
|
|
2
|
+
import { hashJson } from './chunk-UNPLAVE7.js';
|
|
3
|
+
import { createCounter } from './chunk-TQ5UWA7S.js';
|
|
4
|
+
import { createStructuredError } from './chunk-LVIPBYFE.js';
|
|
5
|
+
import './chunk-J5QENANM.js';
|
|
6
|
+
import './chunk-HA2WBOGQ.js';
|
|
7
|
+
import { trace } from '@opentelemetry/api';
|
|
8
|
+
|
|
9
|
+
var mismatchCounter;
|
|
10
|
+
function counter() {
|
|
11
|
+
if (!mismatchCounter) {
|
|
12
|
+
mismatchCounter = createCounter(VALIDATION_METRICS.mismatches, {
|
|
13
|
+
description: "Input payloads that did not match their declared shape"
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return mismatchCounter;
|
|
17
|
+
}
|
|
18
|
+
var listeners = /* @__PURE__ */ new Set();
|
|
19
|
+
function onValidationMismatch(handler) {
|
|
20
|
+
listeners.add(handler);
|
|
21
|
+
return () => {
|
|
22
|
+
listeners.delete(handler);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
var truncate = (values) => values.slice(0, VALIDATION_ISSUE_CAP).join(",");
|
|
26
|
+
function recordValidationMismatch(mismatch) {
|
|
27
|
+
try {
|
|
28
|
+
const paths = mismatch.issues.map((i) => i.path).filter(Boolean);
|
|
29
|
+
const codes = [...new Set(mismatch.issues.map((i) => i.code))];
|
|
30
|
+
const span = trace.getActiveSpan();
|
|
31
|
+
if (span) {
|
|
32
|
+
span.setAttributes({
|
|
33
|
+
[VALIDATION_ATTR.name]: mismatch.name,
|
|
34
|
+
[VALIDATION_ATTR.boundary]: mismatch.boundary,
|
|
35
|
+
[VALIDATION_ATTR.mode]: mismatch.mode,
|
|
36
|
+
[VALIDATION_ATTR.issueCount]: mismatch.issues.length,
|
|
37
|
+
[VALIDATION_ATTR.issuePaths]: truncate(paths),
|
|
38
|
+
[VALIDATION_ATTR.issueCodes]: truncate(codes),
|
|
39
|
+
...mismatch.hash ? { [VALIDATION_ATTR.hash]: mismatch.hash } : {},
|
|
40
|
+
...mismatch.severity ? { [VALIDATION_ATTR.severity]: mismatch.severity } : {}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
counter().add(1, {
|
|
45
|
+
boundary: mismatch.boundary,
|
|
46
|
+
validation: mismatch.name,
|
|
47
|
+
mode: mismatch.mode
|
|
48
|
+
});
|
|
49
|
+
} catch {
|
|
50
|
+
}
|
|
51
|
+
for (const listener of listeners) {
|
|
52
|
+
try {
|
|
53
|
+
listener(mismatch);
|
|
54
|
+
} catch {
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
} catch {
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function formatValidationIssues(error) {
|
|
61
|
+
const raw = extractRawIssues(error);
|
|
62
|
+
return raw.map((issue) => toSafeIssue(issue));
|
|
63
|
+
}
|
|
64
|
+
function extractRawIssues(error) {
|
|
65
|
+
if (error && typeof error === "object") {
|
|
66
|
+
const candidate = error.issues ?? error.errors;
|
|
67
|
+
if (Array.isArray(candidate)) {
|
|
68
|
+
return candidate.filter(
|
|
69
|
+
(i) => i !== null && typeof i === "object"
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
function toSafeIssue(issue) {
|
|
76
|
+
const rawPath = issue.path;
|
|
77
|
+
const path = Array.isArray(rawPath) ? rawPath.map(String).join(".") : typeof rawPath === "string" ? rawPath : "";
|
|
78
|
+
const code = typeof issue.code === "string" ? issue.code : "invalid";
|
|
79
|
+
const expected = typeof issue.expected === "string" ? issue.expected : void 0;
|
|
80
|
+
return expected ? { path, code, expected } : { path, code };
|
|
81
|
+
}
|
|
82
|
+
function defaultRejectError(issues, name) {
|
|
83
|
+
return createStructuredError({
|
|
84
|
+
name: "ValidationError",
|
|
85
|
+
status: 400,
|
|
86
|
+
code: "validation_failed",
|
|
87
|
+
message: `Input for "${name}" did not match its declared shape.`,
|
|
88
|
+
why: `${issues.length} field(s) failed validation: ${issues.map((i) => i.path || "(root)").slice(0, VALIDATION_ISSUE_CAP).join(", ")}.`,
|
|
89
|
+
fix: "Send a payload that matches the schema, or switch this validator to observe mode while you investigate.",
|
|
90
|
+
// PII-safe: paths + codes only, no received values.
|
|
91
|
+
details: { validation: name, issues }
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
function defineValidator(name, schema, options = {}) {
|
|
95
|
+
const mode = options.onMismatch ?? "reject";
|
|
96
|
+
const boundary = options.boundary ?? "input";
|
|
97
|
+
const hash = options.toJsonSchema ? hashJson(options.toJsonSchema(schema)) : void 0;
|
|
98
|
+
const record = (issues) => {
|
|
99
|
+
recordValidationMismatch({
|
|
100
|
+
name,
|
|
101
|
+
boundary,
|
|
102
|
+
mode,
|
|
103
|
+
issues,
|
|
104
|
+
hash,
|
|
105
|
+
severity: options.severity
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
name,
|
|
110
|
+
mode,
|
|
111
|
+
safeParse(input) {
|
|
112
|
+
const parsed = schema.safeParse(input);
|
|
113
|
+
if (parsed.success) return { success: true, data: parsed.data };
|
|
114
|
+
const issues = formatValidationIssues(parsed.error);
|
|
115
|
+
record(issues);
|
|
116
|
+
return { success: false, issues };
|
|
117
|
+
},
|
|
118
|
+
parse(input) {
|
|
119
|
+
const parsed = schema.safeParse(input);
|
|
120
|
+
if (parsed.success) return parsed.data;
|
|
121
|
+
const issues = formatValidationIssues(parsed.error);
|
|
122
|
+
record(issues);
|
|
123
|
+
if (mode === "reject") {
|
|
124
|
+
throw options.onReject?.(issues, name) ?? defaultRejectError(issues, name);
|
|
125
|
+
}
|
|
126
|
+
return input;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export { defineValidator, formatValidationIssues, onValidationMismatch, recordValidationMismatch };
|
|
132
|
+
//# sourceMappingURL=validate.js.map
|
|
133
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/validate.ts"],"names":[],"mappings":";;;;;;;;AA8DA,IAAI,eAAA;AACJ,SAAS,OAAA,GAA4C;AACnD,EAAA,IAAI,CAAC,eAAA,EAAiB;AACpB,IAAA,eAAA,GAAkB,aAAA,CAAc,mBAAmB,UAAA,EAAY;AAAA,MAC7D,WAAA,EA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3E;AAEA,MAAA,OAAO,KAAA;AAAA,IACT;AAAA,GACF;AACF","file":"validate.js","sourcesContent":["/**\n * Validation telemetry — connect runtime input validation (Zod or any\n * `safeParse` schema) to your traces and metrics at the boundaries where bad\n * data actually enters: HTTP bodies, events, messages.\n *\n * Today a `safeParse` failure either throws (no span, no metric, no alert) or\n * is silently swallowed in a handler. `defineValidator` makes the mismatch\n * **observable** — a `validation.*` span attribute set and a counter\n * incremented — with a per-validator `observe` vs `reject` mode:\n *\n * - `reject` (default): record telemetry, then throw a structured 400-shaped\n * error so the boundary can fail cleanly.\n * - `observe`: record telemetry, return the raw input so the handler continues\n * — useful for measuring real-world drift before you enforce it.\n *\n * **Not a security feature by default.** A malformed body is usually a bug or\n * version skew, not an attack. Validation telemetry is first-class on its own\n * metric; escalation to the security path is a deliberate opt-in via\n * {@link onValidationMismatch} (e.g. wired by `autotel-audit`), never automatic.\n *\n * **PII-safe by construction.** Only field *paths*, issue *codes*, and the\n * declared *type* are ever recorded — never the offending value, and never a\n * validator's error `message` (which routinely embeds the received value).\n */\n\nimport { trace } from '@opentelemetry/api';\nimport { createCounter } from './metric-helpers';\nimport { createStructuredError, type StructuredError } from './structured-error';\nimport { hashJson } from './stable-hash';\nimport type { SchemaLike } from './define-event';\nimport {\n VALIDATION_ATTR,\n VALIDATION_ISSUE_CAP,\n VALIDATION_METRICS,\n} from './validation-attributes';\n\nexport type { SchemaLike } from './define-event';\n\nexport type ValidationMode = 'observe' | 'reject';\nexport type ValidationSeverity = 'info' | 'warning' | 'error';\n\n/** A single failing field, stripped of any payload values. */\nexport interface ValidationIssue {\n /** Dotted field path, e.g. `items.0.price`. Never a value. */\n path: string;\n /** Issue code (e.g. Zod's `invalid_type`, `too_small`). Never a value. */\n code: string;\n /** Declared type/constraint summary, e.g. `string`. Never a received value. */\n expected?: string;\n}\n\n/** Everything the recorder needs — already PII-stripped by the caller. */\nexport interface ValidationMismatch {\n /** Contract id, e.g. `POST /orders` or `order.placed`. */\n name: string;\n boundary: string;\n mode: ValidationMode;\n issues: ValidationIssue[];\n hash?: string;\n severity?: ValidationSeverity;\n}\n\nlet mismatchCounter: ReturnType<typeof createCounter> | undefined;\nfunction counter(): ReturnType<typeof createCounter> {\n if (!mismatchCounter) {\n mismatchCounter = createCounter(VALIDATION_METRICS.mismatches, {\n description: 'Input payloads that did not match their declared shape',\n });\n }\n return mismatchCounter;\n}\n\ntype MismatchListener = (mismatch: ValidationMismatch) => void;\nconst listeners = new Set<MismatchListener>();\n\n/**\n * Register an explicit handler called on every recorded mismatch — the opt-in\n * seam for escalating to security events, a webhook, or a custom sink. There is\n * no automatic, package-presence-driven escalation: nothing fires here unless\n * you (or a package you wire up) register a handler.\n *\n * Multiple subscribers coexist: a package (e.g. `autotel-audit` bridging to\n * security events) and your own app code (a webhook, a logger) can both\n * register and all fire. Returns an unsubscribe fn that removes only this\n * handler; registering the same function twice is a no-op (Set semantics).\n */\nexport function onValidationMismatch(handler: MismatchListener): () => void {\n listeners.add(handler);\n return () => {\n listeners.delete(handler);\n };\n}\n\nconst truncate = (values: string[]): string =>\n values.slice(0, VALIDATION_ISSUE_CAP).join(',');\n\n/**\n * Record a validation mismatch as telemetry: `validation.*` attributes on the\n * active span (if any) and an increment on `autotel.validation.mismatches`.\n * Fail-open — never throws, so instrumentation can't break the boundary.\n */\nexport function recordValidationMismatch(mismatch: ValidationMismatch): void {\n try {\n const paths = mismatch.issues.map((i) => i.path).filter(Boolean);\n const codes = [...new Set(mismatch.issues.map((i) => i.code))];\n\n const span = trace.getActiveSpan();\n if (span) {\n span.setAttributes({\n [VALIDATION_ATTR.name]: mismatch.name,\n [VALIDATION_ATTR.boundary]: mismatch.boundary,\n [VALIDATION_ATTR.mode]: mismatch.mode,\n [VALIDATION_ATTR.issueCount]: mismatch.issues.length,\n [VALIDATION_ATTR.issuePaths]: truncate(paths),\n [VALIDATION_ATTR.issueCodes]: truncate(codes),\n ...(mismatch.hash ? { [VALIDATION_ATTR.hash]: mismatch.hash } : {}),\n ...(mismatch.severity\n ? { [VALIDATION_ATTR.severity]: mismatch.severity }\n : {}),\n });\n }\n\n try {\n counter().add(1, {\n boundary: mismatch.boundary,\n validation: mismatch.name,\n mode: mismatch.mode,\n });\n } catch {\n // meter not initialised yet — skip the count, keep the span attrs\n }\n\n // Dispatch to every subscriber with per-listener fault isolation: one\n // throwing subscriber must not starve its peers or break the boundary.\n // Set iteration tolerates concurrent (un)subscription safely.\n for (const listener of listeners) {\n try {\n listener(mismatch);\n } catch {\n // a misbehaving subscriber must not break the boundary or its peers\n }\n }\n } catch {\n // fail-open: telemetry must never break the validated boundary\n }\n}\n\n/**\n * Normalise an arbitrary validation error into PII-safe issues. Reads only\n * `path`, `code`, and (when it is a declared type name) `expected` — and never\n * `message`, `received`, or any value-bearing field. Understands the Zod shape\n * (`error.issues`) and a generic `error.errors` fallback; returns `[]` for\n * anything unrecognised.\n */\nexport function formatValidationIssues(error: unknown): ValidationIssue[] {\n const raw = extractRawIssues(error);\n return raw.map((issue) => toSafeIssue(issue));\n}\n\nfunction extractRawIssues(error: unknown): Array<Record<string, unknown>> {\n if (error && typeof error === 'object') {\n const candidate =\n (error as { issues?: unknown }).issues ??\n (error as { errors?: unknown }).errors;\n if (Array.isArray(candidate)) {\n return candidate.filter(\n (i): i is Record<string, unknown> => i !== null && typeof i === 'object',\n );\n }\n }\n return [];\n}\n\nfunction toSafeIssue(issue: Record<string, unknown>): ValidationIssue {\n const rawPath = issue.path;\n const path = Array.isArray(rawPath)\n ? rawPath.map(String).join('.')\n : typeof rawPath === 'string'\n ? rawPath\n : '';\n const code = typeof issue.code === 'string' ? issue.code : 'invalid';\n // `expected` is a declared type name in Zod (e.g. 'string'); safe. We never\n // read `received`/`message`/`value`, which can carry the offending payload.\n const expected =\n typeof issue.expected === 'string' ? issue.expected : undefined;\n return expected ? { path, code, expected } : { path, code };\n}\n\nexport interface DefineValidatorOptions<S> {\n /** Where validation runs. Defaults to `input`. */\n boundary?: string;\n /** `reject` (default): record then throw. `observe`: record then continue. */\n onMismatch?: ValidationMode;\n /** Project the schema to JSON Schema for a stable `validation.hash`. */\n toJsonSchema?: (schema: S) => unknown;\n severity?: ValidationSeverity;\n /** Build the error thrown in `reject` mode (defaults to a 400 structured error). */\n onReject?: (issues: ValidationIssue[], name: string) => Error;\n}\n\nexport type ValidatorResult<T> =\n | { success: true; data: T }\n | { success: false; issues: ValidationIssue[] };\n\nexport interface Validator<T> {\n readonly name: string;\n readonly mode: ValidationMode;\n /** Validate and record on failure; never throws. */\n safeParse(input: unknown): ValidatorResult<T>;\n /**\n * Validate, record on failure, then apply the mode: `reject` throws,\n * `observe` returns the raw input so the handler can continue.\n */\n parse(input: unknown): T;\n}\n\nfunction defaultRejectError(\n issues: ValidationIssue[],\n name: string,\n): StructuredError {\n return createStructuredError({\n name: 'ValidationError',\n status: 400,\n code: 'validation_failed',\n message: `Input for \"${name}\" did not match its declared shape.`,\n why: `${issues.length} field(s) failed validation: ${issues\n .map((i) => i.path || '(root)')\n .slice(0, VALIDATION_ISSUE_CAP)\n .join(', ')}.`,\n fix: 'Send a payload that matches the schema, or switch this validator to observe mode while you investigate.',\n // PII-safe: paths + codes only, no received values.\n details: { validation: name, issues },\n });\n}\n\n/**\n * Declare an expected input shape once and get a validator that records every\n * mismatch as telemetry.\n *\n * @example\n * ```ts\n * import { z } from 'zod';\n * import { defineValidator } from 'autotel/validate';\n *\n * const OrderBody = defineValidator('POST /orders', z.object({\n * items: z.array(z.object({ sku: z.string(), qty: z.number().int() })),\n * }), { boundary: 'http', toJsonSchema: (s) => z.toJSONSchema(s) });\n *\n * // reject mode (default): records + throws a 400-shaped structured error\n * const order = OrderBody.parse(req.body);\n *\n * // observe mode: records, returns the result, never throws\n * const result = OrderBody.safeParse(req.body);\n * if (!result.success) metrics.onDrift(result.issues);\n * ```\n */\nexport function defineValidator<T, S extends SchemaLike<T>>(\n name: string,\n schema: S,\n options: DefineValidatorOptions<S> = {},\n): Validator<T> {\n const mode = options.onMismatch ?? 'reject';\n const boundary = options.boundary ?? 'input';\n const hash = options.toJsonSchema\n ? hashJson(options.toJsonSchema(schema))\n : undefined;\n\n const record = (issues: ValidationIssue[]): void => {\n recordValidationMismatch({\n name,\n boundary,\n mode,\n issues,\n hash,\n severity: options.severity,\n });\n };\n\n return {\n name,\n mode,\n safeParse(input: unknown): ValidatorResult<T> {\n const parsed = schema.safeParse(input);\n if (parsed.success) return { success: true, data: parsed.data };\n const issues = formatValidationIssues(parsed.error);\n record(issues);\n return { success: false, issues };\n },\n parse(input: unknown): T {\n const parsed = schema.safeParse(input);\n if (parsed.success) return parsed.data;\n const issues = formatValidationIssues(parsed.error);\n record(issues);\n if (mode === 'reject') {\n throw options.onReject?.(issues, name) ?? defaultRejectError(issues, name);\n }\n // observe: continue with the raw input (documented type caveat)\n return input as T;\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkT5WRA76K_cjs = require('./chunk-T5WRA76K.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "VALIDATION_ATTR", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () { return chunkT5WRA76K_cjs.VALIDATION_ATTR; }
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, "VALIDATION_ISSUE_CAP", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return chunkT5WRA76K_cjs.VALIDATION_ISSUE_CAP; }
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(exports, "VALIDATION_METRICS", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return chunkT5WRA76K_cjs.VALIDATION_METRICS; }
|
|
18
|
+
});
|
|
19
|
+
//# sourceMappingURL=validation-attributes.cjs.map
|
|
20
|
+
//# sourceMappingURL=validation-attributes.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"validation-attributes.cjs"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation telemetry wire constants — the single source of truth for the
|
|
3
|
+
* `validation.*` span attributes and the `autotel.validation.mismatches` metric
|
|
4
|
+
* emitted when an input payload (HTTP body, event, message) fails to match its
|
|
5
|
+
* declared shape.
|
|
6
|
+
*
|
|
7
|
+
* Dependency-free and side-effect-free by design (mirrors `security-schema.ts`):
|
|
8
|
+
* safe to import from anything that only needs the constant strings — a
|
|
9
|
+
* dashboard, a CLI, an alert rule — without pulling in the OpenTelemetry SDK.
|
|
10
|
+
*
|
|
11
|
+
* These keys are a public API for the agents that query your telemetry. Treat a
|
|
12
|
+
* rename here the way you'd treat a breaking change to any other contract.
|
|
13
|
+
*/
|
|
14
|
+
declare const VALIDATION_ATTR: {
|
|
15
|
+
/** Contract id of the validated boundary, e.g. `POST /orders`, `order.placed`. */
|
|
16
|
+
readonly name: "validation.name";
|
|
17
|
+
/** Where validation ran: `http` | `event` | `message` | a custom label. */
|
|
18
|
+
readonly boundary: "validation.boundary";
|
|
19
|
+
/** `observe` (recorded, request continues) or `reject` (recorded, then failed). */
|
|
20
|
+
readonly mode: "validation.mode";
|
|
21
|
+
/** Stable hash of the declared shape, when a JSON-schema projection is given. */
|
|
22
|
+
readonly hash: "validation.hash";
|
|
23
|
+
/** `info` | `warning` | `error`. */
|
|
24
|
+
readonly severity: "validation.severity";
|
|
25
|
+
/** Number of failing fields. */
|
|
26
|
+
readonly issueCount: "validation.issue.count";
|
|
27
|
+
/** Comma-separated failing field paths (capped). Never contains values. */
|
|
28
|
+
readonly issuePaths: "validation.issue.paths";
|
|
29
|
+
/** Comma-separated distinct issue codes (capped). Never contains values. */
|
|
30
|
+
readonly issueCodes: "validation.issue.codes";
|
|
31
|
+
};
|
|
32
|
+
type ValidationAttributeKey = (typeof VALIDATION_ATTR)[keyof typeof VALIDATION_ATTR];
|
|
33
|
+
declare const VALIDATION_METRICS: {
|
|
34
|
+
/** Counter, labelled `{ boundary, validation, mode }`. */
|
|
35
|
+
readonly mismatches: "autotel.validation.mismatches";
|
|
36
|
+
};
|
|
37
|
+
/** Max field paths / codes stamped onto a span, to bound attribute size. */
|
|
38
|
+
declare const VALIDATION_ISSUE_CAP = 20;
|
|
39
|
+
|
|
40
|
+
export { VALIDATION_ATTR, VALIDATION_ISSUE_CAP, VALIDATION_METRICS, type ValidationAttributeKey };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation telemetry wire constants — the single source of truth for the
|
|
3
|
+
* `validation.*` span attributes and the `autotel.validation.mismatches` metric
|
|
4
|
+
* emitted when an input payload (HTTP body, event, message) fails to match its
|
|
5
|
+
* declared shape.
|
|
6
|
+
*
|
|
7
|
+
* Dependency-free and side-effect-free by design (mirrors `security-schema.ts`):
|
|
8
|
+
* safe to import from anything that only needs the constant strings — a
|
|
9
|
+
* dashboard, a CLI, an alert rule — without pulling in the OpenTelemetry SDK.
|
|
10
|
+
*
|
|
11
|
+
* These keys are a public API for the agents that query your telemetry. Treat a
|
|
12
|
+
* rename here the way you'd treat a breaking change to any other contract.
|
|
13
|
+
*/
|
|
14
|
+
declare const VALIDATION_ATTR: {
|
|
15
|
+
/** Contract id of the validated boundary, e.g. `POST /orders`, `order.placed`. */
|
|
16
|
+
readonly name: "validation.name";
|
|
17
|
+
/** Where validation ran: `http` | `event` | `message` | a custom label. */
|
|
18
|
+
readonly boundary: "validation.boundary";
|
|
19
|
+
/** `observe` (recorded, request continues) or `reject` (recorded, then failed). */
|
|
20
|
+
readonly mode: "validation.mode";
|
|
21
|
+
/** Stable hash of the declared shape, when a JSON-schema projection is given. */
|
|
22
|
+
readonly hash: "validation.hash";
|
|
23
|
+
/** `info` | `warning` | `error`. */
|
|
24
|
+
readonly severity: "validation.severity";
|
|
25
|
+
/** Number of failing fields. */
|
|
26
|
+
readonly issueCount: "validation.issue.count";
|
|
27
|
+
/** Comma-separated failing field paths (capped). Never contains values. */
|
|
28
|
+
readonly issuePaths: "validation.issue.paths";
|
|
29
|
+
/** Comma-separated distinct issue codes (capped). Never contains values. */
|
|
30
|
+
readonly issueCodes: "validation.issue.codes";
|
|
31
|
+
};
|
|
32
|
+
type ValidationAttributeKey = (typeof VALIDATION_ATTR)[keyof typeof VALIDATION_ATTR];
|
|
33
|
+
declare const VALIDATION_METRICS: {
|
|
34
|
+
/** Counter, labelled `{ boundary, validation, mode }`. */
|
|
35
|
+
readonly mismatches: "autotel.validation.mismatches";
|
|
36
|
+
};
|
|
37
|
+
/** Max field paths / codes stamped onto a span, to bound attribute size. */
|
|
38
|
+
declare const VALIDATION_ISSUE_CAP = 20;
|
|
39
|
+
|
|
40
|
+
export { VALIDATION_ATTR, VALIDATION_ISSUE_CAP, VALIDATION_METRICS, type ValidationAttributeKey };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"validation-attributes.js"}
|
package/dist/webhook.cjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var chunk4P6ZOARG_cjs = require('./chunk-4P6ZOARG.cjs');
|
|
4
|
-
var
|
|
4
|
+
var chunkV7UBMJAB_cjs = require('./chunk-V7UBMJAB.cjs');
|
|
5
5
|
require('./chunk-OPPXYVEZ.cjs');
|
|
6
6
|
require('./chunk-VQTCQKHQ.cjs');
|
|
7
|
-
|
|
7
|
+
require('./chunk-FMTHVSYY.cjs');
|
|
8
|
+
var chunkEE6CPXKH_cjs = require('./chunk-EE6CPXKH.cjs');
|
|
8
9
|
require('./chunk-R7QYGZUP.cjs');
|
|
9
10
|
require('./chunk-QWW3E3JM.cjs');
|
|
10
11
|
require('./chunk-CEAQK2QY.cjs');
|
|
@@ -157,7 +158,7 @@ function createParkingLot(config) {
|
|
|
157
158
|
},
|
|
158
159
|
traceCallback(callbackConfig) {
|
|
159
160
|
return (fnFactory) => {
|
|
160
|
-
return
|
|
161
|
+
return chunkV7UBMJAB_cjs.trace(
|
|
161
162
|
{
|
|
162
163
|
name: callbackConfig.name,
|
|
163
164
|
spanKind: api.SpanKind.SERVER
|
|
@@ -201,7 +202,7 @@ function createParkingLot(config) {
|
|
|
201
202
|
const error = new Error(
|
|
202
203
|
`Required parked context not found for key: ${correlationKey}`
|
|
203
204
|
);
|
|
204
|
-
|
|
205
|
+
chunkEE6CPXKH_cjs.recordStructuredError(baseCtx, error);
|
|
205
206
|
throw error;
|
|
206
207
|
}
|
|
207
208
|
}
|
package/dist/webhook.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/webhook.ts"],"names":["otelTrace","emitCorrelatedEvent","trace","SpanKind","recordStructuredError"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAiQO,IAAM,4BAAN,MAA6D;AAAA,EAIlE,WAAA,CACU,OAAA,GAGJ,EAAC,EACL;AAJQ,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AAMR,IAAA,MAAM,SAAA,GAAY,QAAQ,iBAAA,IAAqB,GAAA;AAC/C,IAAA,IAAI,YAAY,CAAA,EAAG;AACjB,MAAA,IAAA,CAAK,kBAAkB,WAAA,CAAY,MAAM,IAAA,CAAK,OAAA,IAAW,SAAS,CAAA;AAElE,MAAA,IAAI,IAAA,CAAK,gBAAgB,KAAA,EAAO;AAC9B,QAAA,IAAA,CAAK,gBAAgB,KAAA,EAAM;AAAA,MAC7B;AAAA,IACF;AAAA,EACF;AAAA,EAdU,OAAA;AAAA,EAJF,KAAA,uBAAY,GAAA,EAAgC;AAAA,EAC5C,eAAA,GAAyD,IAAA;AAAA,EAmBjE,MAAM,IAAA,CAAK,GAAA,EAAa,OAAA,EAA4C;AAClE,IAAA,IAAA,CAAK,KAAA,CAAM,GAAA,CAAI,GAAA,EAAK,OAAO,CAAA;AAAA,EAC7B;AAAA,EAEA,MAAM,KAAK,GAAA,EAAiD;AAC1D,IAAA,MAAM,OAAA,GAAU,IAAA,CA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AA;AAC9B;AACF,OACF;AAAA,IACF,CAAA;AAAA,IAEA,MAAM,OAAO,cAAA,EAA0C;AACrD,MAAA,MAAM,OAAA,GAAU,UAAU,cAAc,CAAA;AACxC,MAAA,MAAM,OAAA,GAAU,MAAM,KAAA,CAAM,IAAA,CAAK,OAAO,CAAA;AACxC,MAAA,OAAO,OAAA,KAAY,IAAA;AAAA,IACrB;AAAA,GACF;AAEA,EAAA,OAAO,UAAA;AACT;AAkBO,SAAS,wBAAwB,KAAA,EAAoC;AAC1E,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,MAAM,CAAA,CAAE,KAAK,GAAG,CAAA;AACnC;AAQO,SAAS,cAAc,aAAA,EAAgD;AAC5E,EAAA,OAAO;AAAA,IACL,SAAS,aAAA,CAAc,OAAA;AAAA,IACvB,QAAQ,aAAA,CAAc,MAAA;AAAA,IACtB,YAAY,aAAA,CAAc,UAAA;AAAA,IAC1B,QAAA,EAAU;AAAA,GACZ;AACF","file":"webhook.cjs","sourcesContent":["/**\n * Webhook and callback tracing with the \"Parking Lot\" pattern\n *\n * When initiating async operations that return hours/days later (webhooks,\n * payment callbacks, human approvals), you can't keep a span open. This module\n * provides utilities to \"park\" trace context and retrieve it when callbacks arrive.\n *\n * @example Stripe payment webhook\n * ```typescript\n * import { createParkingLot, InMemoryTraceContextStore } from 'autotel/webhook';\n *\n * const parkingLot = createParkingLot({\n * store: new InMemoryTraceContextStore(),\n * defaultTTLMs: 24 * 60 * 60 * 1000, // 24 hours\n * });\n *\n * // When initiating payment\n * export const initiatePayment = trace(ctx => async (orderId: string) => {\n * await parkingLot.park(`payment:${orderId}`, { orderId });\n * await stripeClient.createPaymentIntent({ metadata: { orderId } });\n * });\n *\n * // When Stripe webhook arrives (hours later)\n * export const handleStripeWebhook = parkingLot.traceCallback({\n * name: 'stripe.webhook.payment_intent.succeeded',\n * correlationKeyFrom: (event) => `payment:${event.data.object.metadata.orderId}`,\n * })(ctx => async (event: Stripe.Event) => {\n * // ctx.parkedContext contains the original trace context\n * // ctx.elapsedMs shows time since payment was initiated\n * await fulfillOrder(event.data.object);\n * });\n * ```\n *\n * @module\n */\n\nimport { SpanKind, trace as otelTrace } from '@opentelemetry/api';\nimport type { SpanContext, Link } from '@opentelemetry/api';\nimport { emitCorrelatedEvent } from './correlated-events';\nimport { trace } from './functional';\nimport type { AttributeValue, TraceContext } from './trace-context';\nimport { recordStructuredError } from './structured-error';\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Stored trace context for parking lot pattern\n */\nexport interface StoredTraceContext {\n /** Trace ID from the original span */\n traceId: string;\n\n /** Span ID from the original span */\n spanId: string;\n\n /** Trace flags (sampling decision) */\n traceFlags: number;\n\n /** When the context was parked */\n parkedAt: number;\n\n /** Optional TTL in milliseconds */\n ttlMs?: number;\n\n /** User-provided metadata */\n metadata?: Record<string, string>;\n}\n\n/**\n * Interface for trace context storage backends\n *\n * Implement this interface to use different storage backends (Redis, DynamoDB, etc.)\n */\nexport interface TraceContextStore {\n /**\n * Save trace context with a correlation key\n *\n * @param key - Unique correlation key (e.g., \"payment:order-123\")\n * @param context - The trace context to store\n */\n save(key: string, context: StoredTraceContext): Promise<void>;\n\n /**\n * Load trace context by correlation key\n *\n * @param key - The correlation key used when parking\n * @returns The stored context, or null if not found/expired\n */\n load(key: string): Promise<StoredTraceContext | null>;\n\n /**\n * Delete trace context by correlation key\n *\n * @param key - The correlation key to delete\n */\n delete(key: string): Promise<void>;\n}\n\n/**\n * Configuration for creating a parking lot\n */\nexport interface ParkingLotConfig {\n /** Storage backend for parked contexts */\n store: TraceContextStore;\n\n /** Default TTL in milliseconds (default: 24 hours) */\n defaultTTLMs?: number;\n\n /** Prefix for all correlation keys (default: \"parkingLot:\") */\n keyPrefix?: string;\n\n /** Whether to auto-delete after retrieval (default: true) */\n autoDeleteOnRetrieve?: boolean;\n\n /** Callback when context expires or is not found */\n onMiss?: (correlationKey: string) => void;\n}\n\n/**\n * Configuration for traceCallback wrapper\n */\nexport interface CallbackConfig {\n /** Span name for the callback handler */\n name: string;\n\n /**\n * Extract correlation key from callback arguments\n *\n * @example\n * ```typescript\n * correlationKeyFrom: (event) => `payment:${event.data.orderId}`\n * ```\n */\n correlationKeyFrom: (args: unknown[]) => string;\n\n /** Additional span attributes */\n attributes?: Record<string, string | number | boolean>;\n\n /** Whether to fail if parked context is not found (default: false) */\n requireParkedContext?: boolean;\n}\n\n/**\n * Extended context for callback handlers\n */\nexport interface CallbackContext extends TraceContext {\n /** The retrieved parked context, if found */\n parkedContext: StoredTraceContext | null;\n\n /** Time elapsed since context was parked (ms), or null if not found */\n elapsedMs: number | null;\n\n /** The correlation key used for retrieval */\n correlationKey: string;\n}\n\n/**\n * The parking lot instance\n */\nexport interface ParkingLot {\n /**\n * Park current trace context before initiating async operation\n *\n * Call this before sending a webhook, initiating a payment, or starting\n * any operation that will complete via callback.\n *\n * @param correlationKey - Unique key to retrieve context later (e.g., \"payment:order-123\")\n * @param metadata - Optional metadata to store with the context\n * @returns The correlation key (with prefix applied)\n *\n * @example\n * ```typescript\n * await parkingLot.park(`payment:${orderId}`, {\n * customerId: customer.id,\n * amount: payment.amount.toString(),\n * });\n * ```\n */\n park(\n correlationKey: string,\n metadata?: Record<string, string>,\n ): Promise<string>;\n\n /**\n * Retrieve parked context when callback arrives\n *\n * @param correlationKey - The key used when parking\n * @returns The stored context, or null if not found/expired\n */\n retrieve(correlationKey: string): Promise<StoredTraceContext | null>;\n\n /**\n * Wrap a callback handler with automatic context retrieval and linking\n *\n * Creates a traced function that:\n * 1. Extracts correlation key from arguments\n * 2. Retrieves parked context from storage\n * 3. Creates a span link to the original trace\n * 4. Provides elapsed time since parking\n *\n * @param config - Callback configuration\n * @returns Factory function for the callback handler\n *\n * @example\n * ```typescript\n * export const handleWebhook = parkingLot.traceCallback({\n * name: 'webhook.payment.completed',\n * correlationKeyFrom: (args) => `payment:${args[0].orderId}`,\n * })(ctx => async (event) => {\n * console.log(`Payment completed after ${ctx.elapsedMs}ms`);\n * await processPayment(event);\n * });\n * ```\n */\n traceCallback<TArgs extends unknown[], TReturn>(\n config: CallbackConfig,\n ): (\n fnFactory: (ctx: CallbackContext) => (...args: TArgs) => Promise<TReturn>,\n ) => (...args: TArgs) => Promise<TReturn>;\n\n /**\n * Manually create a span link from stored context\n *\n * Useful when you need more control over span creation.\n *\n * @param storedContext - The stored trace context\n * @returns A span link that can be added to a span\n */\n createLink(storedContext: StoredTraceContext): Link;\n\n /**\n * Check if a parked context exists (without retrieving/deleting it)\n *\n * @param correlationKey - The key to check\n * @returns True if context exists and hasn't expired\n */\n exists(correlationKey: string): Promise<boolean>;\n}\n\n// ============================================================================\n// In-Memory Store (for testing and development)\n// ============================================================================\n\n/**\n * In-memory trace context store\n *\n * Useful for testing and development. For production, use a persistent\n * store like Redis or DynamoDB.\n *\n * @example\n * ```typescript\n * const store = new InMemoryTraceContextStore();\n * const parkingLot = createParkingLot({ store });\n * ```\n */\nexport class InMemoryTraceContextStore implements TraceContextStore {\n private store = new Map<string, StoredTraceContext>();\n private cleanupInterval: ReturnType<typeof setInterval> | null = null;\n\n constructor(\n private options: {\n /** Cleanup interval in ms (default: 60000) */\n cleanupIntervalMs?: number;\n } = {},\n ) {\n // Start periodic cleanup of expired entries\n const cleanupMs = options.cleanupIntervalMs ?? 60_000;\n if (cleanupMs > 0) {\n this.cleanupInterval = setInterval(() => this.cleanup(), cleanupMs);\n // Don't prevent process exit\n if (this.cleanupInterval.unref) {\n this.cleanupInterval.unref();\n }\n }\n }\n\n async save(key: string, context: StoredTraceContext): Promise<void> {\n this.store.set(key, context);\n }\n\n async load(key: string): Promise<StoredTraceContext | null> {\n const context = this.store.get(key);\n if (!context) {\n return null;\n }\n\n // Check TTL expiration\n if (context.ttlMs) {\n const age = Date.now() - context.parkedAt;\n if (age > context.ttlMs) {\n this.store.delete(key);\n return null;\n }\n }\n\n return context;\n }\n\n async delete(key: string): Promise<void> {\n this.store.delete(key);\n }\n\n /**\n * Get number of stored contexts (for testing)\n */\n get size(): number {\n return this.store.size;\n }\n\n /**\n * Clear all stored contexts (for testing)\n */\n clear(): void {\n this.store.clear();\n }\n\n /**\n * Stop the cleanup interval\n */\n destroy(): void {\n if (this.cleanupInterval) {\n clearInterval(this.cleanupInterval);\n this.cleanupInterval = null;\n }\n }\n\n private cleanup(): void {\n const now = Date.now();\n for (const [key, context] of this.store.entries()) {\n if (context.ttlMs) {\n const age = now - context.parkedAt;\n if (age > context.ttlMs) {\n this.store.delete(key);\n }\n }\n }\n }\n}\n\n// ============================================================================\n// Parking Lot Factory\n// ============================================================================\n\n/**\n * Create a parking lot for trace context storage and retrieval\n *\n * @param config - Parking lot configuration\n * @returns A parking lot instance\n *\n * @example Basic usage\n * ```typescript\n * const parkingLot = createParkingLot({\n * store: new InMemoryTraceContextStore(),\n * defaultTTLMs: 24 * 60 * 60 * 1000, // 24 hours\n * });\n * ```\n *\n * @example With Redis store\n * ```typescript\n * class RedisTraceContextStore implements TraceContextStore {\n * constructor(private redis: Redis) {}\n *\n * async save(key: string, context: StoredTraceContext) {\n * const ttlSeconds = context.ttlMs ? Math.ceil(context.ttlMs / 1000) : 86400;\n * await this.redis.setex(key, ttlSeconds, JSON.stringify(context));\n * }\n *\n * async load(key: string) {\n * const data = await this.redis.get(key);\n * return data ? JSON.parse(data) : null;\n * }\n *\n * async delete(key: string) {\n * await this.redis.del(key);\n * }\n * }\n *\n * const parkingLot = createParkingLot({\n * store: new RedisTraceContextStore(redis),\n * });\n * ```\n */\nexport function createParkingLot(config: ParkingLotConfig): ParkingLot {\n const {\n store,\n defaultTTLMs = 24 * 60 * 60 * 1000, // 24 hours\n keyPrefix = 'parkingLot:',\n autoDeleteOnRetrieve = true,\n onMiss,\n } = config;\n\n /**\n * Get current span context from active context\n */\n function getCurrentSpanContext(): SpanContext | null {\n const activeSpan = otelTrace.getActiveSpan();\n if (!activeSpan) {\n return null;\n }\n return activeSpan.spanContext();\n }\n\n /**\n * Apply key prefix\n */\n function prefixKey(key: string): string {\n return `${keyPrefix}${key}`;\n }\n\n const parkingLot: ParkingLot = {\n async park(\n correlationKey: string,\n metadata?: Record<string, string>,\n ): Promise<string> {\n const spanContext = getCurrentSpanContext();\n const fullKey = prefixKey(correlationKey);\n\n const storedContext: StoredTraceContext = {\n traceId: spanContext?.traceId ?? '',\n spanId: spanContext?.spanId ?? '',\n traceFlags: spanContext?.traceFlags ?? 0,\n parkedAt: Date.now(),\n ttlMs: defaultTTLMs,\n metadata,\n };\n\n await store.save(fullKey, storedContext);\n\n const activeSpan = otelTrace.getActiveSpan();\n if (activeSpan) {\n const parkAttrs: Record<string, AttributeValue> = {\n 'parking_lot.correlation_key': correlationKey,\n 'parking_lot.ttl_ms': defaultTTLMs,\n ...(metadata &&\n Object.fromEntries(\n Object.entries(metadata).map(([k, v]) => [\n `parking_lot.metadata.${k}`,\n v,\n ]),\n )),\n };\n emitCorrelatedEvent(\n {\n setAttribute: (k, v) => activeSpan.setAttribute(k, v),\n setAttributes: (a) => activeSpan.setAttributes(a),\n addEvent: (n, a) => activeSpan.addEvent(n, a),\n },\n 'trace_context_parked',\n parkAttrs,\n );\n }\n\n // Return the unprefixed key so callers can use the same key for retrieve()\n return correlationKey;\n },\n\n async retrieve(correlationKey: string): Promise<StoredTraceContext | null> {\n const fullKey = prefixKey(correlationKey);\n const storedContext = await store.load(fullKey);\n\n if (!storedContext) {\n onMiss?.(correlationKey);\n return null;\n }\n\n if (autoDeleteOnRetrieve) {\n await store.delete(fullKey);\n }\n\n return storedContext;\n },\n\n traceCallback<TArgs extends unknown[], TReturn>(\n callbackConfig: CallbackConfig,\n ): (\n fnFactory: (ctx: CallbackContext) => (...args: TArgs) => Promise<TReturn>,\n ) => (...args: TArgs) => Promise<TReturn> {\n return (\n fnFactory: (\n ctx: CallbackContext,\n ) => (...args: TArgs) => Promise<TReturn>,\n ): ((...args: TArgs) => Promise<TReturn>) => {\n return trace<TArgs, TReturn>(\n {\n name: callbackConfig.name,\n spanKind: SpanKind.SERVER,\n },\n (baseCtx) => {\n return async (...args: TArgs) => {\n // Extract correlation key from arguments\n const correlationKey = callbackConfig.correlationKeyFrom(args);\n\n // Retrieve parked context\n const parkedContext = await parkingLot.retrieve(correlationKey);\n\n // Calculate elapsed time\n const elapsedMs = parkedContext\n ? Date.now() - parkedContext.parkedAt\n : null;\n\n // Set span attributes\n baseCtx.setAttribute(\n 'parking_lot.correlation_key',\n correlationKey,\n );\n\n if (parkedContext) {\n baseCtx.setAttribute('parking_lot.elapsed_ms', elapsedMs!);\n baseCtx.setAttribute(\n 'parking_lot.original_trace_id',\n parkedContext.traceId,\n );\n baseCtx.setAttribute(\n 'parking_lot.original_span_id',\n parkedContext.spanId,\n );\n\n // Add metadata as attributes\n if (parkedContext.metadata) {\n for (const [key, value] of Object.entries(\n parkedContext.metadata,\n )) {\n baseCtx.setAttribute(`parking_lot.metadata.${key}`, value);\n }\n }\n\n // Create span link to original trace\n const link = parkingLot.createLink(parkedContext);\n baseCtx.addLinks([link]);\n\n emitCorrelatedEvent(baseCtx, 'parked_context_retrieved', {\n 'parking_lot.correlation_key': correlationKey,\n 'parking_lot.elapsed_ms': elapsedMs!,\n 'parking_lot.original_trace_id': parkedContext.traceId,\n });\n } else {\n baseCtx.setAttribute('parking_lot.context_found', false);\n\n if (callbackConfig.requireParkedContext) {\n const error = new Error(\n `Required parked context not found for key: ${correlationKey}`,\n );\n recordStructuredError(baseCtx, error);\n throw error;\n }\n }\n\n // Apply custom attributes\n if (callbackConfig.attributes) {\n for (const [key, value] of Object.entries(\n callbackConfig.attributes,\n )) {\n baseCtx.setAttribute(key, value);\n }\n }\n\n // Create extended context\n const callbackCtx: CallbackContext = {\n ...baseCtx,\n parkedContext,\n elapsedMs,\n correlationKey,\n };\n\n // Execute user's function\n const userFn = fnFactory(callbackCtx);\n return userFn(...args);\n };\n },\n );\n };\n },\n\n createLink(storedContext: StoredTraceContext): Link {\n return {\n context: {\n traceId: storedContext.traceId,\n spanId: storedContext.spanId,\n traceFlags: storedContext.traceFlags,\n isRemote: true,\n },\n attributes: {\n 'link.type': 'parking_lot',\n 'parking_lot.parked_at': storedContext.parkedAt,\n ...(storedContext.metadata && {\n 'parking_lot.has_metadata': true,\n }),\n },\n };\n },\n\n async exists(correlationKey: string): Promise<boolean> {\n const fullKey = prefixKey(correlationKey);\n const context = await store.load(fullKey);\n return context !== null;\n },\n };\n\n return parkingLot;\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/**\n * Create a correlation key from multiple parts\n *\n * @param parts - Key parts to join\n * @returns A correlation key string\n *\n * @example\n * ```typescript\n * const key = createCorrelationKey('payment', orderId, 'stripe');\n * // Returns: \"payment:order-123:stripe\"\n * ```\n */\nexport function createCorrelationKey(...parts: (string | number)[]): string {\n return parts.map(String).join(':');\n}\n\n/**\n * Extract span context from stored context for manual linking\n *\n * @param storedContext - The stored trace context\n * @returns SpanContext compatible object\n */\nexport function toSpanContext(storedContext: StoredTraceContext): SpanContext {\n return {\n traceId: storedContext.traceId,\n spanId: storedContext.spanId,\n traceFlags: storedContext.traceFlags,\n isRemote: true,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/webhook.ts"],"names":["otelTrace","emitCorrelatedEvent","trace","SpanKind","recordStructuredError"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiQO,IAAM,4BAAN,MAA6D;AAAA,EAIlE,WAAA,CACU,OAAA,GAGJ,EAAC,EACL;AAJQ,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AAMR,IAAA,MAAM,SAAA,GAAY,QAAQ,iBAAA,IAAqB,GAAA;AAC/C,IAAA,IAAI,YAAY,CAAA,EAAG;AACjB,MAAA,IAAA,CAAK,kBAAkB,WAAA,CAAY,MAAM,IAAA,CAAK,OAAA,IAAW,SAAS,CAAA;AAElE,MAAA,IAAI,IAAA,CAAK,gBAAgB,KAAA,EAAO;AAC9B,QAAA,IAAA,CAAK,gBAAgB,KAAA,EAAM;AAAA,MAC7B;AAAA,IACF;AAAA,EACF;AAAA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AS;AAAA,UACP,SAAS,aAAA,CAAc,OAAA;AAAA,UACvB,QAAQ,aAAA,CAAc,MAAA;AAAA,UACtB,YAAY,aAAA,CAAc,UAAA;AAAA,UAC1B,QAAA,EAAU;AAAA,SACZ;AAAA,QACA,UAAA,EAAY;AAAA,UACV,WAAA,EAAa,aAAA;AAAA,UACb,yBAAyB,aAAA,CAAc,QAAA;AAAA,UACvC,GAAI,cAAc,QAAA,IAAY;AAAA,YAC5B,0BAAA,EAA4B;AAAA;AAC9B;AACF,OACF;AAAA,IACF,CAAA;AAAA,IAEA,MAAM,OAAO,cAAA,EAA0C;AACrD,MAAA,MAAM,OAAA,GAAU,UAAU,cAAc,CAAA;AACxC,MAAA,MAAM,OAAA,GAAU,MAAM,KAAA,CAAM,IAAA,CAAK,OAAO,CAAA;AACxC,MAAA,OAAO,OAAA,KAAY,IAAA;AAAA,IACrB;AAAA,GACF;AAEA,EAAA,OAAO,UAAA;AACT;AAkBO,SAAS,wBAAwB,KAAA,EAAoC;AAC1E,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,MAAM,CAAA,CAAE,KAAK,GAAG,CAAA;AACnC;AAQO,SAAS,cAAc,aAAA,EAAgD;AAC5E,EAAA,OAAO;AAAA,IACL,SAAS,aAAA,CAAc,OAAA;AAAA,IACvB,QAAQ,aAAA,CAAc,MAAA;AAAA,IACtB,YAAY,aAAA,CAAc,UAAA;AAAA,IAC1B,QAAA,EAAU;AAAA,GACZ;AACF","file":"webhook.cjs","sourcesContent":["/**\n * Webhook and callback tracing with the \"Parking Lot\" pattern\n *\n * When initiating async operations that return hours/days later (webhooks,\n * payment callbacks, human approvals), you can't keep a span open. This module\n * provides utilities to \"park\" trace context and retrieve it when callbacks arrive.\n *\n * @example Stripe payment webhook\n * ```typescript\n * import { createParkingLot, InMemoryTraceContextStore } from 'autotel/webhook';\n *\n * const parkingLot = createParkingLot({\n * store: new InMemoryTraceContextStore(),\n * defaultTTLMs: 24 * 60 * 60 * 1000, // 24 hours\n * });\n *\n * // When initiating payment\n * export const initiatePayment = trace(ctx => async (orderId: string) => {\n * await parkingLot.park(`payment:${orderId}`, { orderId });\n * await stripeClient.createPaymentIntent({ metadata: { orderId } });\n * });\n *\n * // When Stripe webhook arrives (hours later)\n * export const handleStripeWebhook = parkingLot.traceCallback({\n * name: 'stripe.webhook.payment_intent.succeeded',\n * correlationKeyFrom: (event) => `payment:${event.data.object.metadata.orderId}`,\n * })(ctx => async (event: Stripe.Event) => {\n * // ctx.parkedContext contains the original trace context\n * // ctx.elapsedMs shows time since payment was initiated\n * await fulfillOrder(event.data.object);\n * });\n * ```\n *\n * @module\n */\n\nimport { SpanKind, trace as otelTrace } from '@opentelemetry/api';\nimport type { SpanContext, Link } from '@opentelemetry/api';\nimport { emitCorrelatedEvent } from './correlated-events';\nimport { trace } from './functional';\nimport type { AttributeValue, TraceContext } from './trace-context';\nimport { recordStructuredError } from './structured-error';\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Stored trace context for parking lot pattern\n */\nexport interface StoredTraceContext {\n /** Trace ID from the original span */\n traceId: string;\n\n /** Span ID from the original span */\n spanId: string;\n\n /** Trace flags (sampling decision) */\n traceFlags: number;\n\n /** When the context was parked */\n parkedAt: number;\n\n /** Optional TTL in milliseconds */\n ttlMs?: number;\n\n /** User-provided metadata */\n metadata?: Record<string, string>;\n}\n\n/**\n * Interface for trace context storage backends\n *\n * Implement this interface to use different storage backends (Redis, DynamoDB, etc.)\n */\nexport interface TraceContextStore {\n /**\n * Save trace context with a correlation key\n *\n * @param key - Unique correlation key (e.g., \"payment:order-123\")\n * @param context - The trace context to store\n */\n save(key: string, context: StoredTraceContext): Promise<void>;\n\n /**\n * Load trace context by correlation key\n *\n * @param key - The correlation key used when parking\n * @returns The stored context, or null if not found/expired\n */\n load(key: string): Promise<StoredTraceContext | null>;\n\n /**\n * Delete trace context by correlation key\n *\n * @param key - The correlation key to delete\n */\n delete(key: string): Promise<void>;\n}\n\n/**\n * Configuration for creating a parking lot\n */\nexport interface ParkingLotConfig {\n /** Storage backend for parked contexts */\n store: TraceContextStore;\n\n /** Default TTL in milliseconds (default: 24 hours) */\n defaultTTLMs?: number;\n\n /** Prefix for all correlation keys (default: \"parkingLot:\") */\n keyPrefix?: string;\n\n /** Whether to auto-delete after retrieval (default: true) */\n autoDeleteOnRetrieve?: boolean;\n\n /** Callback when context expires or is not found */\n onMiss?: (correlationKey: string) => void;\n}\n\n/**\n * Configuration for traceCallback wrapper\n */\nexport interface CallbackConfig {\n /** Span name for the callback handler */\n name: string;\n\n /**\n * Extract correlation key from callback arguments\n *\n * @example\n * ```typescript\n * correlationKeyFrom: (event) => `payment:${event.data.orderId}`\n * ```\n */\n correlationKeyFrom: (args: unknown[]) => string;\n\n /** Additional span attributes */\n attributes?: Record<string, string | number | boolean>;\n\n /** Whether to fail if parked context is not found (default: false) */\n requireParkedContext?: boolean;\n}\n\n/**\n * Extended context for callback handlers\n */\nexport interface CallbackContext extends TraceContext {\n /** The retrieved parked context, if found */\n parkedContext: StoredTraceContext | null;\n\n /** Time elapsed since context was parked (ms), or null if not found */\n elapsedMs: number | null;\n\n /** The correlation key used for retrieval */\n correlationKey: string;\n}\n\n/**\n * The parking lot instance\n */\nexport interface ParkingLot {\n /**\n * Park current trace context before initiating async operation\n *\n * Call this before sending a webhook, initiating a payment, or starting\n * any operation that will complete via callback.\n *\n * @param correlationKey - Unique key to retrieve context later (e.g., \"payment:order-123\")\n * @param metadata - Optional metadata to store with the context\n * @returns The correlation key (with prefix applied)\n *\n * @example\n * ```typescript\n * await parkingLot.park(`payment:${orderId}`, {\n * customerId: customer.id,\n * amount: payment.amount.toString(),\n * });\n * ```\n */\n park(\n correlationKey: string,\n metadata?: Record<string, string>,\n ): Promise<string>;\n\n /**\n * Retrieve parked context when callback arrives\n *\n * @param correlationKey - The key used when parking\n * @returns The stored context, or null if not found/expired\n */\n retrieve(correlationKey: string): Promise<StoredTraceContext | null>;\n\n /**\n * Wrap a callback handler with automatic context retrieval and linking\n *\n * Creates a traced function that:\n * 1. Extracts correlation key from arguments\n * 2. Retrieves parked context from storage\n * 3. Creates a span link to the original trace\n * 4. Provides elapsed time since parking\n *\n * @param config - Callback configuration\n * @returns Factory function for the callback handler\n *\n * @example\n * ```typescript\n * export const handleWebhook = parkingLot.traceCallback({\n * name: 'webhook.payment.completed',\n * correlationKeyFrom: (args) => `payment:${args[0].orderId}`,\n * })(ctx => async (event) => {\n * console.log(`Payment completed after ${ctx.elapsedMs}ms`);\n * await processPayment(event);\n * });\n * ```\n */\n traceCallback<TArgs extends unknown[], TReturn>(\n config: CallbackConfig,\n ): (\n fnFactory: (ctx: CallbackContext) => (...args: TArgs) => Promise<TReturn>,\n ) => (...args: TArgs) => Promise<TReturn>;\n\n /**\n * Manually create a span link from stored context\n *\n * Useful when you need more control over span creation.\n *\n * @param storedContext - The stored trace context\n * @returns A span link that can be added to a span\n */\n createLink(storedContext: StoredTraceContext): Link;\n\n /**\n * Check if a parked context exists (without retrieving/deleting it)\n *\n * @param correlationKey - The key to check\n * @returns True if context exists and hasn't expired\n */\n exists(correlationKey: string): Promise<boolean>;\n}\n\n// ============================================================================\n// In-Memory Store (for testing and development)\n// ============================================================================\n\n/**\n * In-memory trace context store\n *\n * Useful for testing and development. For production, use a persistent\n * store like Redis or DynamoDB.\n *\n * @example\n * ```typescript\n * const store = new InMemoryTraceContextStore();\n * const parkingLot = createParkingLot({ store });\n * ```\n */\nexport class InMemoryTraceContextStore implements TraceContextStore {\n private store = new Map<string, StoredTraceContext>();\n private cleanupInterval: ReturnType<typeof setInterval> | null = null;\n\n constructor(\n private options: {\n /** Cleanup interval in ms (default: 60000) */\n cleanupIntervalMs?: number;\n } = {},\n ) {\n // Start periodic cleanup of expired entries\n const cleanupMs = options.cleanupIntervalMs ?? 60_000;\n if (cleanupMs > 0) {\n this.cleanupInterval = setInterval(() => this.cleanup(), cleanupMs);\n // Don't prevent process exit\n if (this.cleanupInterval.unref) {\n this.cleanupInterval.unref();\n }\n }\n }\n\n async save(key: string, context: StoredTraceContext): Promise<void> {\n this.store.set(key, context);\n }\n\n async load(key: string): Promise<StoredTraceContext | null> {\n const context = this.store.get(key);\n if (!context) {\n return null;\n }\n\n // Check TTL expiration\n if (context.ttlMs) {\n const age = Date.now() - context.parkedAt;\n if (age > context.ttlMs) {\n this.store.delete(key);\n return null;\n }\n }\n\n return context;\n }\n\n async delete(key: string): Promise<void> {\n this.store.delete(key);\n }\n\n /**\n * Get number of stored contexts (for testing)\n */\n get size(): number {\n return this.store.size;\n }\n\n /**\n * Clear all stored contexts (for testing)\n */\n clear(): void {\n this.store.clear();\n }\n\n /**\n * Stop the cleanup interval\n */\n destroy(): void {\n if (this.cleanupInterval) {\n clearInterval(this.cleanupInterval);\n this.cleanupInterval = null;\n }\n }\n\n private cleanup(): void {\n const now = Date.now();\n for (const [key, context] of this.store.entries()) {\n if (context.ttlMs) {\n const age = now - context.parkedAt;\n if (age > context.ttlMs) {\n this.store.delete(key);\n }\n }\n }\n }\n}\n\n// ============================================================================\n// Parking Lot Factory\n// ============================================================================\n\n/**\n * Create a parking lot for trace context storage and retrieval\n *\n * @param config - Parking lot configuration\n * @returns A parking lot instance\n *\n * @example Basic usage\n * ```typescript\n * const parkingLot = createParkingLot({\n * store: new InMemoryTraceContextStore(),\n * defaultTTLMs: 24 * 60 * 60 * 1000, // 24 hours\n * });\n * ```\n *\n * @example With Redis store\n * ```typescript\n * class RedisTraceContextStore implements TraceContextStore {\n * constructor(private redis: Redis) {}\n *\n * async save(key: string, context: StoredTraceContext) {\n * const ttlSeconds = context.ttlMs ? Math.ceil(context.ttlMs / 1000) : 86400;\n * await this.redis.setex(key, ttlSeconds, JSON.stringify(context));\n * }\n *\n * async load(key: string) {\n * const data = await this.redis.get(key);\n * return data ? JSON.parse(data) : null;\n * }\n *\n * async delete(key: string) {\n * await this.redis.del(key);\n * }\n * }\n *\n * const parkingLot = createParkingLot({\n * store: new RedisTraceContextStore(redis),\n * });\n * ```\n */\nexport function createParkingLot(config: ParkingLotConfig): ParkingLot {\n const {\n store,\n defaultTTLMs = 24 * 60 * 60 * 1000, // 24 hours\n keyPrefix = 'parkingLot:',\n autoDeleteOnRetrieve = true,\n onMiss,\n } = config;\n\n /**\n * Get current span context from active context\n */\n function getCurrentSpanContext(): SpanContext | null {\n const activeSpan = otelTrace.getActiveSpan();\n if (!activeSpan) {\n return null;\n }\n return activeSpan.spanContext();\n }\n\n /**\n * Apply key prefix\n */\n function prefixKey(key: string): string {\n return `${keyPrefix}${key}`;\n }\n\n const parkingLot: ParkingLot = {\n async park(\n correlationKey: string,\n metadata?: Record<string, string>,\n ): Promise<string> {\n const spanContext = getCurrentSpanContext();\n const fullKey = prefixKey(correlationKey);\n\n const storedContext: StoredTraceContext = {\n traceId: spanContext?.traceId ?? '',\n spanId: spanContext?.spanId ?? '',\n traceFlags: spanContext?.traceFlags ?? 0,\n parkedAt: Date.now(),\n ttlMs: defaultTTLMs,\n metadata,\n };\n\n await store.save(fullKey, storedContext);\n\n const activeSpan = otelTrace.getActiveSpan();\n if (activeSpan) {\n const parkAttrs: Record<string, AttributeValue> = {\n 'parking_lot.correlation_key': correlationKey,\n 'parking_lot.ttl_ms': defaultTTLMs,\n ...(metadata &&\n Object.fromEntries(\n Object.entries(metadata).map(([k, v]) => [\n `parking_lot.metadata.${k}`,\n v,\n ]),\n )),\n };\n emitCorrelatedEvent(\n {\n setAttribute: (k, v) => activeSpan.setAttribute(k, v),\n setAttributes: (a) => activeSpan.setAttributes(a),\n addEvent: (n, a) => activeSpan.addEvent(n, a),\n },\n 'trace_context_parked',\n parkAttrs,\n );\n }\n\n // Return the unprefixed key so callers can use the same key for retrieve()\n return correlationKey;\n },\n\n async retrieve(correlationKey: string): Promise<StoredTraceContext | null> {\n const fullKey = prefixKey(correlationKey);\n const storedContext = await store.load(fullKey);\n\n if (!storedContext) {\n onMiss?.(correlationKey);\n return null;\n }\n\n if (autoDeleteOnRetrieve) {\n await store.delete(fullKey);\n }\n\n return storedContext;\n },\n\n traceCallback<TArgs extends unknown[], TReturn>(\n callbackConfig: CallbackConfig,\n ): (\n fnFactory: (ctx: CallbackContext) => (...args: TArgs) => Promise<TReturn>,\n ) => (...args: TArgs) => Promise<TReturn> {\n return (\n fnFactory: (\n ctx: CallbackContext,\n ) => (...args: TArgs) => Promise<TReturn>,\n ): ((...args: TArgs) => Promise<TReturn>) => {\n return trace<TArgs, TReturn>(\n {\n name: callbackConfig.name,\n spanKind: SpanKind.SERVER,\n },\n (baseCtx) => {\n return async (...args: TArgs) => {\n // Extract correlation key from arguments\n const correlationKey = callbackConfig.correlationKeyFrom(args);\n\n // Retrieve parked context\n const parkedContext = await parkingLot.retrieve(correlationKey);\n\n // Calculate elapsed time\n const elapsedMs = parkedContext\n ? Date.now() - parkedContext.parkedAt\n : null;\n\n // Set span attributes\n baseCtx.setAttribute(\n 'parking_lot.correlation_key',\n correlationKey,\n );\n\n if (parkedContext) {\n baseCtx.setAttribute('parking_lot.elapsed_ms', elapsedMs!);\n baseCtx.setAttribute(\n 'parking_lot.original_trace_id',\n parkedContext.traceId,\n );\n baseCtx.setAttribute(\n 'parking_lot.original_span_id',\n parkedContext.spanId,\n );\n\n // Add metadata as attributes\n if (parkedContext.metadata) {\n for (const [key, value] of Object.entries(\n parkedContext.metadata,\n )) {\n baseCtx.setAttribute(`parking_lot.metadata.${key}`, value);\n }\n }\n\n // Create span link to original trace\n const link = parkingLot.createLink(parkedContext);\n baseCtx.addLinks([link]);\n\n emitCorrelatedEvent(baseCtx, 'parked_context_retrieved', {\n 'parking_lot.correlation_key': correlationKey,\n 'parking_lot.elapsed_ms': elapsedMs!,\n 'parking_lot.original_trace_id': parkedContext.traceId,\n });\n } else {\n baseCtx.setAttribute('parking_lot.context_found', false);\n\n if (callbackConfig.requireParkedContext) {\n const error = new Error(\n `Required parked context not found for key: ${correlationKey}`,\n );\n recordStructuredError(baseCtx, error);\n throw error;\n }\n }\n\n // Apply custom attributes\n if (callbackConfig.attributes) {\n for (const [key, value] of Object.entries(\n callbackConfig.attributes,\n )) {\n baseCtx.setAttribute(key, value);\n }\n }\n\n // Create extended context\n const callbackCtx: CallbackContext = {\n ...baseCtx,\n parkedContext,\n elapsedMs,\n correlationKey,\n };\n\n // Execute user's function\n const userFn = fnFactory(callbackCtx);\n return userFn(...args);\n };\n },\n );\n };\n },\n\n createLink(storedContext: StoredTraceContext): Link {\n return {\n context: {\n traceId: storedContext.traceId,\n spanId: storedContext.spanId,\n traceFlags: storedContext.traceFlags,\n isRemote: true,\n },\n attributes: {\n 'link.type': 'parking_lot',\n 'parking_lot.parked_at': storedContext.parkedAt,\n ...(storedContext.metadata && {\n 'parking_lot.has_metadata': true,\n }),\n },\n };\n },\n\n async exists(correlationKey: string): Promise<boolean> {\n const fullKey = prefixKey(correlationKey);\n const context = await store.load(fullKey);\n return context !== null;\n },\n };\n\n return parkingLot;\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/**\n * Create a correlation key from multiple parts\n *\n * @param parts - Key parts to join\n * @returns A correlation key string\n *\n * @example\n * ```typescript\n * const key = createCorrelationKey('payment', orderId, 'stripe');\n * // Returns: \"payment:order-123:stripe\"\n * ```\n */\nexport function createCorrelationKey(...parts: (string | number)[]): string {\n return parts.map(String).join(':');\n}\n\n/**\n * Extract span context from stored context for manual linking\n *\n * @param storedContext - The stored trace context\n * @returns SpanContext compatible object\n */\nexport function toSpanContext(storedContext: StoredTraceContext): SpanContext {\n return {\n traceId: storedContext.traceId,\n spanId: storedContext.spanId,\n traceFlags: storedContext.traceFlags,\n isRemote: true,\n };\n}\n"]}
|
package/dist/webhook.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { emitCorrelatedEvent } from './chunk-KIL5CUN6.js';
|
|
2
|
-
import { trace } from './chunk-
|
|
2
|
+
import { trace } from './chunk-T7JO2TCP.js';
|
|
3
3
|
import './chunk-HT5JQKN2.js';
|
|
4
4
|
import './chunk-SEO6NAQT.js';
|
|
5
|
-
import
|
|
5
|
+
import './chunk-66YJ66GG.js';
|
|
6
|
+
import { recordStructuredError } from './chunk-LVIPBYFE.js';
|
|
6
7
|
import './chunk-ALPYR2GC.js';
|
|
7
8
|
import './chunk-CMHVQR6P.js';
|
|
8
9
|
import './chunk-A4E5AQFK.js';
|
package/dist/webhook.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/webhook.ts"],"names":["otelTrace"],"mappings":";;;;;;;;;;;;;;;;;;;;AAiQO,IAAM,4BAAN,MAA6D;AAAA,EAIlE,WAAA,CACU,OAAA,GAGJ,EAAC,EACL;AAJQ,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AAMR,IAAA,MAAM,SAAA,GAAY,QAAQ,iBAAA,IAAqB,GAAA;AAC/C,IAAA,IAAI,YAAY,CAAA,EAAG;AACjB,MAAA,IAAA,CAAK,kBAAkB,WAAA,CAAY,MAAM,IAAA,CAAK,OAAA,IAAW,SAAS,CAAA;AAElE,MAAA,IAAI,IAAA,CAAK,gBAAgB,KAAA,EAAO;AAC9B,QAAA,IAAA,CAAK,gBAAgB,KAAA,EAAM;AAAA,MAC7B;AAAA,IACF;AAAA,EACF;AAAA,EAdU,OAAA;AAAA,EAJF,KAAA,uBAAY,GAAA,EAAgC;AAAA,EAC5C,eAAA,GAAyD,IAAA;AAAA,EAmBjE,MAAM,IAAA,CAAK,GAAA,EAAa,OAAA,EAA4C;AAClE,IAAA,IAAA,CAAK,KAAA,CAAM,GAAA,CAAI,GAAA,EAAK,OAAO,CAAA;AAAA,EAC7B;AAAA,EAEA,MAAM,KAAK,GAAA,EAAiD;AAC1D,IAAA,MAAM,OAAA,GAAU,IAAA,CAAK,KAAA,CAAM,GAAA,CAAI,GAAG,CAAA;AAClC,IAAA,IAAI,CAAC,OAAA,EAAS;AACZ,MAAA,OAAO,IAAA;AAAA,IACT;AAGA,IAAA,IAAI,QAAQ,KAAA,EAAO;AACjB,MAAA,MAAM,GAAA,GAAM,IAAA,CAAK,GA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,MAAA,OAAO,OAAA,KAAY,IAAA;AAAA,IACrB;AAAA,GACF;AAEA,EAAA,OAAO,UAAA;AACT;AAkBO,SAAS,wBAAwB,KAAA,EAAoC;AAC1E,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,MAAM,CAAA,CAAE,KAAK,GAAG,CAAA;AACnC;AAQO,SAAS,cAAc,aAAA,EAAgD;AAC5E,EAAA,OAAO;AAAA,IACL,SAAS,aAAA,CAAc,OAAA;AAAA,IACvB,QAAQ,aAAA,CAAc,MAAA;AAAA,IACtB,YAAY,aAAA,CAAc,UAAA;AAAA,IAC1B,QAAA,EAAU;AAAA,GACZ;AACF","file":"webhook.js","sourcesContent":["/**\n * Webhook and callback tracing with the \"Parking Lot\" pattern\n *\n * When initiating async operations that return hours/days later (webhooks,\n * payment callbacks, human approvals), you can't keep a span open. This module\n * provides utilities to \"park\" trace context and retrieve it when callbacks arrive.\n *\n * @example Stripe payment webhook\n * ```typescript\n * import { createParkingLot, InMemoryTraceContextStore } from 'autotel/webhook';\n *\n * const parkingLot = createParkingLot({\n * store: new InMemoryTraceContextStore(),\n * defaultTTLMs: 24 * 60 * 60 * 1000, // 24 hours\n * });\n *\n * // When initiating payment\n * export const initiatePayment = trace(ctx => async (orderId: string) => {\n * await parkingLot.park(`payment:${orderId}`, { orderId });\n * await stripeClient.createPaymentIntent({ metadata: { orderId } });\n * });\n *\n * // When Stripe webhook arrives (hours later)\n * export const handleStripeWebhook = parkingLot.traceCallback({\n * name: 'stripe.webhook.payment_intent.succeeded',\n * correlationKeyFrom: (event) => `payment:${event.data.object.metadata.orderId}`,\n * })(ctx => async (event: Stripe.Event) => {\n * // ctx.parkedContext contains the original trace context\n * // ctx.elapsedMs shows time since payment was initiated\n * await fulfillOrder(event.data.object);\n * });\n * ```\n *\n * @module\n */\n\nimport { SpanKind, trace as otelTrace } from '@opentelemetry/api';\nimport type { SpanContext, Link } from '@opentelemetry/api';\nimport { emitCorrelatedEvent } from './correlated-events';\nimport { trace } from './functional';\nimport type { AttributeValue, TraceContext } from './trace-context';\nimport { recordStructuredError } from './structured-error';\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Stored trace context for parking lot pattern\n */\nexport interface StoredTraceContext {\n /** Trace ID from the original span */\n traceId: string;\n\n /** Span ID from the original span */\n spanId: string;\n\n /** Trace flags (sampling decision) */\n traceFlags: number;\n\n /** When the context was parked */\n parkedAt: number;\n\n /** Optional TTL in milliseconds */\n ttlMs?: number;\n\n /** User-provided metadata */\n metadata?: Record<string, string>;\n}\n\n/**\n * Interface for trace context storage backends\n *\n * Implement this interface to use different storage backends (Redis, DynamoDB, etc.)\n */\nexport interface TraceContextStore {\n /**\n * Save trace context with a correlation key\n *\n * @param key - Unique correlation key (e.g., \"payment:order-123\")\n * @param context - The trace context to store\n */\n save(key: string, context: StoredTraceContext): Promise<void>;\n\n /**\n * Load trace context by correlation key\n *\n * @param key - The correlation key used when parking\n * @returns The stored context, or null if not found/expired\n */\n load(key: string): Promise<StoredTraceContext | null>;\n\n /**\n * Delete trace context by correlation key\n *\n * @param key - The correlation key to delete\n */\n delete(key: string): Promise<void>;\n}\n\n/**\n * Configuration for creating a parking lot\n */\nexport interface ParkingLotConfig {\n /** Storage backend for parked contexts */\n store: TraceContextStore;\n\n /** Default TTL in milliseconds (default: 24 hours) */\n defaultTTLMs?: number;\n\n /** Prefix for all correlation keys (default: \"parkingLot:\") */\n keyPrefix?: string;\n\n /** Whether to auto-delete after retrieval (default: true) */\n autoDeleteOnRetrieve?: boolean;\n\n /** Callback when context expires or is not found */\n onMiss?: (correlationKey: string) => void;\n}\n\n/**\n * Configuration for traceCallback wrapper\n */\nexport interface CallbackConfig {\n /** Span name for the callback handler */\n name: string;\n\n /**\n * Extract correlation key from callback arguments\n *\n * @example\n * ```typescript\n * correlationKeyFrom: (event) => `payment:${event.data.orderId}`\n * ```\n */\n correlationKeyFrom: (args: unknown[]) => string;\n\n /** Additional span attributes */\n attributes?: Record<string, string | number | boolean>;\n\n /** Whether to fail if parked context is not found (default: false) */\n requireParkedContext?: boolean;\n}\n\n/**\n * Extended context for callback handlers\n */\nexport interface CallbackContext extends TraceContext {\n /** The retrieved parked context, if found */\n parkedContext: StoredTraceContext | null;\n\n /** Time elapsed since context was parked (ms), or null if not found */\n elapsedMs: number | null;\n\n /** The correlation key used for retrieval */\n correlationKey: string;\n}\n\n/**\n * The parking lot instance\n */\nexport interface ParkingLot {\n /**\n * Park current trace context before initiating async operation\n *\n * Call this before sending a webhook, initiating a payment, or starting\n * any operation that will complete via callback.\n *\n * @param correlationKey - Unique key to retrieve context later (e.g., \"payment:order-123\")\n * @param metadata - Optional metadata to store with the context\n * @returns The correlation key (with prefix applied)\n *\n * @example\n * ```typescript\n * await parkingLot.park(`payment:${orderId}`, {\n * customerId: customer.id,\n * amount: payment.amount.toString(),\n * });\n * ```\n */\n park(\n correlationKey: string,\n metadata?: Record<string, string>,\n ): Promise<string>;\n\n /**\n * Retrieve parked context when callback arrives\n *\n * @param correlationKey - The key used when parking\n * @returns The stored context, or null if not found/expired\n */\n retrieve(correlationKey: string): Promise<StoredTraceContext | null>;\n\n /**\n * Wrap a callback handler with automatic context retrieval and linking\n *\n * Creates a traced function that:\n * 1. Extracts correlation key from arguments\n * 2. Retrieves parked context from storage\n * 3. Creates a span link to the original trace\n * 4. Provides elapsed time since parking\n *\n * @param config - Callback configuration\n * @returns Factory function for the callback handler\n *\n * @example\n * ```typescript\n * export const handleWebhook = parkingLot.traceCallback({\n * name: 'webhook.payment.completed',\n * correlationKeyFrom: (args) => `payment:${args[0].orderId}`,\n * })(ctx => async (event) => {\n * console.log(`Payment completed after ${ctx.elapsedMs}ms`);\n * await processPayment(event);\n * });\n * ```\n */\n traceCallback<TArgs extends unknown[], TReturn>(\n config: CallbackConfig,\n ): (\n fnFactory: (ctx: CallbackContext) => (...args: TArgs) => Promise<TReturn>,\n ) => (...args: TArgs) => Promise<TReturn>;\n\n /**\n * Manually create a span link from stored context\n *\n * Useful when you need more control over span creation.\n *\n * @param storedContext - The stored trace context\n * @returns A span link that can be added to a span\n */\n createLink(storedContext: StoredTraceContext): Link;\n\n /**\n * Check if a parked context exists (without retrieving/deleting it)\n *\n * @param correlationKey - The key to check\n * @returns True if context exists and hasn't expired\n */\n exists(correlationKey: string): Promise<boolean>;\n}\n\n// ============================================================================\n// In-Memory Store (for testing and development)\n// ============================================================================\n\n/**\n * In-memory trace context store\n *\n * Useful for testing and development. For production, use a persistent\n * store like Redis or DynamoDB.\n *\n * @example\n * ```typescript\n * const store = new InMemoryTraceContextStore();\n * const parkingLot = createParkingLot({ store });\n * ```\n */\nexport class InMemoryTraceContextStore implements TraceContextStore {\n private store = new Map<string, StoredTraceContext>();\n private cleanupInterval: ReturnType<typeof setInterval> | null = null;\n\n constructor(\n private options: {\n /** Cleanup interval in ms (default: 60000) */\n cleanupIntervalMs?: number;\n } = {},\n ) {\n // Start periodic cleanup of expired entries\n const cleanupMs = options.cleanupIntervalMs ?? 60_000;\n if (cleanupMs > 0) {\n this.cleanupInterval = setInterval(() => this.cleanup(), cleanupMs);\n // Don't prevent process exit\n if (this.cleanupInterval.unref) {\n this.cleanupInterval.unref();\n }\n }\n }\n\n async save(key: string, context: StoredTraceContext): Promise<void> {\n this.store.set(key, context);\n }\n\n async load(key: string): Promise<StoredTraceContext | null> {\n const context = this.store.get(key);\n if (!context) {\n return null;\n }\n\n // Check TTL expiration\n if (context.ttlMs) {\n const age = Date.now() - context.parkedAt;\n if (age > context.ttlMs) {\n this.store.delete(key);\n return null;\n }\n }\n\n return context;\n }\n\n async delete(key: string): Promise<void> {\n this.store.delete(key);\n }\n\n /**\n * Get number of stored contexts (for testing)\n */\n get size(): number {\n return this.store.size;\n }\n\n /**\n * Clear all stored contexts (for testing)\n */\n clear(): void {\n this.store.clear();\n }\n\n /**\n * Stop the cleanup interval\n */\n destroy(): void {\n if (this.cleanupInterval) {\n clearInterval(this.cleanupInterval);\n this.cleanupInterval = null;\n }\n }\n\n private cleanup(): void {\n const now = Date.now();\n for (const [key, context] of this.store.entries()) {\n if (context.ttlMs) {\n const age = now - context.parkedAt;\n if (age > context.ttlMs) {\n this.store.delete(key);\n }\n }\n }\n }\n}\n\n// ============================================================================\n// Parking Lot Factory\n// ============================================================================\n\n/**\n * Create a parking lot for trace context storage and retrieval\n *\n * @param config - Parking lot configuration\n * @returns A parking lot instance\n *\n * @example Basic usage\n * ```typescript\n * const parkingLot = createParkingLot({\n * store: new InMemoryTraceContextStore(),\n * defaultTTLMs: 24 * 60 * 60 * 1000, // 24 hours\n * });\n * ```\n *\n * @example With Redis store\n * ```typescript\n * class RedisTraceContextStore implements TraceContextStore {\n * constructor(private redis: Redis) {}\n *\n * async save(key: string, context: StoredTraceContext) {\n * const ttlSeconds = context.ttlMs ? Math.ceil(context.ttlMs / 1000) : 86400;\n * await this.redis.setex(key, ttlSeconds, JSON.stringify(context));\n * }\n *\n * async load(key: string) {\n * const data = await this.redis.get(key);\n * return data ? JSON.parse(data) : null;\n * }\n *\n * async delete(key: string) {\n * await this.redis.del(key);\n * }\n * }\n *\n * const parkingLot = createParkingLot({\n * store: new RedisTraceContextStore(redis),\n * });\n * ```\n */\nexport function createParkingLot(config: ParkingLotConfig): ParkingLot {\n const {\n store,\n defaultTTLMs = 24 * 60 * 60 * 1000, // 24 hours\n keyPrefix = 'parkingLot:',\n autoDeleteOnRetrieve = true,\n onMiss,\n } = config;\n\n /**\n * Get current span context from active context\n */\n function getCurrentSpanContext(): SpanContext | null {\n const activeSpan = otelTrace.getActiveSpan();\n if (!activeSpan) {\n return null;\n }\n return activeSpan.spanContext();\n }\n\n /**\n * Apply key prefix\n */\n function prefixKey(key: string): string {\n return `${keyPrefix}${key}`;\n }\n\n const parkingLot: ParkingLot = {\n async park(\n correlationKey: string,\n metadata?: Record<string, string>,\n ): Promise<string> {\n const spanContext = getCurrentSpanContext();\n const fullKey = prefixKey(correlationKey);\n\n const storedContext: StoredTraceContext = {\n traceId: spanContext?.traceId ?? '',\n spanId: spanContext?.spanId ?? '',\n traceFlags: spanContext?.traceFlags ?? 0,\n parkedAt: Date.now(),\n ttlMs: defaultTTLMs,\n metadata,\n };\n\n await store.save(fullKey, storedContext);\n\n const activeSpan = otelTrace.getActiveSpan();\n if (activeSpan) {\n const parkAttrs: Record<string, AttributeValue> = {\n 'parking_lot.correlation_key': correlationKey,\n 'parking_lot.ttl_ms': defaultTTLMs,\n ...(metadata &&\n Object.fromEntries(\n Object.entries(metadata).map(([k, v]) => [\n `parking_lot.metadata.${k}`,\n v,\n ]),\n )),\n };\n emitCorrelatedEvent(\n {\n setAttribute: (k, v) => activeSpan.setAttribute(k, v),\n setAttributes: (a) => activeSpan.setAttributes(a),\n addEvent: (n, a) => activeSpan.addEvent(n, a),\n },\n 'trace_context_parked',\n parkAttrs,\n );\n }\n\n // Return the unprefixed key so callers can use the same key for retrieve()\n return correlationKey;\n },\n\n async retrieve(correlationKey: string): Promise<StoredTraceContext | null> {\n const fullKey = prefixKey(correlationKey);\n const storedContext = await store.load(fullKey);\n\n if (!storedContext) {\n onMiss?.(correlationKey);\n return null;\n }\n\n if (autoDeleteOnRetrieve) {\n await store.delete(fullKey);\n }\n\n return storedContext;\n },\n\n traceCallback<TArgs extends unknown[], TReturn>(\n callbackConfig: CallbackConfig,\n ): (\n fnFactory: (ctx: CallbackContext) => (...args: TArgs) => Promise<TReturn>,\n ) => (...args: TArgs) => Promise<TReturn> {\n return (\n fnFactory: (\n ctx: CallbackContext,\n ) => (...args: TArgs) => Promise<TReturn>,\n ): ((...args: TArgs) => Promise<TReturn>) => {\n return trace<TArgs, TReturn>(\n {\n name: callbackConfig.name,\n spanKind: SpanKind.SERVER,\n },\n (baseCtx) => {\n return async (...args: TArgs) => {\n // Extract correlation key from arguments\n const correlationKey = callbackConfig.correlationKeyFrom(args);\n\n // Retrieve parked context\n const parkedContext = await parkingLot.retrieve(correlationKey);\n\n // Calculate elapsed time\n const elapsedMs = parkedContext\n ? Date.now() - parkedContext.parkedAt\n : null;\n\n // Set span attributes\n baseCtx.setAttribute(\n 'parking_lot.correlation_key',\n correlationKey,\n );\n\n if (parkedContext) {\n baseCtx.setAttribute('parking_lot.elapsed_ms', elapsedMs!);\n baseCtx.setAttribute(\n 'parking_lot.original_trace_id',\n parkedContext.traceId,\n );\n baseCtx.setAttribute(\n 'parking_lot.original_span_id',\n parkedContext.spanId,\n );\n\n // Add metadata as attributes\n if (parkedContext.metadata) {\n for (const [key, value] of Object.entries(\n parkedContext.metadata,\n )) {\n baseCtx.setAttribute(`parking_lot.metadata.${key}`, value);\n }\n }\n\n // Create span link to original trace\n const link = parkingLot.createLink(parkedContext);\n baseCtx.addLinks([link]);\n\n emitCorrelatedEvent(baseCtx, 'parked_context_retrieved', {\n 'parking_lot.correlation_key': correlationKey,\n 'parking_lot.elapsed_ms': elapsedMs!,\n 'parking_lot.original_trace_id': parkedContext.traceId,\n });\n } else {\n baseCtx.setAttribute('parking_lot.context_found', false);\n\n if (callbackConfig.requireParkedContext) {\n const error = new Error(\n `Required parked context not found for key: ${correlationKey}`,\n );\n recordStructuredError(baseCtx, error);\n throw error;\n }\n }\n\n // Apply custom attributes\n if (callbackConfig.attributes) {\n for (const [key, value] of Object.entries(\n callbackConfig.attributes,\n )) {\n baseCtx.setAttribute(key, value);\n }\n }\n\n // Create extended context\n const callbackCtx: CallbackContext = {\n ...baseCtx,\n parkedContext,\n elapsedMs,\n correlationKey,\n };\n\n // Execute user's function\n const userFn = fnFactory(callbackCtx);\n return userFn(...args);\n };\n },\n );\n };\n },\n\n createLink(storedContext: StoredTraceContext): Link {\n return {\n context: {\n traceId: storedContext.traceId,\n spanId: storedContext.spanId,\n traceFlags: storedContext.traceFlags,\n isRemote: true,\n },\n attributes: {\n 'link.type': 'parking_lot',\n 'parking_lot.parked_at': storedContext.parkedAt,\n ...(storedContext.metadata && {\n 'parking_lot.has_metadata': true,\n }),\n },\n };\n },\n\n async exists(correlationKey: string): Promise<boolean> {\n const fullKey = prefixKey(correlationKey);\n const context = await store.load(fullKey);\n return context !== null;\n },\n };\n\n return parkingLot;\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/**\n * Create a correlation key from multiple parts\n *\n * @param parts - Key parts to join\n * @returns A correlation key string\n *\n * @example\n * ```typescript\n * const key = createCorrelationKey('payment', orderId, 'stripe');\n * // Returns: \"payment:order-123:stripe\"\n * ```\n */\nexport function createCorrelationKey(...parts: (string | number)[]): string {\n return parts.map(String).join(':');\n}\n\n/**\n * Extract span context from stored context for manual linking\n *\n * @param storedContext - The stored trace context\n * @returns SpanContext compatible object\n */\nexport function toSpanContext(storedContext: StoredTraceContext): SpanContext {\n return {\n traceId: storedContext.traceId,\n spanId: storedContext.spanId,\n traceFlags: storedContext.traceFlags,\n isRemote: true,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/webhook.ts"],"names":["otelTrace"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAiQO,IAAM,4BAAN,MAA6D;AAAA,EAIlE,WAAA,CACU,OAAA,GAGJ,EAAC,EACL;AAJQ,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AAMR,IAAA,MAAM,SAAA,GAAY,QAAQ,iBAAA,IAAqB,GAAA;AAC/C,IAAA,IAAI,YAAY,CAAA,EAAG;AACjB,MAAA,IAAA,CAAK,kBAAkB,WAAA,CAAY,MAAM,IAAA,CAAK,OAAA,IAAW,SAAS,CAAA;AAElE,MAAA,IAAI,IAAA,CAAK,gBAAgB,KAAA,EAAO;AAC9B,QAAA,IAAA,CAAK,gBAAgB,KAAA,EAAM;AAAA,MAC7B;AAAA,IACF;AAAA,EACF;AAAA,EAdU,OAAA;AAAA,EAJF,KAAA,uBAAY,GAAA,EAAgC;AAAA,EAC5C,eAAA,GAAyD,IAAA;AAAA,EAmBjE,MAAM,IAAA,CAAK,GAAA,EAAa,OAAA,EAA4C;AAClE,IAAA,IAAA,CAAK,KAAA,CAAM,GAAA,CAAI,GAAA,EAAK,OAAO,CAAA;AAAA,EAC7B;AAAA,EAEA,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vC,GAAI,cAAc,QAAA,IAAY;AAAA,YAC5B,0BAAA,EAA4B;AAAA;AAC9B;AACF,OACF;AAAA,IACF,CAAA;AAAA,IAEA,MAAM,OAAO,cAAA,EAA0C;AACrD,MAAA,MAAM,OAAA,GAAU,UAAU,cAAc,CAAA;AACxC,MAAA,MAAM,OAAA,GAAU,MAAM,KAAA,CAAM,IAAA,CAAK,OAAO,CAAA;AACxC,MAAA,OAAO,OAAA,KAAY,IAAA;AAAA,IACrB;AAAA,GACF;AAEA,EAAA,OAAO,UAAA;AACT;AAkBO,SAAS,wBAAwB,KAAA,EAAoC;AAC1E,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,MAAM,CAAA,CAAE,KAAK,GAAG,CAAA;AACnC;AAQO,SAAS,cAAc,aAAA,EAAgD;AAC5E,EAAA,OAAO;AAAA,IACL,SAAS,aAAA,CAAc,OAAA;AAAA,IACvB,QAAQ,aAAA,CAAc,MAAA;AAAA,IACtB,YAAY,aAAA,CAAc,UAAA;AAAA,IAC1B,QAAA,EAAU;AAAA,GACZ;AACF","file":"webhook.js","sourcesContent":["/**\n * Webhook and callback tracing with the \"Parking Lot\" pattern\n *\n * When initiating async operations that return hours/days later (webhooks,\n * payment callbacks, human approvals), you can't keep a span open. This module\n * provides utilities to \"park\" trace context and retrieve it when callbacks arrive.\n *\n * @example Stripe payment webhook\n * ```typescript\n * import { createParkingLot, InMemoryTraceContextStore } from 'autotel/webhook';\n *\n * const parkingLot = createParkingLot({\n * store: new InMemoryTraceContextStore(),\n * defaultTTLMs: 24 * 60 * 60 * 1000, // 24 hours\n * });\n *\n * // When initiating payment\n * export const initiatePayment = trace(ctx => async (orderId: string) => {\n * await parkingLot.park(`payment:${orderId}`, { orderId });\n * await stripeClient.createPaymentIntent({ metadata: { orderId } });\n * });\n *\n * // When Stripe webhook arrives (hours later)\n * export const handleStripeWebhook = parkingLot.traceCallback({\n * name: 'stripe.webhook.payment_intent.succeeded',\n * correlationKeyFrom: (event) => `payment:${event.data.object.metadata.orderId}`,\n * })(ctx => async (event: Stripe.Event) => {\n * // ctx.parkedContext contains the original trace context\n * // ctx.elapsedMs shows time since payment was initiated\n * await fulfillOrder(event.data.object);\n * });\n * ```\n *\n * @module\n */\n\nimport { SpanKind, trace as otelTrace } from '@opentelemetry/api';\nimport type { SpanContext, Link } from '@opentelemetry/api';\nimport { emitCorrelatedEvent } from './correlated-events';\nimport { trace } from './functional';\nimport type { AttributeValue, TraceContext } from './trace-context';\nimport { recordStructuredError } from './structured-error';\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Stored trace context for parking lot pattern\n */\nexport interface StoredTraceContext {\n /** Trace ID from the original span */\n traceId: string;\n\n /** Span ID from the original span */\n spanId: string;\n\n /** Trace flags (sampling decision) */\n traceFlags: number;\n\n /** When the context was parked */\n parkedAt: number;\n\n /** Optional TTL in milliseconds */\n ttlMs?: number;\n\n /** User-provided metadata */\n metadata?: Record<string, string>;\n}\n\n/**\n * Interface for trace context storage backends\n *\n * Implement this interface to use different storage backends (Redis, DynamoDB, etc.)\n */\nexport interface TraceContextStore {\n /**\n * Save trace context with a correlation key\n *\n * @param key - Unique correlation key (e.g., \"payment:order-123\")\n * @param context - The trace context to store\n */\n save(key: string, context: StoredTraceContext): Promise<void>;\n\n /**\n * Load trace context by correlation key\n *\n * @param key - The correlation key used when parking\n * @returns The stored context, or null if not found/expired\n */\n load(key: string): Promise<StoredTraceContext | null>;\n\n /**\n * Delete trace context by correlation key\n *\n * @param key - The correlation key to delete\n */\n delete(key: string): Promise<void>;\n}\n\n/**\n * Configuration for creating a parking lot\n */\nexport interface ParkingLotConfig {\n /** Storage backend for parked contexts */\n store: TraceContextStore;\n\n /** Default TTL in milliseconds (default: 24 hours) */\n defaultTTLMs?: number;\n\n /** Prefix for all correlation keys (default: \"parkingLot:\") */\n keyPrefix?: string;\n\n /** Whether to auto-delete after retrieval (default: true) */\n autoDeleteOnRetrieve?: boolean;\n\n /** Callback when context expires or is not found */\n onMiss?: (correlationKey: string) => void;\n}\n\n/**\n * Configuration for traceCallback wrapper\n */\nexport interface CallbackConfig {\n /** Span name for the callback handler */\n name: string;\n\n /**\n * Extract correlation key from callback arguments\n *\n * @example\n * ```typescript\n * correlationKeyFrom: (event) => `payment:${event.data.orderId}`\n * ```\n */\n correlationKeyFrom: (args: unknown[]) => string;\n\n /** Additional span attributes */\n attributes?: Record<string, string | number | boolean>;\n\n /** Whether to fail if parked context is not found (default: false) */\n requireParkedContext?: boolean;\n}\n\n/**\n * Extended context for callback handlers\n */\nexport interface CallbackContext extends TraceContext {\n /** The retrieved parked context, if found */\n parkedContext: StoredTraceContext | null;\n\n /** Time elapsed since context was parked (ms), or null if not found */\n elapsedMs: number | null;\n\n /** The correlation key used for retrieval */\n correlationKey: string;\n}\n\n/**\n * The parking lot instance\n */\nexport interface ParkingLot {\n /**\n * Park current trace context before initiating async operation\n *\n * Call this before sending a webhook, initiating a payment, or starting\n * any operation that will complete via callback.\n *\n * @param correlationKey - Unique key to retrieve context later (e.g., \"payment:order-123\")\n * @param metadata - Optional metadata to store with the context\n * @returns The correlation key (with prefix applied)\n *\n * @example\n * ```typescript\n * await parkingLot.park(`payment:${orderId}`, {\n * customerId: customer.id,\n * amount: payment.amount.toString(),\n * });\n * ```\n */\n park(\n correlationKey: string,\n metadata?: Record<string, string>,\n ): Promise<string>;\n\n /**\n * Retrieve parked context when callback arrives\n *\n * @param correlationKey - The key used when parking\n * @returns The stored context, or null if not found/expired\n */\n retrieve(correlationKey: string): Promise<StoredTraceContext | null>;\n\n /**\n * Wrap a callback handler with automatic context retrieval and linking\n *\n * Creates a traced function that:\n * 1. Extracts correlation key from arguments\n * 2. Retrieves parked context from storage\n * 3. Creates a span link to the original trace\n * 4. Provides elapsed time since parking\n *\n * @param config - Callback configuration\n * @returns Factory function for the callback handler\n *\n * @example\n * ```typescript\n * export const handleWebhook = parkingLot.traceCallback({\n * name: 'webhook.payment.completed',\n * correlationKeyFrom: (args) => `payment:${args[0].orderId}`,\n * })(ctx => async (event) => {\n * console.log(`Payment completed after ${ctx.elapsedMs}ms`);\n * await processPayment(event);\n * });\n * ```\n */\n traceCallback<TArgs extends unknown[], TReturn>(\n config: CallbackConfig,\n ): (\n fnFactory: (ctx: CallbackContext) => (...args: TArgs) => Promise<TReturn>,\n ) => (...args: TArgs) => Promise<TReturn>;\n\n /**\n * Manually create a span link from stored context\n *\n * Useful when you need more control over span creation.\n *\n * @param storedContext - The stored trace context\n * @returns A span link that can be added to a span\n */\n createLink(storedContext: StoredTraceContext): Link;\n\n /**\n * Check if a parked context exists (without retrieving/deleting it)\n *\n * @param correlationKey - The key to check\n * @returns True if context exists and hasn't expired\n */\n exists(correlationKey: string): Promise<boolean>;\n}\n\n// ============================================================================\n// In-Memory Store (for testing and development)\n// ============================================================================\n\n/**\n * In-memory trace context store\n *\n * Useful for testing and development. For production, use a persistent\n * store like Redis or DynamoDB.\n *\n * @example\n * ```typescript\n * const store = new InMemoryTraceContextStore();\n * const parkingLot = createParkingLot({ store });\n * ```\n */\nexport class InMemoryTraceContextStore implements TraceContextStore {\n private store = new Map<string, StoredTraceContext>();\n private cleanupInterval: ReturnType<typeof setInterval> | null = null;\n\n constructor(\n private options: {\n /** Cleanup interval in ms (default: 60000) */\n cleanupIntervalMs?: number;\n } = {},\n ) {\n // Start periodic cleanup of expired entries\n const cleanupMs = options.cleanupIntervalMs ?? 60_000;\n if (cleanupMs > 0) {\n this.cleanupInterval = setInterval(() => this.cleanup(), cleanupMs);\n // Don't prevent process exit\n if (this.cleanupInterval.unref) {\n this.cleanupInterval.unref();\n }\n }\n }\n\n async save(key: string, context: StoredTraceContext): Promise<void> {\n this.store.set(key, context);\n }\n\n async load(key: string): Promise<StoredTraceContext | null> {\n const context = this.store.get(key);\n if (!context) {\n return null;\n }\n\n // Check TTL expiration\n if (context.ttlMs) {\n const age = Date.now() - context.parkedAt;\n if (age > context.ttlMs) {\n this.store.delete(key);\n return null;\n }\n }\n\n return context;\n }\n\n async delete(key: string): Promise<void> {\n this.store.delete(key);\n }\n\n /**\n * Get number of stored contexts (for testing)\n */\n get size(): number {\n return this.store.size;\n }\n\n /**\n * Clear all stored contexts (for testing)\n */\n clear(): void {\n this.store.clear();\n }\n\n /**\n * Stop the cleanup interval\n */\n destroy(): void {\n if (this.cleanupInterval) {\n clearInterval(this.cleanupInterval);\n this.cleanupInterval = null;\n }\n }\n\n private cleanup(): void {\n const now = Date.now();\n for (const [key, context] of this.store.entries()) {\n if (context.ttlMs) {\n const age = now - context.parkedAt;\n if (age > context.ttlMs) {\n this.store.delete(key);\n }\n }\n }\n }\n}\n\n// ============================================================================\n// Parking Lot Factory\n// ============================================================================\n\n/**\n * Create a parking lot for trace context storage and retrieval\n *\n * @param config - Parking lot configuration\n * @returns A parking lot instance\n *\n * @example Basic usage\n * ```typescript\n * const parkingLot = createParkingLot({\n * store: new InMemoryTraceContextStore(),\n * defaultTTLMs: 24 * 60 * 60 * 1000, // 24 hours\n * });\n * ```\n *\n * @example With Redis store\n * ```typescript\n * class RedisTraceContextStore implements TraceContextStore {\n * constructor(private redis: Redis) {}\n *\n * async save(key: string, context: StoredTraceContext) {\n * const ttlSeconds = context.ttlMs ? Math.ceil(context.ttlMs / 1000) : 86400;\n * await this.redis.setex(key, ttlSeconds, JSON.stringify(context));\n * }\n *\n * async load(key: string) {\n * const data = await this.redis.get(key);\n * return data ? JSON.parse(data) : null;\n * }\n *\n * async delete(key: string) {\n * await this.redis.del(key);\n * }\n * }\n *\n * const parkingLot = createParkingLot({\n * store: new RedisTraceContextStore(redis),\n * });\n * ```\n */\nexport function createParkingLot(config: ParkingLotConfig): ParkingLot {\n const {\n store,\n defaultTTLMs = 24 * 60 * 60 * 1000, // 24 hours\n keyPrefix = 'parkingLot:',\n autoDeleteOnRetrieve = true,\n onMiss,\n } = config;\n\n /**\n * Get current span context from active context\n */\n function getCurrentSpanContext(): SpanContext | null {\n const activeSpan = otelTrace.getActiveSpan();\n if (!activeSpan) {\n return null;\n }\n return activeSpan.spanContext();\n }\n\n /**\n * Apply key prefix\n */\n function prefixKey(key: string): string {\n return `${keyPrefix}${key}`;\n }\n\n const parkingLot: ParkingLot = {\n async park(\n correlationKey: string,\n metadata?: Record<string, string>,\n ): Promise<string> {\n const spanContext = getCurrentSpanContext();\n const fullKey = prefixKey(correlationKey);\n\n const storedContext: StoredTraceContext = {\n traceId: spanContext?.traceId ?? '',\n spanId: spanContext?.spanId ?? '',\n traceFlags: spanContext?.traceFlags ?? 0,\n parkedAt: Date.now(),\n ttlMs: defaultTTLMs,\n metadata,\n };\n\n await store.save(fullKey, storedContext);\n\n const activeSpan = otelTrace.getActiveSpan();\n if (activeSpan) {\n const parkAttrs: Record<string, AttributeValue> = {\n 'parking_lot.correlation_key': correlationKey,\n 'parking_lot.ttl_ms': defaultTTLMs,\n ...(metadata &&\n Object.fromEntries(\n Object.entries(metadata).map(([k, v]) => [\n `parking_lot.metadata.${k}`,\n v,\n ]),\n )),\n };\n emitCorrelatedEvent(\n {\n setAttribute: (k, v) => activeSpan.setAttribute(k, v),\n setAttributes: (a) => activeSpan.setAttributes(a),\n addEvent: (n, a) => activeSpan.addEvent(n, a),\n },\n 'trace_context_parked',\n parkAttrs,\n );\n }\n\n // Return the unprefixed key so callers can use the same key for retrieve()\n return correlationKey;\n },\n\n async retrieve(correlationKey: string): Promise<StoredTraceContext | null> {\n const fullKey = prefixKey(correlationKey);\n const storedContext = await store.load(fullKey);\n\n if (!storedContext) {\n onMiss?.(correlationKey);\n return null;\n }\n\n if (autoDeleteOnRetrieve) {\n await store.delete(fullKey);\n }\n\n return storedContext;\n },\n\n traceCallback<TArgs extends unknown[], TReturn>(\n callbackConfig: CallbackConfig,\n ): (\n fnFactory: (ctx: CallbackContext) => (...args: TArgs) => Promise<TReturn>,\n ) => (...args: TArgs) => Promise<TReturn> {\n return (\n fnFactory: (\n ctx: CallbackContext,\n ) => (...args: TArgs) => Promise<TReturn>,\n ): ((...args: TArgs) => Promise<TReturn>) => {\n return trace<TArgs, TReturn>(\n {\n name: callbackConfig.name,\n spanKind: SpanKind.SERVER,\n },\n (baseCtx) => {\n return async (...args: TArgs) => {\n // Extract correlation key from arguments\n const correlationKey = callbackConfig.correlationKeyFrom(args);\n\n // Retrieve parked context\n const parkedContext = await parkingLot.retrieve(correlationKey);\n\n // Calculate elapsed time\n const elapsedMs = parkedContext\n ? Date.now() - parkedContext.parkedAt\n : null;\n\n // Set span attributes\n baseCtx.setAttribute(\n 'parking_lot.correlation_key',\n correlationKey,\n );\n\n if (parkedContext) {\n baseCtx.setAttribute('parking_lot.elapsed_ms', elapsedMs!);\n baseCtx.setAttribute(\n 'parking_lot.original_trace_id',\n parkedContext.traceId,\n );\n baseCtx.setAttribute(\n 'parking_lot.original_span_id',\n parkedContext.spanId,\n );\n\n // Add metadata as attributes\n if (parkedContext.metadata) {\n for (const [key, value] of Object.entries(\n parkedContext.metadata,\n )) {\n baseCtx.setAttribute(`parking_lot.metadata.${key}`, value);\n }\n }\n\n // Create span link to original trace\n const link = parkingLot.createLink(parkedContext);\n baseCtx.addLinks([link]);\n\n emitCorrelatedEvent(baseCtx, 'parked_context_retrieved', {\n 'parking_lot.correlation_key': correlationKey,\n 'parking_lot.elapsed_ms': elapsedMs!,\n 'parking_lot.original_trace_id': parkedContext.traceId,\n });\n } else {\n baseCtx.setAttribute('parking_lot.context_found', false);\n\n if (callbackConfig.requireParkedContext) {\n const error = new Error(\n `Required parked context not found for key: ${correlationKey}`,\n );\n recordStructuredError(baseCtx, error);\n throw error;\n }\n }\n\n // Apply custom attributes\n if (callbackConfig.attributes) {\n for (const [key, value] of Object.entries(\n callbackConfig.attributes,\n )) {\n baseCtx.setAttribute(key, value);\n }\n }\n\n // Create extended context\n const callbackCtx: CallbackContext = {\n ...baseCtx,\n parkedContext,\n elapsedMs,\n correlationKey,\n };\n\n // Execute user's function\n const userFn = fnFactory(callbackCtx);\n return userFn(...args);\n };\n },\n );\n };\n },\n\n createLink(storedContext: StoredTraceContext): Link {\n return {\n context: {\n traceId: storedContext.traceId,\n spanId: storedContext.spanId,\n traceFlags: storedContext.traceFlags,\n isRemote: true,\n },\n attributes: {\n 'link.type': 'parking_lot',\n 'parking_lot.parked_at': storedContext.parkedAt,\n ...(storedContext.metadata && {\n 'parking_lot.has_metadata': true,\n }),\n },\n };\n },\n\n async exists(correlationKey: string): Promise<boolean> {\n const fullKey = prefixKey(correlationKey);\n const context = await store.load(fullKey);\n return context !== null;\n },\n };\n\n return parkingLot;\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/**\n * Create a correlation key from multiple parts\n *\n * @param parts - Key parts to join\n * @returns A correlation key string\n *\n * @example\n * ```typescript\n * const key = createCorrelationKey('payment', orderId, 'stripe');\n * // Returns: \"payment:order-123:stripe\"\n * ```\n */\nexport function createCorrelationKey(...parts: (string | number)[]): string {\n return parts.map(String).join(':');\n}\n\n/**\n * Extract span context from stored context for manual linking\n *\n * @param storedContext - The stored trace context\n * @returns SpanContext compatible object\n */\nexport function toSpanContext(storedContext: StoredTraceContext): SpanContext {\n return {\n traceId: storedContext.traceId,\n spanId: storedContext.spanId,\n traceFlags: storedContext.traceFlags,\n isRemote: true,\n };\n}\n"]}
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
var chunk4P6ZOARG_cjs = require('./chunk-4P6ZOARG.cjs');
|
|
4
4
|
var chunkINJD3G4K_cjs = require('./chunk-INJD3G4K.cjs');
|
|
5
|
-
var
|
|
5
|
+
var chunkV7UBMJAB_cjs = require('./chunk-V7UBMJAB.cjs');
|
|
6
6
|
require('./chunk-OPPXYVEZ.cjs');
|
|
7
7
|
require('./chunk-VQTCQKHQ.cjs');
|
|
8
|
-
require('./chunk-
|
|
8
|
+
require('./chunk-FMTHVSYY.cjs');
|
|
9
|
+
require('./chunk-EE6CPXKH.cjs');
|
|
9
10
|
require('./chunk-R7QYGZUP.cjs');
|
|
10
11
|
require('./chunk-QWW3E3JM.cjs');
|
|
11
12
|
require('./chunk-CEAQK2QY.cjs');
|
|
@@ -58,7 +59,7 @@ var WorkflowBaggage = chunkINJD3G4K_cjs.createSafeBaggageSchema(workflowBaggageF
|
|
|
58
59
|
function traceDistributedWorkflow(config) {
|
|
59
60
|
const spanName = `workflow.${config.name}`;
|
|
60
61
|
return (fnFactory) => {
|
|
61
|
-
return
|
|
62
|
+
return chunkV7UBMJAB_cjs.trace(
|
|
62
63
|
{ name: spanName, spanKind: api.SpanKind.INTERNAL },
|
|
63
64
|
(baseCtx) => {
|
|
64
65
|
return async (...args) => {
|
|
@@ -157,7 +158,7 @@ function traceDistributedWorkflow(config) {
|
|
|
157
158
|
function traceDistributedStep(config) {
|
|
158
159
|
const spanName = `workflow.step.${config.name}`;
|
|
159
160
|
return (fnFactory) => {
|
|
160
|
-
return
|
|
161
|
+
return chunkV7UBMJAB_cjs.trace(
|
|
161
162
|
{ name: spanName, spanKind: api.SpanKind.INTERNAL },
|
|
162
163
|
(baseCtx) => {
|
|
163
164
|
return async (...args) => {
|