2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const assert = require('assert-plus')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const Attribute = require('./attribute')
|
2019-08-27 21:11:49 +00:00
|
|
|
// var Protocol = require('./protocol')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- API
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function Change (options) {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (options) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.object(options)
|
|
|
|
assert.optionalString(options.operation)
|
2011-08-04 20:32:01 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
options = {}
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this._modification = false
|
|
|
|
this.operation = options.operation || options.type || 'add'
|
|
|
|
this.modification = options.modification || {}
|
2015-10-31 14:37:58 +00:00
|
|
|
}
|
|
|
|
Object.defineProperties(Change.prototype, {
|
|
|
|
operation: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getOperation () {
|
2015-10-31 14:37:58 +00:00
|
|
|
switch (this._operation) {
|
2019-08-27 21:11:49 +00:00
|
|
|
case 0x00: return 'add'
|
|
|
|
case 0x01: return 'delete'
|
|
|
|
case 0x02: return 'replace'
|
|
|
|
default:
|
|
|
|
throw new Error('0x' + this._operation.toString(16) + ' is invalid')
|
2015-10-31 14:37:58 +00:00
|
|
|
}
|
|
|
|
},
|
2019-08-27 21:11:49 +00:00
|
|
|
set: function setOperation (val) {
|
|
|
|
assert.string(val)
|
2015-10-31 14:37:58 +00:00
|
|
|
switch (val.toLowerCase()) {
|
2019-08-27 21:11:49 +00:00
|
|
|
case 'add':
|
|
|
|
this._operation = 0x00
|
|
|
|
break
|
|
|
|
case 'delete':
|
|
|
|
this._operation = 0x01
|
|
|
|
break
|
|
|
|
case 'replace':
|
|
|
|
this._operation = 0x02
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw new Error('Invalid operation type: 0x' + val.toString(16))
|
2015-10-31 14:37:58 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
modification: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getModification () {
|
|
|
|
return this._modification
|
2015-10-31 14:37:58 +00:00
|
|
|
},
|
2019-08-27 21:11:49 +00:00
|
|
|
set: function setModification (val) {
|
2015-10-31 14:37:58 +00:00
|
|
|
if (Attribute.isAttribute(val)) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this._modification = val
|
|
|
|
return
|
2015-10-31 14:37:58 +00:00
|
|
|
}
|
|
|
|
// Does it have an attribute-like structure
|
2019-08-27 21:11:49 +00:00
|
|
|
if (Object.keys(val).length === 2 &&
|
2015-10-31 14:37:58 +00:00
|
|
|
typeof (val.type) === 'string' &&
|
|
|
|
Array.isArray(val.vals)) {
|
|
|
|
this._modification = new Attribute({
|
|
|
|
type: val.type,
|
|
|
|
vals: val.vals
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
return
|
2015-10-31 14:37:58 +00:00
|
|
|
}
|
2012-02-18 08:15:52 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const keys = Object.keys(val)
|
2015-10-31 14:37:58 +00:00
|
|
|
if (keys.length > 1) {
|
2019-08-27 21:11:49 +00:00
|
|
|
throw new Error('Only one attribute per Change allowed')
|
2015-10-31 14:37:58 +00:00
|
|
|
} else if (keys.length === 0) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return
|
2015-10-31 14:37:58 +00:00
|
|
|
}
|
2011-11-08 21:31:00 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const k = keys[0]
|
|
|
|
const _attr = new Attribute({ type: k })
|
2015-10-31 14:37:58 +00:00
|
|
|
if (Array.isArray(val[k])) {
|
|
|
|
val[k].forEach(function (v) {
|
2019-08-27 21:11:49 +00:00
|
|
|
_attr.addValue(v.toString())
|
|
|
|
})
|
2019-09-06 10:27:37 +00:00
|
|
|
} else if (Buffer.isBuffer(val[k])) {
|
|
|
|
_attr.addValue(val[k])
|
2019-08-26 06:22:26 +00:00
|
|
|
} else if (val[k] !== undefined && val[k] !== null) {
|
2019-08-27 21:11:49 +00:00
|
|
|
_attr.addValue(val[k].toString())
|
2011-08-04 21:25:12 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
this._modification = _attr
|
2015-10-31 14:37:58 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
json: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getJSON () {
|
2015-10-31 14:37:58 +00:00
|
|
|
return {
|
|
|
|
operation: this.operation,
|
|
|
|
modification: this._modification ? this._modification.json : {}
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2015-10-31 14:37:58 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Change.isChange = function isChange (change) {
|
2014-06-24 19:14:17 +00:00
|
|
|
if (!change || typeof (change) !== 'object') {
|
2019-08-27 21:11:49 +00:00
|
|
|
return false
|
2014-06-24 19:14:17 +00:00
|
|
|
}
|
|
|
|
if ((change instanceof Change) ||
|
|
|
|
((typeof (change.toBer) === 'function') &&
|
|
|
|
(change.modification !== undefined) &&
|
|
|
|
(change.operation !== undefined))) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
2014-06-24 19:14:17 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Change.compare = function (a, b) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!Change.isChange(a) || !Change.isChange(b)) { throw new TypeError('can only compare Changes') }
|
2011-08-08 15:03:00 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (a.operation < b.operation) { return -1 }
|
|
|
|
if (a.operation > b.operation) { return 1 }
|
2011-08-08 15:03:00 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return Attribute.compare(a.modification, b.modification)
|
|
|
|
}
|
2011-08-08 15:03:00 +00:00
|
|
|
|
2014-07-16 19:53:13 +00:00
|
|
|
/**
|
|
|
|
* Apply a Change to properties of an object.
|
|
|
|
*
|
|
|
|
* @param {Object} change the change to apply.
|
|
|
|
* @param {Object} obj the object to apply it to.
|
|
|
|
* @param {Boolean} scalar convert single-item arrays to scalars. Default: false
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
Change.apply = function apply (change, obj, scalar) {
|
|
|
|
assert.string(change.operation)
|
|
|
|
assert.string(change.modification.type)
|
|
|
|
assert.ok(Array.isArray(change.modification.vals))
|
|
|
|
assert.object(obj)
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const type = change.modification.type
|
|
|
|
const vals = change.modification.vals
|
|
|
|
let data = obj[type]
|
2014-07-16 19:53:13 +00:00
|
|
|
if (data !== undefined) {
|
|
|
|
if (!Array.isArray(data)) {
|
2019-08-27 21:11:49 +00:00
|
|
|
data = [data]
|
2014-07-16 19:53:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
data = []
|
2014-07-16 19:53:13 +00:00
|
|
|
}
|
|
|
|
switch (change.operation) {
|
2019-08-27 21:11:49 +00:00
|
|
|
case 'replace':
|
|
|
|
if (vals.length === 0) {
|
2014-07-16 19:53:13 +00:00
|
|
|
// replace empty is a delete
|
2019-08-27 21:11:49 +00:00
|
|
|
delete obj[type]
|
|
|
|
return obj
|
|
|
|
} else {
|
|
|
|
data = vals
|
|
|
|
}
|
|
|
|
break
|
2020-10-31 21:07:32 +00:00
|
|
|
case 'add': {
|
2014-07-16 19:53:13 +00:00
|
|
|
// add only new unique entries
|
2020-10-31 21:07:32 +00:00
|
|
|
const newValues = vals.filter(function (entry) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return (data.indexOf(entry) === -1)
|
|
|
|
})
|
|
|
|
data = data.concat(newValues)
|
|
|
|
break
|
2020-10-31 21:07:32 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
case 'delete':
|
|
|
|
data = data.filter(function (entry) {
|
|
|
|
return (vals.indexOf(entry) === -1)
|
|
|
|
})
|
|
|
|
if (data.length === 0) {
|
2014-07-16 19:53:13 +00:00
|
|
|
// Erase the attribute if empty
|
2019-08-27 21:11:49 +00:00
|
|
|
delete obj[type]
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
2014-07-16 19:53:13 +00:00
|
|
|
}
|
|
|
|
if (scalar && data.length === 1) {
|
|
|
|
// store single-value outputs as scalars, if requested
|
2019-08-27 21:11:49 +00:00
|
|
|
obj[type] = data[0]
|
2014-07-16 19:53:13 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj[type] = data
|
2014-07-16 19:53:13 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return obj
|
|
|
|
}
|
2011-08-08 15:03:00 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Change.prototype.parse = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.readSequence()
|
|
|
|
this._operation = ber.readEnumeration()
|
|
|
|
this._modification = new Attribute()
|
|
|
|
this._modification.parse(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Change.prototype.toBer = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.startSequence()
|
|
|
|
ber.writeEnumeration(this._operation)
|
|
|
|
ber = this._modification.toBer(ber)
|
|
|
|
ber.endSequence()
|
2015-10-31 14:37:58 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return ber
|
|
|
|
}
|
2015-10-31 14:37:58 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Exports
|
2015-10-31 14:37:58 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
module.exports = Change
|