UNPKG

diffusion

Version:

Diffusion JavaScript client

84 lines (83 loc) 3.23 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.BinaryDeltaImpl = void 0; var tokeniser_1 = require("./../../cbor/tokeniser"); var buffer_slice_1 = require("./../../data/buffer-slice"); var uint8array_1 = require("./../../util/uint8array"); /** * Implementation of the {@link BinaryDelta} interface. */ var BinaryDeltaImpl = /** @class */ (function (_super) { __extends(BinaryDeltaImpl, _super); /** * Create a new BinaryDeltaImpl * * @param buffer the buffer that contains the delta as CBOR data * @param offset the offset of the data in the buffer representing the delta * @param length the number of the data in the buffer representing the delta */ function BinaryDeltaImpl(buffer, offset, length) { if (offset === void 0) { offset = 0; } if (length === void 0) { length = buffer.length; } return _super.call(this, buffer, offset, length) || this; } /** * Parse the delta as CBOR tokens and let the visitor handle them * @throws an {@link InvalidDataError} if there was an error parsing the next token * * @param visitor the visitor to handle the tokens */ BinaryDeltaImpl.prototype.visit = function (visitor) { if (this.hasChanges()) { var tokeniser = new tokeniser_1.Tokeniser(this.$buffer, this.$offset, this.$length); while (tokeniser.hasRemaining()) { var token = tokeniser.nextToken(); var isEnd = false; if (uint8array_1.isUint8Array(token.value)) { isEnd = !visitor.insert(token.value); } else { var start = token.value; var end = tokeniser.nextToken().value; isEnd = !visitor.match(start, end); } if (isEnd) { visitor.end(); return; } } visitor.end(); } else { visitor.noChange(); } }; /** * Check if the binary delta has any changes * * @return `true` if the binary delta contains any data */ BinaryDeltaImpl.prototype.hasChanges = function () { return this.$length !== 1; }; return BinaryDeltaImpl; }(buffer_slice_1.BufferSlice)); exports.BinaryDeltaImpl = BinaryDeltaImpl;