UNPKG

diffusion

Version:

Diffusion JavaScript client

127 lines (126 loc) 4.4 kB
"use strict"; /** * @module diffusion.datatypes */ 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.BinaryImpl = void 0; var errors_1 = require("./../../../errors/errors"); var binary_delta_type_impl_1 = require("./../../data/binary/binary-delta-type-impl"); var bytes_impl_1 = require("./../../data/bytes-impl"); var function_1 = require("./../../util/function"); var uint8array_1 = require("./../../util/uint8array"); /** * Create a new BinaryImpl object from a buffer or {@link BinaryImpl}. If a * {@link BinaryImpl} is passed as argument it is simply returned. * * @param value the object to create a record from * @return the record created * @throws an error if an incompatible datatype was supplied */ function fromValue(value) { // tslint:disable-next-line:no-use-before-declare if (value instanceof BinaryImpl) { return value; } else if (uint8array_1.isUint8Array(value)) { // tslint:disable-next-line:no-use-before-declare return new BinaryImpl(value); } else { throw new errors_1.IllegalArgumentError("Unable to read Binary value from: " + value); } } /** * Implementation of {@link Binary} */ var BinaryImpl = /** @class */ (function (_super) { __extends(BinaryImpl, _super); /** * Create a new BinaryImpl instance * * @param buffer the internal buffer * @param offset the offset of the slice * @param length the number of bytes in the slice */ function BinaryImpl(buffer, offset, length) { if (offset === void 0) { offset = 0; } if (length === void 0) { length = buffer.length; } return _super.call(this, buffer, offset, length) || this; } /** * Return the name of the data type * * @return `BinaryImpl` */ BinaryImpl.toString = function () { return 'BinaryImpl'; }; /** * Create a new BinaryImpl object from a buffer or {@link BinaryImpl}. If a * {@link BinaryImpl} is passed as argument it is simply returned. * * @param value the object to create a record from * @return the record created * @throws an {@link IllegalArgumentError} if an incompatible datatype was supplied */ BinaryImpl.from = function (value) { return fromValue(value); }; /** * @inheritdoc * * @deprecated since version 6.11 */ BinaryImpl.prototype.get = function () { return Buffer.from(this.getArray()); }; /** * @inheritdoc */ BinaryImpl.prototype.getArray = function () { return this.asArray(); }; /** * @inheritdoc */ BinaryImpl.prototype.diff = function (original, type) { // tslint:disable-next-line:no-use-before-declare return BINARY_DELTA_TYPE.diff(original, this); }; /** * @inheritdoc */ BinaryImpl.prototype.apply = function (delta) { // tslint:disable-next-line:no-use-before-declare return BINARY_DELTA_TYPE.apply(this, delta); }; /** * Convert the objet to a string * * @return a string representation of the BinaryImpl */ BinaryImpl.prototype.toString = function () { return "Binary <" + this.$length + " bytes> " + uint8array_1.uint8toUtf8(this.getArray()); }; return BinaryImpl; }(bytes_impl_1.BytesImpl)); exports.BinaryImpl = BinaryImpl; /** * A unique binary delta type instance to calculate deltas of {@link BinaryImpl} */ var BINARY_DELTA_TYPE = new binary_delta_type_impl_1.BinaryDeltaTypeImpl(BinaryImpl, fromValue, function_1.identity);