How to use the @firebase/util.assert function in @firebase/util

To help you get started, we’ve selected a few @firebase/util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github firebase / firebase-js-sdk / packages / database / src / core / util / NextPushId.ts View on Github external
return function(now: number) {
    const duplicateTime = now === lastPushTime;
    lastPushTime = now;

    let i;
    const timeStampChars = new Array(8);
    for (i = 7; i >= 0; i--) {
      timeStampChars[i] = PUSH_CHARS.charAt(now % 64);
      // NOTE: Can't use << here because javascript will convert to int and lose
      // the upper bits.
      now = Math.floor(now / 64);
    }
    assert(now === 0, 'Cannot push at time == 0');

    let id = timeStampChars.join('');

    if (!duplicateTime) {
      for (i = 0; i < 12; i++) {
        lastRandChars[i] = Math.floor(Math.random() * 64);
      }
    } else {
      // If the timestamp hasn't changed since last push, use the same random
      // number, except incremented by 1.
      for (i = 11; i >= 0 && lastRandChars[i] === 63; i--) {
        lastRandChars[i] = 0;
      }
      lastRandChars[i]++;
    }
    for (i = 0; i < 12; i++) {
github firebase / firebase-js-sdk / packages / database / src / core / snap / LeafNode.ts View on Github external
private compareToLeafNode_(otherLeaf: LeafNode): number {
    const otherLeafType = typeof otherLeaf.value_;
    const thisLeafType = typeof this.value_;
    const otherIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(otherLeafType);
    const thisIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(thisLeafType);
    assert(otherIndex >= 0, 'Unknown leaf type: ' + otherLeafType);
    assert(thisIndex >= 0, 'Unknown leaf type: ' + thisLeafType);
    if (otherIndex === thisIndex) {
      // Same type, compare values
      if (thisLeafType === 'object') {
        // Deferred value nodes are all equal, but we should also never get to this point...
        return 0;
      } else {
        // Note that this works because true > false, all others are number or string comparisons
        if (this.value_ < otherLeaf.value_) {
          return -1;
        } else if (this.value_ === otherLeaf.value_) {
          return 0;
        } else {
          return 1;
        }
      }
github firebase / firebase-js-sdk / packages / database / src / core / CompoundWrite.ts View on Github external
writeTree.children.inorderTraversal((childKey, childTree) => {
      if (childKey === '.priority') {
        // Apply priorities at the end so we don't update priorities for either empty nodes or forget
        // to apply priorities to empty nodes that are later filled
        assert(
          childTree.value !== null,
          'Priority writes must always be leaf nodes'
        );
        priorityWrite = childTree.value;
      } else {
        node = applySubtreeWrite(relativePath.child(childKey), childTree, node);
      }
    });
    // If there was a priority write, we only apply it if the node is not empty
github firebase / firebase-js-sdk / packages / database / src / core / operation / Merge.ts View on Github external
operationForChild(childName: string): Operation {
    if (this.path.isEmpty()) {
      const childTree = this.children.subtree(new Path(childName));
      if (childTree.isEmpty()) {
        // This child is unaffected
        return null;
      } else if (childTree.value) {
        // We have a snapshot for the child in question.  This becomes an overwrite of the child.
        return new Overwrite(this.source, Path.Empty, childTree.value);
      } else {
        // This is a merge at a deeper level
        return new Merge(this.source, Path.Empty, childTree);
      }
    } else {
      assert(
        this.path.getFront() === childName,
        "Can't get a merge for a child not on the path of the operation"
      );
      return new Merge(this.source, this.path.popFront(), this.children);
    }
  }
github firebase / firebase-js-sdk / packages / database / src / realtime / WebSocketConnection.ts View on Github external
private extractFrameCount_(data: string): string | null {
    assert(this.frames === null, 'We already have a frame buffer');
    // TODO: The server is only supposed to send up to 9999 frames (i.e. length <= 4), but that isn't being enforced
    // currently.  So allowing larger frame counts (length <= 6).  See https://app.asana.com/0/search/8688598998380/8237608042508
    if (data.length <= 6) {
      const frameCount = Number(data);
      if (!isNaN(frameCount)) {
        this.handleNewFrameCount_(frameCount);
        return null;
      }
    }
    this.handleNewFrameCount_(1);
    return data;
  }
github firebase / firebase-js-sdk / packages / database / src / core / PersistentConnection.ts View on Github external
private scheduleConnect_(timeout: number) {
    assert(
      !this.realtime_,
      "Scheduling a connect when we're already connected/ing?"
    );

    if (this.establishConnectionTimer_) {
      clearTimeout(this.establishConnectionTimer_);
    }

    // NOTE: Even when timeout is 0, it's important to do a setTimeout to work around an infuriating "Security Error" in
    // Firefox when trying to write to our long-polling iframe in some scenarios (e.g. Forge or our unit tests).

    this.establishConnectionTimer_ = setTimeout(() => {
      this.establishConnectionTimer_ = null;
      this.establishConnection_();
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
    }, Math.floor(timeout)) as any;
github firebase / firebase-js-sdk / packages / database / src / core / view / QueryParams.ts View on Github external
getLimit(): number {
    assert(this.limitSet_, 'Only valid if limit has been set');
    return this.limit_;
  }
github firebase / firebase-js-sdk / packages / database / src / core / view / ChildChangeAccumulator.ts View on Github external
trackChildChange(change: Change) {
    const type = change.type;
    const childKey = change.childName!;
    assert(
      type === Change.CHILD_ADDED ||
        type === Change.CHILD_CHANGED ||
        type === Change.CHILD_REMOVED,
      'Only child changes supported for tracking'
    );
    assert(
      childKey !== '.priority',
      'Only non-priority child changes can be tracked.'
    );
    const oldChange = this.changeMap.get(childKey);
    if (oldChange) {
      const oldType = oldChange.type;
      if (type === Change.CHILD_ADDED && oldType === Change.CHILD_REMOVED) {
        this.changeMap.set(
          childKey,
          Change.childChangedChange(
            childKey,
            change.snapshotNode,
            oldChange.snapshotNode
          )
        );
      } else if (
github firebase / firebase-js-sdk / packages / database / src / core / PersistentConnection.ts View on Github external
const sendRequestFn = function(msg: object) {
        assert(
          connection,
          "sendRequest call when we're not connected not allowed."
        );
        connection.sendRequest(msg);
      };