@onekeyfe/react-native-network-throttle 3.0.82-alpha.0 → 3.0.82

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 CHANGED
@@ -2,13 +2,8 @@
2
2
 
3
3
  React Native native network throttle for OneKey iOS and Android development settings.
4
4
 
5
- Current scope is RN HTTP(S) latency and upload/download throughput. It does not
6
- emulate offline mode, WebView traffic, or third-party native networking stacks.
7
-
8
- `bypassUrlOrigins` excludes exact HTTP(S) origins from all throttling. Origins
9
- are canonicalized with their effective port and registered additively for the
10
- lifetime of the native process. This allows independently initialized React
11
- Native runtimes to register local development servers without clearing each
12
- other's configuration.
5
+ Current scope is an RN HTTP response latency gate. It does not emulate download
6
+ throughput, upload throughput, offline mode, WebView traffic, or third-party
7
+ native networking stacks.
13
8
 
14
9
  This package only owns native request throttling. Product settings, persistence, and UI controls should remain in the host app.
@@ -4,7 +4,6 @@ import android.content.Context
4
4
  import android.util.Log
5
5
  import com.facebook.react.bridge.Arguments
6
6
  import com.facebook.react.bridge.ReadableMap
7
- import com.facebook.react.bridge.ReadableType
8
7
  import com.facebook.react.bridge.WritableMap
9
8
  import com.facebook.react.modules.network.OkHttpClientProvider
10
9
  import java.io.IOException
@@ -12,9 +11,6 @@ import java.io.InterruptedIOException
12
11
  import java.util.concurrent.TimeUnit
13
12
  import java.util.concurrent.atomic.AtomicBoolean
14
13
  import java.util.concurrent.atomic.AtomicLong
15
- import java.util.concurrent.atomic.AtomicReference
16
- import okhttp3.HttpUrl
17
- import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
18
14
  import okhttp3.Interceptor
19
15
  import okhttp3.MediaType
20
16
  import okhttp3.OkHttpClient
@@ -40,7 +36,6 @@ internal object NetworkThrottle {
40
36
  private val downloadBps = AtomicLong(DEFAULT_THROUGHPUT_BPS.toLong())
41
37
  private val uploadBps = AtomicLong(DEFAULT_THROUGHPUT_BPS.toLong())
42
38
  private val installed = AtomicBoolean(false)
43
- private val bypassUrlOrigins = AtomicReference<Set<String>>(emptySet())
44
39
 
45
40
  fun install(context: Context) {
46
41
  if (!installed.compareAndSet(false, true)) {
@@ -90,19 +85,6 @@ internal object NetworkThrottle {
90
85
  if (nextUploadBps <= 0) {
91
86
  nextUploadBps = DEFAULT_THROUGHPUT_BPS.toLong()
92
87
  }
93
- if (config.hasKey("bypassUrlOrigins") && !config.isNull("bypassUrlOrigins")) {
94
- val origins = config.getArray("bypassUrlOrigins")
95
- val normalizedOrigins = buildSet {
96
- if (origins != null) {
97
- for (index in 0 until origins.size()) {
98
- if (origins.getType(index) == ReadableType.String) {
99
- normalizeOrigin(origins.getString(index))?.let(::add)
100
- }
101
- }
102
- }
103
- }
104
- bypassUrlOrigins.updateAndGet { current -> current + normalizedOrigins }
105
- }
106
88
 
107
89
  enabled.set(nextEnabled)
108
90
  latencyNanos.set((nextLatencyMs * 1_000_000.0).toLong())
@@ -122,9 +104,6 @@ internal object NetworkThrottle {
122
104
  map.putDouble("latencyMs", latencyNanos.get() / 1_000_000.0)
123
105
  map.putDouble("downloadBps", downloadBps.get().toDouble())
124
106
  map.putDouble("uploadBps", uploadBps.get().toDouble())
125
- val origins = Arguments.createArray()
126
- bypassUrlOrigins.get().sorted().forEach(origins::pushString)
127
- map.putArray("bypassUrlOrigins", origins)
128
107
  return map
129
108
  }
130
109
 
@@ -132,21 +111,6 @@ internal object NetworkThrottle {
132
111
  private fun getDownloadBps(): Long = if (enabled.get()) downloadBps.get() else 0L
133
112
  private fun getUploadBps(): Long = if (enabled.get()) uploadBps.get() else 0L
134
113
 
135
- private fun canonicalOrigin(url: HttpUrl): String =
136
- HttpUrl.Builder()
137
- .scheme(url.scheme)
138
- .host(url.host)
139
- .port(url.port)
140
- .build()
141
- .toString()
142
- .removeSuffix("/")
143
-
144
- private fun normalizeOrigin(value: String?): String? =
145
- value?.toHttpUrlOrNull()?.let(::canonicalOrigin)
146
-
147
- private fun shouldBypass(requestUrl: HttpUrl): Boolean =
148
- bypassUrlOrigins.get().contains(canonicalOrigin(requestUrl))
149
-
150
114
  private fun sleepNanos(delayNanos: Long) {
151
115
  if (delayNanos <= 0) {
152
116
  return
@@ -237,12 +201,9 @@ internal object NetworkThrottle {
237
201
 
238
202
  private class ThrottleInterceptor : Interceptor {
239
203
  override fun intercept(chain: Interceptor.Chain): Response {
240
- val request = chain.request()
241
- if (shouldBypass(request.url)) {
242
- return chain.proceed(request)
243
- }
244
204
  val requestStartNanos = System.nanoTime()
245
205
  val delayNanos = getLatencyNanos()
206
+ val request = chain.request()
246
207
  val requestBody = request.body
247
208
  val activeUploadBps = getUploadBps()
248
209
  val throttledRequest =
@@ -10,32 +10,16 @@ static const NSTimeInterval OneKeyNetworkThrottleDefaultLatencyMs = 562.5;
10
10
  static const NSInteger OneKeyNetworkThrottleDefaultThroughputBps = 102 * 1024;
11
11
  static const NSUInteger OneKeyNetworkThrottleMaxPendingDownloadBytes = 256 * 1024;
12
12
 
13
- static NSString *OneKeyNetworkThrottleCanonicalOrigin(NSURL *url)
14
- {
15
- NSString *scheme = url.scheme.lowercaseString;
16
- NSString *host = url.host.lowercaseString;
17
- if ((!([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])) || host.length == 0) {
18
- return nil;
19
- }
20
- NSURLComponents *components = [[NSURLComponents alloc] init];
21
- components.scheme = scheme;
22
- components.host = host;
23
- components.port = url.port ?: @([scheme isEqualToString:@"https"] ? 443 : 80);
24
- return components.string;
25
- }
26
-
27
13
  @interface OneKeyNetworkThrottleState : NSObject
28
14
  + (NSDictionary *)currentConfig;
29
15
  + (BOOL)isEnabled;
30
16
  + (NSTimeInterval)latencyMs;
31
17
  + (NSInteger)downloadBps;
32
18
  + (NSInteger)uploadBps;
33
- + (BOOL)shouldBypassURL:(NSURL *)url;
34
19
  + (NSDictionary *)setEnabled:(BOOL)enabled
35
20
  latencyMs:(NSTimeInterval)latencyMs
36
21
  downloadBps:(NSInteger)downloadBps
37
- uploadBps:(NSInteger)uploadBps
38
- bypassUrlOrigins:(NSArray *)bypassUrlOrigins;
22
+ uploadBps:(NSInteger)uploadBps;
39
23
  @end
40
24
 
41
25
  @implementation OneKeyNetworkThrottleState
@@ -44,25 +28,18 @@ static atomic_bool _oneKeyNetworkThrottleEnabled = ATOMIC_VAR_INIT(false);
44
28
  static atomic_llong _oneKeyNetworkThrottleLatencyMicros = ATOMIC_VAR_INIT(562500);
45
29
  static atomic_llong _oneKeyNetworkThrottleDownloadBps = ATOMIC_VAR_INIT(102 * 1024);
46
30
  static atomic_llong _oneKeyNetworkThrottleUploadBps = ATOMIC_VAR_INIT(102 * 1024);
47
- static NSSet<NSString *> *_oneKeyNetworkThrottleBypassOrigins;
48
31
 
49
32
  + (NSDictionary *)currentConfig
50
33
  {
51
34
  BOOL enabled = atomic_load_explicit(&_oneKeyNetworkThrottleEnabled, memory_order_acquire);
52
35
  NSTimeInterval latencyMs =
53
36
  ((NSTimeInterval)atomic_load_explicit(&_oneKeyNetworkThrottleLatencyMicros, memory_order_relaxed)) / 1000.0;
54
- NSArray<NSString *> *bypassUrlOrigins = nil;
55
- @synchronized (self) {
56
- bypassUrlOrigins = [[_oneKeyNetworkThrottleBypassOrigins ?: [NSSet set] allObjects]
57
- sortedArrayUsingSelector:@selector(compare:)];
58
- }
59
37
  return @{
60
38
  @"enabled": @(enabled),
61
39
  @"profile": OneKeyNetworkThrottleProfileSlow4G,
62
40
  @"latencyMs": @(latencyMs),
63
41
  @"downloadBps": @([self downloadBps]),
64
- @"uploadBps": @([self uploadBps]),
65
- @"bypassUrlOrigins": bypassUrlOrigins
42
+ @"uploadBps": @([self uploadBps])
66
43
  };
67
44
  }
68
45
 
@@ -86,17 +63,6 @@ static NSSet<NSString *> *_oneKeyNetworkThrottleBypassOrigins;
86
63
  return (NSInteger)atomic_load_explicit(&_oneKeyNetworkThrottleUploadBps, memory_order_relaxed);
87
64
  }
88
65
 
89
- + (BOOL)shouldBypassURL:(NSURL *)url
90
- {
91
- NSString *origin = OneKeyNetworkThrottleCanonicalOrigin(url);
92
- if (origin == nil) {
93
- return NO;
94
- }
95
- @synchronized (self) {
96
- return [_oneKeyNetworkThrottleBypassOrigins containsObject:origin];
97
- }
98
- }
99
-
100
66
  + (NSInteger)normalizeThroughputBps:(NSInteger)throughputBps
101
67
  {
102
68
  return throughputBps > 0 ? throughputBps : OneKeyNetworkThrottleDefaultThroughputBps;
@@ -106,29 +72,10 @@ static NSSet<NSString *> *_oneKeyNetworkThrottleBypassOrigins;
106
72
  latencyMs:(NSTimeInterval)latencyMs
107
73
  downloadBps:(NSInteger)downloadBps
108
74
  uploadBps:(NSInteger)uploadBps
109
- bypassUrlOrigins:(NSArray *)bypassUrlOrigins
110
75
  {
111
76
  NSTimeInterval normalizedLatencyMs = latencyMs > 0 ? latencyMs : OneKeyNetworkThrottleDefaultLatencyMs;
112
77
  NSInteger normalizedDownloadBps = [self normalizeThroughputBps:downloadBps];
113
78
  NSInteger normalizedUploadBps = [self normalizeThroughputBps:uploadBps];
114
- if ([bypassUrlOrigins isKindOfClass:[NSArray class]]) {
115
- NSMutableSet<NSString *> *normalizedOrigins = [NSMutableSet set];
116
- for (id value in bypassUrlOrigins) {
117
- if (![value isKindOfClass:[NSString class]]) {
118
- continue;
119
- }
120
- NSString *origin = OneKeyNetworkThrottleCanonicalOrigin([NSURL URLWithString:(NSString *)value]);
121
- if (origin != nil) {
122
- [normalizedOrigins addObject:origin];
123
- }
124
- }
125
- @synchronized (self) {
126
- NSMutableSet<NSString *> *nextOrigins =
127
- [_oneKeyNetworkThrottleBypassOrigins mutableCopy] ?: [NSMutableSet set];
128
- [nextOrigins unionSet:normalizedOrigins];
129
- _oneKeyNetworkThrottleBypassOrigins = [nextOrigins copy];
130
- }
131
- }
132
79
  atomic_store_explicit(
133
80
  &_oneKeyNetworkThrottleLatencyMicros,
134
81
  (long long)llround(normalizedLatencyMs * 1000.0),
@@ -190,9 +137,6 @@ static NSSet<NSString *> *_oneKeyNetworkThrottleBypassOrigins;
190
137
  if ([NSURLProtocol propertyForKey:OneKeyNetworkThrottleHandledKey inRequest:request]) {
191
138
  return NO;
192
139
  }
193
- if ([OneKeyNetworkThrottleState shouldBypassURL:request.URL]) {
194
- return NO;
195
- }
196
140
  NSString *scheme = request.URL.scheme.lowercaseString;
197
141
  return [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"];
198
142
  }
@@ -573,14 +517,7 @@ RCT_REMAP_METHOD(setConfig, setConfig:(NSDictionary *)config resolver:(RCTPromis
573
517
  uploadBpsValue != nil && uploadBpsValue != [NSNull null]
574
518
  ? [uploadBpsValue integerValue]
575
519
  : [OneKeyNetworkThrottleState uploadBps];
576
- id bypassUrlOriginsValue = config[@"bypassUrlOrigins"];
577
- NSArray *bypassUrlOrigins = [bypassUrlOriginsValue isKindOfClass:[NSArray class]] ? bypassUrlOriginsValue : nil;
578
- resolve([OneKeyNetworkThrottleState
579
- setEnabled:enabled
580
- latencyMs:latencyMs
581
- downloadBps:downloadBps
582
- uploadBps:uploadBps
583
- bypassUrlOrigins:bypassUrlOrigins]);
520
+ resolve([OneKeyNetworkThrottleState setEnabled:enabled latencyMs:latencyMs downloadBps:downloadBps uploadBps:uploadBps]);
584
521
  }
585
522
 
586
523
  @end
@@ -10,25 +10,17 @@ const LINKING_ERROR = `The package '@onekeyfe/react-native-network-throttle' doe
10
10
  default: ''
11
11
  }) + '- rebuild the app after installing the package';
12
12
  const nativeModule = NativeModules.OneKeyNetworkThrottle;
13
- function normalizeNativeConfig(config) {
14
- return {
15
- ...config,
16
- bypassUrlOrigins: config.bypassUrlOrigins ?? []
17
- };
18
- }
19
13
  export const NetworkThrottle = nativeModule ? {
20
- getConfig: async () => normalizeNativeConfig(await nativeModule.getConfig()),
14
+ getConfig: () => nativeModule.getConfig(),
21
15
  setConfig: async config => {
22
- const currentConfig = normalizeNativeConfig(await nativeModule.getConfig());
23
- const nativeConfig = await nativeModule.setConfig({
16
+ const currentConfig = await nativeModule.getConfig();
17
+ return nativeModule.setConfig({
24
18
  enabled: config.enabled ?? currentConfig.enabled,
25
19
  profile: config.profile ?? currentConfig.profile,
26
20
  latencyMs: config.latencyMs ?? currentConfig.latencyMs,
27
21
  downloadBps: config.downloadBps ?? currentConfig.downloadBps,
28
- uploadBps: config.uploadBps ?? currentConfig.uploadBps,
29
- bypassUrlOrigins: config.bypassUrlOrigins ?? currentConfig.bypassUrlOrigins ?? []
22
+ uploadBps: config.uploadBps ?? currentConfig.uploadBps
30
23
  });
31
- return normalizeNativeConfig(nativeConfig);
32
24
  }
33
25
  } : new Proxy({}, {
34
26
  get() {
@@ -5,7 +5,6 @@ export type NetworkThrottleConfig = {
5
5
  latencyMs: number;
6
6
  downloadBps: number;
7
7
  uploadBps: number;
8
- bypassUrlOrigins: string[];
9
8
  };
10
9
  export declare const NETWORK_THROTTLE_SLOW_4G_LATENCY_MS = 562.5;
11
10
  export declare const NETWORK_THROTTLE_102_KIB_BPS: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-network-throttle",
3
- "version": "3.0.82-alpha.0",
3
+ "version": "3.0.82",
4
4
  "description": "react-native-network-throttle",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -142,6 +142,5 @@
142
142
  }
143
143
  }
144
144
  }
145
- },
146
- "stableVersion": "3.0.81"
145
+ }
147
146
  }
package/src/index.tsx CHANGED
@@ -8,7 +8,6 @@ export type NetworkThrottleConfig = {
8
8
  latencyMs: number;
9
9
  downloadBps: number;
10
10
  uploadBps: number;
11
- bypassUrlOrigins: string[];
12
11
  };
13
12
 
14
13
  export const NETWORK_THROTTLE_SLOW_4G_LATENCY_MS = 562.5;
@@ -17,18 +16,9 @@ export const NETWORK_THROTTLE_SLOW_4G_DOWNLOAD_BPS =
17
16
  NETWORK_THROTTLE_102_KIB_BPS;
18
17
  export const NETWORK_THROTTLE_SLOW_4G_UPLOAD_BPS = NETWORK_THROTTLE_102_KIB_BPS;
19
18
 
20
- type NativeNetworkThrottleConfig = Omit<
21
- NetworkThrottleConfig,
22
- 'bypassUrlOrigins'
23
- > & {
24
- bypassUrlOrigins?: string[];
25
- };
26
-
27
19
  type NativeNetworkThrottleModule = {
28
- getConfig: () => Promise<NativeNetworkThrottleConfig>;
29
- setConfig: (
30
- config: NativeNetworkThrottleConfig
31
- ) => Promise<NativeNetworkThrottleConfig>;
20
+ getConfig: () => Promise<NetworkThrottleConfig>;
21
+ setConfig: (config: NetworkThrottleConfig) => Promise<NetworkThrottleConfig>;
32
22
  };
33
23
 
34
24
  export type NetworkThrottleModule = {
@@ -47,33 +37,18 @@ const nativeModule = NativeModules.OneKeyNetworkThrottle as
47
37
  | NativeNetworkThrottleModule
48
38
  | undefined;
49
39
 
50
- function normalizeNativeConfig(
51
- config: NativeNetworkThrottleConfig
52
- ): NetworkThrottleConfig {
53
- return {
54
- ...config,
55
- bypassUrlOrigins: config.bypassUrlOrigins ?? [],
56
- };
57
- }
58
-
59
40
  export const NetworkThrottle: NetworkThrottleModule = nativeModule
60
41
  ? {
61
- getConfig: async () =>
62
- normalizeNativeConfig(await nativeModule.getConfig()),
42
+ getConfig: () => nativeModule.getConfig(),
63
43
  setConfig: async (config) => {
64
- const currentConfig = normalizeNativeConfig(
65
- await nativeModule.getConfig()
66
- );
67
- const nativeConfig = await nativeModule.setConfig({
44
+ const currentConfig = await nativeModule.getConfig();
45
+ return nativeModule.setConfig({
68
46
  enabled: config.enabled ?? currentConfig.enabled,
69
47
  profile: config.profile ?? currentConfig.profile,
70
48
  latencyMs: config.latencyMs ?? currentConfig.latencyMs,
71
49
  downloadBps: config.downloadBps ?? currentConfig.downloadBps,
72
50
  uploadBps: config.uploadBps ?? currentConfig.uploadBps,
73
- bypassUrlOrigins:
74
- config.bypassUrlOrigins ?? currentConfig.bypassUrlOrigins ?? [],
75
51
  });
76
- return normalizeNativeConfig(nativeConfig);
77
52
  },
78
53
  }
79
54
  : (new Proxy(