ndb 1.26.0 → 1.27.0

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: 32aa148c96adae4a7630f19c721c8a3fd1df71544efb6bdcbd9b0c2942760213
4
- data.tar.gz: 7813b56e1b5d88a917ba396aeedca6634564341b07456d1c85b8886f8bc662d2
3
+ metadata.gz: 1df3fbe43cc8b20318af96ef9f98ed161b88940e582cd9263c3194aa6943cd1e
4
+ data.tar.gz: dcfd8363d5b39b521911140856032b1581e0f1e890d267c89d5154d363972574
5
5
  SHA512:
6
- metadata.gz: 2060a36349070a257a9e02b7e98d79f0e896007a3a343a41aab6c82f2551031e5c2984a4138b3ae2a2305cd3f033aeb43c1ac8a116e4da6e57750fa02c46eaab
7
- data.tar.gz: 421d6a354f03118dbdf09f42aa17a58f4f5980cd0e4ff5080d3e4123229ec4b2ff2cd586b6a14b375ae49c6dc3defd51b7ba238554db94f71a750ccaef193fa9
6
+ metadata.gz: a448ef3e8d1fb513ca0f961994deaf089e0c16e93c3168a0384f5ead761b134f2dd516133b949bba8de7126510ef40a8506ebe4f115e9c9a00b05250f2a934d0
7
+ data.tar.gz: cfd606a955926f0c9a5161eaef6e6e3be544704f89ac296176f99727f2a28f662d3e76d947514fa6c6ee884830bc627649cce71af0ded538647738df8181e8d8
data/README.md CHANGED
@@ -36,6 +36,8 @@ require "ndb"
36
36
  ```ruby
37
37
  ndb = Ndb.connect("unix:///run/ndb/ndb.sock") # unix socket
38
38
  ndb = Ndb.connect("tcp://10.0.0.5:7070", token: "secret") # TCP with a bearer token
39
+ ndb = Ndb.connect("tcp://:secret@10.0.0.5:7070/billion") # token and database in the URL
40
+ ndb = Ndb.connect("unix:///run/ndb/ndb.sock?db=billion") # a database on the unix socket
39
41
  ndb = Ndb.connect # reads NDB_URL and NDB_TOKEN
40
42
 
41
43
  ndb.health # => true when the daemon is ready (GET /_health)
@@ -55,7 +57,8 @@ Ndb::Client.new(
55
57
  ```
56
58
 
57
59
  - The address is, in order: `url:` (the `addr` of `Ndb.connect`), `socket:`, env `NDB_URL`, then the default socket `/run/ndb/ndb.sock`.
58
- - The token is `token:`, or env `NDB_TOKEN`. It goes only in the `Authorization` header. A unix socket does not need it.
60
+ - The token is, in order: `token:`, the token of the URL (`tcp://:<token>@host:port`), env `NDB_TOKEN`. It goes only in the `Authorization` header. A unix socket does not need it.
61
+ - The database is the path of the URL (`tcp://host:7070/billion`), or `?db=` (needed on a unix socket). None is the default database. See [Databases](https://ndb.clowk.in/databases).
59
62
  - A bad address raises `ArgumentError` at once.
60
63
  - `ready_timeout` is how long a call retries while the daemon is not ready (`not_ready`, or the socket is missing or refuses). It uses exponential backoff (50 ms up to 1 s) with jitter. A write is retried only when the daemon certainly did not process it. A read is also retried after a dropped connection. A timeout is never retried. When the deadline passes, the last error is raised.
61
64
  - `health` does not wait: a daemon that is recovering raises `Ndb::NotReady`.
data/lib/ndb/client.rb CHANGED
@@ -29,8 +29,11 @@ module Ndb
29
29
  RETRY_CAP = 1.0
30
30
 
31
31
  # The address is, in order: url:, socket:, env NDB_URL, the default
32
- # socket. The token is token:, or env NDB_TOKEN. It goes only in the
33
- # Authorization header, and the unix socket does not need it.
32
+ # socket. The token is, in order: token:, the token of the address
33
+ # (tcp://:<token>@host:port), env NDB_TOKEN. It goes only in the
34
+ # Authorization header, and the unix socket does not need it. The path
35
+ # of the address names a database (tcp://host:7070/billion; ?db=billion
36
+ # for a unix socket); none is the default database.
34
37
  #
35
38
  # ready_timeout is how many seconds a call retries while the daemon is
36
39
  # starting or recovering (not_ready, socket missing or refusing), with
@@ -40,7 +43,7 @@ module Ndb
40
43
  # after the User-Agent of the gem, so the daemon can tell the apps apart.
41
44
  def initialize(url: nil, socket: nil, token: nil, timeout: 30, pool_size: 16, ready_timeout: 10, user_agent: nil)
42
45
  @endpoint = Endpoint.parse(url || socket || presence(ENV["NDB_URL"]) || DEFAULT_SOCKET)
43
- @token = presence(token) || presence(ENV["NDB_TOKEN"])
46
+ @token = presence(token) || @endpoint.token || presence(ENV["NDB_TOKEN"])
44
47
  @pool = Pool.new(@endpoint, timeout: timeout, size: pool_size)
45
48
  @ready_timeout = ready_timeout.to_f
46
49
  @user_agent = [USER_AGENT, presence(user_agent.to_s.strip)].compact.join(" ")
@@ -48,6 +51,9 @@ module Ndb
48
51
 
49
52
  def socket = @endpoint.path
50
53
 
54
+ # The address only: never the token.
55
+ def inspect = "#<Ndb::Client #{@endpoint}>"
56
+
51
57
  # A collection (or a pattern such as "logs-2026-*") with the query
52
58
  # builder. definition is what the first call declares (PUT /{coll}).
53
59
  def collection(name, definition = {})
@@ -487,9 +493,11 @@ module Ndb
487
493
  end
488
494
  end
489
495
 
490
- # Adds the User-Agent, and the bearer token when the client has one.
496
+ # Adds the User-Agent, the database of the address (Ndb-Database) and
497
+ # the bearer token, when the client has them.
491
498
  def authorize(headers)
492
499
  headers = headers.merge("User-Agent" => @user_agent)
500
+ headers = headers.merge("Ndb-Database" => @endpoint.database) if @endpoint.database
493
501
 
494
502
  @token ? headers.merge("Authorization" => "Bearer #{@token}") : headers
495
503
  end
@@ -12,10 +12,14 @@ module Ndb
12
12
  # https:// URL (ndb behind a TLS proxy such as Caddy). The host name is
13
13
  # resolved each time a connection opens, so a changed DNS record is
14
14
  # followed after a reconnect.
15
+ #
16
+ # The token can be in the address, as in a Redis URL:
17
+ # tcp://:<token>@host:port. The user part is ignored.
15
18
  class Endpoint
16
19
  INVALID = "must be unix:///path, tcp://host:port or https://host"
20
+ NO_COLON = "put the token after a colon: tcp://:<token>@host:port"
17
21
 
18
- attr_reader :path, :host, :port
22
+ attr_reader :path, :host, :port, :token, :database
19
23
 
20
24
  # "unix:///path", "tcp://host:port", "https://host[:port]",
21
25
  # "http://host[:port]", or a bare socket path.
@@ -25,18 +29,55 @@ module Ndb
25
29
  return new(path: address) if address.start_with?("/")
26
30
 
27
31
  uri = URI.parse(address)
32
+ token = token_of(uri, address)
33
+ database = database_of(uri, address)
28
34
 
29
35
  case uri.scheme
30
- when "unix" then new(path: uri.path)
31
- when "tcp" then new(host: uri.host, port: uri.port)
32
- when "https", "http" then new(host: uri.host, port: uri.port, tls: uri.scheme == "https")
33
- else raise ArgumentError, "ndb address #{address.inspect} #{INVALID}"
36
+ when "unix" then new(path: uri.path, database: database)
37
+ when "tcp" then new(host: uri.host, port: uri.port, token: token, database: database)
38
+ when "https", "http" then new(host: uri.host, port: uri.port, tls: uri.scheme == "https", token: token, database: database)
39
+ else raise ArgumentError, "ndb address #{redact(address).inspect} #{INVALID}"
34
40
  end
35
41
  rescue URI::InvalidURIError
36
- raise ArgumentError, "ndb address #{address.inspect} #{INVALID}"
42
+ raise ArgumentError, "ndb address #{redact(address).inspect} #{INVALID}"
37
43
  end
38
44
 
39
- def initialize(path: nil, host: nil, port: nil, tls: false)
45
+ # The password of the userinfo. A user with no password is refused: it
46
+ # is a token in the wrong place.
47
+ def self.token_of(uri, address)
48
+ return nil if uri.userinfo.nil?
49
+
50
+ raise ArgumentError, "ndb address #{redact(address).inspect}: #{NO_COLON}" if uri.password.nil? && !uri.user.to_s.empty?
51
+
52
+ token = URI::RFC2396_PARSER.unescape(uri.password.to_s)
53
+
54
+ token.empty? ? nil : token
55
+ end
56
+
57
+ # The database of the address: the path of a tcp or https address
58
+ # (tcp://host:7070/billion) or the query db (needed for a unix socket,
59
+ # whose path is the file). nil is the default database.
60
+ def self.database_of(uri, address)
61
+ query = URI.decode_www_form(uri.query.to_s).to_h["db"]
62
+ path = uri.scheme == "unix" ? "" : uri.path.to_s.delete_prefix("/").delete_suffix("/")
63
+ shown = redact(address).inspect
64
+
65
+ raise ArgumentError, "ndb address #{shown}: the path names one database: tcp://host:port/<database>" if path.include?("/")
66
+ raise ArgumentError, "ndb address #{shown}: the path and ?db= name two databases" if !path.empty? && query && path != query
67
+
68
+ database = path.empty? ? query : path
69
+
70
+ database.to_s.empty? ? nil : database
71
+ end
72
+
73
+ # address with its userinfo hidden, for error messages.
74
+ def self.redact(address)
75
+ address.sub(%r{\A([a-z][a-z0-9+.-]*://)[^/]*@}i, '\1***@')
76
+ end
77
+
78
+ private_class_method :token_of, :database_of
79
+
80
+ def initialize(path: nil, host: nil, port: nil, tls: false, token: nil, database: nil)
40
81
  if path.to_s.empty? && (host.to_s.empty? || port.nil?)
41
82
  raise ArgumentError, "ndb address needs a socket path or a host and port"
42
83
  end
@@ -45,8 +86,12 @@ module Ndb
45
86
  @host = host
46
87
  @port = port
47
88
  @tls = tls
89
+ @token = token
90
+ @database = database
48
91
  end
49
92
 
93
+ def inspect = "#<Ndb::Endpoint #{self}>"
94
+
50
95
  def unix? = !@path.to_s.empty?
51
96
 
52
97
  def tls? = @tls
data/lib/ndb/errors.rb CHANGED
@@ -140,6 +140,11 @@ module Ndb
140
140
  class UnknownGroup < StreamError; end
141
141
  class GroupExists < StreamError; end
142
142
 
143
+ # A database name that is not [a-z0-9][a-z0-9_-]{0,62}, or a read of a
144
+ # database that does not exist.
145
+ class InvalidDatabase < Error; end
146
+ class DatabaseNotFound < Error; end
147
+
143
148
  # The daemon error code -> the Ruby class.
144
149
  CODE_TO_ERROR = {
145
150
  "unauthorized" => Unauthorized,
@@ -207,7 +212,9 @@ module Ndb
207
212
  "invalid_stream_id" => InvalidStreamID,
208
213
  "stream_id_too_small" => StreamIDTooSmall,
209
214
  "unknown_group" => UnknownGroup,
210
- "group_exists" => GroupExists
215
+ "group_exists" => GroupExists,
216
+ "invalid_database" => InvalidDatabase,
217
+ "database_not_found" => DatabaseNotFound
211
218
  }.freeze
212
219
 
213
220
  def self.error_for(code, message, status)
data/lib/ndb/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ndb
4
- VERSION = "1.26.0"
4
+ VERSION = "1.27.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ndb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.26.0
4
+ version: 1.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thadeu