@flipt-io/flipt-client-js 0.4.2 → 0.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.
@@ -44,6 +44,27 @@ class Engine {
44
44
  }
45
45
  return takeFromExternrefTable0(ret[0]);
46
46
  }
47
+ /**
48
+ * @returns {string}
49
+ */
50
+ get_snapshot() {
51
+ let deferred2_0;
52
+ let deferred2_1;
53
+ try {
54
+ const ret = wasm.engine_get_snapshot(this.__wbg_ptr);
55
+ var ptr1 = ret[0];
56
+ var len1 = ret[1];
57
+ if (ret[3]) {
58
+ ptr1 = 0; len1 = 0;
59
+ throw takeFromExternrefTable0(ret[2]);
60
+ }
61
+ deferred2_0 = ptr1;
62
+ deferred2_1 = len1;
63
+ return getStringFromWasm0(ptr1, len1);
64
+ } finally {
65
+ wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
66
+ }
67
+ }
47
68
  /**
48
69
  * @returns {any}
49
70
  */
@@ -61,10 +82,21 @@ class Engine {
61
82
  const ptr0 = passStringToWasm0(namespace, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
62
83
  const len0 = WASM_VECTOR_LEN;
63
84
  const ret = wasm.engine_new(ptr0, len0);
64
- this.__wbg_ptr = ret >>> 0;
85
+ this.__wbg_ptr = ret;
65
86
  EngineFinalization.register(this, this.__wbg_ptr, this);
66
87
  return this;
67
88
  }
89
+ /**
90
+ * @param {string} snapshot_b64
91
+ */
92
+ seed_snapshot(snapshot_b64) {
93
+ const ptr0 = passStringToWasm0(snapshot_b64, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
94
+ const len0 = WASM_VECTOR_LEN;
95
+ const ret = wasm.engine_seed_snapshot(this.__wbg_ptr, ptr0, len0);
96
+ if (ret[1]) {
97
+ throw takeFromExternrefTable0(ret[0]);
98
+ }
99
+ }
68
100
  /**
69
101
  * @param {any} data
70
102
  */
@@ -79,7 +111,7 @@ if (Symbol.dispose) Engine.prototype[Symbol.dispose] = Engine.prototype.free;
79
111
  function __wbg_get_imports() {
80
112
  const import0 = {
81
113
  __proto__: null,
82
- __wbg_Error_960c155d3d49e4c2: function(arg0, arg1) {
114
+ __wbg_Error_408e67f47ca7b58b: function(arg0, arg1) {
83
115
  const ret = Error(getStringFromWasm0(arg0, arg1));
84
116
  return ret;
85
117
  },
@@ -90,50 +122,50 @@ function __wbg_get_imports() {
90
122
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
91
123
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
92
124
  },
93
- __wbg___wbindgen_boolean_get_6ea149f0a8dcc5ff: function(arg0) {
125
+ __wbg___wbindgen_boolean_get_c9c83ebd41b34df3: function(arg0) {
94
126
  const v = arg0;
95
127
  const ret = typeof(v) === 'boolean' ? v : undefined;
96
128
  return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
97
129
  },
98
- __wbg___wbindgen_debug_string_ab4b34d23d6778bd: function(arg0, arg1) {
130
+ __wbg___wbindgen_debug_string_a57024b9c6e4a48b: function(arg0, arg1) {
99
131
  const ret = debugString(arg1);
100
132
  const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
101
133
  const len1 = WASM_VECTOR_LEN;
102
134
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
103
135
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
104
136
  },
105
- __wbg___wbindgen_in_a5d8b22e52b24dd1: function(arg0, arg1) {
137
+ __wbg___wbindgen_in_ac983077f137f2e6: function(arg0, arg1) {
106
138
  const ret = arg0 in arg1;
107
139
  return ret;
108
140
  },
109
- __wbg___wbindgen_is_function_3baa9db1a987f47d: function(arg0) {
141
+ __wbg___wbindgen_is_function_5e4570eb24ffa122: function(arg0) {
110
142
  const ret = typeof(arg0) === 'function';
111
143
  return ret;
112
144
  },
113
- __wbg___wbindgen_is_object_63322ec0cd6ea4ef: function(arg0) {
145
+ __wbg___wbindgen_is_object_a2790eb24c211ea0: function(arg0) {
114
146
  const val = arg0;
115
147
  const ret = typeof(val) === 'object' && val !== null;
116
148
  return ret;
117
149
  },
118
- __wbg___wbindgen_is_string_6df3bf7ef1164ed3: function(arg0) {
150
+ __wbg___wbindgen_is_string_e6f02f0ea5f20a32: function(arg0) {
119
151
  const ret = typeof(arg0) === 'string';
120
152
  return ret;
121
153
  },
122
- __wbg___wbindgen_is_undefined_29a43b4d42920abd: function(arg0) {
154
+ __wbg___wbindgen_is_undefined_6cff064c44e0d823: function(arg0) {
123
155
  const ret = arg0 === undefined;
124
156
  return ret;
125
157
  },
126
- __wbg___wbindgen_jsval_loose_eq_cac3565e89b4134c: function(arg0, arg1) {
158
+ __wbg___wbindgen_jsval_loose_eq_acf2776254a8d832: function(arg0, arg1) {
127
159
  const ret = arg0 == arg1;
128
160
  return ret;
129
161
  },
130
- __wbg___wbindgen_number_get_c7f42aed0525c451: function(arg0, arg1) {
162
+ __wbg___wbindgen_number_get_136b9679cab35cfb: function(arg0, arg1) {
131
163
  const obj = arg1;
132
164
  const ret = typeof(obj) === 'number' ? obj : undefined;
133
165
  getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
134
166
  getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
135
167
  },
136
- __wbg___wbindgen_string_get_7ed5322991caaec5: function(arg0, arg1) {
168
+ __wbg___wbindgen_string_get_d154f1e671052120: function(arg0, arg1) {
137
169
  const obj = arg1;
138
170
  const ret = typeof(obj) === 'string' ? obj : undefined;
139
171
  var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -141,22 +173,22 @@ function __wbg_get_imports() {
141
173
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
142
174
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
143
175
  },
144
- __wbg___wbindgen_throw_6b64449b9b9ed33c: function(arg0, arg1) {
176
+ __wbg___wbindgen_throw_bb96b2010945f0bc: function(arg0, arg1) {
145
177
  throw new Error(getStringFromWasm0(arg0, arg1));
146
178
  },
147
- __wbg_call_14b169f759b26747: function() { return handleError(function (arg0, arg1) {
179
+ __wbg_call_1c5886ab9c57d1c7: function() { return handleError(function (arg0, arg1) {
148
180
  const ret = arg0.call(arg1);
149
181
  return ret;
150
182
  }, arguments); },
151
- __wbg_done_9158f7cc8751ba32: function(arg0) {
183
+ __wbg_done_669171204c3dcae2: function(arg0) {
152
184
  const ret = arg0.done;
153
185
  return ret;
154
186
  },
155
- __wbg_entries_e0b73aa8571ddb56: function(arg0) {
187
+ __wbg_entries_7774d489e1da5f4f: function(arg0) {
156
188
  const ret = Object.entries(arg0);
157
189
  return ret;
158
190
  },
159
- __wbg_error_a6fa202b58aa1cd3: function(arg0, arg1) {
191
+ __wbg_error_757e9472f8410341: function(arg0, arg1) {
160
192
  let deferred0_0;
161
193
  let deferred0_1;
162
194
  try {
@@ -167,19 +199,19 @@ function __wbg_get_imports() {
167
199
  wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
168
200
  }
169
201
  },
170
- __wbg_getTime_da7c55f52b71e8c6: function(arg0) {
202
+ __wbg_getTime_63fb0332e6c4ec17: function(arg0) {
171
203
  const ret = arg0.getTime();
172
204
  return ret;
173
205
  },
174
- __wbg_get_1affdbdd5573b16a: function() { return handleError(function (arg0, arg1) {
175
- const ret = Reflect.get(arg0, arg1);
176
- return ret;
177
- }, arguments); },
178
- __wbg_get_8360291721e2339f: function(arg0, arg1) {
206
+ __wbg_get_c0c8f8d7da0c03dd: function(arg0, arg1) {
179
207
  const ret = arg0[arg1 >>> 0];
180
208
  return ret;
181
209
  },
182
- __wbg_get_unchecked_17f53dad852b9588: function(arg0, arg1) {
210
+ __wbg_get_d173c0308df22d37: function() { return handleError(function (arg0, arg1) {
211
+ const ret = Reflect.get(arg0, arg1);
212
+ return ret;
213
+ }, arguments); },
214
+ __wbg_get_unchecked_e20b893aeafc3fca: function(arg0, arg1) {
183
215
  const ret = arg0[arg1 >>> 0];
184
216
  return ret;
185
217
  },
@@ -187,7 +219,7 @@ function __wbg_get_imports() {
187
219
  const ret = arg0[arg1];
188
220
  return ret;
189
221
  },
190
- __wbg_instanceof_ArrayBuffer_7c8433c6ed14ffe3: function(arg0) {
222
+ __wbg_instanceof_ArrayBuffer_993d02d2d254cad1: function(arg0) {
191
223
  let result;
192
224
  try {
193
225
  result = arg0 instanceof ArrayBuffer;
@@ -197,7 +229,7 @@ function __wbg_get_imports() {
197
229
  const ret = result;
198
230
  return ret;
199
231
  },
200
- __wbg_instanceof_Uint8Array_152ba1f289edcf3f: function(arg0) {
232
+ __wbg_instanceof_Uint8Array_f935dbb0aa7cdeed: function(arg0) {
201
233
  let result;
202
234
  try {
203
235
  result = arg0 instanceof Uint8Array;
@@ -207,47 +239,47 @@ function __wbg_get_imports() {
207
239
  const ret = result;
208
240
  return ret;
209
241
  },
210
- __wbg_isArray_c3109d14ffc06469: function(arg0) {
242
+ __wbg_isArray_6339f732981044bf: function(arg0) {
211
243
  const ret = Array.isArray(arg0);
212
244
  return ret;
213
245
  },
214
- __wbg_iterator_013bc09ec998c2a7: function() {
246
+ __wbg_iterator_5cebbb86e33c6dd6: function() {
215
247
  const ret = Symbol.iterator;
216
248
  return ret;
217
249
  },
218
- __wbg_length_3d4ecd04bd8d22f1: function(arg0) {
250
+ __wbg_length_36bd29c6848c2144: function(arg0) {
219
251
  const ret = arg0.length;
220
252
  return ret;
221
253
  },
222
- __wbg_length_9f1775224cf1d815: function(arg0) {
254
+ __wbg_length_ecfa2c63d3d0d82c: function(arg0) {
223
255
  const ret = arg0.length;
224
256
  return ret;
225
257
  },
226
- __wbg_new_0_4d657201ced14de3: function() {
258
+ __wbg_new_0_f117d868b403dc07: function() {
227
259
  const ret = new Date();
228
260
  return ret;
229
261
  },
230
- __wbg_new_0c7403db6e782f19: function(arg0) {
231
- const ret = new Uint8Array(arg0);
262
+ __wbg_new_116be93542d39019: function() {
263
+ const ret = new Array();
232
264
  return ret;
233
265
  },
234
266
  __wbg_new_227d7c05414eb861: function() {
235
267
  const ret = new Error();
236
268
  return ret;
237
269
  },
238
- __wbg_new_682678e2f47e32bc: function() {
239
- const ret = new Array();
270
+ __wbg_new_77cc4f4f472aeb81: function(arg0) {
271
+ const ret = new Uint8Array(arg0);
240
272
  return ret;
241
273
  },
242
- __wbg_new_aa8d0fa9762c29bd: function() {
274
+ __wbg_new_ebe3e0f6837f0879: function() {
243
275
  const ret = new Object();
244
276
  return ret;
245
277
  },
246
- __wbg_next_0340c4ae324393c3: function() { return handleError(function (arg0) {
278
+ __wbg_next_42cf16ee0dafc9e2: function() { return handleError(function (arg0) {
247
279
  const ret = arg0.next();
248
280
  return ret;
249
281
  }, arguments); },
250
- __wbg_next_7646edaa39458ef7: function(arg0) {
282
+ __wbg_next_8f26b64fa5e9f64b: function(arg0) {
251
283
  const ret = arg0.next;
252
284
  return ret;
253
285
  },
@@ -259,15 +291,15 @@ function __wbg_get_imports() {
259
291
  const ret = arg0.performance;
260
292
  return ret;
261
293
  },
262
- __wbg_prototypesetcall_a6b02eb00b0f4ce2: function(arg0, arg1, arg2) {
294
+ __wbg_prototypesetcall_de8e0d9553586985: function(arg0, arg1, arg2) {
263
295
  Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
264
296
  },
265
- __wbg_set_3bf1de9fab0cd644: function(arg0, arg1, arg2) {
266
- arg0[arg1 >>> 0] = arg2;
267
- },
268
297
  __wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
269
298
  arg0[arg1] = arg2;
270
299
  },
300
+ __wbg_set_a80955eb93b145c6: function(arg0, arg1, arg2) {
301
+ arg0[arg1 >>> 0] = arg2;
302
+ },
271
303
  __wbg_stack_3b0d974bbf31e44f: function(arg0, arg1) {
272
304
  const ret = arg1.stack;
273
305
  const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -275,23 +307,23 @@ function __wbg_get_imports() {
275
307
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
276
308
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
277
309
  },
278
- __wbg_static_accessor_GLOBAL_8cfadc87a297ca02: function() {
279
- const ret = typeof global === 'undefined' ? null : global;
310
+ __wbg_static_accessor_GLOBAL_THIS_466428f93b4eaa76: function() {
311
+ const ret = typeof globalThis === 'undefined' ? null : globalThis;
280
312
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
281
313
  },
282
- __wbg_static_accessor_GLOBAL_THIS_602256ae5c8f42cf: function() {
283
- const ret = typeof globalThis === 'undefined' ? null : globalThis;
314
+ __wbg_static_accessor_GLOBAL_c7aea38d4de089bc: function() {
315
+ const ret = typeof global === 'undefined' ? null : global;
284
316
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
285
317
  },
286
- __wbg_static_accessor_SELF_e445c1c7484aecc3: function() {
318
+ __wbg_static_accessor_SELF_42d4fae05e59267a: function() {
287
319
  const ret = typeof self === 'undefined' ? null : self;
288
320
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
289
321
  },
290
- __wbg_static_accessor_WINDOW_f20e8576ef1e0f17: function() {
322
+ __wbg_static_accessor_WINDOW_e0db14a0eba6a812: function() {
291
323
  const ret = typeof window === 'undefined' ? null : window;
292
324
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
293
325
  },
294
- __wbg_value_ee3a06f4579184fa: function(arg0) {
326
+ __wbg_value_1e2369fab29b420e: function(arg0) {
295
327
  const ret = arg0.value;
296
328
  return ret;
297
329
  },
@@ -323,7 +355,7 @@ function __wbg_get_imports() {
323
355
 
324
356
  const EngineFinalization = (typeof FinalizationRegistry === 'undefined')
325
357
  ? { register: () => {}, unregister: () => {} }
326
- : new FinalizationRegistry(ptr => wasm.__wbg_engine_free(ptr >>> 0, 1));
358
+ : new FinalizationRegistry(ptr => wasm.__wbg_engine_free(ptr, 1));
327
359
 
328
360
  function addToExternrefTable0(obj) {
329
361
  const idx = wasm.__externref_table_alloc();
@@ -410,8 +442,7 @@ function getDataViewMemory0() {
410
442
  }
411
443
 
412
444
  function getStringFromWasm0(ptr, len) {
413
- ptr = ptr >>> 0;
414
- return decodeText(ptr, len);
445
+ return decodeText(ptr >>> 0, len);
415
446
  }
416
447
 
417
448
  let cachedUint8ArrayMemory0 = null;
@@ -518,11 +549,15 @@ function __wbg_finalize_init(instance, module) {
518
549
 
519
550
  async function __wbg_load(module, imports) {
520
551
  if (typeof Response === 'function' && module instanceof Response) {
552
+ if (!module.ok) {
553
+ throw new Error(`failed to fetch Wasm: ${module.status} ${module.statusText} fetching '${module.url}'`);
554
+ }
555
+
521
556
  if (typeof WebAssembly.instantiateStreaming === 'function') {
522
557
  try {
523
558
  return await WebAssembly.instantiateStreaming(module, imports);
524
559
  } catch (e) {
525
- const validResponse = module.ok && expectedResponseType(module.type);
560
+ const validResponse = expectedResponseType(module.type);
526
561
 
527
562
  if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
528
563
  console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
@@ -648,6 +683,12 @@ class BaseFliptClient {
648
683
  this.etag = etag;
649
684
  }
650
685
  }
686
+ /**
687
+ * Returns a base64-encoded snapshot of the current flag state.
688
+ */
689
+ getSnapshot() {
690
+ return this.engine.get_snapshot();
691
+ }
651
692
  /**
652
693
  * Refresh the flags snapshot
653
694
  * @returns true if snapshot changed
@@ -828,6 +869,26 @@ class BaseFliptClient {
828
869
  }
829
870
  }
830
871
 
872
+ async function initializeSnapshot({ engine, snapshot, errorStrategy, fetcher, storeEtag }) {
873
+ if (snapshot) {
874
+ engine.seed_snapshot(snapshot);
875
+ }
876
+ try {
877
+ const resp = await fetcher();
878
+ if (!resp.ok) {
879
+ throw new Error(`Failed to fetch data: ${resp.statusText}`);
880
+ }
881
+ const data = await resp.json();
882
+ engine.snapshot(data);
883
+ storeEtag(resp);
884
+ }
885
+ catch (error) {
886
+ if (!snapshot || errorStrategy !== ErrorStrategy.Fallback) {
887
+ throw error;
888
+ }
889
+ }
890
+ }
891
+
831
892
  class FliptClient extends BaseFliptClient {
832
893
  /**
833
894
  * Initialize the client
@@ -882,17 +943,16 @@ class FliptClient extends BaseFliptClient {
882
943
  if (!fetcher) {
883
944
  throw new Error('Failed to initialize fetcher');
884
945
  }
885
- // handle case if they pass in a custom fetcher that doesn't throw on non-2xx status codes
886
- const resp = await fetcher();
887
- if (!resp.ok) {
888
- throw new Error(`Failed to fetch data: ${resp.statusText}`);
889
- }
890
- const data = await resp.json();
891
946
  const engine = new Engine(namespace);
892
- engine.snapshot(data);
893
947
  // Create client instance
894
948
  const client = new FliptClient(engine, fetcher);
895
- client.storeEtag(resp);
949
+ await initializeSnapshot({
950
+ engine,
951
+ snapshot: options.snapshot,
952
+ errorStrategy: options.errorStrategy,
953
+ fetcher,
954
+ storeEtag: (resp) => client.storeEtag(resp)
955
+ });
896
956
  client.errorStrategy = options.errorStrategy;
897
957
  client.hook = options.hook;
898
958
  return client;
@@ -11,6 +11,10 @@ export declare abstract class BaseFliptClient {
11
11
  * Store etag from response for next requests
12
12
  */
13
13
  protected storeEtag(resp: Response): void;
14
+ /**
15
+ * Returns a base64-encoded snapshot of the current flag state.
16
+ */
17
+ getSnapshot(): string;
14
18
  /**
15
19
  * Refresh the flags snapshot
16
20
  * @returns true if snapshot changed
@@ -0,0 +1,13 @@
1
+ import { ErrorStrategy, Response } from './types';
2
+ type SnapshotEngine = {
3
+ seed_snapshot(snapshot: string): void;
4
+ snapshot(data: unknown): void;
5
+ };
6
+ export declare function initializeSnapshot({ engine, snapshot, errorStrategy, fetcher, storeEtag }: {
7
+ engine: SnapshotEngine;
8
+ snapshot?: string;
9
+ errorStrategy?: ErrorStrategy;
10
+ fetcher: () => Promise<Response>;
11
+ storeEtag: (resp: Response) => void;
12
+ }): Promise<void>;
13
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -118,6 +118,12 @@ export interface ClientOptions {
118
118
  * @see {@link https://docs.flipt.io/guides/user/using-references}
119
119
  */
120
120
  reference?: string;
121
+ /**
122
+ * Base64-encoded snapshot returned by getSnapshot(). When provided with
123
+ * ErrorStrategy.Fallback, the client can initialize offline and refresh when
124
+ * Flipt becomes reachable again.
125
+ */
126
+ snapshot?: string;
121
127
  /**
122
128
  * The fetcher to use when fetching flag state. If not provided, the client will default to a fetch function.
123
129
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flipt-io/flipt-client-js",
3
- "version": "0.4.2",
3
+ "version": "0.5.1",
4
4
  "description": "Flipt Client Evaluation SDK for Web and Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/node/index.cjs",
@@ -91,7 +91,7 @@
91
91
  "@rollup/plugin-typescript": "^12.1.0",
92
92
  "@rollup/plugin-wasm": "^6.2.2",
93
93
  "@types/jest": "^30.0.0",
94
- "@types/node": "^25.0.2",
94
+ "@types/node": "^26.0.0",
95
95
  "@types/node-fetch": "^2.6.11",
96
96
  "@typescript-eslint/eslint-plugin": "^8.33.1",
97
97
  "@typescript-eslint/parser": "^8.33.1",