-
Notifications
You must be signed in to change notification settings - Fork 10
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 * Escape ccetag query string value * Exclude non-source files so they don't pollute autocompletion/intellisense * Update to configcat-common v9.1.0 * Bump version
- Loading branch information
Showing
11 changed files
with
178 additions
and
84 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,37 +1,58 @@ | ||
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); | ||
} | ||
const configString = this.storage.getItem(key); | ||
if (configString) { | ||
return fromUtf8Base64(configString); | ||
} | ||
catch (ex) { | ||
// local storage is unavailable or invalid cache value in localstorage | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,84 @@ | ||
import { assert } from "chai"; | ||
import { LocalStorageCache } from "../lib/Cache"; | ||
import { LogLevel } from "configcat-common"; | ||
import { LocalStorageCache, fromUtf8Base64, getLocalStorage, toUtf8Base64 } from "../src/Cache"; | ||
import { FakeLogger } from "./helpers/fakes"; | ||
import { createClientWithLazyLoad } from "./helpers/utils"; | ||
|
||
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); | ||
} | ||
} | ||
|
||
describe("LocalStorageCache cache tests", () => { | ||
it("LocalStorageCache works with non latin 1 characters", () => { | ||
const cache = new LocalStorageCache(); | ||
const key = "testkey"; | ||
const text = "äöüÄÖÜçéèñışğ⢙✓😀"; | ||
cache.set(key, text); | ||
const retrievedValue = cache.get(key); | ||
assert.strictEqual(retrievedValue, text); | ||
for (const input of [ | ||
"", | ||
"\n", | ||
"äöüÄÖÜçéèñışğ⢙✓😀", | ||
allBmpChars | ||
]) { | ||
it(`Base64 encode/decode works - input: ${input.slice(0, Math.min(input.length, 128))}`, () => { | ||
assert.strictEqual(fromUtf8Base64(toUtf8Base64(input)), input); | ||
}); | ||
} | ||
}); | ||
|
||
describe("LocalStorageCache cache tests", () => { | ||
it("LocalStorageCache works with non latin 1 characters", () => { | ||
const localStorage = getLocalStorage(); | ||
assert.isNotNull(localStorage); | ||
|
||
const cache = new LocalStorageCache(localStorage!); | ||
const key = "testkey"; | ||
const text = "äöüÄÖÜçéèñışğ⢙✓😀"; | ||
cache.set(key, text); | ||
const retrievedValue = cache.get(key); | ||
assert.strictEqual(retrievedValue, text); | ||
assert.strictEqual(window.localStorage.getItem(key), "w6TDtsO8w4TDlsOcw6fDqcOow7HEscWfxJ/DosKi4oSi4pyT8J+YgA=="); | ||
}); | ||
|
||
it("Error is logged when LocalStorageCache.get throws", async () => { | ||
const errorMessage = "Something went wrong."; | ||
const faultyLocalStorage: Storage = { | ||
get length() { return 0; }, | ||
clear() { }, | ||
getItem() { throw Error(errorMessage); }, | ||
setItem() { }, | ||
removeItem() { }, | ||
key() { return null; } | ||
}; | ||
|
||
const fakeLogger = new FakeLogger(); | ||
|
||
const client = createClientWithLazyLoad("configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/AG6C1ngVb0CvM07un6JisQ", { logger: fakeLogger }, | ||
kernel => LocalStorageCache.setup(kernel, () => faultyLocalStorage)); | ||
|
||
try { await client.getValueAsync("stringDefaultCat", ""); } | ||
finally { client.dispose(); } | ||
|
||
assert.isDefined(fakeLogger.events.find(([level, eventId, , err]) => level === LogLevel.Error && eventId === 2200 && err instanceof Error && err.message === errorMessage)); | ||
}); | ||
|
||
it("Error is logged when LocalStorageCache.set throws", async () => { | ||
const errorMessage = "Something went wrong."; | ||
const faultyLocalStorage: Storage = { | ||
get length() { return 0; }, | ||
clear() { }, | ||
getItem() { return null; }, | ||
setItem() { throw Error(errorMessage); }, | ||
removeItem() { }, | ||
key() { return null; } | ||
}; | ||
|
||
const fakeLogger = new FakeLogger(); | ||
|
||
const client = createClientWithLazyLoad("configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/AG6C1ngVb0CvM07un6JisQ", { logger: fakeLogger }, | ||
kernel => LocalStorageCache.setup(kernel, () => faultyLocalStorage)); | ||
|
||
try { await client.getValueAsync("stringDefaultCat", ""); } | ||
finally { client.dispose(); } | ||
|
||
assert.isDefined(fakeLogger.events.find(([level, eventId, , err]) => level === LogLevel.Error && eventId === 2201 && err instanceof Error && err.message === errorMessage)); | ||
}); | ||
}); |
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,31 +1,31 @@ | ||
import { assert } from "chai"; | ||
import { IConfigCatClient, IEvaluationDetails, IOptions, LogLevel, PollingMode, SettingKeyValue, User } from "configcat-common"; | ||
import { IConfigCatClient, IOptions, LogLevel, PollingMode, User } from "configcat-common"; | ||
import * as configcatClient from "../src"; | ||
import { createConsoleLogger } from "../src"; | ||
|
||
const sdkKey = "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/u28_1qNyZ0Wz-ldYHIU7-g"; | ||
|
||
describe("Special characters test", () => { | ||
|
||
const options: IOptions = { logger: createConsoleLogger(LogLevel.Off) }; | ||
const options: IOptions = { logger: createConsoleLogger(LogLevel.Off) }; | ||
|
||
let client: IConfigCatClient; | ||
let client: IConfigCatClient; | ||
|
||
beforeEach(function () { | ||
client = configcatClient.getClient(sdkKey, PollingMode.AutoPoll, options); | ||
}); | ||
beforeEach(function() { | ||
client = configcatClient.getClient(sdkKey, PollingMode.AutoPoll, options); | ||
}); | ||
|
||
afterEach(function () { | ||
client.dispose(); | ||
}); | ||
afterEach(function() { | ||
client.dispose(); | ||
}); | ||
|
||
it(`Special characters works - cleartext`, async () => { | ||
const actual: string = await client.getValueAsync("specialCharacters", "NOT_CAT", new User("äöüÄÖÜçéèñışğ⢙✓😀")); | ||
assert.strictEqual(actual, "äöüÄÖÜçéèñışğ⢙✓😀"); | ||
}); | ||
it("Special characters works - cleartext", async () => { | ||
const actual: string = await client.getValueAsync("specialCharacters", "NOT_CAT", new User("äöüÄÖÜçéèñışğ⢙✓😀")); | ||
assert.strictEqual(actual, "äöüÄÖÜçéèñışğ⢙✓😀"); | ||
}); | ||
|
||
it(`Special characters works - hashed`, async () => { | ||
const actual: string = await client.getValueAsync("specialCharactersHashed", "NOT_CAT", new User("äöüÄÖÜçéèñışğ⢙✓😀")); | ||
assert.strictEqual(actual, "äöüÄÖÜçéèñışğ⢙✓😀"); | ||
}); | ||
it("Special characters works - hashed", async () => { | ||
const actual: string = await client.getValueAsync("specialCharactersHashed", "NOT_CAT", new User("äöüÄÖÜçéèñışğ⢙✓😀")); | ||
assert.strictEqual(actual, "äöüÄÖÜçéèñışğ⢙✓😀"); | ||
}); | ||
}); |
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,13 +1,13 @@ | ||
import { IConfigCatLogger, LogEventId, LogLevel, LogMessage } from "../../src"; | ||
|
||
export class FakeLogger implements IConfigCatLogger { | ||
messages: [LogLevel, string][] = []; | ||
events: [LogLevel, LogEventId, LogMessage, any?][] = []; | ||
|
||
constructor(public level = LogLevel.Info) { } | ||
|
||
reset(): void { this.messages.splice(0); } | ||
reset(): void { this.events.splice(0); } | ||
|
||
log(level: LogLevel, eventId: LogEventId, message: LogMessage, exception?: any): void { | ||
this.messages.push([level, message.toString()]); | ||
this.events.push([level, eventId, message, exception]); | ||
} | ||
} |
Oops, something went wrong.