react-native-nitro-sse 3.0.0 → 3.2.0-beta.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/NitroSse.podspec +0 -1
- package/README.md +16 -5
- package/android/build.gradle +0 -1
- package/android/src/main/java/com/margelo/nitro/nitrosse/JsonUtils.kt +33 -10
- package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +366 -78
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseConnectionHandler.kt +417 -94
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventBuffer.kt +20 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseReconnectStrategy.kt +9 -0
- package/ios/NitroSse.swift +302 -49
- package/ios/SseConnectionHandler.swift +647 -97
- package/ios/SseEventBuffer.swift +19 -1
- package/ios/SseReconnectStrategy.swift +26 -7
- package/lib/module/FetchSseDriver.js +845 -0
- package/lib/module/MockSseEngine.js +72 -3
- package/lib/module/NitroSseClient.js +43 -18
- package/lib/module/SseDriver.js +31 -15
- package/lib/module/SseEventParser.js +238 -0
- package/lib/module/createNitroSse.js +12 -5
- package/lib/module/createNitroSse.native.js +24 -0
- package/lib/module/createNitroSse.web.js +13 -0
- package/lib/module/index.js +2 -1
- package/lib/module/index.native.js +8 -0
- package/lib/module/index.web.js +9 -0
- package/lib/module/useNitroSse.js +1 -1
- package/lib/typescript/src/FetchSseDriver.d.ts +92 -0
- package/lib/typescript/src/MockSseEngine.d.ts +16 -0
- package/lib/typescript/src/NitroSseClient.d.ts +4 -1
- package/lib/typescript/src/SseDriver.d.ts +8 -4
- package/lib/typescript/src/SseEventParser.d.ts +42 -0
- package/lib/typescript/src/SseInterface.d.ts +46 -3
- package/lib/typescript/src/createNitroSse.d.ts +2 -2
- package/lib/typescript/src/createNitroSse.native.d.ts +9 -0
- package/lib/typescript/src/createNitroSse.web.d.ts +8 -0
- package/lib/typescript/src/index.d.ts +1 -0
- package/lib/typescript/src/index.native.d.ts +6 -0
- package/lib/typescript/src/index.web.d.ts +7 -0
- package/nitrogen/generated/android/c++/JHybridNitroSseSpec.cpp +4 -0
- package/nitrogen/generated/android/c++/JSseDisconnectReason.hpp +67 -0
- package/nitrogen/generated/android/c++/JSseStats.hpp +87 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseDisconnectReason.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseStats.kt +108 -3
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +18 -0
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Umbrella.hpp +3 -0
- package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +3 -0
- package/nitrogen/generated/ios/swift/SseDisconnectReason.swift +52 -0
- package/nitrogen/generated/ios/swift/SseStats.swift +217 -2
- package/nitrogen/generated/shared/c++/SseDisconnectReason.hpp +88 -0
- package/nitrogen/generated/shared/c++/SseStats.hpp +88 -2
- package/package.json +13 -2
|
@@ -7,6 +7,17 @@
|
|
|
7
7
|
export class MockSseEngine {
|
|
8
8
|
_mockIndex = 0;
|
|
9
9
|
_totalBytesReceived = 0;
|
|
10
|
+
_chunksReceived = 0;
|
|
11
|
+
_totalEventsReceived = 0;
|
|
12
|
+
_commentsReceived = 0;
|
|
13
|
+
_linesParsed = 0;
|
|
14
|
+
_parseErrors = 0;
|
|
15
|
+
_lastActivityTime = 0;
|
|
16
|
+
_maxEventGapMs = 0;
|
|
17
|
+
_bufferFlushCount = 0;
|
|
18
|
+
_connectionAttempts = 0;
|
|
19
|
+
_reconnectCount = 0;
|
|
20
|
+
_lastProcessedId = null;
|
|
10
21
|
constructor(config, emitEvents) {
|
|
11
22
|
this._config = config;
|
|
12
23
|
this._emitEvents = emitEvents;
|
|
@@ -58,6 +69,16 @@ export class MockSseEngine {
|
|
|
58
69
|
} = this._config;
|
|
59
70
|
this._mockIndex = 0;
|
|
60
71
|
this._totalBytesReceived = 0;
|
|
72
|
+
this._chunksReceived = 0;
|
|
73
|
+
this._totalEventsReceived = 0;
|
|
74
|
+
this._commentsReceived = 0;
|
|
75
|
+
this._linesParsed = 0;
|
|
76
|
+
this._parseErrors = 0;
|
|
77
|
+
this._maxEventGapMs = 0;
|
|
78
|
+
this._lastActivityTime = 0;
|
|
79
|
+
this._connectionAttempts++;
|
|
80
|
+
this._connectedAt = Date.now();
|
|
81
|
+
this._lastProcessedId = null;
|
|
61
82
|
|
|
62
83
|
// Validate and normalize eventsPerSecond (must be a finite positive number, default to 1)
|
|
63
84
|
let validatedEventsPerSecond = Number(eventsPerSecond);
|
|
@@ -103,6 +124,9 @@ export class MockSseEngine {
|
|
|
103
124
|
message: 'Mock Connection Drop (Simulated Error)',
|
|
104
125
|
statusCode: 500
|
|
105
126
|
};
|
|
127
|
+
this._lastErrorTime = Date.now();
|
|
128
|
+
this._lastErrorCode = 'SIMULATED_MOCK_FAILURE';
|
|
129
|
+
this._reconnectCount++;
|
|
106
130
|
this._emitEvents([errorEvent]);
|
|
107
131
|
|
|
108
132
|
// Retry / reconnect delay simulator (e.g. 2000ms)
|
|
@@ -130,11 +154,32 @@ export class MockSseEngine {
|
|
|
130
154
|
}
|
|
131
155
|
}
|
|
132
156
|
if (batch.length > 0) {
|
|
157
|
+
this._chunksReceived++;
|
|
158
|
+
this._bufferFlushCount++;
|
|
159
|
+
const now = Date.now();
|
|
133
160
|
for (const ev of batch) {
|
|
134
161
|
const dataLen = ev.data ? ev.data.length : 0;
|
|
135
|
-
const evLen = ev.event ? ev.event.length : 0;
|
|
136
|
-
const idLen = ev.id ? ev.id.length : 0;
|
|
162
|
+
const evLen = ev.event && ev.event !== 'message' ? ev.event.length : 0;
|
|
163
|
+
const idLen = ev.id && ev.id !== this._lastProcessedId ? ev.id.length : 0;
|
|
164
|
+
if (ev.id !== undefined) {
|
|
165
|
+
this._lastProcessedId = ev.id === '' ? null : ev.id;
|
|
166
|
+
}
|
|
137
167
|
this._totalBytesReceived += Math.max(1, dataLen + evLen + idLen);
|
|
168
|
+
this._linesParsed += 2;
|
|
169
|
+
if (ev.type === 'message' || ev.event === 'message') {
|
|
170
|
+
this._totalEventsReceived++;
|
|
171
|
+
this._lastEventTime = now;
|
|
172
|
+
} else if (ev.type === 'heartbeat') {
|
|
173
|
+
this._commentsReceived++;
|
|
174
|
+
this._lastHeartbeatTime = now;
|
|
175
|
+
}
|
|
176
|
+
if (this._lastActivityTime > 0) {
|
|
177
|
+
const gap = now - this._lastActivityTime;
|
|
178
|
+
if (gap > this._maxEventGapMs) {
|
|
179
|
+
this._maxEventGapMs = gap;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
this._lastActivityTime = now;
|
|
138
183
|
}
|
|
139
184
|
if (mode === 'replace') {
|
|
140
185
|
this._setMockState('open');
|
|
@@ -170,6 +215,7 @@ export class MockSseEngine {
|
|
|
170
215
|
clearTimeout(this._mockIntervalId);
|
|
171
216
|
this._mockIntervalId = undefined;
|
|
172
217
|
}
|
|
218
|
+
this._connectedAt = undefined;
|
|
173
219
|
if (this._config.mode === 'replace') {
|
|
174
220
|
this._setMockState('closed');
|
|
175
221
|
}
|
|
@@ -183,8 +229,31 @@ export class MockSseEngine {
|
|
|
183
229
|
}
|
|
184
230
|
getStats() {
|
|
185
231
|
return {
|
|
232
|
+
rawBytesReceived: this._totalBytesReceived,
|
|
233
|
+
decompressedBytesReceived: undefined,
|
|
186
234
|
totalBytesReceived: this._totalBytesReceived,
|
|
187
|
-
|
|
235
|
+
chunksReceived: this._chunksReceived,
|
|
236
|
+
lastStatusCode: this._mockState === 'open' ? 200 : undefined,
|
|
237
|
+
totalEventsReceived: this._totalEventsReceived,
|
|
238
|
+
commentsReceived: this._commentsReceived,
|
|
239
|
+
linesParsed: this._linesParsed,
|
|
240
|
+
parseErrors: this._parseErrors,
|
|
241
|
+
serverRetryDelayMs: undefined,
|
|
242
|
+
connectedAt: this._connectedAt,
|
|
243
|
+
timeToFirstByteMs: undefined,
|
|
244
|
+
lastEventTime: this._lastEventTime,
|
|
245
|
+
lastHeartbeatTime: this._lastHeartbeatTime,
|
|
246
|
+
maxEventGapMs: this._maxEventGapMs,
|
|
247
|
+
eventsBuffered: 0,
|
|
248
|
+
peakBufferedEvents: 0,
|
|
249
|
+
bufferFlushCount: this._bufferFlushCount,
|
|
250
|
+
bufferOverflowCount: 0,
|
|
251
|
+
connectionAttempts: this._connectionAttempts,
|
|
252
|
+
reconnectCount: this._reconnectCount,
|
|
253
|
+
lastReconnectDelayMs: undefined,
|
|
254
|
+
disconnectReason: this._mockState === 'closed' ? 'user_stop' : undefined,
|
|
255
|
+
lastErrorTime: this._lastErrorTime,
|
|
256
|
+
lastErrorCode: this._lastErrorCode
|
|
188
257
|
};
|
|
189
258
|
}
|
|
190
259
|
getState() {
|
|
@@ -257,9 +257,21 @@ export class NitroSseClient {
|
|
|
257
257
|
_listeners = new Map();
|
|
258
258
|
_isDisposed = false;
|
|
259
259
|
_pendingHeaders = {};
|
|
260
|
-
constructor(
|
|
261
|
-
|
|
262
|
-
|
|
260
|
+
constructor(nativeOrDriver) {
|
|
261
|
+
if (typeof nativeOrDriver === 'object' && nativeOrDriver !== null && 'injectMockEvent' in nativeOrDriver) {
|
|
262
|
+
this._native = null;
|
|
263
|
+
this._baseDriver = nativeOrDriver;
|
|
264
|
+
if ('setDispatchEvents' in this._baseDriver && typeof this._baseDriver.setDispatchEvents === 'function') {
|
|
265
|
+
this._baseDriver.setDispatchEvents(events => this._dispatchEvents(events));
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
this._native = nativeOrDriver;
|
|
269
|
+
this._baseDriver = new NativeDriver(this._native, events => this._dispatchEvents(events));
|
|
270
|
+
}
|
|
271
|
+
this._driver = this._baseDriver;
|
|
272
|
+
}
|
|
273
|
+
static fromDriver(driver) {
|
|
274
|
+
return new NitroSseClient(driver);
|
|
263
275
|
}
|
|
264
276
|
get isDisposed() {
|
|
265
277
|
return this._isDisposed;
|
|
@@ -327,23 +339,13 @@ export class NitroSseClient {
|
|
|
327
339
|
const mockEngine = new MockSseEngine(mockConfig, events => {
|
|
328
340
|
this._dispatchEvents(events);
|
|
329
341
|
});
|
|
330
|
-
this._driver = mockConfig.mode === 'replace' ? new MockReplaceDriver(mockEngine) : new MockInjectDriver(this.
|
|
342
|
+
this._driver = mockConfig.mode === 'replace' ? new MockReplaceDriver(mockEngine) : new MockInjectDriver(this._baseDriver, mockEngine);
|
|
331
343
|
} else {
|
|
332
|
-
this._driver =
|
|
344
|
+
this._driver = this._baseDriver;
|
|
333
345
|
}
|
|
334
346
|
|
|
335
|
-
//
|
|
336
|
-
|
|
337
|
-
if (safeOnBeforeRequest !== undefined) {
|
|
338
|
-
this._native.setup(this._config, events => {
|
|
339
|
-
this._dispatchEvents(events);
|
|
340
|
-
}, safeOnBeforeRequest);
|
|
341
|
-
} else {
|
|
342
|
-
this._native.setup(this._config, events => {
|
|
343
|
-
this._dispatchEvents(events);
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
+
// Delegate setup to the driver
|
|
348
|
+
this._driver.setup(this._config, safeOnBeforeRequest);
|
|
347
349
|
}
|
|
348
350
|
addEventListener(type, listener) {
|
|
349
351
|
this._checkDisposed();
|
|
@@ -461,8 +463,31 @@ export class NitroSseClient {
|
|
|
461
463
|
getStats() {
|
|
462
464
|
if (this._isDisposed) {
|
|
463
465
|
return {
|
|
466
|
+
rawBytesReceived: 0,
|
|
467
|
+
decompressedBytesReceived: undefined,
|
|
464
468
|
totalBytesReceived: 0,
|
|
465
|
-
|
|
469
|
+
chunksReceived: 0,
|
|
470
|
+
lastStatusCode: undefined,
|
|
471
|
+
totalEventsReceived: 0,
|
|
472
|
+
commentsReceived: 0,
|
|
473
|
+
linesParsed: 0,
|
|
474
|
+
parseErrors: 0,
|
|
475
|
+
serverRetryDelayMs: undefined,
|
|
476
|
+
connectedAt: undefined,
|
|
477
|
+
timeToFirstByteMs: undefined,
|
|
478
|
+
lastEventTime: undefined,
|
|
479
|
+
lastHeartbeatTime: undefined,
|
|
480
|
+
maxEventGapMs: 0,
|
|
481
|
+
eventsBuffered: 0,
|
|
482
|
+
peakBufferedEvents: 0,
|
|
483
|
+
bufferFlushCount: 0,
|
|
484
|
+
bufferOverflowCount: 0,
|
|
485
|
+
connectionAttempts: 0,
|
|
486
|
+
reconnectCount: 0,
|
|
487
|
+
lastReconnectDelayMs: undefined,
|
|
488
|
+
disconnectReason: 'user_stop',
|
|
489
|
+
lastErrorTime: undefined,
|
|
490
|
+
lastErrorCode: undefined
|
|
466
491
|
};
|
|
467
492
|
}
|
|
468
493
|
return this._driver.getStats();
|
package/lib/module/SseDriver.js
CHANGED
|
@@ -14,6 +14,13 @@ export class NativeDriver {
|
|
|
14
14
|
this._native = _native;
|
|
15
15
|
this._dispatchEvents = _dispatchEvents;
|
|
16
16
|
}
|
|
17
|
+
setup(config, onBeforeRequest) {
|
|
18
|
+
if (onBeforeRequest !== undefined) {
|
|
19
|
+
this._native.setup(config, this._dispatchEvents, onBeforeRequest);
|
|
20
|
+
} else {
|
|
21
|
+
this._native.setup(config, this._dispatchEvents);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
17
24
|
start() {
|
|
18
25
|
this._native.start();
|
|
19
26
|
}
|
|
@@ -69,6 +76,14 @@ export class MockReplaceDriver {
|
|
|
69
76
|
get lastProcessedId() {
|
|
70
77
|
return this._lastProcessedId;
|
|
71
78
|
}
|
|
79
|
+
setup(config, _onBeforeRequest) {
|
|
80
|
+
if (config.headers) {
|
|
81
|
+
this._headers = {
|
|
82
|
+
...this._headers,
|
|
83
|
+
...config.headers
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
72
87
|
start() {
|
|
73
88
|
this._mockEngine.start();
|
|
74
89
|
}
|
|
@@ -106,51 +121,52 @@ export class MockReplaceDriver {
|
|
|
106
121
|
}
|
|
107
122
|
|
|
108
123
|
/**
|
|
109
|
-
* Driver running
|
|
124
|
+
* Driver running underlying network streaming in parallel with mock event injection.
|
|
110
125
|
*/
|
|
111
126
|
export class MockInjectDriver {
|
|
112
|
-
constructor(
|
|
113
|
-
this.
|
|
127
|
+
constructor(_realDriver, _mockEngine) {
|
|
128
|
+
this._realDriver = _realDriver;
|
|
114
129
|
this._mockEngine = _mockEngine;
|
|
115
130
|
}
|
|
131
|
+
setup(config, onBeforeRequest) {
|
|
132
|
+
this._realDriver.setup(config, onBeforeRequest);
|
|
133
|
+
}
|
|
116
134
|
start() {
|
|
117
135
|
this._mockEngine.start();
|
|
118
|
-
this.
|
|
136
|
+
this._realDriver.start();
|
|
119
137
|
}
|
|
120
138
|
stop() {
|
|
121
139
|
this._mockEngine.stop();
|
|
122
|
-
this.
|
|
140
|
+
this._realDriver.stop();
|
|
123
141
|
}
|
|
124
142
|
restart() {
|
|
125
143
|
this._mockEngine.restart();
|
|
126
|
-
this.
|
|
144
|
+
this._realDriver.restart();
|
|
127
145
|
}
|
|
128
146
|
flush() {
|
|
129
|
-
this.
|
|
147
|
+
this._realDriver.flush();
|
|
130
148
|
}
|
|
131
149
|
isConnected() {
|
|
132
|
-
return this.
|
|
150
|
+
return this._realDriver.isConnected();
|
|
133
151
|
}
|
|
134
152
|
getStats() {
|
|
135
|
-
return this.
|
|
153
|
+
return this._realDriver.getStats();
|
|
136
154
|
}
|
|
137
155
|
getState() {
|
|
138
|
-
return this.
|
|
156
|
+
return this._realDriver.getState();
|
|
139
157
|
}
|
|
140
158
|
updateHeaders(headers) {
|
|
141
|
-
this.
|
|
159
|
+
this._realDriver.updateHeaders(headers);
|
|
142
160
|
}
|
|
143
161
|
setLastProcessedId(id) {
|
|
144
|
-
this.
|
|
162
|
+
this._realDriver.setLastProcessedId(id);
|
|
145
163
|
}
|
|
146
164
|
injectMockEvent(event) {
|
|
147
165
|
this._mockEngine.injectEvent(event);
|
|
148
166
|
}
|
|
149
167
|
dispose() {
|
|
150
168
|
this._mockEngine.stop();
|
|
151
|
-
|
|
152
|
-
this._native.dispose();
|
|
153
|
-
}
|
|
169
|
+
this._realDriver.dispose();
|
|
154
170
|
}
|
|
155
171
|
}
|
|
156
172
|
//# sourceMappingURL=SseDriver.js.map
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* WHATWG Server-Sent Events (SSE) streaming parser.
|
|
5
|
+
*
|
|
6
|
+
* Implements the W3C/WHATWG SSE parsing algorithm with cross-platform parity
|
|
7
|
+
* matching iOS SseEventParser (Swift) and Android SseEventReader (Kotlin).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const MAX_SSE_LINE_LENGTH = 16 * 1024 * 1024; // 16 MB
|
|
11
|
+
export const MAX_SSE_EVENT_DATA_SIZE = 16 * 1024 * 1024; // 16 MB
|
|
12
|
+
|
|
13
|
+
export class SseEventParser {
|
|
14
|
+
_buffer = '';
|
|
15
|
+
_dataBuffer = '';
|
|
16
|
+
_hasData = false;
|
|
17
|
+
_isAtStreamStart = true;
|
|
18
|
+
_skipLeadingLf = false;
|
|
19
|
+
constructor(delegate, initialLastEventId) {
|
|
20
|
+
this._delegate = delegate;
|
|
21
|
+
this._lastEventId = initialLastEventId;
|
|
22
|
+
}
|
|
23
|
+
get lastEventId() {
|
|
24
|
+
return this._lastEventId;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Resets parser state for reconnection.
|
|
29
|
+
*/
|
|
30
|
+
reset(initialLastEventId) {
|
|
31
|
+
this._buffer = '';
|
|
32
|
+
this._dataBuffer = '';
|
|
33
|
+
this._hasData = false;
|
|
34
|
+
this._eventType = undefined;
|
|
35
|
+
this._lastEventId = initialLastEventId;
|
|
36
|
+
this._isAtStreamStart = true;
|
|
37
|
+
this._skipLeadingLf = false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Signals end of stream (EOF) — flushes pending line and pending event if any.
|
|
42
|
+
*/
|
|
43
|
+
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
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Feeds a decoded UTF-8 text chunk into the streaming parser.
|
|
63
|
+
*/
|
|
64
|
+
feed(chunk) {
|
|
65
|
+
if (!chunk) return;
|
|
66
|
+
let text = chunk;
|
|
67
|
+
|
|
68
|
+
// Handle CR at previous chunk boundary: if followed immediately by LF, consume LF
|
|
69
|
+
if (this._skipLeadingLf) {
|
|
70
|
+
this._skipLeadingLf = false;
|
|
71
|
+
if (text.charCodeAt(0) === 0x0a) {
|
|
72
|
+
text = text.slice(1);
|
|
73
|
+
if (!text && !this._buffer) return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (this._isAtStreamStart) {
|
|
77
|
+
if (text.charCodeAt(0) === 0xfeff) {
|
|
78
|
+
text = text.slice(1);
|
|
79
|
+
}
|
|
80
|
+
this._isAtStreamStart = false;
|
|
81
|
+
if (!text && !this._buffer) return;
|
|
82
|
+
}
|
|
83
|
+
const fullText = this._buffer ? this._buffer + text : text;
|
|
84
|
+
this._buffer = '';
|
|
85
|
+
const len = fullText.length;
|
|
86
|
+
let start = 0;
|
|
87
|
+
let i = 0;
|
|
88
|
+
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;
|
|
101
|
+
}
|
|
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++;
|
|
111
|
+
this._processLine(line);
|
|
112
|
+
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
|
|
133
|
+
const line = fullText.slice(start, i);
|
|
134
|
+
start = i + 1;
|
|
135
|
+
i++;
|
|
136
|
+
this._skipLeadingLf = true;
|
|
137
|
+
this._processLine(line);
|
|
138
|
+
parsedLinesInChunk++;
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
i++;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (start < len) {
|
|
145
|
+
const remaining = fullText.slice(start);
|
|
146
|
+
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;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (parsedLinesInChunk > 0) {
|
|
154
|
+
this._delegate.onLinesParsed(parsedLinesInChunk);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
_processLine(line) {
|
|
158
|
+
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;
|
|
166
|
+
this._eventType = undefined;
|
|
167
|
+
this._delegate.onEvent(id, type, data);
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
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;
|
|
175
|
+
this._delegate.onComment(comment);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const colonIndex = line.indexOf(':');
|
|
179
|
+
let field;
|
|
180
|
+
let value;
|
|
181
|
+
if (colonIndex !== -1) {
|
|
182
|
+
field = line.slice(0, colonIndex);
|
|
183
|
+
const rawValue = line.slice(colonIndex + 1);
|
|
184
|
+
value = rawValue.startsWith(' ') ? rawValue.slice(1) : rawValue;
|
|
185
|
+
} else {
|
|
186
|
+
field = line;
|
|
187
|
+
value = '';
|
|
188
|
+
}
|
|
189
|
+
switch (field) {
|
|
190
|
+
case 'data':
|
|
191
|
+
{
|
|
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
|
+
}
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
case 'id':
|
|
208
|
+
{
|
|
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
|
+
}
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
case 'event':
|
|
217
|
+
{
|
|
218
|
+
this._eventType = value === '' ? undefined : value;
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
case 'retry':
|
|
222
|
+
{
|
|
223
|
+
// WHATWG SSE: If field value consists solely of ASCII digits, set reconnection time
|
|
224
|
+
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
|
+
}
|
|
229
|
+
}
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
default:
|
|
233
|
+
// Unknown fields ignored per WHATWG spec
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
//# sourceMappingURL=SseEventParser.js.map
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
import { Platform } from 'react-native';
|
|
3
4
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
4
5
|
import { NitroSseClient } from "./NitroSseClient.js";
|
|
6
|
+
import { FetchSseDriver } from "./FetchSseDriver.js";
|
|
5
7
|
import { NitroSseModuleNotFoundError } from "./NitroSseError.js";
|
|
6
8
|
/**
|
|
7
9
|
* Creates a high-performance SSE client that supports typed event listeners (`addEventListener`) and legacy batching.
|
|
8
10
|
*
|
|
9
|
-
* @returns An SseClient instance wrapping the native NitroSse implementation.
|
|
10
|
-
* @throws {NitroSseModuleNotFoundError} If the native NitroSse module cannot be found
|
|
11
|
+
* @returns An SseClient instance wrapping the native NitroSse implementation, or FetchSseDriver on Web.
|
|
12
|
+
* @throws {NitroSseModuleNotFoundError} If the native NitroSse module cannot be found on iOS/Android.
|
|
11
13
|
*/
|
|
12
14
|
export function createNitroSse() {
|
|
13
15
|
let nativeSse;
|
|
@@ -16,9 +18,14 @@ export function createNitroSse() {
|
|
|
16
18
|
} catch {
|
|
17
19
|
console.debug('Native NitroSse not found. This might be a test environment or web.');
|
|
18
20
|
}
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
+
if (nativeSse) {
|
|
22
|
+
return new NitroSseClient(nativeSse);
|
|
21
23
|
}
|
|
22
|
-
|
|
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.');
|
|
23
30
|
}
|
|
24
31
|
//# sourceMappingURL=createNitroSse.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
4
|
+
import { NitroSseClient } from "./NitroSseClient.js";
|
|
5
|
+
import { NitroSseModuleNotFoundError } from "./NitroSseError.js";
|
|
6
|
+
/**
|
|
7
|
+
* Creates an SSE client for iOS and Android using Nitro Modules (JSI).
|
|
8
|
+
*
|
|
9
|
+
* @returns An SseClient instance backed by native NitroSse.
|
|
10
|
+
* @throws {NitroSseModuleNotFoundError} If the native NitroSse module cannot be found.
|
|
11
|
+
*/
|
|
12
|
+
export function createNitroSse() {
|
|
13
|
+
let nativeSse;
|
|
14
|
+
try {
|
|
15
|
+
nativeSse = NitroModules.createHybridObject('NitroSse');
|
|
16
|
+
} catch {
|
|
17
|
+
console.debug('Native NitroSse not found. This might be a test environment or web.');
|
|
18
|
+
}
|
|
19
|
+
if (nativeSse) {
|
|
20
|
+
return new NitroSseClient(nativeSse);
|
|
21
|
+
}
|
|
22
|
+
throw new NitroSseModuleNotFoundError('NitroSse: Native module not found. Ensure you have linked the library and built the app for iOS/Android.');
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=createNitroSse.native.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { NitroSseClient } from "./NitroSseClient.js";
|
|
4
|
+
import { FetchSseDriver } from "./FetchSseDriver.js";
|
|
5
|
+
/**
|
|
6
|
+
* Creates an SSE client for Web platform using WHATWG fetch() and ReadableStream.
|
|
7
|
+
*
|
|
8
|
+
* @returns An SseClient instance backed by FetchSseDriver.
|
|
9
|
+
*/
|
|
10
|
+
export function createNitroSse() {
|
|
11
|
+
return new NitroSseClient(new FetchSseDriver());
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=createNitroSse.web.js.map
|
package/lib/module/index.js
CHANGED
|
@@ -4,5 +4,6 @@ export * from "./SseInterface.js";
|
|
|
4
4
|
export * from "./useNitroSse.js";
|
|
5
5
|
export * from "./NitroSseClient.js";
|
|
6
6
|
export * from "./NitroSseError.js";
|
|
7
|
-
export * from
|
|
7
|
+
export * from './createNitroSse';
|
|
8
|
+
export { FetchSseDriver } from "./FetchSseDriver.js";
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
export * from "./SseInterface.js";
|
|
4
|
+
export * from "./useNitroSse.js";
|
|
5
|
+
export * from "./NitroSseClient.js";
|
|
6
|
+
export * from "./NitroSseError.js";
|
|
7
|
+
export * from "./createNitroSse.web.js";
|
|
8
|
+
export { FetchSseDriver } from "./FetchSseDriver.js";
|
|
9
|
+
//# sourceMappingURL=index.web.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
import { useEffect, useRef, useState, useCallback } from 'react';
|
|
4
|
-
import { createNitroSse } from
|
|
4
|
+
import { createNitroSse } from './createNitroSse';
|
|
5
5
|
/**
|
|
6
6
|
* Safely serializes an object into a stable JSON string:
|
|
7
7
|
* - Sorts object keys to avoid reconnection on equivalent configs with reordered keys.
|