Skip to content

Commit

Permalink
Merge 35f3459 into 784ab9a
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 6, 2023
2 parents 784ab9a + 35f3459 commit fe44531
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
8 changes: 8 additions & 0 deletions lib/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ class Agent extends EventEmitter {
req.setOption('Proxy-Uri', url.proxyUri)
}

if (url.accept != null) {
req.setOption('Accept', url.accept)
}

if (url.contentFormat != null) {
req.setOption('Content-Format', url.contentFormat)
}

req.sender.on('error', req.emit.bind(req, 'error'))

req.sender.on('sending', () => {
Expand Down
28 changes: 19 additions & 9 deletions lib/option_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,7 @@ const formatsBinaries = {}
* @param value The numeric code of the Content-Format.
*/
export function registerFormat (name: string, value: number): void {
let bytes: Buffer

if (value > 255) {
bytes = Buffer.alloc(2)
bytes.writeUInt16BE(value, 0)
} else {
bytes = Buffer.of(value)
}
const bytes = numberToBuffer(value)

formatsString[name] = bytes
formatsBinaries[value] = name
Expand Down Expand Up @@ -173,7 +166,11 @@ for (const [name, value] of Object.entries(supportedContentFormats)) {
registerFormat(name, value)
}

function contentFormatToBinary (value: string): Buffer {
function contentFormatToBinary (value: string | number): Buffer {
if (typeof value === 'number') {
return numberToBuffer(value)
}

if (formatsString[value] != null) {
return formatsString[value]
}
Expand Down Expand Up @@ -238,3 +235,16 @@ registerOption('Observe', (sequence: number) => {

return result
})

function numberToBuffer (value: number): Buffer {
let buffer: Buffer

if (value > 255) {
buffer = Buffer.alloc(2)
buffer.writeUInt16BE(value, 0)
} else {
buffer = Buffer.of(value)
}

return buffer
}
2 changes: 2 additions & 0 deletions models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export interface CoapRequestParams {
multicastTimeout?: number
retrySend?: number
token?: Buffer
contentFormat?: string | number
accept?: string | number
}

export interface CoapServerOptions {
Expand Down
24 changes: 24 additions & 0 deletions test/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,30 @@ describe('request', function () {
})
})

it('should be able to include accept and Content-Format parameters', function (done) {
const req = request({
port,
accept: 'application/cbor',
contentFormat: 432
})

if (server == null) {
return
}
server.on('message', (msg) => {
const packet = parse(msg)

expect(packet.options[0].name).to.eql('Content-Format')
expect(packet.options[0].value).to.eql(Buffer.of(0x01, 0xb0))
expect(packet.options[1].name).to.eql('Accept')
expect(packet.options[1].value).to.eql(Buffer.of(60))

done()
})

req.end()
})

it('should overwrite the option', function (done) {
const req = request({
port
Expand Down

0 comments on commit fe44531

Please sign in to comment.