Skip to content
Draft
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
5 changes: 4 additions & 1 deletion packages/effect/src/unstable/rpc/RpcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,9 @@ export const makeProtocolWithHttpEffect: Effect.Effect<
}

yield* Scope.addFinalizerExit(scope, () => {
if (!includesFraming) {
return Effect.void
}
clients.delete(id)
clientIds.delete(id)
Queue.offerUnsafe(disconnects, id)
Expand Down Expand Up @@ -1135,7 +1138,7 @@ export const toHttpEffect: <Rpcs extends Rpc.Any>(
const { httpEffect, protocol } = yield* makeProtocolWithHttpEffect
yield* make(group, options).pipe(
Effect.provideService(Protocol, protocol),
Effect.forkScoped
Effect.forkDetach({ startImmediately: true })
)
// @effect-diagnostics-next-line returnEffectInGen:off
return httpEffect
Expand Down
43 changes: 43 additions & 0 deletions packages/effect/test/rpc/RpcServer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { assert, describe, it } from "@effect/vitest"
import { Effect, Schema } from "effect"
import { HttpEffect } from "effect/unstable/http"
import { Rpc, RpcGroup, RpcSerialization, RpcServer } from "effect/unstable/rpc"

const TestGroup = RpcGroup.make(
Rpc.make("Ping", { success: Schema.String })
)

describe("RpcServer", () => {
it.effect("toHttpEffect keeps non-framed json-rpc server alive outside construction scope", () =>
Effect.gen(function*() {
const httpEffect = yield* RpcServer.toHttpEffect(TestGroup).pipe(
Effect.provide(TestGroup.toLayer({
Ping: () => Effect.succeed("pong")
})),
Effect.provideService(RpcSerialization.RpcSerialization, RpcSerialization.jsonRpc()),
Effect.scoped
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Effect.scoped will shutdown the server immediately.

)

const handler = HttpEffect.toWebHandler(httpEffect)
const response = yield* Effect.promise(() =>
handler(
new Request("http://localhost/rpc", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "Ping"
})
})
)
)

assert.strictEqual(response.status, 200)
assert.deepStrictEqual(yield* Effect.promise(() => response.json()), {
jsonrpc: "2.0",
id: 1,
result: "pong"
})
}).pipe(Effect.timeout("2 seconds")))
})