@logbrew/sdk 0.1.38 → 0.1.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/automatic-delivery.cjs +271 -0
  2. package/batch-sender.cjs +164 -0
  3. package/bounded-number.cjs +34 -0
  4. package/browser-breadcrumbs.cjs +346 -136
  5. package/capture-support.cjs +100 -0
  6. package/client-options.cjs +292 -0
  7. package/client-report.cjs +232 -0
  8. package/client.cjs +447 -0
  9. package/console-arguments.cjs +151 -0
  10. package/console-capture.cjs +194 -0
  11. package/core.cjs +30 -2810
  12. package/delivery-health.cjs +133 -0
  13. package/error-causes.cjs +130 -0
  14. package/error-details.cjs +124 -0
  15. package/error-issue.cjs +187 -0
  16. package/event-clone.cjs +55 -0
  17. package/event-metadata.cjs +40 -0
  18. package/event-pipeline.cjs +325 -0
  19. package/event-queue.cjs +306 -0
  20. package/event-store.cjs +237 -0
  21. package/event-validation.cjs +159 -0
  22. package/examples/agent-timeline.cjs +57 -46
  23. package/examples/agent-timeline.mjs +41 -35
  24. package/examples/index.mjs +122 -94
  25. package/examples/readme-example.cjs +36 -27
  26. package/examples/readme-example.mjs +21 -17
  27. package/examples/real-user-smoke.cjs +41 -32
  28. package/examples/real-user-smoke.mjs +26 -22
  29. package/exception-chain.cjs +205 -0
  30. package/index.d.ts +144 -1076
  31. package/index.js +1 -1
  32. package/issue-exception.cjs +13 -8
  33. package/metric-validation.cjs +95 -0
  34. package/network-milestone.cjs +155 -0
  35. package/null-value.cjs +4 -0
  36. package/opentelemetry-bridge.cjs +330 -0
  37. package/opentelemetry-context.cjs +245 -0
  38. package/opentelemetry-exceptions.cjs +69 -0
  39. package/opentelemetry-keys.cjs +183 -0
  40. package/opentelemetry-span.cjs +286 -0
  41. package/opentelemetry-summary.cjs +213 -0
  42. package/opentelemetry-time.cjs +131 -0
  43. package/opentelemetry.cjs +99 -1068
  44. package/package.json +55 -1
  45. package/pino-destination.cjs +108 -0
  46. package/pino-record.cjs +237 -0
  47. package/product-action.cjs +163 -0
  48. package/provided-breadcrumbs.cjs +117 -0
  49. package/random-hex.cjs +29 -0
  50. package/react-native.js +1 -1
  51. package/recording-transport.cjs +68 -0
  52. package/release-artifacts-build.cjs +263 -84
  53. package/release-artifacts-common.js +347 -66
  54. package/release-artifacts-debug-id.js +178 -0
  55. package/release-artifacts-inspect.js +213 -0
  56. package/release-artifacts-issue-event.js +99 -0
  57. package/release-artifacts-manifest-artifact.js +107 -0
  58. package/release-artifacts-manifest.js +77 -0
  59. package/release-artifacts-mappings.js +265 -0
  60. package/release-artifacts-prepare.js +297 -0
  61. package/release-artifacts-source-context.js +210 -0
  62. package/release-artifacts-symbolication.js +272 -421
  63. package/release-artifacts-upload-files.js +122 -0
  64. package/release-artifacts-upload-request.js +246 -0
  65. package/release-artifacts-upload.js +155 -300
  66. package/release-artifacts-urls.js +49 -0
  67. package/release-artifacts.js +189 -662
  68. package/retry-delay.cjs +32 -0
  69. package/span-validation.cjs +191 -0
  70. package/stored-events.cjs +243 -0
  71. package/types/client.d.ts +67 -0
  72. package/types/delivery.d.ts +125 -0
  73. package/types/issues.d.ts +249 -0
  74. package/types/logging.d.ts +175 -0
  75. package/types/opentelemetry.d.ts +205 -0
  76. package/types/product.d.ts +128 -0
  77. package/types/recording-transport.d.ts +15 -0
  78. package/types/sdk-error.d.ts +9 -0
  79. package/types/telemetry.d.ts +171 -0
  80. package/types/trace-context.d.ts +110 -0
  81. package/types/transport-error.d.ts +10 -0
  82. package/utf8.cjs +37 -0
  83. package/validation.cjs +49 -1
  84. package/vite-release-artifacts-config.cjs +120 -0
  85. package/vite-release-artifacts.cjs +199 -99
  86. package/vite-release-artifacts.d.cts +65 -40
  87. package/vite-release-artifacts.d.ts +65 -40
  88. package/vite-release-artifacts.js +2 -2
  89. package/winston-info.cjs +205 -0
  90. package/winston.cjs +189 -289
@@ -0,0 +1,128 @@
1
+ import type { ActionAttributes, Metadata } from "./telemetry.js";
2
+
3
+ /** Stable product-analytics event categories carried in reserved action metadata. */
4
+ type ProductAnalyticsKind = "page_view" | "screen_view" | "interaction";
5
+
6
+ /** App-owned product step input for agent-readable action timelines. */
7
+ type ProductActionInput = string | {
8
+ name: string;
9
+ status?: ActionAttributes["status"];
10
+ sessionId?: string;
11
+ traceId?: string;
12
+ routeTemplate?: string;
13
+ screen?: string;
14
+ funnel?: string;
15
+ step?: string;
16
+ metadata?: Metadata;
17
+ };
18
+
19
+ /** App-owned API milestone input for agent-readable network timelines. */
20
+ type NetworkMilestoneInput = string | {
21
+ name?: string;
22
+ routeTemplate: string;
23
+ method?: string;
24
+ status?: ActionAttributes["status"];
25
+ statusCode?: number;
26
+ durationMs?: number;
27
+ sessionId?: string;
28
+ traceId?: string;
29
+ metadata?: Metadata;
30
+ };
31
+
32
+ /** Shared timeline helper options for primitive app metadata. */
33
+ type TimelineAttributesOptions = {
34
+ metadata?: Metadata;
35
+ };
36
+
37
+ /** Support-ticket sources accepted by explicit draft helpers. */
38
+ type SupportTicketSource = "cli" | "sdk" | "website" | "docs" | "mobile";
39
+
40
+ /** Support-ticket categories accepted by explicit draft helpers. */
41
+ type SupportTicketCategory =
42
+ | "sdk_install_failure"
43
+ | "ingest_failure"
44
+ | "auth_failure"
45
+ | "project_setup"
46
+ | "dashboard_issue"
47
+ | "docs_confusion"
48
+ | "cli_issue"
49
+ | "mobile_issue"
50
+ | "billing_question"
51
+ | "other";
52
+
53
+ /** JSON-like diagnostics input sanitized before a support-ticket draft is returned. */
54
+ type SupportDiagnosticsValue =
55
+ | string
56
+ | number
57
+ | boolean
58
+ | null
59
+ | SupportDiagnosticsValue[]
60
+ | { [key: string]: SupportDiagnosticsValue };
61
+
62
+ /** Explicit local-only support-ticket draft input. This does not open a ticket. */
63
+ type SupportTicketDraftInput = {
64
+ source: SupportTicketSource;
65
+ category: SupportTicketCategory;
66
+ title: string;
67
+ description: string;
68
+ projectId?: string;
69
+ environment?: string;
70
+ runtime?: string;
71
+ framework?: string;
72
+ sdkPackage?: string;
73
+ sdkVersion?: string;
74
+ release?: string;
75
+ traceId?: string;
76
+ eventId?: string;
77
+ diagnostics?: Record<string, unknown>;
78
+ };
79
+
80
+ /** Support-ticket create body built locally. It sends nothing. */
81
+ type SupportTicketDraft = {
82
+ source: SupportTicketSource;
83
+ category: SupportTicketCategory;
84
+ title: string;
85
+ description: string;
86
+ project_id?: string;
87
+ environment?: string;
88
+ runtime?: string;
89
+ framework?: string;
90
+ sdk_package?: string;
91
+ sdk_version?: string;
92
+ release?: string;
93
+ trace_id?: string;
94
+ event_id?: string;
95
+ diagnostics?: Record<string, SupportDiagnosticsValue>;
96
+ };
97
+
98
+ /** Create safe action attributes for an app-owned product step without automatic UI capture. */
99
+ declare function createProductActionAttributes(
100
+ action: ProductActionInput,
101
+ options?: TimelineAttributesOptions
102
+ ): ActionAttributes;
103
+
104
+ /** Create safe action attributes for an app-owned network milestone without HTTP client patching. */
105
+ declare function createNetworkMilestoneAttributes(
106
+ request: NetworkMilestoneInput,
107
+ options?: TimelineAttributesOptions
108
+ ): ActionAttributes;
109
+
110
+ /** Build a local-only, token-free support-ticket create payload draft without calling backend routes. */
111
+ declare function createSupportTicketDraft(input: SupportTicketDraftInput): SupportTicketDraft;
112
+
113
+ export type {
114
+ NetworkMilestoneInput,
115
+ ProductActionInput,
116
+ ProductAnalyticsKind,
117
+ SupportDiagnosticsValue,
118
+ SupportTicketCategory,
119
+ SupportTicketDraft,
120
+ SupportTicketDraftInput,
121
+ SupportTicketSource,
122
+ TimelineAttributesOptions
123
+ };
124
+ export {
125
+ createNetworkMilestoneAttributes,
126
+ createProductActionAttributes,
127
+ createSupportTicketDraft
128
+ };
@@ -0,0 +1,15 @@
1
+ import type { TransportResponse } from "./delivery.js";
2
+
3
+ /** Scripted transport for previewing, accepting, or failing queued event flushes. */
4
+ declare class RecordingTransport {
5
+ public constructor(scriptedResponses?: ({ statusCode: number; retryAfterMs?: number; errorCode?: string } | Error)[]);
6
+ /** Every request body sent through this transport instance. */
7
+ public sentBodies: string[];
8
+ /** Create a transport that accepts queued flushes with a 202 response. */
9
+ public static alwaysAccept(): RecordingTransport;
10
+ /** Return the most recent request body sent through this transport. */
11
+ public lastBody(): string | null;
12
+ public send(apiKey: string, body: string): Promise<TransportResponse>;
13
+ }
14
+
15
+ export { RecordingTransport };
@@ -0,0 +1,9 @@
1
+ /** Stable public SDK error with parseable code and message fields. */
2
+ declare class SdkError extends Error {
3
+ public code: string;
4
+ public retryAfterMs?: number;
5
+ public retryable?: boolean;
6
+ public constructor(code: string, message: string, details?: { retryAfterMs?: number; retryable?: boolean });
7
+ }
8
+
9
+ export { SdkError };
@@ -0,0 +1,171 @@
1
+ declare const TELEMETRY_CONTEXT_SCHEMA_VERSION = 1;
2
+
3
+ /** Metadata values that can be attached to public LogBrew event payloads. */
4
+ type MetadataValue = string | number | boolean | null;
5
+
6
+ /** Structured metadata map shared by public LogBrew event attribute types. */
7
+ type Metadata = Record<string, MetadataValue>;
8
+
9
+ /** Bounded service, runtime, or framework identity shared by telemetry signals. */
10
+ type TelemetryNamedVersion = {
11
+ name: string;
12
+ version?: string;
13
+ };
14
+
15
+ /** Privacy-bounded resource identity shared by telemetry signals. */
16
+ type TelemetryResource = {
17
+ service?: TelemetryNamedVersion;
18
+ deployment?: { environment?: string; release?: string };
19
+ runtime?: TelemetryNamedVersion;
20
+ framework?: TelemetryNamedVersion;
21
+ operatingSystem?: TelemetryNamedVersion & { build?: string };
22
+ device?: { family?: string; model?: string; architecture?: string };
23
+ application?: { name?: string; version?: string; build?: string };
24
+ };
25
+
26
+ /** W3C-compatible correlation identity shared by non-span telemetry. */
27
+ type TelemetryTraceContext = {
28
+ traceId: string;
29
+ spanId?: string;
30
+ parentSpanId?: string;
31
+ sampled?: boolean;
32
+ };
33
+
34
+ /** Opaque application session identity. */
35
+ type TelemetrySessionContext = {
36
+ id: string;
37
+ previousId?: string;
38
+ };
39
+
40
+ /** Explicit app-owned subject identity; do not send names, email addresses, or IP addresses. */
41
+ type TelemetrySubjectContext = {
42
+ id: string;
43
+ kind: "anonymous" | "user";
44
+ };
45
+
46
+ /** Versioned shared context available on every LogBrew event type. */
47
+ type TelemetryContext = {
48
+ schemaVersion: typeof TELEMETRY_CONTEXT_SCHEMA_VERSION;
49
+ resource?: TelemetryResource;
50
+ trace?: TelemetryTraceContext;
51
+ session?: TelemetrySessionContext;
52
+ subject?: TelemetrySubjectContext;
53
+ /** Up to 32 low-cardinality string dimensions with safe machine keys. */
54
+ tags?: Record<string, string>;
55
+ };
56
+
57
+ /** Canonical user-facing severity categories accepted by LogBrew. */
58
+ type Severity = "info" | "warning" | "error" | "critical";
59
+
60
+ /** Runtime-level aliases accepted for compatibility and normalized before send. */
61
+ type SeverityAlias = "trace" | "debug" | "warn" | "fatal";
62
+
63
+ /** Public severity input accepted by issue and log attributes. */
64
+ type SeverityInput = Severity | SeverityAlias;
65
+
66
+ /** Public release event attributes. */
67
+ type ReleaseAttributes = {
68
+ version: string;
69
+ commit?: string;
70
+ notes?: string;
71
+ metadata?: Metadata;
72
+ context?: TelemetryContext;
73
+ };
74
+
75
+ /** Public environment event attributes. */
76
+ type EnvironmentAttributes = {
77
+ name: string;
78
+ region?: string;
79
+ metadata?: Metadata;
80
+ context?: TelemetryContext;
81
+ };
82
+
83
+ /** Public log event attributes. */
84
+ type LogAttributes = {
85
+ message: string;
86
+ level: SeverityInput;
87
+ logger?: string;
88
+ metadata?: Metadata;
89
+ context?: TelemetryContext;
90
+ };
91
+
92
+ /** Privacy-bounded milestone recorded inside a span. */
93
+ type SpanEventSummary = {
94
+ name: string;
95
+ timestamp?: string;
96
+ metadata?: Metadata;
97
+ };
98
+
99
+ /** Privacy-bounded reference from this span to another trace/span. */
100
+ type SpanLinkSummary = {
101
+ traceId: string;
102
+ spanId: string;
103
+ sampled?: boolean;
104
+ metadata?: Metadata;
105
+ };
106
+
107
+ /** Public span event attributes. */
108
+ type SpanAttributes = {
109
+ name: string;
110
+ traceId: string;
111
+ spanId: string;
112
+ parentSpanId?: string;
113
+ status: "ok" | "error";
114
+ durationMs?: number;
115
+ events?: SpanEventSummary[];
116
+ links?: SpanLinkSummary[];
117
+ metadata?: Metadata;
118
+ context?: TelemetryContext;
119
+ };
120
+
121
+ /** Public action event attributes. */
122
+ type ActionAttributes = {
123
+ name: string;
124
+ status: "queued" | "running" | "success" | "failure";
125
+ metadata?: Metadata;
126
+ context?: TelemetryContext;
127
+ };
128
+
129
+ /** Public metric event attributes. Use low-cardinality metadata only. */
130
+ type MetricAttributes = {
131
+ name: string;
132
+ /** Optional stable, single-line meaning; 1 to 1,024 Unicode characters. */
133
+ description?: string;
134
+ kind: "counter" | "histogram";
135
+ value: number;
136
+ unit: string;
137
+ temporality: "delta" | "cumulative";
138
+ metadata?: Metadata;
139
+ context?: TelemetryContext;
140
+ } | {
141
+ name: string;
142
+ /** Optional stable, single-line meaning; 1 to 1,024 Unicode characters. */
143
+ description?: string;
144
+ kind: "gauge";
145
+ value: number;
146
+ unit: string;
147
+ temporality: "instant";
148
+ metadata?: Metadata;
149
+ };
150
+
151
+ export type {
152
+ ActionAttributes,
153
+ EnvironmentAttributes,
154
+ LogAttributes,
155
+ Metadata,
156
+ MetadataValue,
157
+ MetricAttributes,
158
+ ReleaseAttributes,
159
+ Severity,
160
+ SeverityAlias,
161
+ SeverityInput,
162
+ SpanAttributes,
163
+ SpanEventSummary,
164
+ SpanLinkSummary,
165
+ TelemetryContext,
166
+ TelemetryNamedVersion,
167
+ TelemetryResource,
168
+ TelemetrySessionContext,
169
+ TelemetrySubjectContext,
170
+ TelemetryTraceContext
171
+ };
@@ -0,0 +1,110 @@
1
+ import type { Metadata, SpanAttributes, SpanEventSummary, SpanLinkSummary } from "./telemetry.js";
2
+
3
+ /** Parsed W3C trace context from a traceparent value. */
4
+ type TraceparentContext = {
5
+ version: string;
6
+ traceId: string;
7
+ parentSpanId: string;
8
+ traceFlags: string;
9
+ sampled: boolean;
10
+ };
11
+
12
+ /** Minimal active trace shape accepted by logger adapters for correlation metadata. */
13
+ type LogCorrelationTraceContext = {
14
+ traceId: string;
15
+ spanId: string;
16
+ parentSpanId?: string;
17
+ sampled?: boolean;
18
+ };
19
+
20
+ /** Inputs for creating a W3C traceparent value from known trace/span ids. */
21
+ type TraceparentInput = {
22
+ traceId: string;
23
+ spanId: string;
24
+ traceFlags?: string;
25
+ };
26
+
27
+ /** Privacy-bounded W3C tracestate entry for explicit propagation. */
28
+ type TracestateEntry = {
29
+ key: string;
30
+ value: string;
31
+ };
32
+
33
+ /** W3C baggage entry for explicit propagation. */
34
+ type BaggageEntry = {
35
+ key: string;
36
+ value: string;
37
+ properties?: string[];
38
+ };
39
+
40
+ /** Inputs for creating an explicit W3C trace context header carrier. */
41
+ type TraceContextInput = TraceparentInput & {
42
+ tracestate?: string | TracestateEntry[];
43
+ baggage?: string | BaggageEntry[];
44
+ };
45
+
46
+ /** Span fields supplied when deriving LogBrew span attributes from traceparent. */
47
+ type TraceparentSpanInput = {
48
+ name: string;
49
+ spanId: string;
50
+ status: "ok" | "error";
51
+ durationMs?: number;
52
+ links?: SpanLinkSummary[];
53
+ metadata?: Metadata;
54
+ events?: SpanEventSummary[];
55
+ };
56
+
57
+ /** Parse a W3C traceparent value into normalized trace/span context. */
58
+ declare function parseTraceparent(traceparent: string): TraceparentContext;
59
+
60
+ /** Create a W3C traceparent value from explicit trace/span ids. */
61
+ declare function createTraceparent(input: TraceparentInput): string;
62
+
63
+ /** Create an explicit outbound header carrier containing only traceparent. */
64
+ declare function createTraceparentHeaders(input: TraceparentInput): { traceparent: string };
65
+
66
+ /** Parse a W3C tracestate header into normalized entries. */
67
+ declare function parseTracestate(tracestate: string): TracestateEntry[];
68
+
69
+ /** Create a normalized W3C tracestate header from explicit entries. */
70
+ declare function createTracestate(entries: TracestateEntry[]): string;
71
+
72
+ /** Parse a W3C baggage header into decoded entries. */
73
+ declare function parseBaggage(baggage: string): BaggageEntry[];
74
+
75
+ /** Create a W3C baggage header from explicit entries. */
76
+ declare function createBaggage(entries: BaggageEntry[]): string;
77
+
78
+ /** Create an explicit outbound carrier for traceparent plus optional tracestate and baggage. */
79
+ declare function createTraceContextHeaders(input: TraceContextInput): {
80
+ traceparent: string;
81
+ tracestate?: string;
82
+ baggage?: string;
83
+ };
84
+
85
+ /** Build LogBrew span attributes that continue an incoming W3C traceparent value. */
86
+ declare function spanAttributesFromTraceparent(
87
+ traceparent: string,
88
+ attributes: TraceparentSpanInput
89
+ ): SpanAttributes;
90
+
91
+ export type {
92
+ BaggageEntry,
93
+ LogCorrelationTraceContext,
94
+ TraceContextInput,
95
+ TraceparentContext,
96
+ TraceparentInput,
97
+ TraceparentSpanInput,
98
+ TracestateEntry
99
+ };
100
+ export {
101
+ createBaggage,
102
+ createTraceContextHeaders,
103
+ createTraceparent,
104
+ createTraceparentHeaders,
105
+ createTracestate,
106
+ parseBaggage,
107
+ parseTraceparent,
108
+ parseTracestate,
109
+ spanAttributesFromTraceparent
110
+ };
@@ -0,0 +1,10 @@
1
+ /** Transport error that can optionally be marked retryable by the caller. */
2
+ declare class TransportError extends Error {
3
+ public code: string;
4
+ public retryable: boolean;
5
+ public constructor(code: string, message: string, retryable?: boolean);
6
+ /** Create a retryable network failure that preserves queued events. */
7
+ public static network(message: string): TransportError;
8
+ }
9
+
10
+ export { TransportError };
package/utf8.cjs ADDED
@@ -0,0 +1,37 @@
1
+ const ONE_BYTE_LIMIT = 0x80;
2
+ const TWO_BYTE_LIMIT = 0x8_00;
3
+ const THREE_BYTE_LIMIT = 0x1_00_00;
4
+ const ONE_BYTE = 1;
5
+ const TWO_BYTES = 2;
6
+ const THREE_BYTES = 3;
7
+ const FOUR_BYTES = 4;
8
+ const NO_BYTES = 0;
9
+
10
+ /**
11
+ * @param {string} character
12
+ * @returns {number}
13
+ */
14
+ function characterBytes(character) {
15
+ const codePoint = character.codePointAt(NO_BYTES) ?? NO_BYTES;
16
+ if (codePoint < ONE_BYTE_LIMIT) {
17
+ return ONE_BYTE;
18
+ }
19
+ if (codePoint < TWO_BYTE_LIMIT) {
20
+ return TWO_BYTES;
21
+ }
22
+ return codePoint < THREE_BYTE_LIMIT ? THREE_BYTES : FOUR_BYTES;
23
+ }
24
+
25
+ /**
26
+ * @param {string} value
27
+ * @returns {number}
28
+ */
29
+ function utf8ByteLength(value) {
30
+ let bytes = NO_BYTES;
31
+ for (const character of value) {
32
+ bytes += characterBytes(character);
33
+ }
34
+ return bytes;
35
+ }
36
+
37
+ module.exports = { utf8ByteLength };
package/validation.cjs CHANGED
@@ -7,6 +7,10 @@ const { SdkError } = require("./sdk-error.cjs");
7
7
  * : Value extends object ? { readonly [Key in keyof Value]: DeepReadonly<Value[Key]> } : Value} DeepReadonly
8
8
  */
9
9
 
10
+ /**
11
+ * @typedef {(...args: readonly never[]) => unknown} Callable
12
+ */
13
+
10
14
  const ZERO_TRACE_ID = "00000000000000000000000000000000";
11
15
  const ZERO_SPAN_ID = "0000000000000000";
12
16
  const TRACE_ID_PATTERN = /^[0-9a-fA-F]{32}$/u;
@@ -107,6 +111,45 @@ function requireNonEmpty(label, value) {
107
111
  return value;
108
112
  }
109
113
 
114
+ /** @type {(value: unknown) => boolean} */
115
+ const isTruthy = Boolean;
116
+
117
+ /**
118
+ * @param {unknown} value
119
+ * @returns {value is Callable}
120
+ */
121
+ function isCallable(value) {
122
+ return typeof value === "function";
123
+ }
124
+
125
+ /**
126
+ * @param {unknown} value
127
+ * @returns {value is Readonly<Record<string, unknown>>}
128
+ */
129
+ function isObjectValue(value) {
130
+ return typeof value === "object" && value !== null;
131
+ }
132
+
133
+ /**
134
+ * @param {unknown} value
135
+ * @param {string} key
136
+ * @returns {unknown}
137
+ */
138
+ function optionalProperty(value, key) {
139
+ if (value === undefined || value === null) {
140
+ return undefined;
141
+ }
142
+ return Reflect.get(new Object(value), key);
143
+ }
144
+
145
+ /**
146
+ * @param {unknown} value
147
+ * @returns {string | undefined}
148
+ */
149
+ function stringOrUndefined(value) {
150
+ return typeof value === "string" && value.trim() !== "" ? value : undefined;
151
+ }
152
+
110
153
  /**
111
154
  * @template {string} Allowed
112
155
  * @param {string} value
@@ -262,9 +305,13 @@ module.exports = {
262
305
  ZERO_TRACE_ID,
263
306
  hasUnicodeControlCharacter,
264
307
  isAllowedValue,
308
+ isCallable,
265
309
  isList,
310
+ isObjectValue,
266
311
  isRecord,
312
+ isTruthy,
267
313
  optionalEntry,
314
+ optionalProperty,
268
315
  rejectUnknownKeys,
269
316
  requireAllowedValue,
270
317
  requireFiniteNumber,
@@ -275,5 +322,6 @@ module.exports = {
275
322
  requireSpanId,
276
323
  requireTimestamp,
277
324
  requireTraceFlags,
278
- requireTraceId
325
+ requireTraceId,
326
+ stringOrUndefined
279
327
  };
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const path = require("node:path");
5
+
6
+ /**
7
+ * @typedef {{ readonly build?: unknown, readonly esbuild?: unknown, readonly root?: unknown }} ViteUserConfig
8
+ * @typedef {{
9
+ * build?: { rolldownOptions?: { output: { keepNames: true } }, sourcemap?: "hidden" },
10
+ * esbuild?: { keepNames: true }
11
+ * }} ViteConfigChanges
12
+ */
13
+
14
+ const ROLLDOWN_VITE_MAJOR = 8;
15
+ const UNKNOWN_VITE_MAJOR = 0;
16
+ const DECIMAL = 10;
17
+
18
+ /**
19
+ * @param {unknown} manifest
20
+ * @returns {unknown}
21
+ */
22
+ function packageVersion(manifest) {
23
+ return typeof manifest === "object" && manifest !== null ? Reflect.get(manifest, "version") : undefined;
24
+ }
25
+
26
+ /**
27
+ * @param {unknown} root
28
+ * @returns {string | undefined}
29
+ */
30
+ function lookupRoot(root) {
31
+ const hasRoot = Boolean(root);
32
+ if (!hasRoot) {
33
+ return process.cwd();
34
+ }
35
+ return typeof root === "string" ? path.resolve(root) : undefined;
36
+ }
37
+
38
+ /**
39
+ * @param {unknown} root
40
+ * @returns {number}
41
+ */
42
+ function viteMajorVersion(root) {
43
+ const lookup = lookupRoot(root);
44
+ if (lookup === undefined) {
45
+ return UNKNOWN_VITE_MAJOR;
46
+ }
47
+ try {
48
+ const manifestPath = require.resolve("vite/package.json", { paths: [lookup] });
49
+ /** @type {unknown} */
50
+ const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
51
+ const major = Number.parseInt(String(packageVersion(manifest)), DECIMAL);
52
+ return Number.isNaN(major) ? UNKNOWN_VITE_MAJOR : major;
53
+ } catch {
54
+ return UNKNOWN_VITE_MAJOR;
55
+ }
56
+ }
57
+
58
+ /**
59
+ * @param {unknown} value
60
+ * @param {string} key
61
+ * @returns {unknown}
62
+ */
63
+ function field(value, key) {
64
+ return typeof value === "object" && value !== null ? Reflect.get(value, key) : undefined;
65
+ }
66
+
67
+ /**
68
+ * @param {unknown} build
69
+ * @param {number} viteMajor
70
+ * @returns {boolean}
71
+ */
72
+ function needsRolldownKeepNames(build, viteMajor) {
73
+ const output = field(field(build, "rolldownOptions"), "output");
74
+ return viteMajor >= ROLLDOWN_VITE_MAJOR && !Array.isArray(output) && field(output, "keepNames") === undefined;
75
+ }
76
+
77
+ /**
78
+ * @param {unknown} esbuild
79
+ * @param {number} viteMajor
80
+ * @returns {boolean}
81
+ */
82
+ function needsEsbuildKeepNames(esbuild, viteMajor) {
83
+ return viteMajor < ROLLDOWN_VITE_MAJOR && esbuild !== false && field(esbuild, "keepNames") === undefined;
84
+ }
85
+
86
+ /**
87
+ * @param {boolean} sourceMap
88
+ * @param {boolean} modernNames
89
+ * @returns {ViteConfigChanges}
90
+ */
91
+ function buildChanges(sourceMap, modernNames) {
92
+ if (!sourceMap && !modernNames) {
93
+ return {};
94
+ }
95
+ return {
96
+ build: {
97
+ ...(sourceMap ? { sourcemap: "hidden" } : {}),
98
+ ...(modernNames ? { rolldownOptions: { output: { keepNames: true } } } : {})
99
+ }
100
+ };
101
+ }
102
+
103
+ /**
104
+ * @param {ViteUserConfig} config
105
+ * @param {boolean} enableSourceMaps
106
+ * @returns {ViteConfigChanges | null}
107
+ */
108
+ function viteConfigChanges(config, enableSourceMaps) {
109
+ const sourceMap = enableSourceMaps && field(config.build, "sourcemap") === undefined;
110
+ const keepNames = field(config.build, "minify") === undefined;
111
+ const viteMajor = viteMajorVersion(config.root);
112
+ const modernNames = keepNames && needsRolldownKeepNames(config.build, viteMajor);
113
+ const legacyNames = keepNames && needsEsbuildKeepNames(config.esbuild, viteMajor);
114
+ if (!sourceMap && !modernNames && !legacyNames) {
115
+ return null;
116
+ }
117
+ return { ...buildChanges(sourceMap, modernNames), ...(legacyNames ? { esbuild: { keepNames: true } } : {}) };
118
+ }
119
+
120
+ module.exports = { viteConfigChanges };