From 86f0caae389c3264b81dee4ea699b4c56853f883 Mon Sep 17 00:00:00 2001 From: Sebastian Halder Date: Fri, 13 Oct 2023 05:29:19 +0000 Subject: [PATCH] Support less prominent streaming methods: client-streaming and bidirectional streaming --- .../src/index.ts | 135 ++++++++++++------ 1 file changed, 89 insertions(+), 46 deletions(-) diff --git a/packages/nice-grpc-client-middleware-devtools/src/index.ts b/packages/nice-grpc-client-middleware-devtools/src/index.ts index 1814a8af..82f9bbff 100644 --- a/packages/nice-grpc-client-middleware-devtools/src/index.ts +++ b/packages/nice-grpc-client-middleware-devtools/src/index.ts @@ -1,4 +1,4 @@ -import { isAbortError } from 'abort-controller-x'; +import {isAbortError} from 'abort-controller-x'; import { CallOptions, ClientError, @@ -10,7 +10,7 @@ import { export type DevtoolsLoggingOptions = { /** * Skip logging abort errors. - * + * * By default, abort errors are logged. */ skipAbortErrorLogging?: boolean; @@ -19,7 +19,7 @@ export type DevtoolsLoggingOptions = { export const devtoolsUnaryLoggingMiddleware: ClientMiddleware = async function* devtoolsLoggingMiddleware( call: ClientMiddlewareCall, - options: CallOptions & Partial + options: CallOptions & Partial, ): AsyncGenerator { // skip streaming calls if (call.requestStream || call.responseStream) { @@ -27,7 +27,7 @@ export const devtoolsUnaryLoggingMiddleware: ClientMiddleware = async function* devtoolsLoggingMiddleware( call: ClientMiddlewareCall, - options: CallOptions & Partial + options: CallOptions & Partial, ): AsyncGenerator { // skip unary calls if (!call.responseStream && !call.requestStream) { @@ -112,38 +112,34 @@ export const devtoolsStreamLoggingMiddleware: ClientMiddleware = composeClientMiddleware( - devtoolsUnaryLoggingMiddleware, - devtoolsStreamLoggingMiddleware -); +export const devtoolsLoggingMiddleware: ClientMiddleware = + composeClientMiddleware( + devtoolsUnaryLoggingMiddleware, + devtoolsStreamLoggingMiddleware, + ); // check whether the given object has toObject() method and return the object // otherwise return the object itself @@ -213,4 +210,50 @@ function getAsObject(obj: any) { } // ts-proto return obj; -} \ No newline at end of file +} + +async function* emitRequestMessages( + iterable: AsyncIterable, + path: string, +): AsyncIterable { + for await (const request of iterable) { + logStreamingRequestMessage(request, path); + yield request; + } +} + +async function* emitResponseMessages( + iterable: AsyncIterable, + path: string, +): AsyncIterable { + for await (const reponse of iterable) { + logStreamingResponseMessage(reponse, path); + yield reponse; + } +} + +function logStreamingResponseMessage(response: T, path: string) { + const resObj = getAsObject(response); + window.postMessage( + { + method: path, + methodType: 'server_streaming', + response: resObj, + type: '__GRPCWEB_DEVTOOLS__', + }, + '*', + ); +} + +function logStreamingRequestMessage(request: T, path: string) { + const reqObj = getAsObject(request); + window.postMessage( + { + method: path, + methodType: 'server_streaming', + request: reqObj, + type: '__GRPCWEB_DEVTOOLS__', + }, + '*', + ); +}