Skip to content
Merged
Show file tree
Hide file tree
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
41 changes: 41 additions & 0 deletions packages/client/src/rpc/__tests__/createClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from 'vitest';
import { anyObject } from 'vitest-mock-extended';
import {
createSignalClient,
withHeaders,
Expand Down Expand Up @@ -86,4 +87,44 @@ describe('createClient', () => {
{ param: 'value' },
]);
});

it('withRequestTracer should trace the response of SetPublisher', async () => {
const trace = vi.fn();
const interceptor = withRequestTracer(trace);
const { promise, resolve } = promiseWithResolvers<UnaryCall['then']>();
// @ts-expect-error - partial implementation
const next: NextUnaryFn = vi.fn(() => ({
then: (...args) => promise.then(...args),
}));
interceptor.interceptUnary(
next,
// @ts-expect-error - invalid name
{ name: 'SetPublisher' },
{ param: 'value' },
{ meta: {} },
);

// @ts-expect-error - partial data
resolve({ response: { data: 'response data' } });

interceptor.interceptUnary(
next,
// @ts-expect-error - invalid name
{ name: 'UpdateMuteStates' },
{ data: 'data' },
{ meta: {} },
);
await promise;

expect(next).toHaveBeenCalled();
expect(trace).toHaveBeenCalledWith('SetPublisher', { param: 'value' });
expect(trace).toHaveBeenCalledWith('UpdateMuteStates', { data: 'data' });
expect(trace).toHaveBeenCalledWith('SetPublisherResponse', {
data: 'response data',
});
expect(trace).not.toHaveBeenCalledWith(
'UpdateMuteStatesResponse',
anyObject(),
);
});
});
23 changes: 10 additions & 13 deletions packages/client/src/rpc/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,32 @@ export const withRequestLogger = (
};

export const withRequestTracer = (trace: Trace): RpcInterceptor => {
type RpcMethodNames = {
[K in keyof SignalServerClient as Capitalize<K>]: boolean;
};
type RpcMethodName = Capitalize<keyof SignalServerClient>;
const exclusions = new Set<RpcMethodName>(['SendStats']);
const responseInclusions = new Set<RpcMethodName>(['SetPublisher']);

const traceError = (name: string, input: object, err: unknown) =>
trace(`${name}OnFailure`, [err, input]);

const exclusions: Record<string, boolean | undefined> = {
SendStats: true,
} satisfies Partial<RpcMethodNames>;
return {
interceptUnary(
next: NextUnaryFn,
method: MethodInfo,
input: object,
options: RpcOptions,
): UnaryCall {
if (exclusions[method.name as keyof RpcMethodNames]) {
return next(method, input, options);
}
const name = method.name as RpcMethodName;
if (exclusions.has(name)) return next(method, input, options);

trace(method.name, input);
trace(name, input);
const unaryCall = next(method, input, options);
unaryCall.then(
(invocation) => {
const err = (invocation.response as SfuResponseWithError)?.error;
if (err) traceError(method.name, input, err);
const response = invocation.response as SfuResponseWithError;
if (response.error) traceError(name, input, response.error);
if (responseInclusions.has(name)) trace(`${name}Response`, response);
},
(err) => traceError(method.name, input, err),
(error) => traceError(name, input, error),
);
return unaryCall;
},
Expand Down
Loading