react-native-nitro-sse 2.0.0 → 2.1.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/README.md +2 -0
- package/android/build.gradle +1 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +74 -16
- package/ios/NitroSse.swift +42 -28
- package/lib/module/SseInterface.js +2 -0
- package/lib/typescript/src/SseInterface.d.ts +10 -1
- package/lib/typescript/src/SseInterface.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_SseEvent_.hpp +2 -0
- package/nitrogen/generated/android/c++/JHybridNitroSseSpec.cpp +2 -0
- package/nitrogen/generated/android/c++/JSseConfig.hpp +5 -1
- package/nitrogen/generated/android/c++/JSseEvent.hpp +7 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseConfig.kt +7 -4
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseEvent.kt +6 -3
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +16 -0
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Umbrella.hpp +1 -0
- package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +1 -0
- package/nitrogen/generated/ios/swift/SseConfig.swift +19 -1
- package/nitrogen/generated/ios/swift/SseEvent.swift +19 -1
- package/nitrogen/generated/shared/c++/SseConfig.hpp +5 -1
- package/nitrogen/generated/shared/c++/SseEvent.hpp +6 -1
- package/package.json +9 -4
- package/src/SseInterface.ts +10 -1
- package/android/src/test/java/com/margelo/nitro/nitrosse/HeartbeatScannerTest.kt +0 -98
- package/android/src/test/java/com/margelo/nitro/nitrosse/NitroSseLogicTest.kt +0 -238
- package/ios/Tests/NitroSseTests.swift +0 -242
|
@@ -9,7 +9,7 @@ package com.margelo.nitro.nitrosse
|
|
|
9
9
|
|
|
10
10
|
import androidx.annotation.Keep
|
|
11
11
|
import com.facebook.proguard.annotations.DoNotStrip
|
|
12
|
-
|
|
12
|
+
import com.margelo.nitro.core.AnyMap
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Represents the JavaScript object/struct "SseEvent".
|
|
@@ -25,6 +25,9 @@ data class SseEvent(
|
|
|
25
25
|
val data: String?,
|
|
26
26
|
@DoNotStrip
|
|
27
27
|
@Keep
|
|
28
|
+
val parsedData: AnyMap?,
|
|
29
|
+
@DoNotStrip
|
|
30
|
+
@Keep
|
|
28
31
|
val id: String?,
|
|
29
32
|
@DoNotStrip
|
|
30
33
|
@Keep
|
|
@@ -49,8 +52,8 @@ data class SseEvent(
|
|
|
49
52
|
@Keep
|
|
50
53
|
@Suppress("unused")
|
|
51
54
|
@JvmStatic
|
|
52
|
-
private fun fromCpp(type: SseEventType, data: String?, id: String?, event: String?, message: String?, statusCode: Double?, retry: Double?): SseEvent {
|
|
53
|
-
return SseEvent(type, data, id, event, message, statusCode, retry)
|
|
55
|
+
private fun fromCpp(type: SseEventType, data: String?, parsedData: AnyMap?, id: String?, event: String?, message: String?, statusCode: Double?, retry: Double?): SseEvent {
|
|
56
|
+
return SseEvent(type, data, parsedData, id, event, message, statusCode, retry)
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
59
|
}
|
|
@@ -29,6 +29,7 @@ namespace NitroSse { class HybridNitroSseSpec_cxx; }
|
|
|
29
29
|
#include "SseEvent.hpp"
|
|
30
30
|
#include "SseEventType.hpp"
|
|
31
31
|
#include "SseStats.hpp"
|
|
32
|
+
#include <NitroModules/AnyMap.hpp>
|
|
32
33
|
#include <NitroModules/Promise.hpp>
|
|
33
34
|
#include <NitroModules/PromiseHolder.hpp>
|
|
34
35
|
#include <NitroModules/Result.hpp>
|
|
@@ -274,6 +275,21 @@ namespace margelo::nitro::nitrosse::bridge::swift {
|
|
|
274
275
|
return optional.value();
|
|
275
276
|
}
|
|
276
277
|
|
|
278
|
+
// pragma MARK: std::optional<std::shared_ptr<AnyMap>>
|
|
279
|
+
/**
|
|
280
|
+
* Specialized version of `std::optional<std::shared_ptr<AnyMap>>`.
|
|
281
|
+
*/
|
|
282
|
+
using std__optional_std__shared_ptr_AnyMap__ = std::optional<std::shared_ptr<AnyMap>>;
|
|
283
|
+
inline std::optional<std::shared_ptr<AnyMap>> create_std__optional_std__shared_ptr_AnyMap__(const std::shared_ptr<AnyMap>& value) noexcept {
|
|
284
|
+
return std::optional<std::shared_ptr<AnyMap>>(value);
|
|
285
|
+
}
|
|
286
|
+
inline bool has_value_std__optional_std__shared_ptr_AnyMap__(const std::optional<std::shared_ptr<AnyMap>>& optional) noexcept {
|
|
287
|
+
return optional.has_value();
|
|
288
|
+
}
|
|
289
|
+
inline std::shared_ptr<AnyMap> get_std__optional_std__shared_ptr_AnyMap__(const std::optional<std::shared_ptr<AnyMap>>& optional) noexcept {
|
|
290
|
+
return optional.value();
|
|
291
|
+
}
|
|
292
|
+
|
|
277
293
|
// pragma MARK: std::vector<SseEvent>
|
|
278
294
|
/**
|
|
279
295
|
* Specialized version of `std::vector<SseEvent>`.
|
|
@@ -28,6 +28,7 @@ namespace margelo::nitro::nitrosse { struct SseStats; }
|
|
|
28
28
|
#include "SseEvent.hpp"
|
|
29
29
|
#include "SseEventType.hpp"
|
|
30
30
|
#include "SseStats.hpp"
|
|
31
|
+
#include <NitroModules/AnyMap.hpp>
|
|
31
32
|
#include <NitroModules/Promise.hpp>
|
|
32
33
|
#include <NitroModules/Result.hpp>
|
|
33
34
|
#include <exception>
|
|
@@ -18,7 +18,7 @@ public extension SseConfig {
|
|
|
18
18
|
/**
|
|
19
19
|
* Create a new instance of `SseConfig`.
|
|
20
20
|
*/
|
|
21
|
-
init(url: String, method: HttpMethod?, headers: Dictionary<String, String>?, body: String?, backgroundExecution: Bool?, batchingIntervalMs: Double?, maxBufferSize: Double?, connectionTimeoutMs: Double?, readTimeoutMs: Double?, retryIntervalMs: Double?, maxRetryIntervalMs: Double?, jitterFactor: Double?, maxReconnectAttempts: Double?, onBeforeRequest: (() -> Promise<Promise<Dictionary<String, String>>>)?) {
|
|
21
|
+
init(url: String, method: HttpMethod?, headers: Dictionary<String, String>?, body: String?, backgroundExecution: Bool?, batchingIntervalMs: Double?, maxBufferSize: Double?, connectionTimeoutMs: Double?, readTimeoutMs: Double?, retryIntervalMs: Double?, maxRetryIntervalMs: Double?, jitterFactor: Double?, maxReconnectAttempts: Double?, autoParseJSON: Bool?, onBeforeRequest: (() -> Promise<Promise<Dictionary<String, String>>>)?) {
|
|
22
22
|
self.init(std.string(url), { () -> bridge.std__optional_HttpMethod_ in
|
|
23
23
|
if let __unwrappedValue = method {
|
|
24
24
|
return bridge.create_std__optional_HttpMethod_(__unwrappedValue)
|
|
@@ -97,6 +97,12 @@ public extension SseConfig {
|
|
|
97
97
|
} else {
|
|
98
98
|
return .init()
|
|
99
99
|
}
|
|
100
|
+
}(), { () -> bridge.std__optional_bool_ in
|
|
101
|
+
if let __unwrappedValue = autoParseJSON {
|
|
102
|
+
return bridge.create_std__optional_bool_(__unwrappedValue)
|
|
103
|
+
} else {
|
|
104
|
+
return .init()
|
|
105
|
+
}
|
|
100
106
|
}(), { () -> bridge.std__optional_std__function_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_________ in
|
|
101
107
|
if let __unwrappedValue = onBeforeRequest {
|
|
102
108
|
return bridge.create_std__optional_std__function_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_________({ () -> bridge.Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____ in
|
|
@@ -259,6 +265,18 @@ public extension SseConfig {
|
|
|
259
265
|
}()
|
|
260
266
|
}
|
|
261
267
|
|
|
268
|
+
@inline(__always)
|
|
269
|
+
var autoParseJSON: Bool? {
|
|
270
|
+
return { () -> Bool? in
|
|
271
|
+
if bridge.has_value_std__optional_bool_(self.__autoParseJSON) {
|
|
272
|
+
let __unwrapped = bridge.get_std__optional_bool_(self.__autoParseJSON)
|
|
273
|
+
return __unwrapped
|
|
274
|
+
} else {
|
|
275
|
+
return nil
|
|
276
|
+
}
|
|
277
|
+
}()
|
|
278
|
+
}
|
|
279
|
+
|
|
262
280
|
@inline(__always)
|
|
263
281
|
var onBeforeRequest: (() -> Promise<Promise<Dictionary<String, String>>>)? {
|
|
264
282
|
return { () -> (() -> Promise<Promise<Dictionary<String, String>>>)? in
|
|
@@ -18,13 +18,19 @@ public extension SseEvent {
|
|
|
18
18
|
/**
|
|
19
19
|
* Create a new instance of `SseEvent`.
|
|
20
20
|
*/
|
|
21
|
-
init(type: SseEventType, data: String?, id: String?, event: String?, message: String?, statusCode: Double?, retry: Double?) {
|
|
21
|
+
init(type: SseEventType, data: String?, parsedData: AnyMap?, id: String?, event: String?, message: String?, statusCode: Double?, retry: Double?) {
|
|
22
22
|
self.init(type, { () -> bridge.std__optional_std__string_ in
|
|
23
23
|
if let __unwrappedValue = data {
|
|
24
24
|
return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
|
|
25
25
|
} else {
|
|
26
26
|
return .init()
|
|
27
27
|
}
|
|
28
|
+
}(), { () -> bridge.std__optional_std__shared_ptr_AnyMap__ in
|
|
29
|
+
if let __unwrappedValue = parsedData {
|
|
30
|
+
return bridge.create_std__optional_std__shared_ptr_AnyMap__(__unwrappedValue.cppPart)
|
|
31
|
+
} else {
|
|
32
|
+
return .init()
|
|
33
|
+
}
|
|
28
34
|
}(), { () -> bridge.std__optional_std__string_ in
|
|
29
35
|
if let __unwrappedValue = id {
|
|
30
36
|
return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
|
|
@@ -75,6 +81,18 @@ public extension SseEvent {
|
|
|
75
81
|
}()
|
|
76
82
|
}
|
|
77
83
|
|
|
84
|
+
@inline(__always)
|
|
85
|
+
var parsedData: AnyMap? {
|
|
86
|
+
return { () -> AnyMap? in
|
|
87
|
+
if bridge.has_value_std__optional_std__shared_ptr_AnyMap__(self.__parsedData) {
|
|
88
|
+
let __unwrapped = bridge.get_std__optional_std__shared_ptr_AnyMap__(self.__parsedData)
|
|
89
|
+
return AnyMap(withCppPart: __unwrapped)
|
|
90
|
+
} else {
|
|
91
|
+
return nil
|
|
92
|
+
}
|
|
93
|
+
}()
|
|
94
|
+
}
|
|
95
|
+
|
|
78
96
|
@inline(__always)
|
|
79
97
|
var id: String? {
|
|
80
98
|
return { () -> String? in
|
|
@@ -58,11 +58,12 @@ namespace margelo::nitro::nitrosse {
|
|
|
58
58
|
std::optional<double> maxRetryIntervalMs SWIFT_PRIVATE;
|
|
59
59
|
std::optional<double> jitterFactor SWIFT_PRIVATE;
|
|
60
60
|
std::optional<double> maxReconnectAttempts SWIFT_PRIVATE;
|
|
61
|
+
std::optional<bool> autoParseJSON SWIFT_PRIVATE;
|
|
61
62
|
std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>> onBeforeRequest SWIFT_PRIVATE;
|
|
62
63
|
|
|
63
64
|
public:
|
|
64
65
|
SseConfig() = default;
|
|
65
|
-
explicit SseConfig(std::string url, std::optional<HttpMethod> method, std::optional<std::unordered_map<std::string, std::string>> headers, std::optional<std::string> body, std::optional<bool> backgroundExecution, std::optional<double> batchingIntervalMs, std::optional<double> maxBufferSize, std::optional<double> connectionTimeoutMs, std::optional<double> readTimeoutMs, std::optional<double> retryIntervalMs, std::optional<double> maxRetryIntervalMs, std::optional<double> jitterFactor, std::optional<double> maxReconnectAttempts, std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>> onBeforeRequest): url(url), method(method), headers(headers), body(body), backgroundExecution(backgroundExecution), batchingIntervalMs(batchingIntervalMs), maxBufferSize(maxBufferSize), connectionTimeoutMs(connectionTimeoutMs), readTimeoutMs(readTimeoutMs), retryIntervalMs(retryIntervalMs), maxRetryIntervalMs(maxRetryIntervalMs), jitterFactor(jitterFactor), maxReconnectAttempts(maxReconnectAttempts), onBeforeRequest(onBeforeRequest) {}
|
|
66
|
+
explicit SseConfig(std::string url, std::optional<HttpMethod> method, std::optional<std::unordered_map<std::string, std::string>> headers, std::optional<std::string> body, std::optional<bool> backgroundExecution, std::optional<double> batchingIntervalMs, std::optional<double> maxBufferSize, std::optional<double> connectionTimeoutMs, std::optional<double> readTimeoutMs, std::optional<double> retryIntervalMs, std::optional<double> maxRetryIntervalMs, std::optional<double> jitterFactor, std::optional<double> maxReconnectAttempts, std::optional<bool> autoParseJSON, std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>> onBeforeRequest): url(url), method(method), headers(headers), body(body), backgroundExecution(backgroundExecution), batchingIntervalMs(batchingIntervalMs), maxBufferSize(maxBufferSize), connectionTimeoutMs(connectionTimeoutMs), readTimeoutMs(readTimeoutMs), retryIntervalMs(retryIntervalMs), maxRetryIntervalMs(maxRetryIntervalMs), jitterFactor(jitterFactor), maxReconnectAttempts(maxReconnectAttempts), autoParseJSON(autoParseJSON), onBeforeRequest(onBeforeRequest) {}
|
|
66
67
|
|
|
67
68
|
public:
|
|
68
69
|
// SseConfig is not equatable because these properties are not equatable: onBeforeRequest
|
|
@@ -91,6 +92,7 @@ namespace margelo::nitro {
|
|
|
91
92
|
JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxRetryIntervalMs"))),
|
|
92
93
|
JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "jitterFactor"))),
|
|
93
94
|
JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxReconnectAttempts"))),
|
|
95
|
+
JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "autoParseJSON"))),
|
|
94
96
|
JSIConverter<std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "onBeforeRequest")))
|
|
95
97
|
);
|
|
96
98
|
}
|
|
@@ -109,6 +111,7 @@ namespace margelo::nitro {
|
|
|
109
111
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "maxRetryIntervalMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.maxRetryIntervalMs));
|
|
110
112
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "jitterFactor"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.jitterFactor));
|
|
111
113
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "maxReconnectAttempts"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.maxReconnectAttempts));
|
|
114
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "autoParseJSON"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.autoParseJSON));
|
|
112
115
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "onBeforeRequest"), JSIConverter<std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>>>::toJSI(runtime, arg.onBeforeRequest));
|
|
113
116
|
return obj;
|
|
114
117
|
}
|
|
@@ -133,6 +136,7 @@ namespace margelo::nitro {
|
|
|
133
136
|
if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxRetryIntervalMs")))) return false;
|
|
134
137
|
if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "jitterFactor")))) return false;
|
|
135
138
|
if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxReconnectAttempts")))) return false;
|
|
139
|
+
if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "autoParseJSON")))) return false;
|
|
136
140
|
if (!JSIConverter<std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "onBeforeRequest")))) return false;
|
|
137
141
|
return true;
|
|
138
142
|
}
|
|
@@ -34,6 +34,7 @@ namespace margelo::nitro::nitrosse { enum class SseEventType; }
|
|
|
34
34
|
#include "SseEventType.hpp"
|
|
35
35
|
#include <string>
|
|
36
36
|
#include <optional>
|
|
37
|
+
#include <NitroModules/AnyMap.hpp>
|
|
37
38
|
|
|
38
39
|
namespace margelo::nitro::nitrosse {
|
|
39
40
|
|
|
@@ -44,6 +45,7 @@ namespace margelo::nitro::nitrosse {
|
|
|
44
45
|
public:
|
|
45
46
|
SseEventType type SWIFT_PRIVATE;
|
|
46
47
|
std::optional<std::string> data SWIFT_PRIVATE;
|
|
48
|
+
std::optional<std::shared_ptr<AnyMap>> parsedData SWIFT_PRIVATE;
|
|
47
49
|
std::optional<std::string> id SWIFT_PRIVATE;
|
|
48
50
|
std::optional<std::string> event SWIFT_PRIVATE;
|
|
49
51
|
std::optional<std::string> message SWIFT_PRIVATE;
|
|
@@ -52,7 +54,7 @@ namespace margelo::nitro::nitrosse {
|
|
|
52
54
|
|
|
53
55
|
public:
|
|
54
56
|
SseEvent() = default;
|
|
55
|
-
explicit SseEvent(SseEventType type, std::optional<std::string> data, std::optional<std::string> id, std::optional<std::string> event, std::optional<std::string> message, std::optional<double> statusCode, std::optional<double> retry): type(type), data(data), id(id), event(event), message(message), statusCode(statusCode), retry(retry) {}
|
|
57
|
+
explicit SseEvent(SseEventType type, std::optional<std::string> data, std::optional<std::shared_ptr<AnyMap>> parsedData, std::optional<std::string> id, std::optional<std::string> event, std::optional<std::string> message, std::optional<double> statusCode, std::optional<double> retry): type(type), data(data), parsedData(parsedData), id(id), event(event), message(message), statusCode(statusCode), retry(retry) {}
|
|
56
58
|
|
|
57
59
|
public:
|
|
58
60
|
friend bool operator==(const SseEvent& lhs, const SseEvent& rhs) = default;
|
|
@@ -70,6 +72,7 @@ namespace margelo::nitro {
|
|
|
70
72
|
return margelo::nitro::nitrosse::SseEvent(
|
|
71
73
|
JSIConverter<margelo::nitro::nitrosse::SseEventType>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "type"))),
|
|
72
74
|
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "data"))),
|
|
75
|
+
JSIConverter<std::optional<std::shared_ptr<AnyMap>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "parsedData"))),
|
|
73
76
|
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "id"))),
|
|
74
77
|
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "event"))),
|
|
75
78
|
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "message"))),
|
|
@@ -81,6 +84,7 @@ namespace margelo::nitro {
|
|
|
81
84
|
jsi::Object obj(runtime);
|
|
82
85
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "type"), JSIConverter<margelo::nitro::nitrosse::SseEventType>::toJSI(runtime, arg.type));
|
|
83
86
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "data"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.data));
|
|
87
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "parsedData"), JSIConverter<std::optional<std::shared_ptr<AnyMap>>>::toJSI(runtime, arg.parsedData));
|
|
84
88
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "id"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.id));
|
|
85
89
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "event"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.event));
|
|
86
90
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "message"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.message));
|
|
@@ -98,6 +102,7 @@ namespace margelo::nitro {
|
|
|
98
102
|
}
|
|
99
103
|
if (!JSIConverter<margelo::nitro::nitrosse::SseEventType>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "type")))) return false;
|
|
100
104
|
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "data")))) return false;
|
|
105
|
+
if (!JSIConverter<std::optional<std::shared_ptr<AnyMap>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "parsedData")))) return false;
|
|
101
106
|
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "id")))) return false;
|
|
102
107
|
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "event")))) return false;
|
|
103
108
|
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "message")))) return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-sse",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "High-performance Server-Sent Events (SSE) for React Native, powered by Nitro Modules.",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -23,7 +23,9 @@
|
|
|
23
23
|
"*.podspec",
|
|
24
24
|
"react-native.config.js",
|
|
25
25
|
"!ios/build",
|
|
26
|
+
"!ios/Tests",
|
|
26
27
|
"!android/build",
|
|
28
|
+
"!android/src/test",
|
|
27
29
|
"!android/gradle",
|
|
28
30
|
"!android/gradlew",
|
|
29
31
|
"!android/gradlew.bat",
|
|
@@ -40,6 +42,9 @@
|
|
|
40
42
|
"nitrogen": "nitrogen",
|
|
41
43
|
"typecheck": "tsc",
|
|
42
44
|
"test": "jest",
|
|
45
|
+
"test:android": "cd example/android && ./gradlew :react-native-nitro-sse:testDebugUnitTest",
|
|
46
|
+
"test:ios": "cd example/ios && xcodebuild test -workspace NitroSseExample.xcworkspace -scheme NitroSse -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 16'",
|
|
47
|
+
"test:native": "yarn test:android && yarn test:ios",
|
|
43
48
|
"release": "release-it --only-version",
|
|
44
49
|
"server": "node example/sse-server.js",
|
|
45
50
|
"lint": "eslint \"**/*.{js,ts,tsx}\""
|
|
@@ -72,8 +77,8 @@
|
|
|
72
77
|
"@eslint/compat": "^1.3.2",
|
|
73
78
|
"@eslint/eslintrc": "^3.3.1",
|
|
74
79
|
"@eslint/js": "^9.35.0",
|
|
75
|
-
"@react-native/babel-preset": "0.83.
|
|
76
|
-
"@react-native/eslint-config": "0.83.
|
|
80
|
+
"@react-native/babel-preset": "0.83.4",
|
|
81
|
+
"@react-native/eslint-config": "0.83.4",
|
|
77
82
|
"@release-it/conventional-changelog": "^10.0.1",
|
|
78
83
|
"@types/jest": "^29.5.14",
|
|
79
84
|
"@types/react": "^19.2.0",
|
|
@@ -87,7 +92,7 @@
|
|
|
87
92
|
"nitrogen": "0.35.2",
|
|
88
93
|
"prettier": "^2.8.8",
|
|
89
94
|
"react": "19.2.0",
|
|
90
|
-
"react-native": "0.83.
|
|
95
|
+
"react-native": "0.83.4",
|
|
91
96
|
"react-native-builder-bob": "^0.40.17",
|
|
92
97
|
"react-native-nitro-modules": "0.35.2",
|
|
93
98
|
"release-it": "^19.0.4",
|
package/src/SseInterface.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AnyMap } from 'react-native-nitro-modules';
|
|
1
2
|
/**
|
|
2
3
|
* HTTP method to use for the SSE connection.
|
|
3
4
|
*/
|
|
@@ -80,6 +81,12 @@ export interface SseConfig {
|
|
|
80
81
|
* @default -1
|
|
81
82
|
*/
|
|
82
83
|
maxReconnectAttempts?: number;
|
|
84
|
+
/**
|
|
85
|
+
* Whether to automatically parse message data as JSON in a background native thread.
|
|
86
|
+
* If true, and parsing succeeds, the result will be available in the 'parsedData' field.
|
|
87
|
+
* @default false
|
|
88
|
+
*/
|
|
89
|
+
autoParseJSON?: boolean;
|
|
83
90
|
/**
|
|
84
91
|
* Async interceptor called before every connection attempt (including auto-reconnects).
|
|
85
92
|
* Use this to refresh tokens or calculate dynamic headers.
|
|
@@ -94,8 +101,10 @@ export interface SseConfig {
|
|
|
94
101
|
export interface SseEvent {
|
|
95
102
|
/** The type of the event. */
|
|
96
103
|
type: SseEventType;
|
|
97
|
-
/** The data payload of the event. */
|
|
104
|
+
/** The data payload of the event as a raw string. */
|
|
98
105
|
data?: string;
|
|
106
|
+
/** The parsed JSON data, if autoParseJSON is enabled and parsing succeeds. */
|
|
107
|
+
parsedData?: AnyMap;
|
|
99
108
|
/** The event ID, if provided. */
|
|
100
109
|
id?: string;
|
|
101
110
|
/** The event name, if provided (internal 'event' field in SSE). */
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
package com.margelo.nitro.nitrosse
|
|
2
|
-
|
|
3
|
-
import org.junit.Assert.assertEquals
|
|
4
|
-
import org.junit.Test
|
|
5
|
-
|
|
6
|
-
class HeartbeatScanner {
|
|
7
|
-
private var isAtStartOfLine = true
|
|
8
|
-
var heartbeatCount = 0
|
|
9
|
-
private set
|
|
10
|
-
|
|
11
|
-
fun scan(bytes: ByteArray, length: Int) {
|
|
12
|
-
for (i in 0 until length) {
|
|
13
|
-
val b = bytes[i]
|
|
14
|
-
if (isAtStartOfLine && b == ':'.toByte()) {
|
|
15
|
-
heartbeatCount++
|
|
16
|
-
}
|
|
17
|
-
isAtStartOfLine = (b == '\n'.toByte() || b == '\r'.toByte())
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
class HeartbeatScannerTest {
|
|
23
|
-
|
|
24
|
-
@Test
|
|
25
|
-
fun testNormalHeartbeat() {
|
|
26
|
-
val scanner = HeartbeatScanner()
|
|
27
|
-
val data = ":heartbeat\n".toByteArray()
|
|
28
|
-
scanner.scan(data, data.size)
|
|
29
|
-
assertEquals(1, scanner.heartbeatCount)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
@Test
|
|
33
|
-
fun testHeartbeatAfterNewline() {
|
|
34
|
-
val scanner = HeartbeatScanner()
|
|
35
|
-
val data = "data: hello\n:heartbeat\n".toByteArray()
|
|
36
|
-
scanner.scan(data, data.size)
|
|
37
|
-
assertEquals(1, scanner.heartbeatCount)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
@Test
|
|
41
|
-
fun testSplitPacketHeartbeat() {
|
|
42
|
-
val scanner = HeartbeatScanner()
|
|
43
|
-
|
|
44
|
-
// Packet 1 ends right before heartbeat
|
|
45
|
-
val p1 = "data: hello\n".toByteArray()
|
|
46
|
-
scanner.scan(p1, p1.size)
|
|
47
|
-
|
|
48
|
-
// Packet 2 starts with heartbeat
|
|
49
|
-
val p2 = ":heartbeat\n".toByteArray()
|
|
50
|
-
scanner.scan(p2, p2.size)
|
|
51
|
-
|
|
52
|
-
assertEquals(1, scanner.heartbeatCount)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
@Test
|
|
56
|
-
fun testHeartbeatSplitAcrossPackets() {
|
|
57
|
-
val scanner = HeartbeatScanner()
|
|
58
|
-
|
|
59
|
-
// Packet 1 ends with newline
|
|
60
|
-
val p1 = "data: hello\n".toByteArray()
|
|
61
|
-
scanner.scan(p1, p1.size)
|
|
62
|
-
|
|
63
|
-
// Packet 2 starts with ':' but no more
|
|
64
|
-
val p2 = ":".toByteArray()
|
|
65
|
-
scanner.scan(p2, p2.size)
|
|
66
|
-
assertEquals(1, scanner.heartbeatCount)
|
|
67
|
-
|
|
68
|
-
// Packet 3 contains the rest of comment
|
|
69
|
-
val p3 = "heartbeat\n".toByteArray()
|
|
70
|
-
scanner.scan(p3, p3.size)
|
|
71
|
-
|
|
72
|
-
assertEquals(1, scanner.heartbeatCount)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
@Test
|
|
76
|
-
fun testColonsInDataAreNotHeartbeats() {
|
|
77
|
-
val scanner = HeartbeatScanner()
|
|
78
|
-
val data = "data: something:with:colons\n".toByteArray()
|
|
79
|
-
scanner.scan(data, data.size)
|
|
80
|
-
assertEquals(0, scanner.heartbeatCount)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
@Test
|
|
84
|
-
fun testMultipleHeartbeats() {
|
|
85
|
-
val scanner = HeartbeatScanner()
|
|
86
|
-
val data = ":h1\n:h2\n\n:h3\n".toByteArray()
|
|
87
|
-
scanner.scan(data, data.size)
|
|
88
|
-
assertEquals(3, scanner.heartbeatCount)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
@Test
|
|
92
|
-
fun testCRLFHeartbeat() {
|
|
93
|
-
val scanner = HeartbeatScanner()
|
|
94
|
-
val data = "data: ok\r\n:heartbeat\r\n".toByteArray()
|
|
95
|
-
scanner.scan(data, data.size)
|
|
96
|
-
assertEquals(1, scanner.heartbeatCount)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
package com.margelo.nitro.nitrosse
|
|
2
|
-
|
|
3
|
-
import org.junit.Assert.*
|
|
4
|
-
import org.junit.Before
|
|
5
|
-
import org.junit.Test
|
|
6
|
-
import java.util.concurrent.atomic.AtomicInteger
|
|
7
|
-
import kotlin.random.Random
|
|
8
|
-
|
|
9
|
-
class NitroSseLogicTest {
|
|
10
|
-
|
|
11
|
-
private lateinit var eventBuffer: MutableList<SseEvent>
|
|
12
|
-
private var bufferCapacity = 100
|
|
13
|
-
private var flushCount = 0
|
|
14
|
-
|
|
15
|
-
@Before
|
|
16
|
-
fun setup() {
|
|
17
|
-
eventBuffer = mutableListOf()
|
|
18
|
-
flushCount = 0
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
private fun pushEventToBuffer(event: SseEvent) {
|
|
22
|
-
synchronized(eventBuffer) {
|
|
23
|
-
eventBuffer.add(event)
|
|
24
|
-
if (eventBuffer.size >= bufferCapacity) {
|
|
25
|
-
// Simulate flush logic from NitroSse.kt
|
|
26
|
-
flushCount++
|
|
27
|
-
eventBuffer.clear()
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
@Test
|
|
33
|
-
fun testZeroLossBufferingFlush() {
|
|
34
|
-
bufferCapacity = 5
|
|
35
|
-
|
|
36
|
-
// Fill buffer to capacity
|
|
37
|
-
for (i in 1..5) {
|
|
38
|
-
pushEventToBuffer(SseEvent(SseEventType.MESSAGE, "data $i", "$i", null, null, null, null))
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
assertEquals(1, flushCount)
|
|
42
|
-
assertEquals(0, eventBuffer.size)
|
|
43
|
-
|
|
44
|
-
// Check it doesn't drop items when not at capacity
|
|
45
|
-
for (i in 1..3) {
|
|
46
|
-
pushEventToBuffer(SseEvent(SseEventType.MESSAGE, "data $i", "$i", null, null, null, null))
|
|
47
|
-
}
|
|
48
|
-
assertEquals(3, eventBuffer.size)
|
|
49
|
-
assertEquals(1, flushCount)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
@Test
|
|
53
|
-
fun testBackoffWithJitterCalculation() {
|
|
54
|
-
val retryIntervalMs = 1000L
|
|
55
|
-
val maxRetryIntervalMs = 30000L
|
|
56
|
-
val jitterFactor = 0.5
|
|
57
|
-
|
|
58
|
-
fun calculateDelay(counter: Int, jitter: Double): Long {
|
|
59
|
-
val base = Math.min(retryIntervalMs * (1 shl counter), maxRetryIntervalMs)
|
|
60
|
-
// Logic from NitroSse.kt: (base * (1.0 - jitterFactor + Random.nextDouble() * 2 * jitterFactor)).toLong()
|
|
61
|
-
return (base * (1.0 - jitterFactor + jitter * 2 * jitterFactor)).toLong()
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Test range for counter 0 (Base 1000ms)
|
|
65
|
-
// Min: 1000 * (0.5 + 0) = 500ms
|
|
66
|
-
// Max: 1000 * (0.5 + 1.0) = 1500ms
|
|
67
|
-
assertEquals(500L, calculateDelay(0, 0.0))
|
|
68
|
-
assertEquals(1500L, calculateDelay(0, 1.0))
|
|
69
|
-
|
|
70
|
-
// Test range for counter 5 (Base 32000ms -> Capped 30000ms)
|
|
71
|
-
// Min: 30000 * 0.5 = 15000ms
|
|
72
|
-
// Max: 30000 * 1.5 = 45000ms
|
|
73
|
-
assertEquals(15000L, calculateDelay(5, 0.0))
|
|
74
|
-
assertEquals(45000L, calculateDelay(5, 1.0))
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
@Test
|
|
78
|
-
fun testMaxReconnectAttemptsStop() {
|
|
79
|
-
val maxAttempts = 3
|
|
80
|
-
var currentReconnectAttempts = 0
|
|
81
|
-
var isRunning = true
|
|
82
|
-
|
|
83
|
-
fun onFailure() {
|
|
84
|
-
if (currentReconnectAttempts >= maxAttempts) {
|
|
85
|
-
isRunning = false
|
|
86
|
-
return
|
|
87
|
-
}
|
|
88
|
-
currentReconnectAttempts++
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
for (i in 1..3) {
|
|
92
|
-
onFailure()
|
|
93
|
-
}
|
|
94
|
-
assertTrue(isRunning)
|
|
95
|
-
assertEquals(3, currentReconnectAttempts)
|
|
96
|
-
|
|
97
|
-
onFailure() // 4th call, current (3) >= max (3)
|
|
98
|
-
assertFalse(isRunning)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
@Test
|
|
102
|
-
fun testAuthErrorLogic() {
|
|
103
|
-
fun shouldRetry(statusCode: Int, hasInterceptor: Boolean, consecutiveAuthErrors: Int): Boolean {
|
|
104
|
-
val maxAuthRetries = 3
|
|
105
|
-
if (statusCode == 401 || statusCode == 403) {
|
|
106
|
-
if (!hasInterceptor) return false
|
|
107
|
-
if (consecutiveAuthErrors >= maxAuthRetries) return false
|
|
108
|
-
return true
|
|
109
|
-
}
|
|
110
|
-
return false
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Case 1: 401 without interceptor -> Fail
|
|
114
|
-
assertFalse(shouldRetry(statusCode = 401, hasInterceptor = false, consecutiveAuthErrors = 0))
|
|
115
|
-
|
|
116
|
-
// Case 2: 401 with interceptor -> Retry
|
|
117
|
-
assertTrue(shouldRetry(statusCode = 401, hasInterceptor = true, consecutiveAuthErrors = 0))
|
|
118
|
-
|
|
119
|
-
// Case 3: 401 reaching limit (3) -> Fail
|
|
120
|
-
assertFalse(shouldRetry(statusCode = 401, hasInterceptor = true, consecutiveAuthErrors = 3))
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
@Test
|
|
124
|
-
fun testFatalErrorLogic() {
|
|
125
|
-
fun isFatal(statusCode: Int): Boolean {
|
|
126
|
-
return statusCode == 400 || statusCode == 204
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
assertTrue(isFatal(400))
|
|
130
|
-
assertTrue(isFatal(204))
|
|
131
|
-
assertFalse(isFatal(500))
|
|
132
|
-
assertFalse(isFatal(401))
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
@Test
|
|
136
|
-
fun testRetryAfterExtraction() {
|
|
137
|
-
fun extractMillis(headerValue: String?): Long? {
|
|
138
|
-
if (headerValue == null) return null
|
|
139
|
-
return try {
|
|
140
|
-
headerValue.toLong() * 1000L
|
|
141
|
-
} catch (e: Exception) {
|
|
142
|
-
null // Simplifying for logic test
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
assertEquals(5000L, extractMillis("5"))
|
|
147
|
-
assertEquals(60000L, extractMillis("60"))
|
|
148
|
-
|
|
149
|
-
// Mocking the date parsing behavior from NitroSse.kt
|
|
150
|
-
fun extractDateMillis(headerValue: String?): Long? {
|
|
151
|
-
if (headerValue == "Wed, 21 Oct 2015 07:28:00 GMT") return 10000L // Mocked diff
|
|
152
|
-
return null
|
|
153
|
-
}
|
|
154
|
-
assertEquals(10000L, extractDateMillis("Wed, 21 Oct 2015 07:28:00 GMT"))
|
|
155
|
-
|
|
156
|
-
assertNull(extractMillis(null))
|
|
157
|
-
assertNull(extractMillis("invalid"))
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
@Test
|
|
161
|
-
fun testStartIdempotency() {
|
|
162
|
-
val isRunning = AtomicInteger(0)
|
|
163
|
-
|
|
164
|
-
fun start() {
|
|
165
|
-
// Simulated compareAndSet(false, true) logic
|
|
166
|
-
if (isRunning.get() == 0) {
|
|
167
|
-
isRunning.set(1)
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
start()
|
|
172
|
-
assertEquals(1, isRunning.get())
|
|
173
|
-
start()
|
|
174
|
-
assertEquals(1, isRunning.get()) // Should not have changed
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
@Test
|
|
178
|
-
fun testConcurrentStartStopVersioning() {
|
|
179
|
-
val connectionAttemptVersion = AtomicInteger(0)
|
|
180
|
-
var executeCount = 0
|
|
181
|
-
|
|
182
|
-
fun start(): Int {
|
|
183
|
-
return connectionAttemptVersion.get()
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
fun stop() {
|
|
187
|
-
connectionAttemptVersion.incrementAndGet()
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
fun finishConnection(version: Int) {
|
|
191
|
-
if (version == connectionAttemptVersion.get()) {
|
|
192
|
-
executeCount++
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// 1. User starts
|
|
197
|
-
val v1 = start()
|
|
198
|
-
|
|
199
|
-
// 2. User stops immediately
|
|
200
|
-
stop()
|
|
201
|
-
|
|
202
|
-
// 3. Logic tries to finish with old version
|
|
203
|
-
finishConnection(v1)
|
|
204
|
-
assertEquals(0, executeCount)
|
|
205
|
-
|
|
206
|
-
// 4. New start
|
|
207
|
-
val v2 = start()
|
|
208
|
-
finishConnection(v2)
|
|
209
|
-
assertEquals(1, executeCount)
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
@Test
|
|
213
|
-
fun testStatsPersistenceAcrossAttempts() {
|
|
214
|
-
var totalBytes = 0L
|
|
215
|
-
var reconnectCount = 0
|
|
216
|
-
|
|
217
|
-
fun recordAttempt(bytes: Long) {
|
|
218
|
-
reconnectCount++
|
|
219
|
-
totalBytes += bytes
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
recordAttempt(1024)
|
|
223
|
-
recordAttempt(2048)
|
|
224
|
-
|
|
225
|
-
assertEquals(2, reconnectCount)
|
|
226
|
-
assertEquals(3072L, totalBytes)
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
@Test
|
|
230
|
-
fun testHeartbeatDetectionCount() {
|
|
231
|
-
val totalBytes = 100L
|
|
232
|
-
val comment = ":ping"
|
|
233
|
-
|
|
234
|
-
// Just verify math logic for byte counting in Network Interceptor
|
|
235
|
-
val newTotal = totalBytes + comment.toByteArray(Charsets.UTF_8).size
|
|
236
|
-
assertEquals(105L, newTotal)
|
|
237
|
-
}
|
|
238
|
-
}
|