Repository navigation
fix: surface the broker's Authorization Violation from subscribe() - #10
Merged
Merged
Conversation
nats.js writes CONNECT and PING separately. When the broker rejects the token it answers -ERR and closes before reading the PING; the late PING draws a TCP reset, the client never reads the -ERR, and it retries until a generic TIMEOUT about 20 s later. subscribe() now runs the handshake once by hand with CONNECT and PING in a single write, so the broker's -ERR arrives, and rejects with it immediately. Anything other than a -ERR is left to the real connect. Adds an npm test suite (node:test) with a small in-process NATS broker and runs it in CI. Fixes #6 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 18, 2026
# Conflicts: # client.js # test/fake-nats.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
subscribe()with a wrong NATS token rejected with a genericTIMEOUTafter about 20 s.The broker does answer at once:
-ERR 'Authorization Violation', then closes. But nats.js writesCONNECTandPINGas two separate writes. The broker closes after readingCONNECT, the latePINGlands on a closed socket, the kernel answers with a TCP reset, and the client never reads the-ERR(it seesECONNRESET). It then retries until its 20 s connect timeout. Checked at the protocol level against priompt v0.7.2's embedded broker:CONNECT+PINGin one write gets the-ERRevery time (8/8); split writes getECONNRESET.Fix
subscribe()first runs the handshake once by hand (checkNatsAuth), withCONNECTandPINGin a single write, so the broker reads both and its-ERRarrives. A-ERRrejects immediately:Anything else (a
PONG, a TLS broker, a network error) falls through to the normal nats.js connect, unchanged. The cost is one short extra TCP round trip persubscribe().The repo had no test suite, so this adds
npm test(node --test) with a small in-process NATS broker (test/fake-nats.js), and CI runs it. WithlossyAuthErrthe fake reproduces what nats.js sees from the real broker: a reset with no-ERR.Test evidence
Regression test before the fix (main's
client.js):After:
Real run against
priompt v0.7.2 serve -nats-token natssecret(Windows,nats://wrong@127.0.0.1:48222):A missing token (
nats://127.0.0.1:48222against a token-protected broker) now gives the same immediate error. Before, it timed out.Expected conflict with #9
#9 (replace
natswith@nats-io/transport-node) is independent of this PR. Both branch from main and both add the test harness. Whichever merges second needs a small rebase:client.js: therequire(...)line next to the newawait checkNatsAuth(...). Keep both: the check does not depend on the NATS client package. I also ran it against fix: replace deprecated nats package with @nats-io/transport-node #9's@nats-io/transport-nodev3, which loses the-ERRthe same way.test/fake-nats.js(add/add): keep this PR's version, which addslossyAuthErr.package.jsonscripts.testand.github/workflows/test.yml: identical lines.Fixes #6
🤖 Generated with Claude Code