Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context values to clients #841

Merged
merged 4 commits into from
Oct 3, 2023
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
2 changes: 1 addition & 1 deletion packages/connect-node/src/node-universal-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export function universalRequestFromNodeRequest(
header: nodeHeaderToWebHeader(nodeRequest.headers),
body,
signal: abortController.signal,
values: contextValues,
contextValues: contextValues,
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/connect-web-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ it like a web server would usually do.

| code generator | bundle size | minified | compressed |
|----------------|-------------------:|-----------------------:|---------------------:|
| connect | 113,658 b | 49,964 b | 13,486 b |
| connect | 114,538 b | 50,308 b | 13,557 b |
| grpc-web | 414,071 b | 300,352 b | 53,255 b |
7 changes: 6 additions & 1 deletion packages/connect-web/src/connect-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import type {
Transport,
UnaryRequest,
UnaryResponse,
ContextValues,
} from "@connectrpc/connect";
import { appendHeaders } from "@connectrpc/connect";
import { appendHeaders, createContextValues } from "@connectrpc/connect";
import {
createClientMethodSerializers,
createEnvelopeReadableStream,
Expand Down Expand Up @@ -142,6 +143,7 @@ export function createConnectTransport(
timeoutMs: number | undefined,
header: HeadersInit | undefined,
message: PartialMessage<I>,
contextValues?: ContextValues,
): Promise<UnaryResponse<I, O>> {
const { serialize, parse } = createClientMethodSerializers(
method,
Expand Down Expand Up @@ -176,6 +178,7 @@ export function createConnectTransport(
timeoutMs,
header,
),
contextValues: contextValues ?? createContextValues(),
message,
},
next: async (req: UnaryRequest<I, O>): Promise<UnaryResponse<I, O>> => {
Expand Down Expand Up @@ -242,6 +245,7 @@ export function createConnectTransport(
timeoutMs: number | undefined,
header: HeadersInit | undefined,
input: AsyncIterable<PartialMessage<I>>,
contextValues?: ContextValues,
): Promise<StreamResponse<I, O>> {
const { serialize, parse } = createClientMethodSerializers(
method,
Expand Down Expand Up @@ -320,6 +324,7 @@ export function createConnectTransport(
timeoutMs,
header,
),
contextValues: contextValues ?? createContextValues(),
message: input,
},
next: async (req) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/connect-web/src/grpc-web-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import type {
Transport,
UnaryRequest,
UnaryResponse,
ContextValues,
} from "@connectrpc/connect";
import { createContextValues } from "@connectrpc/connect";
import {
createClientMethodSerializers,
createEnvelopeReadableStream,
Expand Down Expand Up @@ -137,6 +139,7 @@ export function createGrpcWebTransport(
timeoutMs: number | undefined,
header: Headers,
message: PartialMessage<I>,
contextValues?: ContextValues,
): Promise<UnaryResponse<I, O>> {
const { serialize, parse } = createClientMethodSerializers(
method,
Expand Down Expand Up @@ -166,6 +169,7 @@ export function createGrpcWebTransport(
mode: "cors",
},
header: requestHeader(useBinaryFormat, timeoutMs, header),
contextValues: contextValues ?? createContextValues(),
message,
},
next: async (req: UnaryRequest<I, O>): Promise<UnaryResponse<I, O>> => {
Expand Down Expand Up @@ -233,6 +237,7 @@ export function createGrpcWebTransport(
timeoutMs: number | undefined,
header: HeadersInit | undefined,
input: AsyncIterable<PartialMessage<I>>,
contextValues?: ContextValues,
): Promise<StreamResponse<I, O>> {
const { serialize, parse } = createClientMethodSerializers(
method,
Expand Down Expand Up @@ -323,6 +328,7 @@ export function createGrpcWebTransport(
mode: "cors",
},
header: requestHeader(useBinaryFormat, timeoutMs, header),
contextValues: contextValues ?? createContextValues(),
message: input,
},
next: async (req) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/connect/src/call-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import type { ContextValues } from "./context-values.js";

/**
* Options for a call. Every client should accept CallOptions as optional
* argument in its RPC methods.
Expand Down Expand Up @@ -44,4 +46,9 @@ export interface CallOptions {
* Called when response trailers are received.
*/
onTrailer?(trailers: Headers): void;

/**
* ContextValues to pass to the interceptors.
*/
contextValues?: ContextValues;
}
2 changes: 2 additions & 0 deletions packages/connect/src/callback-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function createUnaryFn<I extends Message<I>, O extends Message<O>>(
options.timeoutMs,
options.headers,
requestMessage,
options.contextValues,
)
.then(
(response) => {
Expand Down Expand Up @@ -146,6 +147,7 @@ function createServerStreamingFn<I extends Message<I>, O extends Message<O>>(
options.timeoutMs,
options.headers,
createAsyncIterable([input]),
options.contextValues,
);
options.onHeader?.(response.header);
for await (const message of response.message) {
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ interface HandlerContextInit {
requestHeader?: HeadersInit;
responseHeader?: HeadersInit;
responseTrailer?: HeadersInit;
values?: ContextValues;
contextValues?: ContextValues;
}

interface HandlerContextController extends HandlerContext {
Expand Down Expand Up @@ -181,7 +181,7 @@ export function createHandlerContext(
deadline.cleanup();
abortController.abort(reason);
},
values: init.values ?? createContextValues(),
values: init.contextValues ?? createContextValues(),
};
}

Expand Down
6 changes: 6 additions & 0 deletions packages/connect/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
MethodInfo,
ServiceType,
} from "@bufbuild/protobuf";
import type { ContextValues } from "./context-values.js";

/**
* An interceptor can add logic to clients, similar to the decorators
Expand Down Expand Up @@ -164,6 +165,11 @@ interface RequestCommon<I extends Message<I>, O extends Message<O>> {
* Headers that will be sent along with the request.
*/
readonly header: Headers;

/**
* The context values for the current call.
*/
readonly contextValues: ContextValues;
}

interface ResponseCommon<I extends Message<I>, O extends Message<O>> {
Expand Down
Loading