@logbrew/sdk 0.1.38 → 0.1.40

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 (91) 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.d.ts +67 -6
  51. package/react-native.js +1 -1
  52. package/recording-transport.cjs +68 -0
  53. package/release-artifacts-build.cjs +263 -84
  54. package/release-artifacts-common.js +347 -66
  55. package/release-artifacts-debug-id.js +178 -0
  56. package/release-artifacts-inspect.js +213 -0
  57. package/release-artifacts-issue-event.js +99 -0
  58. package/release-artifacts-manifest-artifact.js +107 -0
  59. package/release-artifacts-manifest.js +77 -0
  60. package/release-artifacts-mappings.js +265 -0
  61. package/release-artifacts-prepare.js +297 -0
  62. package/release-artifacts-source-context.js +210 -0
  63. package/release-artifacts-symbolication.js +272 -421
  64. package/release-artifacts-upload-files.js +122 -0
  65. package/release-artifacts-upload-request.js +246 -0
  66. package/release-artifacts-upload.js +155 -300
  67. package/release-artifacts-urls.js +49 -0
  68. package/release-artifacts.js +189 -662
  69. package/retry-delay.cjs +32 -0
  70. package/span-validation.cjs +191 -0
  71. package/stored-events.cjs +243 -0
  72. package/types/client.d.ts +67 -0
  73. package/types/delivery.d.ts +125 -0
  74. package/types/issues.d.ts +249 -0
  75. package/types/logging.d.ts +175 -0
  76. package/types/opentelemetry.d.ts +205 -0
  77. package/types/product.d.ts +128 -0
  78. package/types/recording-transport.d.ts +15 -0
  79. package/types/sdk-error.d.ts +9 -0
  80. package/types/telemetry.d.ts +171 -0
  81. package/types/trace-context.d.ts +110 -0
  82. package/types/transport-error.d.ts +10 -0
  83. package/utf8.cjs +37 -0
  84. package/validation.cjs +49 -1
  85. package/vite-release-artifacts-config.cjs +120 -0
  86. package/vite-release-artifacts.cjs +199 -99
  87. package/vite-release-artifacts.d.cts +65 -40
  88. package/vite-release-artifacts.d.ts +65 -40
  89. package/vite-release-artifacts.js +2 -2
  90. package/winston-info.cjs +205 -0
  91. package/winston.cjs +189 -289
@@ -0,0 +1,205 @@
1
+ const { compactMetadata, isMetadataValue, traceMetadataFromLogContext } = require("./log-context.cjs");
2
+ const { SdkError } = require("./sdk-error.cjs");
3
+ const { isObjectValue, isRecord, isTruthy, optionalProperty } = require("./validation.cjs");
4
+
5
+ /**
6
+ * @import { Metadata, Severity } from "./index.cjs"
7
+ * @typedef {Readonly<{ includeErrorStack?: unknown, logger?: unknown, metadata?: unknown, trace?: unknown }>} WinstonInfoOptions
8
+ * @typedef {{ message: string, level: Severity, logger?: unknown, metadata: Metadata }} WinstonLogAttributes
9
+ * @typedef {Readonly<Record<string, unknown>>} WinstonInfo
10
+ */
11
+
12
+ const WINSTON_RESERVED_FIELDS = new Set(["level", "message", "timestamp", "time", "err", "error", "stack"]);
13
+ const LINE_BREAK_PATTERN = /\r?\n/u;
14
+ const FIRST_LINE = 0;
15
+ const STACK_HEADLINE_PATTERN = /^(?<name>[A-Za-z][A-Za-z0-9_.]*(?:Error|Exception)?):\s*(?<message>.*)$/u;
16
+ /** @type {ReadonlyMap<string, Severity>} */
17
+ const LOGBREW_LEVELS = new Map([
18
+ ["warn", "warning"],
19
+ ["warning", "warning"],
20
+ ["error", "error"],
21
+ ["crit", "critical"],
22
+ ["critical", "critical"],
23
+ ["fatal", "critical"]
24
+ ]);
25
+
26
+ /**
27
+ * @param {unknown} level
28
+ * @returns {Severity}
29
+ */
30
+ function logbrewLevelFromWinstonLevel(level) {
31
+ return LOGBREW_LEVELS.get(String(level).toLowerCase()) ?? "info";
32
+ }
33
+
34
+ /**
35
+ * @param {unknown} level
36
+ * @returns {string}
37
+ */
38
+ function winstonLevelLabel(level) {
39
+ return typeof level === "string" && level.trim() !== "" ? level : "info";
40
+ }
41
+
42
+ /**
43
+ * @param {unknown} text
44
+ * @returns {text is string}
45
+ */
46
+ function isNonBlankString(text) {
47
+ return typeof text === "string" && text.trim() !== "";
48
+ }
49
+
50
+ /**
51
+ * @param {WinstonInfo} info
52
+ * @returns {string}
53
+ */
54
+ function winstonMessage(info) {
55
+ if (isNonBlankString(info["message"])) {
56
+ return info["message"];
57
+ }
58
+ const error = info["err"] ?? info["error"];
59
+ if (isObjectValue(error) && isNonBlankString(error["message"])) {
60
+ return error["message"];
61
+ }
62
+ return "winston event";
63
+ }
64
+
65
+ /**
66
+ * @param {WinstonInfo} info
67
+ * @returns {Metadata}
68
+ */
69
+ function winstonContextMetadata(info) {
70
+ /** @type {Metadata} */
71
+ const metadata = {};
72
+ for (const [key, value] of Object.entries(info)) {
73
+ if (!WINSTON_RESERVED_FIELDS.has(key) && isMetadataValue(value)) {
74
+ metadata[`context.${key}`] = value;
75
+ }
76
+ }
77
+ return metadata;
78
+ }
79
+
80
+ /**
81
+ * @param {Readonly<Error>} error
82
+ * @param {boolean} includeErrorStack
83
+ * @returns {Metadata}
84
+ */
85
+ function errorInstanceMetadata(error, includeErrorStack) {
86
+ return {
87
+ errorName: error.name || "Error",
88
+ ...(error.message ? { errorMessage: error.message } : {}),
89
+ ...(includeErrorStack && isTruthy(error.stack) ? { errorStack: error.stack } : {})
90
+ };
91
+ }
92
+
93
+ /**
94
+ * @param {Readonly<Record<string, unknown>>} error
95
+ * @param {boolean} includeErrorStack
96
+ * @returns {Metadata}
97
+ */
98
+ function errorObjectMetadata(error, includeErrorStack) {
99
+ const name = error["type"] ?? error["name"];
100
+ const { message, stack } = error;
101
+ return {
102
+ ...(isNonBlankString(name) ? { errorName: name } : {}),
103
+ ...(isNonBlankString(message) ? { errorMessage: message } : {}),
104
+ ...(includeErrorStack && isNonBlankString(stack) ? { errorStack: stack } : {})
105
+ };
106
+ }
107
+
108
+ /**
109
+ * @param {unknown} error
110
+ * @param {boolean} includeErrorStack
111
+ * @returns {Metadata}
112
+ */
113
+ function nestedErrorMetadata(error, includeErrorStack) {
114
+ if (!isTruthy(error)) {
115
+ return {};
116
+ }
117
+ if (error instanceof Error) {
118
+ return errorInstanceMetadata(error, includeErrorStack);
119
+ }
120
+ if (isObjectValue(error)) {
121
+ return errorObjectMetadata(error, includeErrorStack);
122
+ }
123
+ return isNonBlankString(error) ? { errorMessage: error } : {};
124
+ }
125
+
126
+ /**
127
+ * @param {Readonly<Metadata>} metadata
128
+ * @param {string} stack
129
+ * @param {boolean} includeErrorStack
130
+ * @returns {Metadata}
131
+ */
132
+ function stackMetadata(metadata, stack, includeErrorStack) {
133
+ const firstLine = stack.split(LINE_BREAK_PATTERN)[FIRST_LINE] ?? "";
134
+ const groups = STACK_HEADLINE_PATTERN.exec(firstLine)?.groups;
135
+ const name = groups?.["name"];
136
+ const message = groups?.["message"];
137
+ return {
138
+ ...(name !== undefined && metadata["errorName"] === undefined ? { errorName: name } : {}),
139
+ ...(message !== undefined && message !== "" && metadata["errorMessage"] === undefined ? { errorMessage: message } : {}),
140
+ ...(includeErrorStack ? { errorStack: stack } : {})
141
+ };
142
+ }
143
+
144
+ /**
145
+ * @param {Readonly<Metadata>} metadata
146
+ * @param {WinstonInfo} info
147
+ * @param {boolean} includeErrorStack
148
+ * @returns {Metadata}
149
+ */
150
+ function withWinstonErrorMetadata(metadata, info, includeErrorStack) {
151
+ const withError = { ...metadata, ...nestedErrorMetadata(info["err"] ?? info["error"], includeErrorStack) };
152
+ const { stack } = info;
153
+ return isNonBlankString(stack) ? { ...withError, ...stackMetadata(withError, stack, includeErrorStack) } : withError;
154
+ }
155
+
156
+ /**
157
+ * @param {unknown} info
158
+ * @param {WinstonInfoOptions} [options]
159
+ * @returns {WinstonLogAttributes}
160
+ */
161
+ function logAttributesFromWinstonInfo(info, options = {}) {
162
+ if (!isRecord(info)) {
163
+ throw new SdkError("validation_error", "Winston info must be an object");
164
+ }
165
+ const level = logbrewLevelFromWinstonLevel(info["level"]);
166
+ const metadata = withWinstonErrorMetadata(
167
+ {
168
+ ...compactMetadata(options.metadata),
169
+ winstonLevel: winstonLevelLabel(info["level"]),
170
+ ...winstonContextMetadata(info),
171
+ ...traceMetadataFromLogContext(options.trace)
172
+ },
173
+ info,
174
+ options.includeErrorStack === true
175
+ );
176
+ return {
177
+ message: winstonMessage(info),
178
+
179
+ level,
180
+ ...(isTruthy(options.logger) ? { logger: options.logger } : {}),
181
+ metadata
182
+ };
183
+ }
184
+
185
+ /**
186
+ * @param {unknown} info
187
+ * @param {() => unknown} fallbackTimestamp
188
+ * @returns {unknown}
189
+ */
190
+ function timestampFromWinstonInfo(info, fallbackTimestamp) {
191
+ const value = optionalProperty(info, "timestamp") ?? optionalProperty(info, "time");
192
+ if (typeof value === "number" && Number.isFinite(value)) {
193
+ return new Date(value).toISOString();
194
+ }
195
+ if (isNonBlankString(value)) {
196
+ const parsed = new Date(value);
197
+ return Number.isNaN(parsed.valueOf()) ? value : parsed.toISOString();
198
+ }
199
+ if (value instanceof Date && !Number.isNaN(value.valueOf())) {
200
+ return value.toISOString();
201
+ }
202
+ return fallbackTimestamp();
203
+ }
204
+
205
+ module.exports = { logAttributesFromWinstonInfo, timestampFromWinstonInfo, winstonMessage };