-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helpers for formatting JSON CDP responses (#43340)
Summary: Pull Request resolved: #43340 Adds convenience methods `jsonResult`, `jsonError` and `jsonNotification` for more ergonomic construction of CDP JSON responses. Note that CDP is *loosely* based on [JSON-RPC 2.0](https://www.jsonrpc.org/specification), but differs for example in the omission of `"jsonrpc": "2.0"`. Before: ``` frontendChannel_(folly::toJson(folly::dynamic::object("id", req.id)( "error", folly::dynamic::object("code", -32602)( "message", "executionContextName is mutually exclusive with executionContextId")))); ``` After: ``` frontendChannel_(cdp::jsonError( req.id, cdp::ErrorCode::InvalidParams, "executionContextName is mutually exclusive with executionContextId")); ``` Changelog: [Internal] Reviewed By: motiz88 Differential Revision: D54202854 fbshipit-source-id: 76a407ae39ff9c2ec79bcaddb6cd4d494afb7693
- Loading branch information
1 parent
9426d24
commit 3ed0ff3
Showing
13 changed files
with
241 additions
and
178 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
packages/react-native/ReactCommon/jsinspector-modern/CdpJson.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include "CdpJson.h" | ||
|
||
#include <folly/dynamic.h> | ||
#include <folly/json.h> | ||
|
||
namespace facebook::react::jsinspector_modern::cdp { | ||
|
||
PreparsedRequest preparse(std::string_view message) { | ||
folly::dynamic parsed = folly::parseJson(message); | ||
return PreparsedRequest{ | ||
.id = parsed["id"].getInt(), | ||
.method = parsed["method"].getString(), | ||
.params = parsed.count("params") != 0u ? parsed["params"] : nullptr}; | ||
} | ||
|
||
std::string PreparsedRequest::toJson() const { | ||
folly::dynamic obj = folly::dynamic::object; | ||
obj["id"] = id; | ||
obj["method"] = method; | ||
if (params != nullptr) { | ||
obj["params"] = params; | ||
} | ||
return folly::toJson(obj); | ||
} | ||
|
||
std::string jsonError( | ||
std::optional<RequestId> id, | ||
ErrorCode code, | ||
std::optional<std::string> message) { | ||
auto dynamicError = folly::dynamic::object("code", static_cast<int>(code)); | ||
if (message) { | ||
dynamicError("message", *message); | ||
} | ||
return folly::toJson( | ||
(id ? folly::dynamic::object("id", *id) | ||
: folly::dynamic::object( | ||
"id", nullptr))("error", std::move(dynamicError))); | ||
} | ||
|
||
std::string jsonResult(RequestId id, const folly::dynamic& result) { | ||
return folly::toJson(folly::dynamic::object("id", id)("result", result)); | ||
} | ||
|
||
std::string jsonNotification( | ||
std::string_view method, | ||
std::optional<folly::dynamic> params) { | ||
auto dynamicNotification = folly::dynamic::object("method", method); | ||
if (params) { | ||
dynamicNotification("params", *params); | ||
} | ||
return folly::toJson(std::move(dynamicNotification)); | ||
} | ||
|
||
} // namespace facebook::react::jsinspector_modern::cdp |
124 changes: 124 additions & 0 deletions
124
packages/react-native/ReactCommon/jsinspector-modern/CdpJson.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <folly/dynamic.h> | ||
#include <folly/json.h> | ||
#include <string> | ||
#include <string_view> | ||
|
||
namespace facebook::react::jsinspector_modern::cdp { | ||
|
||
using RequestId = long long; | ||
|
||
/** | ||
* Error codes to be used in CDP responses. | ||
* https://www.jsonrpc.org/specification#error_object | ||
*/ | ||
enum class ErrorCode { | ||
ParseError = -32700, | ||
InvalidRequest = -32600, | ||
MethodNotFound = -32601, | ||
InvalidParams = -32602, | ||
InternalError = -32603 | ||
/* -32000 to -32099: Implementation-defined server errors. */ | ||
}; | ||
|
||
/** | ||
* An incoming CDP request that has been parsed into a more usable form. | ||
*/ | ||
struct PreparsedRequest { | ||
public: | ||
/** | ||
* The ID of the request. | ||
*/ | ||
RequestId id{}; | ||
|
||
/** | ||
* The name of the method being invoked. | ||
*/ | ||
std::string method; | ||
|
||
/** | ||
* The parameters passed to the method, if any. | ||
*/ | ||
folly::dynamic params; | ||
|
||
/** | ||
* Equality operator, useful for unit tests | ||
*/ | ||
inline bool operator==(const PreparsedRequest& rhs) const { | ||
return id == rhs.id && method == rhs.method && params == rhs.params; | ||
} | ||
|
||
std::string toJson() const; | ||
}; | ||
|
||
/** | ||
* Parse a JSON-encoded CDP request into its constituent parts. | ||
* \throws ParseError If the input cannot be parsed. | ||
* \throws TypeError If the input does not conform to the expected format. | ||
*/ | ||
PreparsedRequest preparse(std::string_view message); | ||
|
||
/** | ||
* A type error that may be thrown while preparsing a request, or while | ||
* accessing dynamic params on a request. | ||
*/ | ||
using TypeError = folly::TypeError; | ||
|
||
/** | ||
* A parse error that may be thrown while preparsing a request. | ||
*/ | ||
using ParseError = folly::json::parse_error; | ||
|
||
/** | ||
* Helper functions for creating CDP (loosely JSON-RPC) messages of various | ||
* types, returning a JSON string ready for sending over the wire. | ||
*/ | ||
|
||
/** | ||
* Returns a JSON-formatted string representing an error. | ||
* | ||
* {"id": <id>, "error": { "code": <code>, "message": <message> }} | ||
* | ||
* \param id Request ID. Mandatory, null only if the request omitted it or | ||
* could not be parsed. | ||
* \param code Integer code from cdp::ErrorCode. | ||
* \param message Optional, brief human-readable error message. | ||
*/ | ||
std::string jsonError( | ||
std::optional<RequestId> id, | ||
ErrorCode code, | ||
std::optional<std::string> message = std::nullopt); | ||
|
||
/** | ||
* Returns a JSON-formatted string representing a successful response. | ||
* | ||
* {"id": <id>, "result": <result>} | ||
* | ||
* \param id The id of the request that this response corresponds to. | ||
* \param result Result payload, defaulting to {}. | ||
*/ | ||
std::string jsonResult( | ||
RequestId id, | ||
const folly::dynamic& result = folly::dynamic::object()); | ||
|
||
/** | ||
* Returns a JSON-formatted string representing a unilateral notifcation. | ||
* | ||
* {"method": <method>, "params": <params>} | ||
* | ||
* \param method Notification (aka "event") method. | ||
* \param params Optional payload pbject. | ||
*/ | ||
std::string jsonNotification( | ||
std::string_view method, | ||
std::optional<folly::dynamic> params = std::nullopt); | ||
|
||
} // namespace facebook::react::jsinspector_modern::cdp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.