Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ object AutoPrefetcher {
put("headers", headersObj)
if (method != null && method.isNotEmpty() && method != "GET") put("method", method)
if (bodyString != null) put("bodyString", bodyString)
if (bodyBytes != null) put("bodyBytes", bodyBytes)
if (!bodyFormData.isNullOrEmpty()) {
val arr = JSONArray()
bodyFormData.forEach { part ->
Expand Down Expand Up @@ -233,9 +232,6 @@ object AutoPrefetcher {
?.takeIf { it.has("bodyString") && !it.isNull("bodyString") }
?.optString("bodyString")
val bodyString = injectBodyFields(rawBodyString, tokens.bodyFields)
val bodyBytes = entry
?.takeIf { it.has("bodyBytes") && !it.isNull("bodyBytes") }
?.optString("bodyBytes")
val timeoutMs = entry
?.takeIf { it.has("timeoutMs") && !it.isNull("timeoutMs") }
?.optDouble("timeoutMs")
Expand Down Expand Up @@ -267,7 +263,7 @@ object AutoPrefetcher {
method = method,
headers = headerObjs,
bodyString = bodyString,
bodyBytes = bodyBytes,
bodyBytes = null,
bodyFormData = bodyFormData,
timeoutMs = timeoutMs,
followRedirects = followRedirects,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class NitroFetchClient(private val engine: CronetEngine, private val executor: E
val bodyStr = req.bodyString
if ((bodyBytes != null) || !bodyStr.isNullOrEmpty()) {
val body: ByteArray = when {
bodyBytes != null -> ByteArray(1)
bodyBytes != null -> bodyBytes.getBuffer(true).toByteArray()
!bodyStr.isNullOrEmpty() -> bodyStr!!.toByteArray(Charsets.UTF_8)
else -> ByteArray(0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public final class NitroAutoPrefetcher: NSObject {
]
if let method = method, !method.isEmpty, method != "GET" { entry["method"] = method }
if let bodyString = bodyString { entry["bodyString"] = bodyString }
if let bodyBytes = bodyBytes { entry["bodyBytes"] = bodyBytes }
if let parts = bodyFormData, !parts.isEmpty {
entry["bodyFormData"] = parts.map { part -> [String: String] in
var clean: [String: String] = [:]
Expand All @@ -244,7 +243,6 @@ public final class NitroAutoPrefetcher: NSObject {
let methodStr = entry["method"] as? String
let method: NitroRequestMethod? = methodStr.flatMap { NitroRequestMethod(fromString: $0) }
let bodyString = injectBodyFields(entry["bodyString"] as? String, fields: tokens.bodyFields)
let bodyBytes = entry["bodyBytes"] as? String
let timeoutMs = (entry["timeoutMs"] as? NSNumber)?.doubleValue
let followRedirects = (entry["followRedirects"] as? Bool) ?? true
let prefetchCacheTtlMs = (entry["prefetchCacheTtlMs"] as? NSNumber)?.doubleValue
Expand All @@ -265,7 +263,7 @@ public final class NitroAutoPrefetcher: NSObject {
method: method,
headers: mergedHeaders,
bodyString: bodyString,
bodyBytes: bodyBytes,
bodyBytes: nil,
bodyFormData: formData,
timeoutMs: timeoutMs,
followRedirects: followRedirects,
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-nitro-fetch/ios/NitroFetchClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ final class NitroFetchClient: HybridNitroFetchClientSpec {
r.setValue(contentType, forHTTPHeaderField: "Content-Type")
} else if let s = req.bodyString {
r.httpBody = s.data(using: .utf8)
} else if let bytes = req.bodyBytes {
r.httpBody = bytes.toData(copyIfNeeded: true)
}
if let t = req.timeoutMs, t > 0 { r.timeoutInterval = TimeInterval(t) / 1000.0 }
return (r, nil)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/react-native-nitro-fetch/src/NitroFetch.nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface NitroRequest {
headers?: NitroHeader[];
// Body as either UTF-8 string or raw bytes.
bodyString?: string;
bodyBytes?: string; //will be ArrayBuffer in future
bodyBytes?: ArrayBuffer;
// Multipart form data parts (for file uploads)
bodyFormData?: NitroFormDataPart[];
// Controls
Expand Down
43 changes: 43 additions & 0 deletions packages/react-native-nitro-fetch/src/__tests__/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
jest.mock('react-native-nitro-modules', () => ({
NitroModules: {
box: (value: unknown) => value,
createHybridObject: () => ({}),
},
}));

import { buildNitroRequestPure } from '../fetch';

function toArrayBuffer(bytes: Uint8Array): ArrayBuffer {
return bytes.buffer.slice(
bytes.byteOffset,
bytes.byteOffset + bytes.byteLength
) as ArrayBuffer;
}

describe('buildNitroRequestPure', () => {
it('passes ArrayBuffer request bodies through as bodyBytes', () => {
const bytes = new Uint8Array([0x00, 0x7f, 0x80, 0xff]);
const req = buildNitroRequestPure('https://example.com/upload', {
method: 'POST',
body: toArrayBuffer(bytes),
});

expect(req.bodyString).toBeUndefined();
expect(req.bodyBytes).toBeInstanceOf(ArrayBuffer);
expect(new Uint8Array(req.bodyBytes as ArrayBuffer)).toEqual(bytes);
});

it('copies typed array request bodies into exact bodyBytes', () => {
const backing = new Uint8Array([0xaa, 0x00, 0x01, 0x02, 0xbb]);
const view = backing.subarray(1, 4);
const req = buildNitroRequestPure('https://example.com/upload', {
method: 'POST',
body: view,
});

expect(req.bodyString).toBeUndefined();
expect(new Uint8Array(req.bodyBytes as ArrayBuffer)).toEqual(
new Uint8Array([0x00, 0x01, 0x02])
);
});
});
7 changes: 2 additions & 5 deletions packages/react-native-nitro-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function buildNitroRequest(
method: (method?.toUpperCase() as any) ?? 'GET',
headers: headers.length > 0 ? headers : undefined,
bodyString: normalized?.bodyString,
bodyBytes: undefined as any,
bodyBytes: normalized?.bodyBytes,
bodyFormData: normalized?.bodyFormData,
followRedirects,
prefetchCacheTtlMs,
Expand Down Expand Up @@ -376,8 +376,7 @@ export function buildNitroRequestPure(
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,
bodyBytes: normalized?.bodyBytes,
followRedirects: true,
prefetchCacheTtlMs,
};
Expand Down Expand Up @@ -794,8 +793,6 @@ export async function prefetchOnAppStart(
};
if (req.method && req.method !== 'GET') entry.method = req.method;
if (req.bodyString !== undefined) entry.bodyString = req.bodyString;
if (typeof req.bodyBytes === 'string' && req.bodyBytes.length > 0)
entry.bodyBytes = req.bodyBytes;
if (req.bodyFormData && req.bodyFormData.length > 0)
entry.bodyFormData = req.bodyFormData;
if (typeof req.timeoutMs === 'number') entry.timeoutMs = req.timeoutMs;
Expand Down