Skip to content

Commit

Permalink
feat(server): add Size2 logic (#351)
Browse files Browse the repository at this point in the history
* feat(server): add Size2 logic

* chore: bump version to 1.2.0
  • Loading branch information
JKRhb authored Oct 26, 2022
1 parent 131db1a commit ee8762c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,11 @@ class OutMessage extends OutgoingMessage {
}
this.setOption('Block2', block2)
this.setOption('ETag', _toETag(payload))
const size2 = getOption(this._request.options, 'Size2')

if (size2 === 0) {
this.setOption('Size2', payload.length)
}

// cache it
if (this._request.token != null && this._request.token.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coap",
"version": "1.1.0",
"version": "1.2.0",
"description": "A CoAP library for node modelled after 'http'",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
18 changes: 17 additions & 1 deletion test/blockwise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { nextPort } from './common'
import { createServer, request } from '../index'
import { createServer, IncomingMessage, request } from '../index'
import { generate, Packet, parse } from 'coap-packet'
import { getOption, parseBlock2 } from '../lib/helpers'
import { generateBlockOption, parseBlockOption, exponentToByteSize, byteSizeToExponent } from '../lib/block'
Expand Down Expand Up @@ -363,6 +363,22 @@ describe('blockwise2', function () {

parallelBlock2Test(done, checkNothing, checkBlock2Payload, checkNormalRespPayload)
})

it.only('should support the Size2 option', function (done) {
request({
port
})
.setOption('Size2', 0)
.on('response', (res: IncomingMessage) => {
const size2 = res.headers.Size2
expect(size2).to.eql(payload.length)
setImmediate(done)
})
.end()
server.on('request', (req, res) => {
res.end(payload)
})
})
})

describe('blockwise1', () => {
Expand Down

0 comments on commit ee8762c

Please sign in to comment.