Skip to content

Commit fbcd618

Browse files
bug: identify not work when only customized properties changed
Use serialized user as the storage key
1 parent 332cb0d commit fbcd618

10 files changed

Lines changed: 38 additions & 14 deletions

examples/web-app/umd/featbit-js-client-sdk-4.2.4.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/web-app/umd/featbit-js-client-sdk-4.2.4.js renamed to examples/web-app/umd/featbit-js-client-sdk-4.2.5.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/web-app/umd/featbit-js-client-sdk-4.2.5.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/web-app/umd/featbit-js-client-sdk.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/web-app/umd/featbit-js-client-sdk.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@featbit/js-client-sdk",
3-
"version": "4.2.4",
3+
"version": "4.2.5",
44
"description": "https://github.com/featbit/featbit-js-client-sdk",
55
"main": "./dist/esm/index.js",
66
"module": "./dist/esm/index.js",

src/platform/browser/LocalStorageStore.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
import { IOptions } from "../../options/IOptions";
66
import { BaseStore } from "../../store/BaseStore";
77
import { ILogger } from "../../logging";
8-
import { serializeUser } from "../../utils/serializeUser";
8+
import { hashSerializeUser, serializeUser } from "../../utils/serializeUser";
99

1010
export default class LocalStorageStore extends BaseStore {
1111
private logger: ILogger;
@@ -31,12 +31,14 @@ export default class LocalStorageStore extends BaseStore {
3131
}
3232

3333
protected override async dumpStoreToStorage() {
34-
const storageKey = `${StoreStorageKey}-${this._user.keyId}`;
34+
const userHash = await hashSerializeUser(this._user);
35+
const storageKey = `${StoreStorageKey}-${userHash}`;
3536
localStorage.setItem(storageKey, JSON.stringify(this.store));
3637
}
3738

3839
protected override async loadStoreFromStorage() {
39-
const storageKey = `${StoreStorageKey}-${this._user.keyId}`;
40+
const userHash = await hashSerializeUser(this._user);
41+
const storageKey = `${StoreStorageKey}-${userHash}`;
4042
const dataStoreStr = localStorage.getItem(storageKey);
4143
let store: IStoreDataStorage | null = null;
4244

src/store/InMemoryStore.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
IStoreDataStorage
44
} from "./store";
55
import { BaseStore } from "./BaseStore";
6+
import { hashSerializeUser } from "../utils";
67

78
export default class InMemoryStore extends BaseStore {
89
private allStores: { [DataStoreStorageKey: string]: IStoreDataStorage } = {};
@@ -25,12 +26,14 @@ export default class InMemoryStore extends BaseStore {
2526
}
2627

2728
protected override async dumpStoreToStorage() {
28-
const storageKey = `${StoreStorageKey}-${this._user.keyId}`;
29+
const userHash = await hashSerializeUser(this._user);
30+
const storageKey = `${StoreStorageKey}-${userHash}`;
2931
this.allStores[storageKey] = {...this.store};
3032
}
3133

3234
protected override async loadStoreFromStorage() {
33-
const storageKey = `${StoreStorageKey}-${this._user.keyId}`;
35+
const userHash = await hashSerializeUser(this._user);
36+
const storageKey = `${StoreStorageKey}-${userHash}`;
3437

3538
this.store = this.allStores[storageKey] ?? { flags: {}, version: 0 };
3639
}

src/utils/serializeUser.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function serializeUser(user: IUser | undefined): string {
55
return '';
66
}
77

8-
const builtInProperties = `${user.keyId},${user.name}`;
8+
const builtInProperties = `${user.keyId},${user.name ?? ''}`;
99

1010
const customizedProperties = user.customizedProperties
1111
?.sort((a, b) => {
@@ -25,4 +25,23 @@ export function serializeUser(user: IUser | undefined): string {
2525
.join(',');
2626

2727
return `${builtInProperties},${customizedProperties}`;
28-
}
28+
}
29+
30+
export async function hashSerializeUser(
31+
user: IUser | undefined
32+
): Promise<string> {
33+
const serialized = serializeUser(user);
34+
35+
if (!serialized) {
36+
return '';
37+
}
38+
39+
const encoder = new TextEncoder();
40+
const data = encoder.encode(serialized);
41+
42+
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
43+
44+
return Array.from(new Uint8Array(hashBuffer))
45+
.map(b => b.toString(16).padStart(2, '0'))
46+
.join('');
47+
}

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const version = "4.2.4"; export const name = "@featbit/js-client-sdk";
1+
export const version = "4.2.5"; export const name = "@featbit/js-client-sdk";

0 commit comments

Comments
 (0)