first commit

This commit is contained in:
2026-03-10 16:18:05 +00:00
commit 11f9c069b5
31635 changed files with 3187747 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import "RCTInspectorPackagerConnection.h"
#if RCT_DEV || RCT_REMOTE_PROFILE
@interface RCTCxxInspectorPackagerConnection : NSObject <RCTInspectorPackagerConnectionProtocol>
@end
#endif

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <React/RCTInspectorPackagerConnection.h>
#if RCT_DEV || RCT_REMOTE_PROFILE
#import <React/RCTDefines.h>
#import <React/RCTInspector.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import <SocketRocket/SRWebSocket.h>
#import <jsinspector-modern/InspectorPackagerConnection.h>
#import <chrono>
#import <memory>
#import "RCTCxxInspectorPackagerConnection.h"
#import "RCTCxxInspectorPackagerConnectionDelegate.h"
#import "RCTCxxInspectorWebSocketAdapter.h"
#import "RCTInspectorUtils.h"
using namespace facebook::react::jsinspector_modern;
@interface RCTCxxInspectorPackagerConnection () {
std::unique_ptr<InspectorPackagerConnection> _cxxImpl;
}
@end
@implementation RCTCxxInspectorPackagerConnection
RCT_NOT_IMPLEMENTED(-(instancetype)init)
- (instancetype)initWithURL:(NSURL *)url
{
if (self = [super init]) {
auto metadata = [RCTInspectorUtils getHostMetadata];
_cxxImpl = std::make_unique<InspectorPackagerConnection>(
[url absoluteString].UTF8String,
metadata.deviceName.UTF8String,
[[NSBundle mainBundle] bundleIdentifier].UTF8String,
std::make_unique<RCTCxxInspectorPackagerConnectionDelegate>());
}
return self;
}
- (void)sendEventToAllConnections:(NSString *)event
{
_cxxImpl->sendEventToAllConnections(event.UTF8String);
}
- (bool)isConnected
{
return _cxxImpl->isConnected();
}
- (void)connect
{
_cxxImpl->connect();
}
- (void)closeQuietly
{
_cxxImpl->closeQuietly();
}
@end
#endif

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <React/RCTDefines.h>
#if RCT_DEV || RCT_REMOTE_PROFILE
#import "RCTCxxInspectorWebSocketAdapter.h"
#import <jsinspector-modern/InspectorPackagerConnection.h>
#import <chrono>
#import <memory>
#import <string>
namespace facebook::react::jsinspector_modern {
/**
* Glue between C++ and Objective-C for InspectorPackagerConnectionDelegate.
*/
class RCTCxxInspectorPackagerConnectionDelegate : public InspectorPackagerConnectionDelegate {
class WebSocket : public IWebSocket {
public:
WebSocket(RCTCxxInspectorWebSocketAdapter *adapter);
virtual void send(std::string_view message) override;
virtual ~WebSocket() override;
private:
RCTCxxInspectorWebSocketAdapter *const _adapter;
};
public:
virtual std::unique_ptr<IWebSocket> connectWebSocket(
const std::string &url,
std::weak_ptr<IWebSocketDelegate> delegate) override;
virtual void scheduleCallback(std::function<void(void)> callback, std::chrono::milliseconds delayMs) override;
};
} // namespace facebook::react::jsinspector_modern
#endif

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTCxxInspectorPackagerConnectionDelegate.h"
#if RCT_DEV || RCT_REMOTE_PROFILE
#import <dispatch/dispatch.h>
namespace facebook::react::jsinspector_modern {
RCTCxxInspectorPackagerConnectionDelegate::WebSocket::WebSocket(RCTCxxInspectorWebSocketAdapter *adapter)
: _adapter(adapter)
{
}
void RCTCxxInspectorPackagerConnectionDelegate::WebSocket::send(std::string_view message)
{
[_adapter send:message];
}
RCTCxxInspectorPackagerConnectionDelegate::WebSocket::~WebSocket()
{
[_adapter close];
}
std::unique_ptr<IWebSocket> RCTCxxInspectorPackagerConnectionDelegate::connectWebSocket(
const std::string &url,
std::weak_ptr<IWebSocketDelegate> delegate)
{
auto *adapter = [[RCTCxxInspectorWebSocketAdapter alloc] initWithURL:url delegate:delegate];
if (adapter == nullptr) {
return nullptr;
}
return std::make_unique<WebSocket>(adapter);
}
void RCTCxxInspectorPackagerConnectionDelegate::scheduleCallback(
std::function<void(void)> callback,
std::chrono::milliseconds delayMs)
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delayMs.count() * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
callback();
});
}
} // namespace facebook::react::jsinspector_modern
#endif

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <React/RCTDefines.h>
#if RCT_DEV || RCT_REMOTE_PROFILE
#import <jsinspector-modern/InspectorPackagerConnection.h>
#import <memory>
#import <string>
@interface RCTCxxInspectorWebSocketAdapter : NSObject
- (instancetype)initWithURL:(const std::string &)url
delegate:(std::weak_ptr<facebook::react::jsinspector_modern::IWebSocketDelegate>)delegate;
- (void)send:(std::string_view)message;
- (void)close;
@end
#endif

View File

@@ -0,0 +1,100 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTCxxInspectorWebSocketAdapter.h"
#if RCT_DEV || RCT_REMOTE_PROFILE
#import <React/RCTInspector.h>
#import <React/RCTInspectorPackagerConnection.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import <SocketRocket/SRWebSocket.h>
#import <jsinspector-modern/InspectorPackagerConnection.h>
#import <memory>
using namespace facebook::react::jsinspector_modern;
namespace {
NSString *NSStringFromUTF8StringView(std::string_view view)
{
return [[NSString alloc] initWithBytes:(const char *)view.data() length:view.size() encoding:NSUTF8StringEncoding];
}
} // namespace
@interface RCTCxxInspectorWebSocketAdapter () <SRWebSocketDelegate> {
std::weak_ptr<IWebSocketDelegate> _delegate;
SRWebSocket *_webSocket;
}
@end
@implementation RCTCxxInspectorWebSocketAdapter
- (instancetype)initWithURL:(const std::string &)url delegate:(std::weak_ptr<IWebSocketDelegate>)delegate
{
if ((self = [super init]) != nullptr) {
_delegate = delegate;
_webSocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:NSStringFromUTF8StringView(url)]];
_webSocket.delegate = self;
[_webSocket open];
}
return self;
}
- (void)send:(std::string_view)message
{
__weak RCTCxxInspectorWebSocketAdapter *weakSelf = self;
NSString *messageStr = NSStringFromUTF8StringView(message);
dispatch_async(dispatch_get_main_queue(), ^{
RCTCxxInspectorWebSocketAdapter *strongSelf = weakSelf;
if (strongSelf != nullptr) {
[strongSelf->_webSocket sendString:messageStr error:NULL];
}
});
}
- (void)close
{
[_webSocket closeWithCode:1000 reason:@"End of session"];
}
- (void)webSocketDidOpen:(__unused SRWebSocket *)webSocket
{
// NOTE: We are on the main queue here, per SRWebSocket's defaults.
if (auto delegate = _delegate.lock()) {
delegate->didOpen();
}
}
- (void)webSocket:(__unused SRWebSocket *)webSocket didFailWithError:(NSError *)error
{
// NOTE: We are on the main queue here, per SRWebSocket's defaults.
if (auto delegate = _delegate.lock()) {
delegate->didFailWithError([error code], [error description].UTF8String);
}
}
- (void)webSocket:(__unused SRWebSocket *)webSocket didReceiveMessageWithString:(NSString *)message
{
// NOTE: We are on the main queue here, per SRWebSocket's defaults.
if (auto delegate = _delegate.lock()) {
delegate->didReceiveMessage([message UTF8String]);
}
}
- (void)webSocket:(__unused SRWebSocket *)webSocket
didCloseWithCode:(__unused NSInteger)code
reason:(__unused NSString *)reason
wasClean:(__unused BOOL)wasClean
{
// NOTE: We are on the main queue here, per SRWebSocket's defaults.
if (auto delegate = _delegate.lock()) {
delegate->didClose();
}
}
@end
#endif

View File

@@ -0,0 +1,32 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#if RCT_DEV || RCT_REMOTE_PROFILE
@class RCTInspectorRemoteConnection;
@interface RCTInspectorLocalConnection : NSObject
- (void)sendMessage:(NSString *)message;
- (void)disconnect;
@end
@interface RCTInspectorPage : NSObject
@property (nonatomic, readonly) NSInteger id;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSString *vm;
@end
@interface RCTInspector : NSObject
+ (NSArray<RCTInspectorPage *> *)pages;
+ (RCTInspectorLocalConnection *)connectPage:(NSInteger)pageId
forRemoteConnection:(RCTInspectorRemoteConnection *)remote;
@end
#endif

View File

@@ -0,0 +1,130 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <React/RCTInspector.h>
#if RCT_DEV || RCT_REMOTE_PROFILE
#import <jsinspector-modern/InspectorInterfaces.h>
#import <React/RCTDefines.h>
#import <React/RCTInspectorPackagerConnection.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
using namespace facebook::react;
using namespace facebook::react::jsinspector_modern;
// This is a port of the Android impl, at
// react-native-github/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.java
// react-native-github/ReactAndroid/src/main/jni/react/jni/JInspector.cpp
// please keep consistent :)
class RemoteConnection : public IRemoteConnection {
public:
RemoteConnection(RCTInspectorRemoteConnection *connection) : _connection(connection) {}
virtual void onMessage(std::string message) override
{
[_connection onMessage:@(message.c_str())];
}
virtual void onDisconnect() override
{
[_connection onDisconnect];
}
private:
const RCTInspectorRemoteConnection *_connection;
};
@interface RCTInspectorPage () {
NSInteger _id;
NSString *_title;
NSString *_vm;
}
- (instancetype)initWithId:(NSInteger)id title:(NSString *)title vm:(NSString *)vm;
@end
@interface RCTInspectorLocalConnection () {
std::unique_ptr<ILocalConnection> _connection;
}
- (instancetype)initWithConnection:(std::unique_ptr<ILocalConnection>)connection;
@end
static IInspector *getInstance()
{
return &facebook::react::jsinspector_modern::getInspectorInstance();
}
@implementation RCTInspector
RCT_NOT_IMPLEMENTED(-(instancetype)init)
+ (NSArray<RCTInspectorPage *> *)pages
{
std::vector<InspectorPageDescription> pages = getInstance()->getPages();
NSMutableArray<RCTInspectorPage *> *array = [NSMutableArray arrayWithCapacity:pages.size()];
for (size_t i = 0; i < pages.size(); i++) {
RCTInspectorPage *pageWrapper = [[RCTInspectorPage alloc] initWithId:pages[i].id
title:@(pages[i].description.c_str())
vm:@(pages[i].vm.c_str())];
[array addObject:pageWrapper];
}
return array;
}
+ (RCTInspectorLocalConnection *)connectPage:(NSInteger)pageId
forRemoteConnection:(RCTInspectorRemoteConnection *)remote
{
auto localConnection = getInstance()->connect((int)pageId, std::make_unique<RemoteConnection>(remote));
return [[RCTInspectorLocalConnection alloc] initWithConnection:std::move(localConnection)];
}
@end
@implementation RCTInspectorPage
RCT_NOT_IMPLEMENTED(-(instancetype)init)
- (instancetype)initWithId:(NSInteger)id title:(NSString *)title vm:(NSString *)vm
{
if (self = [super init]) {
_id = id;
_title = title;
_vm = vm;
}
return self;
}
@end
@implementation RCTInspectorLocalConnection
RCT_NOT_IMPLEMENTED(-(instancetype)init)
- (instancetype)initWithConnection:(std::unique_ptr<ILocalConnection>)connection
{
if (self = [super init]) {
_connection = std::move(connection);
}
return self;
}
- (void)sendMessage:(NSString *)message
{
_connection->sendMessage([message UTF8String]);
}
- (void)disconnect
{
_connection->disconnect();
}
@end
#endif

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#if RCT_DEV || RCT_REMOTE_PROFILE
@protocol RCTInspectorPackagerConnectionProtocol <NSObject>
- (instancetype)initWithURL:(NSURL *)url;
- (bool)isConnected;
- (void)connect;
- (void)closeQuietly;
- (void)sendEventToAllConnections:(NSString *)event;
@end
@interface RCTInspectorPackagerConnection : NSObject <RCTInspectorPackagerConnectionProtocol>
@end
@interface RCTInspectorRemoteConnection : NSObject
- (void)onMessage:(NSString *)message;
- (void)onDisconnect;
@end
#endif