react-native-nitro-fetch 1.3.3 → 1.4.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/README.md +1 -11
- package/android/src/main/java/com/margelo/nitro/nitrofetch/AutoPrefetcher.kt +6 -2
- package/android/src/main/java/com/margelo/nitro/nitrofetch/NitroFetchClient.kt +45 -0
- package/ios/NitroAutoPrefetcher.swift +4 -0
- package/ios/NitroFetchClient.swift +56 -0
- package/lib/module/CurlGenerator.js.map +1 -2
- package/lib/module/Headers.js.map +2 -2
- package/lib/module/HermesProfiler.js.map +1 -1
- package/lib/module/NetworkInspector.js.map +2 -2
- package/lib/module/NitroFetch.nitro.js.map +2 -1
- package/lib/module/NitroInstances.js.map +2 -2
- package/lib/module/Response.js +3 -1
- package/lib/module/Response.js.map +2 -1
- package/lib/module/fetch.js +147 -1
- package/lib/module/fetch.js.map +1 -2
- package/lib/module/index.js.map +1 -2
- package/lib/module/tokenRefresh.js +1 -4
- package/lib/module/tokenRefresh.js.map +1 -2
- package/lib/typescript/src/fetch.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/CurlGenerator.js +26 -23
- package/src/Headers.js +116 -108
- package/src/HermesProfiler.js +18 -16
- package/src/NetworkInspector.js +179 -171
- package/src/NitroInstances.js +1 -2
- package/src/Request.js +164 -167
- package/src/Response.js +242 -244
- package/src/fetch.js +842 -706
- package/src/fetch.ts +170 -1
- package/src/index.js +2 -17
- package/src/index.web.js +67 -69
- package/src/tokenRefresh.js +77 -75
- package/src/utf8.js +27 -28
package/src/fetch.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import 'web-streams-polyfill/polyfill';
|
|
2
|
-
import {
|
|
3
|
-
NitroFetch as NitroFetchSingleton,
|
|
4
|
-
NitroCronetSingleton,
|
|
5
|
-
} from './NitroInstances';
|
|
2
|
+
import { NitroFetch as NitroFetchSingleton, NitroCronetSingleton, } from './NitroInstances';
|
|
6
3
|
import { NativeStorage as NativeStorageSingleton } from './NitroInstances';
|
|
7
4
|
import { NitroHeaders } from './Headers';
|
|
8
5
|
import { NitroResponse } from './Response';
|
|
@@ -10,777 +7,916 @@ import { NitroRequest as NitroRequestClass } from './Request';
|
|
|
10
7
|
import { NetworkInspector } from './NetworkInspector';
|
|
11
8
|
// No base64: pass strings/ArrayBuffers directly
|
|
12
9
|
function headersToPairs(headers) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
headers
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (Array.isArray(headers)) {
|
|
21
|
-
// Convert tuple pairs to objects if needed
|
|
22
|
-
for (const entry of headers) {
|
|
23
|
-
if (Array.isArray(entry) && entry.length >= 2) {
|
|
24
|
-
pairs.push({ key: String(entry[0]), value: String(entry[1]) });
|
|
25
|
-
} else if (
|
|
26
|
-
entry &&
|
|
27
|
-
typeof entry === 'object' &&
|
|
28
|
-
'key' in entry &&
|
|
29
|
-
'value' in entry
|
|
30
|
-
) {
|
|
31
|
-
pairs.push(entry);
|
|
32
|
-
}
|
|
10
|
+
'worklet';
|
|
11
|
+
if (!headers)
|
|
12
|
+
return undefined;
|
|
13
|
+
const pairs = [];
|
|
14
|
+
if (headers instanceof Headers) {
|
|
15
|
+
headers.forEach((v, k) => pairs.push({ key: k, value: v }));
|
|
16
|
+
return pairs;
|
|
33
17
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
// Plain object (Record<string, string>)
|
|
47
|
-
// Use Object.keys to iterate since Object.entries might not work in worklets
|
|
48
|
-
const keys = Object.keys(headers);
|
|
49
|
-
for (let i = 0; i < keys.length; i++) {
|
|
50
|
-
const k = keys[i];
|
|
51
|
-
const v = headers[k];
|
|
52
|
-
if (v !== undefined) {
|
|
53
|
-
pairs.push({ key: k, value: String(v) });
|
|
18
|
+
if (Array.isArray(headers)) {
|
|
19
|
+
// Convert tuple pairs to objects if needed
|
|
20
|
+
for (const entry of headers) {
|
|
21
|
+
if (Array.isArray(entry) && entry.length >= 2) {
|
|
22
|
+
pairs.push({ key: String(entry[0]), value: String(entry[1]) });
|
|
23
|
+
}
|
|
24
|
+
else if (entry &&
|
|
25
|
+
typeof entry === 'object' &&
|
|
26
|
+
'key' in entry &&
|
|
27
|
+
'value' in entry) {
|
|
28
|
+
pairs.push(entry);
|
|
29
|
+
}
|
|
54
30
|
}
|
|
55
|
-
|
|
56
|
-
return pairs;
|
|
31
|
+
return pairs;
|
|
57
32
|
}
|
|
58
|
-
|
|
59
|
-
|
|
33
|
+
// Check if it's a plain object (Record<string, string>) first
|
|
34
|
+
// Plain objects don't have forEach, so check for its absence
|
|
35
|
+
if (typeof headers === 'object' && headers !== null) {
|
|
36
|
+
// Check if it's a Headers instance by checking for forEach method
|
|
37
|
+
const hasForEach = typeof headers.forEach === 'function';
|
|
38
|
+
if (hasForEach) {
|
|
39
|
+
// Headers-like object (duck typing)
|
|
40
|
+
headers.forEach((v, k) => pairs.push({ key: k, value: v }));
|
|
41
|
+
return pairs;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
// Plain object (Record<string, string>)
|
|
45
|
+
// Use Object.keys to iterate since Object.entries might not work in worklets
|
|
46
|
+
const keys = Object.keys(headers);
|
|
47
|
+
for (let i = 0; i < keys.length; i++) {
|
|
48
|
+
const k = keys[i];
|
|
49
|
+
const v = headers[k];
|
|
50
|
+
if (v !== undefined) {
|
|
51
|
+
pairs.push({ key: k, value: String(v) });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return pairs;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return pairs;
|
|
60
58
|
}
|
|
61
59
|
function serializeFormData(fd) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
60
|
+
const parts = [];
|
|
61
|
+
if (typeof fd.getParts === 'function') {
|
|
62
|
+
const rnParts = fd.getParts();
|
|
63
|
+
for (const part of rnParts) {
|
|
64
|
+
if (part.string !== undefined) {
|
|
65
|
+
parts.push({ name: part.fieldName, value: String(part.string) });
|
|
66
|
+
}
|
|
67
|
+
else if (part.uri) {
|
|
68
|
+
parts.push({
|
|
69
|
+
name: part.fieldName,
|
|
70
|
+
fileUri: part.uri,
|
|
71
|
+
fileName: part.fileName ?? part.name ?? 'file',
|
|
72
|
+
mimeType: part.type ?? 'application/octet-stream',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return parts;
|
|
76
77
|
}
|
|
78
|
+
fd.forEach((value, key) => {
|
|
79
|
+
if (typeof value === 'string') {
|
|
80
|
+
parts.push({ name: key, value });
|
|
81
|
+
}
|
|
82
|
+
else if (value && typeof value === 'object') {
|
|
83
|
+
parts.push({
|
|
84
|
+
name: key,
|
|
85
|
+
fileUri: value.uri ?? value.fileUri,
|
|
86
|
+
fileName: value.name ?? value.fileName ?? 'file',
|
|
87
|
+
mimeType: value.type ?? value.mimeType ?? 'application/octet-stream',
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
});
|
|
77
91
|
return parts;
|
|
78
|
-
}
|
|
79
|
-
fd.forEach((value, key) => {
|
|
80
|
-
if (typeof value === 'string') {
|
|
81
|
-
parts.push({ name: key, value });
|
|
82
|
-
} else if (value && typeof value === 'object') {
|
|
83
|
-
parts.push({
|
|
84
|
-
name: key,
|
|
85
|
-
fileUri: value.uri ?? value.fileUri,
|
|
86
|
-
fileName: value.name ?? value.fileName ?? 'file',
|
|
87
|
-
mimeType: value.type ?? value.mimeType ?? 'application/octet-stream',
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
return parts;
|
|
92
92
|
}
|
|
93
93
|
function isFormData(body) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
body &&
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return false;
|
|
94
|
+
if (typeof FormData !== 'undefined' && body instanceof FormData)
|
|
95
|
+
return true;
|
|
96
|
+
if (body &&
|
|
97
|
+
typeof body === 'object' &&
|
|
98
|
+
typeof body.append === 'function' &&
|
|
99
|
+
typeof body.getParts === 'function') {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
104
103
|
}
|
|
105
104
|
function normalizeBody(body) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
105
|
+
'worklet';
|
|
106
|
+
if (body == null)
|
|
107
|
+
return undefined;
|
|
108
|
+
if (typeof body === 'string')
|
|
109
|
+
return { bodyString: body };
|
|
110
|
+
if (isFormData(body)) {
|
|
111
|
+
return { bodyFormData: serializeFormData(body) };
|
|
112
|
+
}
|
|
113
|
+
if (body instanceof URLSearchParams)
|
|
114
|
+
return { bodyString: body.toString() };
|
|
115
|
+
if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer)
|
|
116
|
+
return { bodyBytes: body };
|
|
117
|
+
if (ArrayBuffer.isView(body)) {
|
|
118
|
+
const view = body;
|
|
119
|
+
return {
|
|
120
|
+
//@ts-ignore
|
|
121
|
+
bodyBytes: view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
throw new Error('Unsupported body type for nitro fetch');
|
|
126
125
|
}
|
|
127
126
|
const NitroFetchHybrid = NitroFetchSingleton;
|
|
128
127
|
let client;
|
|
129
128
|
function ensureClient() {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
129
|
+
if (client)
|
|
130
|
+
return client;
|
|
131
|
+
try {
|
|
132
|
+
client = NitroFetchHybrid.createClient();
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
console.error('Failed to create NitroFetch client', err);
|
|
136
|
+
// native not ready; keep undefined
|
|
137
|
+
}
|
|
138
|
+
return client;
|
|
138
139
|
}
|
|
139
140
|
function buildNitroRequest(input, init) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
headers
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
141
|
+
'worklet';
|
|
142
|
+
let url;
|
|
143
|
+
let method;
|
|
144
|
+
let headersInit;
|
|
145
|
+
let body;
|
|
146
|
+
let redirectOption = init?.redirect ?? 'follow';
|
|
147
|
+
let cacheOption = init?.cache;
|
|
148
|
+
if (input instanceof NitroRequestClass) {
|
|
149
|
+
url = input.url;
|
|
150
|
+
method = init?.method ?? input.method;
|
|
151
|
+
headersInit = init?.headers ?? input.headers;
|
|
152
|
+
body = init?.body ?? input.body ?? null;
|
|
153
|
+
if (!init?.redirect)
|
|
154
|
+
redirectOption = input.redirect;
|
|
155
|
+
if (!init?.cache)
|
|
156
|
+
cacheOption = input.cache;
|
|
157
|
+
}
|
|
158
|
+
else if (typeof input === 'string' || input instanceof URL) {
|
|
159
|
+
url = String(input);
|
|
160
|
+
method = init?.method;
|
|
161
|
+
headersInit = init?.headers;
|
|
162
|
+
body = init?.body ?? null;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
// Standard Request object
|
|
166
|
+
url = input.url;
|
|
167
|
+
method = input.method;
|
|
168
|
+
headersInit = input.headers;
|
|
169
|
+
body = init?.body ?? null;
|
|
170
|
+
}
|
|
171
|
+
const headers = headersToPairs(headersInit) ?? [];
|
|
172
|
+
const normalized = normalizeBody(body);
|
|
173
|
+
// Inject cache-control headers based on cache option
|
|
174
|
+
if (cacheOption === 'no-store') {
|
|
175
|
+
headers.push({ key: 'Cache-Control', value: 'no-store' });
|
|
176
|
+
}
|
|
177
|
+
else if (cacheOption === 'no-cache') {
|
|
178
|
+
headers.push({ key: 'Cache-Control', value: 'no-cache' });
|
|
179
|
+
}
|
|
180
|
+
else if (cacheOption === 'reload') {
|
|
181
|
+
headers.push({ key: 'Cache-Control', value: 'no-cache' });
|
|
182
|
+
headers.push({ key: 'Pragma', value: 'no-cache' });
|
|
183
|
+
}
|
|
184
|
+
// Determine followRedirects based on redirect option
|
|
185
|
+
const followRedirects = redirectOption === 'follow';
|
|
186
|
+
const prefetchCacheTtlMs = typeof init?.prefetchCacheTtlMs === 'number'
|
|
187
|
+
? init.prefetchCacheTtlMs
|
|
188
|
+
: undefined;
|
|
189
|
+
return {
|
|
190
|
+
url,
|
|
191
|
+
method: method?.toUpperCase() ?? 'GET',
|
|
192
|
+
headers: headers.length > 0 ? headers : undefined,
|
|
193
|
+
bodyString: normalized?.bodyString,
|
|
194
|
+
bodyBytes: undefined,
|
|
195
|
+
bodyFormData: normalized?.bodyFormData,
|
|
196
|
+
followRedirects,
|
|
197
|
+
prefetchCacheTtlMs,
|
|
198
|
+
};
|
|
193
199
|
}
|
|
194
200
|
// Pure JS version of buildNitroRequest that doesnt use anything that breaks worklets. TODO: Merge this to use Same logic for Worklets and normal Fetch
|
|
195
201
|
function headersToPairsPure(headers) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
202
|
+
'worklet';
|
|
203
|
+
if (!headers)
|
|
204
|
+
return undefined;
|
|
205
|
+
const pairs = [];
|
|
206
|
+
if (Array.isArray(headers)) {
|
|
207
|
+
// Convert tuple pairs to objects if needed
|
|
208
|
+
for (const entry of headers) {
|
|
209
|
+
if (Array.isArray(entry) && entry.length >= 2) {
|
|
210
|
+
pairs.push({ key: String(entry[0]), value: String(entry[1]) });
|
|
211
|
+
}
|
|
212
|
+
else if (entry &&
|
|
213
|
+
typeof entry === 'object' &&
|
|
214
|
+
'key' in entry &&
|
|
215
|
+
'value' in entry) {
|
|
216
|
+
pairs.push(entry);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return pairs;
|
|
212
220
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
221
|
+
// Check if it's a plain object (Record<string, string>) first
|
|
222
|
+
// Plain objects don't have forEach, so check for its absence
|
|
223
|
+
if (typeof headers === 'object' && headers !== null) {
|
|
224
|
+
// Check if it's a Headers instance by checking for forEach method
|
|
225
|
+
const hasForEach = typeof headers.forEach === 'function';
|
|
226
|
+
if (hasForEach) {
|
|
227
|
+
// Headers-like object (duck typing)
|
|
228
|
+
headers.forEach((v, k) => pairs.push({ key: k, value: v }));
|
|
229
|
+
return pairs;
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
// Plain object (Record<string, string>)
|
|
233
|
+
// Use Object.keys to iterate since Object.entries might not work in worklets
|
|
234
|
+
const keys = Object.keys(headers);
|
|
235
|
+
for (let i = 0; i < keys.length; i++) {
|
|
236
|
+
const k = keys[i];
|
|
237
|
+
const v = headers[k];
|
|
238
|
+
if (v !== undefined) {
|
|
239
|
+
pairs.push({ key: k, value: String(v) });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return pairs;
|
|
233
243
|
}
|
|
234
|
-
}
|
|
235
|
-
return pairs;
|
|
236
244
|
}
|
|
237
|
-
|
|
238
|
-
return pairs;
|
|
245
|
+
return pairs;
|
|
239
246
|
}
|
|
240
247
|
// Pure JS version of buildNitroRequest that doesnt use anything that breaks worklets
|
|
241
248
|
function normalizeBodyPure(body) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
body
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
view.byteOffset + view.byteLength
|
|
270
|
-
),
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
throw new Error(
|
|
274
|
-
'Unsupported body type for nitro fetch worklet (FormData is not available in worklets)'
|
|
275
|
-
);
|
|
249
|
+
'worklet';
|
|
250
|
+
if (body == null)
|
|
251
|
+
return undefined;
|
|
252
|
+
if (typeof body === 'string')
|
|
253
|
+
return { bodyString: body };
|
|
254
|
+
// Check for URLSearchParams (duck typing)
|
|
255
|
+
// It should be an object, have a toString method, and typically append/delete methods
|
|
256
|
+
// But mainly we care about toString() returning the query string
|
|
257
|
+
if (typeof body === 'object' &&
|
|
258
|
+
body !== null &&
|
|
259
|
+
typeof body.toString === 'function' &&
|
|
260
|
+
Object.prototype.toString.call(body) === '[object URLSearchParams]') {
|
|
261
|
+
return { bodyString: body.toString() };
|
|
262
|
+
}
|
|
263
|
+
// Check for ArrayBuffer (using toString tag to avoid instanceof)
|
|
264
|
+
if (typeof ArrayBuffer !== 'undefined' &&
|
|
265
|
+
Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
|
|
266
|
+
return { bodyBytes: body };
|
|
267
|
+
}
|
|
268
|
+
if (ArrayBuffer.isView(body)) {
|
|
269
|
+
const view = body;
|
|
270
|
+
return {
|
|
271
|
+
//@ts-ignore
|
|
272
|
+
bodyBytes: view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
throw new Error('Unsupported body type for nitro fetch worklet (FormData is not available in worklets)');
|
|
276
276
|
}
|
|
277
277
|
// Pure JS version of buildNitroRequest that doesnt use anything that breaks worklets
|
|
278
278
|
export function buildNitroRequestPure(input, init) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
};
|
|
279
|
+
'worklet';
|
|
280
|
+
let url;
|
|
281
|
+
let method;
|
|
282
|
+
let headersInit;
|
|
283
|
+
let body;
|
|
284
|
+
// Check if input is URL-like without instanceof
|
|
285
|
+
const isUrlObject = typeof input === 'object' &&
|
|
286
|
+
input !== null &&
|
|
287
|
+
Object.prototype.toString.call(input) === '[object URL]';
|
|
288
|
+
if (typeof input === 'string' || isUrlObject) {
|
|
289
|
+
url = String(input);
|
|
290
|
+
method = init?.method;
|
|
291
|
+
headersInit = init?.headers;
|
|
292
|
+
body = init?.body ?? null;
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
// Request object
|
|
296
|
+
const req = input;
|
|
297
|
+
url = req.url;
|
|
298
|
+
method = req.method;
|
|
299
|
+
headersInit = req.headers;
|
|
300
|
+
// Clone body if needed – Request objects in RN typically allow direct access
|
|
301
|
+
body = init?.body ?? null;
|
|
302
|
+
}
|
|
303
|
+
const headers = headersToPairsPure(headersInit);
|
|
304
|
+
const normalized = normalizeBodyPure(body);
|
|
305
|
+
const prefetchCacheTtlMs = typeof init?.prefetchCacheTtlMs === 'number'
|
|
306
|
+
? init.prefetchCacheTtlMs
|
|
307
|
+
: undefined;
|
|
308
|
+
return {
|
|
309
|
+
url,
|
|
310
|
+
method: method?.toUpperCase() ?? 'GET',
|
|
311
|
+
headers,
|
|
312
|
+
bodyString: normalized?.bodyString,
|
|
313
|
+
// Only include bodyBytes when provided to avoid signaling upload data unintentionally
|
|
314
|
+
bodyBytes: undefined,
|
|
315
|
+
followRedirects: true,
|
|
316
|
+
prefetchCacheTtlMs,
|
|
317
|
+
};
|
|
319
318
|
}
|
|
320
319
|
function createAbortError() {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
320
|
+
const err = new Error('The operation was aborted.');
|
|
321
|
+
err.name = 'AbortError';
|
|
322
|
+
return err;
|
|
324
323
|
}
|
|
325
324
|
async function resolveRequestBody(input, init) {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
325
|
+
if (typeof input === 'string' || input instanceof URL)
|
|
326
|
+
return init;
|
|
327
|
+
if (input instanceof NitroRequestClass)
|
|
328
|
+
return init;
|
|
329
|
+
if (init?.body != null)
|
|
330
|
+
return init;
|
|
331
|
+
const req = input;
|
|
332
|
+
if (typeof req.clone !== 'function')
|
|
333
|
+
return init;
|
|
334
|
+
const method = (init?.method ?? req.method ?? 'GET').toUpperCase();
|
|
335
|
+
if (method === 'GET' || method === 'HEAD')
|
|
336
|
+
return init;
|
|
337
|
+
try {
|
|
338
|
+
const text = await req.clone().text();
|
|
339
|
+
if (text.length === 0)
|
|
340
|
+
return init;
|
|
341
|
+
return { ...(init ?? {}), body: text };
|
|
342
|
+
}
|
|
343
|
+
catch {
|
|
344
|
+
return init;
|
|
345
|
+
}
|
|
340
346
|
}
|
|
341
347
|
async function resolveBlobBody(init) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
return init;
|
|
348
|
+
if (!init?.body)
|
|
349
|
+
return init;
|
|
350
|
+
if (typeof Blob !== 'undefined' && init.body instanceof Blob) {
|
|
351
|
+
const blob = init.body;
|
|
352
|
+
const text = await new Promise((resolve, reject) => {
|
|
353
|
+
const reader = new FileReader();
|
|
354
|
+
reader.onload = () => resolve(reader.result);
|
|
355
|
+
reader.onerror = () => reject(reader.error);
|
|
356
|
+
reader.readAsText(blob);
|
|
357
|
+
});
|
|
358
|
+
// Auto-set Content-Type from Blob.type if not already provided
|
|
359
|
+
let headers = init.headers;
|
|
360
|
+
if (blob.type) {
|
|
361
|
+
const pairs = headersToPairs(headers) ?? [];
|
|
362
|
+
const hasContentType = pairs.some((h) => h.key.toLowerCase() === 'content-type');
|
|
363
|
+
if (!hasContentType) {
|
|
364
|
+
pairs.push({ key: 'Content-Type', value: blob.type });
|
|
365
|
+
headers = pairs.map((h) => [h.key, h.value]);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return { ...init, body: text, headers };
|
|
369
|
+
}
|
|
370
|
+
return init;
|
|
366
371
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
const
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
372
|
+
// http(s) -> native client; anything else is a local resource (hot path).
|
|
373
|
+
function isHttpUrl(url) {
|
|
374
|
+
if (url.startsWith('http://') || url.startsWith('https://'))
|
|
375
|
+
return true;
|
|
376
|
+
const c = url.charCodeAt(0);
|
|
377
|
+
if (c !== 104 && c !== 72)
|
|
378
|
+
return false; // not 'h'/'H'
|
|
379
|
+
return /^https?:/i.test(url);
|
|
380
|
+
}
|
|
381
|
+
function getUrlString(input) {
|
|
382
|
+
if (typeof input === 'string')
|
|
383
|
+
return input;
|
|
384
|
+
if (input instanceof URL)
|
|
385
|
+
return input.toString();
|
|
386
|
+
const u = input?.url;
|
|
387
|
+
return typeof u === 'string' ? u : String(input);
|
|
388
|
+
}
|
|
389
|
+
function base64ToBytes(b64) {
|
|
390
|
+
const decode = globalThis.atob;
|
|
391
|
+
if (typeof decode === 'function') {
|
|
392
|
+
const bin = decode(b64);
|
|
393
|
+
const out = new Uint8Array(bin.length);
|
|
394
|
+
for (let i = 0; i < bin.length; i++)
|
|
395
|
+
out[i] = bin.charCodeAt(i);
|
|
396
|
+
return out;
|
|
397
|
+
}
|
|
398
|
+
// base64 fallback for runtimes without a global atob.
|
|
399
|
+
/* eslint-disable no-bitwise */
|
|
400
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
401
|
+
const clean = b64.replace(/[^A-Za-z0-9+/]/g, '');
|
|
402
|
+
const out = new Uint8Array(Math.floor((clean.length * 3) / 4));
|
|
403
|
+
let p = 0;
|
|
404
|
+
let buf = 0;
|
|
405
|
+
let bits = 0;
|
|
406
|
+
for (let i = 0; i < clean.length; i++) {
|
|
407
|
+
buf = (buf << 6) | chars.indexOf(clean[i]);
|
|
408
|
+
bits += 6;
|
|
409
|
+
if (bits >= 8) {
|
|
410
|
+
bits -= 8;
|
|
411
|
+
out[p++] = (buf >> bits) & 0xff;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return out;
|
|
415
|
+
/* eslint-enable no-bitwise */
|
|
416
|
+
}
|
|
417
|
+
// Cached: our nitro-text-decoder if the app bundles it (aliased require keeps it optional, not a dep), else a global TextDecoder.
|
|
418
|
+
let _decoder;
|
|
419
|
+
function resolveTextDecoder() {
|
|
420
|
+
if (_decoder !== undefined)
|
|
421
|
+
return _decoder;
|
|
422
|
+
try {
|
|
423
|
+
const dynamicRequire = require;
|
|
424
|
+
const mod = dynamicRequire('react-native-nitro-text-decoder');
|
|
425
|
+
if (mod && typeof mod.TextDecoder === 'function') {
|
|
426
|
+
_decoder = new mod.TextDecoder('utf-8', { fatal: true });
|
|
427
|
+
return _decoder;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
catch {
|
|
431
|
+
// optional, not bundled
|
|
432
|
+
}
|
|
433
|
+
const GlobalTextDecoder = globalThis
|
|
434
|
+
.TextDecoder;
|
|
435
|
+
if (typeof GlobalTextDecoder === 'function') {
|
|
436
|
+
_decoder = new GlobalTextDecoder('utf-8', { fatal: true });
|
|
437
|
+
return _decoder;
|
|
438
|
+
}
|
|
439
|
+
_decoder = null;
|
|
440
|
+
return _decoder;
|
|
441
|
+
}
|
|
442
|
+
// data: text via a TextDecoder; null (bytes-only) + a one-time warn if none.
|
|
443
|
+
let _warnedNoTextDecoder = false;
|
|
444
|
+
function decodeUtf8(bytes) {
|
|
445
|
+
const decoder = resolveTextDecoder();
|
|
446
|
+
if (decoder) {
|
|
447
|
+
try {
|
|
448
|
+
return decoder.decode(bytes);
|
|
449
|
+
}
|
|
450
|
+
catch {
|
|
451
|
+
return null; // invalid UTF-8 -> keep bytes only
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
if (!_warnedNoTextDecoder) {
|
|
455
|
+
_warnedNoTextDecoder = true;
|
|
456
|
+
console.warn('[nitro-fetch] Reading a data: URL as text needs a TextDecoder. Install ' +
|
|
457
|
+
'react-native-nitro-text-decoder or expose a global TextDecoder. The ' +
|
|
458
|
+
'body is still available via response.arrayBuffer()/bytes().');
|
|
459
|
+
}
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
// Decode a data: URL into a synthetic 200 response, entirely in JS.
|
|
463
|
+
function decodeDataUrl(url) {
|
|
464
|
+
const comma = url.indexOf(',');
|
|
465
|
+
if (comma < 0)
|
|
466
|
+
throw new TypeError('Failed to fetch: invalid data: URL');
|
|
467
|
+
const meta = url.slice(5, comma); // strip leading "data:"
|
|
468
|
+
const rawData = url.slice(comma + 1);
|
|
469
|
+
const isBase64 = /;base64\s*$/i.test(meta);
|
|
470
|
+
const mediaType = (isBase64 ? meta.replace(/;base64\s*$/i, '') : meta).trim() ||
|
|
471
|
+
'text/plain;charset=US-ASCII';
|
|
472
|
+
let bodyString;
|
|
473
|
+
let bodyBytes;
|
|
474
|
+
let length;
|
|
475
|
+
if (isBase64) {
|
|
476
|
+
const bytes = base64ToBytes(rawData);
|
|
477
|
+
length = bytes.byteLength;
|
|
478
|
+
// bytes for arrayBuffer/bytes; string for text/json when a decoder exists.
|
|
479
|
+
bodyBytes = bytes.buffer;
|
|
480
|
+
const decoded = decodeUtf8(bytes);
|
|
481
|
+
if (decoded != null)
|
|
482
|
+
bodyString = decoded;
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
bodyString = decodeURIComponent(rawData);
|
|
486
|
+
length =
|
|
487
|
+
typeof TextEncoder !== 'undefined'
|
|
488
|
+
? new TextEncoder().encode(bodyString).length
|
|
489
|
+
: bodyString.length;
|
|
490
|
+
}
|
|
386
491
|
return {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
// Inspector: record start (zero cost when disabled — single boolean check)
|
|
399
|
-
let inspectorId;
|
|
400
|
-
if (NetworkInspector.isEnabled()) {
|
|
401
|
-
inspectorId = String(Date.now()) + '-' + String(Math.random()).slice(2, 8);
|
|
402
|
-
NetworkInspector._recordStart(
|
|
403
|
-
inspectorId,
|
|
404
|
-
req.url,
|
|
405
|
-
req.method ?? 'GET',
|
|
406
|
-
req.headers ?? [],
|
|
407
|
-
req.bodyString
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
|
-
// Only allocate a requestId when a signal is present — zero overhead otherwise.
|
|
411
|
-
const requestId = signal ? String(Math.random()) : undefined;
|
|
412
|
-
if (requestId) req.requestId = requestId;
|
|
413
|
-
ensureClient();
|
|
414
|
-
if (!client || typeof client.request !== 'function')
|
|
415
|
-
throw new Error('NitroFetch client not available');
|
|
416
|
-
// Wire up the abort listener with { once: true } so it auto-removes
|
|
417
|
-
// after firing, avoiding a dangling reference to the client closure.
|
|
418
|
-
let abortListener;
|
|
419
|
-
if (signal && requestId) {
|
|
420
|
-
abortListener = () => {
|
|
421
|
-
try {
|
|
422
|
-
client.cancelRequest(requestId);
|
|
423
|
-
} catch {
|
|
424
|
-
// Client may already be torn down — swallow.
|
|
425
|
-
}
|
|
492
|
+
url,
|
|
493
|
+
status: 200,
|
|
494
|
+
statusText: 'OK',
|
|
495
|
+
ok: true,
|
|
496
|
+
redirected: false,
|
|
497
|
+
headers: [
|
|
498
|
+
{ key: 'Content-Type', value: mediaType },
|
|
499
|
+
{ key: 'Content-Length', value: String(length) },
|
|
500
|
+
],
|
|
501
|
+
bodyString,
|
|
502
|
+
bodyBytes,
|
|
426
503
|
};
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
const
|
|
431
|
-
if (
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
}
|
|
447
|
-
// If the signal was aborted (either before or during the request),
|
|
448
|
-
// surface a spec-compliant AbortError regardless of what native threw.
|
|
504
|
+
}
|
|
505
|
+
// Non-http(s): decode data: in JS, reject blob:, read file/content/path natively.
|
|
506
|
+
async function fetchLocalResource(req) {
|
|
507
|
+
const url = req.url;
|
|
508
|
+
if (url.startsWith('data:'))
|
|
509
|
+
return decodeDataUrl(url);
|
|
510
|
+
if (url.startsWith('blob:')) {
|
|
511
|
+
throw new TypeError('nitro-fetch cannot read blob: URLs (the React Native blob registry is not ' +
|
|
512
|
+
'reachable from native). Read blobs with the platform fetch/FileReader instead.');
|
|
513
|
+
}
|
|
514
|
+
ensureClient();
|
|
515
|
+
if (!client || typeof client.request !== 'function') {
|
|
516
|
+
throw new Error('NitroFetch client not available');
|
|
517
|
+
}
|
|
518
|
+
return client.request(req);
|
|
519
|
+
}
|
|
520
|
+
async function nitroFetchRaw(input, init) {
|
|
521
|
+
const signal = init?.signal;
|
|
522
|
+
// Fast-abort: reject synchronously before any bridge work.
|
|
449
523
|
if (signal?.aborted) {
|
|
450
|
-
|
|
524
|
+
throw createAbortError();
|
|
525
|
+
}
|
|
526
|
+
// Extract body from standard Request when init.body is absent (ky/undici pattern)
|
|
527
|
+
init = await resolveRequestBody(input, init);
|
|
528
|
+
// Resolve Blob body to string before passing to sync buildNitroRequest
|
|
529
|
+
init = await resolveBlobBody(init);
|
|
530
|
+
const hasNative = typeof NitroFetchHybrid?.createClient === 'function';
|
|
531
|
+
if (!hasNative) {
|
|
532
|
+
// Fallback path not supported for raw; use global fetch and synthesize minimal shape
|
|
533
|
+
// @ts-ignore: global fetch exists in RN
|
|
534
|
+
const res = await fetch(input, init);
|
|
535
|
+
const url = res.url ?? String(input);
|
|
536
|
+
const bytes = await res.arrayBuffer();
|
|
537
|
+
const headers = [];
|
|
538
|
+
res.headers.forEach((v, k) => headers.push({ key: k, value: v }));
|
|
539
|
+
return {
|
|
540
|
+
url,
|
|
541
|
+
status: res.status,
|
|
542
|
+
statusText: res.statusText,
|
|
543
|
+
ok: res.ok,
|
|
544
|
+
redirected: res.redirected ?? false,
|
|
545
|
+
headers,
|
|
546
|
+
bodyBytes: bytes,
|
|
547
|
+
bodyString: undefined,
|
|
548
|
+
}; // bleee
|
|
549
|
+
}
|
|
550
|
+
const req = buildNitroRequest(input, init);
|
|
551
|
+
// Route non-http(s) (data:/file://content://scheme-less) off the HTTP client.
|
|
552
|
+
if (!isHttpUrl(req.url)) {
|
|
553
|
+
return fetchLocalResource(req);
|
|
554
|
+
}
|
|
555
|
+
// Inspector: record start (zero cost when disabled — single boolean check)
|
|
556
|
+
let inspectorId;
|
|
557
|
+
if (NetworkInspector.isEnabled()) {
|
|
558
|
+
inspectorId = String(Date.now()) + '-' + String(Math.random()).slice(2, 8);
|
|
559
|
+
NetworkInspector._recordStart(inspectorId, req.url, req.method ?? 'GET', req.headers ?? [], req.bodyString);
|
|
560
|
+
}
|
|
561
|
+
// Only allocate a requestId when a signal is present — zero overhead otherwise.
|
|
562
|
+
const requestId = signal ? String(Math.random()) : undefined;
|
|
563
|
+
if (requestId)
|
|
564
|
+
req.requestId = requestId;
|
|
565
|
+
ensureClient();
|
|
566
|
+
if (!client || typeof client.request !== 'function')
|
|
567
|
+
throw new Error('NitroFetch client not available');
|
|
568
|
+
// Wire up the abort listener with { once: true } so it auto-removes
|
|
569
|
+
// after firing, avoiding a dangling reference to the client closure.
|
|
570
|
+
let abortListener;
|
|
571
|
+
if (signal && requestId) {
|
|
572
|
+
abortListener = () => {
|
|
573
|
+
try {
|
|
574
|
+
client.cancelRequest(requestId);
|
|
575
|
+
}
|
|
576
|
+
catch {
|
|
577
|
+
// Client may already be torn down — swallow.
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
signal.addEventListener('abort', abortListener, { once: true });
|
|
581
|
+
}
|
|
582
|
+
try {
|
|
583
|
+
const res = await client.request(req);
|
|
584
|
+
if (inspectorId) {
|
|
585
|
+
NetworkInspector._recordEnd(inspectorId, res.status, res.statusText, res.headers ?? [], res.bodyString?.length ?? 0, undefined, res.bodyString ?? undefined);
|
|
586
|
+
}
|
|
587
|
+
return res;
|
|
451
588
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
589
|
+
catch (e) {
|
|
590
|
+
if (inspectorId) {
|
|
591
|
+
NetworkInspector._recordEnd(inspectorId, 0, '', [], 0, String(e));
|
|
592
|
+
}
|
|
593
|
+
// If the signal was aborted (either before or during the request),
|
|
594
|
+
// surface a spec-compliant AbortError regardless of what native threw.
|
|
595
|
+
if (signal?.aborted) {
|
|
596
|
+
throw createAbortError();
|
|
597
|
+
}
|
|
598
|
+
throw e;
|
|
599
|
+
}
|
|
600
|
+
finally {
|
|
601
|
+
// Idempotent cleanup — removeEventListener is a no-op if the listener
|
|
602
|
+
// already fired (thanks to { once: true }) or was never added.
|
|
603
|
+
if (signal && abortListener) {
|
|
604
|
+
signal.removeEventListener('abort', abortListener);
|
|
605
|
+
}
|
|
458
606
|
}
|
|
459
|
-
}
|
|
460
607
|
}
|
|
461
608
|
// NitroHeaders is now imported from './Headers'
|
|
462
609
|
async function nitroStreamFetch(input, init) {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
)
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
0,
|
|
557
|
-
'',
|
|
558
|
-
[],
|
|
559
|
-
0,
|
|
560
|
-
'Request canceled'
|
|
561
|
-
);
|
|
562
|
-
}
|
|
563
|
-
if (!responseResolved) {
|
|
564
|
-
responseResolved = true;
|
|
565
|
-
rejectResponse(err);
|
|
566
|
-
} else {
|
|
567
|
-
streamController.error(err);
|
|
568
|
-
}
|
|
610
|
+
const url = typeof input === 'string' ? input : String(input);
|
|
611
|
+
const method = init?.method?.toUpperCase() ?? 'GET';
|
|
612
|
+
const headers = headersToPairs(init?.headers);
|
|
613
|
+
// Inspector: record start
|
|
614
|
+
let inspectorId;
|
|
615
|
+
if (NetworkInspector.isEnabled()) {
|
|
616
|
+
inspectorId = String(Date.now()) + '-' + String(Math.random()).slice(2, 8);
|
|
617
|
+
NetworkInspector._recordStart(inspectorId, url, method, headers ?? [], typeof init?.body === 'string' ? init.body : undefined);
|
|
618
|
+
}
|
|
619
|
+
const builder = NitroCronetSingleton.newUrlRequestBuilder(url);
|
|
620
|
+
builder.setHttpMethod(method);
|
|
621
|
+
headers?.forEach((h) => builder.addHeader(h.key, h.value));
|
|
622
|
+
const body = init?.body;
|
|
623
|
+
if (body != null) {
|
|
624
|
+
if (typeof body === 'string')
|
|
625
|
+
builder.setUploadBody(body);
|
|
626
|
+
else if (body instanceof ArrayBuffer)
|
|
627
|
+
builder.setUploadBody(body);
|
|
628
|
+
}
|
|
629
|
+
return new Promise((resolveResponse, rejectResponse) => {
|
|
630
|
+
let streamController;
|
|
631
|
+
const stream = new ReadableStream({
|
|
632
|
+
start(controller) {
|
|
633
|
+
streamController = controller;
|
|
634
|
+
},
|
|
635
|
+
});
|
|
636
|
+
let responseResolved = false;
|
|
637
|
+
let streamBytesReceived = 0;
|
|
638
|
+
builder.onResponseStarted((info) => {
|
|
639
|
+
if (responseResolved)
|
|
640
|
+
return;
|
|
641
|
+
responseResolved = true;
|
|
642
|
+
const status = info.httpStatusCode;
|
|
643
|
+
const responseHeaders = new NitroHeaders(Object.entries(info.allHeaders).map(([key, value]) => ({ key, value })));
|
|
644
|
+
const response = new NitroResponse({
|
|
645
|
+
url: info.url,
|
|
646
|
+
ok: status >= 200 && status < 300,
|
|
647
|
+
status,
|
|
648
|
+
statusText: info.httpStatusText,
|
|
649
|
+
headers: responseHeaders,
|
|
650
|
+
redirected: false,
|
|
651
|
+
body: stream,
|
|
652
|
+
});
|
|
653
|
+
resolveResponse(response);
|
|
654
|
+
// Android/Cronet: kick off the first buffer read.
|
|
655
|
+
// iOS/URLSession handles reading automatically so this is a no-op there.
|
|
656
|
+
request.read();
|
|
657
|
+
});
|
|
658
|
+
builder.onReadCompleted((_info, byteBuffer, bytesRead) => {
|
|
659
|
+
const chunk = new Uint8Array(byteBuffer, 0, bytesRead).slice();
|
|
660
|
+
streamBytesReceived += bytesRead;
|
|
661
|
+
streamController.enqueue(chunk);
|
|
662
|
+
if (!request.isDone()) {
|
|
663
|
+
request.read();
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
builder.onSucceeded((_info) => {
|
|
667
|
+
streamController.close();
|
|
668
|
+
if (inspectorId) {
|
|
669
|
+
const info = _info;
|
|
670
|
+
const status = info?.httpStatusCode ?? 0;
|
|
671
|
+
const hdrs = info?.allHeadersAsList ?? [];
|
|
672
|
+
NetworkInspector._recordEnd(inspectorId, status, info?.httpStatusText ?? '', hdrs, streamBytesReceived);
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
builder.onFailed((_info, error) => {
|
|
676
|
+
const err = new Error(error.message);
|
|
677
|
+
if (inspectorId) {
|
|
678
|
+
NetworkInspector._recordEnd(inspectorId, 0, '', [], 0, error.message);
|
|
679
|
+
}
|
|
680
|
+
if (!responseResolved) {
|
|
681
|
+
responseResolved = true;
|
|
682
|
+
rejectResponse(err);
|
|
683
|
+
}
|
|
684
|
+
else {
|
|
685
|
+
streamController.error(err);
|
|
686
|
+
}
|
|
687
|
+
});
|
|
688
|
+
builder.onCanceled(() => {
|
|
689
|
+
const err = createAbortError();
|
|
690
|
+
if (inspectorId) {
|
|
691
|
+
NetworkInspector._recordEnd(inspectorId, 0, '', [], 0, 'Request canceled');
|
|
692
|
+
}
|
|
693
|
+
if (!responseResolved) {
|
|
694
|
+
responseResolved = true;
|
|
695
|
+
rejectResponse(err);
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
streamController.error(err);
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
const request = builder.build();
|
|
702
|
+
request.start();
|
|
569
703
|
});
|
|
570
|
-
const request = builder.build();
|
|
571
|
-
request.start();
|
|
572
|
-
});
|
|
573
704
|
}
|
|
574
705
|
export async function nitroFetch(input, init) {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
return response;
|
|
706
|
+
// Merge defaults from NitroRequestClass if input is one
|
|
707
|
+
if (input instanceof NitroRequestClass) {
|
|
708
|
+
init = {
|
|
709
|
+
...init,
|
|
710
|
+
signal: init?.signal ?? input.signal,
|
|
711
|
+
redirect: init?.redirect ?? input.redirect,
|
|
712
|
+
cache: init?.cache ?? input.cache,
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
// Streaming is http(s)-only; local URLs fall through to nitroFetchRaw (check runs only when streaming).
|
|
716
|
+
if (init?.stream === true && isHttpUrl(getUrlString(input))) {
|
|
717
|
+
return nitroStreamFetch(input, init);
|
|
718
|
+
}
|
|
719
|
+
const redirectOption = init?.redirect ?? 'follow';
|
|
720
|
+
const res = await nitroFetchRaw(input, init);
|
|
721
|
+
// Handle redirect: "error" — if we got a 3xx back (followRedirects was false), throw
|
|
722
|
+
if (redirectOption === 'error' && res.status >= 300 && res.status < 400) {
|
|
723
|
+
throw new TypeError(`redirect mode is "error": redirected request to "${res.url}"`);
|
|
724
|
+
}
|
|
725
|
+
const response = new NitroResponse({
|
|
726
|
+
url: res.url,
|
|
727
|
+
status: res.status,
|
|
728
|
+
statusText: res.statusText,
|
|
729
|
+
ok: res.ok,
|
|
730
|
+
redirected: res.redirected,
|
|
731
|
+
headers: res.headers,
|
|
732
|
+
bodyBytes: res.bodyBytes,
|
|
733
|
+
bodyString: res.bodyString,
|
|
734
|
+
});
|
|
735
|
+
return response;
|
|
606
736
|
}
|
|
607
737
|
// Start a native prefetch. Requires a `prefetchKey` header on the request.
|
|
608
738
|
export async function prefetch(input, init) {
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
req.headers?.some((h) => h.key.toLowerCase() === 'prefetchkey') ?? false;
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
await client.prefetch(req);
|
|
739
|
+
// If native implementation is not present yet, do nothing
|
|
740
|
+
const hasNative = typeof NitroFetchHybrid?.createClient === 'function';
|
|
741
|
+
if (!hasNative)
|
|
742
|
+
return;
|
|
743
|
+
// Build NitroRequest and ensure prefetchKey header exists
|
|
744
|
+
const req = buildNitroRequest(input, init);
|
|
745
|
+
const hasKey = req.headers?.some((h) => h.key.toLowerCase() === 'prefetchkey') ?? false;
|
|
746
|
+
// Also support passing prefetchKey via non-standard field on init
|
|
747
|
+
const fromInit = init?.prefetchKey;
|
|
748
|
+
if (!hasKey && fromInit) {
|
|
749
|
+
req.headers = (req.headers ?? []).concat([
|
|
750
|
+
{ key: 'prefetchKey', value: fromInit },
|
|
751
|
+
]);
|
|
752
|
+
}
|
|
753
|
+
const finalHasKey = req.headers?.some((h) => h.key.toLowerCase() === 'prefetchkey');
|
|
754
|
+
if (!finalHasKey) {
|
|
755
|
+
throw new Error('prefetch requires a "prefetchKey" header');
|
|
756
|
+
}
|
|
757
|
+
// Ensure client and call native prefetch
|
|
758
|
+
ensureClient();
|
|
759
|
+
if (!client || typeof client.prefetch !== 'function')
|
|
760
|
+
return;
|
|
761
|
+
await client.prefetch(req);
|
|
633
762
|
}
|
|
634
763
|
const AUTOPREFETCH_QUEUE_KEY = 'nitrofetch_autoprefetch_queue';
|
|
635
764
|
// Persist a request to storage so native can prefetch it on app start.
|
|
636
765
|
export async function prefetchOnAppStart(input, init) {
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
try {
|
|
671
|
-
let arr = [];
|
|
766
|
+
// Resolve request and prefetchKey
|
|
767
|
+
const req = buildNitroRequest(input, init);
|
|
768
|
+
const fromHeader = req.headers?.find((h) => h.key.toLowerCase() === 'prefetchkey')?.value;
|
|
769
|
+
const fromInit = init?.prefetchKey;
|
|
770
|
+
const prefetchKey = fromHeader ?? fromInit;
|
|
771
|
+
if (!prefetchKey) {
|
|
772
|
+
throw new Error('prefetchOnAppStart requires a "prefetchKey" (header or init.prefetchKey)');
|
|
773
|
+
}
|
|
774
|
+
// Convert headers to a plain object for storage
|
|
775
|
+
const headersObj = (req.headers ?? []).reduce((acc, { key, value }) => {
|
|
776
|
+
acc[String(key)] = String(value);
|
|
777
|
+
return acc;
|
|
778
|
+
}, {});
|
|
779
|
+
const entry = {
|
|
780
|
+
url: req.url,
|
|
781
|
+
prefetchKey,
|
|
782
|
+
headers: headersObj,
|
|
783
|
+
};
|
|
784
|
+
if (req.method && req.method !== 'GET')
|
|
785
|
+
entry.method = req.method;
|
|
786
|
+
if (req.bodyString !== undefined)
|
|
787
|
+
entry.bodyString = req.bodyString;
|
|
788
|
+
if (typeof req.bodyBytes === 'string' && req.bodyBytes.length > 0)
|
|
789
|
+
entry.bodyBytes = req.bodyBytes;
|
|
790
|
+
if (req.bodyFormData && req.bodyFormData.length > 0)
|
|
791
|
+
entry.bodyFormData = req.bodyFormData;
|
|
792
|
+
if (typeof req.timeoutMs === 'number')
|
|
793
|
+
entry.timeoutMs = req.timeoutMs;
|
|
794
|
+
if (req.followRedirects === false)
|
|
795
|
+
entry.followRedirects = false;
|
|
796
|
+
if (typeof req.prefetchCacheTtlMs === 'number')
|
|
797
|
+
entry.prefetchCacheTtlMs = req.prefetchCacheTtlMs;
|
|
798
|
+
// Write or append to storage queue
|
|
672
799
|
try {
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
800
|
+
let arr = [];
|
|
801
|
+
try {
|
|
802
|
+
const raw = NativeStorageSingleton.getString(AUTOPREFETCH_QUEUE_KEY);
|
|
803
|
+
if (raw)
|
|
804
|
+
arr = JSON.parse(raw);
|
|
805
|
+
if (!Array.isArray(arr))
|
|
806
|
+
arr = [];
|
|
807
|
+
}
|
|
808
|
+
catch {
|
|
809
|
+
arr = [];
|
|
810
|
+
}
|
|
811
|
+
if (arr.some((e) => e && e.prefetchKey === prefetchKey)) {
|
|
812
|
+
arr = arr.filter((e) => e && e.prefetchKey !== prefetchKey);
|
|
813
|
+
}
|
|
814
|
+
arr.push(entry);
|
|
815
|
+
NativeStorageSingleton.setString(AUTOPREFETCH_QUEUE_KEY, JSON.stringify(arr));
|
|
816
|
+
}
|
|
817
|
+
catch (e) {
|
|
818
|
+
console.warn('Failed to persist prefetch queue', e);
|
|
819
|
+
}
|
|
690
820
|
}
|
|
691
821
|
// Remove one entry (by prefetchKey) from the auto-prefetch queue.
|
|
692
822
|
export async function removeFromAutoPrefetch(prefetchKey) {
|
|
693
|
-
try {
|
|
694
|
-
let arr = [];
|
|
695
823
|
try {
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
824
|
+
let arr = [];
|
|
825
|
+
try {
|
|
826
|
+
const raw = NativeStorageSingleton.getString(AUTOPREFETCH_QUEUE_KEY);
|
|
827
|
+
if (raw)
|
|
828
|
+
arr = JSON.parse(raw);
|
|
829
|
+
if (!Array.isArray(arr))
|
|
830
|
+
arr = [];
|
|
831
|
+
}
|
|
832
|
+
catch {
|
|
833
|
+
arr = [];
|
|
834
|
+
}
|
|
835
|
+
const next = arr.filter((e) => e && e.prefetchKey !== prefetchKey);
|
|
836
|
+
if (next.length === 0) {
|
|
837
|
+
NativeStorageSingleton.removeString(AUTOPREFETCH_QUEUE_KEY);
|
|
838
|
+
}
|
|
839
|
+
else if (next.length !== arr.length) {
|
|
840
|
+
NativeStorageSingleton.setString(AUTOPREFETCH_QUEUE_KEY, JSON.stringify(next));
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
catch (e) {
|
|
844
|
+
console.warn('Failed to remove from prefetch queue', e);
|
|
845
|
+
}
|
|
714
846
|
}
|
|
715
847
|
// Remove all entries from the auto-prefetch queue.
|
|
716
848
|
export async function removeAllFromAutoprefetch() {
|
|
717
|
-
|
|
849
|
+
NativeStorageSingleton.setString(AUTOPREFETCH_QUEUE_KEY, JSON.stringify([]));
|
|
718
850
|
}
|
|
719
851
|
export function __readAutoPrefetchQueue() {
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
852
|
+
try {
|
|
853
|
+
const raw = NativeStorageSingleton.getString(AUTOPREFETCH_QUEUE_KEY);
|
|
854
|
+
if (!raw)
|
|
855
|
+
return [];
|
|
856
|
+
const parsed = JSON.parse(raw);
|
|
857
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
858
|
+
}
|
|
859
|
+
catch {
|
|
860
|
+
return [];
|
|
861
|
+
}
|
|
728
862
|
}
|
|
729
863
|
let nitroRuntime;
|
|
730
864
|
function ensureWorkletRuntime(name = 'nitro-fetch') {
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
865
|
+
try {
|
|
866
|
+
const { createWorkletRuntime } = require('react-native-worklets');
|
|
867
|
+
nitroRuntime = nitroRuntime ?? createWorkletRuntime(name);
|
|
868
|
+
return nitroRuntime;
|
|
869
|
+
}
|
|
870
|
+
catch {
|
|
871
|
+
console.warn('react-native-worklets not available');
|
|
872
|
+
return undefined;
|
|
873
|
+
}
|
|
739
874
|
}
|
|
740
875
|
export async function nitroFetchOnWorklet(input, init, mapWorklet, options) {
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
876
|
+
const preferBytes = options?.preferBytes === true; // default true
|
|
877
|
+
let runOnRuntimeAsync;
|
|
878
|
+
let rt;
|
|
879
|
+
try {
|
|
880
|
+
rt = ensureWorkletRuntime(options?.runtimeName);
|
|
881
|
+
const worklets = require('react-native-worklets');
|
|
882
|
+
runOnRuntimeAsync = worklets.runOnRuntimeAsync;
|
|
883
|
+
}
|
|
884
|
+
catch {
|
|
885
|
+
// Module not available
|
|
886
|
+
}
|
|
887
|
+
// Fallback: if runtime is not available, do the work on JS
|
|
888
|
+
if (!runOnRuntimeAsync || !rt) {
|
|
889
|
+
console.warn('nitroFetchOnWorklet: no runtime, mapping on JS thread');
|
|
890
|
+
const res = await nitroFetchRaw(input, init);
|
|
891
|
+
const payload = {
|
|
892
|
+
url: res.url,
|
|
893
|
+
status: res.status,
|
|
894
|
+
statusText: res.statusText,
|
|
895
|
+
ok: res.ok,
|
|
896
|
+
redirected: res.redirected,
|
|
897
|
+
headers: res.headers,
|
|
898
|
+
bodyBytes: preferBytes ? res.bodyBytes : undefined,
|
|
899
|
+
bodyString: preferBytes ? undefined : res.bodyString,
|
|
900
|
+
};
|
|
901
|
+
return mapWorklet(payload);
|
|
902
|
+
}
|
|
903
|
+
return await runOnRuntimeAsync(rt, () => {
|
|
904
|
+
'worklet';
|
|
905
|
+
const nitroFetchClient = NitroFetchHybrid.createClient();
|
|
906
|
+
const request = buildNitroRequestPure(input, init);
|
|
907
|
+
const res = nitroFetchClient.requestSync(request);
|
|
908
|
+
const payload = {
|
|
909
|
+
url: res.url,
|
|
910
|
+
status: res.status,
|
|
911
|
+
statusText: res.statusText,
|
|
912
|
+
ok: res.ok,
|
|
913
|
+
redirected: res.redirected,
|
|
914
|
+
headers: res.headers,
|
|
915
|
+
bodyBytes: preferBytes ? res.bodyBytes : undefined,
|
|
916
|
+
bodyString: preferBytes ? undefined : res.bodyString,
|
|
917
|
+
};
|
|
918
|
+
return mapWorklet(payload);
|
|
919
|
+
});
|
|
784
920
|
}
|
|
785
921
|
export { NitroHeaders } from './Headers';
|
|
786
922
|
export { NitroResponse } from './Response';
|