flagsmith-nodejs 2.5.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/sdk/index.js
CHANGED
|
@@ -223,6 +223,9 @@ var Flagsmith = /** @class */ (function () {
|
|
|
223
223
|
return __generator(this, function (_b) {
|
|
224
224
|
switch (_b.label) {
|
|
225
225
|
case 0:
|
|
226
|
+
if (!identifier) {
|
|
227
|
+
throw new Error("`identifier` argument is missing or invalid.");
|
|
228
|
+
}
|
|
226
229
|
_a = !!this.cache;
|
|
227
230
|
if (!_a) return [3 /*break*/, 2];
|
|
228
231
|
return [4 /*yield*/, this.cache.get("flags-".concat(identifier))];
|
|
@@ -260,6 +263,9 @@ var Flagsmith = /** @class */ (function () {
|
|
|
260
263
|
*/
|
|
261
264
|
Flagsmith.prototype.getIdentitySegments = function (identifier, traits) {
|
|
262
265
|
var _this = this;
|
|
266
|
+
if (!identifier) {
|
|
267
|
+
throw new Error("`identifier` argument is missing or invalid.");
|
|
268
|
+
}
|
|
263
269
|
traits = traits || {};
|
|
264
270
|
if (this.enableLocalEvaluation) {
|
|
265
271
|
return new Promise(function (resolve, reject) {
|
|
@@ -423,7 +429,7 @@ var Flagsmith = /** @class */ (function () {
|
|
|
423
429
|
featureStates: featureStates,
|
|
424
430
|
analyticsProcessor: this.analyticsProcessor,
|
|
425
431
|
defaultFlagHandler: this.defaultFlagHandler,
|
|
426
|
-
identityID: identityModel.djangoID || identityModel.
|
|
432
|
+
identityID: identityModel.djangoID || identityModel.compositeKey
|
|
427
433
|
});
|
|
428
434
|
if (!!!this.cache) return [3 /*break*/, 2];
|
|
429
435
|
// @ts-ignore node-cache types are incorrect, ttl should be optional
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flagsmith-nodejs",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"description": "Flagsmith lets you manage features flags and remote config across web, mobile and server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"repository": {
|
package/sdk/index.ts
CHANGED
|
@@ -173,6 +173,10 @@ export class Flagsmith {
|
|
|
173
173
|
* @returns Flags object holding all the flags for the given identity.
|
|
174
174
|
*/
|
|
175
175
|
async getIdentityFlags(identifier: string, traits?: { [key: string]: any }): Promise<Flags> {
|
|
176
|
+
if (!identifier) {
|
|
177
|
+
throw new Error("`identifier` argument is missing or invalid.")
|
|
178
|
+
}
|
|
179
|
+
|
|
176
180
|
const cachedItem = !!this.cache && await this.cache.get(`flags-${identifier}`);
|
|
177
181
|
if (!!cachedItem) {
|
|
178
182
|
return cachedItem;
|
|
@@ -203,6 +207,10 @@ export class Flagsmith {
|
|
|
203
207
|
identifier: string,
|
|
204
208
|
traits?: { [key: string]: any }
|
|
205
209
|
): Promise<SegmentModel[]> {
|
|
210
|
+
if (!identifier) {
|
|
211
|
+
throw new Error("`identifier` argument is missing or invalid.")
|
|
212
|
+
}
|
|
213
|
+
|
|
206
214
|
traits = traits || {};
|
|
207
215
|
if (this.enableLocalEvaluation) {
|
|
208
216
|
return new Promise((resolve, reject) => {
|
|
@@ -329,7 +337,7 @@ export class Flagsmith {
|
|
|
329
337
|
featureStates: featureStates,
|
|
330
338
|
analyticsProcessor: this.analyticsProcessor,
|
|
331
339
|
defaultFlagHandler: this.defaultFlagHandler,
|
|
332
|
-
identityID: identityModel.djangoID || identityModel.
|
|
340
|
+
identityID: identityModel.djangoID || identityModel.compositeKey
|
|
333
341
|
});
|
|
334
342
|
|
|
335
343
|
if (!!this.cache) {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
+
import { getHashedPercentateForObjIds } from '../../../../flagsmith-engine/utils/hashing';
|
|
3
|
+
|
|
4
|
+
describe('getHashedPercentageForObjIds', () => {
|
|
5
|
+
it.each([
|
|
6
|
+
[[12, 93]],
|
|
7
|
+
[[uuidv4(), 99]],
|
|
8
|
+
[[99, uuidv4()]],
|
|
9
|
+
[[uuidv4(), uuidv4()]]
|
|
10
|
+
])('returns x where 0 <= x < 100', (objIds: (string|number)[]) => {
|
|
11
|
+
let result = getHashedPercentateForObjIds(objIds);
|
|
12
|
+
expect(result).toBeLessThan(100);
|
|
13
|
+
expect(result).toBeGreaterThanOrEqual(0);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it.each([
|
|
17
|
+
[[12, 93]],
|
|
18
|
+
[[uuidv4(), 99]],
|
|
19
|
+
[[99, uuidv4()]],
|
|
20
|
+
[[uuidv4(), uuidv4()]]
|
|
21
|
+
])('returns the same value each time', (objIds: (string|number)[]) => {
|
|
22
|
+
let resultOne = getHashedPercentateForObjIds(objIds);
|
|
23
|
+
let resultTwo = getHashedPercentateForObjIds(objIds);
|
|
24
|
+
expect(resultOne).toEqual(resultTwo);
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('is unique for different object ids', () => {
|
|
28
|
+
let resultOne = getHashedPercentateForObjIds([14, 106]);
|
|
29
|
+
let resultTwo = getHashedPercentateForObjIds([53, 200]);
|
|
30
|
+
expect(resultOne).not.toEqual(resultTwo);
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('is evenly distributed', () => {
|
|
34
|
+
// copied from python test here:
|
|
35
|
+
// https://github.com/Flagsmith/flagsmith-engine/blob/main/tests/unit/utils/test_utils_hashing.py#L56
|
|
36
|
+
const testSample = 500;
|
|
37
|
+
const numTestBuckets = 50;
|
|
38
|
+
const testBucketSize = Math.floor(testSample / numTestBuckets)
|
|
39
|
+
const errorFactor = 0.1
|
|
40
|
+
|
|
41
|
+
// Given
|
|
42
|
+
let objectIdPairs = Array.from(Array(testSample).keys()).flatMap(d => Array.from(Array(testSample).keys()).map(e => [d, e].flat()))
|
|
43
|
+
|
|
44
|
+
// When
|
|
45
|
+
console.log(objectIdPairs);
|
|
46
|
+
let values = objectIdPairs.map((objIds) => getHashedPercentateForObjIds(objIds));
|
|
47
|
+
|
|
48
|
+
// Then
|
|
49
|
+
for (let i = 0; i++; i < numTestBuckets) {
|
|
50
|
+
let bucketStart = i * testBucketSize;
|
|
51
|
+
let bucketEnd = (i + 1) * testBucketSize;
|
|
52
|
+
let bucketValueLimit = Math.min(
|
|
53
|
+
(i + 1) / numTestBuckets + errorFactor + ((i + 1) / numTestBuckets),
|
|
54
|
+
1
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
for (let i = bucketStart; i++; i < bucketEnd) {
|
|
58
|
+
expect(values[i]).toBeLessThanOrEqual(bucketValueLimit);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
})
|
|
@@ -227,6 +227,23 @@ test('test onEnvironmentChange is called after error', async () => {
|
|
|
227
227
|
expect(callbackSpy).toBeCalled();
|
|
228
228
|
});
|
|
229
229
|
|
|
230
|
+
test('getIdentityFlags throws error if identifier is empty string', async () => {
|
|
231
|
+
const flagsmith = new Flagsmith({
|
|
232
|
+
environmentKey: 'key',
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
await expect(flagsmith.getIdentityFlags('')).rejects.toThrow('`identifier` argument is missing or invalid.');
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
test('getIdentitySegments throws error if identifier is empty string', () => {
|
|
240
|
+
const flagsmith = new Flagsmith({
|
|
241
|
+
environmentKey: 'key',
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
expect(() => { flagsmith.getIdentitySegments(''); }).toThrow('`identifier` argument is missing or invalid.');
|
|
245
|
+
})
|
|
246
|
+
|
|
230
247
|
|
|
231
248
|
|
|
232
249
|
async function wipeFeatureStateUUIDs (enviromentModel: EnvironmentModel) {
|