protocol-jsonrpc 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0488651e9e3c97f90a8bfe83c904ade78a8e0532e524fc6b1e97fd7ec2334f0f'
4
- data.tar.gz: 2d9c402bcd8786051868b545afdb858ca862fcd4e699e6910a720467b4d0ab3d
3
+ metadata.gz: 874cf5c8dbdabf1d9b7e38b2fea872b0ebad5f3375970f3c94f8fda66ab0020d
4
+ data.tar.gz: b135704af442d14ac8f3792a899444ce20253d70212b717249ce7105add41c54
5
5
  SHA512:
6
- metadata.gz: 32a5eaf0c2424ce7a93250a7d164fb98a856eb918422481de1d7348906edbd9817cd87d32f55c1eeef3338c9ff7c531329cdd98bad5512254e4c121ae5442b0f
7
- data.tar.gz: be8901fb47e177223a99175999b3e25798c0709505f558442020cca36d24da59e970114cefdbb907874005fac1c241360a78134e7bba2ca49869fbde3182cab2
6
+ metadata.gz: 4b00488285c5680d63ea23dea25c609c513ebcea886e17ab305ebc3e0f0992b61d78ab7cbbd1f1416bf241d703c4c1b1e1cc760725aa25a610bc869ea60dae55
7
+ data.tar.gz: adb7f0226042ba5a14fbcd7fcbac823546dd9fb228bdfa65c861ca6fb82ca8fc8afbacc9ebf9e61ad2f8daef253d0c06d3ab008d8fe1db7a9ad13280c583eb08
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.2] - 2026-11-06
4
+
5
+ - Require `json >= 2.19.9, < 4.0` to pick up the fix for CVE-2026-54696.
6
+ - Update development dependencies (`erb` 6.0.7 for CVE-2026-41316, `json` 2.21.2).
7
+
8
+ ## [0.2.1] - 2025-06-01
9
+
10
+ - If a reply block for a Notification raises, suppress the error as defined in the spec
11
+
3
12
  ## [0.2.0] - 2025-06-01
4
13
 
5
14
  **Breaking changes**: As I work towarsd a 1.0.0 release, I've changed the interface to ensure a uniform interface for [JSON-RPC 2.0 batch processing](https://www.jsonrpc.org/specification#batch). I believe we have a much more robust implementation now, so I will try to stay more consistent, but please be cautious upgrading until 1.0.0 is released and we finalize the interface.
data/README.md CHANGED
@@ -52,10 +52,8 @@ This wraps a framer and provides higher-level methods for communication.
52
52
  Here's a basic example showing how to create a JSON-RPC connection over a socket pair:
53
53
 
54
54
  ```ruby
55
- require 'protocol/jsonrpc'
56
- require 'protocol/jsonrpc/connection'
57
- require 'protocol/jsonrpc/framer'
58
- require 'socket'
55
+ require "protocol/jsonrpc"
56
+ require "socket"
59
57
 
60
58
  # Create a socket pair for testing
61
59
  client_socket, server_socket = UNIXSocket.pair
@@ -179,6 +177,10 @@ Typically created by replying to a request:
179
177
  # From a request object
180
178
  response = request.reply(19)
181
179
 
180
+ response = request.reply do |message|
181
+ message.params.sum if message.request? && message.method == "add"
182
+ end
183
+
182
184
  # Or directly
183
185
  response = Protocol::Jsonrpc::Response.new(
184
186
  result: 19,
@@ -194,6 +196,11 @@ For error responses:
194
196
  # Create from an error object
195
197
  error = Protocol::Jsonrpc::InvalidParamsError.new("Invalid parameters")
196
198
  error_response = request.reply(error)
199
+
200
+ # Create from a reply that raises
201
+ response = request.reply do |message|
202
+ raise "Oops" # Returns an InternalError response
203
+ end
197
204
  ```
198
205
 
199
206
  Error types represent the standard JSON-RPC error codes:
@@ -206,6 +213,14 @@ Protocol::Jsonrpc::InvalidParamsError
206
213
  Protocol::Jsonrpc::InternalError
207
214
  ```
208
215
 
216
+ Most of the errors happen automatically, but some must be triggered manually.
217
+
218
+ ```ruby
219
+ response = request.reply do |message|
220
+ raise Protocol::Jsonrpc::MethodNotFoundError, "We don't support any methods"
221
+ end
222
+ ```
223
+
209
224
  ## Batch Processing
210
225
 
211
226
  JSON-RPC supports batch requests and responses. The library returns a `Protocol::Jsonrpc::Batch` that acts like an array and provides a `reply` method for processing:
@@ -26,7 +26,9 @@ module Protocol
26
26
  def read(&block)
27
27
  flush
28
28
  frame = read_frame
29
- Message.load(frame.unpack)
29
+ message = Message.load(frame.unpack)
30
+ yield message if block_given?
31
+ message
30
32
  rescue => e
31
33
  InvalidMessage.new(error: e)
32
34
  end
@@ -24,8 +24,10 @@ module Protocol
24
24
  ).freeze
25
25
 
26
26
  # Factory method to create the appropriate error type
27
+ # @param code [Integer] The JSON-RPC error code
28
+ # @param message [String] The error message
29
+ # @param data [#as_json, #to_json] Serializable data to return to the client
27
30
  # @param id [String, Integer] The request ID
28
- # @param error [Hash] The error data from the JSON-RPC response
29
31
  # @return [Error] The appropriate error instance
30
32
  def self.from_message(code:, message:, data: nil, id: nil)
31
33
  case code
@@ -54,17 +56,22 @@ module Protocol
54
56
  error = error.transform_keys(&:to_sym)
55
57
  from_message(id: id, data: data, **error)
56
58
  in Jsonrpc::Error
59
+ error.data ||= data if data
60
+ error.id ||= id if id
57
61
  error
58
62
  in JSON::ParserError
59
63
  ParseError.new("Parse error: #{error.message}", data:, id:)
60
64
  in StandardError
61
65
  InternalError.new(error.message, data:, id:)
62
- else
66
+ in Exception
63
67
  raise error
68
+ else
69
+ raise ArgumentError, "Unknown error type: #{error.class}"
64
70
  end
65
71
  end
66
72
 
67
- attr_reader :data, :code, :id
73
+ attr_accessor :data, :id
74
+ attr_reader :code
68
75
 
69
76
  def initialize(message = nil, data: nil, id: nil)
70
77
  message = nil if message&.empty?
@@ -36,16 +36,17 @@ module Protocol
36
36
  # @return [Message] The parsed message
37
37
  def from_hash(parsed)
38
38
  return InvalidMessage.new(data: parsed.inspect) unless parsed.is_a?(Hash)
39
+
39
40
  jsonrpc = parsed[:jsonrpc]
40
41
 
41
42
  case parsed
42
- in { id:, error: }
43
+ in {id:, error:}
43
44
  ErrorResponse.new(id:, error: Error.from_message(**error), jsonrpc:)
44
- in { id:, result: }
45
+ in {id:, result:}
45
46
  Response.new(id:, result:, jsonrpc:)
46
- in { id:, method: }
47
+ in {id:, method:}
47
48
  Request.new(id:, method:, params: parsed[:params], jsonrpc:)
48
- in { method: }
49
+ in {method:}
49
50
  Notification.new(method:, params: parsed[:params], jsonrpc:)
50
51
  else
51
52
  InvalidMessage.new(data: parsed.inspect)
@@ -29,10 +29,12 @@ module Protocol
29
29
  def id = nil
30
30
 
31
31
  # Compatibility with the Request
32
- # Yields the notificatino for processing but ignores the result
32
+ # Yields the notification for processing but ignores the result
33
33
  def reply(*, &)
34
34
  yield self if block_given?
35
- nil # notification always returns nil
35
+ nil
36
+ rescue
37
+ nil # JSON-RPC 2.0 spec says notifications should never return, even on error.
36
38
  end
37
39
 
38
40
  def notification? = true
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Protocol
7
7
  module Jsonrpc
8
- VERSION = "0.2.0"
8
+ VERSION = "0.2.2"
9
9
  end
10
10
  end
@@ -6,18 +6,18 @@
6
6
  module Protocol
7
7
  module Jsonrpc
8
8
  JSONRPC_VERSION = "2.0"
9
-
10
- autoload :Batch, "protocol/jsonrpc/batch"
11
- autoload :Connection, "protocol/jsonrpc/connection"
12
- autoload :Error, "protocol/jsonrpc/error"
13
- autoload :ErrorResponse, "protocol/jsonrpc/error_response"
14
- autoload :Frame, "protocol/jsonrpc/frame"
15
- autoload :Framer, "protocol/jsonrpc/framer"
16
- autoload :InvalidMessage, "protocol/jsonrpc/invalid_message"
17
- autoload :Message, "protocol/jsonrpc/message"
18
- autoload :Notification, "protocol/jsonrpc/notification"
19
- autoload :Request, "protocol/jsonrpc/request"
20
- autoload :Response, "protocol/jsonrpc/response"
21
- autoload :VERSION, "protocol/jsonrpc/version"
22
9
  end
23
10
  end
11
+
12
+ require_relative "jsonrpc/version"
13
+ require_relative "jsonrpc/error"
14
+ require_relative "jsonrpc/message"
15
+ require_relative "jsonrpc/error_response"
16
+ require_relative "jsonrpc/invalid_message"
17
+ require_relative "jsonrpc/notification"
18
+ require_relative "jsonrpc/request"
19
+ require_relative "jsonrpc/response"
20
+ require_relative "jsonrpc/frame"
21
+ require_relative "jsonrpc/framer"
22
+ require_relative "jsonrpc/connection"
23
+ require_relative "jsonrpc/batch"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-jsonrpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Emde
@@ -13,16 +13,22 @@ dependencies:
13
13
  name: json
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
- - - "~>"
16
+ - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '2.10'
18
+ version: 2.19.9
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '4.0'
19
22
  type: :runtime
20
23
  prerelease: false
21
24
  version_requirements: !ruby/object:Gem::Requirement
22
25
  requirements:
23
- - - "~>"
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: 2.19.9
29
+ - - "<"
24
30
  - !ruby/object:Gem::Version
25
- version: '2.10'
31
+ version: '4.0'
26
32
  description: JSON-RPC 2.0 protocol implementation
27
33
  email:
28
34
  - me@martinemde.com
@@ -69,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
75
  - !ruby/object:Gem::Version
70
76
  version: '0'
71
77
  requirements: []
72
- rubygems_version: 3.6.7
78
+ rubygems_version: 3.6.9
73
79
  specification_version: 4
74
80
  summary: JSON-RPC 2.0 protocol implementation
75
81
  test_files: []