Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make firestore-counter nodejs safe #55

Closed
joemanfoo opened this issue Oct 20, 2019 · 1 comment
Closed

Make firestore-counter nodejs safe #55

joemanfoo opened this issue Oct 20, 2019 · 1 comment
Labels
type: feature request New feature or request

Comments

@joemanfoo
Copy link

[READ] Step 1: Are you in the right place?

Issues filed here should be about a feature request for a specific extension in this repository. To file a feature request that affects multiple extensions or the Firebase Extensions platform, please reach out to
Firebase support directly.

[REQUIRED] Step 2: Extension name

This feature request is for extension: firestore-counter

What feature would you like to see?

The current web client code is not safe for use in nodejs cloud function environment due to the reliance on document, specifically, document.cooke for the storage of the shard id.

How would you use it?

var sharded = require('sharded-counter')
...
I would expect that other folks have need to be able to update counters in a more secure fashion rather than exposed on the client. In my case, updating inventory counts.

Possible solution

I've already tested the below as a possible solution

change /extensions/firestore-counter/clients/web/src/index.ts to:

  function getShardId(cookie: string): string {
    const shardId = uuid.v4();
    // make node env friendly...
    if (typeof document !== 'undefined') {
      const result = new RegExp(
        "(?:^|; )" + encodeURIComponent(cookie) + "=([^;]*)"
      ).exec(document.cookie);
      if (result) return result[1];

      const date = new Date();
      date.setTime(date.getTime() + 30 * 24 * 60 * 60 * 1000);
      const expires = "; expires=" + date.toUTCString();

      document.cookie =
        encodeURIComponent(cookie) + "=" + shardId + expires + "; path=/";
    }
    return shardId;
  }

change: /extensions/firestore-counter/clients/web/webpack.config.js:

change: libraryTarget: 'var',
to:

   libraryTarget: 'umd',
   globalObject: 'this',

I can make the above as a PR since I've already done this locally for my testing to use.
I wanted to ask about this before just submitting a PR since you all might have a really good reason not to have a node friendly option that I'm not aware of - for example is there a terrible thing not to re-use shardId's?

@Ehesp
Copy link
Member

Ehesp commented Nov 21, 2019

Hello, thanks for this. We are tracking this feature request over on #28

@Ehesp Ehesp closed this as completed Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants