Skip to content
Open
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
16 changes: 11 additions & 5 deletions public/connect-web-interceptor.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
/**
* In Connect v2, the `toJson` method is no longer available, and we can directly use the message itself.
*/
const toJson = (obj) => {
return obj.toJson?.() || obj;
}
/**
* Reads the message from the stream and posts it to the window.
* This is a generator function that will be passed to the response stream.
*/
async function* readMessage(req, stream) {
for await (const m of stream) {
if (m) {
const resp = m.toJson?.();
const resp = toJson(m);
window.postMessage({
type: "__GRPCWEB_DEVTOOLS__",
methodType: "server_streaming",
method: req.method.name,
request: req.message.toJson?.(),
request: toJson(req.message),
response: resp,
}, "*");
}
Expand All @@ -31,8 +37,8 @@ const interceptor = (next) => async (req) => {
type: "__GRPCWEB_DEVTOOLS__",
methodType: "unary",
method: req.method.name,
request: req.message.toJson(),
response: resp.message.toJson(),
request: toJson(req.message),
response: toJson(resp.message),
}, "*")
return resp;
} else {
Expand All @@ -46,7 +52,7 @@ const interceptor = (next) => async (req) => {
type: "__GRPCWEB_DEVTOOLS__",
methodType: req.stream ? "server_streaming" : "unary",
method: req.method.name,
request: req.message.toJson?.(),
request: toJson(req.message),
response: undefined,
error: {
message: e.message,
Expand Down