-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add fastify + connectrpc for SDK clients #1328
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
Open
monadoid
wants to merge
11
commits into
main
Choose a base branch
from
add_fastify
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c5f080f
Add fastify server + minimal ping handler
monadoid 1fc7117
Add in buf validation
monadoid de80817
Added in CI check
monadoid 08b9a73
Added "prepare" scripts back in for now
monadoid 5c6ecf4
Using time sync ping example
monadoid 6735d4b
Using google protobuf timestamp
monadoid 4407cfc
Removed rpc:check command + reference to it in CI as it did doesn't w…
monadoid 4682ad0
Update packages/core/server/schema/ping.ts
pirate 126349a
- Consolidated connect.ts and server.ts
monadoid f5c4781
format
monadoid 159b27f
Merge remote-tracking branch 'origin/add_fastify' into add_fastify
monadoid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| version: v2 | ||
| plugins: | ||
| - local: protoc-gen-es | ||
| out: gen | ||
| include_imports: true | ||
| opt: target=ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Generated by buf. DO NOT EDIT. | ||
| version: v2 | ||
| deps: | ||
| - name: buf.build/bufbuild/protovalidate | ||
| commit: 52f32327d4b045a79293a6ad4e7e1236 | ||
| digest: b5:cbabc98d4b7b7b0447c9b15f68eeb8a7a44ef8516cb386ac5f66e7fd4062cd6723ed3f452ad8c384b851f79e33d26e7f8a94e2b807282b3def1cd966c7eace97 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| version: v2 | ||
| modules: | ||
| - path: proto | ||
| deps: | ||
| - buf.build/bufbuild/protovalidate | ||
| lint: | ||
| use: | ||
| - STANDARD | ||
| breaking: | ||
| use: | ||
| - FILE |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // @generated by protoc-gen-es v2.10.1 with parameter "target=ts" | ||
| // @generated from file stagehand/v1/ping.proto (package stagehand.v1, syntax proto3) | ||
| /* eslint-disable */ | ||
|
|
||
| import type { | ||
| GenFile, | ||
| GenMessage, | ||
| GenService, | ||
| } from "@bufbuild/protobuf/codegenv2"; | ||
| import { | ||
| fileDesc, | ||
| messageDesc, | ||
| serviceDesc, | ||
| } from "@bufbuild/protobuf/codegenv2"; | ||
| import type { Timestamp } from "@bufbuild/protobuf/wkt"; | ||
| import { file_google_protobuf_timestamp } from "@bufbuild/protobuf/wkt"; | ||
| import type { Message } from "@bufbuild/protobuf"; | ||
|
|
||
| /** | ||
| * Describes the file stagehand/v1/ping.proto. | ||
| */ | ||
| export const file_stagehand_v1_ping: GenFile = | ||
| /*@__PURE__*/ | ||
| fileDesc( | ||
| "ChdzdGFnZWhhbmQvdjEvcGluZy5wcm90bxIMc3RhZ2VoYW5kLnYxIkMKC1BpbmdSZXF1ZXN0EjQKEGNsaWVudF9zZW5kX3RpbWUYASABKAsyGi5nb29nbGUucHJvdG9idWYuVGltZXN0YW1wInoKDFBpbmdSZXNwb25zZRI0ChBjbGllbnRfc2VuZF90aW1lGAEgASgLMhouZ29vZ2xlLnByb3RvYnVmLlRpbWVzdGFtcBI0ChBzZXJ2ZXJfc2VuZF90aW1lGAIgASgLMhouZ29vZ2xlLnByb3RvYnVmLlRpbWVzdGFtcDJVChRTdGFnZWhhbmRQaW5nU2VydmljZRI9CgRQaW5nEhkuc3RhZ2VoYW5kLnYxLlBpbmdSZXF1ZXN0Ghouc3RhZ2VoYW5kLnYxLlBpbmdSZXNwb25zZWIGcHJvdG8z", | ||
| [file_google_protobuf_timestamp], | ||
| ); | ||
|
|
||
| /** | ||
| * @generated from message stagehand.v1.PingRequest | ||
| */ | ||
| export type PingRequest = Message<"stagehand.v1.PingRequest"> & { | ||
| /** | ||
| * Timestamp representing when the client emitted the ping. | ||
| * | ||
| * @generated from field: google.protobuf.Timestamp client_send_time = 1; | ||
| */ | ||
| clientSendTime?: Timestamp; | ||
| }; | ||
|
|
||
| /** | ||
| * Describes the message stagehand.v1.PingRequest. | ||
| * Use `create(PingRequestSchema)` to create a new message. | ||
| */ | ||
| export const PingRequestSchema: GenMessage<PingRequest> = | ||
| /*@__PURE__*/ | ||
| messageDesc(file_stagehand_v1_ping, 0); | ||
|
|
||
| /** | ||
| * @generated from message stagehand.v1.PingResponse | ||
| */ | ||
| export type PingResponse = Message<"stagehand.v1.PingResponse"> & { | ||
| /** | ||
| * Echo of the client's send time so latency can be derived from RTT. | ||
| * | ||
| * @generated from field: google.protobuf.Timestamp client_send_time = 1; | ||
| */ | ||
| clientSendTime?: Timestamp; | ||
|
|
||
| /** | ||
| * Timestamp representing when the server crafted the response. | ||
| * | ||
| * @generated from field: google.protobuf.Timestamp server_send_time = 2; | ||
| */ | ||
| serverSendTime?: Timestamp; | ||
| }; | ||
|
|
||
| /** | ||
| * Describes the message stagehand.v1.PingResponse. | ||
| * Use `create(PingResponseSchema)` to create a new message. | ||
| */ | ||
| export const PingResponseSchema: GenMessage<PingResponse> = | ||
| /*@__PURE__*/ | ||
| messageDesc(file_stagehand_v1_ping, 1); | ||
|
|
||
| /** | ||
| * @generated from service stagehand.v1.StagehandPingService | ||
| */ | ||
| export const StagehandPingService: GenService<{ | ||
| /** | ||
| * @generated from rpc stagehand.v1.StagehandPingService.Ping | ||
| */ | ||
| ping: { | ||
| methodKind: "unary"; | ||
| input: typeof PingRequestSchema; | ||
| output: typeof PingResponseSchema; | ||
| }; | ||
| }> = /*@__PURE__*/ serviceDesc(file_stagehand_v1_ping, 0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package stagehand.v1; | ||
|
|
||
| import "google/protobuf/timestamp.proto"; | ||
|
|
||
| message PingRequest { | ||
| // Timestamp representing when the client emitted the ping. | ||
| google.protobuf.Timestamp client_send_time = 1; | ||
| } | ||
|
|
||
| message PingResponse { | ||
| // Echo of the client's send time so latency can be derived from RTT. | ||
| google.protobuf.Timestamp client_send_time = 1; | ||
|
|
||
| // Timestamp representing when the server crafted the response. | ||
| google.protobuf.Timestamp server_send_time = 2; | ||
| } | ||
|
|
||
| service StagehandPingService { | ||
| rpc Ping(PingRequest) returns (PingResponse); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { createClient } from "@connectrpc/connect"; | ||
| import { createConnectTransport } from "@connectrpc/connect-node"; | ||
| import { timestampDate, timestampFromDate } from "@bufbuild/protobuf/wkt"; | ||
| import { StagehandPingService } from "../gen/stagehand/v1/ping_pb"; | ||
| import { | ||
| pingRequestSchema, | ||
| pingResponseSchema, | ||
| timestampFromSecondsAndNanos, | ||
| } from "./schema/ping"; | ||
|
|
||
| async function main() { | ||
| const transport = createConnectTransport({ | ||
| baseUrl: "http://localhost:8080", | ||
| httpVersion: "1.1", | ||
| }); | ||
|
|
||
| const client = createClient(StagehandPingService, transport); | ||
|
|
||
| const t0 = Date.now(); | ||
| const parsedRequest = pingRequestSchema.parse({ | ||
| clientSendTime: timestampFromDate(new Date(t0)), | ||
| }); | ||
| // Convert Zod-validated plain object back to Timestamp Message for gRPC client | ||
| const pingRequest = { | ||
| clientSendTime: timestampFromSecondsAndNanos(parsedRequest.clientSendTime), | ||
| }; | ||
| const rawResponse = await client.ping(pingRequest); | ||
| const pingResponse = pingResponseSchema.parse(rawResponse); | ||
|
|
||
| const t3 = Date.now(); | ||
| const rtt = t3 - t0; | ||
| const latency = rtt / 2; | ||
| const clientSendTimeMs = timestampDate(pingResponse.clientSendTime).getTime(); | ||
| const serverSendTimeMs = timestampDate(pingResponse.serverSendTime).getTime(); | ||
| const offset = serverSendTimeMs - (t0 + latency); | ||
|
|
||
| console.log( | ||
| JSON.stringify( | ||
| { | ||
| clientSendTime: clientSendTimeMs, | ||
| serverSendTime: serverSendTimeMs, | ||
| rtt, | ||
| latency, | ||
| offset, | ||
| }, | ||
| null, | ||
| 2, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { ConnectError, Code, type ConnectRouter } from "@connectrpc/connect"; | ||
| import { timestampFromDate } from "@bufbuild/protobuf/wkt"; | ||
| import { StagehandPingService } from "../gen/stagehand/v1/ping_pb"; | ||
| import { pingRequestSchema } from "./schema/ping"; | ||
|
|
||
| export default (router: ConnectRouter) => | ||
| router.service(StagehandPingService, { | ||
| async ping(req) { | ||
| const parsedReq = pingRequestSchema.safeParse(req); | ||
| if (!parsedReq.success) { | ||
| throw new ConnectError( | ||
| `Invalid PingRequest: ${parsedReq.error.message}`, | ||
| Code.InvalidArgument, | ||
| ); | ||
| } | ||
| const serverSendTime = timestampFromDate(new Date()); | ||
| return { | ||
| clientSendTime: parsedReq.data.clientSendTime, | ||
| serverSendTime, | ||
| }; | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { timestampFromDate, type Timestamp } from "@bufbuild/protobuf/wkt"; | ||
| import { z } from "zod"; | ||
|
|
||
| const timestampShape = z.object({ | ||
| seconds: z.bigint({ message: "timestamp seconds required" }), | ||
| nanos: z | ||
| .number({ message: "timestamp nanos required" }) | ||
| .int({ message: "timestamp nanos must be an integer" }) | ||
| .gte(0, { message: "timestamp nanos must be >= 0" }) | ||
| .lte(999_999_999, { message: "timestamp nanos must be < 1,000,000,000" }), | ||
| }); | ||
|
|
||
| const preprocessTimestamp = (value: unknown) => { | ||
| if (value instanceof Date) { | ||
| return timestampFromDate(value); | ||
| } | ||
| if (typeof value === "number") { | ||
| return timestampFromDate(new Date(value)); | ||
| } | ||
| return value; | ||
| }; | ||
|
|
||
| const timestampSchema = z | ||
| .preprocess(preprocessTimestamp, timestampShape) | ||
| .refine( | ||
| (value) => | ||
| value.seconds > BigInt(0) || | ||
| (value.seconds === BigInt(0) && value.nanos > 0), | ||
| { | ||
| message: "timestamp must be greater than zero milliseconds", | ||
| }, | ||
| ); | ||
|
|
||
| /** | ||
| * Converts a plain object with seconds and nanos back to a Timestamp Message. | ||
| * This is needed because Zod validation strips the Message type metadata. | ||
| */ | ||
| export function timestampFromSecondsAndNanos(value: { | ||
| seconds: bigint; | ||
| nanos: number; | ||
| }): Timestamp { | ||
| // Convert seconds (Unix timestamp) to milliseconds for Date constructor | ||
| const date = new Date(Number(value.seconds) * 1000 + value.nanos / 1_000_000); | ||
| return timestampFromDate(date); | ||
| } | ||
|
|
||
| export const pingRequestSchema = z.object({ | ||
| clientSendTime: timestampSchema, | ||
| }); | ||
|
|
||
| export const pingResponseSchema = z.object({ | ||
| clientSendTime: timestampSchema, | ||
| serverSendTime: timestampSchema, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { createValidateInterceptor } from "@connectrpc/validate"; | ||
| import { fastify } from "fastify"; | ||
| import { fastifyConnectPlugin } from "@connectrpc/connect-fastify"; | ||
| import routes from "./connect"; | ||
|
|
||
| async function main() { | ||
| const server = fastify(); | ||
| await server.register(fastifyConnectPlugin, { | ||
| interceptors: [createValidateInterceptor()], | ||
| routes, | ||
| }); | ||
| server.get("/", (_, reply) => { | ||
| reply.type("text/plain"); | ||
| reply.send("Hello World!"); | ||
| }); | ||
| await server.listen({ host: "localhost", port: 8080 }); | ||
| console.log("server is listening at", server.addresses()); | ||
| } | ||
|
|
||
| void main(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.