react-native-nitro-sse 3.0.0-beta.2 → 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.
Files changed (50) hide show
  1. package/NitroSse.podspec +0 -1
  2. package/README.md +44 -24
  3. package/android/build.gradle +0 -1
  4. package/android/src/main/java/com/margelo/nitro/nitrosse/JsonUtils.kt +33 -10
  5. package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +464 -130
  6. package/android/src/main/java/com/margelo/nitro/nitrosse/SseConnectionHandler.kt +420 -88
  7. package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventBuffer.kt +23 -0
  8. package/android/src/main/java/com/margelo/nitro/nitrosse/SseNetworkMonitor.kt +29 -2
  9. package/android/src/main/java/com/margelo/nitro/nitrosse/SseReconnectStrategy.kt +9 -0
  10. package/ios/NitroSse.swift +353 -64
  11. package/ios/SseConfig+CopyWith.swift +35 -19
  12. package/ios/SseConnectionHandler.swift +647 -97
  13. package/ios/SseEventBuffer.swift +19 -1
  14. package/ios/SseReconnectStrategy.swift +26 -7
  15. package/lib/module/MockSseEngine.js +72 -8
  16. package/lib/module/NitroSseClient.js +79 -8
  17. package/lib/module/createNitroSse.js +24 -0
  18. package/lib/module/index.js +1 -22
  19. package/lib/module/useNitroSse.js +22 -5
  20. package/lib/typescript/src/MockSseEngine.d.ts +16 -0
  21. package/lib/typescript/src/NitroSse.nitro.d.ts +2 -2
  22. package/lib/typescript/src/NitroSseClient.d.ts +7 -3
  23. package/lib/typescript/src/SseInterface.d.ts +60 -10
  24. package/lib/typescript/src/createNitroSse.d.ts +9 -0
  25. package/lib/typescript/src/index.d.ts +1 -8
  26. package/lib/typescript/src/useNitroSse.d.ts +2 -2
  27. package/nitrogen/generated/android/c++/JHybridNitroSseSpec.cpp +12 -8
  28. package/nitrogen/generated/android/c++/JHybridNitroSseSpec.hpp +1 -1
  29. package/nitrogen/generated/android/c++/JSseConfig.hpp +1 -18
  30. package/nitrogen/generated/android/c++/JSseDisconnectReason.hpp +67 -0
  31. package/nitrogen/generated/android/c++/JSseStats.hpp +87 -1
  32. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/HybridNitroSseSpec.kt +4 -3
  33. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseConfig.kt +4 -13
  34. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseDisconnectReason.kt +26 -0
  35. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseStats.kt +108 -3
  36. package/nitrogen/generated/android/nitrosseOnLoad.cpp +2 -2
  37. package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.cpp +8 -8
  38. package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +114 -96
  39. package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Umbrella.hpp +3 -0
  40. package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +7 -4
  41. package/nitrogen/generated/ios/swift/HybridNitroSseSpec.swift +1 -1
  42. package/nitrogen/generated/ios/swift/HybridNitroSseSpec_cxx.swift +34 -1
  43. package/nitrogen/generated/ios/swift/SseConfig.swift +1 -48
  44. package/nitrogen/generated/ios/swift/SseDisconnectReason.swift +52 -0
  45. package/nitrogen/generated/ios/swift/SseStats.swift +217 -2
  46. package/nitrogen/generated/shared/c++/HybridNitroSseSpec.hpp +3 -1
  47. package/nitrogen/generated/shared/c++/SseConfig.hpp +2 -8
  48. package/nitrogen/generated/shared/c++/SseDisconnectReason.hpp +88 -0
  49. package/nitrogen/generated/shared/c++/SseStats.hpp +88 -2
  50. package/package.json +2 -2
@@ -18,8 +18,62 @@ public extension SseStats {
18
18
  /**
19
19
  * Create a new instance of `SseStats`.
20
20
  */
21
- init(totalBytesReceived: Double, reconnectCount: Double, lastErrorTime: Double?, lastErrorCode: String?) {
22
- self.init(totalBytesReceived, reconnectCount, { () -> bridge.std__optional_double_ in
21
+ init(rawBytesReceived: Double, decompressedBytesReceived: Double?, totalBytesReceived: Double, chunksReceived: Double, lastStatusCode: Double?, totalEventsReceived: Double, commentsReceived: Double, linesParsed: Double, parseErrors: Double, serverRetryDelayMs: Double?, connectedAt: Double?, timeToFirstByteMs: Double?, lastEventTime: Double?, lastHeartbeatTime: Double?, maxEventGapMs: Double, eventsBuffered: Double, peakBufferedEvents: Double, bufferFlushCount: Double, bufferOverflowCount: Double, connectionAttempts: Double, reconnectCount: Double, lastReconnectDelayMs: Double?, disconnectReason: SseDisconnectReason?, lastErrorTime: Double?, lastErrorCode: String?) {
22
+ self.init(rawBytesReceived, { () -> bridge.std__optional_double_ in
23
+ if let __unwrappedValue = decompressedBytesReceived {
24
+ return bridge.create_std__optional_double_(__unwrappedValue)
25
+ } else {
26
+ return .init()
27
+ }
28
+ }(), totalBytesReceived, chunksReceived, { () -> bridge.std__optional_double_ in
29
+ if let __unwrappedValue = lastStatusCode {
30
+ return bridge.create_std__optional_double_(__unwrappedValue)
31
+ } else {
32
+ return .init()
33
+ }
34
+ }(), totalEventsReceived, commentsReceived, linesParsed, parseErrors, { () -> bridge.std__optional_double_ in
35
+ if let __unwrappedValue = serverRetryDelayMs {
36
+ return bridge.create_std__optional_double_(__unwrappedValue)
37
+ } else {
38
+ return .init()
39
+ }
40
+ }(), { () -> bridge.std__optional_double_ in
41
+ if let __unwrappedValue = connectedAt {
42
+ return bridge.create_std__optional_double_(__unwrappedValue)
43
+ } else {
44
+ return .init()
45
+ }
46
+ }(), { () -> bridge.std__optional_double_ in
47
+ if let __unwrappedValue = timeToFirstByteMs {
48
+ return bridge.create_std__optional_double_(__unwrappedValue)
49
+ } else {
50
+ return .init()
51
+ }
52
+ }(), { () -> bridge.std__optional_double_ in
53
+ if let __unwrappedValue = lastEventTime {
54
+ return bridge.create_std__optional_double_(__unwrappedValue)
55
+ } else {
56
+ return .init()
57
+ }
58
+ }(), { () -> bridge.std__optional_double_ in
59
+ if let __unwrappedValue = lastHeartbeatTime {
60
+ return bridge.create_std__optional_double_(__unwrappedValue)
61
+ } else {
62
+ return .init()
63
+ }
64
+ }(), maxEventGapMs, eventsBuffered, peakBufferedEvents, bufferFlushCount, bufferOverflowCount, connectionAttempts, reconnectCount, { () -> bridge.std__optional_double_ in
65
+ if let __unwrappedValue = lastReconnectDelayMs {
66
+ return bridge.create_std__optional_double_(__unwrappedValue)
67
+ } else {
68
+ return .init()
69
+ }
70
+ }(), { () -> bridge.std__optional_SseDisconnectReason_ in
71
+ if let __unwrappedValue = disconnectReason {
72
+ return bridge.create_std__optional_SseDisconnectReason_(__unwrappedValue)
73
+ } else {
74
+ return .init()
75
+ }
76
+ }(), { () -> bridge.std__optional_double_ in
23
77
  if let __unwrappedValue = lastErrorTime {
24
78
  return bridge.create_std__optional_double_(__unwrappedValue)
25
79
  } else {
@@ -34,16 +88,177 @@ public extension SseStats {
34
88
  }())
35
89
  }
36
90
 
91
+ @inline(__always)
92
+ var rawBytesReceived: Double {
93
+ return self.__rawBytesReceived
94
+ }
95
+
96
+ @inline(__always)
97
+ var decompressedBytesReceived: Double? {
98
+ return { () -> Double? in
99
+ if bridge.has_value_std__optional_double_(self.__decompressedBytesReceived) {
100
+ let __unwrapped = bridge.get_std__optional_double_(self.__decompressedBytesReceived)
101
+ return __unwrapped
102
+ } else {
103
+ return nil
104
+ }
105
+ }()
106
+ }
107
+
37
108
  @inline(__always)
38
109
  var totalBytesReceived: Double {
39
110
  return self.__totalBytesReceived
40
111
  }
41
112
 
113
+ @inline(__always)
114
+ var chunksReceived: Double {
115
+ return self.__chunksReceived
116
+ }
117
+
118
+ @inline(__always)
119
+ var lastStatusCode: Double? {
120
+ return { () -> Double? in
121
+ if bridge.has_value_std__optional_double_(self.__lastStatusCode) {
122
+ let __unwrapped = bridge.get_std__optional_double_(self.__lastStatusCode)
123
+ return __unwrapped
124
+ } else {
125
+ return nil
126
+ }
127
+ }()
128
+ }
129
+
130
+ @inline(__always)
131
+ var totalEventsReceived: Double {
132
+ return self.__totalEventsReceived
133
+ }
134
+
135
+ @inline(__always)
136
+ var commentsReceived: Double {
137
+ return self.__commentsReceived
138
+ }
139
+
140
+ @inline(__always)
141
+ var linesParsed: Double {
142
+ return self.__linesParsed
143
+ }
144
+
145
+ @inline(__always)
146
+ var parseErrors: Double {
147
+ return self.__parseErrors
148
+ }
149
+
150
+ @inline(__always)
151
+ var serverRetryDelayMs: Double? {
152
+ return { () -> Double? in
153
+ if bridge.has_value_std__optional_double_(self.__serverRetryDelayMs) {
154
+ let __unwrapped = bridge.get_std__optional_double_(self.__serverRetryDelayMs)
155
+ return __unwrapped
156
+ } else {
157
+ return nil
158
+ }
159
+ }()
160
+ }
161
+
162
+ @inline(__always)
163
+ var connectedAt: Double? {
164
+ return { () -> Double? in
165
+ if bridge.has_value_std__optional_double_(self.__connectedAt) {
166
+ let __unwrapped = bridge.get_std__optional_double_(self.__connectedAt)
167
+ return __unwrapped
168
+ } else {
169
+ return nil
170
+ }
171
+ }()
172
+ }
173
+
174
+ @inline(__always)
175
+ var timeToFirstByteMs: Double? {
176
+ return { () -> Double? in
177
+ if bridge.has_value_std__optional_double_(self.__timeToFirstByteMs) {
178
+ let __unwrapped = bridge.get_std__optional_double_(self.__timeToFirstByteMs)
179
+ return __unwrapped
180
+ } else {
181
+ return nil
182
+ }
183
+ }()
184
+ }
185
+
186
+ @inline(__always)
187
+ var lastEventTime: Double? {
188
+ return { () -> Double? in
189
+ if bridge.has_value_std__optional_double_(self.__lastEventTime) {
190
+ let __unwrapped = bridge.get_std__optional_double_(self.__lastEventTime)
191
+ return __unwrapped
192
+ } else {
193
+ return nil
194
+ }
195
+ }()
196
+ }
197
+
198
+ @inline(__always)
199
+ var lastHeartbeatTime: Double? {
200
+ return { () -> Double? in
201
+ if bridge.has_value_std__optional_double_(self.__lastHeartbeatTime) {
202
+ let __unwrapped = bridge.get_std__optional_double_(self.__lastHeartbeatTime)
203
+ return __unwrapped
204
+ } else {
205
+ return nil
206
+ }
207
+ }()
208
+ }
209
+
210
+ @inline(__always)
211
+ var maxEventGapMs: Double {
212
+ return self.__maxEventGapMs
213
+ }
214
+
215
+ @inline(__always)
216
+ var eventsBuffered: Double {
217
+ return self.__eventsBuffered
218
+ }
219
+
220
+ @inline(__always)
221
+ var peakBufferedEvents: Double {
222
+ return self.__peakBufferedEvents
223
+ }
224
+
225
+ @inline(__always)
226
+ var bufferFlushCount: Double {
227
+ return self.__bufferFlushCount
228
+ }
229
+
230
+ @inline(__always)
231
+ var bufferOverflowCount: Double {
232
+ return self.__bufferOverflowCount
233
+ }
234
+
235
+ @inline(__always)
236
+ var connectionAttempts: Double {
237
+ return self.__connectionAttempts
238
+ }
239
+
42
240
  @inline(__always)
43
241
  var reconnectCount: Double {
44
242
  return self.__reconnectCount
45
243
  }
46
244
 
245
+ @inline(__always)
246
+ var lastReconnectDelayMs: Double? {
247
+ return { () -> Double? in
248
+ if bridge.has_value_std__optional_double_(self.__lastReconnectDelayMs) {
249
+ let __unwrapped = bridge.get_std__optional_double_(self.__lastReconnectDelayMs)
250
+ return __unwrapped
251
+ } else {
252
+ return nil
253
+ }
254
+ }()
255
+ }
256
+
257
+ @inline(__always)
258
+ var disconnectReason: SseDisconnectReason? {
259
+ return self.__disconnectReason.value
260
+ }
261
+
47
262
  @inline(__always)
48
263
  var lastErrorTime: Double? {
49
264
  return { () -> Double? in
@@ -28,6 +28,8 @@ namespace margelo::nitro::nitrosse { enum class SseState; }
28
28
  #include <functional>
29
29
  #include <string>
30
30
  #include <unordered_map>
31
+ #include <NitroModules/Promise.hpp>
32
+ #include <optional>
31
33
  #include "SseStats.hpp"
32
34
  #include "SseState.hpp"
33
35
 
@@ -62,7 +64,7 @@ namespace margelo::nitro::nitrosse {
62
64
 
63
65
  public:
64
66
  // Methods
65
- virtual void setup(const SseConfig& config, const std::function<void(const std::vector<SseEvent>& /* events */)>& onEvent) = 0;
67
+ virtual void setup(const SseConfig& config, const std::function<void(const std::vector<SseEvent>& /* events */)>& onEvent, const std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>>& onBeforeRequest) = 0;
66
68
  virtual void start() = 0;
67
69
  virtual void stop() = 0;
68
70
  virtual void setLastProcessedId(const std::string& id) = 0;
@@ -37,8 +37,6 @@ namespace margelo::nitro::nitrosse { struct SseMockConfig; }
37
37
  #include "HttpMethod.hpp"
38
38
  #include <optional>
39
39
  #include <unordered_map>
40
- #include <NitroModules/Promise.hpp>
41
- #include <functional>
42
40
  #include "SseMockConfig.hpp"
43
41
 
44
42
  namespace margelo::nitro::nitrosse {
@@ -64,15 +62,14 @@ namespace margelo::nitro::nitrosse {
64
62
  std::optional<double> maxAuthRetries SWIFT_PRIVATE;
65
63
  std::optional<bool> autoParseJSON SWIFT_PRIVATE;
66
64
  std::optional<bool> monitorNetwork SWIFT_PRIVATE;
67
- std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>> onBeforeRequest SWIFT_PRIVATE;
68
65
  std::optional<SseMockConfig> mock SWIFT_PRIVATE;
69
66
 
70
67
  public:
71
68
  SseConfig() = default;
72
- 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<double> maxAuthRetries, std::optional<bool> autoParseJSON, std::optional<bool> monitorNetwork, std::optional<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::unordered_map<std::string, std::string>>>>>()>> onBeforeRequest, std::optional<SseMockConfig> mock): 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), maxAuthRetries(maxAuthRetries), autoParseJSON(autoParseJSON), monitorNetwork(monitorNetwork), onBeforeRequest(onBeforeRequest), mock(mock) {}
69
+ 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<double> maxAuthRetries, std::optional<bool> autoParseJSON, std::optional<bool> monitorNetwork, std::optional<SseMockConfig> mock): 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), maxAuthRetries(maxAuthRetries), autoParseJSON(autoParseJSON), monitorNetwork(monitorNetwork), mock(mock) {}
73
70
 
74
71
  public:
75
- // SseConfig is not equatable because these properties are not equatable: onBeforeRequest
72
+ friend bool operator==(const SseConfig& lhs, const SseConfig& rhs) = default;
76
73
  };
77
74
 
78
75
  } // namespace margelo::nitro::nitrosse
@@ -101,7 +98,6 @@ namespace margelo::nitro {
101
98
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxAuthRetries"))),
102
99
  JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "autoParseJSON"))),
103
100
  JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "monitorNetwork"))),
104
- 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"))),
105
101
  JSIConverter<std::optional<margelo::nitro::nitrosse::SseMockConfig>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "mock")))
106
102
  );
107
103
  }
@@ -123,7 +119,6 @@ namespace margelo::nitro {
123
119
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "maxAuthRetries"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.maxAuthRetries));
124
120
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "autoParseJSON"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.autoParseJSON));
125
121
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "monitorNetwork"), JSIConverter<std::optional<bool>>::toJSI(runtime, arg.monitorNetwork));
126
- 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));
127
122
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "mock"), JSIConverter<std::optional<margelo::nitro::nitrosse::SseMockConfig>>::toJSI(runtime, arg.mock));
128
123
  return obj;
129
124
  }
@@ -151,7 +146,6 @@ namespace margelo::nitro {
151
146
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxAuthRetries")))) return false;
152
147
  if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "autoParseJSON")))) return false;
153
148
  if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "monitorNetwork")))) return false;
154
- 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;
155
149
  if (!JSIConverter<std::optional<margelo::nitro::nitrosse::SseMockConfig>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "mock")))) return false;
156
150
  return true;
157
151
  }
@@ -0,0 +1,88 @@
1
+ ///
2
+ /// SseDisconnectReason.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/NitroHash.hpp>)
11
+ #include <NitroModules/NitroHash.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
16
+ #include <NitroModules/JSIConverter.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
21
+ #include <NitroModules/NitroDefines.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+
26
+ namespace margelo::nitro::nitrosse {
27
+
28
+ /**
29
+ * An enum which can be represented as a JavaScript union (SseDisconnectReason).
30
+ */
31
+ enum class SseDisconnectReason {
32
+ USER_STOP SWIFT_NAME(userStop) = 0,
33
+ NETWORK_ERROR SWIFT_NAME(networkError) = 1,
34
+ SERVER_ERROR SWIFT_NAME(serverError) = 2,
35
+ PARSER_ERROR SWIFT_NAME(parserError) = 3,
36
+ TIMEOUT SWIFT_NAME(timeout) = 4,
37
+ } CLOSED_ENUM;
38
+
39
+ } // namespace margelo::nitro::nitrosse
40
+
41
+ namespace margelo::nitro {
42
+
43
+ // C++ SseDisconnectReason <> JS SseDisconnectReason (union)
44
+ template <>
45
+ struct JSIConverter<margelo::nitro::nitrosse::SseDisconnectReason> final {
46
+ static inline margelo::nitro::nitrosse::SseDisconnectReason fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
47
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
48
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
49
+ case hashString("user_stop"): return margelo::nitro::nitrosse::SseDisconnectReason::USER_STOP;
50
+ case hashString("network_error"): return margelo::nitro::nitrosse::SseDisconnectReason::NETWORK_ERROR;
51
+ case hashString("server_error"): return margelo::nitro::nitrosse::SseDisconnectReason::SERVER_ERROR;
52
+ case hashString("parser_error"): return margelo::nitro::nitrosse::SseDisconnectReason::PARSER_ERROR;
53
+ case hashString("timeout"): return margelo::nitro::nitrosse::SseDisconnectReason::TIMEOUT;
54
+ default: [[unlikely]]
55
+ throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum SseDisconnectReason - invalid value!");
56
+ }
57
+ }
58
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::nitrosse::SseDisconnectReason arg) {
59
+ switch (arg) {
60
+ case margelo::nitro::nitrosse::SseDisconnectReason::USER_STOP: return JSIConverter<std::string>::toJSI(runtime, "user_stop");
61
+ case margelo::nitro::nitrosse::SseDisconnectReason::NETWORK_ERROR: return JSIConverter<std::string>::toJSI(runtime, "network_error");
62
+ case margelo::nitro::nitrosse::SseDisconnectReason::SERVER_ERROR: return JSIConverter<std::string>::toJSI(runtime, "server_error");
63
+ case margelo::nitro::nitrosse::SseDisconnectReason::PARSER_ERROR: return JSIConverter<std::string>::toJSI(runtime, "parser_error");
64
+ case margelo::nitro::nitrosse::SseDisconnectReason::TIMEOUT: return JSIConverter<std::string>::toJSI(runtime, "timeout");
65
+ default: [[unlikely]]
66
+ throw std::invalid_argument("Cannot convert SseDisconnectReason to JS - invalid value: "
67
+ + std::to_string(static_cast<int>(arg)) + "!");
68
+ }
69
+ }
70
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
71
+ if (!value.isString()) {
72
+ return false;
73
+ }
74
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
75
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
76
+ case hashString("user_stop"):
77
+ case hashString("network_error"):
78
+ case hashString("server_error"):
79
+ case hashString("parser_error"):
80
+ case hashString("timeout"):
81
+ return true;
82
+ default:
83
+ return false;
84
+ }
85
+ }
86
+ };
87
+
88
+ } // namespace margelo::nitro
@@ -28,9 +28,11 @@
28
28
  #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
29
29
  #endif
30
30
 
31
-
31
+ // Forward declaration of `SseDisconnectReason` to properly resolve imports.
32
+ namespace margelo::nitro::nitrosse { enum class SseDisconnectReason; }
32
33
 
33
34
  #include <optional>
35
+ #include "SseDisconnectReason.hpp"
34
36
  #include <string>
35
37
 
36
38
  namespace margelo::nitro::nitrosse {
@@ -40,14 +42,35 @@ namespace margelo::nitro::nitrosse {
40
42
  */
41
43
  struct SseStats final {
42
44
  public:
45
+ double rawBytesReceived SWIFT_PRIVATE;
46
+ std::optional<double> decompressedBytesReceived SWIFT_PRIVATE;
43
47
  double totalBytesReceived SWIFT_PRIVATE;
48
+ double chunksReceived SWIFT_PRIVATE;
49
+ std::optional<double> lastStatusCode SWIFT_PRIVATE;
50
+ double totalEventsReceived SWIFT_PRIVATE;
51
+ double commentsReceived SWIFT_PRIVATE;
52
+ double linesParsed SWIFT_PRIVATE;
53
+ double parseErrors SWIFT_PRIVATE;
54
+ std::optional<double> serverRetryDelayMs SWIFT_PRIVATE;
55
+ std::optional<double> connectedAt SWIFT_PRIVATE;
56
+ std::optional<double> timeToFirstByteMs SWIFT_PRIVATE;
57
+ std::optional<double> lastEventTime SWIFT_PRIVATE;
58
+ std::optional<double> lastHeartbeatTime SWIFT_PRIVATE;
59
+ double maxEventGapMs SWIFT_PRIVATE;
60
+ double eventsBuffered SWIFT_PRIVATE;
61
+ double peakBufferedEvents SWIFT_PRIVATE;
62
+ double bufferFlushCount SWIFT_PRIVATE;
63
+ double bufferOverflowCount SWIFT_PRIVATE;
64
+ double connectionAttempts SWIFT_PRIVATE;
44
65
  double reconnectCount SWIFT_PRIVATE;
66
+ std::optional<double> lastReconnectDelayMs SWIFT_PRIVATE;
67
+ std::optional<SseDisconnectReason> disconnectReason SWIFT_PRIVATE;
45
68
  std::optional<double> lastErrorTime SWIFT_PRIVATE;
46
69
  std::optional<std::string> lastErrorCode SWIFT_PRIVATE;
47
70
 
48
71
  public:
49
72
  SseStats() = default;
50
- explicit SseStats(double totalBytesReceived, double reconnectCount, std::optional<double> lastErrorTime, std::optional<std::string> lastErrorCode): totalBytesReceived(totalBytesReceived), reconnectCount(reconnectCount), lastErrorTime(lastErrorTime), lastErrorCode(lastErrorCode) {}
73
+ explicit SseStats(double rawBytesReceived, std::optional<double> decompressedBytesReceived, double totalBytesReceived, double chunksReceived, std::optional<double> lastStatusCode, double totalEventsReceived, double commentsReceived, double linesParsed, double parseErrors, std::optional<double> serverRetryDelayMs, std::optional<double> connectedAt, std::optional<double> timeToFirstByteMs, std::optional<double> lastEventTime, std::optional<double> lastHeartbeatTime, double maxEventGapMs, double eventsBuffered, double peakBufferedEvents, double bufferFlushCount, double bufferOverflowCount, double connectionAttempts, double reconnectCount, std::optional<double> lastReconnectDelayMs, std::optional<SseDisconnectReason> disconnectReason, std::optional<double> lastErrorTime, std::optional<std::string> lastErrorCode): rawBytesReceived(rawBytesReceived), decompressedBytesReceived(decompressedBytesReceived), totalBytesReceived(totalBytesReceived), chunksReceived(chunksReceived), lastStatusCode(lastStatusCode), totalEventsReceived(totalEventsReceived), commentsReceived(commentsReceived), linesParsed(linesParsed), parseErrors(parseErrors), serverRetryDelayMs(serverRetryDelayMs), connectedAt(connectedAt), timeToFirstByteMs(timeToFirstByteMs), lastEventTime(lastEventTime), lastHeartbeatTime(lastHeartbeatTime), maxEventGapMs(maxEventGapMs), eventsBuffered(eventsBuffered), peakBufferedEvents(peakBufferedEvents), bufferFlushCount(bufferFlushCount), bufferOverflowCount(bufferOverflowCount), connectionAttempts(connectionAttempts), reconnectCount(reconnectCount), lastReconnectDelayMs(lastReconnectDelayMs), disconnectReason(disconnectReason), lastErrorTime(lastErrorTime), lastErrorCode(lastErrorCode) {}
51
74
 
52
75
  public:
53
76
  friend bool operator==(const SseStats& lhs, const SseStats& rhs) = default;
@@ -63,16 +86,58 @@ namespace margelo::nitro {
63
86
  static inline margelo::nitro::nitrosse::SseStats fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
64
87
  jsi::Object obj = arg.asObject(runtime);
65
88
  return margelo::nitro::nitrosse::SseStats(
89
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "rawBytesReceived"))),
90
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "decompressedBytesReceived"))),
66
91
  JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "totalBytesReceived"))),
92
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "chunksReceived"))),
93
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastStatusCode"))),
94
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "totalEventsReceived"))),
95
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "commentsReceived"))),
96
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "linesParsed"))),
97
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "parseErrors"))),
98
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "serverRetryDelayMs"))),
99
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "connectedAt"))),
100
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "timeToFirstByteMs"))),
101
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastEventTime"))),
102
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastHeartbeatTime"))),
103
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxEventGapMs"))),
104
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "eventsBuffered"))),
105
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "peakBufferedEvents"))),
106
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "bufferFlushCount"))),
107
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "bufferOverflowCount"))),
108
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "connectionAttempts"))),
67
109
  JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "reconnectCount"))),
110
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastReconnectDelayMs"))),
111
+ JSIConverter<std::optional<margelo::nitro::nitrosse::SseDisconnectReason>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "disconnectReason"))),
68
112
  JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastErrorTime"))),
69
113
  JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastErrorCode")))
70
114
  );
71
115
  }
72
116
  static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::nitrosse::SseStats& arg) {
73
117
  jsi::Object obj(runtime);
118
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "rawBytesReceived"), JSIConverter<double>::toJSI(runtime, arg.rawBytesReceived));
119
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "decompressedBytesReceived"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.decompressedBytesReceived));
74
120
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "totalBytesReceived"), JSIConverter<double>::toJSI(runtime, arg.totalBytesReceived));
121
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "chunksReceived"), JSIConverter<double>::toJSI(runtime, arg.chunksReceived));
122
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "lastStatusCode"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.lastStatusCode));
123
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "totalEventsReceived"), JSIConverter<double>::toJSI(runtime, arg.totalEventsReceived));
124
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "commentsReceived"), JSIConverter<double>::toJSI(runtime, arg.commentsReceived));
125
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "linesParsed"), JSIConverter<double>::toJSI(runtime, arg.linesParsed));
126
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "parseErrors"), JSIConverter<double>::toJSI(runtime, arg.parseErrors));
127
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "serverRetryDelayMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.serverRetryDelayMs));
128
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "connectedAt"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.connectedAt));
129
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "timeToFirstByteMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.timeToFirstByteMs));
130
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "lastEventTime"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.lastEventTime));
131
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "lastHeartbeatTime"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.lastHeartbeatTime));
132
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "maxEventGapMs"), JSIConverter<double>::toJSI(runtime, arg.maxEventGapMs));
133
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "eventsBuffered"), JSIConverter<double>::toJSI(runtime, arg.eventsBuffered));
134
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "peakBufferedEvents"), JSIConverter<double>::toJSI(runtime, arg.peakBufferedEvents));
135
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "bufferFlushCount"), JSIConverter<double>::toJSI(runtime, arg.bufferFlushCount));
136
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "bufferOverflowCount"), JSIConverter<double>::toJSI(runtime, arg.bufferOverflowCount));
137
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "connectionAttempts"), JSIConverter<double>::toJSI(runtime, arg.connectionAttempts));
75
138
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "reconnectCount"), JSIConverter<double>::toJSI(runtime, arg.reconnectCount));
139
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "lastReconnectDelayMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.lastReconnectDelayMs));
140
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "disconnectReason"), JSIConverter<std::optional<margelo::nitro::nitrosse::SseDisconnectReason>>::toJSI(runtime, arg.disconnectReason));
76
141
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "lastErrorTime"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.lastErrorTime));
77
142
  obj.setProperty(runtime, PropNameIDCache::get(runtime, "lastErrorCode"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.lastErrorCode));
78
143
  return obj;
@@ -85,8 +150,29 @@ namespace margelo::nitro {
85
150
  if (!nitro::isPlainObject(runtime, obj)) {
86
151
  return false;
87
152
  }
153
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "rawBytesReceived")))) return false;
154
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "decompressedBytesReceived")))) return false;
88
155
  if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "totalBytesReceived")))) return false;
156
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "chunksReceived")))) return false;
157
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastStatusCode")))) return false;
158
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "totalEventsReceived")))) return false;
159
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "commentsReceived")))) return false;
160
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "linesParsed")))) return false;
161
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "parseErrors")))) return false;
162
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "serverRetryDelayMs")))) return false;
163
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "connectedAt")))) return false;
164
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "timeToFirstByteMs")))) return false;
165
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastEventTime")))) return false;
166
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastHeartbeatTime")))) return false;
167
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "maxEventGapMs")))) return false;
168
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "eventsBuffered")))) return false;
169
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "peakBufferedEvents")))) return false;
170
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "bufferFlushCount")))) return false;
171
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "bufferOverflowCount")))) return false;
172
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "connectionAttempts")))) return false;
89
173
  if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "reconnectCount")))) return false;
174
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastReconnectDelayMs")))) return false;
175
+ if (!JSIConverter<std::optional<margelo::nitro::nitrosse::SseDisconnectReason>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "disconnectReason")))) return false;
90
176
  if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastErrorTime")))) return false;
91
177
  if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "lastErrorCode")))) return false;
92
178
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-sse",
3
- "version": "3.0.0-beta.2",
3
+ "version": "3.1.0",
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",
@@ -43,7 +43,7 @@
43
43
  "test:android": "(cd example/android && ./gradlew :react-native-nitro-sse:testDebugUnitTest)",
44
44
  "test:ios": "(cd example/ios && xcodebuild test -workspace NitroSseExample.xcworkspace -scheme NitroSse-Unit-Tests -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 16e')",
45
45
  "test:native": "yarn test:android && yarn test:ios",
46
- "ci": "yarn lint && yarn typecheck && yarn test && yarn prepare && (cd example/ios && pod install) && yarn test:native && yarn turbo run build:android build:ios",
46
+ "ci": "yarn lint && yarn typecheck && yarn test && yarn prepare && (cd example/ios && pod install) && yarn test:native && yarn turbo run build:android build:ios && node -e \"console.log('Everything \\x1b[1;32msuccessful\\x1b[0m')\"",
47
47
  "release": "release-it --only-version",
48
48
  "server": "node example/script/sse-server.mjs",
49
49
  "bench:server": "node example/script/sse-benchmark-server.mjs",