uws-react-native 0.0.2 → 0.0.4

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
@@ -61,19 +61,15 @@ uws-react-native is still in heavy development and research. All the uWebSockets
61
61
 
62
62
  There are development and research things that you can read for your informations, especially if you are already familiar with uWebSockets. The most things are because of the React Native's architecture differentiation compared to the casual uWebSockets server program.
63
63
 
64
- ### Performance & Stability
65
-
66
- This library is indeed focusing on performance, but we will focus more of it later until we reach the stability of uws-react-native due to [threading](#threading) issue. It does not mean uws-react-native will performs badly in terms of performance speed, but it is not stable enough in under a stress situation.
67
-
68
- I have tested a stress test in an Android device. A lot of incoming request roughly 100 requests per second in roughly 10 seconds for files uploading (multipart/form-data) may crash the app. We do not have the proper benchmark yet, but it is as I expected from a platform restriction of memory usage for an app. An Android device with 12GB RAM does not mean an app can use all of it. I am keeping my eyes on this issue to keep improving this stress test.
69
-
70
64
  ### Threading
71
65
 
72
66
  We are embracing the main chaos of supporting uWebSockets in React Native architecture, which is to prevent UI/Main thread from blocking.
73
67
 
74
- Intentionally, we make the uWebSockets runs in another thread, therefore we have to make sure the communication safety between uWebSockets runner thread to the JS thread and vice versa. In theory, we can make uWebSockets runs in the main thread, but the app will be unusable, and then force closing the app is the only way to stop the server.
68
+ Intentionally, we make the uWebSockets will runs in another thread, therefore we have to make sure the communication safety between uWebSockets runner thread to the JS thread and vice versa. In theory, we can make uWebSockets runs in the main thread, but the app will be unusable, and then force closing the app is the only way to stop the server.
69
+
70
+ Yet, we only use one dedicated thread per uWebSockets run. I am in my own research to improve this by maximizing it to the hardware concurrency limit, but also to be careful about JavaScript runtime, because we have no control of it.
75
71
 
76
- We have another issue because of the uWebSockets runs in another thread. From the JSI C++ side, we have to assume any JS function as a callback especially the route method handler is asynchronous. We cannot make a sync call to the JS function from an arbitrary thread to the JS thread, and it makes JS call to the uWebSockets runner is also late.
72
+ We have another issue because of the uWebSockets runs in another thread. From the JSI C++ side, we have to assume any JavaScript functions as a callback especially the route method handler are asynchronous. We cannot make a sync call to the JavaScript function from an arbitrary thread to the JavaScript thread, and due to the asynchronous call, it makes the JavaScript call to the uWebSockets instances is late.
77
73
 
78
74
  There some topics you may to read regarding this threading research
79
75
 
@@ -68,12 +68,16 @@ facebook::jsi::Object UwsReactNativeModule::getParts(facebook::jsi::Runtime &rt,
68
68
  /// See /uws-react-native/package/src/modules/getParts.ts
69
69
 
70
70
  if(!body.isArrayBuffer(rt)) {
71
- /// undefined
72
- return facebook::jsi::Object(rt);
71
+ throw facebook::jsi::JSError(rt, "First argument of `getParts` has to be an ArrayBuffer");
73
72
  }
74
73
 
75
74
  auto arrayBuffer = body.getArrayBuffer(rt);
76
75
 
76
+ if(arrayBuffer.size(rt) == 0) {
77
+ // undefined
78
+ return facebook::jsi::Object(rt);
79
+ }
80
+
77
81
  auto mp = uWS::MultipartParser(contentType.utf8(rt));
78
82
  if(mp.isValid()) {
79
83
  mp.setBody(std::string_view(reinterpret_cast<char *>(arrayBuffer.data(rt)),
@@ -1,24 +1,18 @@
1
1
  #pragma once
2
2
 
3
- #include <ReactCommon/CallInvoker.h>
4
- #include <algorithm>
5
- #include <cmath>
6
3
  #include <jsi/jsi.h>
4
+ #include <memory>
7
5
  #include "RecognizedString.h"
8
6
  #include "uWebSockets/HttpContextData.h"
9
7
 
10
8
  namespace uws_react_native {
11
9
 
10
+ /// Only use this in JavaScript thread
12
11
  class HttpRequestObject : public facebook::jsi::Object {
13
12
 
14
13
  public:
15
14
  HttpRequestObject(facebook::jsi::Runtime &rt,
16
- uWS::HttpRequest *pReq) : facebook::jsi::Object(rt) {
17
-
18
- /// Without shared pointer
19
- /// uWS::HttpRequest returns weird behaviour for some methods like
20
- /// getHeader, getUrl, and getParameter
21
- auto req = std::make_shared<uWS::HttpRequest>(*pReq);
15
+ const std::shared_ptr<uWS::HttpRequest> &req) : facebook::jsi::Object(rt) {
22
16
 
23
17
  this->setProperty(rt,
24
18
  "forEach",
@@ -135,7 +129,7 @@ public:
135
129
  }
136
130
 
137
131
  return facebook::jsi::String::createFromAscii(rt_1,
138
- std::string(query));
132
+ std::string(query));
139
133
  }));
140
134
 
141
135
  this->setProperty(rt,
@@ -164,10 +158,8 @@ public:
164
158
  return {rt_1, thisValue};
165
159
  }));
166
160
 
167
- req.reset();
168
-
169
- } // HttpRequestObject
161
+ } // HttpResponseObject
170
162
 
171
- };
163
+ }; // HttpRequestObject
172
164
 
173
- }
165
+ } // namespace uws_react_native