react-native-nitro-fetch 1.2.1 → 1.3.1
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/src/main/java/com/margelo/nitro/nitrofetch/AutoPrefetcher.kt +114 -31
- package/android/src/main/java/com/margelo/nitro/nitrofetch/NitroFetchClient.kt +43 -6
- package/ios/NitroAutoPrefetcher.h +21 -0
- package/ios/NitroAutoPrefetcher.swift +149 -27
- package/ios/NitroFetchClient.swift +17 -6
- package/lib/module/CurlGenerator.js.map +1 -1
- package/lib/module/HermesProfiler.js.map +2 -1
- package/lib/module/NetworkInspector.js +0 -4
- package/lib/module/NetworkInspector.js.map +1 -1
- package/lib/module/NitroCronet.nitro.js.map +1 -1
- package/lib/module/NitroFetch.nitro.js.map +1 -0
- package/lib/module/NitroInstances.js.map +1 -1
- package/lib/module/Response.js +3 -4
- package/lib/module/Response.js.map +2 -1
- package/lib/module/fetch.js +26 -6
- package/lib/module/fetch.js.map +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +2 -1
- package/lib/module/index.web.js +1 -2
- package/lib/module/utf8.js +19 -8
- package/lib/module/utf8.js.map +2 -1
- package/lib/typescript/src/NitroFetch.nitro.d.ts +2 -1
- package/lib/typescript/src/NitroFetch.nitro.d.ts.map +1 -1
- package/lib/typescript/src/Response.d.ts.map +1 -1
- package/lib/typescript/src/fetch.d.ts +4 -1
- package/lib/typescript/src/fetch.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/utf8.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JHybridNitroFetchClientSpec.cpp +2 -0
- package/nitrogen/generated/android/c++/JNitroRequest.hpp +5 -1
- package/nitrogen/generated/android/c++/JNitroResponse.hpp +7 -5
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrofetch/NitroRequest.kt +5 -2
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrofetch/NitroResponse.kt +3 -3
- package/nitrogen/generated/ios/NitroFetch-Swift-Cxx-Bridge.hpp +15 -0
- package/nitrogen/generated/ios/c++/HybridNitroFetchClientSpecSwift.hpp +4 -0
- package/nitrogen/generated/ios/swift/NitroRequest.swift +19 -1
- package/nitrogen/generated/ios/swift/NitroResponse.swift +8 -8
- package/nitrogen/generated/shared/c++/NitroRequest.hpp +5 -1
- package/nitrogen/generated/shared/c++/NitroResponse.hpp +6 -5
- package/package.json +18 -3
- package/src/CurlGenerator.js +31 -0
- package/src/Headers.js +127 -0
- package/src/HermesProfiler.js +22 -0
- package/src/NetworkInspector.js +183 -0
- package/src/NitroCronet.nitro.js +1 -0
- package/src/NitroFetch.nitro.js +1 -0
- package/src/NitroFetch.nitro.ts +4 -2
- package/src/NitroInstances.js +6 -0
- package/src/Request.js +173 -0
- package/src/Response.js +258 -0
- package/src/Response.ts +2 -1
- package/src/fetch.js +772 -0
- package/src/fetch.ts +46 -9
- package/src/index.js +10 -0
- package/src/index.tsx +1 -0
- package/src/index.web.js +104 -0
- package/src/tokenRefresh.js +104 -0
- package/src/utf8.js +40 -0
- package/src/utf8.ts +29 -14
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { generateCurl } from './CurlGenerator';
|
|
2
|
+
class NetworkInspectorImpl {
|
|
3
|
+
_enabled = false;
|
|
4
|
+
_entries = [];
|
|
5
|
+
_maxEntries = 500;
|
|
6
|
+
_maxBodyCapture = 4096;
|
|
7
|
+
_listeners = new Set();
|
|
8
|
+
enable(options) {
|
|
9
|
+
this._enabled = true;
|
|
10
|
+
if (options?.maxEntries != null)
|
|
11
|
+
this._maxEntries = options.maxEntries;
|
|
12
|
+
if (options?.maxBodyCapture != null)
|
|
13
|
+
this._maxBodyCapture = options.maxBodyCapture;
|
|
14
|
+
}
|
|
15
|
+
disable() {
|
|
16
|
+
this._enabled = false;
|
|
17
|
+
}
|
|
18
|
+
isEnabled() {
|
|
19
|
+
return this._enabled;
|
|
20
|
+
}
|
|
21
|
+
getEntries() {
|
|
22
|
+
return this._entries;
|
|
23
|
+
}
|
|
24
|
+
getHttpEntries() {
|
|
25
|
+
return this._entries.filter((e) => e.type === 'http');
|
|
26
|
+
}
|
|
27
|
+
getWebSocketEntries() {
|
|
28
|
+
return this._entries.filter((e) => e.type === 'websocket');
|
|
29
|
+
}
|
|
30
|
+
getEntry(id) {
|
|
31
|
+
return this._entries.find((e) => e.id === id);
|
|
32
|
+
}
|
|
33
|
+
clear() {
|
|
34
|
+
this._entries = [];
|
|
35
|
+
}
|
|
36
|
+
onEntry(callback) {
|
|
37
|
+
this._listeners.add(callback);
|
|
38
|
+
return () => {
|
|
39
|
+
this._listeners.delete(callback);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
_notify(entry) {
|
|
43
|
+
for (const cb of this._listeners) {
|
|
44
|
+
try {
|
|
45
|
+
cb(entry);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// swallow listener errors
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
_trimEntries() {
|
|
53
|
+
if (this._entries.length > this._maxEntries) {
|
|
54
|
+
this._entries.shift();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// --- HTTP recording ---
|
|
58
|
+
_recordStart(id, url, method, headers, body) {
|
|
59
|
+
if (!this._enabled)
|
|
60
|
+
return;
|
|
61
|
+
const bodySize = body ? body.length : 0;
|
|
62
|
+
const entry = {
|
|
63
|
+
id,
|
|
64
|
+
type: 'http',
|
|
65
|
+
url,
|
|
66
|
+
method,
|
|
67
|
+
requestHeaders: headers.map((h) => ({ key: h.key, value: h.value })),
|
|
68
|
+
requestBody: body ? body.slice(0, this._maxBodyCapture) : undefined,
|
|
69
|
+
requestBodySize: bodySize,
|
|
70
|
+
status: 0,
|
|
71
|
+
statusText: '',
|
|
72
|
+
responseHeaders: [],
|
|
73
|
+
responseBodySize: 0,
|
|
74
|
+
startTime: performance.now(),
|
|
75
|
+
endTime: 0,
|
|
76
|
+
duration: 0,
|
|
77
|
+
curl: generateCurl({ url, method, headers, body }),
|
|
78
|
+
};
|
|
79
|
+
this._entries.push(entry);
|
|
80
|
+
this._trimEntries();
|
|
81
|
+
}
|
|
82
|
+
_recordEnd(id, status, statusText, headers, bodySize, error, responseBody) {
|
|
83
|
+
if (!this._enabled)
|
|
84
|
+
return;
|
|
85
|
+
const entry = this._entries.find((e) => e.id === id && e.type === 'http');
|
|
86
|
+
if (!entry)
|
|
87
|
+
return;
|
|
88
|
+
entry.status = status;
|
|
89
|
+
entry.statusText = statusText;
|
|
90
|
+
entry.responseHeaders = headers.map((h) => ({
|
|
91
|
+
key: h.key,
|
|
92
|
+
value: h.value,
|
|
93
|
+
}));
|
|
94
|
+
entry.responseBodySize = bodySize;
|
|
95
|
+
entry.endTime = performance.now();
|
|
96
|
+
entry.duration = entry.endTime - entry.startTime;
|
|
97
|
+
if (error)
|
|
98
|
+
entry.error = error;
|
|
99
|
+
if (responseBody != null) {
|
|
100
|
+
entry.responseBody = responseBody.slice(0, this._maxBodyCapture);
|
|
101
|
+
}
|
|
102
|
+
this._notify(entry);
|
|
103
|
+
}
|
|
104
|
+
// --- WebSocket recording ---
|
|
105
|
+
_recordWsOpen(id, url, protocols, headers) {
|
|
106
|
+
if (!this._enabled)
|
|
107
|
+
return;
|
|
108
|
+
const entry = {
|
|
109
|
+
id,
|
|
110
|
+
type: 'websocket',
|
|
111
|
+
url,
|
|
112
|
+
protocols,
|
|
113
|
+
requestHeaders: headers.map((h) => ({ key: h.key, value: h.value })),
|
|
114
|
+
startTime: performance.now(),
|
|
115
|
+
endTime: 0,
|
|
116
|
+
duration: 0,
|
|
117
|
+
readyState: 'CONNECTING',
|
|
118
|
+
messages: [],
|
|
119
|
+
messagesSent: 0,
|
|
120
|
+
messagesReceived: 0,
|
|
121
|
+
bytesSent: 0,
|
|
122
|
+
bytesReceived: 0,
|
|
123
|
+
};
|
|
124
|
+
this._entries.push(entry);
|
|
125
|
+
this._trimEntries();
|
|
126
|
+
this._notify(entry);
|
|
127
|
+
}
|
|
128
|
+
_recordWsConnected(id) {
|
|
129
|
+
if (!this._enabled)
|
|
130
|
+
return;
|
|
131
|
+
const entry = this._entries.find((e) => e.id === id && e.type === 'websocket');
|
|
132
|
+
if (!entry)
|
|
133
|
+
return;
|
|
134
|
+
entry.readyState = 'OPEN';
|
|
135
|
+
this._notify(entry);
|
|
136
|
+
}
|
|
137
|
+
_recordWsMessage(id, direction, data, size, isBinary) {
|
|
138
|
+
if (!this._enabled)
|
|
139
|
+
return;
|
|
140
|
+
const entry = this._entries.find((e) => e.id === id && e.type === 'websocket');
|
|
141
|
+
if (!entry)
|
|
142
|
+
return;
|
|
143
|
+
entry.messages.push({
|
|
144
|
+
direction,
|
|
145
|
+
data: data.slice(0, this._maxBodyCapture),
|
|
146
|
+
size,
|
|
147
|
+
isBinary,
|
|
148
|
+
timestamp: performance.now(),
|
|
149
|
+
});
|
|
150
|
+
if (direction === 'sent') {
|
|
151
|
+
entry.messagesSent++;
|
|
152
|
+
entry.bytesSent += size;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
entry.messagesReceived++;
|
|
156
|
+
entry.bytesReceived += size;
|
|
157
|
+
}
|
|
158
|
+
this._notify(entry);
|
|
159
|
+
}
|
|
160
|
+
_recordWsClose(id, code, reason) {
|
|
161
|
+
if (!this._enabled)
|
|
162
|
+
return;
|
|
163
|
+
const entry = this._entries.find((e) => e.id === id && e.type === 'websocket');
|
|
164
|
+
if (!entry)
|
|
165
|
+
return;
|
|
166
|
+
entry.readyState = 'CLOSED';
|
|
167
|
+
entry.closeCode = code;
|
|
168
|
+
entry.closeReason = reason;
|
|
169
|
+
entry.endTime = performance.now();
|
|
170
|
+
entry.duration = entry.endTime - entry.startTime;
|
|
171
|
+
this._notify(entry);
|
|
172
|
+
}
|
|
173
|
+
_recordWsError(id, error) {
|
|
174
|
+
if (!this._enabled)
|
|
175
|
+
return;
|
|
176
|
+
const entry = this._entries.find((e) => e.id === id && e.type === 'websocket');
|
|
177
|
+
if (!entry)
|
|
178
|
+
return;
|
|
179
|
+
entry.error = error;
|
|
180
|
+
this._notify(entry);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
export const NetworkInspector = new NetworkInspectorImpl();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/src/NitroFetch.nitro.ts
CHANGED
|
@@ -36,6 +36,9 @@ export interface NitroRequest {
|
|
|
36
36
|
// Controls
|
|
37
37
|
timeoutMs?: number;
|
|
38
38
|
followRedirects?: boolean; // default true
|
|
39
|
+
// Max age (ms) a prefetch cache entry is considered fresh at read time.
|
|
40
|
+
// Default 5000 when omitted. <= 0 disables cache hits.
|
|
41
|
+
prefetchCacheTtlMs?: number;
|
|
39
42
|
// Optional ID used for cancellation via cancelRequest()
|
|
40
43
|
requestId?: string;
|
|
41
44
|
}
|
|
@@ -47,9 +50,8 @@ export interface NitroResponse {
|
|
|
47
50
|
ok: boolean;
|
|
48
51
|
redirected: boolean;
|
|
49
52
|
headers: NitroHeader[];
|
|
50
|
-
// Body as either UTF-8 string or raw bytes (first implementation target)
|
|
51
53
|
bodyString?: string;
|
|
52
|
-
bodyBytes?:
|
|
54
|
+
bodyBytes?: ArrayBuffer;
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
export interface NitroFetchClient extends HybridObject<{
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
+
// Create singletons once per JS runtime
|
|
3
|
+
export const NitroFetch = NitroModules.createHybridObject('NitroFetch');
|
|
4
|
+
export const NativeStorage = NitroModules.createHybridObject('NativeStorage');
|
|
5
|
+
export const boxedNitroFetch = NitroModules.box(NitroFetch);
|
|
6
|
+
export const NitroCronetSingleton = NitroModules.createHybridObject('NitroCronet');
|
package/src/Request.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { NitroHeaders } from './Headers';
|
|
2
|
+
import { stringToUTF8, utf8ToString } from './utf8';
|
|
3
|
+
export class NitroRequest {
|
|
4
|
+
url;
|
|
5
|
+
method;
|
|
6
|
+
headers;
|
|
7
|
+
redirect;
|
|
8
|
+
signal;
|
|
9
|
+
cache;
|
|
10
|
+
credentials;
|
|
11
|
+
mode;
|
|
12
|
+
referrer;
|
|
13
|
+
referrerPolicy;
|
|
14
|
+
integrity;
|
|
15
|
+
keepalive;
|
|
16
|
+
destination;
|
|
17
|
+
_body;
|
|
18
|
+
_bodyUsed = false;
|
|
19
|
+
constructor(input, init) {
|
|
20
|
+
if (input instanceof NitroRequest) {
|
|
21
|
+
// Clone from another NitroRequest
|
|
22
|
+
this.url = input.url;
|
|
23
|
+
this.method = (init?.method ?? input.method).toUpperCase();
|
|
24
|
+
this.headers = new NitroHeaders(init?.headers
|
|
25
|
+
? init.headers instanceof NitroHeaders
|
|
26
|
+
? init.headers
|
|
27
|
+
: init.headers
|
|
28
|
+
: input.headers);
|
|
29
|
+
this.redirect = init?.redirect ?? input.redirect;
|
|
30
|
+
this.signal = init?.signal ?? input.signal;
|
|
31
|
+
this.cache = init?.cache ?? input.cache;
|
|
32
|
+
this.credentials = init?.credentials ?? input.credentials;
|
|
33
|
+
this.mode = init?.mode ?? input.mode;
|
|
34
|
+
this.referrer = init?.referrer ?? input.referrer;
|
|
35
|
+
this.referrerPolicy = init?.referrerPolicy ?? input.referrerPolicy;
|
|
36
|
+
this.integrity = init?.integrity ?? input.integrity;
|
|
37
|
+
this.keepalive = init?.keepalive ?? input.keepalive;
|
|
38
|
+
this._body = init?.body !== undefined ? (init.body ?? null) : input._body;
|
|
39
|
+
}
|
|
40
|
+
else if (typeof input === 'object' &&
|
|
41
|
+
input !== null &&
|
|
42
|
+
'url' in input &&
|
|
43
|
+
'method' in input &&
|
|
44
|
+
'headers' in input &&
|
|
45
|
+
!(input instanceof URL)) {
|
|
46
|
+
// Construct from a Request-like object (standard Request or duck-typed)
|
|
47
|
+
this.url = input.url;
|
|
48
|
+
this.method = (init?.method ?? input.method).toUpperCase();
|
|
49
|
+
this.headers = new NitroHeaders(init?.headers
|
|
50
|
+
? init.headers instanceof NitroHeaders
|
|
51
|
+
? init.headers
|
|
52
|
+
: init.headers
|
|
53
|
+
: input.headers);
|
|
54
|
+
this.redirect =
|
|
55
|
+
init?.redirect ?? input.redirect ?? 'follow';
|
|
56
|
+
this.signal = init?.signal ?? input.signal;
|
|
57
|
+
this.cache = init?.cache ?? input.cache ?? 'default';
|
|
58
|
+
this.credentials =
|
|
59
|
+
init?.credentials ?? input.credentials ?? 'same-origin';
|
|
60
|
+
this.mode = init?.mode ?? input.mode ?? 'cors';
|
|
61
|
+
this.referrer = init?.referrer ?? input.referrer ?? 'about:client';
|
|
62
|
+
this.referrerPolicy =
|
|
63
|
+
init?.referrerPolicy ?? input.referrerPolicy ?? '';
|
|
64
|
+
this.integrity = init?.integrity ?? input.integrity ?? '';
|
|
65
|
+
this.keepalive = init?.keepalive ?? input.keepalive ?? false;
|
|
66
|
+
this._body = init?.body ?? null;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
this.url = String(input);
|
|
70
|
+
this.method = (init?.method ?? 'GET').toUpperCase();
|
|
71
|
+
this.headers = new NitroHeaders(init?.headers
|
|
72
|
+
? init.headers instanceof NitroHeaders
|
|
73
|
+
? init.headers
|
|
74
|
+
: init.headers
|
|
75
|
+
: undefined);
|
|
76
|
+
this.redirect = init?.redirect ?? 'follow';
|
|
77
|
+
this.signal = init?.signal ?? new AbortController().signal;
|
|
78
|
+
this.cache = init?.cache ?? 'default';
|
|
79
|
+
this.credentials = init?.credentials ?? 'same-origin';
|
|
80
|
+
this.mode = init?.mode ?? 'cors';
|
|
81
|
+
this.referrer = init?.referrer ?? 'about:client';
|
|
82
|
+
this.referrerPolicy = init?.referrerPolicy ?? '';
|
|
83
|
+
this.integrity = init?.integrity ?? '';
|
|
84
|
+
this.keepalive = init?.keepalive ?? false;
|
|
85
|
+
this._body = init?.body ?? null;
|
|
86
|
+
}
|
|
87
|
+
this.destination = '';
|
|
88
|
+
}
|
|
89
|
+
get bodyUsed() {
|
|
90
|
+
return this._bodyUsed;
|
|
91
|
+
}
|
|
92
|
+
get body() {
|
|
93
|
+
if (this._body == null)
|
|
94
|
+
return null;
|
|
95
|
+
const bodyBytes = this._getBodyBytes();
|
|
96
|
+
if (!bodyBytes)
|
|
97
|
+
return null;
|
|
98
|
+
return new ReadableStream({
|
|
99
|
+
start(controller) {
|
|
100
|
+
controller.enqueue(new Uint8Array(bodyBytes));
|
|
101
|
+
controller.close();
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
_throwIfBodyUsed() {
|
|
106
|
+
if (this._bodyUsed) {
|
|
107
|
+
throw new TypeError('Body has already been consumed.');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
_getBodyBytes() {
|
|
111
|
+
if (this._body == null)
|
|
112
|
+
return undefined;
|
|
113
|
+
if (typeof this._body === 'string') {
|
|
114
|
+
const encoded = stringToUTF8(this._body);
|
|
115
|
+
return encoded.buffer.slice(encoded.byteOffset, encoded.byteOffset + encoded.byteLength);
|
|
116
|
+
}
|
|
117
|
+
if (this._body instanceof ArrayBuffer)
|
|
118
|
+
return this._body;
|
|
119
|
+
if (ArrayBuffer.isView(this._body)) {
|
|
120
|
+
const view = this._body;
|
|
121
|
+
return view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
|
122
|
+
}
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
_getBodyString() {
|
|
126
|
+
if (this._body == null)
|
|
127
|
+
return '';
|
|
128
|
+
if (typeof this._body === 'string')
|
|
129
|
+
return this._body;
|
|
130
|
+
const bytes = this._getBodyBytes();
|
|
131
|
+
if (bytes)
|
|
132
|
+
return utf8ToString(new Uint8Array(bytes));
|
|
133
|
+
return '';
|
|
134
|
+
}
|
|
135
|
+
async text() {
|
|
136
|
+
this._throwIfBodyUsed();
|
|
137
|
+
this._bodyUsed = true;
|
|
138
|
+
return this._getBodyString();
|
|
139
|
+
}
|
|
140
|
+
async json() {
|
|
141
|
+
this._throwIfBodyUsed();
|
|
142
|
+
this._bodyUsed = true;
|
|
143
|
+
const t = this._getBodyString();
|
|
144
|
+
return JSON.parse(t || '{}');
|
|
145
|
+
}
|
|
146
|
+
async arrayBuffer() {
|
|
147
|
+
this._throwIfBodyUsed();
|
|
148
|
+
this._bodyUsed = true;
|
|
149
|
+
return this._getBodyBytes() ?? new ArrayBuffer(0);
|
|
150
|
+
}
|
|
151
|
+
async blob() {
|
|
152
|
+
this._throwIfBodyUsed();
|
|
153
|
+
this._bodyUsed = true;
|
|
154
|
+
const buffer = this._getBodyBytes() ?? new ArrayBuffer(0);
|
|
155
|
+
const contentType = this.headers.get('content-type') ?? '';
|
|
156
|
+
return new Blob([buffer], { type: contentType });
|
|
157
|
+
}
|
|
158
|
+
async bytes() {
|
|
159
|
+
this._throwIfBodyUsed();
|
|
160
|
+
this._bodyUsed = true;
|
|
161
|
+
const buffer = this._getBodyBytes() ?? new ArrayBuffer(0);
|
|
162
|
+
return new Uint8Array(buffer);
|
|
163
|
+
}
|
|
164
|
+
clone() {
|
|
165
|
+
if (this._bodyUsed) {
|
|
166
|
+
throw new TypeError('Cannot clone a Request whose body has been used.');
|
|
167
|
+
}
|
|
168
|
+
return new NitroRequest(this);
|
|
169
|
+
}
|
|
170
|
+
async formData() {
|
|
171
|
+
throw new TypeError('formData() is not supported in NitroRequest');
|
|
172
|
+
}
|
|
173
|
+
}
|
package/src/Response.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { NitroHeaders } from './Headers';
|
|
2
|
+
import { stringToUTF8, utf8ToString } from './utf8';
|
|
3
|
+
function isNitroResponseInit(arg) {
|
|
4
|
+
return (arg != null &&
|
|
5
|
+
typeof arg === 'object' &&
|
|
6
|
+
'url' in arg &&
|
|
7
|
+
'status' in arg &&
|
|
8
|
+
'ok' in arg);
|
|
9
|
+
}
|
|
10
|
+
export class NitroResponse {
|
|
11
|
+
url;
|
|
12
|
+
ok;
|
|
13
|
+
status;
|
|
14
|
+
statusText;
|
|
15
|
+
redirected;
|
|
16
|
+
headers;
|
|
17
|
+
type;
|
|
18
|
+
_bodyBytes;
|
|
19
|
+
_bodyString;
|
|
20
|
+
_bodyStream;
|
|
21
|
+
_bodyUsed = false;
|
|
22
|
+
constructor(bodyOrInit, init) {
|
|
23
|
+
if (isNitroResponseInit(bodyOrInit)) {
|
|
24
|
+
// Internal constructor path
|
|
25
|
+
const nitroInit = bodyOrInit;
|
|
26
|
+
this.url = nitroInit.url;
|
|
27
|
+
this.ok = nitroInit.ok;
|
|
28
|
+
this.status = nitroInit.status;
|
|
29
|
+
this.statusText = nitroInit.statusText;
|
|
30
|
+
this.redirected = nitroInit.redirected;
|
|
31
|
+
this.type = nitroInit.type ?? 'basic';
|
|
32
|
+
if (nitroInit.headers instanceof NitroHeaders) {
|
|
33
|
+
this.headers = nitroInit.headers;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.headers = new NitroHeaders(nitroInit.headers);
|
|
37
|
+
}
|
|
38
|
+
this._bodyBytes = nitroInit.bodyBytes;
|
|
39
|
+
this._bodyString = nitroInit.bodyString;
|
|
40
|
+
this._bodyStream = nitroInit.body;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
// Public constructor: new Response(body?, init?)
|
|
44
|
+
const body = bodyOrInit;
|
|
45
|
+
this.status = init?.status ?? 200;
|
|
46
|
+
this.statusText = init?.statusText ?? '';
|
|
47
|
+
this.ok = this.status >= 200 && this.status < 300;
|
|
48
|
+
this.url = '';
|
|
49
|
+
this.redirected = false;
|
|
50
|
+
this.type = 'default';
|
|
51
|
+
this.headers = new NitroHeaders(init?.headers);
|
|
52
|
+
if (body == null) {
|
|
53
|
+
// no body
|
|
54
|
+
}
|
|
55
|
+
else if (typeof body === 'string') {
|
|
56
|
+
this._bodyString = body;
|
|
57
|
+
}
|
|
58
|
+
else if (body instanceof ArrayBuffer) {
|
|
59
|
+
this._bodyBytes = body;
|
|
60
|
+
}
|
|
61
|
+
else if (ArrayBuffer.isView(body)) {
|
|
62
|
+
const view = body;
|
|
63
|
+
this._bodyBytes = view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
|
64
|
+
}
|
|
65
|
+
else if (typeof ReadableStream !== 'undefined' &&
|
|
66
|
+
body instanceof ReadableStream) {
|
|
67
|
+
this._bodyStream = body;
|
|
68
|
+
}
|
|
69
|
+
else if (typeof URLSearchParams !== 'undefined' &&
|
|
70
|
+
body instanceof URLSearchParams) {
|
|
71
|
+
this._bodyString = body.toString();
|
|
72
|
+
if (!this.headers.has('content-type')) {
|
|
73
|
+
this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (typeof Blob !== 'undefined' && body instanceof Blob) {
|
|
77
|
+
// Store as string — RN Blobs are string-backed
|
|
78
|
+
this._bodyString = '';
|
|
79
|
+
this._bodyStream = body.stream?.();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
get bodyUsed() {
|
|
84
|
+
return this._bodyUsed;
|
|
85
|
+
}
|
|
86
|
+
get body() {
|
|
87
|
+
if (this._bodyStream)
|
|
88
|
+
return this._bodyStream;
|
|
89
|
+
const bytes = this._getBodyBytes();
|
|
90
|
+
if (!bytes)
|
|
91
|
+
return null;
|
|
92
|
+
return new ReadableStream({
|
|
93
|
+
start(controller) {
|
|
94
|
+
controller.enqueue(new Uint8Array(bytes));
|
|
95
|
+
controller.close();
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
_throwIfBodyUsed() {
|
|
100
|
+
if (this._bodyUsed) {
|
|
101
|
+
throw new TypeError('Body has already been consumed.');
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
_getBodyBytes() {
|
|
105
|
+
// TODO: copy buffer to avoid clone being modifying res
|
|
106
|
+
if (this._bodyBytes != null)
|
|
107
|
+
return this._bodyBytes;
|
|
108
|
+
if (this._bodyString != null) {
|
|
109
|
+
const encoded = stringToUTF8(this._bodyString);
|
|
110
|
+
return encoded.buffer.slice(encoded.byteOffset, encoded.byteOffset + encoded.byteLength);
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
_getBodyString() {
|
|
115
|
+
if (this._bodyString != null)
|
|
116
|
+
return this._bodyString;
|
|
117
|
+
if (this._bodyBytes) {
|
|
118
|
+
return utf8ToString(new Uint8Array(this._bodyBytes));
|
|
119
|
+
}
|
|
120
|
+
return '';
|
|
121
|
+
}
|
|
122
|
+
async text() {
|
|
123
|
+
this._throwIfBodyUsed();
|
|
124
|
+
this._bodyUsed = true;
|
|
125
|
+
if (this._bodyStream && !this._bodyBytes && this._bodyString == null) {
|
|
126
|
+
const reader = this._bodyStream.getReader();
|
|
127
|
+
const chunks = [];
|
|
128
|
+
while (true) {
|
|
129
|
+
const { done, value } = await reader.read();
|
|
130
|
+
if (done)
|
|
131
|
+
break;
|
|
132
|
+
if (value)
|
|
133
|
+
chunks.push(value);
|
|
134
|
+
}
|
|
135
|
+
// Concatenate chunks
|
|
136
|
+
let totalLen = 0;
|
|
137
|
+
for (const c of chunks)
|
|
138
|
+
totalLen += c.byteLength;
|
|
139
|
+
const merged = new Uint8Array(totalLen);
|
|
140
|
+
let offset = 0;
|
|
141
|
+
for (const c of chunks) {
|
|
142
|
+
merged.set(c, offset);
|
|
143
|
+
offset += c.byteLength;
|
|
144
|
+
}
|
|
145
|
+
return utf8ToString(merged);
|
|
146
|
+
}
|
|
147
|
+
return this._getBodyString();
|
|
148
|
+
}
|
|
149
|
+
async json() {
|
|
150
|
+
this._throwIfBodyUsed();
|
|
151
|
+
this._bodyUsed = true;
|
|
152
|
+
const t = this._getBodyString();
|
|
153
|
+
return JSON.parse(t || '{}');
|
|
154
|
+
}
|
|
155
|
+
async arrayBuffer() {
|
|
156
|
+
this._throwIfBodyUsed();
|
|
157
|
+
this._bodyUsed = true;
|
|
158
|
+
if (this._bodyStream && !this._bodyBytes && this._bodyString == null) {
|
|
159
|
+
const reader = this._bodyStream.getReader();
|
|
160
|
+
const chunks = [];
|
|
161
|
+
while (true) {
|
|
162
|
+
const { done, value } = await reader.read();
|
|
163
|
+
if (done)
|
|
164
|
+
break;
|
|
165
|
+
if (value)
|
|
166
|
+
chunks.push(value);
|
|
167
|
+
}
|
|
168
|
+
let totalLen = 0;
|
|
169
|
+
for (const c of chunks)
|
|
170
|
+
totalLen += c.byteLength;
|
|
171
|
+
const merged = new Uint8Array(totalLen);
|
|
172
|
+
let offset = 0;
|
|
173
|
+
for (const c of chunks) {
|
|
174
|
+
merged.set(c, offset);
|
|
175
|
+
offset += c.byteLength;
|
|
176
|
+
}
|
|
177
|
+
return merged.buffer.slice(merged.byteOffset, merged.byteOffset + merged.byteLength);
|
|
178
|
+
}
|
|
179
|
+
return this._getBodyBytes() ?? new ArrayBuffer(0);
|
|
180
|
+
}
|
|
181
|
+
async blob() {
|
|
182
|
+
this._throwIfBodyUsed();
|
|
183
|
+
this._bodyUsed = true;
|
|
184
|
+
// RN's Blob doesn't support ArrayBuffer/ArrayBufferView — use string body
|
|
185
|
+
const bodyStr = this._getBodyString();
|
|
186
|
+
const contentType = this.headers.get('content-type') ?? '';
|
|
187
|
+
return new Blob([bodyStr], { type: contentType });
|
|
188
|
+
}
|
|
189
|
+
async bytes() {
|
|
190
|
+
this._throwIfBodyUsed();
|
|
191
|
+
this._bodyUsed = true;
|
|
192
|
+
const buffer = this._getBodyBytes() ?? new ArrayBuffer(0);
|
|
193
|
+
return new Uint8Array(buffer);
|
|
194
|
+
}
|
|
195
|
+
clone() {
|
|
196
|
+
if (this._bodyUsed) {
|
|
197
|
+
throw new TypeError('Cannot clone a Response whose body has been used.');
|
|
198
|
+
}
|
|
199
|
+
return new NitroResponse({
|
|
200
|
+
url: this.url,
|
|
201
|
+
status: this.status,
|
|
202
|
+
statusText: this.statusText,
|
|
203
|
+
ok: this.ok,
|
|
204
|
+
redirected: this.redirected,
|
|
205
|
+
headers: this.headers,
|
|
206
|
+
bodyBytes: this._bodyBytes,
|
|
207
|
+
bodyString: this._bodyString,
|
|
208
|
+
type: this.type,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
async formData() {
|
|
212
|
+
throw new TypeError('formData() is not supported in NitroResponse');
|
|
213
|
+
}
|
|
214
|
+
// --- Static methods ---
|
|
215
|
+
static error() {
|
|
216
|
+
return new NitroResponse({
|
|
217
|
+
url: '',
|
|
218
|
+
status: 0,
|
|
219
|
+
statusText: '',
|
|
220
|
+
ok: false,
|
|
221
|
+
redirected: false,
|
|
222
|
+
headers: [],
|
|
223
|
+
type: 'error',
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
static json(data, init) {
|
|
227
|
+
const body = JSON.stringify(data);
|
|
228
|
+
const headers = new NitroHeaders(init?.headers);
|
|
229
|
+
if (!headers.has('content-type')) {
|
|
230
|
+
headers.set('content-type', 'application/json');
|
|
231
|
+
}
|
|
232
|
+
return new NitroResponse({
|
|
233
|
+
url: '',
|
|
234
|
+
status: init?.status ?? 200,
|
|
235
|
+
statusText: init?.statusText ?? '',
|
|
236
|
+
ok: (init?.status ?? 200) >= 200 && (init?.status ?? 200) < 300,
|
|
237
|
+
redirected: false,
|
|
238
|
+
headers,
|
|
239
|
+
bodyString: body,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
static redirect(url, status = 302) {
|
|
243
|
+
const validStatuses = [301, 302, 303, 307, 308];
|
|
244
|
+
if (!validStatuses.includes(status)) {
|
|
245
|
+
throw new RangeError(`Invalid redirect status: ${status}. Must be one of ${validStatuses.join(', ')}`);
|
|
246
|
+
}
|
|
247
|
+
const headers = new NitroHeaders();
|
|
248
|
+
headers.set('location', url);
|
|
249
|
+
return new NitroResponse({
|
|
250
|
+
url: '',
|
|
251
|
+
status,
|
|
252
|
+
statusText: '',
|
|
253
|
+
ok: false,
|
|
254
|
+
redirected: false,
|
|
255
|
+
headers,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
package/src/Response.ts
CHANGED
|
@@ -144,7 +144,8 @@ export class NitroResponse {
|
|
|
144
144
|
}
|
|
145
145
|
|
|
146
146
|
private _getBodyBytes(): ArrayBuffer | undefined {
|
|
147
|
-
|
|
147
|
+
// TODO: copy buffer to avoid clone being modifying res
|
|
148
|
+
if (this._bodyBytes != null) return this._bodyBytes;
|
|
148
149
|
if (this._bodyString != null) {
|
|
149
150
|
const encoded = stringToUTF8(this._bodyString);
|
|
150
151
|
return (encoded.buffer as ArrayBuffer).slice(
|