@flipt-io/flipt-client-js 0.4.1 → 0.5.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.
@@ -47,6 +47,27 @@ class Engine {
47
47
  }
48
48
  return takeFromExternrefTable0(ret[0]);
49
49
  }
50
+ /**
51
+ * @returns {string}
52
+ */
53
+ get_snapshot() {
54
+ let deferred2_0;
55
+ let deferred2_1;
56
+ try {
57
+ const ret = wasm.engine_get_snapshot(this.__wbg_ptr);
58
+ var ptr1 = ret[0];
59
+ var len1 = ret[1];
60
+ if (ret[3]) {
61
+ ptr1 = 0; len1 = 0;
62
+ throw takeFromExternrefTable0(ret[2]);
63
+ }
64
+ deferred2_0 = ptr1;
65
+ deferred2_1 = len1;
66
+ return getStringFromWasm0(ptr1, len1);
67
+ } finally {
68
+ wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
69
+ }
70
+ }
50
71
  /**
51
72
  * @returns {any}
52
73
  */
@@ -64,10 +85,21 @@ class Engine {
64
85
  const ptr0 = passStringToWasm0(namespace, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
65
86
  const len0 = WASM_VECTOR_LEN;
66
87
  const ret = wasm.engine_new(ptr0, len0);
67
- this.__wbg_ptr = ret >>> 0;
88
+ this.__wbg_ptr = ret;
68
89
  EngineFinalization.register(this, this.__wbg_ptr, this);
69
90
  return this;
70
91
  }
92
+ /**
93
+ * @param {string} snapshot_b64
94
+ */
95
+ seed_snapshot(snapshot_b64) {
96
+ const ptr0 = passStringToWasm0(snapshot_b64, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
97
+ const len0 = WASM_VECTOR_LEN;
98
+ const ret = wasm.engine_seed_snapshot(this.__wbg_ptr, ptr0, len0);
99
+ if (ret[1]) {
100
+ throw takeFromExternrefTable0(ret[0]);
101
+ }
102
+ }
71
103
  /**
72
104
  * @param {any} data
73
105
  */
@@ -79,11 +111,10 @@ class Engine {
79
111
  }
80
112
  }
81
113
  if (Symbol.dispose) Engine.prototype[Symbol.dispose] = Engine.prototype.free;
82
-
83
114
  function __wbg_get_imports() {
84
115
  const import0 = {
85
116
  __proto__: null,
86
- __wbg_Error_55538483de6e3abe: function(arg0, arg1) {
117
+ __wbg_Error_fdd633d4bb5dd76a: function(arg0, arg1) {
87
118
  const ret = Error(getStringFromWasm0(arg0, arg1));
88
119
  return ret;
89
120
  },
@@ -94,50 +125,50 @@ function __wbg_get_imports() {
94
125
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
95
126
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
96
127
  },
97
- __wbg___wbindgen_boolean_get_fe2a24fdfdb4064f: function(arg0) {
128
+ __wbg___wbindgen_boolean_get_edaed31a367ce1bd: function(arg0) {
98
129
  const v = arg0;
99
130
  const ret = typeof(v) === 'boolean' ? v : undefined;
100
131
  return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
101
132
  },
102
- __wbg___wbindgen_debug_string_d89627202d0155b7: function(arg0, arg1) {
133
+ __wbg___wbindgen_debug_string_8a447059637473e2: function(arg0, arg1) {
103
134
  const ret = debugString(arg1);
104
135
  const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
105
136
  const len1 = WASM_VECTOR_LEN;
106
137
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
107
138
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
108
139
  },
109
- __wbg___wbindgen_in_fe3eb6a509f75744: function(arg0, arg1) {
140
+ __wbg___wbindgen_in_4990f46af709e33c: function(arg0, arg1) {
110
141
  const ret = arg0 in arg1;
111
142
  return ret;
112
143
  },
113
- __wbg___wbindgen_is_function_2a95406423ea8626: function(arg0) {
144
+ __wbg___wbindgen_is_function_acc5528be2b923f2: function(arg0) {
114
145
  const ret = typeof(arg0) === 'function';
115
146
  return ret;
116
147
  },
117
- __wbg___wbindgen_is_object_59a002e76b059312: function(arg0) {
148
+ __wbg___wbindgen_is_object_0beba4a1980d3eea: function(arg0) {
118
149
  const val = arg0;
119
150
  const ret = typeof(val) === 'object' && val !== null;
120
151
  return ret;
121
152
  },
122
- __wbg___wbindgen_is_string_624d5244bb2bc87c: function(arg0) {
153
+ __wbg___wbindgen_is_string_1fca8072260dd261: function(arg0) {
123
154
  const ret = typeof(arg0) === 'string';
124
155
  return ret;
125
156
  },
126
- __wbg___wbindgen_is_undefined_87a3a837f331fef5: function(arg0) {
157
+ __wbg___wbindgen_is_undefined_721f8decd50c87a3: function(arg0) {
127
158
  const ret = arg0 === undefined;
128
159
  return ret;
129
160
  },
130
- __wbg___wbindgen_jsval_loose_eq_cf851f110c48f9ba: function(arg0, arg1) {
161
+ __wbg___wbindgen_jsval_loose_eq_4b9aba9e5b3c4582: function(arg0, arg1) {
131
162
  const ret = arg0 == arg1;
132
163
  return ret;
133
164
  },
134
- __wbg___wbindgen_number_get_769f3676dc20c1d7: function(arg0, arg1) {
165
+ __wbg___wbindgen_number_get_1cc01dd708740256: function(arg0, arg1) {
135
166
  const obj = arg1;
136
167
  const ret = typeof(obj) === 'number' ? obj : undefined;
137
168
  getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
138
169
  getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
139
170
  },
140
- __wbg___wbindgen_string_get_f1161390414f9b59: function(arg0, arg1) {
171
+ __wbg___wbindgen_string_get_71bb4348194e31f0: function(arg0, arg1) {
141
172
  const obj = arg1;
142
173
  const ret = typeof(obj) === 'string' ? obj : undefined;
143
174
  var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -145,18 +176,18 @@ function __wbg_get_imports() {
145
176
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
146
177
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
147
178
  },
148
- __wbg___wbindgen_throw_5549492daedad139: function(arg0, arg1) {
179
+ __wbg___wbindgen_throw_ea4887a5f8f9a9db: function(arg0, arg1) {
149
180
  throw new Error(getStringFromWasm0(arg0, arg1));
150
181
  },
151
- __wbg_call_6ae20895a60069a2: function() { return handleError(function (arg0, arg1) {
182
+ __wbg_call_8e98ed2f3c86c4b5: function() { return handleError(function (arg0, arg1) {
152
183
  const ret = arg0.call(arg1);
153
184
  return ret;
154
185
  }, arguments); },
155
- __wbg_done_19f92cb1f8738aba: function(arg0) {
186
+ __wbg_done_b62d4a7d2286852a: function(arg0) {
156
187
  const ret = arg0.done;
157
188
  return ret;
158
189
  },
159
- __wbg_entries_28ed7cb892e12eff: function(arg0) {
190
+ __wbg_entries_c261c3fa1f281256: function(arg0) {
160
191
  const ret = Object.entries(arg0);
161
192
  return ret;
162
193
  },
@@ -171,19 +202,19 @@ function __wbg_get_imports() {
171
202
  wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
172
203
  }
173
204
  },
174
- __wbg_getTime_c3af35594e283356: function(arg0) {
205
+ __wbg_getTime_7a770f8a2ec8d634: function(arg0) {
175
206
  const ret = arg0.getTime();
176
207
  return ret;
177
208
  },
178
- __wbg_get_94f5fc088edd3138: function(arg0, arg1) {
209
+ __wbg_get_197a3fe98f169e38: function(arg0, arg1) {
179
210
  const ret = arg0[arg1 >>> 0];
180
211
  return ret;
181
212
  },
182
- __wbg_get_a50328e7325d7f9b: function() { return handleError(function (arg0, arg1) {
213
+ __wbg_get_9a29be2cb383ed9a: function() { return handleError(function (arg0, arg1) {
183
214
  const ret = Reflect.get(arg0, arg1);
184
215
  return ret;
185
216
  }, arguments); },
186
- __wbg_get_unchecked_7c6bbabf5b0b1fbf: function(arg0, arg1) {
217
+ __wbg_get_unchecked_54a4374c38e08460: function(arg0, arg1) {
187
218
  const ret = arg0[arg1 >>> 0];
188
219
  return ret;
189
220
  },
@@ -191,7 +222,7 @@ function __wbg_get_imports() {
191
222
  const ret = arg0[arg1];
192
223
  return ret;
193
224
  },
194
- __wbg_instanceof_ArrayBuffer_8d855993947fc3a2: function(arg0) {
225
+ __wbg_instanceof_ArrayBuffer_2a7bb09fee70c2da: function(arg0) {
195
226
  let result;
196
227
  try {
197
228
  result = arg0 instanceof ArrayBuffer;
@@ -201,7 +232,7 @@ function __wbg_get_imports() {
201
232
  const ret = result;
202
233
  return ret;
203
234
  },
204
- __wbg_instanceof_Uint8Array_ce24d58a5f4bdcc3: function(arg0) {
235
+ __wbg_instanceof_Uint8Array_f080092dc70f5d58: function(arg0) {
205
236
  let result;
206
237
  try {
207
238
  result = arg0 instanceof Uint8Array;
@@ -211,50 +242,50 @@ function __wbg_get_imports() {
211
242
  const ret = result;
212
243
  return ret;
213
244
  },
214
- __wbg_isArray_867202cf8f195ed8: function(arg0) {
245
+ __wbg_isArray_145a34fd0a38d37b: function(arg0) {
215
246
  const ret = Array.isArray(arg0);
216
247
  return ret;
217
248
  },
218
- __wbg_iterator_54661826e186eb6a: function() {
249
+ __wbg_iterator_cc47ba25a2be735a: function() {
219
250
  const ret = Symbol.iterator;
220
251
  return ret;
221
252
  },
222
- __wbg_length_e6e1633fbea6cfa9: function(arg0) {
253
+ __wbg_length_589238bdcf171f0e: function(arg0) {
223
254
  const ret = arg0.length;
224
255
  return ret;
225
256
  },
226
- __wbg_length_fae3e439140f48a4: function(arg0) {
257
+ __wbg_length_c6054974c0a6cdb9: function(arg0) {
227
258
  const ret = arg0.length;
228
259
  return ret;
229
260
  },
230
- __wbg_new_0_e649c99e7382313f: function() {
261
+ __wbg_new_0_1b32bedde98fef4b: function() {
231
262
  const ret = new Date();
232
263
  return ret;
233
264
  },
234
- __wbg_new_1d96678aaacca32e: function(arg0) {
235
- const ret = new Uint8Array(arg0);
236
- return ret;
237
- },
238
265
  __wbg_new_227d7c05414eb861: function() {
239
266
  const ret = new Error();
240
267
  return ret;
241
268
  },
242
- __wbg_new_4370be21fa2b2f80: function() {
243
- const ret = new Array();
269
+ __wbg_new_2e117a478906f062: function() {
270
+ const ret = new Object();
244
271
  return ret;
245
272
  },
246
- __wbg_new_48e1d86cfd30c8e7: function() {
247
- const ret = new Object();
273
+ __wbg_new_36e147a8ced3c6e0: function() {
274
+ const ret = new Array();
248
275
  return ret;
249
276
  },
250
- __wbg_next_55d835fe0ab5b3e7: function(arg0) {
251
- const ret = arg0.next;
277
+ __wbg_new_81880fb5002cb255: function(arg0) {
278
+ const ret = new Uint8Array(arg0);
252
279
  return ret;
253
280
  },
254
- __wbg_next_e34cfb9df1518d7c: function() { return handleError(function (arg0) {
281
+ __wbg_next_0c4066e251d2eff9: function() { return handleError(function (arg0) {
255
282
  const ret = arg0.next();
256
283
  return ret;
257
284
  }, arguments); },
285
+ __wbg_next_402fa10b59ab20c3: function(arg0) {
286
+ const ret = arg0.next;
287
+ return ret;
288
+ },
258
289
  __wbg_now_e7c6795a7f81e10f: function(arg0) {
259
290
  const ret = arg0.now();
260
291
  return ret;
@@ -263,15 +294,15 @@ function __wbg_get_imports() {
263
294
  const ret = arg0.performance;
264
295
  return ret;
265
296
  },
266
- __wbg_prototypesetcall_3875d54d12ef2eec: function(arg0, arg1, arg2) {
297
+ __wbg_prototypesetcall_d721637c7ca66eb8: function(arg0, arg1, arg2) {
267
298
  Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
268
299
  },
269
- __wbg_set_4702dfa37c77f492: function(arg0, arg1, arg2) {
270
- arg0[arg1 >>> 0] = arg2;
271
- },
272
300
  __wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
273
301
  arg0[arg1] = arg2;
274
302
  },
303
+ __wbg_set_dc601f4a69da0bc2: function(arg0, arg1, arg2) {
304
+ arg0[arg1 >>> 0] = arg2;
305
+ },
275
306
  __wbg_stack_3b0d974bbf31e44f: function(arg0, arg1) {
276
307
  const ret = arg1.stack;
277
308
  const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -279,23 +310,23 @@ function __wbg_get_imports() {
279
310
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
280
311
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
281
312
  },
282
- __wbg_static_accessor_GLOBAL_8dfb7f5e26ebe523: function() {
283
- const ret = typeof global === 'undefined' ? null : global;
313
+ __wbg_static_accessor_GLOBAL_THIS_2fee5048bcca5938: function() {
314
+ const ret = typeof globalThis === 'undefined' ? null : globalThis;
284
315
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
285
316
  },
286
- __wbg_static_accessor_GLOBAL_THIS_941154efc8395cdd: function() {
287
- const ret = typeof globalThis === 'undefined' ? null : globalThis;
317
+ __wbg_static_accessor_GLOBAL_ce44e66a4935da8c: function() {
318
+ const ret = typeof global === 'undefined' ? null : global;
288
319
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
289
320
  },
290
- __wbg_static_accessor_SELF_58dac9af822f561f: function() {
321
+ __wbg_static_accessor_SELF_44f6e0cb5e67cdad: function() {
291
322
  const ret = typeof self === 'undefined' ? null : self;
292
323
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
293
324
  },
294
- __wbg_static_accessor_WINDOW_ee64f0b3d8354c0b: function() {
325
+ __wbg_static_accessor_WINDOW_168f178805d978fe: function() {
295
326
  const ret = typeof window === 'undefined' ? null : window;
296
327
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
297
328
  },
298
- __wbg_value_d5b248ce8419bd1b: function(arg0) {
329
+ __wbg_value_49f783bb59765962: function(arg0) {
299
330
  const ret = arg0.value;
300
331
  return ret;
301
332
  },
@@ -327,7 +358,7 @@ function __wbg_get_imports() {
327
358
 
328
359
  const EngineFinalization = (typeof FinalizationRegistry === 'undefined')
329
360
  ? { register: () => {}, unregister: () => {} }
330
- : new FinalizationRegistry(ptr => wasm.__wbg_engine_free(ptr >>> 0, 1));
361
+ : new FinalizationRegistry(ptr => wasm.__wbg_engine_free(ptr, 1));
331
362
 
332
363
  function addToExternrefTable0(obj) {
333
364
  const idx = wasm.__externref_table_alloc();
@@ -414,8 +445,7 @@ function getDataViewMemory0() {
414
445
  }
415
446
 
416
447
  function getStringFromWasm0(ptr, len) {
417
- ptr = ptr >>> 0;
418
- return decodeText(ptr, len);
448
+ return decodeText(ptr >>> 0, len);
419
449
  }
420
450
 
421
451
  let cachedUint8ArrayMemory0 = null;
@@ -652,6 +682,12 @@ class BaseFliptClient {
652
682
  this.etag = etag;
653
683
  }
654
684
  }
685
+ /**
686
+ * Returns a base64-encoded snapshot of the current flag state.
687
+ */
688
+ getSnapshot() {
689
+ return this.engine.get_snapshot();
690
+ }
655
691
  /**
656
692
  * Refresh the flags snapshot
657
693
  * @returns true if snapshot changed
@@ -832,6 +868,26 @@ class BaseFliptClient {
832
868
  }
833
869
  }
834
870
 
871
+ async function initializeSnapshot({ engine, snapshot, errorStrategy, fetcher, storeEtag }) {
872
+ if (snapshot) {
873
+ engine.seed_snapshot(snapshot);
874
+ }
875
+ try {
876
+ const resp = await fetcher();
877
+ if (!resp.ok) {
878
+ throw new Error(`Failed to fetch data: ${resp.statusText}`);
879
+ }
880
+ const data = await resp.json();
881
+ engine.snapshot(data);
882
+ storeEtag(resp);
883
+ }
884
+ catch (error) {
885
+ if (!snapshot || errorStrategy !== exports.ErrorStrategy.Fallback) {
886
+ throw error;
887
+ }
888
+ }
889
+ }
890
+
835
891
  class FliptClient extends BaseFliptClient {
836
892
  /**
837
893
  * Initialize the client
@@ -886,17 +942,16 @@ class FliptClient extends BaseFliptClient {
886
942
  if (!fetcher) {
887
943
  throw new Error('Failed to initialize fetcher');
888
944
  }
889
- // handle case if they pass in a custom fetcher that doesn't throw on non-2xx status codes
890
- const resp = await fetcher();
891
- if (!resp.ok) {
892
- throw new Error(`Failed to fetch data: ${resp.statusText}`);
893
- }
894
- const data = await resp.json();
895
945
  const engine = new Engine(namespace);
896
- engine.snapshot(data);
897
946
  // Create client instance
898
947
  const client = new FliptClient(engine, fetcher);
899
- client.storeEtag(resp);
948
+ await initializeSnapshot({
949
+ engine,
950
+ snapshot: options.snapshot,
951
+ errorStrategy: options.errorStrategy,
952
+ fetcher,
953
+ storeEtag: (resp) => client.storeEtag(resp)
954
+ });
900
955
  client.errorStrategy = options.errorStrategy;
901
956
  client.hook = options.hook;
902
957
  return client;