forked from margelo/react-native-nitro-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.ts
More file actions
601 lines (545 loc) · 17.9 KB
/
fetch.ts
File metadata and controls
601 lines (545 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
276
277
278
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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
551
552
553
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
import type {
NitroFetch as NitroFetchModule,
NitroHeader,
NitroRequest,
NitroResponse,
} from './NitroFetch.nitro';
import {
boxedNitroFetch,
NitroFetch as NitroFetchSingleton,
} from './NitroInstances';
import { NativeStorage as NativeStorageSingleton } from './NitroInstances';
// No base64: pass strings/ArrayBuffers directly
function headersToPairs(headers?: HeadersInit): NitroHeader[] | undefined {
'worklet';
if (!headers) return undefined;
const pairs: NitroHeader[] = [];
if (headers instanceof Headers) {
headers.forEach((v, k) => pairs.push({ key: k, value: v }));
return pairs;
}
if (Array.isArray(headers)) {
// Convert tuple pairs to objects if needed
for (const entry of headers as any[]) {
if (Array.isArray(entry) && entry.length >= 2) {
pairs.push({ key: String(entry[0]), value: String(entry[1]) });
} else if (
entry &&
typeof entry === 'object' &&
'key' in entry &&
'value' in entry
) {
pairs.push(entry as NitroHeader);
}
}
return pairs;
}
// Check if it's a plain object (Record<string, string>) first
// Plain objects don't have forEach, so check for its absence
if (typeof headers === 'object' && headers !== null) {
// Check if it's a Headers instance by checking for forEach method
const hasForEach = typeof (headers as any).forEach === 'function';
if (hasForEach) {
// Headers-like object (duck typing)
(headers as any).forEach((v: string, k: string) =>
pairs.push({ key: k, value: v })
);
return pairs;
} else {
// Plain object (Record<string, string>)
// Use Object.keys to iterate since Object.entries might not work in worklets
const keys = Object.keys(headers);
for (let i = 0; i < keys.length; i++) {
const k = keys[i];
const v = (headers as Record<string, string>)[k];
if (v !== undefined) {
pairs.push({ key: k, value: String(v) });
}
}
return pairs;
}
}
return pairs;
}
function normalizeBody(
body: BodyInit | null | undefined
): { bodyString?: string; bodyBytes?: ArrayBuffer } | undefined {
'worklet';
if (body == null) return undefined;
if (typeof body === 'string') return { bodyString: body };
if (body instanceof URLSearchParams) return { bodyString: body.toString() };
if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer)
return { bodyBytes: body };
if (ArrayBuffer.isView(body)) {
const view = body as ArrayBufferView;
// Pass a copy/slice of the underlying bytes without base64
return {
//@ts-ignore
bodyBytes: view.buffer.slice(
view.byteOffset,
view.byteOffset + view.byteLength
),
};
}
// TODO: Blob/FormData support can be added later
throw new Error('Unsupported body type for nitro fetch');
}
const NitroFetchHybrid: NitroFetchModule = NitroFetchSingleton;
let client: ReturnType<NitroFetchModule['createClient']> | undefined;
function ensureClient() {
if (client) return client;
try {
client = NitroFetchHybrid.createClient();
} catch (err) {
console.error('Failed to create NitroFetch client', err);
// native not ready; keep undefined
}
return client;
}
function buildNitroRequest(
input: RequestInfo | URL,
init?: RequestInit
): NitroRequest {
'worklet';
let url: string;
let method: string | undefined;
let headersInit: HeadersInit | undefined;
let body: BodyInit | null | undefined;
if (typeof input === 'string' || input instanceof URL) {
url = String(input);
method = init?.method;
headersInit = init?.headers;
body = init?.body ?? null;
} else {
// Request object
url = input.url;
method = input.method;
headersInit = input.headers as any;
// Clone body if needed – Request objects in RN typically allow direct access
body = init?.body ?? null;
}
const headers = headersToPairs(headersInit);
const normalized = normalizeBody(body);
return {
url,
method: (method?.toUpperCase() as any) ?? 'GET',
headers,
bodyString: normalized?.bodyString,
// Only include bodyBytes when provided to avoid signaling upload data unintentionally
bodyBytes: undefined as any,
followRedirects: true,
};
}
// Pure JS version of buildNitroRequest that doesnt use anything that breaks worklets. TODO: Merge this to use Same logic for Worklets and normal Fetch
function headersToPairsPure(headers?: HeadersInit): NitroHeader[] | undefined {
'worklet';
if (!headers) return undefined;
const pairs: NitroHeader[] = [];
if (Array.isArray(headers)) {
// Convert tuple pairs to objects if needed
for (const entry of headers as any[]) {
if (Array.isArray(entry) && entry.length >= 2) {
pairs.push({ key: String(entry[0]), value: String(entry[1]) });
} else if (
entry &&
typeof entry === 'object' &&
'key' in entry &&
'value' in entry
) {
pairs.push(entry as NitroHeader);
}
}
return pairs;
}
// Check if it's a plain object (Record<string, string>) first
// Plain objects don't have forEach, so check for its absence
if (typeof headers === 'object' && headers !== null) {
// Check if it's a Headers instance by checking for forEach method
const hasForEach = typeof (headers as any).forEach === 'function';
if (hasForEach) {
// Headers-like object (duck typing)
(headers as any).forEach((v: string, k: string) =>
pairs.push({ key: k, value: v })
);
return pairs;
} else {
// Plain object (Record<string, string>)
// Use Object.keys to iterate since Object.entries might not work in worklets
const keys = Object.keys(headers);
for (let i = 0; i < keys.length; i++) {
const k = keys[i];
const v = (headers as Record<string, string>)[k];
if (v !== undefined) {
pairs.push({ key: k, value: String(v) });
}
}
return pairs;
}
}
return pairs;
}
// Pure JS version of buildNitroRequest that doesnt use anything that breaks worklets
function normalizeBodyPure(
body: BodyInit | null | undefined
): { bodyString?: string; bodyBytes?: ArrayBuffer } | undefined {
'worklet';
if (body == null) return undefined;
if (typeof body === 'string') return { bodyString: body };
// Check for URLSearchParams (duck typing)
// It should be an object, have a toString method, and typically append/delete methods
// But mainly we care about toString() returning the query string
if (
typeof body === 'object' &&
body !== null &&
typeof (body as any).toString === 'function' &&
Object.prototype.toString.call(body) === '[object URLSearchParams]'
) {
return { bodyString: body.toString() };
}
// Check for ArrayBuffer (using toString tag to avoid instanceof)
if (
typeof ArrayBuffer !== 'undefined' &&
Object.prototype.toString.call(body) === '[object ArrayBuffer]'
) {
return { bodyBytes: body as ArrayBuffer };
}
if (ArrayBuffer.isView(body)) {
const view = body as ArrayBufferView;
// Pass a copy/slice of the underlying bytes without base64
return {
//@ts-ignore
bodyBytes: view.buffer.slice(
view.byteOffset,
view.byteOffset + view.byteLength
),
};
}
// TODO: Blob/FormData support can be added later
throw new Error('Unsupported body type for nitro fetch');
}
// Pure JS version of buildNitroRequest that doesnt use anything that breaks worklets
export function buildNitroRequestPure(
input: RequestInfo | URL,
init?: RequestInit
): NitroRequest {
'worklet';
let url: string;
let method: string | undefined;
let headersInit: HeadersInit | undefined;
let body: BodyInit | null | undefined;
// Check if input is URL-like without instanceof
const isUrlObject =
typeof input === 'object' &&
input !== null &&
Object.prototype.toString.call(input) === '[object URL]';
if (typeof input === 'string' || isUrlObject) {
url = String(input);
method = init?.method;
headersInit = init?.headers;
body = init?.body ?? null;
} else {
// Request object
const req = input as Request;
url = req.url;
method = req.method;
headersInit = req.headers;
// Clone body if needed – Request objects in RN typically allow direct access
body = init?.body ?? null;
}
const headers = headersToPairsPure(headersInit);
const normalized = normalizeBodyPure(body);
return {
url,
method: (method?.toUpperCase() as any) ?? 'GET',
headers,
bodyString: normalized?.bodyString,
// Only include bodyBytes when provided to avoid signaling upload data unintentionally
bodyBytes: undefined as any,
followRedirects: true,
};
}
async function nitroFetchRaw(
input: RequestInfo | URL,
init?: RequestInit
): Promise<NitroResponse> {
const hasNative =
typeof (NitroFetchHybrid as any)?.createClient === 'function';
if (!hasNative) {
// Fallback path not supported for raw; use global fetch and synthesize minimal shape
// @ts-ignore: global fetch exists in RN
const res = await fetch(input as any, init);
const url = (res as any).url ?? String(input);
const bytes = await res.arrayBuffer();
const headers: NitroHeader[] = [];
res.headers.forEach((v, k) => headers.push({ key: k, value: v }));
return {
url,
status: res.status,
statusText: res.statusText,
ok: res.ok,
redirected: (res as any).redirected ?? false,
headers,
bodyBytes: bytes,
bodyString: undefined,
} as any as NitroResponse; // bleee
}
const req = buildNitroRequest(input, init);
ensureClient();
if (!client || typeof (client as any).request !== 'function')
throw new Error('NitroFetch client not available');
const res: NitroResponse = await client.request(req);
return res;
}
// Simple Headers-like class that supports get() method
class NitroHeaders {
private _headers: Map<string, string>;
constructor(headers: NitroHeader[]) {
this._headers = new Map();
for (const { key, value } of headers) {
// Headers are case-insensitive, normalize to lowercase
this._headers.set(key.toLowerCase(), value);
}
}
get(name: string): string | null {
return this._headers.get(name.toLowerCase()) ?? null;
}
has(name: string): boolean {
return this._headers.has(name.toLowerCase());
}
forEach(callback: (value: string, key: string) => void): void {
this._headers.forEach(callback);
}
entries(): IterableIterator<[string, string]> {
return this._headers.entries();
}
keys(): IterableIterator<string> {
return this._headers.keys();
}
values(): IterableIterator<string> {
return this._headers.values();
}
}
export async function nitroFetch(
input: RequestInfo | URL,
init?: RequestInit
): Promise<Response> {
const res = await nitroFetchRaw(input, init);
const headersObj = new NitroHeaders(res.headers);
const bodyBytes = res.bodyBytes;
const bodyString = res.bodyString;
const makeLight = (): any => ({
url: res.url,
ok: res.ok,
status: res.status,
statusText: res.statusText,
redirected: res.redirected,
headers: headersObj,
arrayBuffer: async () => bodyBytes,
text: async () => bodyString,
json: async () => JSON.parse(bodyString ?? '{}'),
clone: () => makeLight(),
});
const light: any = makeLight();
return light as Response;
}
// Start a native prefetch. Requires a `prefetchKey` header on the request.
export async function prefetch(
input: RequestInfo | URL,
init?: RequestInit
): Promise<void> {
// If native implementation is not present yet, do nothing
const hasNative =
typeof (NitroFetchHybrid as any)?.createClient === 'function';
if (!hasNative) return;
// Build NitroRequest and ensure prefetchKey header exists
const req = buildNitroRequest(input, init);
const hasKey =
req.headers?.some((h) => h.key.toLowerCase() === 'prefetchkey') ?? false;
// Also support passing prefetchKey via non-standard field on init
const fromInit = (init as any)?.prefetchKey as string | undefined;
if (!hasKey && fromInit) {
req.headers = (req.headers ?? []).concat([
{ key: 'prefetchKey', value: fromInit },
]);
}
const finalHasKey = req.headers?.some(
(h) => h.key.toLowerCase() === 'prefetchkey'
);
if (!finalHasKey) {
throw new Error('prefetch requires a "prefetchKey" header');
}
// Ensure client and call native prefetch
ensureClient();
if (!client || typeof (client as any).prefetch !== 'function') return;
await client.prefetch(req);
}
// Persist a request to storage so native can prefetch it on app start.
export async function prefetchOnAppStart(
input: RequestInfo | URL,
init?: RequestInit & { prefetchKey?: string }
): Promise<void> {
// Resolve request and prefetchKey
const req = buildNitroRequest(input, init);
const fromHeader = req.headers?.find(
(h) => h.key.toLowerCase() === 'prefetchkey'
)?.value;
const fromInit = (init as any)?.prefetchKey as string | undefined;
const prefetchKey = fromHeader ?? fromInit;
if (!prefetchKey) {
throw new Error(
'prefetchOnAppStart requires a "prefetchKey" (header or init.prefetchKey)'
);
}
// Convert headers to a plain object for storage
const headersObj = (req.headers ?? []).reduce(
(acc, { key, value }) => {
acc[String(key)] = String(value);
return acc;
},
{} as Record<string, string>
);
const entry = {
url: req.url,
prefetchKey,
headers: headersObj,
} as const;
// Write or append to storage queue
try {
const KEY = 'nitrofetch_autoprefetch_queue';
let arr: any[] = [];
try {
const raw = NativeStorageSingleton.getString(
'nitrofetch_autoprefetch_queue'
);
if (raw) arr = JSON.parse(raw);
if (!Array.isArray(arr)) arr = [];
} catch {
arr = [];
}
if (arr.some((e) => e && e.prefetchKey === prefetchKey)) {
arr = arr.filter((e) => e && e.prefetchKey !== prefetchKey);
}
arr.push(entry);
NativeStorageSingleton.setString(KEY, JSON.stringify(arr));
} catch (e) {
console.warn('Failed to persist prefetch queue', e);
}
}
// Remove one entry (by prefetchKey) from the auto-prefetch queue.
export async function removeFromAutoPrefetch(
prefetchKey: string
): Promise<void> {
try {
const KEY = 'nitrofetch_autoprefetch_queue';
let arr: any[] = [];
try {
const raw = NativeStorageSingleton.getString(
'nitrofetch_autoprefetch_queue'
);
if (raw) arr = JSON.parse(raw);
if (!Array.isArray(arr)) arr = [];
} catch {
arr = [];
}
const next = arr.filter((e) => e && e.prefetchKey !== prefetchKey);
if (next.length === 0) {
NativeStorageSingleton.removeString(KEY);
} else if (next.length !== arr.length) {
NativeStorageSingleton.setString(KEY, JSON.stringify(next));
}
} catch (e) {
console.warn('Failed to remove from prefetch queue', e);
}
}
// Remove all entries from the auto-prefetch queue.
export async function removeAllFromAutoprefetch(): Promise<void> {
const KEY = 'nitrofetch_autoprefetch_queue';
NativeStorageSingleton.setString(KEY, JSON.stringify([]));
}
// Optional off-thread processing using react-native-worklets-core
export type NitroWorkletMapper<T> = (payload: {
url: string;
status: number;
statusText: string;
ok: boolean;
redirected: boolean;
headers: NitroHeader[];
bodyBytes?: ArrayBuffer;
bodyString?: string;
}) => T;
let nitroRuntime: any | undefined;
let WorkletsRef: any | undefined;
function ensureWorkletRuntime(name = 'nitro-fetch'): any | undefined {
try {
const { Worklets } = require('react-native-worklets-core');
nitroRuntime = nitroRuntime ?? Worklets.createContext(name);
return nitroRuntime;
} catch {
console.warn('react-native-worklets-core not available');
return undefined;
}
}
function getWorklets(): any | undefined {
try {
if (WorkletsRef) return WorkletsRef;
const { Worklets } = require('react-native-worklets-core');
WorkletsRef = Worklets;
return WorkletsRef;
} catch {
console.warn('react-native-worklets-core not available');
return undefined;
}
}
export async function nitroFetchOnWorklet<T>(
input: RequestInfo | URL,
init: RequestInit | undefined,
mapWorklet: NitroWorkletMapper<T>,
options?: { preferBytes?: boolean; runtimeName?: string }
): Promise<T> {
const preferBytes = options?.preferBytes === true; // default true
let rt: any | undefined;
let Worklets: any | undefined;
try {
rt = ensureWorkletRuntime(options?.runtimeName);
Worklets = getWorklets();
} catch (e) {
console.error('nitroFetchOnWorklet: setup failed', e);
}
// Fallback: if runtime is not available, do the work on JS
if (!rt || !Worklets || typeof rt.runAsync !== 'function') {
console.warn('nitroFetchOnWorklet: no runtime, mapping on JS thread');
const res = await nitroFetchRaw(input, init);
const payload = {
url: res.url,
status: res.status,
statusText: res.statusText,
ok: res.ok,
redirected: res.redirected,
headers: res.headers,
bodyBytes: preferBytes ? res.bodyBytes : undefined,
bodyString: preferBytes ? undefined : res.bodyString,
} as const;
return mapWorklet(payload as any);
}
return await rt.runAsync(() => {
'worklet';
const unboxedNitroFetch = boxedNitroFetch.unbox();
const unboxedClient = unboxedNitroFetch.createClient();
const request = buildNitroRequestPure(input, init);
const res = unboxedClient.requestSync(request);
const payload = {
url: res.url,
status: res.status,
statusText: res.statusText,
ok: res.ok,
redirected: res.redirected,
headers: res.headers,
bodyBytes: preferBytes ? res.bodyBytes : undefined,
bodyString: preferBytes ? undefined : res.bodyString,
} as const;
return mapWorklet(payload as any);
});
}
export const x = ensureWorkletRuntime();
export const y = getWorklets();
export type { NitroRequest, NitroResponse } from './NitroFetch.nitro';