rudder-sdk-js icon indicating copy to clipboard operation
rudder-sdk-js copied to clipboard

feat: set server side cookies

Open MoumitaM opened this issue 1 year ago β€’ 6 comments

PR Description

  • added new load option to enable setting cookie from server side
  • added new load option to take the base URL to set the cookie to if not same as dataplane url
  • If the option is enabled SDK will make a POST request to the API gateway with the cookie name value and settings.
  • Whenever a cookie is modified cookie setter method will be called

Linear task (optional)

https://linear.app/rudderstack/issue/SDK-1301/create-a-cookie-setter-provider-for-service

Cross Browser Tests

Please confirm you have tested for the following browsers:

  • [ ] Chrome
  • [ ] Firefox
  • [ ] IE11

Sanity Suite

  • [ ] All sanity suite test cases pass locally

Security

  • [ ] The code changed/added as part of this pull request won't create any security issues with how the software is being used.

Summary by CodeRabbit

  • New Features

    • Added server-side cookie handling capabilities.
    • Enhanced analytics tracking with new HTTP post handlers for different endpoints.
    • Introduced new configuration options for server-side cookies in analytics setup.
  • Enhancements

    • Increased size limits for various analytics modules to accommodate new features.
    • Improved error logging related to data server URL validation and cookie setting failures.
  • Bug Fixes

    • Fixed syntax in Criteo integration and streamlined configuration assignment in FacebookPixel integration.
  • Documentation

    • Updated public-facing configuration examples to demonstrate the use of server-side cookies.

MoumitaM avatar Mar 18 '24 14:03 MoumitaM

Walkthrough

The updates involve enhancing functionality related to server-side cookie management and data handling in an analytics JavaScript library. New properties and methods support server-side cookies, including configuration validation and HTTP handling. Additionally, the inclusion of workspaceId enhances data source identification, aligning with enhanced security and configuration precision.

Changes

File Path Changes
.../types/LoadOptions.ts, .../utilities/loadOptions.ts Added useServerSideCookies and dataServerUrl to handle server-side cookie configurations.
.../types/Source.ts, .../configManager/ConfigManager.ts Introduced workspaceId for better source management.
.../__fixtures__/msw.handlers.ts New HTTP POST handler for server-side cookie requests.
.../__tests__/components/configManager/ConfigManager.test.ts, .../__tests__/components/userSessionManager/UserSessionManager.test.ts Updated tests to include new configurations and methods related to server-side cookies and workspaceId.
.../src/components/configManager/util/validate.ts, .../src/components/userSessionManager/UserSessionManager.ts Added functions and methods for validating and setting server-side cookies, including error handling.
.../src/components/userSessionManager/types.ts Defined new types for managing cookie data.

πŸ‡πŸŒŸ In the meadow of code, where the data streams flow,
A rabbit hopped in, with updates in tow.
Server-side cookies, oh so sweet,
Ensuring every byte is neat.
With a hop, skip, and a commendable feat,
Our rabbit’s work is now complete! 🌼πŸͺ πŸ‡πŸŒŸ


Recent Review Details

Configuration used: CodeRabbit UI Review profile: CHILL

Commits Files that changed from the base of the PR and between a59c8d0648e3eb076823d0c8835dc422dac0c67d and 0c65220d014a7f165991d7b52ca50e038b069b6c.
Files selected for processing (10)
  • packages/analytics-js-common/src/types/LoadOptions.ts (1 hunks)
  • packages/analytics-js-common/src/types/Source.ts (1 hunks)
  • packages/analytics-js/fixtures/msw.handlers.ts (1 hunks)
  • packages/analytics-js/tests/components/configManager/ConfigManager.test.ts (1 hunks)
  • packages/analytics-js/tests/components/userSessionManager/UserSessionManager.test.ts (3 hunks)
  • packages/analytics-js/src/components/configManager/ConfigManager.ts (3 hunks)
  • packages/analytics-js/src/components/configManager/util/validate.ts (2 hunks)
  • packages/analytics-js/src/components/userSessionManager/UserSessionManager.ts (5 hunks)
  • packages/analytics-js/src/components/userSessionManager/types.ts (1 hunks)
  • packages/analytics-js/src/components/utilities/loadOptions.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • packages/analytics-js/src/components/userSessionManager/types.ts
Additional Context Used
Learnings (2)
packages/analytics-js/src/components/userSessionManager/UserSessionManager.ts (1)
User: MoumitaM
PR: rudderlabs/rudder-sdk-js#1625
File: packages/analytics-js/__tests__/components/userSessionManager/UserSessionManager.test.ts:1374-1385
Timestamp: 2024-02-22T10:46:36.393Z
Learning: Multiple cookies with the same name are not possible in web browsers; a newer cookie will overwrite an older one if they share the same name.
packages/analytics-js/__tests__/components/userSessionManager/UserSessionManager.test.ts (1)
User: MoumitaM
PR: rudderlabs/rudder-sdk-js#1625
File: packages/analytics-js/__tests__/components/userSessionManager/UserSessionManager.test.ts:1374-1385
Timestamp: 2024-02-22T10:46:36.393Z
Learning: Multiple cookies with the same name are not possible in web browsers; a newer cookie will overwrite an older one if they share the same name.
Additional comments not posted (12)
packages/analytics-js-common/src/types/Source.ts (1)

21-21: The addition of workspaceId to the Source type is correctly implemented.

packages/analytics-js/__fixtures__/msw.handlers.ts (1)

80-87: Consider adding cookie headers in the POST handler for /rsaRequest to better simulate the server-side cookie setting feature.

packages/analytics-js-common/src/types/LoadOptions.ts (1)

152-153: The addition of useServerSideCookies and dataServerUrl to the LoadOptions type is correctly implemented.

packages/analytics-js/src/components/utilities/loadOptions.ts (1)

60-64: The logic to handle the useServerSideCookies flag and validate the dataServerUrl in the normalizeLoadOptions function is correctly implemented.

packages/analytics-js/src/components/configManager/ConfigManager.ts (2)

98-104: The integration of validateAndReturnCookieServerUrl to validate dataServerUrl in the ConfigManager class is correctly implemented. Ensure that the function behaves as expected during integration tests.


174-174: The addition of workspaceId to the source object in the ConfigManager class is correctly implemented.

packages/analytics-js/__tests__/components/configManager/ConfigManager.test.ts (1)

177-177: The addition of workspaceId to the expectedSourceState object in the ConfigManager test suite is correctly implemented.

packages/analytics-js/__tests__/components/userSessionManager/UserSessionManager.test.ts (5)

23-24: The addition of imports for msw.server and HttpClient is appropriate for the new functionalities being tested, such as server-side cookie setting.


89-89: The addition of defaultHttpClient to the UserSessionManager constructor is crucial for enabling HTTP communication for server-side cookie management. Ensure that all instances of UserSessionManager across the project are updated to include this new dependency.


1390-1406: The test cases for syncValueToStorage method correctly handle scenarios where useServerSideCookies is set and not set. This ensures that the setServerSideCookie method is only called when appropriate.


1410-1507: The test cases for setServerSideCookie method are well-implemented. They check the functionality of making external requests for cookie setting, using both the default and a provided server URL. It's important to ensure that the encryption function (encrypt) is correctly implemented and tested since it's mocked here.


1377-1382: The updated test cases for getExternalAnonymousIdByCookieName method are correctly implemented to handle scenarios where the cookie does not exist and when it does. This ensures robustness in fetching external anonymous IDs.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Mar 18 '24 14:03 coderabbitai[bot]

Codecov Report

Attention: Patch coverage is 75.80645% with 15 lines in your changes are missing coverage. Please review.

Project coverage is 53.99%. Comparing base (695b229) to head (0c65220).

Files Patch % Lines
...s-js/src/components/configManager/util/validate.ts 28.57% 5 Missing :warning:
...omponents/userSessionManager/UserSessionManager.ts 86.48% 4 Missing and 1 partial :warning:
...s-js/src/components/configManager/ConfigManager.ts 33.33% 2 Missing :warning:
packages/analytics-js/src/constants/logMessages.ts 77.77% 2 Missing :warning:
...alytics-js/src/components/utilities/loadOptions.ts 66.66% 1 Missing :warning:
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1649      +/-   ##
===========================================
+ Coverage    53.92%   53.99%   +0.06%     
===========================================
  Files          461      461              
  Lines        15615    15669      +54     
  Branches      3101     3112      +11     
===========================================
+ Hits          8421     8461      +40     
- Misses        5890     5920      +30     
+ Partials      1304     1288      -16     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Mar 18 '24 14:03 codecov[bot]

size-limit report πŸ“¦

Name Size (Base) Size (Current) Size Limit Status
Common Code - No bundling 15.78 KB 15.78 KB (0%) 16.5 KB βœ…
Remote Module Federation Mapping - CDN 330 B 330 B (0%) 512 B βœ…
Remote Module Federated Plugins - CDN 5.71 KB 5.71 KB (0%) 6 KB βœ…
Core ESM - NPM 7.66 KB 7.67 KB (+0.12% β–²) 8 KB βœ…
Core CJS - NPM 22.47 KB 22.91 KB (+2% β–²) 23 KB βœ…
Core - NPM 22.56 KB 23.01 KB (+2.01% β–²) 23 KB ❌
Core Legacy - CDN 43 KB 43.44 KB (+1.04% β–²) 44 KB βœ…
Core - CDN 22.71 KB 23.17 KB (+2.03% β–²) 23.5 KB βœ…
Core (legacy build) - CDN - v1.1 31.57 KB 31.57 KB (0%) 32 KB βœ…
Core - NPM - v1.1 31.56 KB 31.56 KB (0%) 32 KB βœ…
Service Worker Module 22.64 KB 22.64 KB (0%) 24 KB βœ…
All Integrations (legacy build) - CDN 98.4 KB 98.4 KB (0%) 105 KB βœ…

github-actions[bot] avatar Mar 18 '24 14:03 github-actions[bot]

Hello! This PR has been open for 20 days without any activity. Therefore, it's considered as stale and is scheduled to be closed in 10 days. If you're still working on this, please remove the 'Stale' label or add a comment to keep it open. Thanks for your contribution!

github-actions[bot] avatar Apr 17 '24 00:04 github-actions[bot]

All the previous comments are addressed

MoumitaM avatar Apr 23 '24 12:04 MoumitaM