borc
Version:
Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC7049).
108 lines (99 loc) • 2.5 kB
JavaScript
const constants = require('./constants')
const MT = constants.MT
const SIMPLE = constants.SIMPLE
const SYMS = constants.SYMS
/**
* A CBOR Simple Value that does not map onto a known constant.
*/
class Simple {
/**
* Creates an instance of Simple.
*
* @param {integer} value - the simple value's integer value
*/
constructor (value) {
if (typeof value !== 'number') {
throw new Error('Invalid Simple type: ' + (typeof value))
}
if ((value < 0) || (value > 255) || ((value | 0) !== value)) {
throw new Error('value must be a small positive integer: ' + value)
}
this.value = value
}
/**
* Debug string for simple value
*
* @returns {string} simple(value)
*/
toString () {
return 'simple(' + this.value + ')'
}
/**
* Debug string for simple value
*
* @returns {string} simple(value)
*/
inspect () {
return 'simple(' + this.value + ')'
}
/**
* Push the simple value onto the CBOR stream
*
* @param {cbor.Encoder} gen - The generator to push onto
* @returns {number}
*/
encodeCBOR (gen) {
return gen._pushInt(this.value, MT.SIMPLE_FLOAT)
}
/**
* Is the given object a Simple?
*
* @param {any} obj - object to test
* @returns {bool} - is it Simple?
*/
static isSimple (obj) {
return obj instanceof Simple
}
/**
* Decode from the CBOR additional information into a JavaScript value.
* If the CBOR item has no parent, return a "safe" symbol instead of
* `null` or `undefined`, so that the value can be passed through a
* stream in object mode.
*
* @param {number} val - the CBOR additional info to convert
* @param {bool} hasParent - Does the CBOR item have a parent?
* @returns {(null | undefined | boolean | symbol)} - the decoded value
*/
static decode (val, hasParent) {
if (hasParent == null) {
hasParent = true
}
switch (val) {
case SIMPLE.FALSE:
return false
case SIMPLE.TRUE:
return true
case SIMPLE.NULL:
if (hasParent) {
return null
} else {
return SYMS.NULL
}
case SIMPLE.UNDEFINED:
if (hasParent) {
return undefined
} else {
return SYMS.UNDEFINED
}
case -1:
if (!hasParent) {
throw new Error('Invalid BREAK')
}
return SYMS.BREAK
default:
return new Simple(val)
}
}
}
module.exports = Simple