Skip to content

Commit 30435e1

Browse files
author
zoey
authored
fix: correctly set supported versions for different transports (#205)
This pull request refactors how protocol version compatibility is handled across transports in the Hermes system. It introduces a new mechanism for specifying supported protocol versions, updates the behavior definition, and modifies several transport modules to align with the new approach. ### Refactoring protocol version handling: * [`lib/hermes/protocol.ex`](diffhunk://#diff-98232ed245d0a2546cad3d194c94fd8e637e46a66416b40a5d067ae1850522dbL81-R102): Added the `supported_transport_versions/1` private function to handle cases where a transport specifies `:all` as its supported protocol versions, mapping it to the global `@supported_versions`. Updated the `validate_transport/2` function to use this new mechanism. ### Updates to transport modules: * [`lib/hermes/transport/behaviour.ex`](diffhunk://#diff-71dc0b3f96d79ae5f656538529828d6dbcc9c2cb2f58b5d938b51fdc06851cadL25-R25): Updated the `supported_protocol_versions/0` callback to allow transports to return either a list of strings or `:all`. * `lib/hermes/server/transport/stdio.ex`, `lib/hermes/transport/stdio.ex`, `lib/hermes/transport/websocket.ex`, `test/support/mock_transport.ex`, `test/support/stub_transport.ex`: Modified the `supported_protocol_versions/0` implementations to return `:all` instead of hardcoded lists for certain transports. [[1]](diffhunk://#diff-61921887b952ce41ebfb436711dca9d7f8b7c6a9dfd56cc0c2a20baced77362fL98-R98) [[2]](diffhunk://#diff-6545e6cca647b52bd939e362d7a246724f0190d4eabbeb3cad9724a2f0c4a63cL86-R86) [[3]](diffhunk://#diff-c1dee42ccc6dfb67a6df853f41e6afb92bcf40f4abf0084d0a4f261da09a0ba4L94-R94) [[4]](diffhunk://#diff-ceaf02dc0afad3b8c01fbd0ee8194081ab1c0b5ce78f6e1fcbdf3b5ebb04fbbaL15-R15) [[5]](diffhunk://#diff-1d9b45a0670ea752f56967d81e40b4f9dd2870b0e71c863bb352bf6c94ea2995L93-R93) * [`lib/hermes/server/transport/streamable_http.ex`](diffhunk://#diff-da9c9a7a019b46d51290734c0735d5901ec1f1ee58ee44dc8b68912164e90ba0L128-R128): Updated `supported_protocol_versions/0` to reflect new supported versions, adding "2025-06-18". [[1]](diffhunk://#diff-da9c9a7a019b46d51290734c0735d5901ec1f1ee58ee44dc8b68912164e90ba0L128-R128) [[2]](diffhunk://#diff-783726aa86a0f2e2c55f5aedce0e709b795bc7550f9b816eae300be91329bb75L109-R109) ### Code style improvements: * [`lib/hermes/transport/streamable_http.ex`](diffhunk://#diff-783726aa86a0f2e2c55f5aedce0e709b795bc7550f9b816eae300be91329bb75L80-R80): Adjusted schema definitions to use parentheses for consistency in formatting. [[1]](diffhunk://#diff-783726aa86a0f2e2c55f5aedce0e709b795bc7550f9b816eae300be91329bb75L80-R80) [[2]](diffhunk://#diff-783726aa86a0f2e2c55f5aedce0e709b795bc7550f9b816eae300be91329bb75L89-R89) These changes enhance flexibility in defining protocol compatibility, improve maintainability, and ensure consistency across the codebase.
1 parent 2f41337 commit 30435e1

File tree

9 files changed

+20
-25
lines changed

9 files changed

+20
-25
lines changed

lib/hermes/protocol.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,28 @@ defmodule Hermes.Protocol do
7878
Validates if a transport is compatible with a protocol version.
7979
"""
8080
@spec validate_transport(version(), module()) :: :ok | {:error, Error.t()}
81-
def validate_transport(version, transport_module) do
82-
supported_versions = transport_module.supported_protocol_versions()
81+
def validate_transport(version, transport) do
82+
supported_versions = supported_transport_versions(transport)
8383

8484
if version in supported_versions do
8585
:ok
8686
else
8787
{:error,
8888
Error.transport(:incompatible_transport, %{
8989
version: version,
90-
transport: transport_module,
90+
transport: transport,
9191
supported_versions: supported_versions
9292
})}
9393
end
9494
end
9595

96+
defp supported_transport_versions(transport) do
97+
case transport.supported_protocol_versions() do
98+
:all -> @supported_versions
99+
[_ | _] = versions -> versions
100+
end
101+
end
102+
96103
@doc """
97104
Returns the set of features supported by a protocol version.
98105
"""

lib/hermes/server/transport/stdio.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ defmodule Hermes.Server.Transport.STDIO do
9595
end
9696

9797
@impl Transport
98-
def supported_protocol_versions do
99-
["2024-11-05", "2025-03-26"]
100-
end
98+
def supported_protocol_versions, do: :all
10199

102100
@impl GenServer
103101
def init(opts) do

lib/hermes/server/transport/streamable_http.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ defmodule Hermes.Server.Transport.StreamableHTTP do
125125
end
126126

127127
@impl Transport
128-
def supported_protocol_versions do
129-
["2024-11-05", "2025-03-26"]
130-
end
128+
def supported_protocol_versions, do: ["2025-03-26", "2025-06-18"]
131129

132130
@doc """
133131
Registers an SSE handler process for a session.

lib/hermes/transport/behaviour.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ defmodule Hermes.Transport.Behaviour do
2222
iex> MyTransport.supported_protocol_versions()
2323
["2024-11-05", "2025-03-26"]
2424
"""
25-
@callback supported_protocol_versions() :: [String.t()]
25+
@callback supported_protocol_versions() :: [String.t()] | :all
2626
end

lib/hermes/transport/stdio.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ defmodule Hermes.Transport.STDIO do
8383
end
8484

8585
@impl Transport
86-
def supported_protocol_versions do
87-
["2024-11-05", "2025-03-26"]
88-
end
86+
def supported_protocol_versions, do: :all
8987

9088
@impl GenServer
9189
def init(%{} = opts) do

lib/hermes/transport/streamable_http.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ defmodule Hermes.Transport.StreamableHTTP do
7777
| {:enable_sse, boolean()}
7878
| GenServer.option()
7979

80-
defschema :options_schema, %{
80+
defschema(:options_schema, %{
8181
name: {{:custom, &Hermes.genserver_name/1}, {:default, __MODULE__}},
8282
client: {:required, Hermes.get_schema(:process_name)},
8383
base_url: {:required, {:string, {:transform, &URI.new!/1}}},
@@ -86,7 +86,7 @@ defmodule Hermes.Transport.StreamableHTTP do
8686
transport_opts: {:any, {:default, []}},
8787
http_options: {:any, {:default, []}},
8888
enable_sse: {:boolean, {:default, false}}
89-
}
89+
})
9090

9191
@impl Transport
9292
@spec start_link(params_t) :: GenServer.on_start()
@@ -106,9 +106,7 @@ defmodule Hermes.Transport.StreamableHTTP do
106106
end
107107

108108
@impl Transport
109-
def supported_protocol_versions do
110-
["2025-03-26"]
111-
end
109+
def supported_protocol_versions, do: ["2025-03-26", "2025-06-18"]
112110

113111
@impl GenServer
114112
def init(opts) do

lib/hermes/transport/websocket.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ if Code.ensure_loaded?(:gun) do
9191
end
9292

9393
@impl Transport
94-
def supported_protocol_versions do
95-
["2024-11-05", "2025-03-26"]
96-
end
94+
def supported_protocol_versions, do: :all
9795

9896
@impl GenServer
9997
def init(%{} = opts) do

test/support/mock_transport.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ defmodule MockTransport do
1212
def shutdown(_), do: :ok
1313

1414
@impl true
15-
def supported_protocol_versions, do: ["2025-06-18", "2025-03-26", "2024-11-05"]
15+
def supported_protocol_versions, do: :all
1616
end

test/support/stub_transport.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ defmodule StubTransport do
9090
end
9191

9292
@impl true
93-
def supported_protocol_versions do
94-
["2024-11-05", "2025-03-26"]
95-
end
93+
def supported_protocol_versions, do: :all
9694

9795
@impl true
9896
def init(state) do

0 commit comments

Comments
 (0)