-
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
Closed
Closed
Changes from all 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,28 @@ | ||
| import { type ConnectRouter } from "@connectrpc/connect"; | ||
| import { timestampNow } from "@bufbuild/protobuf/wkt"; | ||
| import { createValidateInterceptor } from "@connectrpc/validate"; | ||
| import { fastify } from "fastify"; | ||
| import { fastifyConnectPlugin } from "@connectrpc/connect-fastify"; | ||
| import { StagehandPingService } from "../gen/stagehand/v1/ping_pb"; | ||
|
|
||
| export const routes = (router: ConnectRouter) => | ||
| router.service(StagehandPingService, { | ||
| async ping(req) { | ||
| return { | ||
| clientSendTime: req.clientSendTime, | ||
| serverSendTime: timestampNow(), | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| async function main() { | ||
| const server = fastify(); | ||
| await server.register(fastifyConnectPlugin, { | ||
| interceptors: [createValidateInterceptor()], | ||
| routes, | ||
| }); | ||
| await server.listen({ host: "localhost", port: 8080 }); | ||
| console.log("server is listening at", server.addresses()); | ||
| } | ||
|
|
||
| void main(); | ||
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,96 @@ | ||
| import { beforeAll, afterAll, describe, expect, it } from "vitest"; | ||
| import { fastify } from "fastify"; | ||
| import { createValidateInterceptor } from "@connectrpc/validate"; | ||
| import { fastifyConnectPlugin } from "@connectrpc/connect-fastify"; | ||
| import { Code, createClient } from "@connectrpc/connect"; | ||
| import { createConnectTransport } from "@connectrpc/connect-node"; | ||
| import { timestampFromMs, timestampMs } from "@bufbuild/protobuf/wkt"; | ||
| import { routes } from "../../server/server"; | ||
| import { StagehandPingService } from "../../gen/stagehand/v1/ping_pb"; | ||
|
|
||
| const TEST_HOST = "127.0.0.1"; | ||
| const TEST_PORT = 0; // use ephemeral port | ||
|
|
||
| describe("Stagehand Ping RPC", () => { | ||
| const server = fastify(); | ||
| let baseUrl: string; | ||
|
|
||
| beforeAll(async () => { | ||
| await server.register(fastifyConnectPlugin, { | ||
| interceptors: [createValidateInterceptor()], | ||
| routes, | ||
| }); | ||
| baseUrl = await server.listen({ host: TEST_HOST, port: TEST_PORT }); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await server.close(); | ||
| }); | ||
|
|
||
| it("responds with server timestamp echoing client timestamp", async () => { | ||
| const client = createClient( | ||
| StagehandPingService, | ||
| createConnectTransport({ baseUrl, httpVersion: "1.1" }), | ||
| ); | ||
| const clientSendTime = Date.now(); | ||
| const response = await client.ping({ | ||
| clientSendTime: timestampFromMs(clientSendTime), | ||
| }); | ||
| expect(timestampMs(response.clientSendTime)).toBe(clientSendTime); | ||
| expect(timestampMs(response.serverSendTime)).toBeGreaterThanOrEqual( | ||
| clientSendTime, | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects invalid timestamp structure", async () => { | ||
| const client = createClient( | ||
| StagehandPingService, | ||
| createConnectTransport({ baseUrl, httpVersion: "1.1" }), | ||
| ); | ||
| await expect( | ||
| client.ping({ | ||
| clientSendTime: { | ||
| seconds: "not a bigint" as never, // Wrong type | ||
| nanos: "not a number" as never, // Wrong type | ||
| } as never, | ||
| }), | ||
| ).rejects.toMatchObject({ | ||
| code: Code.Internal, | ||
| }); | ||
| }); | ||
|
|
||
| it("calculates RTT, latency, and clock offset correctly", async () => { | ||
| const client = createClient( | ||
| StagehandPingService, | ||
| createConnectTransport({ baseUrl, httpVersion: "1.1" }), | ||
| ); | ||
|
|
||
| const t0 = Date.now(); | ||
| const pingResponse = await client.ping({ | ||
| clientSendTime: timestampFromMs(t0), | ||
| }); | ||
|
|
||
| const t3 = Date.now(); | ||
| const rtt = t3 - t0; | ||
| const latency = rtt / 2; | ||
| const clientSendTimeMs = timestampMs(pingResponse.clientSendTime); | ||
| const serverSendTimeMs = timestampMs(pingResponse.serverSendTime); | ||
| const offset = serverSendTimeMs - (t0 + latency); | ||
|
|
||
| // RTT should be positive and reasonable (less than 1 second for local test) | ||
| expect(rtt).toBeGreaterThan(0); | ||
| expect(rtt).toBeLessThan(1000); | ||
|
|
||
| // Latency should be half of RTT | ||
| expect(latency).toBe(rtt / 2); | ||
|
|
||
| // Client send time should match what we sent | ||
| expect(clientSendTimeMs).toBe(t0); | ||
|
|
||
| // Server send time should be after client send time | ||
| expect(serverSendTimeMs).toBeGreaterThanOrEqual(t0); | ||
|
|
||
| // Clock offset should be reasonable (within a few seconds for local test) | ||
| expect(Math.abs(offset)).toBeLessThan(5000); | ||
| }); | ||
| }); |
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
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.