-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve base64 decode perf. + add tests + fix code formatting * Use LocalStorageCache only when localStorage is available + don't swallow exceptions in LocalStorageCache.get/set * Send etag as query param (ccetag) when SDK runs in browser * Exclude non-source files so they don't pollute autocompletion/intellisense * Update to configcat-common v9.1.0 * Bump version
- Loading branch information
Showing
7 changed files
with
103 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,36 @@ | ||
import { LocalStorageCache } from "./Cache"; | ||
import { LocalStorageCache, fromUtf8Base64, getLocalStorage, toUtf8Base64 } from "../src/Cache"; | ||
|
||
it("LocalStorageCache works with non latin 1 characters", () => { | ||
const cache = new LocalStorageCache(); | ||
describe("Base64 encode/decode test", () => { | ||
let allBmpChars = ""; | ||
for (let i = 0; i <= 0xFFFF; i++) { | ||
if (i < 0xD800 || 0xDFFF < i) { // skip lone surrogate chars | ||
allBmpChars += String.fromCharCode(i); | ||
} | ||
} | ||
|
||
for (const input of [ | ||
"", | ||
"\n", | ||
"äöüÄÖÜçéèñışğ⢙✓😀", | ||
allBmpChars | ||
]) { | ||
it(`Base64 encode/decode works - input: ${input.slice(0, Math.min(input.length, 128))}`, () => { | ||
expect(fromUtf8Base64(toUtf8Base64(input))).toStrictEqual(input); | ||
}); | ||
} | ||
}); | ||
|
||
describe("LocalStorageCache cache tests", () => { | ||
it("LocalStorageCache works with non latin 1 characters", () => { | ||
const localStorage = getLocalStorage(); | ||
expect(localStorage).not.toBeNull(); | ||
|
||
const cache = new LocalStorageCache(localStorage!); | ||
const key = "testkey"; | ||
const text = "äöüÄÖÜçéèñışğ⢙✓😀"; | ||
cache.set(key, text); | ||
const retrievedValue = cache.get(key); | ||
expect(retrievedValue).toStrictEqual(text); | ||
expect(window.localStorage.getItem(key)).toStrictEqual("w6TDtsO8w4TDlsOcw6fDqcOow7HEscWfxJ/DosKi4oSi4pyT8J+YgA=="); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,59 @@ | ||
import type { IConfigCatCache } from "configcat-common"; | ||
import type { IConfigCatCache, IConfigCatKernel } from "configcat-common"; | ||
import { ExternalConfigCache } from "configcat-common"; | ||
|
||
export class LocalStorageCache implements IConfigCatCache { | ||
set(key: string, value: string): void { | ||
try { | ||
localStorage.setItem(key, this.b64EncodeUnicode(value)); | ||
} | ||
catch (ex) { | ||
// local storage is unavailable | ||
static setup(kernel: IConfigCatKernel, localStorageGetter?: () => Storage | null): IConfigCatKernel { | ||
const localStorage = (localStorageGetter ?? getLocalStorage)(); | ||
if (localStorage) { | ||
kernel.defaultCacheFactory = options => new ExternalConfigCache(new LocalStorageCache(localStorage), options.logger); | ||
} | ||
return kernel; | ||
} | ||
|
||
constructor(private readonly storage: Storage) { | ||
} | ||
|
||
set(key: string, value: string): void { | ||
this.storage.setItem(key, toUtf8Base64(value)); | ||
} | ||
|
||
get(key: string): string | undefined { | ||
try { | ||
const configString = localStorage.getItem(key); | ||
if (configString) { | ||
return this.b64DecodeUnicode(configString); | ||
} | ||
} | ||
catch (ex) { | ||
// local storage is unavailable or invalid cache value in localstorage | ||
const configString = this.storage.getItem(key); | ||
if (configString) { | ||
return fromUtf8Base64(configString); | ||
} | ||
return void 0; | ||
} | ||
} | ||
|
||
private b64EncodeUnicode(str: string): string { | ||
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (_, p1) { | ||
return String.fromCharCode(parseInt(p1, 16)) | ||
})); | ||
} | ||
export function getLocalStorage(): Storage | null { | ||
const testKey = "__configcat_localStorage_test"; | ||
|
||
try { | ||
const storage = window.localStorage; | ||
storage.setItem(testKey, testKey); | ||
|
||
let retrievedItem: string | null; | ||
try { retrievedItem = storage.getItem(testKey); } | ||
finally { storage.removeItem(testKey); } | ||
|
||
private b64DecodeUnicode(str: string): string { | ||
return decodeURIComponent(Array.prototype.map.call(atob(str), function (c: string) { | ||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) | ||
}).join('')); | ||
if (retrievedItem === testKey) { | ||
return storage; | ||
} | ||
} | ||
catch (err) { /* intentional no-op */ } | ||
|
||
return null; | ||
} | ||
|
||
export function toUtf8Base64(str: string): string { | ||
str = encodeURIComponent(str); | ||
str = str.replace(/%([0-9A-F]{2})/g, (_, p1) => String.fromCharCode(parseInt(p1, 16))); | ||
return btoa(str); | ||
} | ||
|
||
export function fromUtf8Base64(str: string): string { | ||
str = atob(str); | ||
str = str.replace(/[%\x80-\xFF]/g, m => "%" + m.charCodeAt(0).toString(16)); | ||
return decodeURIComponent(str); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters