react-native-nitro-sse 3.2.0-beta.1 → 3.3.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 (109) hide show
  1. package/README.md +50 -27
  2. package/android/build.gradle +7 -3
  3. package/android/src/main/java/com/margelo/nitro/nitrosse/JsonUtils.kt +8 -32
  4. package/android/src/main/java/com/margelo/nitro/nitrosse/NetworkInspector.kt +124 -6
  5. package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +439 -509
  6. package/android/src/main/java/com/margelo/nitro/nitrosse/SseConnectionHandler.kt +137 -316
  7. package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventBuffer.kt +18 -8
  8. package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventReader.kt +231 -0
  9. package/android/src/main/java/com/margelo/nitro/nitrosse/SseFailureClassifier.kt +245 -0
  10. package/android/src/main/java/com/margelo/nitro/nitrosse/SseMetrics.kt +237 -0
  11. package/android/src/main/java/com/margelo/nitro/nitrosse/SseNetworkMonitor.kt +13 -6
  12. package/android/src/main/java/com/margelo/nitro/nitrosse/SseReconnectStrategy.kt +48 -22
  13. package/ios/NitroSse.swift +402 -484
  14. package/ios/SseConfig+CopyWith.swift +0 -2
  15. package/ios/SseConnectionHandler.swift +238 -446
  16. package/ios/SseDispatchQueueDispatcher.swift +7 -7
  17. package/ios/SseDispatcher.swift +5 -4
  18. package/ios/SseEventBuffer.swift +24 -23
  19. package/ios/SseEventParser.swift +360 -0
  20. package/ios/SseFailureClassifier.swift +249 -0
  21. package/ios/SseLifecycleManager.swift +19 -14
  22. package/ios/SseMetrics.swift +188 -0
  23. package/ios/SseNetworkMonitor.swift +18 -14
  24. package/ios/SseReconnectStrategy.swift +28 -30
  25. package/lib/module/FetchSseDriver.js +398 -170
  26. package/lib/module/MockSseEngine.js +15 -17
  27. package/lib/module/NitroSseClient.js +70 -56
  28. package/lib/module/SseConsoleLogger.js +64 -0
  29. package/lib/module/SseEventParser.js +173 -115
  30. package/lib/module/createNitroSse.js +7 -14
  31. package/lib/module/createNitroSse.native.js +2 -1
  32. package/lib/module/useNitroSse.js +103 -90
  33. package/lib/typescript/src/FetchSseDriver.d.ts +11 -4
  34. package/lib/typescript/src/MockSseEngine.d.ts +2 -2
  35. package/lib/typescript/src/NitroSse.nitro.d.ts +9 -1
  36. package/lib/typescript/src/NitroSseClient.d.ts +4 -1
  37. package/lib/typescript/src/SseConsoleLogger.d.ts +28 -0
  38. package/lib/typescript/src/SseEventParser.d.ts +18 -2
  39. package/lib/typescript/src/SseInterface.d.ts +21 -4
  40. package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____.hpp +1 -1
  41. package/nitrogen/generated/android/c++/JFunc_void_std__vector_SseEvent_.hpp +1 -1
  42. package/nitrogen/generated/android/c++/JHttpMethod.hpp +1 -1
  43. package/nitrogen/generated/android/c++/JHybridNitroSseSpec.cpp +1 -1
  44. package/nitrogen/generated/android/c++/JHybridNitroSseSpec.hpp +1 -1
  45. package/nitrogen/generated/android/c++/JSseConfig.hpp +1 -1
  46. package/nitrogen/generated/android/c++/JSseDisconnectReason.hpp +1 -1
  47. package/nitrogen/generated/android/c++/JSseEvent.hpp +1 -1
  48. package/nitrogen/generated/android/c++/JSseEventType.hpp +1 -1
  49. package/nitrogen/generated/android/c++/JSseMockConfig.hpp +1 -1
  50. package/nitrogen/generated/android/c++/JSseMockEvent.hpp +1 -1
  51. package/nitrogen/generated/android/c++/JSseMockMode.hpp +1 -1
  52. package/nitrogen/generated/android/c++/JSseState.hpp +1 -1
  53. package/nitrogen/generated/android/c++/JSseStats.hpp +1 -1
  54. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____.kt +1 -1
  55. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/Func_void_std__vector_SseEvent_.kt +1 -1
  56. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/HttpMethod.kt +1 -1
  57. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/HybridNitroSseSpec.kt +1 -1
  58. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseConfig.kt +1 -1
  59. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseDisconnectReason.kt +1 -1
  60. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseEvent.kt +1 -1
  61. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseEventType.kt +1 -1
  62. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockConfig.kt +1 -1
  63. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockEvent.kt +1 -1
  64. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockMode.kt +1 -1
  65. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseState.kt +1 -1
  66. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseStats.kt +1 -1
  67. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/nitrosseOnLoad.kt +1 -1
  68. package/nitrogen/generated/android/nitrosse+autolinking.cmake +1 -1
  69. package/nitrogen/generated/android/nitrosse+autolinking.gradle +1 -1
  70. package/nitrogen/generated/android/nitrosseOnLoad.cpp +1 -1
  71. package/nitrogen/generated/android/nitrosseOnLoad.hpp +1 -1
  72. package/nitrogen/generated/ios/NitroSse+autolinking.rb +1 -1
  73. package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.cpp +1 -1
  74. package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +1 -1
  75. package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Umbrella.hpp +1 -1
  76. package/nitrogen/generated/ios/NitroSseAutolinking.mm +1 -1
  77. package/nitrogen/generated/ios/NitroSseAutolinking.swift +1 -5
  78. package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.cpp +1 -1
  79. package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +1 -1
  80. package/nitrogen/generated/ios/swift/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____.swift +1 -1
  81. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +1 -1
  82. package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_Promise_std__unordered_map_std__string__std__string___.swift +1 -1
  83. package/nitrogen/generated/ios/swift/Func_void_std__unordered_map_std__string__std__string_.swift +1 -1
  84. package/nitrogen/generated/ios/swift/Func_void_std__vector_SseEvent_.swift +1 -1
  85. package/nitrogen/generated/ios/swift/HttpMethod.swift +1 -1
  86. package/nitrogen/generated/ios/swift/HybridNitroSseSpec.swift +1 -1
  87. package/nitrogen/generated/ios/swift/HybridNitroSseSpec_cxx.swift +3 -3
  88. package/nitrogen/generated/ios/swift/SseConfig.swift +1 -1
  89. package/nitrogen/generated/ios/swift/SseDisconnectReason.swift +1 -1
  90. package/nitrogen/generated/ios/swift/SseEvent.swift +1 -1
  91. package/nitrogen/generated/ios/swift/SseEventType.swift +1 -1
  92. package/nitrogen/generated/ios/swift/SseMockConfig.swift +1 -1
  93. package/nitrogen/generated/ios/swift/SseMockEvent.swift +1 -1
  94. package/nitrogen/generated/ios/swift/SseMockMode.swift +1 -1
  95. package/nitrogen/generated/ios/swift/SseState.swift +1 -1
  96. package/nitrogen/generated/ios/swift/SseStats.swift +1 -1
  97. package/nitrogen/generated/shared/c++/HttpMethod.hpp +1 -1
  98. package/nitrogen/generated/shared/c++/HybridNitroSseSpec.cpp +1 -1
  99. package/nitrogen/generated/shared/c++/HybridNitroSseSpec.hpp +1 -1
  100. package/nitrogen/generated/shared/c++/SseConfig.hpp +1 -1
  101. package/nitrogen/generated/shared/c++/SseDisconnectReason.hpp +1 -1
  102. package/nitrogen/generated/shared/c++/SseEvent.hpp +1 -1
  103. package/nitrogen/generated/shared/c++/SseEventType.hpp +1 -1
  104. package/nitrogen/generated/shared/c++/SseMockConfig.hpp +1 -1
  105. package/nitrogen/generated/shared/c++/SseMockEvent.hpp +1 -1
  106. package/nitrogen/generated/shared/c++/SseMockMode.hpp +1 -1
  107. package/nitrogen/generated/shared/c++/SseState.hpp +1 -1
  108. package/nitrogen/generated/shared/c++/SseStats.hpp +1 -1
  109. package/package.json +18 -10
@@ -10,15 +10,25 @@
10
10
  export const MAX_SSE_LINE_LENGTH = 16 * 1024 * 1024; // 16 MB
11
11
  export const MAX_SSE_EVENT_DATA_SIZE = 16 * 1024 * 1024; // 16 MB
12
12
 
13
+ // ASCII char codes
14
+ const LF = 0x0a; // 10
15
+ const CR = 0x0d; // 13
16
+ const SPACE = 0x20; // 32
17
+ const COLON = 0x3a; // 58
18
+
13
19
  export class SseEventParser {
14
- _buffer = '';
20
+ _pendingFragments = [];
21
+ _pendingLength = 0;
15
22
  _dataBuffer = '';
16
23
  _hasData = false;
24
+ _idUpdatedInCurrentEvent = false;
17
25
  _isAtStreamStart = true;
18
26
  _skipLeadingLf = false;
27
+ _aborted = false;
19
28
  constructor(delegate, initialLastEventId) {
20
29
  this._delegate = delegate;
21
30
  this._lastEventId = initialLastEventId;
31
+ this._lastEventIdBuffer = initialLastEventId;
22
32
  }
23
33
  get lastEventId() {
24
34
  return this._lastEventId;
@@ -28,150 +38,193 @@ export class SseEventParser {
28
38
  * Resets parser state for reconnection.
29
39
  */
30
40
  reset(initialLastEventId) {
31
- this._buffer = '';
41
+ this._pendingFragments.length = 0;
42
+ this._pendingLength = 0;
32
43
  this._dataBuffer = '';
33
44
  this._hasData = false;
34
45
  this._eventType = undefined;
35
46
  this._lastEventId = initialLastEventId;
47
+ this._lastEventIdBuffer = initialLastEventId;
48
+ this._idUpdatedInCurrentEvent = false;
36
49
  this._isAtStreamStart = true;
37
50
  this._skipLeadingLf = false;
51
+ this._aborted = false;
38
52
  }
39
53
 
40
54
  /**
41
- * Signals end of stream (EOF) — flushes pending line and pending event if any.
55
+ * Signals end of stream (EOF).
56
+ * Per WHATWG SSE specification:
57
+ * "Once the end of the file is reached, any pending data must be discarded.
58
+ * (If the file ends in the middle of an event, before the final empty line,
59
+ * the incomplete event is not dispatched.)"
42
60
  */
43
61
  endOfStream() {
44
- if (this._buffer.length > 0) {
45
- const line = this._buffer;
46
- this._buffer = '';
47
- this._processLine(line);
48
- this._delegate.onLinesParsed(1);
49
- }
50
- if (this._hasData) {
51
- const data = this._dataBuffer;
52
- const id = this._lastEventId;
53
- const type = this._eventType;
54
- this._dataBuffer = '';
55
- this._hasData = false;
56
- this._eventType = undefined;
57
- this._delegate.onEvent(id, type, data);
58
- }
62
+ if (this._aborted) return;
63
+ this._pendingFragments.length = 0;
64
+ this._pendingLength = 0;
65
+ this._dataBuffer = '';
66
+ this._hasData = false;
67
+ this._eventType = undefined;
68
+ this._idUpdatedInCurrentEvent = false;
69
+ this._lastEventIdBuffer = this._lastEventId;
70
+ }
71
+
72
+ /**
73
+ * Aborts parsing immediately and discards pending fragments.
74
+ */
75
+ abort() {
76
+ this._aborted = true;
77
+ this._pendingFragments.length = 0;
78
+ this._pendingLength = 0;
79
+ }
80
+ _fail(message) {
81
+ this._aborted = true;
82
+ this._pendingFragments.length = 0;
83
+ this._pendingLength = 0;
84
+ this._delegate.onParseError(new Error(message));
59
85
  }
60
86
 
61
87
  /**
62
88
  * Feeds a decoded UTF-8 text chunk into the streaming parser.
63
89
  */
64
90
  feed(chunk) {
65
- if (!chunk) return;
91
+ if (!chunk || this._aborted) return;
66
92
  let text = chunk;
67
93
 
68
94
  // Handle CR at previous chunk boundary: if followed immediately by LF, consume LF
69
95
  if (this._skipLeadingLf) {
70
96
  this._skipLeadingLf = false;
71
- if (text.charCodeAt(0) === 0x0a) {
97
+ if (text.charCodeAt(0) === LF) {
72
98
  text = text.slice(1);
73
- if (!text && !this._buffer) return;
99
+ if (!text) return;
74
100
  }
75
- }
76
- if (this._isAtStreamStart) {
101
+ } else if (this._isAtStreamStart) {
77
102
  if (text.charCodeAt(0) === 0xfeff) {
78
103
  text = text.slice(1);
79
104
  }
80
105
  this._isAtStreamStart = false;
81
- if (!text && !this._buffer) return;
106
+ if (!text) return;
107
+ }
108
+ const hasLf = text.indexOf('\n') !== -1;
109
+ const hasCr = text.indexOf('\r') !== -1;
110
+
111
+ // Optimization: When the incoming chunk has no line terminator, buffer it as an array fragment
112
+ // without concatenating strings on every feed. This eliminates O(N^2) memory reallocation.
113
+ if (!hasLf && !hasCr) {
114
+ if (this._pendingLength + text.length > MAX_SSE_LINE_LENGTH) {
115
+ this._fail(`SSE line exceeded maximum limit of ${MAX_SSE_LINE_LENGTH} characters`);
116
+ return;
117
+ }
118
+ this._pendingFragments.push(text);
119
+ this._pendingLength += text.length;
120
+ return;
121
+ }
122
+
123
+ // A terminator arrived: join any accumulated fragments + current chunk once.
124
+ // Accumulated fragments are guaranteed not to contain CR or LF, so scanning resumes at `carried`.
125
+ const carried = this._pendingLength;
126
+ let fullText;
127
+ if (this._pendingFragments.length > 0) {
128
+ this._pendingFragments.push(text);
129
+ fullText = this._pendingFragments.join('');
130
+ this._pendingFragments.length = 0;
131
+ this._pendingLength = 0;
132
+ } else {
133
+ fullText = text;
82
134
  }
83
- const fullText = this._buffer ? this._buffer + text : text;
84
- this._buffer = '';
85
135
  const len = fullText.length;
86
- let start = 0;
87
- let i = 0;
88
136
  let parsedLinesInChunk = 0;
89
- while (i < len) {
90
- // Check maximum line length constraint before scanning further
91
- if (i - start > MAX_SSE_LINE_LENGTH) {
92
- this._delegate.onParseError(new Error(`SSE line exceeded maximum limit of ${MAX_SSE_LINE_LENGTH} characters`));
93
- // Discard current overflowing line and advance
94
- while (i < len && fullText.charCodeAt(i) !== 0x0a && fullText.charCodeAt(i) !== 0x0d) {
95
- i++;
96
- }
97
- if (i < len && fullText.charCodeAt(i) === 0x0d && i + 1 < len && fullText.charCodeAt(i + 1) === 0x0a) {
98
- i += 2;
99
- } else if (i < len) {
100
- i += 1;
137
+ let start = 0;
138
+
139
+ // Fast-path: When there are no carriage returns in fullText (the vast majority of SSE streams),
140
+ // scan using native SIMD-accelerated indexOf('\n') starting at `carried` instead of 0.
141
+ if (!hasCr) {
142
+ let nextLf = fullText.indexOf('\n', carried);
143
+ while (nextLf !== -1 && !this._aborted) {
144
+ if (nextLf - start > MAX_SSE_LINE_LENGTH) {
145
+ this._fail(`SSE line exceeded maximum limit of ${MAX_SSE_LINE_LENGTH} characters`);
146
+ return;
101
147
  }
102
- start = i;
103
- continue;
104
- }
105
- const code = fullText.charCodeAt(i);
106
- if (code === 0x0a) {
107
- // \n (LF)
108
- const line = fullText.slice(start, i);
109
- start = i + 1;
110
- i++;
148
+ const line = fullText.slice(start, nextLf);
149
+ start = nextLf + 1;
111
150
  this._processLine(line);
112
151
  parsedLinesInChunk++;
113
- } else if (code === 0x0d) {
114
- // \r (CR or CRLF)
115
- if (i + 1 < len) {
116
- if (fullText.charCodeAt(i + 1) === 0x0a) {
117
- // CRLF in current buffer
118
- const line = fullText.slice(start, i);
119
- start = i + 2;
120
- i += 2;
121
- this._processLine(line);
122
- parsedLinesInChunk++;
123
- } else {
124
- // Bare CR in current buffer
125
- const line = fullText.slice(start, i);
126
- start = i + 1;
127
- i++;
128
- this._processLine(line);
129
- parsedLinesInChunk++;
130
- }
131
- } else {
132
- // \r at the very end of current buffer
152
+ nextLf = fullText.indexOf('\n', start);
153
+ }
154
+ } else {
155
+ // Slow-path: Handles streams with CR, CRLF, and bare CR line endings
156
+ let i = carried;
157
+ while (i < len && !this._aborted) {
158
+ if (i - start > MAX_SSE_LINE_LENGTH) {
159
+ this._fail(`SSE line exceeded maximum limit of ${MAX_SSE_LINE_LENGTH} characters`);
160
+ return;
161
+ }
162
+ const code = fullText.charCodeAt(i);
163
+ if (code === LF) {
133
164
  const line = fullText.slice(start, i);
134
165
  start = i + 1;
135
166
  i++;
136
- this._skipLeadingLf = true;
137
167
  this._processLine(line);
138
168
  parsedLinesInChunk++;
169
+ } else if (code === CR) {
170
+ const isCrlf = i + 1 < len && fullText.charCodeAt(i + 1) === LF;
171
+ const step = isCrlf ? 2 : 1;
172
+ if (i + 1 === len) {
173
+ this._skipLeadingLf = true;
174
+ }
175
+ const line = fullText.slice(start, i);
176
+ start = i + step;
177
+ i += step;
178
+ this._processLine(line);
179
+ parsedLinesInChunk++;
180
+ } else {
181
+ i++;
139
182
  }
140
- } else {
141
- i++;
142
183
  }
143
184
  }
185
+ if (this._aborted) return;
144
186
  if (start < len) {
145
187
  const remaining = fullText.slice(start);
146
188
  if (remaining.length > MAX_SSE_LINE_LENGTH) {
147
- this._delegate.onParseError(new Error(`SSE line exceeded maximum limit of ${MAX_SSE_LINE_LENGTH} characters`));
148
- this._buffer = '';
149
- } else {
150
- this._buffer = remaining;
189
+ this._fail(`SSE line exceeded maximum limit of ${MAX_SSE_LINE_LENGTH} characters`);
190
+ return;
151
191
  }
192
+ this._pendingFragments.push(remaining);
193
+ this._pendingLength = remaining.length;
152
194
  }
153
- if (parsedLinesInChunk > 0) {
154
- this._delegate.onLinesParsed(parsedLinesInChunk);
155
- }
195
+ this._delegate.onLinesParsed(parsedLinesInChunk);
156
196
  }
157
197
  _processLine(line) {
158
198
  if (line === '') {
159
- // Empty line dispatches the current event
160
- if (this._hasData) {
161
- const data = this._dataBuffer;
162
- const id = this._lastEventId;
163
- const type = this._eventType;
164
- this._dataBuffer = '';
165
- this._hasData = false;
199
+ // Empty line dispatches the current event per WHATWG specification
200
+ // Step 1: Set last event ID string of event source to value of last event ID buffer
201
+ if (this._idUpdatedInCurrentEvent) {
202
+ this._idUpdatedInCurrentEvent = false;
203
+ this._lastEventId = this._lastEventIdBuffer;
204
+ this._delegate.onIdUpdate(this._lastEventId);
205
+ }
206
+
207
+ // Step 2: If data buffer is empty, reset data buffer and event type buffer to empty string and return
208
+ if (!this._hasData) {
166
209
  this._eventType = undefined;
167
- this._delegate.onEvent(id, type, data);
210
+ return;
168
211
  }
212
+
213
+ // Steps 3-6: Dispatch event
214
+ const data = this._dataBuffer;
215
+ const id = this._lastEventId;
216
+ const type = this._eventType;
217
+ this._dataBuffer = '';
218
+ this._hasData = false;
219
+ this._eventType = undefined;
220
+ this._delegate.onEvent(id, type, data);
169
221
  return;
170
222
  }
171
- if (line.startsWith(':')) {
172
- // Lines starting with ':' are comments / heartbeats
173
- const rawComment = line.slice(1);
174
- const comment = rawComment.startsWith(' ') ? rawComment.slice(1) : rawComment;
223
+ const firstCharCode = line.charCodeAt(0);
224
+
225
+ // Comment line: starts with ':'
226
+ if (firstCharCode === COLON) {
227
+ const comment = line.charCodeAt(1) === SPACE ? line.slice(2) : line.slice(1);
175
228
  this._delegate.onComment(comment);
176
229
  return;
177
230
  }
@@ -181,7 +234,7 @@ export class SseEventParser {
181
234
  if (colonIndex !== -1) {
182
235
  field = line.slice(0, colonIndex);
183
236
  const rawValue = line.slice(colonIndex + 1);
184
- value = rawValue.startsWith(' ') ? rawValue.slice(1) : rawValue;
237
+ value = rawValue.charCodeAt(0) === SPACE ? rawValue.slice(1) : rawValue;
185
238
  } else {
186
239
  field = line;
187
240
  value = '';
@@ -189,43 +242,24 @@ export class SseEventParser {
189
242
  switch (field) {
190
243
  case 'data':
191
244
  {
192
- const separatorSize = this._hasData ? 1 : 0;
193
- if (this._dataBuffer.length + separatorSize + value.length > MAX_SSE_EVENT_DATA_SIZE) {
194
- this._delegate.onParseError(new Error(`SSE event data exceeded maximum limit of ${MAX_SSE_EVENT_DATA_SIZE} characters`));
195
- this._dataBuffer = '';
196
- this._hasData = false;
197
- return;
198
- }
199
- if (this._hasData) {
200
- this._dataBuffer += '\n' + value;
201
- } else {
202
- this._dataBuffer = value;
203
- this._hasData = true;
204
- }
245
+ this._appendData(value);
205
246
  break;
206
247
  }
207
248
  case 'id':
208
249
  {
209
- // WHATWG SSE: If field value contains a U+0000 NULL character, ignore the field
210
- if (!value.includes('\0')) {
211
- this._lastEventId = value === '' ? undefined : value;
212
- this._delegate.onIdUpdate(this._lastEventId);
213
- }
250
+ this._handleId(value);
214
251
  break;
215
252
  }
216
253
  case 'event':
217
254
  {
218
- this._eventType = value === '' ? undefined : value;
255
+ this._handleEvent(value);
219
256
  break;
220
257
  }
221
258
  case 'retry':
222
259
  {
223
260
  // WHATWG SSE: If field value consists solely of ASCII digits, set reconnection time
224
261
  if (value.length > 0 && /^[0-9]+$/.test(value)) {
225
- const retryVal = parseInt(value, 10);
226
- if (!Number.isNaN(retryVal) && retryVal >= 0) {
227
- this._delegate.onRetry(retryVal);
228
- }
262
+ this._delegate.onRetry(parseInt(value, 10));
229
263
  }
230
264
  break;
231
265
  }
@@ -234,5 +268,29 @@ export class SseEventParser {
234
268
  break;
235
269
  }
236
270
  }
271
+ _handleEvent(value) {
272
+ this._eventType = value === '' ? undefined : value;
273
+ }
274
+ _handleId(value) {
275
+ if (!value.includes('\0')) {
276
+ this._lastEventIdBuffer = value === '' ? undefined : value;
277
+ this._idUpdatedInCurrentEvent = true;
278
+ }
279
+ }
280
+ _appendData(value) {
281
+ const separatorSize = this._hasData ? 1 : 0;
282
+ if (this._dataBuffer.length + separatorSize + value.length > MAX_SSE_EVENT_DATA_SIZE) {
283
+ this._dataBuffer = '';
284
+ this._hasData = false;
285
+ this._fail(`SSE event data exceeded maximum limit of ${MAX_SSE_EVENT_DATA_SIZE} characters`);
286
+ return;
287
+ }
288
+ if (this._hasData) {
289
+ this._dataBuffer += '\n' + value;
290
+ } else {
291
+ this._dataBuffer = value;
292
+ this._hasData = true;
293
+ }
294
+ }
237
295
  }
238
296
  //# sourceMappingURL=SseEventParser.js.map
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
 
3
3
  import { Platform } from 'react-native';
4
- import { NitroModules } from 'react-native-nitro-modules';
5
4
  import { NitroSseClient } from "./NitroSseClient.js";
6
5
  import { FetchSseDriver } from "./FetchSseDriver.js";
7
6
  import { NitroSseModuleNotFoundError } from "./NitroSseError.js";
7
+ import { createNitroSse as createNativeNitroSse } from "./createNitroSse.native.js";
8
8
  /**
9
9
  * Creates a high-performance SSE client that supports typed event listeners (`addEventListener`) and legacy batching.
10
10
  *
@@ -12,20 +12,13 @@ import { NitroSseModuleNotFoundError } from "./NitroSseError.js";
12
12
  * @throws {NitroSseModuleNotFoundError} If the native NitroSse module cannot be found on iOS/Android.
13
13
  */
14
14
  export function createNitroSse() {
15
- let nativeSse;
16
15
  try {
17
- nativeSse = NitroModules.createHybridObject('NitroSse');
18
- } catch {
19
- console.debug('Native NitroSse not found. This might be a test environment or web.');
16
+ return createNativeNitroSse();
17
+ } catch (error) {
18
+ if (Platform.OS === 'web' && error instanceof NitroSseModuleNotFoundError) {
19
+ return new NitroSseClient(new FetchSseDriver());
20
+ }
21
+ throw error;
20
22
  }
21
- if (nativeSse) {
22
- return new NitroSseClient(nativeSse);
23
- }
24
-
25
- // Graceful fallback for Web
26
- if (Platform.OS === 'web') {
27
- return new NitroSseClient(new FetchSseDriver());
28
- }
29
- throw new NitroSseModuleNotFoundError('NitroSse: Native module not found. Ensure you have linked the library and built the app for iOS/Android.');
30
23
  }
31
24
  //# sourceMappingURL=createNitroSse.js.map
@@ -3,6 +3,7 @@
3
3
  import { NitroModules } from 'react-native-nitro-modules';
4
4
  import { NitroSseClient } from "./NitroSseClient.js";
5
5
  import { NitroSseModuleNotFoundError } from "./NitroSseError.js";
6
+ import { devDebug } from "./SseConsoleLogger.js";
6
7
  /**
7
8
  * Creates an SSE client for iOS and Android using Nitro Modules (JSI).
8
9
  *
@@ -14,7 +15,7 @@ export function createNitroSse() {
14
15
  try {
15
16
  nativeSse = NitroModules.createHybridObject('NitroSse');
16
17
  } catch {
17
- console.debug('Native NitroSse not found. This might be a test environment or web.');
18
+ devDebug('Native NitroSse not found. This might be a test environment or web.');
18
19
  }
19
20
  if (nativeSse) {
20
21
  return new NitroSseClient(nativeSse);