@statsig/serverless-client 3.28.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,14 @@
1
+ ISC License (ISC)
2
+ Copyright (c) 2021, Statsig, Inc.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any purpose
5
+ with or without fee is hereby granted, provided that the above copyright notice
6
+ and this permission notice appear in all copies.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
10
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
12
+ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
13
+ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
14
+ THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Statsig helps you move faster with feature gates (feature flags), and/or dynamic configs. It also allows you to run A/B/n tests to validate your new features and understand their impact on your KPIs. If you're new to Statsig, check out our product and create an account at [statsig.com](https://www.statsig.com/?ref=gh_jsm).
2
+
3
+ <h1 align="center">
4
+ <a href="https://statsig.com/?ref=gh_jsm">
5
+ <img src="https://github.com/statsig-io/js-client-monorepo/assets/95646168/ae5499ed-20ff-4584-bf21-8857f800d485" />
6
+ </a>
7
+ <div />
8
+ <a href="https://statsig.com/?ref=gh_jsm">Statsig</a>
9
+ </h1>
10
+
11
+ <p align="center">
12
+ <a href="https://github.com/statsig-io/js-client-monorepo/blob/main/LICENSE">
13
+ <img src="https://img.shields.io/badge/license-ISC-blue.svg?colorA=1b2528&colorB=ccfbc7&style=for-the-badge">
14
+ </a>
15
+ <a href="https://www.npmjs.com/package/@statsig/js-client">
16
+ <img src="https://img.shields.io/npm/v/@statsig/js-client.svg?colorA=1b2528&colorB=b2d3ff&style=for-the-badge">
17
+ </a>
18
+ <a href="https://statsig.com/community?ref=gh_jsm">
19
+ <img src="https://img.shields.io/badge/slack-statsig-brightgreen.svg?logo=slack&colorA=1b2528&colorB=FFF8BA&style=for-the-badge">
20
+ </a>
21
+ </p>
22
+
23
+ ## Getting Started
24
+
25
+ Read through the [Documentation](https://docs.statsig.com/client/javascript-sdk?ref=gh_jsm) or check out the [Samples](samples/).
26
+
27
+ ## Packages
28
+
29
+ Clients
30
+
31
+ - Precomputed Evaluations (Recommended) [[npm](https://www.npmjs.com/package/@statsig/js-client)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/js-client)]
32
+ - On Device Evaluations [[npm](https://www.npmjs.com/package/@statsig/js-on-device-eval-client)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/js-on-device-eval-client)]
33
+
34
+ Product Bundles
35
+
36
+ - Session Replay [[npm](https://www.npmjs.com/package/@statsig/session-replay)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/session-replay)]
37
+ - Web Analytics [[npm](https://www.npmjs.com/package/@statsig/web-analytics)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/web-analytics)]
38
+
39
+ Framework Specific Bindings
40
+
41
+ - React [[npm](https://www.npmjs.com/package/@statsig/react-bindings)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/react-bindings)]
42
+
43
+ - React Native [[npm](https://www.npmjs.com/package/@statsig/react-native-bindings)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/react-native-bindings)]
44
+
45
+ - React Native (Expo) [[npm](https://www.npmjs.com/package/@statsig/expo-bindings)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/expo-bindings)]
46
+
47
+ ## Community
48
+
49
+ If you need any assistance or just have a question, feel free to reach out to us on [Slack](https://statsig.com/community?ref=gh_jsm).
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@statsig/serverless-client",
3
+ "version": "3.28.0",
4
+ "license": "ISC",
5
+ "homepage": "https://github.com/statsig-io/js-client-monorepo",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "github:statsig-io/js-client-monorepo",
9
+ "directory": "packages/serverless"
10
+ },
11
+ "dependencies": {
12
+ "@statsig/client-core": "3.28.0",
13
+ "@statsig/js-on-device-eval-client": "3.28.0"
14
+ },
15
+ "type": "commonjs",
16
+ "main": "./src/index.js",
17
+ "typings": "./src/index.d.ts",
18
+ "exports": {
19
+ "./cloudflare": {
20
+ "types": "./src/provider/cloudflare.d.ts",
21
+ "default": "./src/provider/cloudflare.js"
22
+ }
23
+ },
24
+ "devDependencies": {
25
+ "ts-node": "^10.9.1"
26
+ }
27
+ }
@@ -0,0 +1,4 @@
1
+ import { StatsigOnDeviceEvalClient, StatsigOptions } from '@statsig/js-on-device-eval-client';
2
+ export declare class StatsigServerlessClient extends StatsigOnDeviceEvalClient {
3
+ constructor(sdkKey: string, options?: StatsigOptions | null);
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StatsigServerlessClient = void 0;
4
+ const js_on_device_eval_client_1 = require("@statsig/js-on-device-eval-client");
5
+ class StatsigServerlessClient extends js_on_device_eval_client_1.StatsigOnDeviceEvalClient {
6
+ constructor(sdkKey, options = null) {
7
+ const edgeSafeOptions = Object.assign({ disableStorage: true, loggingEnabled: 'always', includeCurrentPageUrlWithEvents: false }, options);
8
+ super(sdkKey, edgeSafeOptions);
9
+ }
10
+ }
11
+ exports.StatsigServerlessClient = StatsigServerlessClient;
package/src/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type { StatsigUser, FeatureGateEvaluationOptions, } from '@statsig/client-core';
2
+ export type { StatsigOptions } from '@statsig/js-on-device-eval-client';
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,28 @@
1
+ import { StatsigUpdateDetails } from '@statsig/client-core';
2
+ import type { StatsigOptions } from '@statsig/js-on-device-eval-client';
3
+ import { StatsigServerlessClient } from '../StatsigServerlessClient';
4
+ type KVNamespace = {
5
+ get(key: string): Promise<string | null>;
6
+ };
7
+ export type { StatsigUser, FeatureGateEvaluationOptions, } from '@statsig/client-core';
8
+ export type { StatsigOptions } from '@statsig/js-on-device-eval-client';
9
+ export declare class StatsigCloudflareClient extends StatsigServerlessClient {
10
+ initializeFromKV(kvBinding: KVNamespace, kvKey: string): Promise<StatsigUpdateDetails>;
11
+ }
12
+ interface ExecutionContext {
13
+ waitUntil(promise: Promise<any>): void;
14
+ }
15
+ type Env = Record<string, any>;
16
+ export interface statsig {
17
+ (request: Request, env: Env, ctx: ExecutionContext, client: StatsigCloudflareClient): Promise<Response> | Response;
18
+ }
19
+ export interface paramsObject {
20
+ kvKey: string;
21
+ envStatsigKey: string;
22
+ envKvBindingName: string;
23
+ statsigOptions?: StatsigOptions;
24
+ }
25
+ interface WorkerExport {
26
+ fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
27
+ }
28
+ export declare function handleWithStatsig(handler: statsig, params: paramsObject): WorkerExport;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.handleWithStatsig = exports.StatsigCloudflareClient = void 0;
13
+ const client_core_1 = require("@statsig/client-core");
14
+ const StatsigServerlessClient_1 = require("../StatsigServerlessClient");
15
+ class StatsigCloudflareClient extends StatsigServerlessClient_1.StatsigServerlessClient {
16
+ initializeFromKV(kvBinding, kvKey) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ const startTime = performance.now();
19
+ if (!kvBinding) {
20
+ client_core_1.Log.error('Invalid KV binding provided');
21
+ return {
22
+ duration: performance.now() - startTime,
23
+ source: 'Bootstrap',
24
+ success: false,
25
+ error: new Error('Invalid KV binding provided'),
26
+ sourceUrl: null,
27
+ };
28
+ }
29
+ if (!kvKey || typeof kvKey !== 'string' || kvKey.trim().length === 0) {
30
+ client_core_1.Log.error('Invalid KV key provided');
31
+ return {
32
+ duration: performance.now() - startTime,
33
+ source: 'Bootstrap',
34
+ success: false,
35
+ error: new Error('Invalid KV key provided'),
36
+ sourceUrl: null,
37
+ };
38
+ }
39
+ try {
40
+ const specs = yield kvBinding.get(kvKey);
41
+ if (specs) {
42
+ this.dataAdapter.setData(specs);
43
+ return this.initializeSync({
44
+ disableBackgroundCacheRefresh: true,
45
+ });
46
+ }
47
+ else {
48
+ client_core_1.Log.error(`Failed to fetch specs from Cloudflare KV for key "${kvKey}"`);
49
+ return {
50
+ duration: performance.now() - startTime,
51
+ source: 'Bootstrap',
52
+ success: false,
53
+ error: new Error(`Failed to fetch specs from Cloudflare KV for key "${kvKey}"`),
54
+ sourceUrl: null,
55
+ };
56
+ }
57
+ }
58
+ catch (error) {
59
+ client_core_1.Log.error(`Failed to fetch specs from Cloudflare KV for key: "${kvKey}"`, error);
60
+ return {
61
+ duration: performance.now() - startTime,
62
+ source: 'Bootstrap',
63
+ success: false,
64
+ error: new Error(`Failed to fetch specs from Cloudflare KV for key: "${kvKey}"`),
65
+ sourceUrl: null,
66
+ };
67
+ }
68
+ });
69
+ }
70
+ }
71
+ exports.StatsigCloudflareClient = StatsigCloudflareClient;
72
+ function handleWithStatsig(handler, params) {
73
+ return {
74
+ fetch(request, env, ctx) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ const envStatsigKey = params.envStatsigKey;
77
+ const envKvBindingName = params.envKvBindingName;
78
+ const kvKey = params.kvKey;
79
+ if (!env[envStatsigKey]) {
80
+ return new Response(JSON.stringify({
81
+ error: `Missing ${envStatsigKey} environment variable`,
82
+ }), {
83
+ headers: { 'Content-Type': 'application/json' },
84
+ });
85
+ }
86
+ if (!env[envKvBindingName]) {
87
+ return new Response(JSON.stringify({ error: `Missing ${envKvBindingName} Binding` }), {
88
+ headers: { 'Content-Type': 'application/json' },
89
+ });
90
+ }
91
+ const client = new StatsigCloudflareClient(env[envStatsigKey], params.statsigOptions);
92
+ yield client.initializeFromKV(env[envKvBindingName], env[kvKey]);
93
+ const response = yield handler(request, env, ctx, client);
94
+ if (response.status >= 200 && response.status < 300) {
95
+ ctx.waitUntil(client.flush());
96
+ }
97
+ return response;
98
+ });
99
+ },
100
+ };
101
+ }
102
+ exports.handleWithStatsig = handleWithStatsig;