How to use @firebase/util - 10 common examples

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 / validation.ts View on Github external
export const validateUrl = function(
  fnName: string,
  argumentNumber: number,
  parsedUrl: { repoInfo: RepoInfo; path: Path }
) {
  // TODO = Validate server better.
  const pathString = parsedUrl.path.toString();
  if (
    !(typeof parsedUrl.repoInfo.host === 'string') ||
    parsedUrl.repoInfo.host.length === 0 ||
    (!isValidKey(parsedUrl.repoInfo.namespace) &&
      parsedUrl.repoInfo.host.split(':')[0] !== 'localhost') ||
    (pathString.length !== 0 && !isValidRootPathString(pathString))
  ) {
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, false) +
        'must be a valid firebase URL and ' +
        'the path can\'t contain ".", "#", "$", "[", or "]".'
    );
  }
};
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 maierj / fastlane-action / node_modules / @firebase / app / dist / index.esm2017.js View on Github external
function app(name) {
        name = name || DEFAULT_ENTRY_NAME;
        if (!contains(apps, name)) {
            throw ERROR_FACTORY.create("no-app" /* NO_APP */, { appName: name });
        }
        return apps[name];
    }
    // @ts-ignore
github maierj / fastlane-action / node_modules / @firebase / app / dist / index.rn.cjs.js View on Github external
if (rawConfig === void 0) { rawConfig = {}; }
        if (typeof rawConfig !== 'object' || rawConfig === null) {
            var name_1 = rawConfig;
            rawConfig = { name: name_1 };
        }
        var config = rawConfig;
        if (config.name === undefined) {
            config.name = DEFAULT_ENTRY_NAME;
        }
        var name = config.name;
        if (typeof name !== 'string' || !name) {
            throw ERROR_FACTORY.create("bad-app-name" /* BAD_APP_NAME */, {
                appName: String(name)
            });
        }
        if (util.contains(apps, name)) {
            throw ERROR_FACTORY.create("duplicate-app" /* DUPLICATE_APP */, { appName: name });
        }
        var app = new firebaseAppImpl(options, config, namespace);
        apps[name] = app;
        return app;
    }
    /*
github maierj / fastlane-action / node_modules / @firebase / app / dist / index.esm.js View on Github external
function app(name) {
        name = name || DEFAULT_ENTRY_NAME;
        if (!contains(apps, name)) {
            throw ERROR_FACTORY.create("no-app" /* NO_APP */, { appName: name });
        }
        return apps[name];
    }
    // @ts-ignore