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 a ConnectRouter based Transport #548

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions packages/connect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ export {
createMethodImplSpec,
} from "./implementation.js";
export type { ServiceImplSpec, MethodImplSpec } from "./implementation.js";

export { createRouterHttpClient } from "./router-http-client.js";
69 changes: 69 additions & 0 deletions packages/connect/src/router-http-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import type { ConnectRouter } from "./router.js";
import type { UniversalClientFn } from "./protocol/universal.js";
import type { UniversalHandler } from "./protocol/universal-handler.js";
import { ConnectError } from "./connect-error.js";
import { createAsyncIterable } from "./protocol/async-iterable.js";
import { Code } from "./code.js";

/**
* An in-memory {@link UniversalClientFn | http client} that can be used to route requests to a {@link ConnectRouter}
* by passing network calls. Useful for testing and calling in-process services.
*
* @param router The {@link ConnectRouter} to use.
* @returns The {@link UniversalClientFn} that can be passed to `createTransport` functions.
srikrsna-buf marked this conversation as resolved.
Show resolved Hide resolved
*/
export function createRouterHttpClient(
router: ConnectRouter
): UniversalClientFn {
srikrsna-buf marked this conversation as resolved.
Show resolved Hide resolved
const handlerMap = new Map<string, UniversalHandler>();
for (const handler of router.handlers) {
handlerMap.set(handler.requestPath, handler);
}
return async (uClientReq) => {
const reqUrl = new URL(uClientReq.url);
const handler = handlerMap.get(reqUrl.pathname);
if (!handler) {
throw new ConnectError(
`RouterHttpClient: no handler registered for ${reqUrl.pathname}`,
Code.Unimplemented
);
}
const uServerRes = await handler({
body: uClientReq.body,
httpVersion: "1.1",
srikrsna-buf marked this conversation as resolved.
Show resolved Hide resolved
method: uClientReq.method,
url: reqUrl,
header: uClientReq.header,
});
if (uServerRes.body === undefined) {
throw new ConnectError(
`RouterHttpClient: missing body in handler response for ${reqUrl.pathname}`,
Code.Internal
);
}
let body = uServerRes.body;
if (body instanceof Uint8Array) {
body = createAsyncIterable([body]);
}
return {
body: body,
header: new Headers(uServerRes.header),
status: uServerRes.status,
trailer: new Headers(uServerRes.trailer),
};
};
}