react-native-nitro-sse 3.2.0 → 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.
- package/README.md +50 -27
- package/android/build.gradle +4 -2
- package/android/src/main/java/com/margelo/nitro/nitrosse/JsonUtils.kt +8 -32
- package/android/src/main/java/com/margelo/nitro/nitrosse/NetworkInspector.kt +124 -6
- package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +439 -510
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseConnectionHandler.kt +137 -316
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventBuffer.kt +18 -8
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseEventReader.kt +231 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseFailureClassifier.kt +245 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseMetrics.kt +237 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseNetworkMonitor.kt +13 -6
- package/android/src/main/java/com/margelo/nitro/nitrosse/SseReconnectStrategy.kt +48 -22
- package/ios/NitroSse.swift +402 -484
- package/ios/SseConfig+CopyWith.swift +0 -2
- package/ios/SseConnectionHandler.swift +238 -446
- package/ios/SseDispatchQueueDispatcher.swift +7 -7
- package/ios/SseDispatcher.swift +5 -4
- package/ios/SseEventBuffer.swift +24 -23
- package/ios/SseEventParser.swift +360 -0
- package/ios/SseFailureClassifier.swift +249 -0
- package/ios/SseLifecycleManager.swift +19 -14
- package/ios/SseMetrics.swift +188 -0
- package/ios/SseNetworkMonitor.swift +18 -14
- package/ios/SseReconnectStrategy.swift +28 -30
- package/lib/module/FetchSseDriver.js +385 -169
- package/lib/module/MockSseEngine.js +15 -17
- package/lib/module/NitroSseClient.js +70 -56
- package/lib/module/SseConsoleLogger.js +64 -0
- package/lib/module/SseEventParser.js +173 -115
- package/lib/module/createNitroSse.js +7 -14
- package/lib/module/createNitroSse.native.js +2 -1
- package/lib/module/useNitroSse.js +103 -90
- package/lib/typescript/src/FetchSseDriver.d.ts +11 -4
- package/lib/typescript/src/MockSseEngine.d.ts +2 -2
- package/lib/typescript/src/NitroSse.nitro.d.ts +9 -1
- package/lib/typescript/src/NitroSseClient.d.ts +4 -1
- package/lib/typescript/src/SseConsoleLogger.d.ts +28 -0
- package/lib/typescript/src/SseEventParser.d.ts +18 -2
- package/lib/typescript/src/SseInterface.d.ts +21 -4
- package/nitrogen/generated/android/c++/JFunc_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____.hpp +1 -1
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_SseEvent_.hpp +1 -1
- package/nitrogen/generated/android/c++/JHttpMethod.hpp +1 -1
- package/nitrogen/generated/android/c++/JHybridNitroSseSpec.cpp +1 -1
- package/nitrogen/generated/android/c++/JHybridNitroSseSpec.hpp +1 -1
- package/nitrogen/generated/android/c++/JSseConfig.hpp +1 -1
- package/nitrogen/generated/android/c++/JSseDisconnectReason.hpp +1 -1
- package/nitrogen/generated/android/c++/JSseEvent.hpp +1 -1
- package/nitrogen/generated/android/c++/JSseEventType.hpp +1 -1
- package/nitrogen/generated/android/c++/JSseMockConfig.hpp +1 -1
- package/nitrogen/generated/android/c++/JSseMockEvent.hpp +1 -1
- package/nitrogen/generated/android/c++/JSseMockMode.hpp +1 -1
- package/nitrogen/generated/android/c++/JSseState.hpp +1 -1
- package/nitrogen/generated/android/c++/JSseStats.hpp +1 -1
- 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
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/Func_void_std__vector_SseEvent_.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/HttpMethod.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/HybridNitroSseSpec.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseConfig.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseDisconnectReason.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseEvent.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseEventType.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockConfig.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockEvent.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockMode.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseState.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseStats.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/nitrosseOnLoad.kt +1 -1
- package/nitrogen/generated/android/nitrosse+autolinking.cmake +1 -1
- package/nitrogen/generated/android/nitrosse+autolinking.gradle +1 -1
- package/nitrogen/generated/android/nitrosseOnLoad.cpp +1 -1
- package/nitrogen/generated/android/nitrosseOnLoad.hpp +1 -1
- package/nitrogen/generated/ios/NitroSse+autolinking.rb +1 -1
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.cpp +1 -1
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +1 -1
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Umbrella.hpp +1 -1
- package/nitrogen/generated/ios/NitroSseAutolinking.mm +1 -1
- package/nitrogen/generated/ios/NitroSseAutolinking.swift +1 -5
- package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.cpp +1 -1
- package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +1 -1
- package/nitrogen/generated/ios/swift/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__unordered_map_std__string__std__string_____.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_Promise_std__unordered_map_std__string__std__string___.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__unordered_map_std__string__std__string_.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__vector_SseEvent_.swift +1 -1
- package/nitrogen/generated/ios/swift/HttpMethod.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridNitroSseSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridNitroSseSpec_cxx.swift +3 -3
- package/nitrogen/generated/ios/swift/SseConfig.swift +1 -1
- package/nitrogen/generated/ios/swift/SseDisconnectReason.swift +1 -1
- package/nitrogen/generated/ios/swift/SseEvent.swift +1 -1
- package/nitrogen/generated/ios/swift/SseEventType.swift +1 -1
- package/nitrogen/generated/ios/swift/SseMockConfig.swift +1 -1
- package/nitrogen/generated/ios/swift/SseMockEvent.swift +1 -1
- package/nitrogen/generated/ios/swift/SseMockMode.swift +1 -1
- package/nitrogen/generated/ios/swift/SseState.swift +1 -1
- package/nitrogen/generated/ios/swift/SseStats.swift +1 -1
- package/nitrogen/generated/shared/c++/HttpMethod.hpp +1 -1
- package/nitrogen/generated/shared/c++/HybridNitroSseSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridNitroSseSpec.hpp +1 -1
- package/nitrogen/generated/shared/c++/SseConfig.hpp +1 -1
- package/nitrogen/generated/shared/c++/SseDisconnectReason.hpp +1 -1
- package/nitrogen/generated/shared/c++/SseEvent.hpp +1 -1
- package/nitrogen/generated/shared/c++/SseEventType.hpp +1 -1
- package/nitrogen/generated/shared/c++/SseMockConfig.hpp +1 -1
- package/nitrogen/generated/shared/c++/SseMockEvent.hpp +1 -1
- package/nitrogen/generated/shared/c++/SseMockMode.hpp +1 -1
- package/nitrogen/generated/shared/c++/SseState.hpp +1 -1
- package/nitrogen/generated/shared/c++/SseStats.hpp +1 -1
- package/package.json +12 -4
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* Handles timer intervals, looping, custom delays, simulated error rates, and mock state tracking.
|
|
6
6
|
*/
|
|
7
7
|
export class MockSseEngine {
|
|
8
|
+
_runGeneration = 0;
|
|
8
9
|
_mockIndex = 0;
|
|
9
10
|
_totalBytesReceived = 0;
|
|
10
11
|
_chunksReceived = 0;
|
|
@@ -53,13 +54,13 @@ export class MockSseEngine {
|
|
|
53
54
|
|
|
54
55
|
/**
|
|
55
56
|
* Starts mock stream scheduling.
|
|
56
|
-
* @returns true if native stream should be skipped (replace mode), false otherwise (inject mode).
|
|
57
57
|
*/
|
|
58
58
|
start() {
|
|
59
59
|
if (this._mockIntervalId) {
|
|
60
60
|
clearTimeout(this._mockIntervalId);
|
|
61
61
|
this._mockIntervalId = undefined;
|
|
62
62
|
}
|
|
63
|
+
const gen = ++this._runGeneration;
|
|
63
64
|
const {
|
|
64
65
|
mode,
|
|
65
66
|
data,
|
|
@@ -82,13 +83,13 @@ export class MockSseEngine {
|
|
|
82
83
|
|
|
83
84
|
// Validate and normalize eventsPerSecond (must be a finite positive number, default to 1)
|
|
84
85
|
let validatedEventsPerSecond = Number(eventsPerSecond);
|
|
85
|
-
if (
|
|
86
|
+
if (!Number.isFinite(validatedEventsPerSecond) || validatedEventsPerSecond <= 0) {
|
|
86
87
|
validatedEventsPerSecond = 1;
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
// Validate and normalize errorRate (must be a finite number within [0,1], default to 0)
|
|
90
91
|
let validatedErrorRate = Number(errorRate);
|
|
91
|
-
if (
|
|
92
|
+
if (!Number.isFinite(validatedErrorRate)) {
|
|
92
93
|
validatedErrorRate = 0;
|
|
93
94
|
} else {
|
|
94
95
|
validatedErrorRate = Math.max(0, Math.min(1, validatedErrorRate));
|
|
@@ -99,14 +100,12 @@ export class MockSseEngine {
|
|
|
99
100
|
const batchSize = Math.max(1, Math.round(validatedEventsPerSecond / 100)); // chunk size for 10ms intervals
|
|
100
101
|
const intervalMs = Math.max(10, delayMs * batchSize);
|
|
101
102
|
const scheduleNext = () => {
|
|
103
|
+
this._mockIntervalId = undefined;
|
|
104
|
+
if (gen !== this._runGeneration) return;
|
|
102
105
|
if (this._mockIndex >= data.length) {
|
|
103
106
|
if (loop && data.length > 0) {
|
|
104
107
|
this._mockIndex = 0;
|
|
105
108
|
} else {
|
|
106
|
-
if (this._mockIntervalId) {
|
|
107
|
-
clearTimeout(this._mockIntervalId);
|
|
108
|
-
this._mockIntervalId = undefined;
|
|
109
|
-
}
|
|
110
109
|
if (mode === 'replace') {
|
|
111
110
|
this._setMockState('closed');
|
|
112
111
|
}
|
|
@@ -128,6 +127,7 @@ export class MockSseEngine {
|
|
|
128
127
|
this._lastErrorCode = 'SIMULATED_MOCK_FAILURE';
|
|
129
128
|
this._reconnectCount++;
|
|
130
129
|
this._emitEvents([errorEvent]);
|
|
130
|
+
if (gen !== this._runGeneration) return;
|
|
131
131
|
|
|
132
132
|
// Retry / reconnect delay simulator (e.g. 2000ms)
|
|
133
133
|
this._mockIntervalId = setTimeout(scheduleNext, 2000);
|
|
@@ -137,10 +137,8 @@ export class MockSseEngine {
|
|
|
137
137
|
const hasCustomDelay = currentItem && typeof currentItem.delayMs === 'number';
|
|
138
138
|
const batch = [];
|
|
139
139
|
if (hasCustomDelay) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
batch.push(MockSseEngine.createSseEvent(rawMock));
|
|
143
|
-
}
|
|
140
|
+
batch.push(MockSseEngine.createSseEvent(currentItem));
|
|
141
|
+
this._mockIndex++;
|
|
144
142
|
} else {
|
|
145
143
|
for (let i = 0; i < batchSize && this._mockIndex < data.length; i++) {
|
|
146
144
|
const nextItem = data[this._mockIndex];
|
|
@@ -185,6 +183,7 @@ export class MockSseEngine {
|
|
|
185
183
|
this._setMockState('open');
|
|
186
184
|
}
|
|
187
185
|
this._emitEvents(batch);
|
|
186
|
+
if (gen !== this._runGeneration) return;
|
|
188
187
|
}
|
|
189
188
|
const nextItem = this._mockIndex < data.length ? data[this._mockIndex] : data[0];
|
|
190
189
|
const nextDelay = this._mockIndex >= data.length && !loop ? 0 : typeof nextItem?.delayMs === 'number' ? nextItem.delayMs : intervalMs;
|
|
@@ -194,7 +193,9 @@ export class MockSseEngine {
|
|
|
194
193
|
const firstDelay = firstItem && typeof firstItem.delayMs === 'number' ? firstItem.delayMs : intervalMs;
|
|
195
194
|
if (mode === 'replace') {
|
|
196
195
|
this._setMockState('connecting');
|
|
196
|
+
if (gen !== this._runGeneration) return;
|
|
197
197
|
this._setMockState('open');
|
|
198
|
+
if (gen !== this._runGeneration) return;
|
|
198
199
|
|
|
199
200
|
// Emit simulated open event
|
|
200
201
|
const openEvent = {
|
|
@@ -202,15 +203,12 @@ export class MockSseEngine {
|
|
|
202
203
|
statusCode: 200
|
|
203
204
|
};
|
|
204
205
|
this._emitEvents([openEvent]);
|
|
205
|
-
|
|
206
|
-
return true; // Indicates replace mode (skip native start)
|
|
207
|
-
} else if (mode === 'inject') {
|
|
208
|
-
this._mockIntervalId = setTimeout(scheduleNext, firstDelay);
|
|
209
|
-
return false; // Indicates inject mode (continue native start)
|
|
206
|
+
if (gen !== this._runGeneration) return;
|
|
210
207
|
}
|
|
211
|
-
|
|
208
|
+
this._mockIntervalId = setTimeout(scheduleNext, firstDelay);
|
|
212
209
|
}
|
|
213
210
|
stop() {
|
|
211
|
+
this._runGeneration++;
|
|
214
212
|
if (this._mockIntervalId) {
|
|
215
213
|
clearTimeout(this._mockIntervalId);
|
|
216
214
|
this._mockIntervalId = undefined;
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
import { MockSseEngine } from "./MockSseEngine.js";
|
|
4
4
|
import { NativeDriver, MockReplaceDriver, MockInjectDriver } from "./SseDriver.js";
|
|
5
5
|
import { NitroSseValidationError, NitroSseDisposedError, NitroSseStateError } from "./NitroSseError.js";
|
|
6
|
+
import { devWarn, isDevEnvironment, logError, UnhandledSseErrorReporter } from "./SseConsoleLogger.js";
|
|
7
|
+
|
|
6
8
|
/**
|
|
7
9
|
* Sanitizes HTTP headers by stripping carriage returns, newlines, null bytes,
|
|
8
10
|
* converting non-string values to strings, and pruning null/undefined values.
|
|
@@ -41,16 +43,18 @@ function isLoopbackUrl(url) {
|
|
|
41
43
|
* Warns in development when credential headers are transmitted over insecure cleartext HTTP (non-loopback).
|
|
42
44
|
*/
|
|
43
45
|
export function warnInsecureCredentials(url, headers) {
|
|
44
|
-
if (
|
|
45
|
-
if (!url || typeof url !== 'string' || !headers || typeof headers !== 'object') return;
|
|
46
|
+
if (!isDevEnvironment()) return false;
|
|
47
|
+
if (!url || typeof url !== 'string' || !headers || typeof headers !== 'object') return false;
|
|
46
48
|
const trimmedUrl = url.trim();
|
|
47
49
|
const lowerUrl = trimmedUrl.toLowerCase();
|
|
48
50
|
if (lowerUrl.startsWith('http://') && !isLoopbackUrl(trimmedUrl)) {
|
|
49
51
|
const hasSensitiveHeader = Object.keys(headers).some(key => SENSITIVE_HEADER_REGEX.test(key.trim()));
|
|
50
52
|
if (hasSensitiveHeader) {
|
|
51
|
-
|
|
53
|
+
devWarn(`[NitroSse] Security Warning: Sensitive credential header(s) detected over an insecure, unencrypted HTTP connection ("${trimmedUrl}"). Transmitting credentials over plaintext HTTP is vulnerable to interception and token theft. Use HTTPS in production.`);
|
|
54
|
+
return true;
|
|
52
55
|
}
|
|
53
56
|
}
|
|
57
|
+
return false;
|
|
54
58
|
}
|
|
55
59
|
|
|
56
60
|
/**
|
|
@@ -82,9 +86,7 @@ export function validateConfig(config) {
|
|
|
82
86
|
});
|
|
83
87
|
}
|
|
84
88
|
if (!lowerUrl.startsWith('http://') && !lowerUrl.startsWith('https://')) {
|
|
85
|
-
|
|
86
|
-
console.warn(`[NitroSse] URL "${trimmedUrl}" does not start with http:// or https://. Ensure it is an absolute URL; relative paths will fail on native.`);
|
|
87
|
-
}
|
|
89
|
+
devWarn(`[NitroSse] URL "${trimmedUrl}" does not start with http:// or https://. Ensure it is an absolute URL; relative paths will fail on native.`);
|
|
88
90
|
}
|
|
89
91
|
if (config.headers !== undefined) {
|
|
90
92
|
if (!config.headers || typeof config.headers !== 'object' || Array.isArray(config.headers)) {
|
|
@@ -118,36 +120,28 @@ export function validateConfig(config) {
|
|
|
118
120
|
let connectionTimeoutMs = config.connectionTimeoutMs;
|
|
119
121
|
if (connectionTimeoutMs !== undefined) {
|
|
120
122
|
if (!Number.isFinite(connectionTimeoutMs) || connectionTimeoutMs < 0) {
|
|
121
|
-
|
|
122
|
-
console.warn(`[NitroSse] Invalid connectionTimeoutMs (${connectionTimeoutMs}). Resetting to default 15000.`);
|
|
123
|
-
}
|
|
123
|
+
devWarn(`[NitroSse] Invalid connectionTimeoutMs (${connectionTimeoutMs}). Resetting to default 15000.`);
|
|
124
124
|
connectionTimeoutMs = 15000;
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
let readTimeoutMs = config.readTimeoutMs;
|
|
128
128
|
if (readTimeoutMs !== undefined) {
|
|
129
129
|
if (!Number.isFinite(readTimeoutMs) || readTimeoutMs <= 0) {
|
|
130
|
-
|
|
131
|
-
console.warn(`[NitroSse] Invalid readTimeoutMs (${readTimeoutMs}). Resetting to default 300000.`);
|
|
132
|
-
}
|
|
130
|
+
devWarn(`[NitroSse] Invalid readTimeoutMs (${readTimeoutMs}). Resetting to default 300000.`);
|
|
133
131
|
readTimeoutMs = 300000;
|
|
134
132
|
}
|
|
135
133
|
}
|
|
136
134
|
let batchingIntervalMs = config.batchingIntervalMs;
|
|
137
135
|
if (batchingIntervalMs !== undefined) {
|
|
138
136
|
if (!Number.isFinite(batchingIntervalMs) || batchingIntervalMs < 0) {
|
|
139
|
-
|
|
140
|
-
console.warn(`[NitroSse] Invalid batchingIntervalMs (${batchingIntervalMs}). Clamping to 0.`);
|
|
141
|
-
}
|
|
137
|
+
devWarn(`[NitroSse] Invalid batchingIntervalMs (${batchingIntervalMs}). Clamping to 0.`);
|
|
142
138
|
batchingIntervalMs = 0;
|
|
143
139
|
}
|
|
144
140
|
}
|
|
145
141
|
let maxBufferSize = config.maxBufferSize;
|
|
146
142
|
if (maxBufferSize !== undefined) {
|
|
147
143
|
if (!Number.isFinite(maxBufferSize) || maxBufferSize <= 0) {
|
|
148
|
-
|
|
149
|
-
console.warn(`[NitroSse] Invalid maxBufferSize (${maxBufferSize}). Resetting to default 1000.`);
|
|
150
|
-
}
|
|
144
|
+
devWarn(`[NitroSse] Invalid maxBufferSize (${maxBufferSize}). Resetting to default 1000.`);
|
|
151
145
|
maxBufferSize = 1000;
|
|
152
146
|
}
|
|
153
147
|
}
|
|
@@ -164,35 +158,33 @@ export function validateConfig(config) {
|
|
|
164
158
|
}
|
|
165
159
|
}
|
|
166
160
|
if (retryIntervalMs !== undefined && maxRetryIntervalMs !== undefined && retryIntervalMs > maxRetryIntervalMs) {
|
|
167
|
-
|
|
168
|
-
console.warn(`[NitroSse] retryIntervalMs (${retryIntervalMs}) cannot exceed maxRetryIntervalMs (${maxRetryIntervalMs}). Clamping retryIntervalMs.`);
|
|
169
|
-
}
|
|
161
|
+
devWarn(`[NitroSse] retryIntervalMs (${retryIntervalMs}) cannot exceed maxRetryIntervalMs (${maxRetryIntervalMs}). Clamping retryIntervalMs.`);
|
|
170
162
|
retryIntervalMs = maxRetryIntervalMs;
|
|
171
163
|
}
|
|
164
|
+
if (retryIntervalMs !== undefined && retryIntervalMs < 1000) {
|
|
165
|
+
devWarn(`[NitroSse] retryIntervalMs (${retryIntervalMs}ms) is below the 1000ms minimum reconnect floor enforced across all platforms. Effective reconnect delays will be at least 1000ms.`);
|
|
166
|
+
}
|
|
167
|
+
if (maxRetryIntervalMs !== undefined && maxRetryIntervalMs < 1000) {
|
|
168
|
+
devWarn(`[NitroSse] maxRetryIntervalMs (${maxRetryIntervalMs}ms) is below the 1000ms minimum reconnect floor enforced across all platforms. Effective reconnect delays will be at least 1000ms.`);
|
|
169
|
+
}
|
|
172
170
|
let jitterFactor = config.jitterFactor;
|
|
173
171
|
if (jitterFactor !== undefined) {
|
|
174
172
|
if (!Number.isFinite(jitterFactor) || jitterFactor < 0 || jitterFactor > 1) {
|
|
175
|
-
|
|
176
|
-
console.warn(`[NitroSse] Invalid jitterFactor (${jitterFactor}). Clamping between 0.0 and 1.0.`);
|
|
177
|
-
}
|
|
173
|
+
devWarn(`[NitroSse] Invalid jitterFactor (${jitterFactor}). Clamping between 0.0 and 1.0.`);
|
|
178
174
|
jitterFactor = Math.max(0, Math.min(1, Number.isFinite(jitterFactor) ? jitterFactor : 0.5));
|
|
179
175
|
}
|
|
180
176
|
}
|
|
181
177
|
let maxReconnectAttempts = config.maxReconnectAttempts;
|
|
182
178
|
if (maxReconnectAttempts !== undefined) {
|
|
183
179
|
if (!Number.isFinite(maxReconnectAttempts) || maxReconnectAttempts < -1) {
|
|
184
|
-
|
|
185
|
-
console.warn(`[NitroSse] Invalid maxReconnectAttempts (${maxReconnectAttempts}). Defaulting to -1 (infinite).`);
|
|
186
|
-
}
|
|
180
|
+
devWarn(`[NitroSse] Invalid maxReconnectAttempts (${maxReconnectAttempts}). Defaulting to -1 (infinite).`);
|
|
187
181
|
maxReconnectAttempts = -1;
|
|
188
182
|
}
|
|
189
183
|
}
|
|
190
184
|
let maxAuthRetries = config.maxAuthRetries;
|
|
191
185
|
if (maxAuthRetries !== undefined) {
|
|
192
186
|
if (!Number.isFinite(maxAuthRetries) || maxAuthRetries < 0) {
|
|
193
|
-
|
|
194
|
-
console.warn(`[NitroSse] Invalid maxAuthRetries (${maxAuthRetries}). Defaulting to 3.`);
|
|
195
|
-
}
|
|
187
|
+
devWarn(`[NitroSse] Invalid maxAuthRetries (${maxAuthRetries}). Defaulting to 3.`);
|
|
196
188
|
maxAuthRetries = 3;
|
|
197
189
|
}
|
|
198
190
|
}
|
|
@@ -257,11 +249,14 @@ export class NitroSseClient {
|
|
|
257
249
|
_listeners = new Map();
|
|
258
250
|
_isDisposed = false;
|
|
259
251
|
_pendingHeaders = {};
|
|
252
|
+
_unhandledErrorReporter = new UnhandledSseErrorReporter();
|
|
253
|
+
_warnedInsecureInterceptorUrl = null;
|
|
254
|
+
_isConfiguring = false;
|
|
260
255
|
constructor(nativeOrDriver) {
|
|
261
256
|
if (typeof nativeOrDriver === 'object' && nativeOrDriver !== null && 'injectMockEvent' in nativeOrDriver) {
|
|
262
257
|
this._native = null;
|
|
263
258
|
this._baseDriver = nativeOrDriver;
|
|
264
|
-
if (
|
|
259
|
+
if (typeof this._baseDriver.setDispatchEvents === 'function') {
|
|
265
260
|
this._baseDriver.setDispatchEvents(events => this._dispatchEvents(events));
|
|
266
261
|
}
|
|
267
262
|
} else {
|
|
@@ -289,11 +284,16 @@ export class NitroSseClient {
|
|
|
289
284
|
});
|
|
290
285
|
}
|
|
291
286
|
const validatedConfig = validateConfig(config);
|
|
287
|
+
this._unhandledErrorReporter.reset();
|
|
288
|
+
this._warnedInsecureInterceptorUrl = null;
|
|
292
289
|
if (this.isConnected()) {
|
|
293
|
-
|
|
294
|
-
|
|
290
|
+
devWarn('[NitroSse] setup() called while connection is active; stopping previous stream before reconfiguring.');
|
|
291
|
+
this._isConfiguring = true;
|
|
292
|
+
try {
|
|
293
|
+
this._driver.stop();
|
|
294
|
+
} finally {
|
|
295
|
+
this._isConfiguring = false;
|
|
295
296
|
}
|
|
296
|
-
this._driver?.stop();
|
|
297
297
|
}
|
|
298
298
|
const hasHeaders = Object.keys(this._pendingHeaders).length > 0 || validatedConfig.headers !== undefined;
|
|
299
299
|
const cleanHeaders = hasHeaders ? sanitizeHeaders({
|
|
@@ -305,15 +305,20 @@ export class NitroSseClient {
|
|
|
305
305
|
try {
|
|
306
306
|
const h = await rawOnBeforeRequest();
|
|
307
307
|
if (h && typeof h === 'object' && !Array.isArray(h)) {
|
|
308
|
-
|
|
308
|
+
if (this._warnedInsecureInterceptorUrl !== validatedConfig.url) {
|
|
309
|
+
const warned = warnInsecureCredentials(validatedConfig.url, h);
|
|
310
|
+
if (warned) {
|
|
311
|
+
this._warnedInsecureInterceptorUrl = validatedConfig.url;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
309
314
|
return sanitizeHeaders(h);
|
|
310
315
|
}
|
|
311
|
-
if (
|
|
312
|
-
|
|
316
|
+
if (h !== undefined && h !== null) {
|
|
317
|
+
devWarn('[NitroSse] onBeforeRequest returned an invalid headers value (expected an object):', h);
|
|
313
318
|
}
|
|
314
319
|
return {};
|
|
315
320
|
} catch (err) {
|
|
316
|
-
|
|
321
|
+
logError('[NitroSse] Error in onBeforeRequest interceptor:', err);
|
|
317
322
|
throw err;
|
|
318
323
|
}
|
|
319
324
|
} : undefined;
|
|
@@ -329,13 +334,13 @@ export class NitroSseClient {
|
|
|
329
334
|
};
|
|
330
335
|
this._pendingHeaders = {};
|
|
331
336
|
this._legacyCallback = onEvent;
|
|
332
|
-
const isDev =
|
|
337
|
+
const isDev = isDevEnvironment();
|
|
333
338
|
const mockConfig = isDev ? this._config.mock : undefined;
|
|
334
339
|
if (!isDev) {
|
|
335
340
|
delete this._config.mock;
|
|
336
341
|
}
|
|
337
342
|
if (mockConfig) {
|
|
338
|
-
|
|
343
|
+
devWarn('\n' + '=================================================================\n' + '⚠️ [react-native-nitro-sse] WARNING: MOCK STREAMING IS ENABLED! ⚠️\n' + ` Mode: ${mockConfig.mode.toUpperCase()} | Speed: ${mockConfig.eventsPerSecond ?? 1} events/sec\n` + ' Please ensure mocking is disabled before building for production!\n' + '=================================================================\n');
|
|
339
344
|
const mockEngine = new MockSseEngine(mockConfig, events => {
|
|
340
345
|
this._dispatchEvents(events);
|
|
341
346
|
});
|
|
@@ -360,6 +365,9 @@ export class NitroSseClient {
|
|
|
360
365
|
});
|
|
361
366
|
}
|
|
362
367
|
const cleanType = type.trim();
|
|
368
|
+
if (cleanType === 'error') {
|
|
369
|
+
this._unhandledErrorReporter.reset();
|
|
370
|
+
}
|
|
363
371
|
if (!this._listeners.has(cleanType)) {
|
|
364
372
|
this._listeners.set(cleanType, new Set());
|
|
365
373
|
}
|
|
@@ -386,19 +394,7 @@ export class NitroSseClient {
|
|
|
386
394
|
const listeners = this._listeners.get(type);
|
|
387
395
|
if (!listeners || listeners.size === 0) return;
|
|
388
396
|
|
|
389
|
-
//
|
|
390
|
-
if (listeners.size === 1) {
|
|
391
|
-
for (const listener of listeners) {
|
|
392
|
-
try {
|
|
393
|
-
listener(event);
|
|
394
|
-
} catch (e) {
|
|
395
|
-
console.error(`[NitroSse] Error in event listener for "${type}":`, e);
|
|
396
|
-
}
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
// Multi-listener path: snapshot count to skip listeners added mid-iteration
|
|
397
|
+
// Snapshot count to skip listeners added mid-iteration
|
|
402
398
|
const snapshot = listeners.size;
|
|
403
399
|
let i = 0;
|
|
404
400
|
for (const listener of listeners) {
|
|
@@ -406,7 +402,7 @@ export class NitroSseClient {
|
|
|
406
402
|
try {
|
|
407
403
|
listener(event);
|
|
408
404
|
} catch (e) {
|
|
409
|
-
|
|
405
|
+
logError(`[NitroSse] Error in event listener for "${type}":`, e);
|
|
410
406
|
}
|
|
411
407
|
}
|
|
412
408
|
}
|
|
@@ -415,10 +411,18 @@ export class NitroSseClient {
|
|
|
415
411
|
try {
|
|
416
412
|
this._legacyCallback?.(events);
|
|
417
413
|
} catch (e) {
|
|
418
|
-
|
|
414
|
+
logError('[NitroSse] Error in legacy onEvent callback:', e);
|
|
419
415
|
}
|
|
420
416
|
for (const event of events) {
|
|
421
417
|
if (!event || typeof event !== 'object') continue;
|
|
418
|
+
if (event.type === 'open') {
|
|
419
|
+
this._unhandledErrorReporter.reset();
|
|
420
|
+
} else if (event.type === 'error') {
|
|
421
|
+
const hasErrorHandler = Boolean(this._legacyCallback) || (this._listeners.get('error')?.size ?? 0) > 0;
|
|
422
|
+
if (!hasErrorHandler) {
|
|
423
|
+
this._unhandledErrorReporter.report('[NitroSse]', event);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
422
426
|
if (event.type) {
|
|
423
427
|
this._emit(event.type, event);
|
|
424
428
|
}
|
|
@@ -436,6 +440,10 @@ export class NitroSseClient {
|
|
|
436
440
|
}
|
|
437
441
|
start() {
|
|
438
442
|
this._checkDisposed();
|
|
443
|
+
if (this._isConfiguring) {
|
|
444
|
+
devWarn('[NitroSse] Ignoring start() called synchronously during setup() teardown.');
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
439
447
|
if (!this._config) {
|
|
440
448
|
throw new NitroSseStateError('[NitroSse] Cannot start SSE stream: client is not configured. Call setup(config) first.');
|
|
441
449
|
}
|
|
@@ -447,6 +455,10 @@ export class NitroSseClient {
|
|
|
447
455
|
}
|
|
448
456
|
restart() {
|
|
449
457
|
this._checkDisposed();
|
|
458
|
+
if (this._isConfiguring) {
|
|
459
|
+
devWarn('[NitroSse] Ignoring restart() called synchronously during setup() teardown.');
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
450
462
|
if (!this._config) {
|
|
451
463
|
throw new NitroSseStateError('[NitroSse] Cannot restart SSE stream: client is not configured. Call setup(config) first.');
|
|
452
464
|
}
|
|
@@ -543,7 +555,9 @@ export class NitroSseClient {
|
|
|
543
555
|
this._listeners.clear();
|
|
544
556
|
this._legacyCallback = undefined;
|
|
545
557
|
this._pendingHeaders = {};
|
|
546
|
-
this.
|
|
558
|
+
this._unhandledErrorReporter.reset();
|
|
559
|
+
this._warnedInsecureInterceptorUrl = null;
|
|
560
|
+
this._driver.dispose();
|
|
547
561
|
}
|
|
548
562
|
}
|
|
549
563
|
//# sourceMappingURL=NitroSseClient.js.map
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns true when running in a React Native development environment (`__DEV__ === true`).
|
|
5
|
+
*/
|
|
6
|
+
export function isDevEnvironment() {
|
|
7
|
+
return typeof __DEV__ !== 'undefined' && __DEV__ === true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Emits a warning to the DevTools console in development builds only.
|
|
12
|
+
*/
|
|
13
|
+
export function devWarn(message, ...args) {
|
|
14
|
+
if (!isDevEnvironment()) return;
|
|
15
|
+
console.warn(message, ...args);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Emits a debug message to the DevTools console in development builds only.
|
|
20
|
+
*/
|
|
21
|
+
export function devDebug(message, ...args) {
|
|
22
|
+
if (!isDevEnvironment()) return;
|
|
23
|
+
console.debug(message, ...args);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Emits an error to the DevTools console (used for user-code exceptions and invalid options).
|
|
28
|
+
*/
|
|
29
|
+
export function logError(message, ...args) {
|
|
30
|
+
console.error(message, ...args);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Reports unhandled native/driver SSE `error` events in `__DEV__` via `console.warn`
|
|
35
|
+
* (avoiding RedBox overlays for transient reconnects) while deduplicating consecutive
|
|
36
|
+
* identical error signatures to prevent reconnect-loop console spam.
|
|
37
|
+
*/
|
|
38
|
+
export class UnhandledSseErrorReporter {
|
|
39
|
+
_lastSignature = null;
|
|
40
|
+
report(scope, event) {
|
|
41
|
+
if (!isDevEnvironment()) return;
|
|
42
|
+
const detail = event.message ?? 'Unknown SSE error';
|
|
43
|
+
const statusPart = event.statusCode !== undefined ? ` (status: ${event.statusCode})` : '';
|
|
44
|
+
const signature = `${event.statusCode ?? ''}|${detail}`;
|
|
45
|
+
if (this._lastSignature === signature) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
this._lastSignature = signature;
|
|
49
|
+
const hint = scope === '[useNitroSse]' ? 'Provide an `onError` callback to handle stream errors.' : "Attach an `addEventListener('error', ...)` listener to handle stream errors.";
|
|
50
|
+
const hasData = event.data !== undefined && event.data.length > 0;
|
|
51
|
+
console.warn(`${scope} Unhandled SSE error: ${detail}${statusPart}. ${hint}`, {
|
|
52
|
+
type: event.type,
|
|
53
|
+
statusCode: event.statusCode,
|
|
54
|
+
message: event.message,
|
|
55
|
+
retry: event.retry,
|
|
56
|
+
hasData,
|
|
57
|
+
dataLength: event.data?.length ?? 0
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
reset() {
|
|
61
|
+
this._lastSignature = null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=SseConsoleLogger.js.map
|