Skip to content

Commit

Permalink
chore: address review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
MoumitaM committed May 7, 2024
1 parent 5d277b6 commit e4ce0a0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,6 @@ describe('User session manager', () => {
expect(spy).toHaveBeenCalledWith(
[{ name: 'rl_anonymous_id', value: '' }],
expect.any(Object),
true,
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics-js/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
destSDKBaseURL:
'__DEST_SDK_BASE_URL__' + window.rudderAnalyticsBuildType + '/js-integrations',
pluginsSDKBaseURL: '__PLUGINS_BASE_URL__' + window.rudderAnalyticsBuildType + '/plugins',
// useServerSideCookie:true,
useServerSideCookies:true,
// queueOptions: {
// batch: {
// maxSize: 5 * 1024, // 5KB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
DATA_SERVER_REQUEST_FAIL_ERROR,
FAILED_SETTING_COOKIE_FROM_SERVER_ERROR,
FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR,
FAILED_TO_REMOVE_COOKIE_FROM_SERVER_ERROR,
TIMEOUT_NOT_NUMBER_WARNING,
TIMEOUT_NOT_RECOMMENDED_WARNING,
TIMEOUT_ZERO_WARNING,
Expand Down Expand Up @@ -280,19 +281,22 @@ class UserSessionManager implements IUserSessionManager {
* @param key cookie name
* @param value encrypted cookie value
*/
setServerSideCookie(cookieData: CookieData[], store: IStore, removeCookie?: boolean): void {
setServerSideCookie(cookieData: CookieData[], store?: IStore): void {
try {
let encryptedCookieData: EncryptedCookieData[] = [];
const encryptedCookieData: EncryptedCookieData[] = [];
cookieData.forEach(e => {
encryptedCookieData.push({
name: e.name,
value: removeCookie
? (e.value as string)
: store?.encrypt(stringifyWithoutCircular(e.value, false, [], this.logger)),
});
const encryptedValue =
e.value === ''
? e.value
: store?.encrypt(stringifyWithoutCircular(e.value, false, [], this.logger));
if (isDefinedAndNotNull(encryptedValue)) {
encryptedCookieData.push({
name: e.name,
value: encryptedValue,
});
}
});
encryptedCookieData = encryptedCookieData.filter(e => isDefinedAndNotNull(e.value));
if (encryptedCookieData.length > 0 || removeCookie) {
if (encryptedCookieData.length > 0) {
this.httpClient?.getAsyncData({
url: `${state.serverCookies.dataServerUrl.value}/rsaRequest`,
options: {
Expand All @@ -319,8 +323,12 @@ class UserSessionManager implements IUserSessionManager {
if (details?.xhr?.status === 200) {
cookieData.forEach(each => {
const cookieValue = store?.get(each.name);
if (cookieValue !== each.value) {
this.logger?.error(FAILED_SETTING_COOKIE_FROM_SERVER_ERROR(each.name));
if (each.value) {
if (cookieValue !== each.value) {
this.logger?.error(FAILED_SETTING_COOKIE_FROM_SERVER_ERROR(each.name));
}
} else if (cookieValue) {
this.logger?.error(FAILED_TO_REMOVE_COOKIE_FROM_SERVER_ERROR(each.name));
}
});
} else {
Expand All @@ -334,6 +342,9 @@ class UserSessionManager implements IUserSessionManager {
}
} catch (e) {
this.onError(e, FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR);
cookieData.forEach(each => {

Check warning on line 345 in packages/analytics-js/src/components/userSessionManager/UserSessionManager.ts

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js/src/components/userSessionManager/UserSessionManager.ts#L344-L345

Added lines #L344 - L345 were not covered by tests
store?.set(each.name, each.value);
});
}
}

Expand All @@ -352,28 +363,26 @@ class UserSessionManager implements IUserSessionManager {
const curStore = this.storeManager?.getStore(
storageClientDataStoreNameMap[storageType] as string,
);
if (curStore) {
const key = entries[sessionKey]?.key as string;
if (value && (isString(value) || isNonEmptyObject(value))) {
// if useServerSideCookies load option is set to true
// set the cookie from server side
if (
state.serverCookies.isEnabledServerSideCookies.value &&
storageType === COOKIE_STORAGE
) {
this.setServerSideCookie([{ name: key, value }], curStore);
} else {
curStore?.set(key, value);
}
} else if (
const key = entries[sessionKey]?.key as string;
if (value && (isString(value) || isNonEmptyObject(value))) {
// if useServerSideCookies load option is set to true
// set the cookie from server side
if (
state.serverCookies.isEnabledServerSideCookies.value &&
storageType === COOKIE_STORAGE
) {
// remove cookie that is set from server side
this.setServerSideCookie([{ name: key, value: '' }], curStore, true);
this.setServerSideCookie([{ name: key, value }], curStore);
} else {
curStore?.remove(key);
curStore?.set(key, value);
}
} else if (
state.serverCookies.isEnabledServerSideCookies.value &&
storageType === COOKIE_STORAGE
) {
// remove cookie that is set from server side
this.setServerSideCookie([{ name: key, value: '' }], curStore);
} else {
curStore?.remove(key);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/analytics-js/src/constants/logMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ const DATA_SERVER_REQUEST_FAIL_ERROR = (status?: number) =>
`The server responded with status ${status} while setting the cookies. As a fallback, the cookies will be set from the client side.`;
const FAILED_SETTING_COOKIE_FROM_SERVER_ERROR = (key: string) =>
`The server failed to set the ${key} cookie.`;
const FAILED_TO_REMOVE_COOKIE_FROM_SERVER_ERROR = (key: string) =>
`The server failed to remove the ${key} cookie.`;

Check warning on line 102 in packages/analytics-js/src/constants/logMessages.ts

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js/src/constants/logMessages.ts#L102

Added line #L102 was not covered by tests
const FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR = `setServerSideCookie method failed`;

// WARNING
Expand Down Expand Up @@ -314,5 +316,6 @@ export {
DATA_SERVER_REQUEST_FAIL_ERROR,
FAILED_SETTING_COOKIE_FROM_SERVER_ERROR,
FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR,
FAILED_TO_REMOVE_COOKIE_FROM_SERVER_ERROR,
MISCONFIGURED_PLUGINS_WARNING,
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ const getStorageEngine = (type?: StorageType): IStorage => {
* Configure cookie storage singleton
*/
const configureCookieStorageEngine = (options: Partial<ICookieStorageOptions>) => {
const cookieStorage = new CookieStorage({}, defaultLogger).configure(options);
const cookieStorageOptions = new CookieStorage({}, defaultLogger).configure(options);
state.storage.cookie.value = {
maxage: cookieStorage.maxage,
path: cookieStorage.path,
domain: cookieStorage.domain,
samesite: cookieStorage.samesite,
expires: cookieStorage.expires,
secure: cookieStorage.secure,
maxage: cookieStorageOptions.maxage,
path: cookieStorageOptions.path,
domain: cookieStorageOptions.domain,
samesite: cookieStorageOptions.samesite,
expires: cookieStorageOptions.expires,
secure: cookieStorageOptions.secure,
};
};

Expand Down

0 comments on commit e4ce0a0

Please sign in to comment.