@logbrew/react-native 0.1.39 → 0.1.41
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/README.md +3 -3
- package/android-native-diagnostics.d.ts +15 -5
- package/android-native-diagnostics.js +170 -94
- package/apollo.cjs +435 -226
- package/apollo.d.cts +2 -70
- package/apollo.d.ts +24 -12
- package/apollo.js +438 -221
- package/apple-native-diagnostics-config.js +176 -0
- package/apple-native-diagnostics-context.js +232 -0
- package/apple-native-diagnostics-module.js +229 -0
- package/apple-native-diagnostics.d.ts +42 -11
- package/apple-native-diagnostics.js +60 -389
- package/expo.cjs +29 -16
- package/expo.d.ts +10 -9
- package/expo.js +4 -3
- package/fatal-admission.cjs +113 -0
- package/fatal-record.cjs +317 -0
- package/fatal-replay.cjs +297 -508
- package/global-errors-capture.cjs +369 -0
- package/global-errors-event.cjs +174 -0
- package/global-errors-support.cjs +128 -0
- package/global-errors.cjs +75 -351
- package/global-errors.d.cts +29 -278
- package/global-errors.d.ts +57 -27
- package/global-errors.js +7 -2
- package/global-errors.native.js +83 -48
- package/index-breadcrumbs.cjs +139 -0
- package/index-errors.cjs +167 -0
- package/index-events.cjs +365 -0
- package/index-navigation.cjs +327 -0
- package/index-random.cjs +120 -0
- package/index-react.cjs +189 -0
- package/index-spans.cjs +190 -0
- package/index-trace-fetch.cjs +322 -0
- package/index-trace.cjs +335 -0
- package/index-transport.cjs +201 -0
- package/index-values.cjs +232 -0
- package/index.cjs +97 -1245
- package/index.d.cts +2 -505
- package/index.d.ts +130 -469
- package/index.native.d.ts +146 -22
- package/index.native.js +184 -159
- package/instrumentation-fetch.cjs +113 -0
- package/instrumentation-install.cjs +241 -0
- package/instrumentation-runtime.cjs +65 -0
- package/instrumentation-teardown.cjs +93 -0
- package/instrumentation-values.cjs +282 -0
- package/instrumentation-xhr-request.cjs +298 -0
- package/instrumentation-xhr.cjs +233 -0
- package/instrumentation.cjs +110 -599
- package/instrumentation.d.cts +2 -84
- package/instrumentation.d.ts +22 -8
- package/ios/AppleDiagnostics/LBRNAppleNativeDiagnostics.swift +1 -1
- package/ios/GeneratedAppleDiagnostics/LogBrew/DeliveryLifecycle.swift +1 -1
- package/ios/GeneratedAppleDiagnostics/SOURCE-MANIFEST.json +1 -1
- package/lifecycle.cjs +181 -78
- package/lifecycle.d.cts +1 -50
- package/lifecycle.d.ts +17 -6
- package/metadata.cjs +279 -114
- package/metro-debug-id.cjs +296 -0
- package/metro-serializer.cjs +219 -0
- package/metro.cjs +153 -297
- package/metro.d.cts +2 -57
- package/metro.d.ts +27 -13
- package/metro.js +2 -3
- package/native-bridge-async.cjs +165 -0
- package/native-bridge-scope.cjs +148 -0
- package/native-bridge.cjs +83 -186
- package/native-bridge.d.cts +2 -94
- package/native-bridge.d.ts +28 -15
- package/native-client-bindings.js +230 -0
- package/native-diagnostics-values.js +120 -0
- package/native-module-lookup.js +31 -0
- package/native-null.js +3 -0
- package/null-value.cjs +4 -0
- package/package.json +43 -1
- package/persistent-delivery-store.js +247 -0
- package/persistent-delivery.native.js +183 -261
- package/promise-rejection-handlers.cjs +368 -0
- package/promise-rejections.cjs +169 -361
- package/react-native.config.js +3 -1
- package/release-artifacts-files.cjs +94 -0
- package/release-artifacts-options.cjs +182 -0
- package/release-artifacts.cjs +278 -207
- package/release-artifacts.d.cts +2 -55
- package/release-artifacts.d.ts +18 -7
- package/release-artifacts.js +2 -4
- package/resource-fetch-graphql.cjs +265 -0
- package/resource-fetch-size.cjs +156 -0
- package/resource-fetch.cjs +227 -428
- package/resource-fetch.d.cts +1 -60
- package/resource-fetch.d.ts +17 -6
- package/scripts/sync-apple-native-sources.mjs +101 -53
- package/types/capture.d.ts +95 -0
- package/types/config.d.ts +85 -0
- package/types/defaults.d.ts +55 -0
- package/types/events.d.ts +203 -0
- package/types/provider.d.ts +81 -0
- package/types/trace.d.ts +104 -0
package/release-artifacts.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
type LogBrewReactNativeReleaseArtifactPlatform = "android" | "ios";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type LogBrewReactNativeReleaseArtifactsOptions = {
|
|
4
4
|
bundle: string;
|
|
5
5
|
platform: LogBrewReactNativeReleaseArtifactPlatform;
|
|
6
6
|
release: string;
|
|
@@ -18,7 +18,7 @@ export type LogBrewReactNativeReleaseArtifactsOptions = {
|
|
|
18
18
|
stripSourcePrefix?: string[];
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
type LogBrewReactNativeReleaseArtifactsResult = {
|
|
22
22
|
buildDir: string;
|
|
23
23
|
bundlePath: string;
|
|
24
24
|
sourcemapPath: string;
|
|
@@ -28,7 +28,7 @@ export type LogBrewReactNativeReleaseArtifactsResult = {
|
|
|
28
28
|
manifestReport: Record<string, unknown>;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
type LogBrewReactNativeReleaseArtifactUploadOptions = LogBrewReactNativeReleaseArtifactsOptions & {
|
|
32
32
|
endpoint: string;
|
|
33
33
|
allowHostedUpload?: boolean;
|
|
34
34
|
tokenEnv?: string;
|
|
@@ -38,16 +38,27 @@ export type LogBrewReactNativeReleaseArtifactUploadOptions = LogBrewReactNativeR
|
|
|
38
38
|
timeout?: string | number;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
type LogBrewReactNativeReleaseArtifactUploadResult = LogBrewReactNativeReleaseArtifactsResult & {
|
|
42
42
|
uploadReport: Record<string, unknown>;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
declare function prepareLogBrewReactNativeReleaseArtifacts(
|
|
46
46
|
options: LogBrewReactNativeReleaseArtifactsOptions
|
|
47
47
|
): LogBrewReactNativeReleaseArtifactsResult;
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
declare function uploadLogBrewReactNativeReleaseArtifacts(
|
|
50
50
|
options: LogBrewReactNativeReleaseArtifactUploadOptions
|
|
51
51
|
): LogBrewReactNativeReleaseArtifactUploadResult;
|
|
52
52
|
|
|
53
|
+
export type {
|
|
54
|
+
LogBrewReactNativeReleaseArtifactPlatform,
|
|
55
|
+
LogBrewReactNativeReleaseArtifactUploadOptions,
|
|
56
|
+
LogBrewReactNativeReleaseArtifactUploadResult,
|
|
57
|
+
LogBrewReactNativeReleaseArtifactsOptions,
|
|
58
|
+
LogBrewReactNativeReleaseArtifactsResult
|
|
59
|
+
};
|
|
60
|
+
export {
|
|
61
|
+
prepareLogBrewReactNativeReleaseArtifacts,
|
|
62
|
+
uploadLogBrewReactNativeReleaseArtifacts
|
|
63
|
+
};
|
|
53
64
|
export default prepareLogBrewReactNativeReleaseArtifacts;
|
package/release-artifacts.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import releaseArtifacts from "./release-artifacts.cjs";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
releaseArtifacts.prepareLogBrewReactNativeReleaseArtifacts;
|
|
5
|
-
export const uploadLogBrewReactNativeReleaseArtifacts =
|
|
6
|
-
releaseArtifacts.uploadLogBrewReactNativeReleaseArtifacts;
|
|
3
|
+
const { prepareLogBrewReactNativeReleaseArtifacts, uploadLogBrewReactNativeReleaseArtifacts } = releaseArtifacts;
|
|
7
4
|
|
|
5
|
+
export { prepareLogBrewReactNativeReleaseArtifacts, uploadLogBrewReactNativeReleaseArtifacts };
|
|
8
6
|
export default prepareLogBrewReactNativeReleaseArtifacts;
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { SdkError } = require("@logbrew/sdk");
|
|
4
|
+
const {
|
|
5
|
+
isCallable,
|
|
6
|
+
metadataRuntime,
|
|
7
|
+
parseUrl,
|
|
8
|
+
readField,
|
|
9
|
+
stringValue,
|
|
10
|
+
stripQueryAndHash
|
|
11
|
+
} = require("./instrumentation-values.cjs");
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @import { Metadata } from "@logbrew/sdk"
|
|
15
|
+
* @import { Callable } from "./instrumentation-values.cjs"
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {string | RegExp | Callable} EndpointMatcher
|
|
20
|
+
* @typedef {{ readonly operationName?: string | undefined, readonly operationType?: string | undefined }} OperationDetails
|
|
21
|
+
* @typedef {{ readonly endpoint?: unknown, readonly metadataFactory?: unknown }} GraphQLMetadataFactoryOptions
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const MAX_GRAPHQL_BODY_CHARS = 16_384;
|
|
25
|
+
const MAX_GRAPHQL_OPERATION_NAME_CHARS = 128;
|
|
26
|
+
const EMPTY_LENGTH = 0;
|
|
27
|
+
const FIRST_MATCH = 0;
|
|
28
|
+
const LAST_CANDIDATE = -1;
|
|
29
|
+
const GRAPHQL_OPERATION_NAME_RE = /^[_A-Za-z][_0-9A-Za-z]*$/u;
|
|
30
|
+
const GRAPHQL_OPERATION_RE = /^(?<type>query|mutation|subscription)\b(?:\s+(?<name>[_A-Za-z][_0-9A-Za-z]*))?/u;
|
|
31
|
+
const LEADING_GRAPHQL_COMMENTS_RE = /^(?:#[^\n\r]*(?:\r?\n|$)\s*)+/u;
|
|
32
|
+
const ABSOLUTE_HTTP_URL_RE = /^https?:\/\//iu;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {unknown} value
|
|
36
|
+
* @returns {string[]}
|
|
37
|
+
*/
|
|
38
|
+
function endpointCandidatesFromUrl(value) {
|
|
39
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
const parsedUrl = parseUrl(value);
|
|
43
|
+
if (parsedUrl === undefined) {
|
|
44
|
+
return [stripQueryAndHash(value)];
|
|
45
|
+
}
|
|
46
|
+
const candidates = [parsedUrl.pathname];
|
|
47
|
+
if (ABSOLUTE_HTTP_URL_RE.test(value)) {
|
|
48
|
+
candidates.push(`${parsedUrl.origin}${parsedUrl.pathname}`);
|
|
49
|
+
}
|
|
50
|
+
return candidates;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {string} endpoint
|
|
55
|
+
* @returns {string}
|
|
56
|
+
*/
|
|
57
|
+
function normalizeEndpointString(endpoint) {
|
|
58
|
+
const value = endpoint.trim();
|
|
59
|
+
const candidates = endpointCandidatesFromUrl(value);
|
|
60
|
+
return candidates.at(LAST_CANDIDATE) ?? stripQueryAndHash(value);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {unknown} candidate
|
|
65
|
+
* @returns {EndpointMatcher}
|
|
66
|
+
*/
|
|
67
|
+
function normalizeGraphQLEndpointMatcher(candidate) {
|
|
68
|
+
const text = stringValue(candidate);
|
|
69
|
+
if (text !== undefined) {
|
|
70
|
+
const value = normalizeEndpointString(text);
|
|
71
|
+
if (value === "") {
|
|
72
|
+
throw new SdkError("configuration_error", "GraphQL endpoint strings must not be empty");
|
|
73
|
+
}
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
if (candidate instanceof RegExp || isCallable(candidate)) {
|
|
77
|
+
return candidate;
|
|
78
|
+
}
|
|
79
|
+
throw new SdkError("configuration_error", "GraphQL endpoint must be a string, RegExp, function, or array");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {unknown} endpoint
|
|
84
|
+
* @returns {EndpointMatcher[]}
|
|
85
|
+
*/
|
|
86
|
+
function normalizeGraphQLEndpointMatchers(endpoint) {
|
|
87
|
+
if (endpoint === undefined) {
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
const endpoints = Array.isArray(endpoint) ? endpoint : [endpoint];
|
|
91
|
+
return endpoints.map(candidate => normalizeGraphQLEndpointMatcher(candidate));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {RegExp} matcher
|
|
96
|
+
* @param {string} candidate
|
|
97
|
+
* @returns {boolean}
|
|
98
|
+
*/
|
|
99
|
+
function endpointRegExpMatches(matcher, candidate) {
|
|
100
|
+
matcher.lastIndex = FIRST_MATCH;
|
|
101
|
+
const matched = matcher.test(candidate);
|
|
102
|
+
matcher.lastIndex = FIRST_MATCH;
|
|
103
|
+
return matched;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @param {unknown} context
|
|
108
|
+
* @returns {string[]}
|
|
109
|
+
*/
|
|
110
|
+
function graphqlEndpointCandidates(context) {
|
|
111
|
+
/** @type {Set<string>} */
|
|
112
|
+
const candidates = new Set();
|
|
113
|
+
const routeTemplate = readField(context, "routeTemplate");
|
|
114
|
+
if (typeof routeTemplate === "string" && routeTemplate.trim() !== "") {
|
|
115
|
+
candidates.add(routeTemplate);
|
|
116
|
+
}
|
|
117
|
+
for (const candidate of endpointCandidatesFromUrl(readField(context, "url"))) {
|
|
118
|
+
candidates.add(candidate);
|
|
119
|
+
}
|
|
120
|
+
return [...candidates];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @param {unknown} context
|
|
125
|
+
* @param {readonly EndpointMatcher[]} endpointMatchers
|
|
126
|
+
* @returns {boolean}
|
|
127
|
+
*/
|
|
128
|
+
function matchesGraphQLEndpoint(context, endpointMatchers) {
|
|
129
|
+
const candidates = graphqlEndpointCandidates(context);
|
|
130
|
+
return endpointMatchers.some(matcher => {
|
|
131
|
+
if (typeof matcher === "string") {
|
|
132
|
+
return candidates.includes(matcher);
|
|
133
|
+
}
|
|
134
|
+
if (matcher instanceof RegExp) {
|
|
135
|
+
return candidates.some(candidate => endpointRegExpMatches(matcher, candidate));
|
|
136
|
+
}
|
|
137
|
+
return matcher(context) === true;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @param {unknown} context
|
|
143
|
+
* @returns {string | undefined}
|
|
144
|
+
*/
|
|
145
|
+
function requestBody(context) {
|
|
146
|
+
const body = readField(readField(context, "init"), "body") ?? readField(readField(context, "input"), "body");
|
|
147
|
+
return stringValue(body);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @param {string | undefined} body
|
|
152
|
+
* @returns {unknown}
|
|
153
|
+
*/
|
|
154
|
+
function graphqlPayloadFromBody(body) {
|
|
155
|
+
if (typeof body !== "string" || body.length > MAX_GRAPHQL_BODY_CHARS) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
/** @type {unknown} */
|
|
160
|
+
const payload = JSON.parse(body);
|
|
161
|
+
return payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : undefined;
|
|
162
|
+
} catch {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @param {unknown} value
|
|
169
|
+
* @returns {string | undefined}
|
|
170
|
+
*/
|
|
171
|
+
function safeGraphqlOperationName(value) {
|
|
172
|
+
if (typeof value !== "string") {
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
const name = value.trim();
|
|
176
|
+
if (
|
|
177
|
+
name.length === EMPTY_LENGTH ||
|
|
178
|
+
name.length > MAX_GRAPHQL_OPERATION_NAME_CHARS ||
|
|
179
|
+
!GRAPHQL_OPERATION_NAME_RE.test(name)
|
|
180
|
+
) {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
return name;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @param {unknown} query
|
|
188
|
+
* @returns {OperationDetails}
|
|
189
|
+
*/
|
|
190
|
+
function graphqlOperationDetails(query) {
|
|
191
|
+
if (typeof query !== "string" || query.length > MAX_GRAPHQL_BODY_CHARS) {
|
|
192
|
+
return {};
|
|
193
|
+
}
|
|
194
|
+
const source = query.trimStart();
|
|
195
|
+
if (source.startsWith("{")) {
|
|
196
|
+
return { operationType: "query" };
|
|
197
|
+
}
|
|
198
|
+
const match = GRAPHQL_OPERATION_RE.exec(source.replace(LEADING_GRAPHQL_COMMENTS_RE, "").trimStart());
|
|
199
|
+
if (!match) {
|
|
200
|
+
return {};
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
operationName: safeGraphqlOperationName(match.groups?.["name"]),
|
|
204
|
+
operationType: match.groups?.["type"]
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* @param {string | undefined} operationName
|
|
210
|
+
* @param {string | undefined} operationType
|
|
211
|
+
* @returns {Metadata}
|
|
212
|
+
*/
|
|
213
|
+
function operationMetadata(operationName, operationType) {
|
|
214
|
+
/** @type {Metadata} */
|
|
215
|
+
const metadata = {};
|
|
216
|
+
if (operationName !== undefined) {
|
|
217
|
+
metadata["graphqlOperationName"] = operationName;
|
|
218
|
+
}
|
|
219
|
+
if (operationType !== undefined) {
|
|
220
|
+
metadata["graphqlOperationType"] = operationType;
|
|
221
|
+
}
|
|
222
|
+
return metadata;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @param {unknown} context
|
|
227
|
+
* @returns {Metadata}
|
|
228
|
+
*/
|
|
229
|
+
function graphqlMetadataFromContext(context) {
|
|
230
|
+
const payload = graphqlPayloadFromBody(requestBody(context));
|
|
231
|
+
if (payload === undefined) {
|
|
232
|
+
return {};
|
|
233
|
+
}
|
|
234
|
+
const operation = graphqlOperationDetails(readField(payload, "query"));
|
|
235
|
+
return operationMetadata(
|
|
236
|
+
safeGraphqlOperationName(readField(payload, "operationName")) ?? operation.operationName,
|
|
237
|
+
operation.operationType
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @param {GraphQLMetadataFactoryOptions} [options]
|
|
243
|
+
* @returns {(context: unknown) => Metadata}
|
|
244
|
+
*/
|
|
245
|
+
function createReactNativeGraphQLMetadataFactory({
|
|
246
|
+
endpoint,
|
|
247
|
+
metadataFactory
|
|
248
|
+
} = {}) {
|
|
249
|
+
const endpointMatchers = normalizeGraphQLEndpointMatchers(endpoint);
|
|
250
|
+
if (metadataFactory !== undefined && typeof metadataFactory !== "function") {
|
|
251
|
+
throw new SdkError("configuration_error", "metadataFactory must be a function");
|
|
252
|
+
}
|
|
253
|
+
return function logBrewReactNativeGraphQLMetadata(context) {
|
|
254
|
+
const safeMetadata = metadataRuntime.safeReactNativeMetadataFactoryResult(isCallable(metadataFactory) ? metadataFactory(context) : undefined);
|
|
255
|
+
if (endpointMatchers.length > EMPTY_LENGTH && !matchesGraphQLEndpoint(context, endpointMatchers)) {
|
|
256
|
+
return safeMetadata;
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
...safeMetadata,
|
|
260
|
+
...graphqlMetadataFromContext(context)
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
module.exports = { createReactNativeGraphQLMetadataFactory };
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { isCallable, readField, stringValue, toText, utf8ByteLength } = require("./instrumentation-values.cjs");
|
|
4
|
+
|
|
5
|
+
const MIN_BYTES = 0;
|
|
6
|
+
const DECIMAL = 10;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {unknown} value
|
|
10
|
+
* @returns {value is number}
|
|
11
|
+
*/
|
|
12
|
+
function isByteCount(value) {
|
|
13
|
+
return typeof value === "number" && Number.isFinite(value) && value >= MIN_BYTES;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {unknown} target
|
|
18
|
+
* @param {string} name
|
|
19
|
+
* @returns {Promise<unknown>}
|
|
20
|
+
*/
|
|
21
|
+
async function callMethod(target, name) {
|
|
22
|
+
const method = readField(target, name);
|
|
23
|
+
return isCallable(method) ? Reflect.apply(method, target, []) : undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {unknown} response
|
|
28
|
+
* @param {string} name
|
|
29
|
+
* @returns {unknown}
|
|
30
|
+
*/
|
|
31
|
+
function responseHeader(response, name) {
|
|
32
|
+
const headers = readField(response, "headers");
|
|
33
|
+
const get = readField(headers, "get");
|
|
34
|
+
if (!isCallable(get)) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
return Reflect.apply(get, headers, [name]) ?? Reflect.apply(get, headers, [name.toLowerCase()]) ?? undefined;
|
|
39
|
+
} catch {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {unknown} response
|
|
46
|
+
* @returns {number | undefined}
|
|
47
|
+
*/
|
|
48
|
+
function responseContentLengthBytes(response) {
|
|
49
|
+
const header = responseHeader(response, "Content-Length");
|
|
50
|
+
const value = Number.parseInt(toText(header ?? ""), DECIMAL);
|
|
51
|
+
return Number.isFinite(value) && value >= MIN_BYTES ? value : undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {unknown} response
|
|
56
|
+
* @returns {Promise<number | undefined>}
|
|
57
|
+
*/
|
|
58
|
+
async function responseArrayBufferSizeBytes(response) {
|
|
59
|
+
if (!isCallable(readField(response, "arrayBuffer"))) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const byteLength = readField(await callMethod(response, "arrayBuffer"), "byteLength");
|
|
64
|
+
return isByteCount(byteLength) ? byteLength : undefined;
|
|
65
|
+
} catch {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {unknown} response
|
|
72
|
+
* @returns {Promise<number | undefined>}
|
|
73
|
+
*/
|
|
74
|
+
async function responseBlobSizeBytes(response) {
|
|
75
|
+
if (!isCallable(readField(response, "blob"))) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const size = readField(await callMethod(response, "blob"), "size");
|
|
80
|
+
return isByteCount(size) ? size : undefined;
|
|
81
|
+
} catch {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {unknown} response
|
|
88
|
+
* @returns {Promise<number | undefined>}
|
|
89
|
+
*/
|
|
90
|
+
async function responseTextSizeBytes(response) {
|
|
91
|
+
if (!isCallable(readField(response, "text"))) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const text = stringValue(await callMethod(response, "text"));
|
|
96
|
+
return text === undefined ? undefined : utf8ByteLength(text);
|
|
97
|
+
} catch {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @param {unknown} response
|
|
104
|
+
* @returns {Promise<number | undefined>}
|
|
105
|
+
*/
|
|
106
|
+
async function responseBodySizeBytes(response) {
|
|
107
|
+
const arrayBufferSize = await responseArrayBufferSizeBytes(response);
|
|
108
|
+
if (arrayBufferSize !== undefined) {
|
|
109
|
+
return arrayBufferSize;
|
|
110
|
+
}
|
|
111
|
+
const blobSize = await responseBlobSizeBytes(response);
|
|
112
|
+
if (blobSize !== undefined) {
|
|
113
|
+
return blobSize;
|
|
114
|
+
}
|
|
115
|
+
return responseTextSizeBytes(response);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @param {unknown} response
|
|
120
|
+
* @param {(...args: readonly unknown[]) => unknown} clone
|
|
121
|
+
* @returns {unknown}
|
|
122
|
+
*/
|
|
123
|
+
function cloneResponse(response, clone) {
|
|
124
|
+
try {
|
|
125
|
+
return Reflect.apply(clone, response, []);
|
|
126
|
+
} catch {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @param {unknown} response
|
|
133
|
+
* @returns {Promise<number | undefined>}
|
|
134
|
+
*/
|
|
135
|
+
async function clonedResponseBodySizeBytes(response) {
|
|
136
|
+
if (!isCallable(readField(response, "clone"))) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
const clone = readField(response, "clone");
|
|
140
|
+
return isCallable(clone) ? responseBodySizeBytes(cloneResponse(response, clone)) : undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {unknown} response
|
|
145
|
+
* @param {boolean} measureResponseBodySize
|
|
146
|
+
* @returns {Promise<number | undefined>}
|
|
147
|
+
*/
|
|
148
|
+
async function responseSizeBytesFromResponse(response, measureResponseBodySize) {
|
|
149
|
+
const contentLength = responseContentLengthBytes(response);
|
|
150
|
+
if (contentLength !== undefined) {
|
|
151
|
+
return contentLength;
|
|
152
|
+
}
|
|
153
|
+
return measureResponseBodySize ? clonedResponseBodySizeBytes(response) : undefined;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
module.exports = { responseSizeBytesFromResponse };
|