@logbrew/react-native 0.1.39 → 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 (96) hide show
  1. package/android-native-diagnostics.d.ts +15 -5
  2. package/android-native-diagnostics.js +170 -94
  3. package/apollo.cjs +435 -226
  4. package/apollo.d.cts +2 -70
  5. package/apollo.d.ts +24 -12
  6. package/apollo.js +438 -221
  7. package/apple-native-diagnostics-config.js +176 -0
  8. package/apple-native-diagnostics-context.js +232 -0
  9. package/apple-native-diagnostics-module.js +229 -0
  10. package/apple-native-diagnostics.d.ts +42 -11
  11. package/apple-native-diagnostics.js +60 -389
  12. package/expo.cjs +29 -16
  13. package/expo.d.ts +10 -9
  14. package/expo.js +4 -3
  15. package/fatal-admission.cjs +113 -0
  16. package/fatal-record.cjs +317 -0
  17. package/fatal-replay.cjs +297 -508
  18. package/global-errors-capture.cjs +369 -0
  19. package/global-errors-event.cjs +174 -0
  20. package/global-errors-support.cjs +128 -0
  21. package/global-errors.cjs +75 -351
  22. package/global-errors.d.cts +29 -278
  23. package/global-errors.d.ts +57 -27
  24. package/global-errors.js +7 -2
  25. package/global-errors.native.js +83 -48
  26. package/index-breadcrumbs.cjs +139 -0
  27. package/index-errors.cjs +167 -0
  28. package/index-events.cjs +365 -0
  29. package/index-navigation.cjs +327 -0
  30. package/index-random.cjs +120 -0
  31. package/index-react.cjs +189 -0
  32. package/index-spans.cjs +190 -0
  33. package/index-trace-fetch.cjs +322 -0
  34. package/index-trace.cjs +335 -0
  35. package/index-transport.cjs +201 -0
  36. package/index-values.cjs +232 -0
  37. package/index.cjs +95 -1243
  38. package/index.d.cts +2 -505
  39. package/index.d.ts +130 -469
  40. package/index.native.d.ts +146 -22
  41. package/index.native.js +184 -159
  42. package/instrumentation-fetch.cjs +113 -0
  43. package/instrumentation-install.cjs +241 -0
  44. package/instrumentation-runtime.cjs +65 -0
  45. package/instrumentation-teardown.cjs +93 -0
  46. package/instrumentation-values.cjs +282 -0
  47. package/instrumentation-xhr-request.cjs +298 -0
  48. package/instrumentation-xhr.cjs +233 -0
  49. package/instrumentation.cjs +110 -599
  50. package/instrumentation.d.cts +2 -84
  51. package/instrumentation.d.ts +22 -8
  52. package/ios/AppleDiagnostics/LBRNAppleNativeDiagnostics.swift +1 -1
  53. package/lifecycle.cjs +181 -78
  54. package/lifecycle.d.cts +1 -50
  55. package/lifecycle.d.ts +17 -6
  56. package/metadata.cjs +279 -114
  57. package/metro-debug-id.cjs +296 -0
  58. package/metro-serializer.cjs +219 -0
  59. package/metro.cjs +153 -297
  60. package/metro.d.cts +2 -57
  61. package/metro.d.ts +27 -13
  62. package/metro.js +2 -3
  63. package/native-bridge-async.cjs +165 -0
  64. package/native-bridge-scope.cjs +148 -0
  65. package/native-bridge.cjs +83 -186
  66. package/native-bridge.d.cts +2 -94
  67. package/native-bridge.d.ts +28 -15
  68. package/native-client-bindings.js +230 -0
  69. package/native-diagnostics-values.js +120 -0
  70. package/native-module-lookup.js +31 -0
  71. package/native-null.js +3 -0
  72. package/null-value.cjs +4 -0
  73. package/package.json +43 -1
  74. package/persistent-delivery-store.js +247 -0
  75. package/persistent-delivery.native.js +183 -261
  76. package/promise-rejection-handlers.cjs +368 -0
  77. package/promise-rejections.cjs +169 -361
  78. package/react-native.config.js +3 -1
  79. package/release-artifacts-files.cjs +94 -0
  80. package/release-artifacts-options.cjs +182 -0
  81. package/release-artifacts.cjs +278 -207
  82. package/release-artifacts.d.cts +2 -55
  83. package/release-artifacts.d.ts +18 -7
  84. package/release-artifacts.js +2 -4
  85. package/resource-fetch-graphql.cjs +265 -0
  86. package/resource-fetch-size.cjs +156 -0
  87. package/resource-fetch.cjs +227 -428
  88. package/resource-fetch.d.cts +1 -60
  89. package/resource-fetch.d.ts +17 -6
  90. package/scripts/sync-apple-native-sources.mjs +101 -53
  91. package/types/capture.d.ts +95 -0
  92. package/types/config.d.ts +85 -0
  93. package/types/defaults.d.ts +55 -0
  94. package/types/events.d.ts +203 -0
  95. package/types/provider.d.ts +81 -0
  96. package/types/trace.d.ts +104 -0
@@ -0,0 +1,201 @@
1
+ const { SdkError, TransportError } = require("@logbrew/sdk");
2
+ const { readField, textOf } = require("./index-values.cjs");
3
+
4
+ /**
5
+ * @typedef {{ readonly get: (name: string) => unknown }} HeadersLike
6
+ * @typedef {(endpoint: string, init: Readonly<Record<string, unknown>>) => unknown} FetchLike
7
+ * @typedef {{
8
+ * readonly endpoint?: unknown,
9
+ * readonly fetchImpl?: unknown,
10
+ * readonly headers?: unknown
11
+ * }} FetchTransportConfig
12
+ * @typedef {{
13
+ * statusCode: unknown,
14
+ * attempts: number,
15
+ * retryAfterMs?: number,
16
+ * errorCode?: string
17
+ * }} FetchTransportResponse
18
+ * @typedef {{ readonly send: (apiKey: string, body: string) => Promise<FetchTransportResponse> }} FetchTransport
19
+ */
20
+
21
+ const DEFAULT_ENDPOINT = "https://api.logbrew.co/v1/events";
22
+ const RATE_LIMIT_BODY_LIMIT_BYTES = 4096;
23
+ const TOO_MANY_REQUESTS = 429;
24
+ const MS_PER_SECOND = 1000;
25
+ const NO_WAIT = 0;
26
+ const SINGLE_ATTEMPT = 1;
27
+ const RETRY_AFTER_SECONDS_RE = /^\d+$/u;
28
+ const HTTP_DATE_RE = /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/u;
29
+
30
+ /**
31
+ * @param {unknown} value
32
+ * @returns {value is FetchLike}
33
+ */
34
+ function isFetchLike(value) {
35
+ return typeof value === "function";
36
+ }
37
+
38
+ /**
39
+ * @param {unknown} value
40
+ * @returns {value is HeadersLike}
41
+ */
42
+ function isHeadersLike(value) {
43
+ return typeof readField(value, "get") === "function";
44
+ }
45
+
46
+ /**
47
+ * @param {unknown} value
48
+ * @returns {value is { readonly text: () => unknown }}
49
+ */
50
+ function hasText(value) {
51
+ return typeof readField(value, "text") === "function";
52
+ }
53
+
54
+ /**
55
+ * @returns {FetchLike | undefined}
56
+ */
57
+ function defaultFetch() {
58
+ return typeof globalThis.fetch === "function" ? globalThis.fetch.bind(globalThis) : undefined;
59
+ }
60
+
61
+ /**
62
+ * @param {string} endpoint
63
+ * @returns {URL}
64
+ */
65
+ function parseEndpointUrl(endpoint) {
66
+ try {
67
+ return new URL(endpoint);
68
+ } catch {
69
+ throw new SdkError("configuration_error", "createReactNativeFetchTransport endpoint must be an absolute URL");
70
+ }
71
+ }
72
+
73
+ /**
74
+ * @param {unknown} endpoint
75
+ * @returns {asserts endpoint is string}
76
+ */
77
+ function validateDeliveryEndpoint(endpoint) {
78
+ if (typeof endpoint !== "string" || endpoint.trim() === "") {
79
+ throw new SdkError("configuration_error", "createReactNativeFetchTransport requires a non-empty endpoint");
80
+ }
81
+ const parsed = parseEndpointUrl(endpoint);
82
+ if (parsed.protocol !== "https:") {
83
+ throw new SdkError("configuration_error", "createReactNativeFetchTransport endpoint must use HTTPS");
84
+ }
85
+ if (endpoint !== `${parsed.origin}${parsed.pathname}`) {
86
+ throw new SdkError("configuration_error", "createReactNativeFetchTransport endpoint must be a plain HTTPS path");
87
+ }
88
+ }
89
+
90
+ /**
91
+ * @param {unknown} value
92
+ * @param {number} [now]
93
+ * @returns {number | undefined}
94
+ */
95
+ function retryAfterMsFromHeader(value, now = Date.now()) {
96
+ if (typeof value !== "string" || value.trim() === "") {
97
+ return undefined;
98
+ }
99
+ const trimmed = value.trim();
100
+ if (RETRY_AFTER_SECONDS_RE.test(trimmed)) {
101
+ const milliseconds = Number(trimmed) * MS_PER_SECOND;
102
+ return Number.isSafeInteger(milliseconds) ? milliseconds : undefined;
103
+ }
104
+ const timestamp = HTTP_DATE_RE.test(trimmed) ? Date.parse(trimmed) : Number.NaN;
105
+ return Number.isFinite(timestamp) ? Math.max(NO_WAIT, timestamp - now) : undefined;
106
+ }
107
+
108
+ /**
109
+ * @param {unknown} headers
110
+ * @returns {number | undefined}
111
+ */
112
+ function retryAfterMsFromHeaders(headers) {
113
+ if (!isHeadersLike(headers)) {
114
+ return undefined;
115
+ }
116
+ return retryAfterMsFromHeader(headers.get("retry-after"));
117
+ }
118
+
119
+ /**
120
+ * @param {unknown} response
121
+ * @returns {number}
122
+ */
123
+ function declaredContentLength(response) {
124
+ const headers = readField(response, "headers");
125
+ return Number(isHeadersLike(headers) ? headers.get("content-length") : undefined);
126
+ }
127
+
128
+ /**
129
+ * @param {unknown} text
130
+ * @returns {unknown}
131
+ */
132
+ function rateLimitBodyCode(text) {
133
+ const textLength = typeof text === "string" ? text.length : readField(text, "length");
134
+ /** @type {unknown} */
135
+ const parsed = Number(textLength) <= RATE_LIMIT_BODY_LIMIT_BYTES ? JSON.parse(textOf(text)) : undefined;
136
+ return readField(parsed, "code");
137
+ }
138
+
139
+ /**
140
+ * @param {unknown} response
141
+ * @returns {Promise<string | undefined>}
142
+ */
143
+ async function rateLimitCode(response) {
144
+ try {
145
+ const length = declaredContentLength(response);
146
+ if (!Number.isSafeInteger(length) || length > RATE_LIMIT_BODY_LIMIT_BYTES || !hasText(response)) {
147
+ return undefined;
148
+ }
149
+ const code = rateLimitBodyCode(await response.text());
150
+ return code === "rate_limited" || code === "usage_limit_exceeded" ? code : undefined;
151
+ } catch {
152
+ return undefined;
153
+ }
154
+ }
155
+
156
+ /**
157
+ * @param {FetchTransportConfig} [config]
158
+ * @returns {FetchTransport}
159
+ */
160
+ function createReactNativeFetchTransport({
161
+ endpoint = DEFAULT_ENDPOINT,
162
+ fetchImpl = defaultFetch(),
163
+ headers = {}
164
+ } = {}) {
165
+ validateDeliveryEndpoint(endpoint);
166
+ if (!isFetchLike(fetchImpl)) {
167
+ throw new SdkError("configuration_error", "createReactNativeFetchTransport requires fetch");
168
+ }
169
+ if (headers === null || Array.isArray(headers) || typeof headers !== "object") {
170
+ throw new SdkError("configuration_error", "createReactNativeFetchTransport headers must be an object");
171
+ }
172
+
173
+ return Object.freeze({
174
+ async send(apiKey, body) {
175
+ try {
176
+ const response = await fetchImpl(endpoint, {
177
+ body,
178
+ headers: {
179
+ ...headers,
180
+ authorization: `Bearer ${apiKey}`,
181
+ "content-type": "application/json"
182
+ },
183
+ method: "POST"
184
+ });
185
+ const retryAfterMs = retryAfterMsFromHeaders(readField(response, "headers"));
186
+ const errorCode = readField(response, "status") === TOO_MANY_REQUESTS ? await rateLimitCode(response) : undefined;
187
+ return {
188
+ statusCode: readField(response, "status"),
189
+
190
+ attempts: SINGLE_ATTEMPT,
191
+ ...(retryAfterMs === undefined ? {} : { retryAfterMs }),
192
+ ...(errorCode === undefined ? {} : { errorCode })
193
+ };
194
+ } catch {
195
+ throw TransportError.network("LogBrew React Native delivery failed");
196
+ }
197
+ }
198
+ });
199
+ }
200
+
201
+ module.exports = { createReactNativeFetchTransport, defaultFetch };
@@ -0,0 +1,232 @@
1
+ const { SdkError } = require("@logbrew/sdk");
2
+ const { NULL_VALUE } = require("./null-value.cjs");
3
+
4
+ /**
5
+ * @import { Metadata, MetadataValue } from "@logbrew/sdk"
6
+ * @typedef {Readonly<Record<string, unknown>>} LooseRecord
7
+ * @typedef {{
8
+ * readonly OS?: unknown,
9
+ * readonly Version?: unknown,
10
+ * readonly isPad?: unknown,
11
+ * readonly constants?: { readonly isTesting?: unknown } | null | undefined
12
+ * }} PlatformLike
13
+ * @typedef {{
14
+ * readonly currentState?: unknown,
15
+ * readonly addEventListener?: unknown
16
+ * }} AppStateLike
17
+ * @typedef {{
18
+ * readonly traceId: string,
19
+ * readonly spanId: string,
20
+ * readonly parentSpanId: string | undefined,
21
+ * readonly traceFlags: string,
22
+ * readonly sampled: boolean
23
+ * }} TraceContext
24
+ * @typedef {{
25
+ * readonly traceId?: unknown,
26
+ * readonly spanId?: unknown,
27
+ * readonly parentSpanId?: unknown,
28
+ * readonly traceFlags?: unknown,
29
+ * readonly sampled?: unknown
30
+ * }} TraceObjectInput
31
+ * @typedef {TraceObjectInput | string | null | undefined} TraceInput
32
+ * @typedef {{
33
+ * readonly platform?: PlatformLike | null | undefined,
34
+ * readonly appState?: AppStateLike | null | undefined,
35
+ * readonly metadata?: LooseRecord | undefined
36
+ * }} ContextOptions
37
+ * @typedef {(id: string, timestamp: string, attributes: LooseRecord) => unknown} ClientEventMethod
38
+ * @typedef {{
39
+ * readonly action: ClientEventMethod,
40
+ * readonly span: ClientEventMethod,
41
+ * readonly issue: ClientEventMethod,
42
+ * readonly log: ClientEventMethod,
43
+ * readonly release: ClientEventMethod,
44
+ * readonly environment: ClientEventMethod,
45
+ * readonly flush: () => Promise<unknown>,
46
+ * readonly shutdown: () => Promise<unknown>,
47
+ * readonly deliveryHealth: () => unknown,
48
+ * readonly droppedEvents: () => unknown,
49
+ * readonly pendingBytes: () => unknown,
50
+ * readonly previewJson: () => unknown,
51
+ * readonly pendingEvents: () => unknown,
52
+ * readonly addBreadcrumb?: unknown
53
+ * }} ClientLike
54
+ * @typedef {(() => void) | { readonly remove?: unknown } | null | undefined} SubscriptionLike
55
+ */
56
+
57
+ const FIRST = 0;
58
+ const LIMIT_ONE = 1;
59
+ const MIN_FAILURE_STATUS = 400;
60
+
61
+ /**
62
+ * @param {unknown} value
63
+ * @returns {value is null | undefined}
64
+ */
65
+ function isNullish(value) {
66
+ return value === undefined || value === null;
67
+ }
68
+
69
+ /**
70
+ * @param {unknown} value
71
+ * @returns {string}
72
+ */
73
+ function textOf(value) {
74
+ return typeof value === "string" ? value : String(value);
75
+ }
76
+
77
+ /**
78
+ * @param {unknown} value
79
+ * @returns {value is MetadataValue}
80
+ */
81
+ function isMetadataValue(value) {
82
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null;
83
+ }
84
+
85
+ /**
86
+ * @param {unknown} value
87
+ * @param {PropertyKey} key
88
+ * @returns {unknown}
89
+ */
90
+ function readField(value, key) {
91
+ return (typeof value === "object" && value !== null) || typeof value === "function" ? Reflect.get(value, key) : undefined;
92
+ }
93
+
94
+ /**
95
+ * @param {unknown} client
96
+ * @returns {void}
97
+ */
98
+ function requireClient(client) {
99
+ const present = Boolean(client);
100
+ if (!present) {
101
+ throw new SdkError("configuration_error", "LogBrew React Native helpers require a client");
102
+ }
103
+ }
104
+
105
+ /**
106
+ * @param {string} label
107
+ * @param {unknown} value
108
+ * @returns {void}
109
+ */
110
+ function requireNonEmpty(label, value) {
111
+ if (typeof value !== "string" || value.trim() === "") {
112
+ throw new SdkError("validation_error", `${label} must be non-empty`);
113
+ }
114
+ }
115
+
116
+ /**
117
+ * @param {unknown} value
118
+ * @returns {MetadataValue}
119
+ */
120
+ function normalizeMetadataValue(value) {
121
+ if (value === undefined) {
122
+ return NULL_VALUE;
123
+ }
124
+ return isMetadataValue(value) ? value : textOf(value);
125
+ }
126
+
127
+ /**
128
+ * @param {LooseRecord} metadata
129
+ * @returns {Metadata}
130
+ */
131
+ function compactMetadata(metadata) {
132
+ /** @type {Metadata} */
133
+ const compacted = {};
134
+ for (const [key, value] of Object.entries(metadata)) {
135
+ if (isMetadataValue(value)) {
136
+ compacted[key] = value;
137
+ }
138
+ }
139
+ return compacted;
140
+ }
141
+
142
+ /**
143
+ * @param {unknown} value
144
+ * @returns {string | undefined}
145
+ */
146
+ function stripQueryAndHash(value) {
147
+ return isNullish(value) ? undefined : textOf(value).split(/[?#]/u, LIMIT_ONE)[FIRST];
148
+ }
149
+
150
+ /**
151
+ * @param {unknown} statusCode
152
+ * @returns {"failure" | "success"}
153
+ */
154
+ function statusFromStatusCode(statusCode) {
155
+ if (typeof statusCode === "number" && Number.isFinite(statusCode) && statusCode >= MIN_FAILURE_STATUS) {
156
+ return "failure";
157
+ }
158
+ return "success";
159
+ }
160
+
161
+ /**
162
+ * @param {unknown} statusCode
163
+ * @returns {"error" | "ok"}
164
+ */
165
+ function spanStatusFromStatusCode(statusCode) {
166
+ if (typeof statusCode === "number" && Number.isFinite(statusCode) && statusCode >= MIN_FAILURE_STATUS) {
167
+ return "error";
168
+ }
169
+ return "ok";
170
+ }
171
+
172
+ /**
173
+ * @param {ContextOptions} [options]
174
+ * @returns {Record<string, unknown>}
175
+ */
176
+ function getReactNativeContext({ platform, appState, metadata = {} } = {}) {
177
+ return {
178
+ platform: platform?.OS ?? "unknown",
179
+ platformVersion: normalizeMetadataValue(platform?.Version),
180
+
181
+ appState: normalizeMetadataValue(appState?.currentState),
182
+ isPad: normalizeMetadataValue(platform?.isPad),
183
+ isTesting: normalizeMetadataValue(platform?.constants?.isTesting),
184
+ ...metadata
185
+ };
186
+ }
187
+
188
+ /**
189
+ * @param {unknown} value
190
+ * @returns {value is () => unknown}
191
+ */
192
+ function isRemover(value) {
193
+ return typeof value === "function";
194
+ }
195
+
196
+ /**
197
+ * @param {unknown} value
198
+ * @returns {value is { readonly remove: () => unknown }}
199
+ */
200
+ function hasRemove(value) {
201
+ return typeof readField(value, "remove") === "function";
202
+ }
203
+
204
+ /**
205
+ * @param {unknown} subscription
206
+ * @returns {() => unknown}
207
+ */
208
+ function subscriptionRemover(subscription) {
209
+ if (isRemover(subscription)) {
210
+ return subscription;
211
+ }
212
+ if (hasRemove(subscription)) {
213
+ return () => subscription.remove();
214
+ }
215
+ return () => undefined;
216
+ }
217
+
218
+ module.exports = {
219
+ compactMetadata,
220
+ getReactNativeContext,
221
+ isMetadataValue,
222
+ isNullish,
223
+ normalizeMetadataValue,
224
+ readField,
225
+ requireClient,
226
+ requireNonEmpty,
227
+ spanStatusFromStatusCode,
228
+ statusFromStatusCode,
229
+ stripQueryAndHash,
230
+ subscriptionRemover,
231
+ textOf
232
+ };