@jewel998/config 0.1.0 → 0.1.3
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 +84 -0
- package/README.md +234 -0
- package/dist/client/ConfigClient.d.ts +3 -1
- package/dist/client/ConfigClient.d.ts.map +1 -1
- package/dist/client/ConfigClient.js +53 -19
- package/dist/client/ConfigClient.js.map +1 -1
- package/dist/context/autoContext.d.ts +28 -0
- package/dist/context/autoContext.d.ts.map +1 -0
- package/dist/context/autoContext.js +135 -0
- package/dist/context/autoContext.js.map +1 -0
- package/dist/createConfig.d.ts.map +1 -1
- package/dist/createConfig.js +31 -7
- package/dist/createConfig.js.map +1 -1
- package/dist/index.d.ts +6 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -9
- package/dist/index.js.map +1 -1
- package/dist/initConfig.d.ts +66 -0
- package/dist/initConfig.d.ts.map +1 -0
- package/dist/initConfig.js +128 -0
- package/dist/initConfig.js.map +1 -0
- package/dist/loading/pessimistic.d.ts.map +1 -1
- package/dist/loading/pessimistic.js +12 -5
- package/dist/loading/pessimistic.js.map +1 -1
- package/dist/plugins/models.d.ts +1 -0
- package/dist/plugins/models.d.ts.map +1 -1
- package/dist/plugins/prerequisites/prerequisitePlugin.d.ts.map +1 -1
- package/dist/plugins/prerequisites/prerequisitePlugin.js +22 -2
- package/dist/plugins/prerequisites/prerequisitePlugin.js.map +1 -1
- package/dist/transport/HttpTransport.d.ts +4 -0
- package/dist/transport/HttpTransport.d.ts.map +1 -1
- package/dist/transport/HttpTransport.js +15 -4
- package/dist/transport/HttpTransport.js.map +1 -1
- package/dist/types.d.ts +24 -37
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/webhook-formats.d.ts +92 -0
- package/dist/webhook-formats.d.ts.map +1 -0
- package/dist/webhook-formats.js +141 -0
- package/dist/webhook-formats.js.map +1 -0
- package/package.json +21 -3
- package/dist/cache/storage.d.ts +0 -15
- package/dist/cache/storage.d.ts.map +0 -1
- package/dist/cache/storage.js +0 -43
- package/dist/cache/storage.js.map +0 -1
- package/dist/cache/types.d.ts +0 -3
- package/dist/cache/types.js +0 -2
- package/dist/cache/types.js.map +0 -1
- package/dist/client.d.ts +0 -15
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -80
- package/dist/client.js.map +0 -1
- package/dist/core/resolver.d.ts +0 -8
- package/dist/core/resolver.d.ts.map +0 -1
- package/dist/core/resolver.js +0 -22
- package/dist/core/resolver.js.map +0 -1
- package/dist/errors/index.d.ts +0 -17
- package/dist/errors/index.d.ts.map +0 -1
- package/dist/errors/index.js +0 -29
- package/dist/errors/index.js.map +0 -1
- package/dist/example.d.ts +0 -2
- package/dist/example.d.ts.map +0 -1
- package/dist/example.js +0 -23
- package/dist/example.js.map +0 -1
- package/dist/loading/deferred.d.ts +0 -3
- package/dist/loading/deferred.js +0 -5
- package/dist/loading/deferred.js.map +0 -1
- package/dist/loading/optimistic.d.ts +0 -3
- package/dist/loading/pessimistic.d.ts +0 -3
- package/dist/management/index.d.ts +0 -3
- package/dist/management/index.d.ts.map +0 -1
- package/dist/management/index.js +0 -2
- package/dist/management/index.js.map +0 -1
- package/dist/management/manager.d.ts +0 -34
- package/dist/management/manager.d.ts.map +0 -1
- package/dist/management/manager.js +0 -21
- package/dist/management/manager.js.map +0 -1
- package/dist/management/memory-backend.d.ts +0 -6
- package/dist/management/memory-backend.d.ts.map +0 -1
- package/dist/management/memory-backend.js +0 -78
- package/dist/management/memory-backend.js.map +0 -1
- package/dist/plugins/index.d.ts +0 -5
- package/dist/plugins/lifecycle/index.d.ts +0 -4
- package/dist/plugins/overrides/index.d.ts +0 -2
- package/dist/plugins/prerequisites/index.d.ts +0 -3
- package/dist/plugins/rollout/index.d.ts +0 -3
- package/dist/plugins/rollout/murmurhash3.d.ts +0 -6
- package/dist/plugins/rollout/murmurhash3.d.ts.map +0 -1
- package/dist/plugins/rollout/murmurhash3.js +0 -56
- package/dist/plugins/rollout/murmurhash3.js.map +0 -1
- package/dist/plugins/schedule/index.d.ts +0 -2
- package/dist/plugins/targeting/index.d.ts +0 -4
- package/dist/plugins/targeting/predicates.d.ts +0 -23
- package/dist/plugins/targeting/predicates.js +0 -110
- package/dist/plugins/targeting/predicates.js.map +0 -1
- package/dist/plugins/types.d.ts +0 -35
- package/dist/plugins/types.js +0 -13
- package/dist/plugins/types.js.map +0 -1
- package/dist/registry.d.ts +0 -9
- package/dist/registry.d.ts.map +0 -1
- package/dist/registry.js +0 -22
- package/dist/registry.js.map +0 -1
- package/dist/remote/firebase-adapter.d.ts +0 -6
- package/dist/remote/firebase-adapter.d.ts.map +0 -1
- package/dist/remote/firebase-adapter.js +0 -12
- package/dist/remote/firebase-adapter.js.map +0 -1
- package/dist/remote/provider.d.ts +0 -6
- package/dist/remote/provider.d.ts.map +0 -1
- package/dist/remote/provider.js +0 -5
- package/dist/remote/provider.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Elastic License 2.0 (ELv2)
|
|
2
|
+
|
|
3
|
+
## Acceptance
|
|
4
|
+
|
|
5
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
6
|
+
|
|
7
|
+
## Copyright License
|
|
8
|
+
|
|
9
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
10
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
11
|
+
available, and prepare derivative works of the software, in each case subject
|
|
12
|
+
to the limitations below.
|
|
13
|
+
|
|
14
|
+
## Limitations
|
|
15
|
+
|
|
16
|
+
You may not provide the software to third parties as a hosted or managed
|
|
17
|
+
service, where the service provides users with access to any substantial set
|
|
18
|
+
of the features or functionality of the software.
|
|
19
|
+
|
|
20
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
21
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
22
|
+
software that is protected by the license key.
|
|
23
|
+
|
|
24
|
+
You may not alter, remove, or obscure any licensing, copyright, or other
|
|
25
|
+
notices of the licensor in the software.
|
|
26
|
+
|
|
27
|
+
## Patents
|
|
28
|
+
|
|
29
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
30
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
31
|
+
sale, import and have imported the software, in each case subject to the
|
|
32
|
+
limitations and conditions in this license. This license does not cover any
|
|
33
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
34
|
+
the software.
|
|
35
|
+
|
|
36
|
+
## No Other Rights
|
|
37
|
+
|
|
38
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
39
|
+
these terms.
|
|
40
|
+
|
|
41
|
+
## Termination
|
|
42
|
+
|
|
43
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
44
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
45
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
46
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
47
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
48
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
49
|
+
to terminate automatically and permanently.
|
|
50
|
+
|
|
51
|
+
## No Liability
|
|
52
|
+
|
|
53
|
+
As far as the law allows, the software comes as is, without any warranty or
|
|
54
|
+
condition, and the licensor will not be liable to you for any damages arising
|
|
55
|
+
out of these terms or the use or nature of the software, under any kind of
|
|
56
|
+
legal claim.
|
|
57
|
+
|
|
58
|
+
## Definitions
|
|
59
|
+
|
|
60
|
+
The **licensor** is the entity offering these terms, and the **software** is
|
|
61
|
+
the software the licensor makes available under these terms, including any
|
|
62
|
+
portion of it.
|
|
63
|
+
|
|
64
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
65
|
+
|
|
66
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of
|
|
67
|
+
organization that you work for, plus all organizations that have control over,
|
|
68
|
+
are under the control of, or are under common control with that organization.
|
|
69
|
+
|
|
70
|
+
**Control** means ownership of substantially all the assets of an entity, or
|
|
71
|
+
the power to direct its management and policies by vote, contract, or
|
|
72
|
+
otherwise. Control can be direct or indirect.
|
|
73
|
+
|
|
74
|
+
**Your licenses** are all the licenses granted to you for the software under
|
|
75
|
+
these terms.
|
|
76
|
+
|
|
77
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
|
78
|
+
|
|
79
|
+
**Trademark** this license does not grant any rights in the trademarks, service
|
|
80
|
+
marks, brand names, or logos of the licensor.
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
Copyright (c) 2024-present Jyotirmoy Barman. All Rights Reserved.
|
package/README.md
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# @jewel998/config
|
|
2
|
+
|
|
3
|
+
A free, self-hostable feature flag and remote configuration platform. Deploy to your own Firebase project. Replace LaunchDarkly at $0/month.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Feature flags** — Boolean, string, number, JSON, array value types
|
|
8
|
+
- **Targeting rules** — Serve different values based on user attributes (plan, country, etc.)
|
|
9
|
+
- **Segments** — Reusable audience groups with one-click targeting
|
|
10
|
+
- **Percentage rollouts** — Gradually roll out features with deterministic bucketing
|
|
11
|
+
- **Prerequisites** — Flag dependencies with operator support (equals, greater_than, etc.)
|
|
12
|
+
- **Scheduling** — Schedule config changes for a future date/time
|
|
13
|
+
- **Environments** — Separate dev/staging/production configs
|
|
14
|
+
- **Audit log** — Track every change with actor, timestamp, and diff viewer
|
|
15
|
+
- **Webhooks** — Notify Slack, Discord, Google Chat, MS Teams, or custom endpoints
|
|
16
|
+
- **RBAC** — Viewer, Editor, Admin roles per project
|
|
17
|
+
- **Server-side evaluation** — Client keys never expose targeting rules or segments
|
|
18
|
+
- **SDK** — Optimistic defaults, auto-context detection, instant reads
|
|
19
|
+
- **Self-hosted** — Runs entirely on Firebase free tier ($0/month)
|
|
20
|
+
- **7 languages** — English, Spanish, French, Arabic, Chinese, Hindi, Japanese
|
|
21
|
+
|
|
22
|
+
## Self-Hosting
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# 1. Clone
|
|
26
|
+
git clone https://github.com/jewel998/config && cd config
|
|
27
|
+
|
|
28
|
+
# 2. Connect your Firebase project
|
|
29
|
+
firebase use --add your-project-id
|
|
30
|
+
|
|
31
|
+
# 3. Install and deploy
|
|
32
|
+
pnpm install && firebase deploy
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
That's it. Portal, API, and hosting — all on your Firebase project.
|
|
36
|
+
|
|
37
|
+
## SDK Quick Start
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @jewel998/config
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { initConfig, autoContext } from "@jewel998/config";
|
|
45
|
+
|
|
46
|
+
const flags = initConfig({
|
|
47
|
+
clientId: "cid_xxx", // From your Portal → API Keys
|
|
48
|
+
baseUrl: "https://your-project.web.app/api", // Your Firebase URL
|
|
49
|
+
defaults: {
|
|
50
|
+
"feature.dark_mode": false,
|
|
51
|
+
"app.upload_limit": 50,
|
|
52
|
+
},
|
|
53
|
+
context: autoContext({ userId: "user_123", plan: "pro" }),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Instant — returns default value, no loading state
|
|
57
|
+
flags.get("feature.dark_mode"); // → false
|
|
58
|
+
|
|
59
|
+
// After API responds, returns resolved value
|
|
60
|
+
flags.on("updated", () => {
|
|
61
|
+
flags.get("feature.dark_mode"); // → true (matched targeting rule)
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### API Key Types
|
|
66
|
+
|
|
67
|
+
| Prefix | Type | Behavior |
|
|
68
|
+
| ------ | ------ | ------------------------------------------------------------------------------------------------------------------- |
|
|
69
|
+
| `cid_` | Client | For frontend. API evaluates targeting server-side, returns only values. Targeting rules and segments never exposed. |
|
|
70
|
+
| `svr_` | Server | For backend. API returns full flag data + segments for local evaluation via plugins. |
|
|
71
|
+
|
|
72
|
+
The API enforces the mode based on key prefix — cannot be overridden.
|
|
73
|
+
|
|
74
|
+
### Server Key (Backend)
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { createConfig } from "@jewel998/config";
|
|
78
|
+
import { targetingPlugin } from "@jewel998/config/targeting";
|
|
79
|
+
import { rolloutPlugin } from "@jewel998/config/rollout";
|
|
80
|
+
|
|
81
|
+
const config = createConfig({
|
|
82
|
+
clientId: "svr_xxx",
|
|
83
|
+
baseUrl: "https://your-project.web.app/api",
|
|
84
|
+
plugins: [targetingPlugin(), rolloutPlugin()],
|
|
85
|
+
context: { userId: "user_123", attributes: { plan: "pro", country: "US" } },
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
config.getFlag("feature.checkout_v2"); // Evaluated locally — no round-trip
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Architecture
|
|
92
|
+
|
|
93
|
+
```
|
|
94
|
+
┌─────────────────────────────────────────────────────┐
|
|
95
|
+
│ Your App │
|
|
96
|
+
│ │
|
|
97
|
+
│ import { initConfig } from "@jewel998/config" │
|
|
98
|
+
│ const flags = initConfig({ clientId, baseUrl }) │
|
|
99
|
+
│ flags.get("feature.dark_mode") → true │
|
|
100
|
+
└──────────────────────┬──────────────────────────────┘
|
|
101
|
+
│ POST /api/getConfig
|
|
102
|
+
▼
|
|
103
|
+
┌──────────────────────────────────────────────────────┐
|
|
104
|
+
│ Your Firebase Hosting (CDN cached) │
|
|
105
|
+
└──────────────────────┬───────────────────────────────┘
|
|
106
|
+
│ cache miss
|
|
107
|
+
▼
|
|
108
|
+
┌──────────────────────────────────────────────────────┐
|
|
109
|
+
│ Your Cloud Function: getConfig │
|
|
110
|
+
│ - Validates clientId (cid_ or svr_) │
|
|
111
|
+
│ - cid_: evaluates targeting server-side │
|
|
112
|
+
│ - svr_: returns full flag data for local eval │
|
|
113
|
+
└──────────────────────┬───────────────────────────────┘
|
|
114
|
+
│
|
|
115
|
+
▼
|
|
116
|
+
┌──────────────────────────────────────────────────────┐
|
|
117
|
+
│ Your Firestore (configs, segments, projects) │
|
|
118
|
+
└──────────────────────────────────────────────────────┘
|
|
119
|
+
▲
|
|
120
|
+
│
|
|
121
|
+
┌──────────────────────┴───────────────────────────────┐
|
|
122
|
+
│ Your Portal (React SPA on Firebase Hosting) │
|
|
123
|
+
│ - Create/edit feature flags and segments │
|
|
124
|
+
│ - Set targeting rules, rollouts, schedules │
|
|
125
|
+
│ - Manage team, environments, API keys │
|
|
126
|
+
│ - View audit log with diff viewer │
|
|
127
|
+
│ - Configure webhook notifications │
|
|
128
|
+
└──────────────────────────────────────────────────────┘
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## API Reference
|
|
132
|
+
|
|
133
|
+
### POST /api/getConfig
|
|
134
|
+
|
|
135
|
+
The SDK calls this endpoint. Mode is determined by the API key prefix.
|
|
136
|
+
|
|
137
|
+
**Request:**
|
|
138
|
+
|
|
139
|
+
```json
|
|
140
|
+
{
|
|
141
|
+
"data": {
|
|
142
|
+
"clientId": "cid_xxx",
|
|
143
|
+
"context": {
|
|
144
|
+
"userId": "user_123",
|
|
145
|
+
"attributes": { "plan": "pro", "country": "US" }
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**Response (cid\_ key) — resolved values only:**
|
|
152
|
+
|
|
153
|
+
```json
|
|
154
|
+
{
|
|
155
|
+
"data": { "feature.dark_mode": true, "app.upload_limit": 200 },
|
|
156
|
+
"version": "3",
|
|
157
|
+
"timestamp": "2025-01-15T09:30:00.000Z"
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**Response (svr\_ key) — full flag data + segments:**
|
|
162
|
+
|
|
163
|
+
```json
|
|
164
|
+
{
|
|
165
|
+
"data": {
|
|
166
|
+
"feature.dark_mode": {
|
|
167
|
+
"key": "feature.dark_mode",
|
|
168
|
+
"value": false,
|
|
169
|
+
"targetingRules": [...]
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
"segments": { "seg_beta": { "id": "...", "conditions": [...] } },
|
|
173
|
+
"version": "3",
|
|
174
|
+
"timestamp": "2025-01-15T09:30:00.000Z"
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
**Errors:** `401` Invalid key · `403` Domain not allowed · `413` Context too large
|
|
179
|
+
|
|
180
|
+
## Monorepo Structure
|
|
181
|
+
|
|
182
|
+
```
|
|
183
|
+
apps/
|
|
184
|
+
portal/ — Admin portal (React + Vite + TanStack Router)
|
|
185
|
+
landing/ — Marketing site (Astro + React)
|
|
186
|
+
docs/ — Documentation (VitePress)
|
|
187
|
+
packages/
|
|
188
|
+
config/ — SDK (@jewel998/config)
|
|
189
|
+
tour/ — Tour framework (@jewel998/tour)
|
|
190
|
+
functions/ — Firebase Cloud Functions (API + webhooks)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Development
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
pnpm install # Install deps
|
|
197
|
+
pnpm --filter @jewel998/config-portal run dev # Portal dev server
|
|
198
|
+
pnpm --filter @jewel998/config-landing run dev # Landing page dev
|
|
199
|
+
pnpm --filter @jewel998/config run test # SDK tests (210 passing)
|
|
200
|
+
firebase deploy # Deploy everything
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Upgrades
|
|
204
|
+
|
|
205
|
+
When new versions are released, pull the latest and redeploy:
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
git pull origin main
|
|
209
|
+
pnpm install
|
|
210
|
+
firebase deploy
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Migration guides are provided for breaking changes. Your data stays in your Firestore.
|
|
214
|
+
|
|
215
|
+
## Cost
|
|
216
|
+
|
|
217
|
+
The SDK polls a lightweight version endpoint (100 bytes) every 5 minutes. CDN absorbs 99%+ of these requests. Full config fetches only happen on init or version change.
|
|
218
|
+
|
|
219
|
+
| Scale | Monthly Cost | Notes |
|
|
220
|
+
| -------------------- | ------------ | --------------------------------------------- |
|
|
221
|
+
| 1–1,000 users | **$0** | Well within Firebase Spark (free) tier |
|
|
222
|
+
| 1,000–50,000 users | **$0** | CDN handles most version polls at edge |
|
|
223
|
+
| 50,000–100,000 users | **$0–5** | May slightly exceed free function invocations |
|
|
224
|
+
| 100,000+ users | **$5–15** | Blaze plan with minimal overage |
|
|
225
|
+
|
|
226
|
+
Firebase free tier: 2M function invocations/month, 50K Firestore reads/day, 10GB bandwidth.
|
|
227
|
+
|
|
228
|
+
## License
|
|
229
|
+
|
|
230
|
+
[Elastic License 2.0](./LICENSE) — Use, modify, and self-host freely. Cannot be provided as a managed service to third parties.
|
|
231
|
+
|
|
232
|
+
## Contributing
|
|
233
|
+
|
|
234
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for setup instructions, commit conventions, and PR guidelines.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { EvaluationContext, EvaluationPlugin } from "../plugins/types.js";
|
|
2
|
-
import type { CacheStorage, ConfigClient, ConfigFetcher, EventEmitterInterface, FetchGranularity, RetryConfig } from "../types";
|
|
2
|
+
import type { CacheStorage, ConfigClient, ConfigFetcher, EvaluationMode, EventEmitterInterface, FetchGranularity, RetryConfig } from "../types";
|
|
3
3
|
export interface ConfigClientInternals {
|
|
4
4
|
data: Record<string, unknown>;
|
|
5
5
|
cache: CacheStorage;
|
|
@@ -11,6 +11,8 @@ export interface ConfigClientInternals {
|
|
|
11
11
|
plugins?: EvaluationPlugin[];
|
|
12
12
|
context?: EvaluationContext;
|
|
13
13
|
consentAware?: boolean;
|
|
14
|
+
evaluationMode?: EvaluationMode;
|
|
15
|
+
onContextChange?: (ctx: EvaluationContext) => void;
|
|
14
16
|
}
|
|
15
17
|
export declare const buildConfigClient: (internals: ConfigClientInternals) => ConfigClient;
|
|
16
18
|
//# sourceMappingURL=ConfigClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConfigClient.d.ts","sourceRoot":"","sources":["../../src/client/ConfigClient.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAG/E,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EAGZ,aAAa,EACb,qBAAqB,EACrB,gBAAgB,EAChB,WAAW,EACZ,MAAM,UAAU,CAAC;AAGlB,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,KAAK,EAAE,YAAY,CAAC;IACpB,OAAO,EAAE,aAAa,CAAC;IACvB,MAAM,EAAE,qBAAqB,CAAC;IAC9B,KAAK,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IAC7B,WAAW,EAAE,gBAAgB,CAAC;IAC9B,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAC7B,OAAO,CAAC,EAAE,iBAAiB,CAAC;IAC5B,YAAY,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"ConfigClient.d.ts","sourceRoot":"","sources":["../../src/client/ConfigClient.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAG/E,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EAGZ,aAAa,EACb,cAAc,EACd,qBAAqB,EACrB,gBAAgB,EAChB,WAAW,EACZ,MAAM,UAAU,CAAC;AAGlB,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,KAAK,EAAE,YAAY,CAAC;IACpB,OAAO,EAAE,aAAa,CAAC;IACvB,MAAM,EAAE,qBAAqB,CAAC;IAC9B,KAAK,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IAC7B,WAAW,EAAE,gBAAgB,CAAC;IAC9B,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAC7B,OAAO,CAAC,EAAE,iBAAiB,CAAC;IAC5B,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC,eAAe,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,KAAK,IAAI,CAAC;CACpD;AAED,eAAO,MAAM,iBAAiB,GAC5B,WAAW,qBAAqB,KAC/B,YAuPF,CAAC"}
|
|
@@ -6,8 +6,16 @@ export const buildConfigClient = (internals) => {
|
|
|
6
6
|
const plugins = internals.plugins ?? [];
|
|
7
7
|
let evalContext = internals.context ?? {};
|
|
8
8
|
const consentAware = internals.consentAware ?? false;
|
|
9
|
+
const evaluationMode = internals.evaluationMode ?? "server";
|
|
10
|
+
const onContextChange = internals.onContextChange;
|
|
9
11
|
let data = { ...internals.data };
|
|
10
12
|
let batchFetchTriggered = false;
|
|
13
|
+
// ── Request deduplication & debouncing ──
|
|
14
|
+
let inflightRefresh = null;
|
|
15
|
+
let lastFetchTime = 0;
|
|
16
|
+
let contextDebounceTimer = null;
|
|
17
|
+
const MIN_FETCH_INTERVAL = 30_000; // Don't re-fetch within 30s
|
|
18
|
+
const CONTEXT_DEBOUNCE_MS = 100; // Batch rapid setContext calls
|
|
11
19
|
const triggerDeferredFetch = () => {
|
|
12
20
|
if (!isDeferred || batchFetchTriggered) {
|
|
13
21
|
return;
|
|
@@ -158,26 +166,35 @@ export const buildConfigClient = (internals) => {
|
|
|
158
166
|
return { ...data };
|
|
159
167
|
},
|
|
160
168
|
async refresh() {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
169
|
+
// Deduplication: if a refresh is already in-flight, reuse it
|
|
170
|
+
if (inflightRefresh)
|
|
171
|
+
return inflightRefresh;
|
|
172
|
+
inflightRefresh = (async () => {
|
|
173
|
+
try {
|
|
174
|
+
const result = await withRetry(() => fetcher.fetchAll(), retry);
|
|
175
|
+
cache.set("__all__", result, DEFAULT_CACHE_TTL);
|
|
176
|
+
for (const [key, value] of Object.entries(result)) {
|
|
177
|
+
cache.set(key, value, DEFAULT_CACHE_TTL);
|
|
178
|
+
}
|
|
179
|
+
data = { ...data, ...result };
|
|
180
|
+
lastFetchTime = Date.now();
|
|
181
|
+
events.emit("updated", {
|
|
182
|
+
keys: Object.keys(result),
|
|
183
|
+
source: "refresh",
|
|
184
|
+
});
|
|
166
185
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
throw error;
|
|
180
|
-
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
events.emit("fetchError", {
|
|
188
|
+
error: error,
|
|
189
|
+
retryCount: retry.maxRetries,
|
|
190
|
+
willRetry: false,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
finally {
|
|
194
|
+
inflightRefresh = null;
|
|
195
|
+
}
|
|
196
|
+
})();
|
|
197
|
+
return inflightRefresh;
|
|
181
198
|
},
|
|
182
199
|
on(event, callback) {
|
|
183
200
|
events.on(event, callback);
|
|
@@ -187,6 +204,23 @@ export const buildConfigClient = (internals) => {
|
|
|
187
204
|
},
|
|
188
205
|
setContext(newContext) {
|
|
189
206
|
evalContext = { ...newContext };
|
|
207
|
+
// Notify the transport layer of the context change
|
|
208
|
+
if (onContextChange) {
|
|
209
|
+
onContextChange(evalContext);
|
|
210
|
+
}
|
|
211
|
+
// In server mode, context change means we need fresh resolved values.
|
|
212
|
+
// Debounce rapid setContext calls (e.g., form typing, navigation)
|
|
213
|
+
if (evaluationMode === "server") {
|
|
214
|
+
if (contextDebounceTimer)
|
|
215
|
+
clearTimeout(contextDebounceTimer);
|
|
216
|
+
contextDebounceTimer = setTimeout(() => {
|
|
217
|
+
contextDebounceTimer = null;
|
|
218
|
+
// Skip if we fetched very recently (stale check)
|
|
219
|
+
if (Date.now() - lastFetchTime < MIN_FETCH_INTERVAL)
|
|
220
|
+
return;
|
|
221
|
+
void client.refresh();
|
|
222
|
+
}, CONTEXT_DEBOUNCE_MS);
|
|
223
|
+
}
|
|
190
224
|
},
|
|
191
225
|
};
|
|
192
226
|
return client;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConfigClient.js","sourceRoot":"","sources":["../../src/client/ConfigClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAGjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"ConfigClient.js","sourceRoot":"","sources":["../../src/client/ConfigClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAGjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAYlE,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAiB7C,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,SAAgC,EAClB,EAAE;IAChB,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,GAAG,SAAS,CAAC;IAC7E,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,IAAI,EAAE,CAAC;IACxC,IAAI,WAAW,GAAsB,SAAS,CAAC,OAAO,IAAI,EAAE,CAAC;IAC7D,MAAM,YAAY,GAAG,SAAS,CAAC,YAAY,IAAI,KAAK,CAAC;IACrD,MAAM,cAAc,GAAG,SAAS,CAAC,cAAc,IAAI,QAAQ,CAAC;IAC5D,MAAM,eAAe,GAAG,SAAS,CAAC,eAAe,CAAC;IAClD,IAAI,IAAI,GAAG,EAAE,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC;IACjC,IAAI,mBAAmB,GA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AI,KAAK;QACd,OAAO,IAAI,KAAK;QAChB,gBAAgB,IAAI,KAAK,CAC1B,CAAC;AACJ,CAAC;AAED,gDAAgD;AAChD,SAAS,cAAc,CACrB,KAAc;IAEd,IAAI,OAAO,KAAK,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IACjD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAC/C,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAC/C,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { EvaluationContext } from "../plugins/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Automatically detect browser/device attributes and merge with user-provided context.
|
|
4
|
+
*
|
|
5
|
+
* Can be called two ways:
|
|
6
|
+
* 1. No args: returns auto-detected context only
|
|
7
|
+
* `autoContext()`
|
|
8
|
+
*
|
|
9
|
+
* 2. With user attributes: auto-detects AND merges your attributes (yours win on conflict)
|
|
10
|
+
* `autoContext({ userId: "user_123", plan: "pro", country: "US" })`
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const flags = initFlags({
|
|
15
|
+
* clientId: "cid_xxx",
|
|
16
|
+
* context: autoContext({ userId: "user_123", plan: "pro" }),
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function autoContext(user?: {
|
|
21
|
+
userId?: string;
|
|
22
|
+
[key: string]: string | number | boolean | string[] | undefined;
|
|
23
|
+
}): EvaluationContext;
|
|
24
|
+
/**
|
|
25
|
+
* Deep-merge two evaluation contexts. User-provided values take precedence.
|
|
26
|
+
*/
|
|
27
|
+
export declare function mergeContext(auto: EvaluationContext, user: EvaluationContext): EvaluationContext;
|
|
28
|
+
//# sourceMappingURL=autoContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"autoContext.d.ts","sourceRoot":"","sources":["../../src/context/autoContext.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAE7D;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,WAAW,CAAC,IAAI,CAAC,EAAE;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC;CACjE,GAAG,iBAAiB,CAmCpB;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,iBAAiB,EACvB,IAAI,EAAE,iBAAiB,GACtB,iBAAiB,CASnB"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════
|
|
2
|
+
// Auto-Context Detection Helpers
|
|
3
|
+
// Tree-shakeable: only included if explicitly imported by consumer
|
|
4
|
+
// ═══════════════════════════════════════════════════════════════
|
|
5
|
+
/**
|
|
6
|
+
* Automatically detect browser/device attributes and merge with user-provided context.
|
|
7
|
+
*
|
|
8
|
+
* Can be called two ways:
|
|
9
|
+
* 1. No args: returns auto-detected context only
|
|
10
|
+
* `autoContext()`
|
|
11
|
+
*
|
|
12
|
+
* 2. With user attributes: auto-detects AND merges your attributes (yours win on conflict)
|
|
13
|
+
* `autoContext({ userId: "user_123", plan: "pro", country: "US" })`
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const flags = initFlags({
|
|
18
|
+
* clientId: "cid_xxx",
|
|
19
|
+
* context: autoContext({ userId: "user_123", plan: "pro" }),
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function autoContext(user) {
|
|
24
|
+
const detected = {
|
|
25
|
+
attributes: {},
|
|
26
|
+
};
|
|
27
|
+
if (typeof window !== "undefined" && typeof navigator !== "undefined") {
|
|
28
|
+
const ua = navigator.userAgent;
|
|
29
|
+
detected.attributes = {
|
|
30
|
+
browser: detectBrowser(ua),
|
|
31
|
+
browserVersion: detectBrowserVersion(ua),
|
|
32
|
+
os: detectOS(ua),
|
|
33
|
+
device: detectDevice(),
|
|
34
|
+
screenWidth: window.screen?.width ?? 0,
|
|
35
|
+
screenHeight: window.screen?.height ?? 0,
|
|
36
|
+
locale: navigator.language ?? "en",
|
|
37
|
+
timezone: getTimezone(),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
if (!user)
|
|
41
|
+
return detected;
|
|
42
|
+
// Extract userId from the user object, rest goes to attributes
|
|
43
|
+
const { userId, ...attrs } = user;
|
|
44
|
+
const userAttrs = {};
|
|
45
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
46
|
+
if (v !== undefined)
|
|
47
|
+
userAttrs[k] = v;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
userId,
|
|
51
|
+
attributes: {
|
|
52
|
+
...(detected.attributes ?? {}),
|
|
53
|
+
...userAttrs,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Deep-merge two evaluation contexts. User-provided values take precedence.
|
|
59
|
+
*/
|
|
60
|
+
export function mergeContext(auto, user) {
|
|
61
|
+
return {
|
|
62
|
+
userId: user.userId ?? auto.userId,
|
|
63
|
+
attributes: {
|
|
64
|
+
...(auto.attributes ?? {}),
|
|
65
|
+
...(user.attributes ?? {}),
|
|
66
|
+
},
|
|
67
|
+
consentGranted: user.consentGranted ?? auto.consentGranted,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// ═══════════════════════════════════════════════════════════════
|
|
71
|
+
// Detection Helpers (lightweight, no external deps)
|
|
72
|
+
// ═══════════════════════════════════════════════════════════════
|
|
73
|
+
function detectBrowser(ua) {
|
|
74
|
+
if (ua.includes("Firefox/"))
|
|
75
|
+
return "Firefox";
|
|
76
|
+
if (ua.includes("Edg/"))
|
|
77
|
+
return "Edge";
|
|
78
|
+
if (ua.includes("OPR/") || ua.includes("Opera"))
|
|
79
|
+
return "Opera";
|
|
80
|
+
if (ua.includes("Chrome/") && !ua.includes("Edg/"))
|
|
81
|
+
return "Chrome";
|
|
82
|
+
if (ua.includes("Safari/") && !ua.includes("Chrome/"))
|
|
83
|
+
return "Safari";
|
|
84
|
+
return "Unknown";
|
|
85
|
+
}
|
|
86
|
+
function detectBrowserVersion(ua) {
|
|
87
|
+
const patterns = [
|
|
88
|
+
["Firefox", /Firefox\/(\d+[\d.]*)/],
|
|
89
|
+
["Edge", /Edg\/(\d+[\d.]*)/],
|
|
90
|
+
["Opera", /OPR\/(\d+[\d.]*)/],
|
|
91
|
+
["Chrome", /Chrome\/(\d+[\d.]*)/],
|
|
92
|
+
["Safari", /Version\/(\d+[\d.]*)/],
|
|
93
|
+
];
|
|
94
|
+
for (const [, regex] of patterns) {
|
|
95
|
+
const match = ua.match(regex);
|
|
96
|
+
if (match?.[1])
|
|
97
|
+
return match[1];
|
|
98
|
+
}
|
|
99
|
+
return "0";
|
|
100
|
+
}
|
|
101
|
+
function detectOS(ua) {
|
|
102
|
+
if (ua.includes("Windows"))
|
|
103
|
+
return "Windows";
|
|
104
|
+
if (ua.includes("Mac OS"))
|
|
105
|
+
return "macOS";
|
|
106
|
+
if (ua.includes("Linux") && !ua.includes("Android"))
|
|
107
|
+
return "Linux";
|
|
108
|
+
if (ua.includes("Android"))
|
|
109
|
+
return "Android";
|
|
110
|
+
if (/iPhone|iPad|iPod/.test(ua))
|
|
111
|
+
return "iOS";
|
|
112
|
+
if (ua.includes("CrOS"))
|
|
113
|
+
return "ChromeOS";
|
|
114
|
+
return "Unknown";
|
|
115
|
+
}
|
|
116
|
+
function detectDevice() {
|
|
117
|
+
if (typeof window === "undefined")
|
|
118
|
+
return "desktop";
|
|
119
|
+
const width = window.screen?.width ?? window.innerWidth;
|
|
120
|
+
const hasTouch = "ontouchstart" in window || navigator.maxTouchPoints > 0;
|
|
121
|
+
if (hasTouch && width < 768)
|
|
122
|
+
return "mobile";
|
|
123
|
+
if (hasTouch && width >= 768 && width < 1024)
|
|
124
|
+
return "tablet";
|
|
125
|
+
return "desktop";
|
|
126
|
+
}
|
|
127
|
+
function getTimezone() {
|
|
128
|
+
try {
|
|
129
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return "UTC";
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=autoContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"autoContext.js","sourceRoot":"","sources":["../../src/context/autoContext.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,iCAAiC;AACjC,mEAAmE;AACnE,kEAAkE;AAIlE;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,WAAW,CAAC,IAG3B;IACC,MAAM,QAAQ,GAAsB;QAClC,UAAU,EAAE,EAAE;KACf,CAAC;IAEF,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,SAAS,KAAK,WAAW,EAAE,CAAC;QACtE,MAAM,EAAE,GAAG,SAAS,CAAC,SAAS,CAAC;QAC/B,QAAQ,CAAC,UAAU,GAAG;YACpB,OAAO,EAAE,aAAa,CAAC,EAAE,CAAC;YAC1B,cAAc,EAAE,oBAAoB,CAAC,EAAE,CAAC;YACxC,EAAE,EAAE,QAAQ,CAAC,EAAE,CAAC;YAChB,MAAM,EAAE,YAAY,EAAE;YACtB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC;YACtC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,IAAI,CAAC;YACxC,MAAM,EAAE,SAAS,CAAC,QAAQ,IAAI,IAAI;YAClC,QAAQ,EAAE,WAAW,EAAE;SACxB,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,IAAI;QAAE,OAAO,QAAQ,CAAC;IAE3B,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OAAO,UAAU,CAAC;IAC3C,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,YAAY;IACnB,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,OAAO,SAAS,CAAC;IAEpD,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,IAAI,MAAM,CAAC,UAAU,CAAC;IACxD,MAAM,QAAQ,GAAG,cAAc,IAAI,MAAM,IAAI,SAAS,CAAC,cAAc,GAAG,CAAC,CAAC;IAE1E,IAAI,QAAQ,IAAI,KAAK,GAAG,GAAG;QAAE,OAAO,QAAQ,CAAC;IAC7C,IAAI,QAAQ,IAAI,KAAK,IAAI,GAAG,IAAI,KAAK,GAAG,IAAI;QAAE,OAAO,QAAQ,CAAC;IAC9D,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,WAAW;IAClB,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,cAAc,EAAE,CAAC,eAAe,EAAE,CAAC,QAAQ,CAAC;IAC1D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createConfig.d.ts","sourceRoot":"","sources":["../src/createConfig.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createConfig.d.ts","sourceRoot":"","sources":["../src/createConfig.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EACV,YAAY,EACZ,mBAAmB,EAEpB,MAAM,SAAS,CAAC;AAKjB,wBAAgB,YAAY,CAC1B,OAAO,EAAE,mBAAmB,GAAG;IAAE,eAAe,EAAE,aAAa,CAAA;CAAE,GAChE,OAAO,CAAC,YAAY,CAAC,CAAC;AACzB,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,CAAC"}
|