react-nitro-networking
v0.0.1-alpha.17
Published
The runtime calls `installNetworkingGlobals()` from `react-nitro-networking/installNetworkingGlobals` at startup to install `fetch`, `Headers`, `AbortController`, `AbortSignal`, and `ReadableStream`. Importing `react-nitro-networking` on its own leaves gl
Readme
React Nitro Networking
The runtime calls installNetworkingGlobals() from react-nitro-networking/installNetworkingGlobals at startup to install fetch, Headers, AbortController, AbortSignal, and ReadableStream. Importing react-nitro-networking on its own leaves globals untouched.
Fetch
Call Networking.fetch() directly on the imported class:
import { Networking, Response } from 'react-nitro-networking'
const response = await Networking.fetch('https://example.com')
console.log(response instanceof Response) // true
console.log(await response.text())Pass request headers as an optional string record:
const response = await Networking.fetch('https://example.com/api/items', {
headers: { Accept: 'application/json' },
})To post JSON, pass the serialized text and set its content type.
import { Networking, RequestMethod } from 'react-nitro-networking'
const body = JSON.stringify({ text: 'Hello' })
const response = await Networking.fetch('https://example.com/api/items', {
method: RequestMethod.POST,
headers: { 'Content-Type': 'application/json' },
body: body,
})The supported methods are GET (default), HEAD, POST, PUT, PATCH, DELETE, and
OPTIONS. Both RequestMethod.POST and the uppercase literal 'POST' are
type-checked. Enum members pass directly to the native bridge as numbers.
A body on GET or HEAD rejects the promise. Strings use UTF-8 and default to text/plain;charset=UTF-8.
An ArrayBuffer, typed array, or DataView sends its byte range and defaults
to application/octet-stream.
Apps can also pass URLSearchParams, Blob, or FormData from their web API
polyfills. Search parameters use URL-encoded form data. A blob uses its MIME
type. For multipart uploads, Swift and Kotlin assemble the body and set its
content type. React Native form parts can reference files by URI. The native
transport reads those files directly.
The runtime's global fetch accepts the same body types, a URL object, and request
headers supplied as Headers, a headers polyfill with get and forEach,
a record, or pairs. Use response.headers.get(name) for a case-insensitive lookup
and response.headers.getSetCookie() for individual cookies. For direct
Networking.fetch calls, response.headers is a string record and
response.headerEntries exposes the individual entries.
On Apple platforms, URLSession combines repeated Set-Cookie fields. The
runtime preserves commas in expiration dates and quoted values. An unquoted
, name=value inside a cookie attribute is indistinguishable from a new cookie.
Redirects are followed across origins. A caller-supplied Authorization header stays on the original
origin and is removed once the request leaves it. A 303 after a POST sends a GET
without the body. A 307 or 308 preserves the method and body.
Pass an AbortSignal to global fetch to cancel the native request. Several requests can share one signal. Aborting after headers arrive also cancels a pending body read.
const controller = new AbortController()
const response = fetch('https://example.com', { signal: controller.signal })
controller.abort()WebSockets
new WebSocket(url) starts connecting immediately. Each socket supports text and
ArrayBuffer messages. Listeners are additive and return a subscription with an idempotent remove().
import { WebSocket } from 'react-nitro-networking'
const socket = new WebSocket('wss://example.com/socket')
const messages = socket.addOnMessageReceivedListener((data) => {
console.log(data)
})
socket.addOnOpenedListener(() => console.log('Connected'))
socket.addOnErrorListener((error) => console.error(error))
socket.addOnClosedListener(() => console.log('Closed'))
try {
// send() waits for the handshake if the connection is still opening.
await socket.send('Hello')
} catch (error) {
console.error('Could not send:', error)
}
// When the connection is no longer needed:
messages.remove()
socket.close()Binary messages use the same methods. Await sends to preserve their order:
await socket.send(new Uint8Array([0, 1, 255]).buffer)
await socket.send(new ArrayBuffer(0))Invalid URLs throw from the constructor. Handshake and transport failures notify error listeners,
then closed listeners. Late opened listeners are notified if the socket is still open; late error
and closed listeners receive the terminal outcome. close() is idempotent, releases listeners,
and reports local shutdown without waiting for a peer acknowledgement. Reconnecting requires a new socket.
Receiving starts immediately. Until the first message listener is registered, up to 64 messages or 1 MiB are buffered. Exceeding either limit closes the connection with an error. After the first subscription, messages without listeners are discarded. Closing also discards buffered messages.
Apple platforms use URLSessionWebSocketTask; Android uses Nitro Fetch's native
libwebsockets/mbedTLS transport, with no Java payload bridge. On Android,
send() resolves when the native send queue accepts the message. Use an application
acknowledgement when delivery needs to be confirmed. The Android host closes its
network operations when the runtime ends.
Android HTTP
HTTP uses the embedded Cronet engine from react-native-nitro-fetch, with HTTP/2, QUIC, Brotli, and a shared 50 MiB disk cache. It does not require Google Play Services. The runtime prewarms the engine with the application context.
Fetch resolves when response headers arrive. Body methods start reading on demand, with Cronet writing into direct buffers. Repeated body reads remain supported; arrayBuffer() returns an independent copy each time.
Native transport sources, pinned revisions, and license attribution are documented in third-party/nitro-fetch.
