Skip to content

Commit f63c32d

Browse files
Latin1fix (#89)
* latin1 fix * strict * return * codecleanup * cache * version
1 parent 10d1590 commit f63c32d

File tree

7 files changed

+65
-7
lines changed

7 files changed

+65
-7
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
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": "configcat-js",
3-
"version": "9.0.0",
3+
"version": "9.1.0",
44
"description": "ConfigCat is a configuration as a service that lets you manage your features and configurations without actually deploying new code.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/Cache.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { IConfigCatCache } from "configcat-common";
33
export class LocalStorageCache implements IConfigCatCache {
44
set(key: string, value: string): void {
55
try {
6-
localStorage.setItem(key, btoa(value));
6+
localStorage.setItem(key, this.b64EncodeUnicode(value));
77
}
88
catch (ex) {
99
// local storage is unavailable
@@ -14,11 +14,24 @@ export class LocalStorageCache implements IConfigCatCache {
1414
try {
1515
const configString = localStorage.getItem(key);
1616
if (configString) {
17-
return atob(configString);
17+
return this.b64DecodeUnicode(configString);
1818
}
1919
}
2020
catch (ex) {
2121
// local storage is unavailable or invalid cache value in localstorage
2222
}
23+
return void 0;
24+
}
25+
26+
private b64EncodeUnicode(str: string): string {
27+
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (_, p1) {
28+
return String.fromCharCode(parseInt(p1, 16))
29+
}));
30+
}
31+
32+
private b64DecodeUnicode(str: string): string {
33+
return decodeURIComponent(Array.prototype.map.call(atob(str), function (c: string) {
34+
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
35+
}).join(''));
2336
}
2437
}

test/CacheTests.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { assert } from "chai";
2+
import { LocalStorageCache } from "../lib/Cache";
3+
4+
5+
describe("LocalStorageCache cache tests", () => {
6+
it("LocalStorageCache works with non latin 1 characters", () => {
7+
const cache = new LocalStorageCache();
8+
const key = "testkey";
9+
const text = "äöüÄÖÜçéèñışğ⢙✓😀";
10+
cache.set(key, text);
11+
const retrievedValue = cache.get(key);
12+
assert.strictEqual(retrievedValue, text);
13+
});
14+
});

test/HttpTests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { FakeLogger } from "./helpers/fakes";
55
import * as utils from "./helpers/utils";
66

77
describe("HTTP tests", () => {
8-
const sdkKey = "PKDVCLf-Hq-h-kCzMp-L7Q/psuH7BGHoUmdONrzzUOY7A";
8+
const sdkKey = "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/AG6C1ngVb0CvM07un6JisQ";
99
const baseUrl = "https://cdn-global.test.com";
1010

1111
it("HTTP timeout", async () => {

test/IntegrationTests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IConfigCatClient, IEvaluationDetails, IOptions, LogLevel, PollingMode,
33
import * as configcatClient from "../src";
44
import { createConsoleLogger } from "../src";
55

6-
const sdkKey = "PKDVCLf-Hq-h-kCzMp-L7Q/psuH7BGHoUmdONrzzUOY7A";
6+
const sdkKey = "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/AG6C1ngVb0CvM07un6JisQ";
77

88
describe("Integration tests - Normal use", () => {
99

test/SpecialCharacterTests.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { assert } from "chai";
2+
import { IConfigCatClient, IEvaluationDetails, IOptions, LogLevel, PollingMode, SettingKeyValue, User } from "configcat-common";
3+
import * as configcatClient from "../src";
4+
import { createConsoleLogger } from "../src";
5+
6+
const sdkKey = "configcat-sdk-1/PKDVCLf-Hq-h-kCzMp-L7Q/u28_1qNyZ0Wz-ldYHIU7-g";
7+
8+
describe("Special characters test", () => {
9+
10+
const options: IOptions = { logger: createConsoleLogger(LogLevel.Off) };
11+
12+
let client: IConfigCatClient;
13+
14+
beforeEach(function () {
15+
client = configcatClient.getClient(sdkKey, PollingMode.AutoPoll, options);
16+
});
17+
18+
afterEach(function () {
19+
client.dispose();
20+
});
21+
22+
it(`Special characters works - cleartext`, async () => {
23+
const actual: string = await client.getValueAsync("specialCharacters", "NOT_CAT", new User("äöüÄÖÜçéèñışğ⢙✓😀"));
24+
assert.strictEqual(actual, "äöüÄÖÜçéèñışğ⢙✓😀");
25+
});
26+
27+
it(`Special characters works - hashed`, async () => {
28+
const actual: string = await client.getValueAsync("specialCharactersHashed", "NOT_CAT", new User("äöüÄÖÜçéèñışğ⢙✓😀"));
29+
assert.strictEqual(actual, "äöüÄÖÜçéèñışğ⢙✓😀");
30+
});
31+
});

0 commit comments

Comments
 (0)