From b041be3c1092ac9b52e2ce167278b3bf3f7c293b Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Thu, 21 Dec 2017 18:33:17 +0100 Subject: [PATCH] return the complete payload to listeners instead of the last fragment --- build/CoapClient.js | 9 +++++++-- src/CoapClient.ts | 8 ++++++-- src/Message.test.ts | 4 +++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/build/CoapClient.js b/build/CoapClient.js index d9ab72ce..7588ef28 100644 --- a/build/CoapClient.js +++ b/build/CoapClient.js @@ -561,7 +561,12 @@ class CoapClient { // TODO: we might have to check if we got the correct fragment request.partialResponse.payload = Buffer.concat([request.partialResponse.payload, coapMsg.payload]); } - if (!blockOption.isLastBlock) { + if (blockOption.isLastBlock) { + // override the message payload with the assembled partial payload + // so the full payload gets returned to the listeners + coapMsg.payload = request.partialResponse.payload; + } + else { CoapClient.requestNextBlock(request); responseIsComplete = false; } @@ -659,7 +664,7 @@ class CoapClient { // and continue working off the queue when it drops request.on("concurrencyChanged", (req) => { debug(`request 0x${message.messageId.toString(16)}: concurrency changed => ${req.concurrency}`); - if (request.concurrency === 0) + if (req.concurrency === 0) CoapClient.workOffSendQueue(); }); } diff --git a/src/CoapClient.ts b/src/CoapClient.ts index ffb46c58..afbb8256 100644 --- a/src/CoapClient.ts +++ b/src/CoapClient.ts @@ -701,7 +701,11 @@ export class CoapClient { // TODO: we might have to check if we got the correct fragment request.partialResponse.payload = Buffer.concat([request.partialResponse.payload, coapMsg.payload]); } - if (!blockOption.isLastBlock) { + if (blockOption.isLastBlock) { + // override the message payload with the assembled partial payload + // so the full payload gets returned to the listeners + coapMsg.payload = request.partialResponse.payload; + } else { CoapClient.requestNextBlock(request); responseIsComplete = false; } @@ -830,7 +834,7 @@ export class CoapClient { // and continue working off the queue when it drops request.on("concurrencyChanged", (req: PendingRequest) => { debug(`request 0x${message.messageId.toString(16)}: concurrency changed => ${req.concurrency}`); - if (request.concurrency === 0) CoapClient.workOffSendQueue(); + if (req.concurrency === 0) CoapClient.workOffSendQueue(); }); } diff --git a/src/Message.test.ts b/src/Message.test.ts index a1a3e3b9..0f157c94 100644 --- a/src/Message.test.ts +++ b/src/Message.test.ts @@ -108,10 +108,12 @@ describe.only("blockwise tests =>", () => { // limit response size coap.setDefaultRequestOptions({ - preferredBlockSize: 16, + preferredBlockSize: 64, }); const resp = await coap.request(`${requestBase}15011/15012`, "get"); + console.log("got complete payload:"); + console.log(resp.payload.toString("utf8")); coap.reset(); });