react-native-nitro-sse 3.0.0 → 3.1.0
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/NitroSse.podspec +0 -1
- package/README.md +1 -1
- package/android/build.gradle +0 -1
- package/android/src/main/java/com/margelo/nitro/nitrosse/JsonUtils.kt +33 -10
- package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +366 -78
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseConnectionHandler.kt +417 -94
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventBuffer.kt +20 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseReconnectStrategy.kt +9 -0
- package/ios/NitroSse.swift +302 -49
- package/ios/SseConnectionHandler.swift +647 -97
- package/ios/SseEventBuffer.swift +19 -1
- package/ios/SseReconnectStrategy.swift +26 -7
- package/lib/module/MockSseEngine.js +72 -3
- package/lib/module/NitroSseClient.js +24 -1
- package/lib/typescript/src/MockSseEngine.d.ts +16 -0
- package/lib/typescript/src/SseInterface.d.ts +46 -3
- package/nitrogen/generated/android/c++/JHybridNitroSseSpec.cpp +4 -0
- package/nitrogen/generated/android/c++/JSseDisconnectReason.hpp +67 -0
- package/nitrogen/generated/android/c++/JSseStats.hpp +87 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseDisconnectReason.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseStats.kt +108 -3
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +18 -0
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Umbrella.hpp +3 -0
- package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +3 -0
- package/nitrogen/generated/ios/swift/SseDisconnectReason.swift +52 -0
- package/nitrogen/generated/ios/swift/SseStats.swift +217 -2
- package/nitrogen/generated/shared/c++/SseDisconnectReason.hpp +88 -0
- package/nitrogen/generated/shared/c++/SseStats.hpp +88 -2
- package/package.json +2 -2
package/NitroSse.podspec
CHANGED
package/README.md
CHANGED
|
@@ -209,7 +209,7 @@ sse.start();
|
|
|
209
209
|
- **`flush()`**: Flushes pending events in the buffer immediately.
|
|
210
210
|
- **`isConnected()`**: Returns boolean indicating if connection is active (`connecting`, `open`, `reconnecting`).
|
|
211
211
|
- **`getState()`**: Returns current `SseState`.
|
|
212
|
-
- **`getStats()`**: Returns connection metrics (`totalBytesReceived`, `reconnectCount`, `lastErrorTime`, `lastErrorCode`).
|
|
212
|
+
- **`getStats()`**: Returns comprehensive connection metrics across transport (`rawBytesReceived`, `decompressedBytesReceived`, `totalBytesReceived`, `chunksReceived`, `lastStatusCode`), parser (`totalEventsReceived`, `commentsReceived`, `linesParsed`, `parseErrors`, `serverRetryDelayMs`), latency (`connectedAt`, `timeToFirstByteMs`, `lastEventTime`, `lastHeartbeatTime`, `maxEventGapMs`), buffer (`eventsBuffered`, `peakBufferedEvents`, `bufferFlushCount`, `bufferOverflowCount`), and diagnostics/lifecycle (`connectionAttempts`, `reconnectCount`, `lastReconnectDelayMs`, `disconnectReason`, `lastErrorTime`, `lastErrorCode`).
|
|
213
213
|
- **`updateHeaders(headers)`**: Merges new headers into current configuration without closing the connection.
|
|
214
214
|
- **`setLastProcessedId(id)`**: Sets the event ID to send in `Last-Event-ID` on subsequent reconnections.
|
|
215
215
|
- **`addEventListener(type, listener)`**: Subscribes to an event type (`'message'`, `'open'`, `'close'`, `'error'`, `'heartbeat'`, `'state'`, or custom event name).
|
package/android/build.gradle
CHANGED
|
@@ -126,7 +126,6 @@ dependencies {
|
|
|
126
126
|
implementation "com.facebook.react:react-android"
|
|
127
127
|
implementation project(":react-native-nitro-modules")
|
|
128
128
|
implementation "com.squareup.okhttp3:okhttp:4.12.0"
|
|
129
|
-
implementation "com.squareup.okhttp3:okhttp-sse:4.12.0"
|
|
130
129
|
implementation "androidx.lifecycle:lifecycle-process:2.8.7"
|
|
131
130
|
|
|
132
131
|
testImplementation "junit:junit:4.13.2"
|
|
@@ -25,7 +25,7 @@ object JsonUtils {
|
|
|
25
25
|
if (depth > MAX_DEPTH) {
|
|
26
26
|
throw IllegalArgumentException("JSON nesting depth limit ($MAX_DEPTH) exceeded")
|
|
27
27
|
}
|
|
28
|
-
val map =
|
|
28
|
+
val map = HashMap<String, Any?>(jsonObject.length())
|
|
29
29
|
val keys = jsonObject.keys()
|
|
30
30
|
while (keys.hasNext()) {
|
|
31
31
|
val key = keys.next()
|
|
@@ -38,27 +38,50 @@ object JsonUtils {
|
|
|
38
38
|
if (depth > MAX_DEPTH) {
|
|
39
39
|
throw IllegalArgumentException("JSON nesting depth limit ($MAX_DEPTH) exceeded")
|
|
40
40
|
}
|
|
41
|
-
val list =
|
|
41
|
+
val list = ArrayList<Any?>(jsonArray.length())
|
|
42
42
|
for (i in 0 until jsonArray.length()) {
|
|
43
43
|
list.add(convertJsonValue(jsonArray.get(i), depth))
|
|
44
44
|
}
|
|
45
45
|
return list
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Parses a raw JSON string into a [Map]. Returns null on non-object roots.
|
|
50
|
+
* Throws [org.json.JSONException] on malformed JSON objects.
|
|
51
|
+
*/
|
|
52
|
+
fun parseJsonToMap(data: String): Map<String, Any?>? {
|
|
53
|
+
val len = data.length
|
|
54
|
+
var firstChar = ' '
|
|
55
|
+
var idx = 0
|
|
56
|
+
var needsSubstring = false
|
|
57
|
+
while (idx < len) {
|
|
58
|
+
val c = data[idx]
|
|
59
|
+
if (c > ' ') {
|
|
60
|
+
if (c.isWhitespace()) {
|
|
61
|
+
needsSubstring = true
|
|
62
|
+
} else {
|
|
63
|
+
firstChar = c
|
|
64
|
+
break
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
idx++
|
|
68
|
+
}
|
|
69
|
+
if (firstChar != '{') {
|
|
70
|
+
return null
|
|
71
|
+
}
|
|
72
|
+
val jsonStr = if (needsSubstring) data.substring(idx) else data
|
|
73
|
+
val jsonObject = JSONObject(jsonStr)
|
|
74
|
+
return jsonObjectToMap(jsonObject)
|
|
75
|
+
}
|
|
76
|
+
|
|
48
77
|
/**
|
|
49
78
|
* Parses a raw JSON string into [AnyMap]. Returns null on malformed JSON or non-object roots
|
|
50
79
|
* to prevent invalid SSE payloads from interrupting stream processing.
|
|
51
80
|
*/
|
|
52
81
|
fun parseJsonToAnyMap(data: String): AnyMap? {
|
|
53
82
|
return try {
|
|
54
|
-
val
|
|
55
|
-
|
|
56
|
-
val jsonObject = JSONObject(trimmed)
|
|
57
|
-
val map = jsonObjectToMap(jsonObject)
|
|
58
|
-
AnyMap.fromMap(map, true)
|
|
59
|
-
} else {
|
|
60
|
-
null
|
|
61
|
-
}
|
|
83
|
+
val map = parseJsonToMap(data) ?: return null
|
|
84
|
+
AnyMap.fromMap(map, true)
|
|
62
85
|
} catch (t: Throwable) {
|
|
63
86
|
try {
|
|
64
87
|
Log.w("JsonUtils", "Failed to parse JSON: ${t.message}")
|