@flipt-io/flipt-client-js 0.0.4 → 0.0.6
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/README.md +107 -110
- package/dist/browser/index.cjs +1 -1
- package/dist/browser/index.mjs +1 -1
- package/dist/flipt_engine_wasm_js_bg.wasm +0 -0
- package/dist/node/index.cjs +1 -1
- package/dist/node/index.mjs +1 -1
- package/engine.wasm.js +2 -0
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -135,125 +135,122 @@ To disable ETag support, you can implement a [custom fetcher](#custom-fetcher) t
|
|
|
135
135
|
|
|
136
136
|
## Slim Module
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
The `slim` module is designed for advanced or non-standard environments where you need full control over how the WASM binary is loaded. Unlike the default client, which tries to load the WASM file automatically, the slim client requires you to explicitly provide the WASM file (as a URL, buffer, etc.).
|
|
139
139
|
|
|
140
|
-
|
|
140
|
+
**Why use the slim module?**
|
|
141
141
|
|
|
142
|
-
|
|
142
|
+
- **Edge/Serverless/Custom Environments:** Some platforms (like Cloudflare Workers, Vercel Edge, or certain Node.js setups) restrict how files are loaded or bundled. The slim client lets you load the WASM file in a way that works for your environment.
|
|
143
|
+
- **Full Control:** You decide how and when the WASM file is loaded—fetch from a CDN, load from disk, or use a custom loader.
|
|
144
|
+
- **Compatibility:** The slim client removes all assumptions about WASM loading, making it compatible with any environment where you can provide the WASM binary.
|
|
145
|
+
- **Bundle Size:** The slim build may exclude some code related to WASM loading, making it lighter and more tree-shakable for certain use cases.
|
|
143
146
|
|
|
144
|
-
|
|
147
|
+
If you are using a standard browser app with a bundler, the default client is usually easier. For edge, serverless, or custom setups, the slim client is the right tool.
|
|
145
148
|
|
|
146
|
-
|
|
149
|
+
### Using the `@flipt-io/flipt-client-js/slim` Module
|
|
147
150
|
|
|
148
|
-
|
|
149
|
-
// Works in all environments (Node.js, browsers, edge functions)
|
|
150
|
-
import { FliptClient } from '@flipt-io/flipt-client-js/slim';
|
|
151
|
-
import wasm from '@flipt-io/flipt-client-js/engine.wasm';
|
|
151
|
+
The `slim` build of the FliptClient is designed for environments where you want to provide your own WASM binary, such as modern browsers (with a bundler) or Node.js.
|
|
152
152
|
|
|
153
|
-
|
|
154
|
-
{
|
|
155
|
-
namespace: 'default',
|
|
156
|
-
url: 'https://flipt.example.com'
|
|
157
|
-
},
|
|
158
|
-
{ wasm }
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
// Use the client as normal
|
|
162
|
-
const result = client.evaluateBoolean({
|
|
163
|
-
flagKey: 'my-flag',
|
|
164
|
-
entityId: 'user-123',
|
|
165
|
-
context: { country: 'US' }
|
|
166
|
-
});
|
|
167
|
-
```
|
|
153
|
+
#### Browser Usage (with a Bundler like Vite, Webpack, etc.)
|
|
168
154
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
When using Cloudflare Workers with Webpack or a similar bundler:
|
|
172
|
-
|
|
173
|
-
```js
|
|
174
|
-
import { FliptClient } from '@flipt-io/flipt-client-js/slim';
|
|
175
|
-
import wasm from '@flipt-io/flipt-client-js/engine.wasm';
|
|
176
|
-
|
|
177
|
-
export default {
|
|
178
|
-
async fetch(request, env, ctx) {
|
|
179
|
-
// Initialize the client with the WASM module
|
|
180
|
-
const client = await FliptClient.init(
|
|
181
|
-
{
|
|
182
|
-
namespace: 'default',
|
|
183
|
-
url: 'https://flipt.example.com'
|
|
184
|
-
},
|
|
185
|
-
{ wasm }
|
|
186
|
-
);
|
|
187
|
-
|
|
188
|
-
// Use the client as normal
|
|
189
|
-
const result = client.evaluateBoolean({
|
|
190
|
-
flagKey: 'my-flag',
|
|
191
|
-
entityId: 'user-123',
|
|
192
|
-
context: { country: 'US' }
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
return new Response(result.enabled ? 'Flag enabled' : 'Flag disabled');
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
```
|
|
155
|
+
1. **Import and initialize the client:**
|
|
199
156
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
```js
|
|
205
|
-
// middleware.js or an Edge API Route
|
|
206
|
-
import { FliptClient } from '@flipt-io/flipt-client-js/slim';
|
|
207
|
-
import wasm from '@flipt-io/flipt-client-js/engine.wasm';
|
|
208
|
-
import { NextResponse } from 'next/server';
|
|
209
|
-
|
|
210
|
-
export const config = {
|
|
211
|
-
runtime: 'edge'
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
export default async function middleware(req) {
|
|
215
|
-
// Initialize the client with the WASM module
|
|
216
|
-
const client = await FliptClient.init(
|
|
217
|
-
{
|
|
218
|
-
namespace: 'default',
|
|
219
|
-
url: process.env.FLIPT_URL || 'https://flipt.example.com',
|
|
220
|
-
authentication: {
|
|
221
|
-
clientToken: process.env.FLIPT_TOKEN
|
|
222
|
-
}
|
|
223
|
-
},
|
|
224
|
-
{ wasm }
|
|
225
|
-
);
|
|
226
|
-
|
|
227
|
-
// Use the client to evaluate a flag
|
|
228
|
-
const result = client.evaluateBoolean({
|
|
229
|
-
flagKey: 'my-edge-feature',
|
|
230
|
-
entityId: 'user-123',
|
|
231
|
-
context: {
|
|
232
|
-
path: req.nextUrl.pathname,
|
|
233
|
-
country: req.geo?.country || 'unknown'
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
// Use the flag result to make decisions
|
|
238
|
-
if (result.enabled) {
|
|
239
|
-
if (req.nextUrl.pathname.startsWith('/api/')) {
|
|
240
|
-
// For API routes, modify the response
|
|
241
|
-
return new Response(JSON.stringify({ featureEnabled: true }), {
|
|
242
|
-
status: 200,
|
|
243
|
-
headers: { 'Content-Type': 'application/json' }
|
|
244
|
-
});
|
|
245
|
-
} else {
|
|
246
|
-
// For page routes, redirect or rewrite
|
|
247
|
-
const url = req.nextUrl.clone();
|
|
248
|
-
url.pathname = '/new-experience';
|
|
249
|
-
return NextResponse.rewrite(url);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
157
|
+
```ts
|
|
158
|
+
import { FliptClient } from '@flipt-io/flipt-client-js/slim';
|
|
159
|
+
import wasmUrl from '@flipt-io/flipt-client-js/engine.wasm?url';
|
|
252
160
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
161
|
+
async function main() {
|
|
162
|
+
const client = await FliptClient.init(
|
|
163
|
+
{
|
|
164
|
+
namespace: 'default',
|
|
165
|
+
url: 'http://localhost:8080'
|
|
166
|
+
},
|
|
167
|
+
{ wasm: wasmUrl }
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
// Use the client as normal
|
|
171
|
+
const result = client.evaluateBoolean({
|
|
172
|
+
flagKey: 'my-flag',
|
|
173
|
+
entityId: 'user-123',
|
|
174
|
+
context: { country: 'US' }
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
console.log(result);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
main();
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
- **Note:** If your bundler supports importing JS proxies for assets, you can also use:
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
import wasmUrl from '@flipt-io/flipt-client-js/engine.wasm';
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Otherwise, use the `?url` syntax for maximum compatibility.
|
|
190
|
+
|
|
191
|
+
2. **Ensure your bundler is configured to handle `.wasm` files.**
|
|
192
|
+
- For Vite, this works out of the box.
|
|
193
|
+
- For Webpack, you may need to add a rule for `.wasm` files.
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
#### Node.js Usage
|
|
198
|
+
|
|
199
|
+
1. **Import and initialize the client:**
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
import { FliptClient } from '@flipt-io/flipt-client-js/slim';
|
|
203
|
+
import { readFileSync } from 'fs';
|
|
204
|
+
import path from 'path';
|
|
205
|
+
import { fileURLToPath } from 'url';
|
|
206
|
+
|
|
207
|
+
// Resolve the path to the WASM file
|
|
208
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
209
|
+
const __dirname = path.dirname(__filename);
|
|
210
|
+
const wasmPath = path.join(
|
|
211
|
+
__dirname,
|
|
212
|
+
'../node_modules/@flipt-io/flipt-client-js/engine.wasm'
|
|
213
|
+
);
|
|
214
|
+
const wasmBuffer = readFileSync(wasmPath);
|
|
215
|
+
|
|
216
|
+
async function main() {
|
|
217
|
+
const client = await FliptClient.init(
|
|
218
|
+
{
|
|
219
|
+
namespace: 'default',
|
|
220
|
+
url: 'http://localhost:8080'
|
|
221
|
+
},
|
|
222
|
+
{ wasm: wasmBuffer }
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
// Use the client as normal
|
|
226
|
+
const result = client.evaluateBoolean({
|
|
227
|
+
flagKey: 'my-flag',
|
|
228
|
+
entityId: 'user-123',
|
|
229
|
+
context: { country: 'US' }
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
console.log(result);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
main();
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
- **Note:** In Node.js, you must load the WASM file as a buffer using `fs.readFileSync`.
|
|
239
|
+
|
|
240
|
+
2. **Run your script with a loader that supports ESM and top-level await, such as `ts-node` or `tsx`:**
|
|
241
|
+
|
|
242
|
+
```sh
|
|
243
|
+
npx tsx src/index.ts
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
### Additional Notes
|
|
249
|
+
|
|
250
|
+
- The `slim` build requires you to provide the WASM binary via the `wasm` option.
|
|
251
|
+
- For browser usage, pass the URL to the WASM file (using your bundler's asset handling).
|
|
252
|
+
- For Node.js, pass the WASM file as a buffer.
|
|
253
|
+
- Make sure your environment supports top-level `await` or wrap your code in an async function.
|
|
257
254
|
|
|
258
255
|
## Development
|
|
259
256
|
|