react-native-nitro-sse 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -4
- package/android/build.gradle +3 -1
- package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +3 -2
- package/lib/module/FetchSseDriver.js +857 -0
- package/lib/module/NitroSseClient.js +19 -17
- 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/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/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/package.json +18 -7
|
@@ -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();
|
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.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { SseDriver } from './SseDriver';
|
|
2
|
+
import type { SseConfig, SseEvent, SseState, SseStats } from './SseInterface';
|
|
3
|
+
import { type SseEventParserDelegate } from './SseEventParser';
|
|
4
|
+
/**
|
|
5
|
+
* Web SSE Driver executing server-sent events using WHATWG fetch(), ReadableStream, and TextDecoder.
|
|
6
|
+
*/
|
|
7
|
+
export declare class FetchSseDriver implements SseDriver, SseEventParserDelegate {
|
|
8
|
+
private _config?;
|
|
9
|
+
private _onBeforeRequest?;
|
|
10
|
+
private _dispatchEvents;
|
|
11
|
+
private _state;
|
|
12
|
+
private _isRunning;
|
|
13
|
+
private _isReconnecting;
|
|
14
|
+
private _lastProcessedId?;
|
|
15
|
+
private _dynamicHeaders;
|
|
16
|
+
private _consecutiveAuthErrors;
|
|
17
|
+
private _connectionAttemptVersion;
|
|
18
|
+
private _abortController?;
|
|
19
|
+
private _activeReader?;
|
|
20
|
+
private _connectionTimer?;
|
|
21
|
+
private _readTimeoutTimer?;
|
|
22
|
+
private _reconnectTimer?;
|
|
23
|
+
private _batchTimer?;
|
|
24
|
+
private _batchBuffer;
|
|
25
|
+
private _wasRunningBeforeNetworkLoss;
|
|
26
|
+
private _reconnectStrategy;
|
|
27
|
+
private _parser?;
|
|
28
|
+
private _rawBytesReceived;
|
|
29
|
+
private _totalBytesReceived;
|
|
30
|
+
private _chunksReceived;
|
|
31
|
+
private _lastStatusCode?;
|
|
32
|
+
private _totalEventsReceived;
|
|
33
|
+
private _commentsReceived;
|
|
34
|
+
private _linesParsed;
|
|
35
|
+
private _parseErrors;
|
|
36
|
+
private _serverRetryDelayMs?;
|
|
37
|
+
private _connectedAt?;
|
|
38
|
+
private _timeToFirstByteMs?;
|
|
39
|
+
private _connectionAttemptStartTime?;
|
|
40
|
+
private _lastEventTime?;
|
|
41
|
+
private _lastHeartbeatTime?;
|
|
42
|
+
private _lastActivityTime;
|
|
43
|
+
private _maxEventGapMs;
|
|
44
|
+
private _peakBufferedEvents;
|
|
45
|
+
private _bufferFlushCount;
|
|
46
|
+
private _bufferOverflowCount;
|
|
47
|
+
private _connectionAttempts;
|
|
48
|
+
private _reconnectCount;
|
|
49
|
+
private _lastReconnectDelayMs?;
|
|
50
|
+
private _disconnectReason?;
|
|
51
|
+
private _lastErrorTime?;
|
|
52
|
+
private _lastErrorCode?;
|
|
53
|
+
private _onlineListener?;
|
|
54
|
+
private _offlineListener?;
|
|
55
|
+
constructor(dispatchEvents?: (events: SseEvent[]) => void);
|
|
56
|
+
setDispatchEvents(dispatchEvents: (events: SseEvent[]) => void): void;
|
|
57
|
+
setup(config: SseConfig, onBeforeRequest?: () => Promise<Record<string, string>>): void;
|
|
58
|
+
start(): void;
|
|
59
|
+
stop(): void;
|
|
60
|
+
restart(): void;
|
|
61
|
+
flush(): void;
|
|
62
|
+
isConnected(): boolean;
|
|
63
|
+
getStats(): SseStats;
|
|
64
|
+
getState(): SseState;
|
|
65
|
+
updateHeaders(headers: Record<string, string>): void;
|
|
66
|
+
setLastProcessedId(id: string): void;
|
|
67
|
+
injectMockEvent(event: Partial<SseEvent>): void;
|
|
68
|
+
dispose(): void;
|
|
69
|
+
private _establishConnection;
|
|
70
|
+
private _scheduleReconnect;
|
|
71
|
+
private _stopInternal;
|
|
72
|
+
private _failAndStop;
|
|
73
|
+
private _cleanupTransport;
|
|
74
|
+
private _cleanupTimers;
|
|
75
|
+
private _resetReadTimeout;
|
|
76
|
+
private _clearReadTimeout;
|
|
77
|
+
private _updateState;
|
|
78
|
+
private _recordError;
|
|
79
|
+
private _parseRetryAfterHeader;
|
|
80
|
+
private _pushEvent;
|
|
81
|
+
private _flushBatch;
|
|
82
|
+
private _cancelBatchTimer;
|
|
83
|
+
onEvent(id: string | undefined, type: string | undefined, data: string): void;
|
|
84
|
+
onComment(comment: string): void;
|
|
85
|
+
onRetry(retryMs: number): void;
|
|
86
|
+
onIdUpdate(id: string | undefined): void;
|
|
87
|
+
onParseError(error: Error): void;
|
|
88
|
+
onLinesParsed(count: number): void;
|
|
89
|
+
private _setupNetworkListeners;
|
|
90
|
+
private _teardownNetworkListeners;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=FetchSseDriver.d.ts.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AnyMap } from 'react-native-nitro-modules';
|
|
2
2
|
import type { NitroSse } from './NitroSse.nitro';
|
|
3
3
|
import type { SseClient, SseClientOptions, SseEvent, SseListener, SseStats, SseState } from './SseInterface';
|
|
4
|
+
import { type SseDriver } from './SseDriver';
|
|
4
5
|
/**
|
|
5
6
|
* Sanitizes HTTP headers by stripping carriage returns, newlines, null bytes,
|
|
6
7
|
* converting non-string values to strings, and pruning null/undefined values.
|
|
@@ -21,13 +22,15 @@ export declare function validateConfig(config: SseClientOptions): SseClientOptio
|
|
|
21
22
|
*/
|
|
22
23
|
export declare class NitroSseClient implements SseClient {
|
|
23
24
|
private _native;
|
|
25
|
+
private _baseDriver;
|
|
24
26
|
private _driver;
|
|
25
27
|
private _listeners;
|
|
26
28
|
private _legacyCallback?;
|
|
27
29
|
private _config?;
|
|
28
30
|
private _isDisposed;
|
|
29
31
|
private _pendingHeaders;
|
|
30
|
-
constructor(
|
|
32
|
+
constructor(nativeOrDriver: NitroSse | SseDriver);
|
|
33
|
+
static fromDriver(driver: SseDriver): NitroSseClient;
|
|
31
34
|
get isDisposed(): boolean;
|
|
32
35
|
private _checkDisposed;
|
|
33
36
|
setup(config: SseClientOptions, onEvent?: (events: SseEvent[]) => void): void;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { NitroSse } from './NitroSse.nitro';
|
|
2
|
-
import type { SseEvent, SseState, SseStats } from './SseInterface';
|
|
2
|
+
import type { SseConfig, SseEvent, SseState, SseStats } from './SseInterface';
|
|
3
3
|
import { MockSseEngine } from './MockSseEngine';
|
|
4
4
|
/**
|
|
5
5
|
* Internal execution strategy driving the SSE lifecycle.
|
|
6
6
|
*/
|
|
7
7
|
export interface SseDriver {
|
|
8
|
+
setup(config: SseConfig, onBeforeRequest?: () => Promise<Record<string, string>>): void;
|
|
8
9
|
start(): void;
|
|
9
10
|
stop(): void;
|
|
10
11
|
restart(): void;
|
|
@@ -24,6 +25,7 @@ export declare class NativeDriver implements SseDriver {
|
|
|
24
25
|
private _native;
|
|
25
26
|
private _dispatchEvents;
|
|
26
27
|
constructor(_native: NitroSse, _dispatchEvents: (events: SseEvent[]) => void);
|
|
28
|
+
setup(config: SseConfig, onBeforeRequest?: () => Promise<Record<string, string>>): void;
|
|
27
29
|
start(): void;
|
|
28
30
|
stop(): void;
|
|
29
31
|
restart(): void;
|
|
@@ -46,6 +48,7 @@ export declare class MockReplaceDriver implements SseDriver {
|
|
|
46
48
|
constructor(_mockEngine: MockSseEngine);
|
|
47
49
|
get headers(): Record<string, string>;
|
|
48
50
|
get lastProcessedId(): string | undefined;
|
|
51
|
+
setup(config: SseConfig, _onBeforeRequest?: () => Promise<Record<string, string>>): void;
|
|
49
52
|
start(): void;
|
|
50
53
|
stop(): void;
|
|
51
54
|
restart(): void;
|
|
@@ -59,12 +62,13 @@ export declare class MockReplaceDriver implements SseDriver {
|
|
|
59
62
|
dispose(): void;
|
|
60
63
|
}
|
|
61
64
|
/**
|
|
62
|
-
* Driver running
|
|
65
|
+
* Driver running underlying network streaming in parallel with mock event injection.
|
|
63
66
|
*/
|
|
64
67
|
export declare class MockInjectDriver implements SseDriver {
|
|
65
|
-
private
|
|
68
|
+
private _realDriver;
|
|
66
69
|
private _mockEngine;
|
|
67
|
-
constructor(
|
|
70
|
+
constructor(_realDriver: SseDriver, _mockEngine: MockSseEngine);
|
|
71
|
+
setup(config: SseConfig, onBeforeRequest?: () => Promise<Record<string, string>>): void;
|
|
68
72
|
start(): void;
|
|
69
73
|
stop(): void;
|
|
70
74
|
restart(): void;
|