Skip to content

Commit

Permalink
chore(release): pull main into develop post release v3.14.0 (#1761)
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs authored Jun 25, 2024
2 parents 2c2f549 + b147c9d commit 7a8ea30
Show file tree
Hide file tree
Showing 72 changed files with 1,176 additions and 297 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"cancelAnimationFrame",
"navigator.sendBeacon",
"Uint8Array",
"Set"
"Set",
"atob"
]
},
"env": {
Expand Down
2 changes: 1 addition & 1 deletion jest.preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = {
},
testEnvironmentOptions: {
...nxPreset.testEnvironmentOptions,
url: 'http://www.test-host.com',
url: 'https://www.test-host.com',
runScripts: 'dangerously',
resources: 'usable',
},
Expand Down
26 changes: 20 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rudderstack/analytics-js-monorepo",
"version": "3.13.0",
"version": "3.14.0",
"private": true,
"description": "Monorepo for RudderStack Analytics JS SDK",
"workspaces": [
Expand Down
17 changes: 17 additions & 0 deletions packages/analytics-js-cookies/.jscpd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"threshold": 5,
"reporters": ["html", "console"],
"pattern": "**/*",
"ignore": [
"**/__tests__/**",
"**/__mocks__/**",
"**/__fixtures__/**",
"**/node_modules/**",
"*.test.*"
],
"absolute": true,
"gitignore": true,
"output": "./reports/",
"mode": "strict",
"format": ["typescript", "tsx"]
}
11 changes: 11 additions & 0 deletions packages/analytics-js-cookies/.size-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* DO NOT EDIT the size limits configured in this file.
* Should you update them, please contact CODEOWNERS.
*/
module.exports = [
{
name: 'Cookies Utilities',
path: 'dist/**/*.js',
limit: '2 KiB',
},
];
17 changes: 17 additions & 0 deletions packages/analytics-js-cookies/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

## 0.1.0 (2024-06-25)

### Dependency Updates

* `@rudderstack/analytics-js-common` updated to version `0.1.0`

### Features

* create new package for cookie utilities ([#1759](https://github.com/rudderlabs/rudder-sdk-js/issues/1759)) ([92826a4](https://github.com/rudderlabs/rudder-sdk-js/commit/92826a4f2e87dd2d0d755016592e36e4708f34d4))

# Changelog

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
13 changes: 13 additions & 0 deletions packages/analytics-js-cookies/CHANGELOG_LATEST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## 0.1.0 (2024-06-25)

### Dependency Updates

* `@rudderstack/analytics-js-common` updated to version `0.1.0`

### Features

* create new package for cookie utilities ([#1759](https://github.com/rudderlabs/rudder-sdk-js/issues/1759)) ([92826a4](https://github.com/rudderlabs/rudder-sdk-js/commit/92826a4f2e87dd2d0d755016592e36e4708f34d4))

# Changelog

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
21 changes: 21 additions & 0 deletions packages/analytics-js-cookies/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 RudderStack

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions packages/analytics-js-cookies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
## [![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](https://www.typescriptlang.org/)

<p align="center">
<a href="https://rudderstack.com/">
<img alt="RudderStack" width="512" src="https://raw.githubusercontent.com/rudderlabs/rudder-sdk-js/develop/assets/rs-logo-full-light.jpg">
</a>
<br />
<caption>The Customer Data Platform for Developers</caption>
</p>
<p align="center">
<b>
<a href="https://rudderstack.com">Website</a>
·
<a href="https://rudderstack.com/docs/stream-sources/rudderstack-sdk-integration-guides/rudderstack-javascript-sdk/">Documentation</a>
·
<a href="https://rudderstack.com/join-rudderstack-slack-community">Community Slack</a>
</b>
</p>

---

# @rudderstack/analytics-js-cookies

RudderStack JavaScript SDK utility for cookies.

## APIs

### `getDecryptedCookie`

This function decrypts and returns the RudderStack JavaScript SDK cookie values.

The return type is either a `string` or an `object` as some cookies like user ID, anonymous user ID have string values while user traits are objects.

It returns `null` in either of the following scenarios:

- If the cookie is not present.
- If the cookie is not properly encrypted.
- It only decrypts the cookies that are created by the RudderStack JavaScript SDK encryption version "v3".
- If the decrypted cookie value is not a valid JSON string.
- If the provided cookie name is not a valid RudderStack JavaScript SDK cookie name.

> If unencrypted, the cookie value will be returned as is.
> Any errors during decryption are swallowed by the function, returning `null`.
The following are the available cookie key exports:
- `userIdKey`: The key for the user ID cookie.
- `userTraitsKey`: The key for the user traits cookie.
- `anonymousUserIdKey`: The key for the anonymous user ID cookie.
- `groupIdKey`: The key for the group ID cookie.
- `groupTraitsKey`: The key for the group traits cookie.
- `pageInitialReferrerKey`: The key for the page initial referrer cookie.
- `pageInitialReferringDomainKey`: The key for the page initial referring domain cookie.
- `sessionInfoKey`: The key for the session ID cookie.
- `authTokenKey`: The key for the auth token cookie.


```javascript
import {
getDecryptedCookie,
anonymousUserIdKey,
userTraitsKey,
} from '@rudderstack/analytics-js-cookies';

const anonymousId = getDecryptedCookie(anonymousUserIdKey);
console.log('Anonymous User ID: ', anonymousId);
// Output: Anonymous User ID: 2c5b6d48-ea90-43a2-a2f6-457d27f90328

const userTraits = getDecryptedCookie(userTraitsKey);
console.log('User Traits: ', userTraits);
// Output: User Traits: {"email":"[email protected]","name":"John Doe"}

const invalidCookie = getDecryptedCookie('invalid-cookie-name');
console.log('Invalid Cookie: ', invalidCookie);
// Output: Invalid Cookie: null
```

> For detailed documentation on the RudderStack JavaScript SDK, click [**here**](https://www.rudderstack.com/docs/sources/event-streams/sdks/rudderstack-javascript-sdk/).
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { cookie } from '../../../../src/services/StoreManager/component-cookie';
import type { ILogger } from '@rudderstack/analytics-js-common/types/Logger';
import { cookie } from '../../src/component-cookie';

class MockLogger implements ILogger {
error = jest.fn();
}

const mockLoggerInstance = new MockLogger();

beforeEach(() => {
const allCookies = cookie();
Object.keys(allCookies).forEach(cookieName => {
cookie(cookieName, null);
});
});

// TODO: clean up structure here, clean up cookie before each test
describe('cookie(name, value)', () => {
describe('cookies', () => {
it('should set a cookie', () => {
cookie('name', 'tobi');
expect('tobi').toEqual(cookie('name'));
Expand All @@ -23,7 +37,29 @@ describe('cookie(name, value)', () => {
it('should ignore URIError', () => {
cookie('bad', '%');
cookie('bad', null);
expect(undefined).toEqual(cookie('bad'));

cookie('bad', '\ud83d', undefined, mockLoggerInstance);
expect(mockLoggerInstance.error).toHaveBeenCalledTimes(1);
expect(mockLoggerInstance.error).toHaveBeenNthCalledWith(
1,
'Failed to encode the cookie data.',
expect.any(Error),
);

expect(cookie('bad')).toEqual('undefined');
});

it('should return undefined if the cookies are not encoded properly', () => {
document.cookie = 'name=tobi%ferret';
expect(cookie('name')).toEqual(undefined);

// delete cookie
document.cookie = 'name=; expires=Thu, 01 Jan 1970 00:00:00 UTC';
});

it('should set cookie as per the options', () => {
cookie('name', 'tobi', { domain: '.test-host.com', path: '/', secure: true, samesite: 'None' });
expect(document.cookie.includes('name=tobi')).toBeTruthy();
});
});

Expand Down Expand Up @@ -62,4 +98,14 @@ describe('cookie()', () => {
expect('loki').toEqual(obj.name);
expect('ferret').toEqual(obj.species);
});

it('should return all cookies if the first argument is not a valid string', () => {
cookie('name', 'loki');
cookie('species', 'ferret');
const obj = cookie(false);

expect(2).toEqual(Object.keys(obj).length);
expect('loki').toEqual(obj.name);
expect('ferret').toEqual(obj.species);
});
});
Loading

0 comments on commit 7a8ea30

Please sign in to comment.