Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions examples/android-ios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
"@audira/carbon-react-native-elements": "catalog:",
"@carbon/icons": "catalog:",
"@examples/app": "workspace:*",
"expo": "57.0.15",
"expo-build-properties": "57.0.13",
"expo-font": "57.0.1",
"expo-image": "57.0.3",
"expo-splash-screen": "57.0.7",
"expo": "57.0.23",
"expo-build-properties": "57.0.20",
"expo-font": "57.0.4",
"expo-image": "57.0.5",
"expo-splash-screen": "57.0.9",
"expo-status-bar": "57.0.1",
"expo-system-ui": "57.0.2",
"expo-system-ui": "57.0.4",
"react": "catalog:",
"react-native": "0.86.2",
"react-native": "0.86.3",
"react-native-safe-area-context": "5.7.0",
"react-native-svg": "catalog:"
},
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type-check": "tsc --build"
},
"devDependencies": {
"@eslint/compat": "2.1.0",
"@eslint/compat": "2.1.1",
"@eslint/js": "10.0.1",
"@react-native-community/cli": "20.1.3",
"@react-native/eslint-config": "0.85.3",
Expand All @@ -20,19 +20,19 @@
"@types/node": "^24",
"@types/react": "19.2.14",
"@types/react-dom": "19.2.4",
"@types/react-native-web": "0.19.2",
"eslint": "10.9.0",
"@types/react-native-web": "0.19.3",
"eslint": "10.10.0",
"eslint-import-resolver-typescript": "4.4.5",
"eslint-plugin-import-x": "4.17.1",
"eslint-plugin-react": "7.37.5",
"eslint-plugin-react-hooks": "7.1.1",
"eslint-plugin-react-native": "^5.0.0",
"globals": "17.11.0",
"globals": "17.12.0",
"husky": "9.1.7",
"lint-staged": "17.3.0",
"package-builder": "workspace:*",
"typescript": "6.0.3",
"typescript-eslint": "8.67.0"
"typescript-eslint": "8.70.0"
},
"engines": {
"node": ">=24",
Expand Down
308 changes: 169 additions & 139 deletions package/cpp/app/HttpResponseObject.h

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

namespace uws_react_native {

class HttpResponseObjectProvider {
class HttpResponseObjectNativeState : public facebook::jsi::NativeState {

public:
HttpResponseObjectProvider(uWS::HttpResponse<false> *pRes) {
HttpResponseObjectNativeState(uWS::HttpResponse<false> *pRes) {
this->res = pRes;
}

uWS::HttpResponse<false> *res;

struct {
std::shared_ptr<facebook::react::AsyncCallback<facebook::jsi::Value>> callback = nullptr;

bool isAlreadyAborted = false;
} dataAbort;

struct {
/**
* These handlers are for
Expand Down Expand Up @@ -126,7 +126,7 @@ class HttpResponseObjectProvider {
}
}
#endif
}; // class HttpResponseObjectProvider

}; // class HttpResponseObjectNativeState

} // namespace uws_react_native
42 changes: 21 additions & 21 deletions package/cpp/app/TemplatedAppObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "HttpRequestObject.h"
#include "HttpResponseObject.h"
#include "HttpResponseObjectProvider.h"
#include "HttpResponseObjectNativeState.h"
#include "RecognizedString.h"
#include "WebSocketBehaviorImpl.h"
#include "uWebSockets/App.h"
Expand Down Expand Up @@ -91,20 +92,20 @@ class TemplatedAppObject : public facebook::jsi::Object {

// AppRunner thread
std::function<void (uWS::HttpResponse<false> *res, uWS::HttpRequest *req)> uwsRouteHandler = [&jsInvoker, disableBodyRead, maxBodySize, asyncCallback = facebook::react::AsyncCallback(rt, std::move(callback), jsInvoker)](uWS::HttpResponse<false> *res, uWS::HttpRequest *req) {
auto httpResponseObjectProvider = std::make_shared<HttpResponseObjectProvider>(res);
auto httpResponseObjectNativeState = std::make_shared<HttpResponseObjectNativeState>(res);

/// Intentionally I have to take another copy of uWS::HttpRequest here to be consumed from JS thread
/// I always get "bad_alloc" error if I didn't this
auto httpRequest = std::make_shared<uWS::HttpRequest>(*req);

asyncCallback.callWithPriority(facebook::react::SchedulerPriority::ImmediatePriority,
[httpResponseObjectProvider,
[httpResponseObjectNativeState,
httpRequest,
&jsInvoker](facebook::jsi::Runtime &rt_1, facebook::jsi::Function &cb) {
// React Native JS runtime
if(httpResponseObjectProvider && httpRequest) {
if(httpResponseObjectNativeState && httpRequest) {
cb.call(rt_1,
HttpResponseObject(rt_1, httpResponseObjectProvider, jsInvoker),
HttpResponseObject(rt_1, jsInvoker, httpResponseObjectNativeState),
HttpRequestObject(rt_1, httpRequest));
}
});
Expand All @@ -118,31 +119,31 @@ class TemplatedAppObject : public facebook::jsi::Object {
/// `Returning from a request handler without responding or attaching an onAborted handler is ill-use`
///
/// I thought `onAborted` is just a callback or event listener.
res->onAborted([httpResponseObjectProvider]() {
if(httpResponseObjectProvider->dataAbort.callback) {
httpResponseObjectProvider->dataAbort.callback->call(facebook::jsi::Value::undefined());
res->onAborted([httpResponseObjectNativeState]() {
if(httpResponseObjectNativeState->dataAbort.callback) {
httpResponseObjectNativeState->dataAbort.callback->call(facebook::jsi::Value::undefined());
}
});

/// Sadly, we can't do late assignment to the onDataV2 and onData.
/// uWebSockets will do nothing to our handler if we assign the lambda so late.
/// So we have to predefined onDataV2 handler here, and save the chunk.
if(!disableBodyRead) {
res->onDataV2([httpResponseObjectProvider, maxBodySize](auto chunk, auto maxRemainingBodyLength) {
if(httpResponseObjectProvider->dataBody.isStopCollecting) {
res->onDataV2([httpResponseObjectNativeState, maxBodySize](auto chunk, auto maxRemainingBodyLength) {
if(httpResponseObjectNativeState->dataBody.isStopCollecting) {
return;
}

if(maxBodySize > 0) {
auto chunkSize = chunk.size();
auto currentChunkSize = httpResponseObjectProvider->dataBody.buffer->size();
auto currentChunkSize = httpResponseObjectNativeState->dataBody.buffer->size();

/// First and possibly only chunk
if(currentChunkSize == 0 && chunkSize > maxBodySize) {
httpResponseObjectProvider->dataBody.isStopCollecting = true;
httpResponseObjectNativeState->dataBody.isStopCollecting = true;
/// set the first chunk
httpResponseObjectProvider->insertBodyBuffer(chunk, maxRemainingBodyLength);
httpResponseObjectProvider->invokeBodyCallback();
httpResponseObjectNativeState->insertBodyBuffer(chunk, maxRemainingBodyLength);
httpResponseObjectNativeState->invokeBodyCallback();
/// Don't worry,
/// JS call may late
/// it will invokes the handler once when user pass the handler.
Expand All @@ -153,17 +154,17 @@ class TemplatedAppObject : public facebook::jsi::Object {
if(currentChunkSize > 0 && chunkSize > maxBodySize - currentChunkSize) {
/// tell to JS that we already stop collecting chunk
/// and invoke the JSI onData / onDataText / onDataV2 / onFullData / onFullDataText handler immediately
httpResponseObjectProvider->dataBody.isStopCollecting = true;
httpResponseObjectProvider->invokeBodyCallback();
httpResponseObjectNativeState->dataBody.isStopCollecting = true;
httpResponseObjectNativeState->invokeBodyCallback();
/// Don't worry,
/// JS call may late
/// it will invokes the handler once when user pass the handler.
return;
}
}

httpResponseObjectProvider->insertBodyBuffer(chunk, maxRemainingBodyLength);
httpResponseObjectProvider->invokeBodyCallback();
httpResponseObjectNativeState->insertBodyBuffer(chunk, maxRemainingBodyLength);
httpResponseObjectNativeState->invokeBodyCallback();
});
}
};
Expand Down Expand Up @@ -263,11 +264,10 @@ class TemplatedAppObject : public facebook::jsi::Object {
auto callback = obj.asFunction(rt_1);

appRunner.app.filter([&jsInvoker, asyncCallback = facebook::react::AsyncCallback(rt_1, std::move(callback), jsInvoker)](auto *res, int count) {
auto httpResponseObjectProvider = std::make_shared<HttpResponseObjectProvider>(res);

asyncCallback.call([&jsInvoker, count, httpResponseObjectProvider](facebook::jsi::Runtime &rt_2, facebook::jsi::Function &cb) {
auto httpResponseObjectNativeState = std::make_shared<HttpResponseObjectNativeState>(res);
asyncCallback.call([&jsInvoker, count, httpResponseObjectNativeState](facebook::jsi::Runtime &rt_2, facebook::jsi::Function &cb) {
cb.call(rt_2,
HttpResponseObject(rt_2, httpResponseObjectProvider, jsInvoker),
HttpResponseObject(rt_2, jsInvoker, httpResponseObjectNativeState),
count);
});
});
Expand Down
15 changes: 8 additions & 7 deletions package/cpp/app/WebSocketBehaviorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "AppRunner.h"
#include "HttpRequestObject.h"
#include "HttpResponseObject.h"
#include "HttpResponseObjectNativeState.h"
#include "WebSocketUserDataStorage.h"
#include "WebSocketObject.h"
#include "jsi/Buffer.h"
Expand Down Expand Up @@ -279,30 +280,30 @@ struct WebSocketBehaviorImpl : uWS::App::WebSocketBehavior<WebSocketUserDataStor

this->upgrade = [fn = facebook::react::AsyncCallback(rt, std::move(obj).asFunction(rt), jsInvoker), &jsInvoker](auto *res, auto *req, us_socket_context_t *context) -> void {
// AppRunner thread
auto httpResponseObjectProvider = std::make_shared<HttpResponseObjectProvider>(res);
auto httpResponseNativeState = std::make_shared<HttpResponseObjectNativeState>(res);

/// Intentionally I have to take another copy of uWS::HttpRequest here to be consumed from JS thread
/// I always get "bad_alloc" error if I didn't this
auto httpRequest = std::make_shared<uWS::HttpRequest>(*req);

fn.callWithPriority(facebook::react::SchedulerPriority::ImmediatePriority, [httpResponseObjectProvider,
fn.callWithPriority(facebook::react::SchedulerPriority::ImmediatePriority, [httpResponseNativeState,
httpRequest,
&jsInvoker,
contextAddress = reinterpret_cast<uintptr_t>(context)](facebook::jsi::Runtime &rt_1, facebook::jsi::Function &cb) {
// React Native JS runtime
if(httpResponseObjectProvider && httpRequest) {
if(httpResponseNativeState && httpRequest) {
cb.call(rt_1,
HttpResponseObject(rt_1, httpResponseObjectProvider, jsInvoker),
HttpResponseObject(rt_1, jsInvoker, httpResponseNativeState),
HttpRequestObject(rt_1, httpRequest),
facebook::jsi::BigInt::fromUint64(rt_1, contextAddress));
}
});

/// Same like the .get, .post, .put, and others in the TemplatedAppObject.h
/// We have to predefined the res->onAborted here
res->onAborted([httpResponseObjectProvider]() -> void {
if(httpResponseObjectProvider->dataAbort.callback) {
httpResponseObjectProvider->dataAbort.callback->call(facebook::jsi::Value::undefined());
res->onAborted([httpResponseNativeState]() -> void {
if(httpResponseNativeState->dataAbort.callback) {
httpResponseNativeState->dataAbort.callback->call(facebook::jsi::Value::undefined());
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uws-react-native",
"version": "0.1.5",
"version": "0.2.0",
"description": "The port of uWebSockets server library for React Native",
"license": "Apache-2.0",
"author": "RakaDoank <rakaaudira@gmail.com> (https://github.com/RakaDoank)",
Expand Down
Loading