Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): add Size2 logic #351

Merged
merged 2 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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