@tallyrow/safesignal 1.0.1-rc.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { g as LoggerConfig, C as CreateLoggerOptions, f as Logger, R as RedactionRule, h as Redactor, i as ScrubUrlOptions, j as TransportFactory } from './types-D-xVvmvX.cjs';
2
- export { A as AppIdentity, a as AttributeValue, b as Attributes, E as ErrorInfo, L as LevelMap, c as LogContext, d as LogEvent, e as LogLevel, M as ModuleIdentity, S as SanitizerLimits, T as Transport } from './types-D-xVvmvX.cjs';
1
+ import { g as LoggerConfig, C as CreateLoggerOptions, f as Logger, R as RedactionRule, h as Redactor, i as ScrubUrlOptions, T as TraceContext, k as TransportFactory } from './types-BiRyHi1e.cjs';
2
+ export { A as AppIdentity, a as AttributeValue, b as Attributes, E as ErrorInfo, L as LevelMap, c as LogContext, d as LogEvent, e as LogLevel, M as ModuleIdentity, S as SanitizerLimits, j as Transport } from './types-BiRyHi1e.cjs';
3
3
 
4
4
  /**
5
5
  * Public logger factories and root configuration flow.
@@ -138,6 +138,29 @@ declare function createRedactor(rules?: RedactionRule[]): Redactor;
138
138
 
139
139
  declare function scrubUrl(url: string, options?: ScrubUrlOptions): string;
140
140
 
141
+ /**
142
+ * Pure W3C `traceparent` parser — the one ergonomic affordance for the common
143
+ * case where a host holds the header string (e.g. SSR-injected) rather than a
144
+ * pre-parsed object.
145
+ *
146
+ * Format: `version-traceId-spanId-flags` =
147
+ * `00-<32hex>-<16hex>-<2hex>`. Version `00` is the current spec; for forward
148
+ * compatibility, any 2-hex version is accepted and only the four known fields
149
+ * are read (extra trailing fields are ignored).
150
+ *
151
+ * The parsed candidate is run through `normalizeTraceContext`, so the result is
152
+ * always a valid `TraceContext` or `undefined`. NEVER throws.
153
+ *
154
+ * Specs: `specs/008-trace-context/contracts/trace-context.md` TC-5;
155
+ * `research.md` D2.
156
+ */
157
+
158
+ /**
159
+ * Parse a W3C `traceparent` string (and optional `tracestate`) into a validated
160
+ * `TraceContext`, or `undefined` on any shape violation. Pure; never throws.
161
+ */
162
+ declare function parseTraceparent(traceparent: string, tracestate?: string): TraceContext | undefined;
163
+
141
164
  /**
142
165
  * Built-in `ConsoleTransport`. Passes events to `console[level]` with the
143
166
  * event message as the first argument and the **structured `LogEvent`
@@ -164,4 +187,4 @@ declare const ConsoleTransport: TransportFactory;
164
187
 
165
188
  declare const NoopTransport: TransportFactory;
166
189
 
167
- export { ConsoleTransport, CreateLoggerOptions, Logger, LoggerConfig, NoopTransport, RedactionRule, Redactor, ScrubUrlOptions, TransportFactory, configureLogging, createLogger, createRedactor, getRootLogger, scrubUrl };
190
+ export { ConsoleTransport, CreateLoggerOptions, Logger, LoggerConfig, NoopTransport, RedactionRule, Redactor, ScrubUrlOptions, TraceContext, TransportFactory, configureLogging, createLogger, createRedactor, getRootLogger, parseTraceparent, scrubUrl };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { g as LoggerConfig, C as CreateLoggerOptions, f as Logger, R as RedactionRule, h as Redactor, i as ScrubUrlOptions, j as TransportFactory } from './types-D-xVvmvX.js';
2
- export { A as AppIdentity, a as AttributeValue, b as Attributes, E as ErrorInfo, L as LevelMap, c as LogContext, d as LogEvent, e as LogLevel, M as ModuleIdentity, S as SanitizerLimits, T as Transport } from './types-D-xVvmvX.js';
1
+ import { g as LoggerConfig, C as CreateLoggerOptions, f as Logger, R as RedactionRule, h as Redactor, i as ScrubUrlOptions, T as TraceContext, k as TransportFactory } from './types-BiRyHi1e.js';
2
+ export { A as AppIdentity, a as AttributeValue, b as Attributes, E as ErrorInfo, L as LevelMap, c as LogContext, d as LogEvent, e as LogLevel, M as ModuleIdentity, S as SanitizerLimits, j as Transport } from './types-BiRyHi1e.js';
3
3
 
4
4
  /**
5
5
  * Public logger factories and root configuration flow.
@@ -138,6 +138,29 @@ declare function createRedactor(rules?: RedactionRule[]): Redactor;
138
138
 
139
139
  declare function scrubUrl(url: string, options?: ScrubUrlOptions): string;
140
140
 
141
+ /**
142
+ * Pure W3C `traceparent` parser — the one ergonomic affordance for the common
143
+ * case where a host holds the header string (e.g. SSR-injected) rather than a
144
+ * pre-parsed object.
145
+ *
146
+ * Format: `version-traceId-spanId-flags` =
147
+ * `00-<32hex>-<16hex>-<2hex>`. Version `00` is the current spec; for forward
148
+ * compatibility, any 2-hex version is accepted and only the four known fields
149
+ * are read (extra trailing fields are ignored).
150
+ *
151
+ * The parsed candidate is run through `normalizeTraceContext`, so the result is
152
+ * always a valid `TraceContext` or `undefined`. NEVER throws.
153
+ *
154
+ * Specs: `specs/008-trace-context/contracts/trace-context.md` TC-5;
155
+ * `research.md` D2.
156
+ */
157
+
158
+ /**
159
+ * Parse a W3C `traceparent` string (and optional `tracestate`) into a validated
160
+ * `TraceContext`, or `undefined` on any shape violation. Pure; never throws.
161
+ */
162
+ declare function parseTraceparent(traceparent: string, tracestate?: string): TraceContext | undefined;
163
+
141
164
  /**
142
165
  * Built-in `ConsoleTransport`. Passes events to `console[level]` with the
143
166
  * event message as the first argument and the **structured `LogEvent`
@@ -164,4 +187,4 @@ declare const ConsoleTransport: TransportFactory;
164
187
 
165
188
  declare const NoopTransport: TransportFactory;
166
189
 
167
- export { ConsoleTransport, CreateLoggerOptions, Logger, LoggerConfig, NoopTransport, RedactionRule, Redactor, ScrubUrlOptions, TransportFactory, configureLogging, createLogger, createRedactor, getRootLogger, scrubUrl };
190
+ export { ConsoleTransport, CreateLoggerOptions, Logger, LoggerConfig, NoopTransport, RedactionRule, Redactor, ScrubUrlOptions, TraceContext, TransportFactory, configureLogging, createLogger, createRedactor, getRootLogger, parseTraceparent, scrubUrl };
package/dist/index.mjs CHANGED
@@ -18,12 +18,16 @@ function mergeContexts(...sources) {
18
18
  src.attributes
19
19
  );
20
20
  }
21
+ if (src.trace !== void 0) {
22
+ merged.trace = src.trace;
23
+ }
21
24
  }
22
25
  const out = {};
23
26
  if (merged.application !== void 0) out.application = merged.application;
24
27
  if (merged.module !== void 0) out.module = merged.module;
25
28
  if (merged.environment !== void 0) out.environment = merged.environment;
26
29
  if (merged.attributes !== void 0) out.attributes = merged.attributes;
30
+ if (merged.trace !== void 0) out.trace = merged.trace;
27
31
  return out;
28
32
  }
29
33
  function deepMergeAttributes(earlier, later) {
@@ -92,7 +96,7 @@ function escapeControlChars(value) {
92
96
  if (!HAS_CONTROL_CHAR.test(value)) return value;
93
97
  return value.replace(CONTROL_CHAR_GLOBAL, (ch) => {
94
98
  const code = ch.charCodeAt(0);
95
- return "\\u" + code.toString(16).padStart(4, "0");
99
+ return `\\u${code.toString(16).padStart(4, "0")}`;
96
100
  });
97
101
  }
98
102
  var controlCharGuard = (event, _config) => {
@@ -240,7 +244,10 @@ function createRedactor(rules) {
240
244
  let error = event.error;
241
245
  if (event.error !== void 0) {
242
246
  const escName = applyShapeRules(event.error.name, compiled.shapeRules);
243
- const escMessage = applyShapeRules(event.error.message, compiled.shapeRules);
247
+ const escMessage = applyShapeRules(
248
+ event.error.message,
249
+ compiled.shapeRules
250
+ );
244
251
  const stack = event.error.stack;
245
252
  const escStack = stack === void 0 ? void 0 : applyShapeRules(stack, compiled.shapeRules);
246
253
  const errorChanged = escName !== event.error.name || escMessage !== event.error.message || escStack !== stack;
@@ -365,12 +372,13 @@ function isLogEventShape(value) {
365
372
  return false;
366
373
  }
367
374
  const obj = value;
368
- if (typeof obj["timestamp"] !== "string") return false;
369
- if (typeof obj["level"] !== "string") return false;
370
- if (!VALID_LEVELS.has(obj["level"])) return false;
371
- if (typeof obj["message"] !== "string") return false;
372
- if (obj["attributes"] === null || typeof obj["attributes"] !== "object") return false;
373
- if (obj["context"] === null || typeof obj["context"] !== "object") return false;
375
+ if (typeof obj.timestamp !== "string") return false;
376
+ if (typeof obj.level !== "string") return false;
377
+ if (!VALID_LEVELS.has(obj.level)) return false;
378
+ if (typeof obj.message !== "string") return false;
379
+ if (obj.attributes === null || typeof obj.attributes !== "object")
380
+ return false;
381
+ if (obj.context === null || typeof obj.context !== "object") return false;
374
382
  return true;
375
383
  }
376
384
 
@@ -450,7 +458,8 @@ function sanitizeValueImpl(value, depth, ctx) {
450
458
  if (depth > ctx.maxDepth) return "[MaxDepth]";
451
459
  if (value === null) return null;
452
460
  const t = typeof value;
453
- if (t === "string") return truncateString(value, ctx.maxStringLength);
461
+ if (t === "string")
462
+ return truncateString(value, ctx.maxStringLength);
454
463
  if (t === "number") {
455
464
  const n = value;
456
465
  return Number.isFinite(n) ? n : null;
@@ -560,7 +569,7 @@ function sanitizeErrorAsAttribute(err, depth, ctx) {
560
569
  };
561
570
  const stack = safeOptional(() => err.stack);
562
571
  if (stack !== void 0) {
563
- reduced["stack"] = stack;
572
+ reduced.stack = stack;
564
573
  }
565
574
  return sanitizeObject(reduced, depth, ctx);
566
575
  }
@@ -681,7 +690,7 @@ var urlScrub = (event, _config) => {
681
690
  const message = maybeScrubString(event.message);
682
691
  const attributes = walkAttributes2(event.attributes);
683
692
  const context = walkContext3(event.context);
684
- let error = void 0;
693
+ let error;
685
694
  if (event.error !== void 0) {
686
695
  const scrubbedMessage = maybeScrubString(event.error.message);
687
696
  const stack = event.error.stack;
@@ -745,7 +754,7 @@ function scrubHashFragment(parsed, extras) {
745
754
  }
746
755
  }
747
756
  if (!changed) return false;
748
- parsed.hash = "#" + out.join("&");
757
+ parsed.hash = `#${out.join("&")}`;
749
758
  return true;
750
759
  }
751
760
  function isDenied(name, extras) {
@@ -1108,6 +1117,36 @@ function installRuntime(runtime) {
1108
1117
  return previous;
1109
1118
  }
1110
1119
 
1120
+ // src/trace/validate.ts
1121
+ var MAX_TRACESTATE_LEN = 512;
1122
+ var TRACE_ID_RE = /^[0-9a-f]{32}$/;
1123
+ var SPAN_ID_RE = /^[0-9a-f]{16}$/;
1124
+ var ALL_ZERO_TRACE_ID = "0".repeat(32);
1125
+ var ALL_ZERO_SPAN_ID = "0".repeat(16);
1126
+ function isValidTraceId(value) {
1127
+ return typeof value === "string" && TRACE_ID_RE.test(value) && value !== ALL_ZERO_TRACE_ID;
1128
+ }
1129
+ function isValidSpanId(value) {
1130
+ return typeof value === "string" && SPAN_ID_RE.test(value) && value !== ALL_ZERO_SPAN_ID;
1131
+ }
1132
+ function normalizeTraceContext(candidate) {
1133
+ if (typeof candidate !== "object" || candidate === null) {
1134
+ return void 0;
1135
+ }
1136
+ const c = candidate;
1137
+ if (!isValidTraceId(c.traceId) || !isValidSpanId(c.spanId)) {
1138
+ return void 0;
1139
+ }
1140
+ const trace = { traceId: c.traceId, spanId: c.spanId };
1141
+ if (typeof c.traceFlags === "number" && Number.isInteger(c.traceFlags) && c.traceFlags >= 0 && c.traceFlags <= 255) {
1142
+ trace.traceFlags = c.traceFlags;
1143
+ }
1144
+ if (typeof c.traceState === "string" && c.traceState.length > 0 && c.traceState.length <= MAX_TRACESTATE_LEN) {
1145
+ trace.traceState = c.traceState;
1146
+ }
1147
+ return trace;
1148
+ }
1149
+
1111
1150
  // src/api/logger.ts
1112
1151
  var rootLogger;
1113
1152
  function installState(config) {
@@ -1180,6 +1219,14 @@ function makeLogger(options, chainedContexts) {
1180
1219
  ...chainedContexts,
1181
1220
  correlation
1182
1221
  );
1222
+ if (context.trace !== void 0) {
1223
+ const normalized = normalizeTraceContext(context.trace);
1224
+ if (normalized === void 0) {
1225
+ delete context.trace;
1226
+ } else {
1227
+ context.trace = normalized;
1228
+ }
1229
+ }
1183
1230
  const event = buildLogEvent({
1184
1231
  level,
1185
1232
  message,
@@ -1211,6 +1258,27 @@ function makeLogger(options, chainedContexts) {
1211
1258
  };
1212
1259
  }
1213
1260
 
1261
+ // src/trace/traceparent.ts
1262
+ var VERSION_RE = /^[0-9a-f]{2}$/;
1263
+ function parseTraceparent(traceparent, tracestate) {
1264
+ if (typeof traceparent !== "string") return void 0;
1265
+ const parts = traceparent.trim().split("-");
1266
+ if (parts.length < 4) return void 0;
1267
+ const [version, traceId, spanId, flagsHex] = parts;
1268
+ if (version === void 0 || !VERSION_RE.test(version)) return void 0;
1269
+ if (version === "ff") return void 0;
1270
+ if (flagsHex === void 0 || !/^[0-9a-f]{2}$/.test(flagsHex)) {
1271
+ return void 0;
1272
+ }
1273
+ const traceFlags = Number.parseInt(flagsHex, 16);
1274
+ return normalizeTraceContext({
1275
+ traceId,
1276
+ spanId,
1277
+ traceFlags,
1278
+ ...typeof tracestate === "string" ? { traceState: tracestate } : {}
1279
+ });
1280
+ }
1281
+
1214
1282
  // src/transport/console-transport.ts
1215
1283
  function resolveConsoleMethod(level) {
1216
1284
  const slot = console[level];
@@ -1227,6 +1295,6 @@ var ConsoleTransport = () => ({
1227
1295
  }
1228
1296
  });
1229
1297
 
1230
- export { ConsoleTransport, NoopTransport, configureLogging, createLogger, createRedactor, getRootLogger, scrubUrl };
1298
+ export { ConsoleTransport, NoopTransport, configureLogging, createLogger, createRedactor, getRootLogger, parseTraceparent, scrubUrl };
1231
1299
  //# sourceMappingURL=index.mjs.map
1232
1300
  //# sourceMappingURL=index.mjs.map