Skip to content

Commit b79e488

Browse files
motiz88facebook-github-bot
authored andcommitted
Log a message identifying Fusebox based on a handshake with the frontend (#43565)
Summary: Pull Request resolved: #43565 Changelog: [Internal] Uses facebook/react-native-devtools-frontend#24 to identify the Fusebox frontend and show a corresponding message in the logs. Also tweaks the wording and formatting of other messages logged by the Fusebox backend - including removing the "you are using the modern CDP backend" one. Reviewed By: huntie Differential Revision: D55075645 fbshipit-source-id: c82670570c79b61efd399f26684139ce97f017ef
1 parent 059615f commit b79e488

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

packages/react-native/ReactCommon/jsinspector-modern/HostAgent.cpp

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,8 @@ namespace facebook::react::jsinspector_modern {
2222

2323
#define ANSI_WEIGHT_BOLD "\x1B[1m"
2424
#define ANSI_WEIGHT_RESET "\x1B[22m"
25-
#define ANSI_STYLE_ITALIC "\x1B[3m"
26-
#define ANSI_STYLE_RESET "\x1B[23m"
2725
#define ANSI_COLOR_BG_YELLOW "\x1B[48;2;253;247;231m"
28-
29-
static constexpr auto kModernCDPBackendNotice =
30-
ANSI_COLOR_BG_YELLOW ANSI_WEIGHT_BOLD
31-
"NOTE:" ANSI_WEIGHT_RESET " You are using the " ANSI_STYLE_ITALIC
32-
"modern" ANSI_STYLE_RESET " CDP backend for React Native (HostTarget)."sv;
26+
#define CSS_STYLE_PLACEHOLDER "%c"
3327

3428
HostAgent::HostAgent(
3529
FrontendChannel frontendChannel,
@@ -51,12 +45,15 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
5145
if (req.method == "Log.enable") {
5246
sessionState_.isLogDomainEnabled = true;
5347

54-
// Send a log entry identifying the modern CDP backend.
55-
sendInfoLogEntry(kModernCDPBackendNotice);
48+
if (sessionState_.isFuseboxClientDetected) {
49+
sendFuseboxNotice();
50+
}
5651

5752
// Send a log entry with the integration name.
5853
if (sessionMetadata_.integrationName) {
59-
sendInfoLogEntry("Integration: " + *sessionMetadata_.integrationName);
54+
sendInfoLogEntry(
55+
ANSI_COLOR_BG_YELLOW "Debugger integration: " +
56+
*sessionMetadata_.integrationName);
6057
}
6158

6259
shouldSendOKResponse = true;
@@ -100,6 +97,15 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
10097
: std::nullopt,
10198
});
10299

100+
shouldSendOKResponse = true;
101+
isFinishedHandlingRequest = true;
102+
} else if (req.method == "FuseboxClient.setClientMetadata") {
103+
sessionState_.isFuseboxClientDetected = true;
104+
105+
if (sessionState_.isLogDomainEnabled) {
106+
sendFuseboxNotice();
107+
}
108+
103109
shouldSendOKResponse = true;
104110
isFinishedHandlingRequest = true;
105111
}
@@ -120,7 +126,23 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
120126
req.method + " not implemented yet"));
121127
}
122128

123-
void HostAgent::sendInfoLogEntry(std::string_view text) {
129+
void HostAgent::sendFuseboxNotice() {
130+
static constexpr auto kFuseboxNotice = ANSI_COLOR_BG_YELLOW
131+
"Welcome to the new React Native debugger (codename " ANSI_WEIGHT_BOLD
132+
"React Fusebox " CSS_STYLE_PLACEHOLDER
133+
"⚡️" CSS_STYLE_PLACEHOLDER ANSI_WEIGHT_RESET ")."sv;
134+
135+
sendInfoLogEntry(
136+
kFuseboxNotice, {"font-family: sans-serif;", "font-family: monospace;"});
137+
}
138+
139+
void HostAgent::sendInfoLogEntry(
140+
std::string_view text,
141+
std::initializer_list<std::string_view> args) {
142+
folly::dynamic argsArray = folly::dynamic::array();
143+
for (auto arg : args) {
144+
argsArray.push_back(arg);
145+
}
124146
frontendChannel_(cdp::jsonNotification(
125147
"Log.entryAdded",
126148
folly::dynamic::object(
@@ -130,7 +152,7 @@ void HostAgent::sendInfoLogEntry(std::string_view text) {
130152
duration_cast<milliseconds>(
131153
system_clock::now().time_since_epoch())
132154
.count())("source", "other")(
133-
"level", "info")("text", text))));
155+
"level", "info")("text", text)("args", std::move(argsArray)))));
134156
}
135157

136158
void HostAgent::setCurrentInstanceAgent(

packages/react-native/ReactCommon/jsinspector-modern/HostAgent.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ class HostAgent final {
7171
* Runtime.consoleAPICalled is that the latter requires an execution context
7272
* ID, which does not exist at the Host level.
7373
*/
74-
void sendInfoLogEntry(std::string_view text);
74+
void sendInfoLogEntry(
75+
std::string_view text,
76+
std::initializer_list<std::string_view> args = {});
77+
78+
void sendFuseboxNotice();
7579

7680
FrontendChannel frontendChannel_;
7781
HostTargetController& targetController_;

packages/react-native/ReactCommon/jsinspector-modern/SessionState.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct SessionState {
3636
std::unordered_map<std::string, ExecutionContextSelectorSet>
3737
subscribedBindings;
3838

39+
bool isFuseboxClientDetected{false};
40+
3941
/**
4042
* Stores the state object exported from the last main RuntimeAgent, if any,
4143
* before it was destroyed.

packages/react-native/ReactCommon/jsinspector-modern/tests/HostTargetTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ TEST_F(HostTargetProtocolTest, InjectLogsToIdentifyBackend) {
155155
onMessage(JsonParsed(AllOf(
156156
AtJsonPtr("/method", "Log.entryAdded"),
157157
AtJsonPtr("/params/entry", Not(IsEmpty()))))))
158-
.Times(2)
159-
.RetiresOnSaturation();
158+
.Times(AtLeast(1));
160159
EXPECT_CALL(fromPage(), onMessage(JsonEq(R"({
161160
"id": 1,
162161
"result": {}

packages/react-native/ReactCommon/jsinspector-modern/tests/JsiIntegrationTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,21 @@ TYPED_TEST(JsiIntegrationPortableTest, ExceptionDuringAddBindingIsIgnored) {
317317
EXPECT_TRUE(this->eval("globalThis.foo === 42").getBool());
318318
}
319319

320+
TYPED_TEST(JsiIntegrationPortableTest, FuseboxSetClientMetadata) {
321+
this->connect();
322+
323+
this->expectMessageFromPage(JsonEq(R"({
324+
"id": 1,
325+
"result": {}
326+
})"));
327+
328+
this->toPage_->sendMessage(R"({
329+
"id": 1,
330+
"method": "FuseboxClient.setClientMetadata",
331+
"params": {}
332+
})");
333+
}
334+
320335
#pragma endregion // AllEngines
321336
#pragma region AllHermesVariants
322337

0 commit comments

Comments
 (0)