@growthbook/proxy-eval 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 GrowthBook, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,8 @@
1
+ export declare function evaluateFeatures({ payload, attributes, forcedVariations, forcedFeatures, url, ctx, }: {
2
+ payload: any;
3
+ attributes: Record<string, any>;
4
+ forcedVariations?: Record<string, number>;
5
+ forcedFeatures?: Map<string, any>;
6
+ url?: string;
7
+ ctx?: any;
8
+ }): any;
package/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.evaluateFeatures = void 0;
4
+ /* eslint-disable @typescript-eslint/no-explicit-any */
5
+ const growthbook_1 = require("@growthbook/growthbook");
6
+ function evaluateFeatures({ payload, attributes, forcedVariations, forcedFeatures, url, ctx, }) {
7
+ var _a;
8
+ const evaluatedFeatures = {};
9
+ const evaluatedExperiments = [];
10
+ const features = payload === null || payload === void 0 ? void 0 : payload.features;
11
+ const experiments = payload === null || payload === void 0 ? void 0 : payload.experiments;
12
+ const context = { attributes };
13
+ if (features) {
14
+ context.features = features;
15
+ }
16
+ if (experiments) {
17
+ context.experiments = experiments;
18
+ }
19
+ if (forcedVariations) {
20
+ context.forcedVariations = forcedVariations;
21
+ }
22
+ if (url !== undefined) {
23
+ context.url = url;
24
+ }
25
+ if (features || experiments) {
26
+ const gb = new growthbook_1.GrowthBook(context);
27
+ if (forcedFeatures) {
28
+ gb.setForcedFeatures(forcedFeatures);
29
+ }
30
+ if (ctx === null || ctx === void 0 ? void 0 : ctx.verboseDebugging) {
31
+ gb.debug = true;
32
+ }
33
+ const gbFeatures = gb.getFeatures();
34
+ for (const key in gbFeatures) {
35
+ const result = gb.evalFeature(key);
36
+ if (result.on || result.experiment) {
37
+ // reduced feature definition
38
+ evaluatedFeatures[key] = {
39
+ defaultValue: result.value,
40
+ };
41
+ if (result.source === "experiment") {
42
+ // reduced experiment definition for tracking
43
+ const scrubbedResultExperiment = ((_a = result === null || result === void 0 ? void 0 : result.experimentResult) === null || _a === void 0 ? void 0 : _a.variationId) !== undefined
44
+ ? scrubExperiment(result.experiment, result.experimentResult.variationId)
45
+ : result.experiment;
46
+ evaluatedFeatures[key].rules = [
47
+ {
48
+ force: result.value,
49
+ tracks: [{ experiment: scrubbedResultExperiment, result }],
50
+ },
51
+ ];
52
+ }
53
+ }
54
+ }
55
+ const gbExperiments = gb.getExperiments();
56
+ for (const experiment of gbExperiments) {
57
+ const result = gb.run(experiment);
58
+ if (result.inExperiment) {
59
+ // reduced experiment definition
60
+ const evaluatedExperiment = scrubExperiment(experiment, result.variationId);
61
+ evaluatedExperiments.push(evaluatedExperiment);
62
+ }
63
+ }
64
+ }
65
+ return Object.assign(Object.assign({}, payload), { features: evaluatedFeatures, experiments: evaluatedExperiments });
66
+ }
67
+ exports.evaluateFeatures = evaluateFeatures;
68
+ function scrubExperiment(experiment, allowedVariation) {
69
+ const scrubbedExperiment = Object.assign(Object.assign({}, experiment), { variations: experiment.variations.map((v, i) => allowedVariation === i ? v : {}) });
70
+ delete scrubbedExperiment.condition;
71
+ return scrubbedExperiment;
72
+ }
73
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uDAAuD;AACvD,uDAA0E;AAE1E,SAAgB,gBAAgB,CAAC,EAC/B,OAAO,EACP,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,GAAG,EACH,GAAG,GAQJ;;IACC,MAAM,iBAAiB,GAAwB,EAAE,CAAC;IAClD,MAAM,oBAAoB,GAAU,EAAE,CAAC;IAEvC,MAAM,QAAQ,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,CAAC;IACnC,MAAM,WAAW,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,WAAW,CAAC;IACzC,MAAM,OAAO,GAAc,EAAE,UAAU,EAAE,CAAC;IAC1C,IAAI,QAAQ,EAAE;QACZ,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAC;KAC7B;IACD,IAAI,WAAW,EAAE;QACf,OAAO,CAAC,WAAW,GAAG,WAAW,CAAC;KACnC;IACD,IAAI,gBAAgB,EAAE;QACpB,OAAO,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;KAC7C;IACD,IAAI,GAAG,KAAK,SAAS,EAAE;QACrB,OAAO,CAAC,GAAG,GAAG,GAAG,CAAC;KACnB;IAED,IAAI,QAAQ,IAAI,WAAW,EAAE;QAC3B,MAAM,EAAE,GAAG,IAAI,uBAAU,CAAC,OAAO,CAAC,CAAC;QACnC,IAAI,cAAc,EAAE;YAClB,EAAE,CAAC,iBAAiB,CAAC,cAA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zC,UAAU,EACV,MAAM,CAAC,WAAW,CACnB,CAAC;gBACF,oBAAoB,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;aAChD;SACF;KACF;IAED,uCACK,OAAO,KACV,QAAQ,EAAE,iBAAiB,EAC3B,WAAW,EAAE,oBAAoB,IACjC;AACJ,CAAC;AAzFD,4CAyFC;AAED,SAAS,eAAe,CAAC,UAAe,EAAE,gBAAwB;IAChE,MAAM,kBAAkB,mCACnB,UAAU,KACb,UAAU,EAAE,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAM,EAAE,CAAS,EAAE,EAAE,CAC1D,gBAAgB,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAChC,GACF,CAAC;IACF,OAAO,kBAAkB,CAAC,SAAS,CAAC;IACpC,OAAO,kBAAkB,CAAC;AAC5B,CAAC"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@growthbook/proxy-eval",
3
+ "description": "Remote evaluation service for proxies, edge workers, or backend APIs",
4
+ "version": "1.0.0",
5
+ "main": "dist/index.js",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/growthbook/growthbook-proxy.git",
10
+ "directory": "packages/shared/eval"
11
+ },
12
+ "author": "Bryce Fitzsimons",
13
+ "scripts": {
14
+ "build:clean": "rimraf -rf dist",
15
+ "build:typescript": "tsc",
16
+ "build": "yarn build:clean && yarn build:typescript",
17
+ "type-check": "tsc --pretty --noEmit",
18
+ "dev": "tsc --watch"
19
+ },
20
+ "dependencies": {
21
+ "@growthbook/growthbook": "^0.29.0"
22
+ },
23
+ "devDependencies": {
24
+ "rimraf": "^5.0.5",
25
+ "typescript": "5.2.2"
26
+ }
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,104 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { GrowthBook, Context as GBContext } from "@growthbook/growthbook";
3
+
4
+ export function evaluateFeatures({
5
+ payload,
6
+ attributes,
7
+ forcedVariations,
8
+ forcedFeatures,
9
+ url,
10
+ ctx,
11
+ }: {
12
+ payload: any;
13
+ attributes: Record<string, any>;
14
+ forcedVariations?: Record<string, number>;
15
+ forcedFeatures?: Map<string, any>;
16
+ url?: string;
17
+ ctx?: any;
18
+ }) {
19
+ const evaluatedFeatures: Record<string, any> = {};
20
+ const evaluatedExperiments: any[] = [];
21
+
22
+ const features = payload?.features;
23
+ const experiments = payload?.experiments;
24
+ const context: GBContext = { attributes };
25
+ if (features) {
26
+ context.features = features;
27
+ }
28
+ if (experiments) {
29
+ context.experiments = experiments;
30
+ }
31
+ if (forcedVariations) {
32
+ context.forcedVariations = forcedVariations;
33
+ }
34
+ if (url !== undefined) {
35
+ context.url = url;
36
+ }
37
+
38
+ if (features || experiments) {
39
+ const gb = new GrowthBook(context);
40
+ if (forcedFeatures) {
41
+ gb.setForcedFeatures(forcedFeatures);
42
+ }
43
+ if (ctx?.verboseDebugging) {
44
+ gb.debug = true;
45
+ }
46
+
47
+ const gbFeatures = gb.getFeatures();
48
+ for (const key in gbFeatures) {
49
+ const result = gb.evalFeature(key);
50
+ if (result.on || result.experiment) {
51
+ // reduced feature definition
52
+ evaluatedFeatures[key] = {
53
+ defaultValue: result.value,
54
+ };
55
+ if (result.source === "experiment") {
56
+ // reduced experiment definition for tracking
57
+ const scrubbedResultExperiment =
58
+ result?.experimentResult?.variationId !== undefined
59
+ ? scrubExperiment(
60
+ result.experiment,
61
+ result.experimentResult.variationId,
62
+ )
63
+ : result.experiment;
64
+ evaluatedFeatures[key].rules = [
65
+ {
66
+ force: result.value,
67
+ tracks: [{ experiment: scrubbedResultExperiment, result }],
68
+ },
69
+ ];
70
+ }
71
+ }
72
+ }
73
+
74
+ const gbExperiments = gb.getExperiments();
75
+ for (const experiment of gbExperiments) {
76
+ const result = gb.run(experiment);
77
+ if (result.inExperiment) {
78
+ // reduced experiment definition
79
+ const evaluatedExperiment = scrubExperiment(
80
+ experiment,
81
+ result.variationId,
82
+ );
83
+ evaluatedExperiments.push(evaluatedExperiment);
84
+ }
85
+ }
86
+ }
87
+
88
+ return {
89
+ ...payload,
90
+ features: evaluatedFeatures,
91
+ experiments: evaluatedExperiments,
92
+ };
93
+ }
94
+
95
+ function scrubExperiment(experiment: any, allowedVariation: number) {
96
+ const scrubbedExperiment = {
97
+ ...experiment,
98
+ variations: experiment.variations.map((v: any, i: number) =>
99
+ allowedVariation === i ? v : {},
100
+ ),
101
+ };
102
+ delete scrubbedExperiment.condition;
103
+ return scrubbedExperiment;
104
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "strict": true,
5
+ "esModuleInterop": true,
6
+ "allowSyntheticDefaultImports": true,
7
+ "target": "es6",
8
+ "noImplicitAny": true,
9
+ "moduleResolution": "node",
10
+ "sourceMap": true,
11
+ "outDir": "dist",
12
+ "declaration": true,
13
+ "typeRoots": ["node_modules/@types", "./typings"],
14
+ "strictNullChecks": true,
15
+ "strictBindCallApply": true,
16
+ "strictPropertyInitialization": true,
17
+ "incremental": true
18
+ },
19
+ "include": [
20
+ "src/**/*"
21
+ ]
22
+ }