react-native-nitro-fetch 1.3.1 → 1.3.2
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/android/build.gradle +12 -0
- package/android/src/main/java/com/margelo/nitro/nitrofetch/AutoPrefetcher.kt +148 -55
- package/ios/NitroAutoPrefetcher.swift +149 -53
- package/lib/module/CurlGenerator.js.map +2 -1
- package/lib/module/HermesProfiler.js.map +1 -2
- package/lib/module/NetworkInspector.js +4 -0
- package/lib/module/NetworkInspector.js.map +1 -1
- package/lib/module/NitroFetch.nitro.js.map +1 -2
- package/lib/module/NitroInstances.js.map +1 -1
- package/lib/module/Request.js.map +2 -1
- package/lib/module/Response.js +3 -1
- package/lib/module/Response.js.map +2 -2
- package/lib/module/fetch.js +8 -10
- package/lib/module/fetch.js.map +1 -1
- package/lib/module/index.js.map +1 -2
- package/lib/module/index.web.js +1 -1
- package/lib/module/index.web.js.map +2 -1
- package/lib/module/tokenRefresh.js +1 -4
- package/lib/module/tokenRefresh.js.map +2 -1
- package/lib/module/utf8.js.map +2 -2
- package/lib/typescript/src/fetch.d.ts.map +1 -1
- package/lib/typescript/src/tokenRefresh.d.ts +14 -0
- package/lib/typescript/src/tokenRefresh.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/CurlGenerator.js +23 -26
- package/src/Headers.js +108 -116
- package/src/HermesProfiler.js +16 -18
- package/src/NetworkInspector.js +171 -179
- package/src/NitroInstances.js +2 -1
- package/src/Request.js +167 -164
- package/src/Response.js +244 -242
- package/src/fetch.js +708 -693
- package/src/fetch.ts +15 -16
- package/src/index.js +17 -2
- package/src/index.web.js +69 -67
- package/src/tokenRefresh.js +75 -77
- package/src/tokenRefresh.ts +16 -0
- package/src/utf8.js +28 -27
package/src/fetch.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import 'web-streams-polyfill/polyfill';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
NitroFetch as NitroFetchSingleton,
|
|
4
|
+
NitroCronetSingleton,
|
|
5
|
+
} from './NitroInstances';
|
|
3
6
|
import { NativeStorage as NativeStorageSingleton } from './NitroInstances';
|
|
4
7
|
import { NitroHeaders } from './Headers';
|
|
5
8
|
import { NitroResponse } from './Response';
|
|
@@ -7,765 +10,777 @@ import { NitroRequest as NitroRequestClass } from './Request';
|
|
|
7
10
|
import { NetworkInspector } from './NetworkInspector';
|
|
8
11
|
// No base64: pass strings/ArrayBuffers directly
|
|
9
12
|
function headersToPairs(headers) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
return pairs;
|
|
13
|
+
'worklet';
|
|
14
|
+
if (!headers) return undefined;
|
|
15
|
+
const pairs = [];
|
|
16
|
+
if (headers instanceof Headers) {
|
|
17
|
+
headers.forEach((v, k) => pairs.push({ key: k, value: v }));
|
|
18
|
+
return pairs;
|
|
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
|
+
}
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
return pairs;
|
|
34
|
+
return pairs;
|
|
35
|
+
}
|
|
36
|
+
// Check if it's a plain object (Record<string, string>) first
|
|
37
|
+
// Plain objects don't have forEach, so check for its absence
|
|
38
|
+
if (typeof headers === 'object' && headers !== null) {
|
|
39
|
+
// Check if it's a Headers instance by checking for forEach method
|
|
40
|
+
const hasForEach = typeof headers.forEach === 'function';
|
|
41
|
+
if (hasForEach) {
|
|
42
|
+
// Headers-like object (duck typing)
|
|
43
|
+
headers.forEach((v, k) => pairs.push({ key: k, value: v }));
|
|
44
|
+
return pairs;
|
|
45
|
+
} else {
|
|
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) });
|
|
55
54
|
}
|
|
55
|
+
}
|
|
56
|
+
return pairs;
|
|
56
57
|
}
|
|
57
|
-
|
|
58
|
+
}
|
|
59
|
+
return pairs;
|
|
58
60
|
}
|
|
59
61
|
function serializeFormData(fd) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return parts;
|
|
62
|
+
const parts = [];
|
|
63
|
+
if (typeof fd.getParts === 'function') {
|
|
64
|
+
const rnParts = fd.getParts();
|
|
65
|
+
for (const part of rnParts) {
|
|
66
|
+
if (part.string !== undefined) {
|
|
67
|
+
parts.push({ name: part.fieldName, value: String(part.string) });
|
|
68
|
+
} else if (part.uri) {
|
|
69
|
+
parts.push({
|
|
70
|
+
name: part.fieldName,
|
|
71
|
+
fileUri: part.uri,
|
|
72
|
+
fileName: part.fileName ?? part.name ?? 'file',
|
|
73
|
+
mimeType: part.type ?? 'application/octet-stream',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
77
76
|
}
|
|
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
|
-
});
|
|
91
77
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
94
|
+
if (typeof FormData !== 'undefined' && body instanceof FormData) return true;
|
|
95
|
+
if (
|
|
96
|
+
body &&
|
|
97
|
+
typeof body === 'object' &&
|
|
98
|
+
typeof body.append === 'function' &&
|
|
99
|
+
typeof body.getParts === 'function'
|
|
100
|
+
) {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
103
104
|
}
|
|
104
105
|
function normalizeBody(body) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
106
|
+
'worklet';
|
|
107
|
+
if (body == null) return undefined;
|
|
108
|
+
if (typeof body === 'string') return { bodyString: body };
|
|
109
|
+
if (isFormData(body)) {
|
|
110
|
+
return { bodyFormData: serializeFormData(body) };
|
|
111
|
+
}
|
|
112
|
+
if (body instanceof URLSearchParams) return { bodyString: body.toString() };
|
|
113
|
+
if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer)
|
|
114
|
+
return { bodyBytes: body };
|
|
115
|
+
if (ArrayBuffer.isView(body)) {
|
|
116
|
+
const view = body;
|
|
117
|
+
return {
|
|
118
|
+
//@ts-ignore
|
|
119
|
+
bodyBytes: view.buffer.slice(
|
|
120
|
+
view.byteOffset,
|
|
121
|
+
view.byteOffset + view.byteLength
|
|
122
|
+
),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
throw new Error('Unsupported body type for nitro fetch');
|
|
125
126
|
}
|
|
126
127
|
const NitroFetchHybrid = NitroFetchSingleton;
|
|
127
128
|
let client;
|
|
128
129
|
function ensureClient() {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
return client;
|
|
130
|
+
if (client) return client;
|
|
131
|
+
try {
|
|
132
|
+
client = NitroFetchHybrid.createClient();
|
|
133
|
+
} catch (err) {
|
|
134
|
+
console.error('Failed to create NitroFetch client', err);
|
|
135
|
+
// native not ready; keep undefined
|
|
136
|
+
}
|
|
137
|
+
return client;
|
|
139
138
|
}
|
|
140
139
|
function buildNitroRequest(input, init) {
|
|
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
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
bodyBytes: undefined,
|
|
195
|
-
bodyFormData: normalized?.bodyFormData,
|
|
196
|
-
followRedirects,
|
|
197
|
-
prefetchCacheTtlMs,
|
|
198
|
-
};
|
|
140
|
+
'worklet';
|
|
141
|
+
let url;
|
|
142
|
+
let method;
|
|
143
|
+
let headersInit;
|
|
144
|
+
let body;
|
|
145
|
+
let redirectOption = init?.redirect ?? 'follow';
|
|
146
|
+
let cacheOption = init?.cache;
|
|
147
|
+
if (input instanceof NitroRequestClass) {
|
|
148
|
+
url = input.url;
|
|
149
|
+
method = init?.method ?? input.method;
|
|
150
|
+
headersInit = init?.headers ?? input.headers;
|
|
151
|
+
body = init?.body ?? input.body ?? null;
|
|
152
|
+
if (!init?.redirect) redirectOption = input.redirect;
|
|
153
|
+
if (!init?.cache) cacheOption = input.cache;
|
|
154
|
+
} else if (typeof input === 'string' || input instanceof URL) {
|
|
155
|
+
url = String(input);
|
|
156
|
+
method = init?.method;
|
|
157
|
+
headersInit = init?.headers;
|
|
158
|
+
body = init?.body ?? null;
|
|
159
|
+
} else {
|
|
160
|
+
// Standard Request object
|
|
161
|
+
url = input.url;
|
|
162
|
+
method = input.method;
|
|
163
|
+
headersInit = input.headers;
|
|
164
|
+
body = init?.body ?? null;
|
|
165
|
+
}
|
|
166
|
+
const headers = headersToPairs(headersInit) ?? [];
|
|
167
|
+
const normalized = normalizeBody(body);
|
|
168
|
+
// Inject cache-control headers based on cache option
|
|
169
|
+
if (cacheOption === 'no-store') {
|
|
170
|
+
headers.push({ key: 'Cache-Control', value: 'no-store' });
|
|
171
|
+
} else if (cacheOption === 'no-cache') {
|
|
172
|
+
headers.push({ key: 'Cache-Control', value: 'no-cache' });
|
|
173
|
+
} else if (cacheOption === 'reload') {
|
|
174
|
+
headers.push({ key: 'Cache-Control', value: 'no-cache' });
|
|
175
|
+
headers.push({ key: 'Pragma', value: 'no-cache' });
|
|
176
|
+
}
|
|
177
|
+
// Determine followRedirects based on redirect option
|
|
178
|
+
const followRedirects = redirectOption === 'follow';
|
|
179
|
+
const prefetchCacheTtlMs =
|
|
180
|
+
typeof init?.prefetchCacheTtlMs === 'number'
|
|
181
|
+
? init.prefetchCacheTtlMs
|
|
182
|
+
: undefined;
|
|
183
|
+
return {
|
|
184
|
+
url,
|
|
185
|
+
method: method?.toUpperCase() ?? 'GET',
|
|
186
|
+
headers: headers.length > 0 ? headers : undefined,
|
|
187
|
+
bodyString: normalized?.bodyString,
|
|
188
|
+
bodyBytes: undefined,
|
|
189
|
+
bodyFormData: normalized?.bodyFormData,
|
|
190
|
+
followRedirects,
|
|
191
|
+
prefetchCacheTtlMs,
|
|
192
|
+
};
|
|
199
193
|
}
|
|
200
194
|
// Pure JS version of buildNitroRequest that doesnt use anything that breaks worklets. TODO: Merge this to use Same logic for Worklets and normal Fetch
|
|
201
195
|
function headersToPairsPure(headers) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
return pairs;
|
|
196
|
+
'worklet';
|
|
197
|
+
if (!headers) return undefined;
|
|
198
|
+
const pairs = [];
|
|
199
|
+
if (Array.isArray(headers)) {
|
|
200
|
+
// Convert tuple pairs to objects if needed
|
|
201
|
+
for (const entry of headers) {
|
|
202
|
+
if (Array.isArray(entry) && entry.length >= 2) {
|
|
203
|
+
pairs.push({ key: String(entry[0]), value: String(entry[1]) });
|
|
204
|
+
} else if (
|
|
205
|
+
entry &&
|
|
206
|
+
typeof entry === 'object' &&
|
|
207
|
+
'key' in entry &&
|
|
208
|
+
'value' in entry
|
|
209
|
+
) {
|
|
210
|
+
pairs.push(entry);
|
|
211
|
+
}
|
|
220
212
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
}
|
|
242
|
-
return pairs;
|
|
213
|
+
return pairs;
|
|
214
|
+
}
|
|
215
|
+
// Check if it's a plain object (Record<string, string>) first
|
|
216
|
+
// Plain objects don't have forEach, so check for its absence
|
|
217
|
+
if (typeof headers === 'object' && headers !== null) {
|
|
218
|
+
// Check if it's a Headers instance by checking for forEach method
|
|
219
|
+
const hasForEach = typeof headers.forEach === 'function';
|
|
220
|
+
if (hasForEach) {
|
|
221
|
+
// Headers-like object (duck typing)
|
|
222
|
+
headers.forEach((v, k) => pairs.push({ key: k, value: v }));
|
|
223
|
+
return pairs;
|
|
224
|
+
} else {
|
|
225
|
+
// Plain object (Record<string, string>)
|
|
226
|
+
// Use Object.keys to iterate since Object.entries might not work in worklets
|
|
227
|
+
const keys = Object.keys(headers);
|
|
228
|
+
for (let i = 0; i < keys.length; i++) {
|
|
229
|
+
const k = keys[i];
|
|
230
|
+
const v = headers[k];
|
|
231
|
+
if (v !== undefined) {
|
|
232
|
+
pairs.push({ key: k, value: String(v) });
|
|
243
233
|
}
|
|
234
|
+
}
|
|
235
|
+
return pairs;
|
|
244
236
|
}
|
|
245
|
-
|
|
237
|
+
}
|
|
238
|
+
return pairs;
|
|
246
239
|
}
|
|
247
240
|
// Pure JS version of buildNitroRequest that doesnt use anything that breaks worklets
|
|
248
241
|
function normalizeBodyPure(body) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
242
|
+
'worklet';
|
|
243
|
+
if (body == null) return undefined;
|
|
244
|
+
if (typeof body === 'string') return { bodyString: body };
|
|
245
|
+
// Check for URLSearchParams (duck typing)
|
|
246
|
+
// It should be an object, have a toString method, and typically append/delete methods
|
|
247
|
+
// But mainly we care about toString() returning the query string
|
|
248
|
+
if (
|
|
249
|
+
typeof body === 'object' &&
|
|
250
|
+
body !== null &&
|
|
251
|
+
typeof body.toString === 'function' &&
|
|
252
|
+
Object.prototype.toString.call(body) === '[object URLSearchParams]'
|
|
253
|
+
) {
|
|
254
|
+
return { bodyString: body.toString() };
|
|
255
|
+
}
|
|
256
|
+
// Check for ArrayBuffer (using toString tag to avoid instanceof)
|
|
257
|
+
if (
|
|
258
|
+
typeof ArrayBuffer !== 'undefined' &&
|
|
259
|
+
Object.prototype.toString.call(body) === '[object ArrayBuffer]'
|
|
260
|
+
) {
|
|
261
|
+
return { bodyBytes: body };
|
|
262
|
+
}
|
|
263
|
+
if (ArrayBuffer.isView(body)) {
|
|
264
|
+
const view = body;
|
|
265
|
+
return {
|
|
266
|
+
//@ts-ignore
|
|
267
|
+
bodyBytes: view.buffer.slice(
|
|
268
|
+
view.byteOffset,
|
|
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
|
+
);
|
|
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
|
-
|
|
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 =
|
|
286
|
+
typeof input === 'object' &&
|
|
287
|
+
input !== null &&
|
|
288
|
+
Object.prototype.toString.call(input) === '[object URL]';
|
|
289
|
+
if (typeof input === 'string' || isUrlObject) {
|
|
290
|
+
url = String(input);
|
|
291
|
+
method = init?.method;
|
|
292
|
+
headersInit = init?.headers;
|
|
293
|
+
body = init?.body ?? null;
|
|
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 =
|
|
306
|
+
typeof init?.prefetchCacheTtlMs === 'number'
|
|
307
|
+
? init.prefetchCacheTtlMs
|
|
308
|
+
: undefined;
|
|
309
|
+
return {
|
|
310
|
+
url,
|
|
311
|
+
method: method?.toUpperCase() ?? 'GET',
|
|
312
|
+
headers,
|
|
313
|
+
bodyString: normalized?.bodyString,
|
|
314
|
+
// Only include bodyBytes when provided to avoid signaling upload data unintentionally
|
|
315
|
+
bodyBytes: undefined,
|
|
316
|
+
followRedirects: true,
|
|
317
|
+
prefetchCacheTtlMs,
|
|
318
|
+
};
|
|
318
319
|
}
|
|
319
320
|
function createAbortError() {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
321
|
+
const err = new Error('The operation was aborted.');
|
|
322
|
+
err.name = 'AbortError';
|
|
323
|
+
return err;
|
|
323
324
|
}
|
|
324
325
|
async function resolveRequestBody(input, init) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
if (text.length === 0)
|
|
340
|
-
return init;
|
|
341
|
-
return { ...(init ?? {}), body: text };
|
|
342
|
-
}
|
|
343
|
-
catch {
|
|
344
|
-
return init;
|
|
345
|
-
}
|
|
326
|
+
if (typeof input === 'string' || input instanceof URL) return init;
|
|
327
|
+
if (input instanceof NitroRequestClass) return init;
|
|
328
|
+
if (init?.body != null) return init;
|
|
329
|
+
const req = input;
|
|
330
|
+
if (typeof req.clone !== 'function') return init;
|
|
331
|
+
const method = (init?.method ?? req.method ?? 'GET').toUpperCase();
|
|
332
|
+
if (method === 'GET' || method === 'HEAD') return init;
|
|
333
|
+
try {
|
|
334
|
+
const text = await req.clone().text();
|
|
335
|
+
if (text.length === 0) return init;
|
|
336
|
+
return { ...(init ?? {}), body: text };
|
|
337
|
+
} catch {
|
|
338
|
+
return init;
|
|
339
|
+
}
|
|
346
340
|
}
|
|
347
341
|
async function resolveBlobBody(init) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
}
|
|
370
|
-
|
|
342
|
+
if (!init?.body) return init;
|
|
343
|
+
if (typeof Blob !== 'undefined' && init.body instanceof Blob) {
|
|
344
|
+
const blob = init.body;
|
|
345
|
+
const text = await new Promise((resolve, reject) => {
|
|
346
|
+
const reader = new FileReader();
|
|
347
|
+
reader.onload = () => resolve(reader.result);
|
|
348
|
+
reader.onerror = () => reject(reader.error);
|
|
349
|
+
reader.readAsText(blob);
|
|
350
|
+
});
|
|
351
|
+
// Auto-set Content-Type from Blob.type if not already provided
|
|
352
|
+
let headers = init.headers;
|
|
353
|
+
if (blob.type) {
|
|
354
|
+
const pairs = headersToPairs(headers) ?? [];
|
|
355
|
+
const hasContentType = pairs.some(
|
|
356
|
+
(h) => h.key.toLowerCase() === 'content-type'
|
|
357
|
+
);
|
|
358
|
+
if (!hasContentType) {
|
|
359
|
+
pairs.push({ key: 'Content-Type', value: blob.type });
|
|
360
|
+
headers = pairs.map((h) => [h.key, h.value]);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return { ...init, body: text, headers };
|
|
364
|
+
}
|
|
365
|
+
return init;
|
|
371
366
|
}
|
|
372
367
|
async function nitroFetchRaw(input, init) {
|
|
373
|
-
|
|
374
|
-
|
|
368
|
+
const signal = init?.signal;
|
|
369
|
+
// Fast-abort: reject synchronously before any bridge work.
|
|
370
|
+
if (signal?.aborted) {
|
|
371
|
+
throw createAbortError();
|
|
372
|
+
}
|
|
373
|
+
// Extract body from standard Request when init.body is absent (ky/undici pattern)
|
|
374
|
+
init = await resolveRequestBody(input, init);
|
|
375
|
+
// Resolve Blob body to string before passing to sync buildNitroRequest
|
|
376
|
+
init = await resolveBlobBody(init);
|
|
377
|
+
const hasNative = typeof NitroFetchHybrid?.createClient === 'function';
|
|
378
|
+
if (!hasNative) {
|
|
379
|
+
// Fallback path not supported for raw; use global fetch and synthesize minimal shape
|
|
380
|
+
// @ts-ignore: global fetch exists in RN
|
|
381
|
+
const res = await fetch(input, init);
|
|
382
|
+
const url = res.url ?? String(input);
|
|
383
|
+
const bytes = await res.arrayBuffer();
|
|
384
|
+
const headers = [];
|
|
385
|
+
res.headers.forEach((v, k) => headers.push({ key: k, value: v }));
|
|
386
|
+
return {
|
|
387
|
+
url,
|
|
388
|
+
status: res.status,
|
|
389
|
+
statusText: res.statusText,
|
|
390
|
+
ok: res.ok,
|
|
391
|
+
redirected: res.redirected ?? false,
|
|
392
|
+
headers,
|
|
393
|
+
bodyBytes: bytes,
|
|
394
|
+
bodyString: undefined,
|
|
395
|
+
}; // bleee
|
|
396
|
+
}
|
|
397
|
+
const req = buildNitroRequest(input, init);
|
|
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
|
+
}
|
|
426
|
+
};
|
|
427
|
+
signal.addEventListener('abort', abortListener, { once: true });
|
|
428
|
+
}
|
|
429
|
+
try {
|
|
430
|
+
const res = await client.request(req);
|
|
431
|
+
if (inspectorId) {
|
|
432
|
+
NetworkInspector._recordEnd(
|
|
433
|
+
inspectorId,
|
|
434
|
+
res.status,
|
|
435
|
+
res.statusText,
|
|
436
|
+
res.headers ?? [],
|
|
437
|
+
res.bodyString?.length ?? 0,
|
|
438
|
+
undefined,
|
|
439
|
+
res.bodyString ?? undefined
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
return res;
|
|
443
|
+
} catch (e) {
|
|
444
|
+
if (inspectorId) {
|
|
445
|
+
NetworkInspector._recordEnd(inspectorId, 0, '', [], 0, String(e));
|
|
446
|
+
}
|
|
447
|
+
// If the signal was aborted (either before or during the request),
|
|
448
|
+
// surface a spec-compliant AbortError regardless of what native threw.
|
|
375
449
|
if (signal?.aborted) {
|
|
376
|
-
|
|
450
|
+
throw createAbortError();
|
|
377
451
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
//
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
// Fallback path not supported for raw; use global fetch and synthesize minimal shape
|
|
385
|
-
// @ts-ignore: global fetch exists in RN
|
|
386
|
-
const res = await fetch(input, init);
|
|
387
|
-
const url = res.url ?? String(input);
|
|
388
|
-
const bytes = await res.arrayBuffer();
|
|
389
|
-
const headers = [];
|
|
390
|
-
res.headers.forEach((v, k) => headers.push({ key: k, value: v }));
|
|
391
|
-
return {
|
|
392
|
-
url,
|
|
393
|
-
status: res.status,
|
|
394
|
-
statusText: res.statusText,
|
|
395
|
-
ok: res.ok,
|
|
396
|
-
redirected: res.redirected ?? false,
|
|
397
|
-
headers,
|
|
398
|
-
bodyBytes: bytes,
|
|
399
|
-
bodyString: undefined,
|
|
400
|
-
}; // bleee
|
|
401
|
-
}
|
|
402
|
-
const req = buildNitroRequest(input, init);
|
|
403
|
-
// Inspector: record start (zero cost when disabled — single boolean check)
|
|
404
|
-
let inspectorId;
|
|
405
|
-
if (NetworkInspector.isEnabled()) {
|
|
406
|
-
inspectorId = String(Date.now()) + '-' + String(Math.random()).slice(2, 8);
|
|
407
|
-
NetworkInspector._recordStart(inspectorId, req.url, req.method ?? 'GET', req.headers ?? [], req.bodyString);
|
|
408
|
-
}
|
|
409
|
-
// Only allocate a requestId when a signal is present — zero overhead otherwise.
|
|
410
|
-
const requestId = signal ? String(Math.random()) : undefined;
|
|
411
|
-
if (requestId)
|
|
412
|
-
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
|
-
}
|
|
424
|
-
catch {
|
|
425
|
-
// Client may already be torn down — swallow.
|
|
426
|
-
}
|
|
427
|
-
};
|
|
428
|
-
signal.addEventListener('abort', abortListener, { once: true });
|
|
429
|
-
}
|
|
430
|
-
try {
|
|
431
|
-
const res = await client.request(req);
|
|
432
|
-
if (inspectorId) {
|
|
433
|
-
NetworkInspector._recordEnd(inspectorId, res.status, res.statusText, res.headers ?? [], res.bodyString?.length ?? 0, undefined, res.bodyString ?? undefined);
|
|
434
|
-
}
|
|
435
|
-
return res;
|
|
436
|
-
}
|
|
437
|
-
catch (e) {
|
|
438
|
-
if (inspectorId) {
|
|
439
|
-
NetworkInspector._recordEnd(inspectorId, 0, '', [], 0, String(e));
|
|
440
|
-
}
|
|
441
|
-
// If the signal was aborted (either before or during the request),
|
|
442
|
-
// surface a spec-compliant AbortError regardless of what native threw.
|
|
443
|
-
if (signal?.aborted) {
|
|
444
|
-
throw createAbortError();
|
|
445
|
-
}
|
|
446
|
-
throw e;
|
|
447
|
-
}
|
|
448
|
-
finally {
|
|
449
|
-
// Idempotent cleanup — removeEventListener is a no-op if the listener
|
|
450
|
-
// already fired (thanks to { once: true }) or was never added.
|
|
451
|
-
if (signal && abortListener) {
|
|
452
|
-
signal.removeEventListener('abort', abortListener);
|
|
453
|
-
}
|
|
452
|
+
throw e;
|
|
453
|
+
} finally {
|
|
454
|
+
// Idempotent cleanup — removeEventListener is a no-op if the listener
|
|
455
|
+
// already fired (thanks to { once: true }) or was never added.
|
|
456
|
+
if (signal && abortListener) {
|
|
457
|
+
signal.removeEventListener('abort', abortListener);
|
|
454
458
|
}
|
|
459
|
+
}
|
|
455
460
|
}
|
|
456
461
|
// NitroHeaders is now imported from './Headers'
|
|
457
462
|
async function nitroStreamFetch(input, init) {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
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
|
-
|
|
463
|
+
const url = typeof input === 'string' ? input : String(input);
|
|
464
|
+
const method = init?.method?.toUpperCase() ?? 'GET';
|
|
465
|
+
const headers = headersToPairs(init?.headers);
|
|
466
|
+
// Inspector: record start
|
|
467
|
+
let inspectorId;
|
|
468
|
+
if (NetworkInspector.isEnabled()) {
|
|
469
|
+
inspectorId = String(Date.now()) + '-' + String(Math.random()).slice(2, 8);
|
|
470
|
+
NetworkInspector._recordStart(
|
|
471
|
+
inspectorId,
|
|
472
|
+
url,
|
|
473
|
+
method,
|
|
474
|
+
headers ?? [],
|
|
475
|
+
typeof init?.body === 'string' ? init.body : undefined
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
const builder = NitroCronetSingleton.newUrlRequestBuilder(url);
|
|
479
|
+
builder.setHttpMethod(method);
|
|
480
|
+
headers?.forEach((h) => builder.addHeader(h.key, h.value));
|
|
481
|
+
const body = init?.body;
|
|
482
|
+
if (body != null) {
|
|
483
|
+
if (typeof body === 'string') builder.setUploadBody(body);
|
|
484
|
+
else if (body instanceof ArrayBuffer) builder.setUploadBody(body);
|
|
485
|
+
}
|
|
486
|
+
return new Promise((resolveResponse, rejectResponse) => {
|
|
487
|
+
let streamController;
|
|
488
|
+
const stream = new ReadableStream({
|
|
489
|
+
start(controller) {
|
|
490
|
+
streamController = controller;
|
|
491
|
+
},
|
|
492
|
+
});
|
|
493
|
+
let responseResolved = false;
|
|
494
|
+
let streamBytesReceived = 0;
|
|
495
|
+
builder.onResponseStarted((info) => {
|
|
496
|
+
if (responseResolved) return;
|
|
497
|
+
responseResolved = true;
|
|
498
|
+
const status = info.httpStatusCode;
|
|
499
|
+
const responseHeaders = new NitroHeaders(
|
|
500
|
+
Object.entries(info.allHeaders).map(([key, value]) => ({ key, value }))
|
|
501
|
+
);
|
|
502
|
+
const response = new NitroResponse({
|
|
503
|
+
url: info.url,
|
|
504
|
+
ok: status >= 200 && status < 300,
|
|
505
|
+
status,
|
|
506
|
+
statusText: info.httpStatusText,
|
|
507
|
+
headers: responseHeaders,
|
|
508
|
+
redirected: false,
|
|
509
|
+
body: stream,
|
|
510
|
+
});
|
|
511
|
+
resolveResponse(response);
|
|
512
|
+
// Android/Cronet: kick off the first buffer read.
|
|
513
|
+
// iOS/URLSession handles reading automatically so this is a no-op there.
|
|
514
|
+
request.read();
|
|
515
|
+
});
|
|
516
|
+
builder.onReadCompleted((_info, byteBuffer, bytesRead) => {
|
|
517
|
+
const chunk = new Uint8Array(byteBuffer, 0, bytesRead).slice();
|
|
518
|
+
streamBytesReceived += bytesRead;
|
|
519
|
+
streamController.enqueue(chunk);
|
|
520
|
+
if (!request.isDone()) {
|
|
521
|
+
request.read();
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
builder.onSucceeded((_info) => {
|
|
525
|
+
streamController.close();
|
|
526
|
+
if (inspectorId) {
|
|
527
|
+
const info = _info;
|
|
528
|
+
const status = info?.httpStatusCode ?? 0;
|
|
529
|
+
const hdrs = info?.allHeadersAsList ?? [];
|
|
530
|
+
NetworkInspector._recordEnd(
|
|
531
|
+
inspectorId,
|
|
532
|
+
status,
|
|
533
|
+
info?.httpStatusText ?? '',
|
|
534
|
+
hdrs,
|
|
535
|
+
streamBytesReceived
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
builder.onFailed((_info, error) => {
|
|
540
|
+
const err = new Error(error.message);
|
|
541
|
+
if (inspectorId) {
|
|
542
|
+
NetworkInspector._recordEnd(inspectorId, 0, '', [], 0, error.message);
|
|
543
|
+
}
|
|
544
|
+
if (!responseResolved) {
|
|
545
|
+
responseResolved = true;
|
|
546
|
+
rejectResponse(err);
|
|
547
|
+
} else {
|
|
548
|
+
streamController.error(err);
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
builder.onCanceled(() => {
|
|
552
|
+
const err = createAbortError();
|
|
553
|
+
if (inspectorId) {
|
|
554
|
+
NetworkInspector._recordEnd(
|
|
555
|
+
inspectorId,
|
|
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
|
+
}
|
|
551
569
|
});
|
|
570
|
+
const request = builder.build();
|
|
571
|
+
request.start();
|
|
572
|
+
});
|
|
552
573
|
}
|
|
553
574
|
export async function nitroFetch(input, init) {
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
575
|
+
// Merge defaults from NitroRequestClass if input is one
|
|
576
|
+
if (input instanceof NitroRequestClass) {
|
|
577
|
+
init = {
|
|
578
|
+
...init,
|
|
579
|
+
signal: init?.signal ?? input.signal,
|
|
580
|
+
redirect: init?.redirect ?? input.redirect,
|
|
581
|
+
cache: init?.cache ?? input.cache,
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
if (init?.stream === true) {
|
|
585
|
+
return nitroStreamFetch(input, init);
|
|
586
|
+
}
|
|
587
|
+
const redirectOption = init?.redirect ?? 'follow';
|
|
588
|
+
const res = await nitroFetchRaw(input, init);
|
|
589
|
+
// Handle redirect: "error" — if we got a 3xx back (followRedirects was false), throw
|
|
590
|
+
if (redirectOption === 'error' && res.status >= 300 && res.status < 400) {
|
|
591
|
+
throw new TypeError(
|
|
592
|
+
`redirect mode is "error": redirected request to "${res.url}"`
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
const response = new NitroResponse({
|
|
596
|
+
url: res.url,
|
|
597
|
+
status: res.status,
|
|
598
|
+
statusText: res.statusText,
|
|
599
|
+
ok: res.ok,
|
|
600
|
+
redirected: res.redirected,
|
|
601
|
+
headers: res.headers,
|
|
602
|
+
bodyBytes: res.bodyBytes,
|
|
603
|
+
bodyString: res.bodyString,
|
|
604
|
+
});
|
|
605
|
+
return response;
|
|
583
606
|
}
|
|
584
607
|
// Start a native prefetch. Requires a `prefetchKey` header on the request.
|
|
585
608
|
export async function prefetch(input, init) {
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
+
// If native implementation is not present yet, do nothing
|
|
610
|
+
const hasNative = typeof NitroFetchHybrid?.createClient === 'function';
|
|
611
|
+
if (!hasNative) return;
|
|
612
|
+
// Build NitroRequest and ensure prefetchKey header exists
|
|
613
|
+
const req = buildNitroRequest(input, init);
|
|
614
|
+
const hasKey =
|
|
615
|
+
req.headers?.some((h) => h.key.toLowerCase() === 'prefetchkey') ?? false;
|
|
616
|
+
// Also support passing prefetchKey via non-standard field on init
|
|
617
|
+
const fromInit = init?.prefetchKey;
|
|
618
|
+
if (!hasKey && fromInit) {
|
|
619
|
+
req.headers = (req.headers ?? []).concat([
|
|
620
|
+
{ key: 'prefetchKey', value: fromInit },
|
|
621
|
+
]);
|
|
622
|
+
}
|
|
623
|
+
const finalHasKey = req.headers?.some(
|
|
624
|
+
(h) => h.key.toLowerCase() === 'prefetchkey'
|
|
625
|
+
);
|
|
626
|
+
if (!finalHasKey) {
|
|
627
|
+
throw new Error('prefetch requires a "prefetchKey" header');
|
|
628
|
+
}
|
|
629
|
+
// Ensure client and call native prefetch
|
|
630
|
+
ensureClient();
|
|
631
|
+
if (!client || typeof client.prefetch !== 'function') return;
|
|
632
|
+
await client.prefetch(req);
|
|
609
633
|
}
|
|
634
|
+
const AUTOPREFETCH_QUEUE_KEY = 'nitrofetch_autoprefetch_queue';
|
|
610
635
|
// Persist a request to storage so native can prefetch it on app start.
|
|
611
636
|
export async function prefetchOnAppStart(input, init) {
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
637
|
+
// Resolve request and prefetchKey
|
|
638
|
+
const req = buildNitroRequest(input, init);
|
|
639
|
+
const fromHeader = req.headers?.find(
|
|
640
|
+
(h) => h.key.toLowerCase() === 'prefetchkey'
|
|
641
|
+
)?.value;
|
|
642
|
+
const fromInit = init?.prefetchKey;
|
|
643
|
+
const prefetchKey = fromHeader ?? fromInit;
|
|
644
|
+
if (!prefetchKey) {
|
|
645
|
+
throw new Error(
|
|
646
|
+
'prefetchOnAppStart requires a "prefetchKey" (header or init.prefetchKey)'
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
// Convert headers to a plain object for storage
|
|
650
|
+
const headersObj = (req.headers ?? []).reduce((acc, { key, value }) => {
|
|
651
|
+
acc[String(key)] = String(value);
|
|
652
|
+
return acc;
|
|
653
|
+
}, {});
|
|
654
|
+
const entry = {
|
|
655
|
+
url: req.url,
|
|
656
|
+
prefetchKey,
|
|
657
|
+
headers: headersObj,
|
|
658
|
+
};
|
|
659
|
+
if (req.method && req.method !== 'GET') entry.method = req.method;
|
|
660
|
+
if (req.bodyString !== undefined) entry.bodyString = req.bodyString;
|
|
661
|
+
if (typeof req.bodyBytes === 'string' && req.bodyBytes.length > 0)
|
|
662
|
+
entry.bodyBytes = req.bodyBytes;
|
|
663
|
+
if (req.bodyFormData && req.bodyFormData.length > 0)
|
|
664
|
+
entry.bodyFormData = req.bodyFormData;
|
|
665
|
+
if (typeof req.timeoutMs === 'number') entry.timeoutMs = req.timeoutMs;
|
|
666
|
+
if (req.followRedirects === false) entry.followRedirects = false;
|
|
667
|
+
if (typeof req.prefetchCacheTtlMs === 'number')
|
|
668
|
+
entry.prefetchCacheTtlMs = req.prefetchCacheTtlMs;
|
|
669
|
+
// Write or append to storage queue
|
|
670
|
+
try {
|
|
671
|
+
let arr = [];
|
|
645
672
|
try {
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
}
|
|
664
|
-
catch (e) {
|
|
665
|
-
console.warn('Failed to persist prefetch queue', e);
|
|
666
|
-
}
|
|
673
|
+
const raw = NativeStorageSingleton.getString(AUTOPREFETCH_QUEUE_KEY);
|
|
674
|
+
if (raw) arr = JSON.parse(raw);
|
|
675
|
+
if (!Array.isArray(arr)) arr = [];
|
|
676
|
+
} catch {
|
|
677
|
+
arr = [];
|
|
678
|
+
}
|
|
679
|
+
if (arr.some((e) => e && e.prefetchKey === prefetchKey)) {
|
|
680
|
+
arr = arr.filter((e) => e && e.prefetchKey !== prefetchKey);
|
|
681
|
+
}
|
|
682
|
+
arr.push(entry);
|
|
683
|
+
NativeStorageSingleton.setString(
|
|
684
|
+
AUTOPREFETCH_QUEUE_KEY,
|
|
685
|
+
JSON.stringify(arr)
|
|
686
|
+
);
|
|
687
|
+
} catch (e) {
|
|
688
|
+
console.warn('Failed to persist prefetch queue', e);
|
|
689
|
+
}
|
|
667
690
|
}
|
|
668
691
|
// Remove one entry (by prefetchKey) from the auto-prefetch queue.
|
|
669
692
|
export async function removeFromAutoPrefetch(prefetchKey) {
|
|
693
|
+
try {
|
|
694
|
+
let arr = [];
|
|
670
695
|
try {
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
catch (e) {
|
|
692
|
-
console.warn('Failed to remove from prefetch queue', e);
|
|
693
|
-
}
|
|
696
|
+
const raw = NativeStorageSingleton.getString(AUTOPREFETCH_QUEUE_KEY);
|
|
697
|
+
if (raw) arr = JSON.parse(raw);
|
|
698
|
+
if (!Array.isArray(arr)) arr = [];
|
|
699
|
+
} catch {
|
|
700
|
+
arr = [];
|
|
701
|
+
}
|
|
702
|
+
const next = arr.filter((e) => e && e.prefetchKey !== prefetchKey);
|
|
703
|
+
if (next.length === 0) {
|
|
704
|
+
NativeStorageSingleton.removeString(AUTOPREFETCH_QUEUE_KEY);
|
|
705
|
+
} else if (next.length !== arr.length) {
|
|
706
|
+
NativeStorageSingleton.setString(
|
|
707
|
+
AUTOPREFETCH_QUEUE_KEY,
|
|
708
|
+
JSON.stringify(next)
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
} catch (e) {
|
|
712
|
+
console.warn('Failed to remove from prefetch queue', e);
|
|
713
|
+
}
|
|
694
714
|
}
|
|
695
715
|
// Remove all entries from the auto-prefetch queue.
|
|
696
716
|
export async function removeAllFromAutoprefetch() {
|
|
697
|
-
|
|
698
|
-
NativeStorageSingleton.setString(KEY, JSON.stringify([]));
|
|
717
|
+
NativeStorageSingleton.setString(AUTOPREFETCH_QUEUE_KEY, JSON.stringify([]));
|
|
699
718
|
}
|
|
700
719
|
export function __readAutoPrefetchQueue() {
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
return [];
|
|
710
|
-
}
|
|
720
|
+
try {
|
|
721
|
+
const raw = NativeStorageSingleton.getString(AUTOPREFETCH_QUEUE_KEY);
|
|
722
|
+
if (!raw) return [];
|
|
723
|
+
const parsed = JSON.parse(raw);
|
|
724
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
725
|
+
} catch {
|
|
726
|
+
return [];
|
|
727
|
+
}
|
|
711
728
|
}
|
|
712
729
|
let nitroRuntime;
|
|
713
730
|
function ensureWorkletRuntime(name = 'nitro-fetch') {
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
}
|
|
731
|
+
try {
|
|
732
|
+
const { createWorkletRuntime } = require('react-native-worklets');
|
|
733
|
+
nitroRuntime = nitroRuntime ?? createWorkletRuntime(name);
|
|
734
|
+
return nitroRuntime;
|
|
735
|
+
} catch {
|
|
736
|
+
console.warn('react-native-worklets not available');
|
|
737
|
+
return undefined;
|
|
738
|
+
}
|
|
723
739
|
}
|
|
724
740
|
export async function nitroFetchOnWorklet(input, init, mapWorklet, options) {
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
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
|
-
});
|
|
741
|
+
const preferBytes = options?.preferBytes === true; // default true
|
|
742
|
+
let runOnRuntimeAsync;
|
|
743
|
+
let rt;
|
|
744
|
+
try {
|
|
745
|
+
rt = ensureWorkletRuntime(options?.runtimeName);
|
|
746
|
+
const worklets = require('react-native-worklets');
|
|
747
|
+
runOnRuntimeAsync = worklets.runOnRuntimeAsync;
|
|
748
|
+
} catch {
|
|
749
|
+
// Module not available
|
|
750
|
+
}
|
|
751
|
+
// Fallback: if runtime is not available, do the work on JS
|
|
752
|
+
if (!runOnRuntimeAsync || !rt) {
|
|
753
|
+
console.warn('nitroFetchOnWorklet: no runtime, mapping on JS thread');
|
|
754
|
+
const res = await nitroFetchRaw(input, init);
|
|
755
|
+
const payload = {
|
|
756
|
+
url: res.url,
|
|
757
|
+
status: res.status,
|
|
758
|
+
statusText: res.statusText,
|
|
759
|
+
ok: res.ok,
|
|
760
|
+
redirected: res.redirected,
|
|
761
|
+
headers: res.headers,
|
|
762
|
+
bodyBytes: preferBytes ? res.bodyBytes : undefined,
|
|
763
|
+
bodyString: preferBytes ? undefined : res.bodyString,
|
|
764
|
+
};
|
|
765
|
+
return mapWorklet(payload);
|
|
766
|
+
}
|
|
767
|
+
return await runOnRuntimeAsync(rt, () => {
|
|
768
|
+
'worklet';
|
|
769
|
+
const nitroFetchClient = NitroFetchHybrid.createClient();
|
|
770
|
+
const request = buildNitroRequestPure(input, init);
|
|
771
|
+
const res = nitroFetchClient.requestSync(request);
|
|
772
|
+
const payload = {
|
|
773
|
+
url: res.url,
|
|
774
|
+
status: res.status,
|
|
775
|
+
statusText: res.statusText,
|
|
776
|
+
ok: res.ok,
|
|
777
|
+
redirected: res.redirected,
|
|
778
|
+
headers: res.headers,
|
|
779
|
+
bodyBytes: preferBytes ? res.bodyBytes : undefined,
|
|
780
|
+
bodyString: preferBytes ? undefined : res.bodyString,
|
|
781
|
+
};
|
|
782
|
+
return mapWorklet(payload);
|
|
783
|
+
});
|
|
769
784
|
}
|
|
770
785
|
export { NitroHeaders } from './Headers';
|
|
771
786
|
export { NitroResponse } from './Response';
|