diff --git a/src/Config.php b/src/Config.php index 5dd243a..e8f50c1 100644 --- a/src/Config.php +++ b/src/Config.php @@ -15,6 +15,7 @@ private const int DEFAULT_CONNECTION_TIMEOUT = 10; private const int DEFAULT_PING_INTERVAL = 10000; private const int DEFAULT_MAX_PINGS = 5; + private const string DEFAULT_CLIENT_NAME = 'thesis/nats'; /** @var non-empty-string */ public string $version; @@ -29,6 +30,7 @@ * @param ?positive-int $ping in milliseconds * @param positive-int $maxPings the maximum number of pings that we have not received a response to, after which the connection to the server will be closed * @param ?non-empty-string $jetStreamDomain + * @param non-empty-string $clientName */ public function __construct( public array $urls = [self::DEFAULT_URL], @@ -48,6 +50,7 @@ public function __construct( public ?int $ping = self::DEFAULT_PING_INTERVAL, public int $maxPings = self::DEFAULT_MAX_PINGS, public ?string $jetStreamDomain = null, + public string $clientName = self::DEFAULT_CLIENT_NAME, ) { $this->version = '0.1.x'; // TODO: replace with actual version. } @@ -151,6 +154,11 @@ public static function fromURI(#[\SensitiveParameter] string $uri): self $nkey = $query['nkey']; } + $clientName = self::DEFAULT_CLIENT_NAME; + if (isset($query['client_name']) && \is_string($query['client_name']) && $query['client_name'] !== '') { + $clientName = $query['client_name']; + } + return new self( urls: $urls, verbose: $verbose, @@ -165,6 +173,7 @@ public static function fromURI(#[\SensitiveParameter] string $uri): self ping: $ping, maxPings: $maxPings, jetStreamDomain: $jetStreamDomain, + clientName: $clientName, ); } @@ -183,6 +192,7 @@ public static function fromURI(#[\SensitiveParameter] string $uri): self * ping?: positive-int, * max_pings?: positive-int, * jetstream_domain?: non-empty-string, + * client_name?: non-empty-string, * } $options */ public static function fromArray(#[\SensitiveParameter] array $options): self @@ -201,6 +211,7 @@ public static function fromArray(#[\SensitiveParameter] array $options): self ping: $options['ping'] ?? self::DEFAULT_PING_INTERVAL, maxPings: $options['max_pings'] ?? self::DEFAULT_MAX_PINGS, jetStreamDomain: $options['jetstream_domain'] ?? null, + clientName: $options['client_name'] ?? self::DEFAULT_CLIENT_NAME, ); } } diff --git a/src/Internal/Connection/SocketConnection.php b/src/Internal/Connection/SocketConnection.php index d266888..4cb7363 100644 --- a/src/Internal/Connection/SocketConnection.php +++ b/src/Internal/Connection/SocketConnection.php @@ -70,7 +70,7 @@ public function startup(): void verbose: $this->config->verbose, pedantic: $this->config->pedantic, tlsRequired: false, - name: 'thesis/nats', + name: $this->config->clientName, version: $this->config->version, user: $this->config->user, pass: $this->config->password, diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 8db413d..5ebfe52 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -40,6 +40,18 @@ final class ConfigTest extends TestCase ), ], )] + #[TestWith( + [ + 'tcp://admin:secret@127.0.0.1:4222?verbose=false&pedantic=true&client_name=prod', + new Config( + verbose: false, + pedantic: true, + user: 'admin', + password: 'secret', + clientName: 'prod', + ), + ], + )] #[TestWith( [ 'tcp://admin:secret@127.0.0.1:4222,127.0.0.1:4223?verbose=false&pedantic=true',