UNPKG

diffusion

Version:

Diffusion JavaScript client

236 lines (235 loc) 10.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.isEqualityOperator = exports.PartialJSONImpl = exports.JSONCondition = exports.NoValue = exports.NoTopic = exports.TopicValueConstraint = void 0; var errors_1 = require("./../../errors/errors"); var update_constraint_1 = require("./../../topic-update/update-constraint"); var abstract_datatype_1 = require("../data/abstract-datatype"); var datatypes_1 = require("../data/datatypes"); var json_pointer_1 = require("../data/json/json-pointer"); var require_non_null_1 = require("../util/require-non-null"); var update_constraint_2 = require("./update-constraint"); /** * Bytes that represent CBOR null value. */ var CBOR_NULL = new Uint8Array([-10]); /** * Bytes that represent CBOR true value. */ var CBOR_TRUE = new Uint8Array([-11]); /** * Bytes that represent CBOR false value. */ var CBOR_FALSE = new Uint8Array([-12]); /** * Constraint of the whole topic value. * * Renamed from BinaryValue at 6.10 when operator was introduced. */ var TopicValueConstraint = /** @class */ (function (_super) { __extends(TopicValueConstraint, _super); /** * Create a new TopicValueConstraint instance * * @param bytes the value that the topic needs to match */ function TopicValueConstraint(operator, valueType, bytes) { var _this = _super.call(this, update_constraint_2.ConstraintType.TOPIC_VALUE) || this; _this.operator = operator; _this.valueType = valueType; _this.bytes = bytes; return _this; } TopicValueConstraint.prototype.andBlockedFor = function (other) { return other instanceof update_constraint_2.NoValueConstraint || other instanceof PartialJSONImpl || other instanceof update_constraint_2.CompoundConstraint && other.andBlockedFor(this); }; return TopicValueConstraint; }(update_constraint_2.TopicConstraint)); exports.TopicValueConstraint = TopicValueConstraint; /** * A constraint requiring the path to have no topic */ var NoTopic = /** @class */ (function (_super) { __extends(NoTopic, _super); /** * Constructor */ function NoTopic() { return _super.call(this, update_constraint_2.ConstraintType.NO_TOPIC) || this; } return NoTopic; }(update_constraint_2.NoValueConstraint)); exports.NoTopic = NoTopic; /** * A constraint requiring the topic to have no value */ var NoValue = /** @class */ (function (_super) { __extends(NoValue, _super); /** * Constructor */ function NoValue() { return _super.call(this, update_constraint_2.ConstraintType.NO_VALUE) || this; } return NoValue; }(update_constraint_2.NoValueConstraint)); exports.NoValue = NoValue; /** * A condition to apply to a pointer position. */ var JSONCondition = /** @class */ (function () { /** * Constructor. */ function JSONCondition(operator, valueType, buffer) { this.operator = operator; this.valueType = valueType; this.buffer = buffer; } return JSONCondition; }()); exports.JSONCondition = JSONCondition; /** * A constraint that partially matches the current JSON topic value */ var PartialJSONImpl = /** @class */ (function (_super) { __extends(PartialJSONImpl, _super); /** * Create a new PartialJSONImpl instance * * @param withValues the values required to exist in the JSON value * @param withoutValues the JSON pointers that should hold no value */ function PartialJSONImpl(withValues, withoutValues) { if (withValues === void 0) { withValues = {}; } if (withoutValues === void 0) { withoutValues = new Set(); } var _this = _super.call(this, update_constraint_2.ConstraintType.PARTIAL_JSON) || this; _this.withValues = withValues; _this.withoutValues = withoutValues; return _this; } PartialJSONImpl.prototype.validateValue = function (operator, valueType) { if ([update_constraint_2.ConstraintValueType.BINARY, update_constraint_2.ConstraintValueType.NULL].includes(valueType)) { if (operator !== update_constraint_1.UpdateConstraintOperator.IS) { throw new errors_1.IllegalArgumentError('Invalid operator for Binary value'); } } else if (valueType === update_constraint_2.ConstraintValueType.BOOLEAN) { if (operator !== update_constraint_1.UpdateConstraintOperator.EQ) { throw new errors_1.IllegalArgumentError('Invalid operator for Boolean value'); } } else if (valueType === update_constraint_2.ConstraintValueType.STRING) { if (!isEqualityOperator(operator)) { throw new errors_1.IllegalArgumentError('Invalid operator for String value'); } } else if (![update_constraint_2.ConstraintValueType.DOUBLE, update_constraint_2.ConstraintValueType.INT64].includes(valueType)) { throw new errors_1.IllegalArgumentError("value type " + update_constraint_2.ConstraintValueType[valueType] + " " + ("is not supported with the " + update_constraint_1.UpdateConstraintOperator[operator] + " operator")); } }; PartialJSONImpl.prototype.andBlockedFor = function (other) { return other instanceof update_constraint_2.NoValueConstraint || other instanceof TopicValueConstraint || other instanceof update_constraint_2.CompoundConstraint && other.andBlockedFor(this); }; /** * Get the required JSON values * * @return the a map of JSON values that are required */ PartialJSONImpl.prototype.getWithValues = function () { return Object.assign({}, this.withValues); }; /** * Get the JSON pointer expressions that should be empty in the JSON value * * @return a set of pointer expressions */ PartialJSONImpl.prototype.getWithoutValues = function () { return new Set(this.withoutValues); }; /** * @inheritdoc */ // eslint-disable-next-line complexity PartialJSONImpl.prototype.with = function (pointer, value, operatorOrDataType, dataType) { var oldApi = operatorOrDataType === undefined || operatorOrDataType instanceof abstract_datatype_1.AbstractDataType; var operator = oldApi ? update_constraint_1.UpdateConstraintOperator.IS : operatorOrDataType; var theDataType = (oldApi ? operatorOrDataType : dataType); if (oldApi) { require_non_null_1.requireNonNull(value, 'value'); } var jsonPointer = json_pointer_1.JSONPointer.parse(require_non_null_1.requireNonNull(pointer, 'pointer')); require_non_null_1.requireNonNull(operator, 'operator'); var valueBytes; var constraintValueType; if (value === undefined || value === null) { if (!isEqualityOperator(operator)) { throw new errors_1.IllegalArgumentError('Invalid operator for null value'); } valueBytes = CBOR_NULL; constraintValueType = update_constraint_2.ConstraintValueType.NULL; } else { if (theDataType !== undefined) { constraintValueType = update_constraint_2.ConstraintValueType[theDataType.name().toUpperCase()]; valueBytes = theDataType.writeValueToArray(value); } else if (typeof value === 'boolean') { constraintValueType = update_constraint_2.ConstraintValueType.BOOLEAN; valueBytes = value ? CBOR_TRUE : CBOR_FALSE; } else { var valueType = datatypes_1.DataTypes.getByValue(value); if (valueType === undefined) { throw new errors_1.IllegalArgumentError('Datatype invalid or could not be inferred'); } constraintValueType = update_constraint_2.ConstraintValueType[valueType.name().toUpperCase()]; valueBytes = valueType.writeValueToArray(value); } this.validateValue(operator, constraintValueType); } var withProperty = {}; withProperty[jsonPointer.toString()] = new JSONCondition(operator, operator === update_constraint_1.UpdateConstraintOperator.IS ? update_constraint_2.ConstraintValueType.BINARY : constraintValueType, valueBytes); var newWithValues = Object.assign({}, this.withValues, withProperty); var newWithoutValues = new Set(this.withoutValues); newWithoutValues.delete(jsonPointer.toString()); return new PartialJSONImpl(newWithValues, newWithoutValues); }; /** * @inheritdoc */ PartialJSONImpl.prototype.without = function (pointer) { json_pointer_1.JSONPointer.parse(require_non_null_1.requireNonNull(pointer, 'pointer')); var newWithoutValues = new Set(this.withoutValues); newWithoutValues.add(pointer.toString()); return new PartialJSONImpl(this.withValues, newWithoutValues); }; return PartialJSONImpl; }(update_constraint_2.TopicConstraint)); exports.PartialJSONImpl = PartialJSONImpl; /** * Checks if an UpdateConstraintOperator is comparing equality */ function isEqualityOperator(operator) { return [update_constraint_1.UpdateConstraintOperator.IS, update_constraint_1.UpdateConstraintOperator.EQ, update_constraint_1.UpdateConstraintOperator.NE] .includes(operator); } exports.isEqualityOperator = isEqualityOperator;