All files / node-coap/lib option_converter.js

95.97% Statements 119/124
85.71% Branches 30/35
100% Functions 14/14
95.9% Lines 117/122

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201                      1x 1x     1x   1x 1519x 1293x   226x     226x     1x 753x   753x 484x   269x     1x 8x 8x     1x   1x 3x     1x   1x 1x 1x   1x 1492x       1x 10x     1x 17x     1x 1x 1x 1x   1x 2x 2x     2x 2x 4x 4x   2x     1x 2x 2x 4x   2x     1x     1x 1x   1x     28x 8x 8x   20x     28x 28x     1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 145x 145x     145x     1x 182x     182x 178x 4x 2x   2x   180x   180x   178x     1x 1x 1x     69x 28x 41x 36x 5x 2x   3x     69x   68x   68x 40x 28x   28x 1x     68x    
'use strict'
 
/*
 * Copyright (c) 2013-2021 node-coap contributors.
 *
 * node-coap is licensed under an MIT +no-false-attribs license.
 * All rights not explicitly granted in the MIT license are reserved.
 * See the included LICENSE file for more details.
 */
 
// Generic toBinary and fromBinary definitions
var optionToBinaryFunctions = {}
var optionFromBinaryFunctions = {}
 
// list of options silently ignored
var ignoredOptions = {}
 
module.exports.toBinary = function(name, value) {
  if (Buffer.isBuffer(value))
    return value
 
  Iif (!optionToBinaryFunctions[name])
    throw new Error('Unknown string to Buffer converter for option: ' + name)
 
  return optionToBinaryFunctions[name](value)
}
 
module.exports.fromBinary = function(name, value) {
  var convert = optionFromBinaryFunctions[name]
 
  if (!convert)
    return value
 
  return convert(value)
}
 
var registerOption = function(name, toBinary, fromBinary) {
  optionFromBinaryFunctions[name] = fromBinary
  optionToBinaryFunctions[name] = toBinary
}
 
module.exports.registerOption = registerOption
 
var ignoreOption = function(name) {
  ignoredOptions[name] = true
}
 
module.exports.ignoreOption = ignoreOption
 
ignoreOption('Cache-Control')
ignoreOption('Content-Length')
ignoreOption('Accept-Ranges')
 
module.exports.isIgnored = function(name) {
  return !!ignoredOptions[name]
}
 
// ETag option registration
var fromString = function(result) {
  return Buffer.from(result)
}
 
var toString = function(value) {
  return value.toString()
}
 
registerOption('ETag', fromString, toString)
registerOption('Location-Path', fromString, toString)
registerOption('Location-Query', fromString, toString)
registerOption('Proxy-Uri', fromString, toString)
 
var fromUint = function(result) {
  var uint = Number(result)
  Iif (!isFinite(uint) || Math.floor(uint) !== uint || uint < 0) {
    throw TypeError('Expected uint, got ' + result)
  }
  var parts = []
  while (uint > 0) {
    parts.unshift(uint % 256)
    uint = Math.floor(uint / 256)
  }
  return Buffer.from(parts)
}
 
var toUint = function(value) {
  var result = 0
  for (var i = 0; i < value.length; ++i) {
    result = 256 * result + value[i]
  }
  return result
}
 
registerOption('Max-Age', fromUint, toUint)
 
// Content-Format and Accept options registration
var formatsString = {}
var formatsBinaries = {}
 
var registerFormat = function(name, value) {
  var bytes
 
  if (value > 255) {
    bytes = Buffer.alloc(2)
    bytes.writeUInt16BE(value, 0)
  } else {
    bytes = Buffer.of(value)
  }
 
  formatsString[name] = bytes
  formatsBinaries[value] = name
}
 
module.exports.registerFormat = registerFormat
 
// See https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#content-formats
// for a list of all registered content-formats
registerFormat('text/plain', 0)
registerFormat('application/link-format', 40)
registerFormat('application/xml', 41)
registerFormat('application/octet-stream', 42)
registerFormat('application/exi', 47)
registerFormat('application/json', 50)
registerFormat('application/json-patch+json', 51)
registerFormat('application/merge-patch+json', 52)
registerFormat('application/cbor', 60)
registerFormat('application/cwt', 61)
registerFormat('application/multipart-core', 62)
registerFormat('application/cbor-seq', 63)
registerFormat('application/cose-key', 101)
registerFormat('application/cose-key-set', 102)
registerFormat('application/senml+json', 110)
registerFormat('application/sensml+json', 111)
registerFormat('application/senml+cbor', 112)
registerFormat('application/sensml+cbor', 113)
registerFormat('application/senml-exi', 114)
registerFormat('application/sensml-exi', 115)
registerFormat('application/coap-group+json', 256)
registerFormat('application/dots+cbor', 271)
registerFormat('application/missing-blocks+cbor-seq', 272)
registerFormat('application/senml+xml', 310)
registerFormat('application/sensml+xml', 311)
registerFormat('application/senml-etch+json', 320)
registerFormat('application/senml-etch+cbor', 322)
registerFormat('application/td+json', 432)
 
var contentFormatToBinary = function(value) {
  var result = formatsString[value.split(';')[0]]
  Iif (!result)
    throw new Error('Unknown Content-Format: ' + value)
 
  return result
}
 
var contentFormatToString = function(value) {
  Iif (value.length === 0)
    return formatsBinaries[0]
 
  if (value.length === 1)
    value = value.readUInt8(0)
  else if (value.length === 2)
    value = value.readUInt16BE(0)
  else
    return null
 
  var result = formatsBinaries[value]
 
  if (!result) return value
 
  return result
}
 
registerOption('Content-Format', contentFormatToBinary, contentFormatToString)
registerOption('Accept', contentFormatToBinary, contentFormatToString)
registerOption('Observe', function(sequence) {
  var buf
 
  if (!sequence) {
    buf = Buffer.alloc(0)
  } else if (sequence <= 0xff) {
    buf = Buffer.of(sequence)
  } else if (sequence <= 0xffff) {
    buf = Buffer.of(sequence >> 8, sequence)
  } else {
    buf = Buffer.of(sequence >> 16, sequence >> 8, sequence)
  }
 
  return buf
}, function(buf) {
  var result = 0
 
  if (buf.length === 1) {
    result = buf.readUInt8(0)
  } else Iif (buf.length === 2) {
    result = buf.readUInt16BE(0)
  } else if (buf.length === 3) {
    result = (buf.readUInt8(0) << 16) | buf.readUInt16BE(1)
  }
 
  return result
})