react-native-nitro-sse 2.2.2 → 2.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/ios/NitroSse.swift +4 -2
- package/ios/NitroSseNetworkInspector.mm +6 -6
- package/lib/module/NitroSseClient.js +171 -2
- package/lib/module/NitroSseClient.js.map +1 -1
- package/lib/typescript/src/NitroSseClient.d.ts +5 -0
- package/lib/typescript/src/NitroSseClient.d.ts.map +1 -1
- package/lib/typescript/src/SseInterface.d.ts +61 -0
- package/lib/typescript/src/SseInterface.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_SseEvent_.hpp +14 -14
- package/nitrogen/generated/android/c++/JHybridNitroSseSpec.cpp +17 -5
- package/nitrogen/generated/android/c++/JSseConfig.hpp +18 -3
- package/nitrogen/generated/android/c++/JSseMockConfig.hpp +101 -0
- package/nitrogen/generated/android/c++/JSseMockEvent.hpp +94 -0
- package/nitrogen/generated/android/c++/JSseMockMode.hpp +58 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseConfig.kt +53 -5
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseEvent.kt +27 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockConfig.kt +71 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockEvent.kt +91 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseMockMode.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseStats.kt +19 -0
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +50 -0
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Umbrella.hpp +9 -0
- package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +12 -3
- package/nitrogen/generated/ios/swift/SseConfig.swift +12 -1
- package/nitrogen/generated/ios/swift/SseMockConfig.swift +94 -0
- package/nitrogen/generated/ios/swift/SseMockEvent.swift +179 -0
- package/nitrogen/generated/ios/swift/SseMockMode.swift +40 -0
- package/nitrogen/generated/shared/c++/SseConfig.hpp +9 -2
- package/nitrogen/generated/shared/c++/SseMockConfig.hpp +105 -0
- package/nitrogen/generated/shared/c++/SseMockEvent.hpp +119 -0
- package/nitrogen/generated/shared/c++/SseMockMode.hpp +76 -0
- package/package.json +4 -4
- package/src/NitroSseClient.ts +203 -2
- package/src/SseInterface.ts +64 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// SseMockEvent.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
11
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
16
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/JSIHelpers.hpp>)
|
|
21
|
+
#include <NitroModules/JSIHelpers.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
#if __has_include(<NitroModules/PropNameIDCache.hpp>)
|
|
26
|
+
#include <NitroModules/PropNameIDCache.hpp>
|
|
27
|
+
#else
|
|
28
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
// Forward declaration of `SseEventType` to properly resolve imports.
|
|
32
|
+
namespace margelo::nitro::nitrosse { enum class SseEventType; }
|
|
33
|
+
|
|
34
|
+
#include "SseEventType.hpp"
|
|
35
|
+
#include <optional>
|
|
36
|
+
#include <string>
|
|
37
|
+
#include <NitroModules/AnyMap.hpp>
|
|
38
|
+
|
|
39
|
+
namespace margelo::nitro::nitrosse {
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A struct which can be represented as a JavaScript object (SseMockEvent).
|
|
43
|
+
*/
|
|
44
|
+
struct SseMockEvent final {
|
|
45
|
+
public:
|
|
46
|
+
std::optional<SseEventType> type SWIFT_PRIVATE;
|
|
47
|
+
std::optional<std::string> data SWIFT_PRIVATE;
|
|
48
|
+
std::optional<std::shared_ptr<AnyMap>> parsedData SWIFT_PRIVATE;
|
|
49
|
+
std::optional<std::string> id SWIFT_PRIVATE;
|
|
50
|
+
std::optional<std::string> event SWIFT_PRIVATE;
|
|
51
|
+
std::optional<std::string> message SWIFT_PRIVATE;
|
|
52
|
+
std::optional<double> statusCode SWIFT_PRIVATE;
|
|
53
|
+
std::optional<double> retry SWIFT_PRIVATE;
|
|
54
|
+
std::optional<double> delayMs SWIFT_PRIVATE;
|
|
55
|
+
|
|
56
|
+
public:
|
|
57
|
+
SseMockEvent() = default;
|
|
58
|
+
explicit SseMockEvent(std::optional<SseEventType> type, std::optional<std::string> data, std::optional<std::shared_ptr<AnyMap>> parsedData, std::optional<std::string> id, std::optional<std::string> event, std::optional<std::string> message, std::optional<double> statusCode, std::optional<double> retry, std::optional<double> delayMs): type(type), data(data), parsedData(parsedData), id(id), event(event), message(message), statusCode(statusCode), retry(retry), delayMs(delayMs) {}
|
|
59
|
+
|
|
60
|
+
public:
|
|
61
|
+
friend bool operator==(const SseMockEvent& lhs, const SseMockEvent& rhs) = default;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
} // namespace margelo::nitro::nitrosse
|
|
65
|
+
|
|
66
|
+
namespace margelo::nitro {
|
|
67
|
+
|
|
68
|
+
// C++ SseMockEvent <> JS SseMockEvent (object)
|
|
69
|
+
template <>
|
|
70
|
+
struct JSIConverter<margelo::nitro::nitrosse::SseMockEvent> final {
|
|
71
|
+
static inline margelo::nitro::nitrosse::SseMockEvent fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
72
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
73
|
+
return margelo::nitro::nitrosse::SseMockEvent(
|
|
74
|
+
JSIConverter<std::optional<margelo::nitro::nitrosse::SseEventType>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "type"))),
|
|
75
|
+
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "data"))),
|
|
76
|
+
JSIConverter<std::optional<std::shared_ptr<AnyMap>>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "parsedData"))),
|
|
77
|
+
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "id"))),
|
|
78
|
+
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "event"))),
|
|
79
|
+
JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "message"))),
|
|
80
|
+
JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "statusCode"))),
|
|
81
|
+
JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "retry"))),
|
|
82
|
+
JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "delayMs")))
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::nitrosse::SseMockEvent& arg) {
|
|
86
|
+
jsi::Object obj(runtime);
|
|
87
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "type"), JSIConverter<std::optional<margelo::nitro::nitrosse::SseEventType>>::toJSI(runtime, arg.type));
|
|
88
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "data"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.data));
|
|
89
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "parsedData"), JSIConverter<std::optional<std::shared_ptr<AnyMap>>>::toJSI(runtime, arg.parsedData));
|
|
90
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "id"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.id));
|
|
91
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "event"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.event));
|
|
92
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "message"), JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.message));
|
|
93
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "statusCode"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.statusCode));
|
|
94
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "retry"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.retry));
|
|
95
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "delayMs"), JSIConverter<std::optional<double>>::toJSI(runtime, arg.delayMs));
|
|
96
|
+
return obj;
|
|
97
|
+
}
|
|
98
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
99
|
+
if (!value.isObject()) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
jsi::Object obj = value.getObject(runtime);
|
|
103
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
if (!JSIConverter<std::optional<margelo::nitro::nitrosse::SseEventType>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "type")))) return false;
|
|
107
|
+
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "data")))) return false;
|
|
108
|
+
if (!JSIConverter<std::optional<std::shared_ptr<AnyMap>>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "parsedData")))) return false;
|
|
109
|
+
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "id")))) return false;
|
|
110
|
+
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "event")))) return false;
|
|
111
|
+
if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "message")))) return false;
|
|
112
|
+
if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "statusCode")))) return false;
|
|
113
|
+
if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "retry")))) return false;
|
|
114
|
+
if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "delayMs")))) return false;
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
} // namespace margelo::nitro
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// SseMockMode.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/NitroHash.hpp>)
|
|
11
|
+
#include <NitroModules/NitroHash.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
16
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
21
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
namespace margelo::nitro::nitrosse {
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* An enum which can be represented as a JavaScript union (SseMockMode).
|
|
30
|
+
*/
|
|
31
|
+
enum class SseMockMode {
|
|
32
|
+
REPLACE SWIFT_NAME(replace) = 0,
|
|
33
|
+
INJECT SWIFT_NAME(inject) = 1,
|
|
34
|
+
} CLOSED_ENUM;
|
|
35
|
+
|
|
36
|
+
} // namespace margelo::nitro::nitrosse
|
|
37
|
+
|
|
38
|
+
namespace margelo::nitro {
|
|
39
|
+
|
|
40
|
+
// C++ SseMockMode <> JS SseMockMode (union)
|
|
41
|
+
template <>
|
|
42
|
+
struct JSIConverter<margelo::nitro::nitrosse::SseMockMode> final {
|
|
43
|
+
static inline margelo::nitro::nitrosse::SseMockMode fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
44
|
+
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
|
|
45
|
+
switch (hashString(unionValue.c_str(), unionValue.size())) {
|
|
46
|
+
case hashString("replace"): return margelo::nitro::nitrosse::SseMockMode::REPLACE;
|
|
47
|
+
case hashString("inject"): return margelo::nitro::nitrosse::SseMockMode::INJECT;
|
|
48
|
+
default: [[unlikely]]
|
|
49
|
+
throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum SseMockMode - invalid value!");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::nitrosse::SseMockMode arg) {
|
|
53
|
+
switch (arg) {
|
|
54
|
+
case margelo::nitro::nitrosse::SseMockMode::REPLACE: return JSIConverter<std::string>::toJSI(runtime, "replace");
|
|
55
|
+
case margelo::nitro::nitrosse::SseMockMode::INJECT: return JSIConverter<std::string>::toJSI(runtime, "inject");
|
|
56
|
+
default: [[unlikely]]
|
|
57
|
+
throw std::invalid_argument("Cannot convert SseMockMode to JS - invalid value: "
|
|
58
|
+
+ std::to_string(static_cast<int>(arg)) + "!");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
62
|
+
if (!value.isString()) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
|
|
66
|
+
switch (hashString(unionValue.c_str(), unionValue.size())) {
|
|
67
|
+
case hashString("replace"):
|
|
68
|
+
case hashString("inject"):
|
|
69
|
+
return true;
|
|
70
|
+
default:
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
} // namespace margelo::nitro
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-sse",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "High-performance Server-Sent Events (SSE) for React Native, powered by Nitro Modules.",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -89,12 +89,12 @@
|
|
|
89
89
|
"eslint-plugin-prettier": "^5.5.4",
|
|
90
90
|
"jest": "^29.7.0",
|
|
91
91
|
"lefthook": "^2.0.3",
|
|
92
|
-
"nitrogen": "
|
|
92
|
+
"nitrogen": "0.35.6",
|
|
93
93
|
"prettier": "^2.8.8",
|
|
94
94
|
"react": "19.2.0",
|
|
95
95
|
"react-native": "0.83.4",
|
|
96
96
|
"react-native-builder-bob": "^0.40.17",
|
|
97
|
-
"react-native-nitro-modules": "
|
|
97
|
+
"react-native-nitro-modules": "0.35.6",
|
|
98
98
|
"release-it": "^19.0.4",
|
|
99
99
|
"turbo": "^2.5.6",
|
|
100
100
|
"typescript": "^5.9.2"
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"peerDependencies": {
|
|
103
103
|
"react": "*",
|
|
104
104
|
"react-native": "*",
|
|
105
|
-
"react-native-nitro-modules": ">= 0.35.
|
|
105
|
+
"react-native-nitro-modules": ">= 0.35.6"
|
|
106
106
|
},
|
|
107
107
|
"workspaces": [
|
|
108
108
|
"example"
|
package/src/NitroSseClient.ts
CHANGED
|
@@ -7,20 +7,49 @@ import type {
|
|
|
7
7
|
SseStats,
|
|
8
8
|
} from './SseInterface';
|
|
9
9
|
|
|
10
|
+
declare const __DEV__: boolean | undefined;
|
|
11
|
+
|
|
10
12
|
export class NitroSseClient implements SseClient {
|
|
11
13
|
private _native: NitroSse;
|
|
12
14
|
private _listeners: Map<string, Set<SseListener>> = new Map();
|
|
13
15
|
private _legacyCallback?: (events: SseEvent[]) => void;
|
|
16
|
+
private _config?: SseConfig;
|
|
17
|
+
private _mockIntervalId?: any;
|
|
18
|
+
private _mockIndex: number = 0;
|
|
14
19
|
|
|
15
20
|
constructor(native: NitroSse) {
|
|
16
21
|
this._native = native;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
setup(config: SseConfig, onEvent?: (events: SseEvent[]) => void): void {
|
|
25
|
+
this._config = { ...config };
|
|
20
26
|
this._legacyCallback = onEvent;
|
|
21
27
|
|
|
28
|
+
const isDev = typeof __DEV__ !== 'undefined' ? __DEV__ === true : false;
|
|
29
|
+
if (!isDev) {
|
|
30
|
+
delete this._config.mock;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (this._config.mock) {
|
|
34
|
+
console.warn(
|
|
35
|
+
'\n' +
|
|
36
|
+
'=================================================================\n' +
|
|
37
|
+
'⚠️ [react-native-nitro-sse] WARNING: MOCK STREAMING IS ENABLED! ⚠️\n' +
|
|
38
|
+
` Mode: ${this._config.mock.mode.toUpperCase()} | Speed: ${
|
|
39
|
+
this._config.mock.eventsPerSecond ?? 1
|
|
40
|
+
} events/sec\n` +
|
|
41
|
+
' Please ensure mocking is disabled before building for production!\n' +
|
|
42
|
+
'=================================================================\n'
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (this._mockIntervalId) {
|
|
47
|
+
clearTimeout(this._mockIntervalId);
|
|
48
|
+
this._mockIntervalId = undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
22
51
|
// We wrap the native setup to dispatch events to typed listeners
|
|
23
|
-
this._native.setup(
|
|
52
|
+
this._native.setup(this._config, (events) => {
|
|
24
53
|
// 1. Call legacy batch callback if provided
|
|
25
54
|
this._legacyCallback?.(events);
|
|
26
55
|
|
|
@@ -62,16 +91,170 @@ export class NitroSseClient implements SseClient {
|
|
|
62
91
|
}
|
|
63
92
|
}
|
|
64
93
|
|
|
94
|
+
private _createSseEvent(rawMock: any): SseEvent {
|
|
95
|
+
return {
|
|
96
|
+
type: rawMock.type ?? 'message',
|
|
97
|
+
data: rawMock.data,
|
|
98
|
+
parsedData: rawMock.parsedData,
|
|
99
|
+
id: rawMock.id,
|
|
100
|
+
event: rawMock.event,
|
|
101
|
+
message: rawMock.message,
|
|
102
|
+
statusCode: rawMock.statusCode ?? 200,
|
|
103
|
+
retry: rawMock.retry,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
65
107
|
start(): void {
|
|
108
|
+
if (this._mockIntervalId) {
|
|
109
|
+
clearTimeout(this._mockIntervalId);
|
|
110
|
+
this._mockIntervalId = undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (this._config?.mock) {
|
|
114
|
+
const {
|
|
115
|
+
mode,
|
|
116
|
+
data,
|
|
117
|
+
eventsPerSecond = 1,
|
|
118
|
+
loop = false,
|
|
119
|
+
errorRate = 0,
|
|
120
|
+
} = this._config.mock;
|
|
121
|
+
this._mockIndex = 0;
|
|
122
|
+
|
|
123
|
+
// Validate and normalize eventsPerSecond (must be a finite positive number, default to 1)
|
|
124
|
+
let validatedEventsPerSecond = Number(eventsPerSecond);
|
|
125
|
+
if (
|
|
126
|
+
Number.isNaN(validatedEventsPerSecond) ||
|
|
127
|
+
!Number.isFinite(validatedEventsPerSecond) ||
|
|
128
|
+
validatedEventsPerSecond <= 0
|
|
129
|
+
) {
|
|
130
|
+
validatedEventsPerSecond = 1;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Validate and normalize errorRate (must be a finite number within [0,1], default to 0)
|
|
134
|
+
let validatedErrorRate = Number(errorRate);
|
|
135
|
+
if (
|
|
136
|
+
Number.isNaN(validatedErrorRate) ||
|
|
137
|
+
!Number.isFinite(validatedErrorRate)
|
|
138
|
+
) {
|
|
139
|
+
validatedErrorRate = 0;
|
|
140
|
+
} else {
|
|
141
|
+
validatedErrorRate = Math.max(0, Math.min(1, validatedErrorRate));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const delayMs = 1000 / validatedEventsPerSecond;
|
|
145
|
+
// Safeguard: if eventsPerSecond is huge (e.g. 1000), setInterval/setTimeout is too slow.
|
|
146
|
+
// We batch events together in chunks if the calculated interval is under 10ms.
|
|
147
|
+
const batchSize = Math.max(1, Math.round(validatedEventsPerSecond / 100)); // chunk size for 10ms intervals
|
|
148
|
+
const intervalMs = Math.max(10, delayMs * batchSize);
|
|
149
|
+
|
|
150
|
+
const scheduleNext = () => {
|
|
151
|
+
if (this._mockIndex >= data.length) {
|
|
152
|
+
if (loop && data.length > 0) {
|
|
153
|
+
this._mockIndex = 0;
|
|
154
|
+
} else {
|
|
155
|
+
if (this._mockIntervalId) {
|
|
156
|
+
clearTimeout(this._mockIntervalId);
|
|
157
|
+
this._mockIntervalId = undefined;
|
|
158
|
+
}
|
|
159
|
+
const closeEvent: SseEvent = { type: 'close', statusCode: 200 };
|
|
160
|
+
this._legacyCallback?.([closeEvent]);
|
|
161
|
+
this._emit('close', closeEvent);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Simulate connection drops
|
|
167
|
+
if (validatedErrorRate && Math.random() < validatedErrorRate) {
|
|
168
|
+
const errorEvent: SseEvent = {
|
|
169
|
+
type: 'error',
|
|
170
|
+
message: 'Mock Connection Drop (Simulated Error)',
|
|
171
|
+
statusCode: 500,
|
|
172
|
+
};
|
|
173
|
+
this._legacyCallback?.([errorEvent]);
|
|
174
|
+
this._emit('error', errorEvent);
|
|
175
|
+
|
|
176
|
+
// Retry / reconnect delay simulator (e.g. 2000ms)
|
|
177
|
+
this._mockIntervalId = setTimeout(scheduleNext, 2000);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const currentItem = data[this._mockIndex];
|
|
182
|
+
const hasCustomDelay =
|
|
183
|
+
currentItem && typeof currentItem.delayMs === 'number';
|
|
184
|
+
|
|
185
|
+
const batch: SseEvent[] = [];
|
|
186
|
+
if (hasCustomDelay) {
|
|
187
|
+
const rawMock = data[this._mockIndex++];
|
|
188
|
+
if (rawMock) {
|
|
189
|
+
batch.push(this._createSseEvent(rawMock));
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
for (let i = 0; i < batchSize && this._mockIndex < data.length; i++) {
|
|
193
|
+
const nextItem = data[this._mockIndex];
|
|
194
|
+
if (nextItem && typeof nextItem.delayMs === 'number') {
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
const rawMock = data[this._mockIndex++];
|
|
198
|
+
if (rawMock) {
|
|
199
|
+
batch.push(this._createSseEvent(rawMock));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (batch.length > 0) {
|
|
205
|
+
// 1. Call legacy batch callback if provided
|
|
206
|
+
this._legacyCallback?.(batch);
|
|
207
|
+
|
|
208
|
+
// 2. Dispatch individual events to registered listeners
|
|
209
|
+
for (const event of batch) {
|
|
210
|
+
this._emit(event.type, event);
|
|
211
|
+
if (event.event && event.event !== event.type) {
|
|
212
|
+
this._emit(event.event, event);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const nextDelay = hasCustomDelay ? currentItem.delayMs! : intervalMs;
|
|
218
|
+
this._mockIntervalId = setTimeout(scheduleNext, nextDelay);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const firstItem = data[0];
|
|
222
|
+
const firstDelay =
|
|
223
|
+
firstItem && typeof firstItem.delayMs === 'number'
|
|
224
|
+
? firstItem.delayMs
|
|
225
|
+
: intervalMs;
|
|
226
|
+
|
|
227
|
+
if (mode === 'replace') {
|
|
228
|
+
// Emit simulated open event
|
|
229
|
+
const openEvent: SseEvent = { type: 'open', statusCode: 200 };
|
|
230
|
+
this._legacyCallback?.([openEvent]);
|
|
231
|
+
this._emit('open', openEvent);
|
|
232
|
+
|
|
233
|
+
this._mockIntervalId = setTimeout(scheduleNext, firstDelay);
|
|
234
|
+
return; // Skip native start
|
|
235
|
+
} else if (mode === 'inject') {
|
|
236
|
+
this._mockIntervalId = setTimeout(scheduleNext, firstDelay);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
66
240
|
this._native.start();
|
|
67
241
|
}
|
|
68
242
|
|
|
69
243
|
stop(): void {
|
|
244
|
+
if (this._mockIntervalId) {
|
|
245
|
+
clearTimeout(this._mockIntervalId);
|
|
246
|
+
this._mockIntervalId = undefined;
|
|
247
|
+
}
|
|
70
248
|
this._native.stop();
|
|
71
249
|
}
|
|
72
250
|
|
|
73
251
|
restart(): void {
|
|
74
|
-
this.
|
|
252
|
+
if (this._config?.mock) {
|
|
253
|
+
this.stop();
|
|
254
|
+
this.start();
|
|
255
|
+
} else {
|
|
256
|
+
this._native.restart();
|
|
257
|
+
}
|
|
75
258
|
}
|
|
76
259
|
|
|
77
260
|
flush(): void {
|
|
@@ -79,10 +262,19 @@ export class NitroSseClient implements SseClient {
|
|
|
79
262
|
}
|
|
80
263
|
|
|
81
264
|
isConnected(): boolean {
|
|
265
|
+
if (this._config?.mock?.mode === 'replace' && this._mockIntervalId) {
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
82
268
|
return this._native.isConnected();
|
|
83
269
|
}
|
|
84
270
|
|
|
85
271
|
getStats(): SseStats {
|
|
272
|
+
if (this._config?.mock?.mode === 'replace') {
|
|
273
|
+
return {
|
|
274
|
+
totalBytesReceived: this._mockIndex * 150,
|
|
275
|
+
reconnectCount: 0,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
86
278
|
return this._native.getStats();
|
|
87
279
|
}
|
|
88
280
|
|
|
@@ -93,4 +285,13 @@ export class NitroSseClient implements SseClient {
|
|
|
93
285
|
setLastProcessedId(id: string): void {
|
|
94
286
|
this._native.setLastProcessedId(id);
|
|
95
287
|
}
|
|
288
|
+
|
|
289
|
+
injectMockEvent(event: Partial<SseEvent>): void {
|
|
290
|
+
const sseEvent = this._createSseEvent(event);
|
|
291
|
+
this._legacyCallback?.([sseEvent]);
|
|
292
|
+
this._emit(sseEvent.type, sseEvent);
|
|
293
|
+
if (sseEvent.event && sseEvent.event !== sseEvent.type) {
|
|
294
|
+
this._emit(sseEvent.event, sseEvent);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
96
297
|
}
|
package/src/SseInterface.ts
CHANGED
|
@@ -100,6 +100,68 @@ export interface SseConfig {
|
|
|
100
100
|
* Note: This is protected by a native timeout to prevent the app from hanging.
|
|
101
101
|
*/
|
|
102
102
|
onBeforeRequest?: () => Promise<Record<string, string>>;
|
|
103
|
+
/**
|
|
104
|
+
* Configuration for mock streaming data.
|
|
105
|
+
* Used for local testing and debugging.
|
|
106
|
+
*/
|
|
107
|
+
mock?: SseMockConfig;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type SseMockMode = 'replace' | 'inject';
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Represents a single mock SSE event configuration.
|
|
114
|
+
*/
|
|
115
|
+
export interface SseMockEvent {
|
|
116
|
+
/** The type of the event. */
|
|
117
|
+
type?: SseEventType;
|
|
118
|
+
/** The data payload of the event as a raw string. */
|
|
119
|
+
data?: string;
|
|
120
|
+
/** The parsed JSON data, if autoParseJSON is enabled and parsing succeeds. */
|
|
121
|
+
parsedData?: AnyMap;
|
|
122
|
+
/** The event ID, if provided. */
|
|
123
|
+
id?: string;
|
|
124
|
+
/** The event name, if provided (internal 'event' field in SSE). */
|
|
125
|
+
event?: string;
|
|
126
|
+
/** System message or error description. */
|
|
127
|
+
message?: string;
|
|
128
|
+
/** HTTP status code if applicable. */
|
|
129
|
+
statusCode?: number;
|
|
130
|
+
/** Server-requested retry delay in milliseconds. */
|
|
131
|
+
retry?: number;
|
|
132
|
+
/** Custom delay in milliseconds before this event is dispatched. */
|
|
133
|
+
delayMs?: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Configuration for mock streaming data.
|
|
138
|
+
*/
|
|
139
|
+
export interface SseMockConfig {
|
|
140
|
+
/**
|
|
141
|
+
* - 'replace': Simulates the stream completely in JavaScript without any native network connection.
|
|
142
|
+
* - 'inject': Establishes the real server connection and injects mock events alongside it.
|
|
143
|
+
*/
|
|
144
|
+
mode: SseMockMode;
|
|
145
|
+
/**
|
|
146
|
+
* The list of mock events to be streamed.
|
|
147
|
+
*/
|
|
148
|
+
data: SseMockEvent[];
|
|
149
|
+
/**
|
|
150
|
+
* The frequency at which mock events are dispatched.
|
|
151
|
+
* Represented in events per second.
|
|
152
|
+
* @default 1
|
|
153
|
+
*/
|
|
154
|
+
eventsPerSecond?: number;
|
|
155
|
+
/**
|
|
156
|
+
* Whether to loop the mock stream indefinitely.
|
|
157
|
+
* @default false
|
|
158
|
+
*/
|
|
159
|
+
loop?: boolean;
|
|
160
|
+
/**
|
|
161
|
+
* Probability of simulated connection drop/error events (0.0 to 1.0).
|
|
162
|
+
* @default 0
|
|
163
|
+
*/
|
|
164
|
+
errorRate?: number;
|
|
103
165
|
}
|
|
104
166
|
|
|
105
167
|
/**
|
|
@@ -181,4 +243,6 @@ export interface SseClient {
|
|
|
181
243
|
updateHeaders(headers: Record<string, string>): void;
|
|
182
244
|
/** Set last event ID. */
|
|
183
245
|
setLastProcessedId(id: string): void;
|
|
246
|
+
/** Manually inject a mock event into the stream (for testing/debugging). */
|
|
247
|
+
injectMockEvent(event: Partial<SseEvent>): void;
|
|
184
248
|
}
|