Skip to content

Conversation

@stoprocent
Copy link
Contributor

Problem

The node-coap client was incorrectly handling Block1 transfers when servers send non-piggybacked responses. Specifically, when a server:

  1. Sends an ACK without Block1 option
  2. Then sends a separate CON/NON message with Block1 response

The client would prematurely complete the Block1 transfer because it only processed Block1 options on ACK messages.

Root Cause

The condition if (block1 != null && packet.ack) in lib/agent.ts was too restrictive. It only processed Block1 options when the packet was an ACK, but RFC 7959 allows Block1 responses to be sent as separate messages (non-piggybacked).

Solution

Changed the condition to if (block1 != null) to process Block1 options regardless of message type. This handles:

  • Piggybacked responses: ACK with Block1 option
  • Non-piggybacked responses: Separate CON/NON messages with Block1 option

RFC Compliance

According to RFC 7959 (Block Transfer), Block1 responses can be sent either:

  • Piggybacked on ACK messages
  • As separate confirmable or non-confirmable messages

The fix ensures compliance with both approaches.

Testing

  • Verified the fix handles both piggybacked and non-piggybacked Block1 responses
  • Maintains backward compatibility with existing ACK-based Block1 handling
  • No breaking changes to the public API

Files Changed

  • lib/agent.ts: Modified Block1 processing condition to handle all message types

This fix resolves issues when communicating with third-party CoAP servers that implement non-piggybacked Block1 responses.

Fix Block1 transfer completion when server sends non-piggybacked responses.
Previously, only ACK messages with Block1 options were processed for Block1
transfers, causing premature completion when servers send separate CON/NON
messages with Block1 responses after sending ACK.

RFC 7959 allows Block1 responses to be sent as separate messages (non-piggybacked)
and these should be processed regardless of whether they are confirmable or not.
@Apollon77
Copy link
Collaborator

Apollon77 commented Jul 14, 2025

Thanks @stoprocent :-) (long time no see ...)

LGTM.

@JKRhb WDYT? Updating deps and release after merge?

@coveralls
Copy link

coveralls commented Jul 14, 2025

Pull Request Test Coverage Report for Build 16274004738

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+1.1%) to 92.743%

Totals Coverage Status
Change from base Build 12888787371: 1.1%
Covered Lines: 2920
Relevant Lines: 3103

💛 - Coveralls

1 similar comment
@coveralls
Copy link

Pull Request Test Coverage Report for Build 16274004738

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+1.1%) to 92.743%

Totals Coverage Status
Change from base Build 12888787371: 1.1%
Covered Lines: 2920
Relevant Lines: 3103

💛 - Coveralls

@stoprocent
Copy link
Contributor Author

Hey @JKRhb
Can you have a look ?
cheers

Copy link
Member

@JKRhb JKRhb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stoprocent, thank you for your comprehensive PR! LGTM :)

@JKRhb JKRhb merged commit e9a8082 into coapjs:master Jul 15, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants