@flipt-io/flipt-client-js 0.4.2 → 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
  */
@@ -82,7 +114,7 @@ if (Symbol.dispose) Engine.prototype[Symbol.dispose] = Engine.prototype.free;
82
114
  function __wbg_get_imports() {
83
115
  const import0 = {
84
116
  __proto__: null,
85
- __wbg_Error_960c155d3d49e4c2: function(arg0, arg1) {
117
+ __wbg_Error_fdd633d4bb5dd76a: function(arg0, arg1) {
86
118
  const ret = Error(getStringFromWasm0(arg0, arg1));
87
119
  return ret;
88
120
  },
@@ -93,50 +125,50 @@ function __wbg_get_imports() {
93
125
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
94
126
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
95
127
  },
96
- __wbg___wbindgen_boolean_get_6ea149f0a8dcc5ff: function(arg0) {
128
+ __wbg___wbindgen_boolean_get_edaed31a367ce1bd: function(arg0) {
97
129
  const v = arg0;
98
130
  const ret = typeof(v) === 'boolean' ? v : undefined;
99
131
  return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
100
132
  },
101
- __wbg___wbindgen_debug_string_ab4b34d23d6778bd: function(arg0, arg1) {
133
+ __wbg___wbindgen_debug_string_8a447059637473e2: function(arg0, arg1) {
102
134
  const ret = debugString(arg1);
103
135
  const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
104
136
  const len1 = WASM_VECTOR_LEN;
105
137
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
106
138
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
107
139
  },
108
- __wbg___wbindgen_in_a5d8b22e52b24dd1: function(arg0, arg1) {
140
+ __wbg___wbindgen_in_4990f46af709e33c: function(arg0, arg1) {
109
141
  const ret = arg0 in arg1;
110
142
  return ret;
111
143
  },
112
- __wbg___wbindgen_is_function_3baa9db1a987f47d: function(arg0) {
144
+ __wbg___wbindgen_is_function_acc5528be2b923f2: function(arg0) {
113
145
  const ret = typeof(arg0) === 'function';
114
146
  return ret;
115
147
  },
116
- __wbg___wbindgen_is_object_63322ec0cd6ea4ef: function(arg0) {
148
+ __wbg___wbindgen_is_object_0beba4a1980d3eea: function(arg0) {
117
149
  const val = arg0;
118
150
  const ret = typeof(val) === 'object' && val !== null;
119
151
  return ret;
120
152
  },
121
- __wbg___wbindgen_is_string_6df3bf7ef1164ed3: function(arg0) {
153
+ __wbg___wbindgen_is_string_1fca8072260dd261: function(arg0) {
122
154
  const ret = typeof(arg0) === 'string';
123
155
  return ret;
124
156
  },
125
- __wbg___wbindgen_is_undefined_29a43b4d42920abd: function(arg0) {
157
+ __wbg___wbindgen_is_undefined_721f8decd50c87a3: function(arg0) {
126
158
  const ret = arg0 === undefined;
127
159
  return ret;
128
160
  },
129
- __wbg___wbindgen_jsval_loose_eq_cac3565e89b4134c: function(arg0, arg1) {
161
+ __wbg___wbindgen_jsval_loose_eq_4b9aba9e5b3c4582: function(arg0, arg1) {
130
162
  const ret = arg0 == arg1;
131
163
  return ret;
132
164
  },
133
- __wbg___wbindgen_number_get_c7f42aed0525c451: function(arg0, arg1) {
165
+ __wbg___wbindgen_number_get_1cc01dd708740256: function(arg0, arg1) {
134
166
  const obj = arg1;
135
167
  const ret = typeof(obj) === 'number' ? obj : undefined;
136
168
  getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
137
169
  getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
138
170
  },
139
- __wbg___wbindgen_string_get_7ed5322991caaec5: function(arg0, arg1) {
171
+ __wbg___wbindgen_string_get_71bb4348194e31f0: function(arg0, arg1) {
140
172
  const obj = arg1;
141
173
  const ret = typeof(obj) === 'string' ? obj : undefined;
142
174
  var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -144,18 +176,18 @@ function __wbg_get_imports() {
144
176
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
145
177
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
146
178
  },
147
- __wbg___wbindgen_throw_6b64449b9b9ed33c: function(arg0, arg1) {
179
+ __wbg___wbindgen_throw_ea4887a5f8f9a9db: function(arg0, arg1) {
148
180
  throw new Error(getStringFromWasm0(arg0, arg1));
149
181
  },
150
- __wbg_call_14b169f759b26747: function() { return handleError(function (arg0, arg1) {
182
+ __wbg_call_8e98ed2f3c86c4b5: function() { return handleError(function (arg0, arg1) {
151
183
  const ret = arg0.call(arg1);
152
184
  return ret;
153
185
  }, arguments); },
154
- __wbg_done_9158f7cc8751ba32: function(arg0) {
186
+ __wbg_done_b62d4a7d2286852a: function(arg0) {
155
187
  const ret = arg0.done;
156
188
  return ret;
157
189
  },
158
- __wbg_entries_e0b73aa8571ddb56: function(arg0) {
190
+ __wbg_entries_c261c3fa1f281256: function(arg0) {
159
191
  const ret = Object.entries(arg0);
160
192
  return ret;
161
193
  },
@@ -170,19 +202,19 @@ function __wbg_get_imports() {
170
202
  wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
171
203
  }
172
204
  },
173
- __wbg_getTime_da7c55f52b71e8c6: function(arg0) {
205
+ __wbg_getTime_7a770f8a2ec8d634: function(arg0) {
174
206
  const ret = arg0.getTime();
175
207
  return ret;
176
208
  },
177
- __wbg_get_1affdbdd5573b16a: function() { return handleError(function (arg0, arg1) {
178
- const ret = Reflect.get(arg0, arg1);
179
- return ret;
180
- }, arguments); },
181
- __wbg_get_8360291721e2339f: function(arg0, arg1) {
209
+ __wbg_get_197a3fe98f169e38: function(arg0, arg1) {
182
210
  const ret = arg0[arg1 >>> 0];
183
211
  return ret;
184
212
  },
185
- __wbg_get_unchecked_17f53dad852b9588: function(arg0, arg1) {
213
+ __wbg_get_9a29be2cb383ed9a: function() { return handleError(function (arg0, arg1) {
214
+ const ret = Reflect.get(arg0, arg1);
215
+ return ret;
216
+ }, arguments); },
217
+ __wbg_get_unchecked_54a4374c38e08460: function(arg0, arg1) {
186
218
  const ret = arg0[arg1 >>> 0];
187
219
  return ret;
188
220
  },
@@ -190,7 +222,7 @@ function __wbg_get_imports() {
190
222
  const ret = arg0[arg1];
191
223
  return ret;
192
224
  },
193
- __wbg_instanceof_ArrayBuffer_7c8433c6ed14ffe3: function(arg0) {
225
+ __wbg_instanceof_ArrayBuffer_2a7bb09fee70c2da: function(arg0) {
194
226
  let result;
195
227
  try {
196
228
  result = arg0 instanceof ArrayBuffer;
@@ -200,7 +232,7 @@ function __wbg_get_imports() {
200
232
  const ret = result;
201
233
  return ret;
202
234
  },
203
- __wbg_instanceof_Uint8Array_152ba1f289edcf3f: function(arg0) {
235
+ __wbg_instanceof_Uint8Array_f080092dc70f5d58: function(arg0) {
204
236
  let result;
205
237
  try {
206
238
  result = arg0 instanceof Uint8Array;
@@ -210,47 +242,47 @@ function __wbg_get_imports() {
210
242
  const ret = result;
211
243
  return ret;
212
244
  },
213
- __wbg_isArray_c3109d14ffc06469: function(arg0) {
245
+ __wbg_isArray_145a34fd0a38d37b: function(arg0) {
214
246
  const ret = Array.isArray(arg0);
215
247
  return ret;
216
248
  },
217
- __wbg_iterator_013bc09ec998c2a7: function() {
249
+ __wbg_iterator_cc47ba25a2be735a: function() {
218
250
  const ret = Symbol.iterator;
219
251
  return ret;
220
252
  },
221
- __wbg_length_3d4ecd04bd8d22f1: function(arg0) {
253
+ __wbg_length_589238bdcf171f0e: function(arg0) {
222
254
  const ret = arg0.length;
223
255
  return ret;
224
256
  },
225
- __wbg_length_9f1775224cf1d815: function(arg0) {
257
+ __wbg_length_c6054974c0a6cdb9: function(arg0) {
226
258
  const ret = arg0.length;
227
259
  return ret;
228
260
  },
229
- __wbg_new_0_4d657201ced14de3: function() {
261
+ __wbg_new_0_1b32bedde98fef4b: function() {
230
262
  const ret = new Date();
231
263
  return ret;
232
264
  },
233
- __wbg_new_0c7403db6e782f19: function(arg0) {
234
- const ret = new Uint8Array(arg0);
235
- return ret;
236
- },
237
265
  __wbg_new_227d7c05414eb861: function() {
238
266
  const ret = new Error();
239
267
  return ret;
240
268
  },
241
- __wbg_new_682678e2f47e32bc: function() {
269
+ __wbg_new_2e117a478906f062: function() {
270
+ const ret = new Object();
271
+ return ret;
272
+ },
273
+ __wbg_new_36e147a8ced3c6e0: function() {
242
274
  const ret = new Array();
243
275
  return ret;
244
276
  },
245
- __wbg_new_aa8d0fa9762c29bd: function() {
246
- const ret = new Object();
277
+ __wbg_new_81880fb5002cb255: function(arg0) {
278
+ const ret = new Uint8Array(arg0);
247
279
  return ret;
248
280
  },
249
- __wbg_next_0340c4ae324393c3: function() { return handleError(function (arg0) {
281
+ __wbg_next_0c4066e251d2eff9: function() { return handleError(function (arg0) {
250
282
  const ret = arg0.next();
251
283
  return ret;
252
284
  }, arguments); },
253
- __wbg_next_7646edaa39458ef7: function(arg0) {
285
+ __wbg_next_402fa10b59ab20c3: function(arg0) {
254
286
  const ret = arg0.next;
255
287
  return ret;
256
288
  },
@@ -262,15 +294,15 @@ function __wbg_get_imports() {
262
294
  const ret = arg0.performance;
263
295
  return ret;
264
296
  },
265
- __wbg_prototypesetcall_a6b02eb00b0f4ce2: function(arg0, arg1, arg2) {
297
+ __wbg_prototypesetcall_d721637c7ca66eb8: function(arg0, arg1, arg2) {
266
298
  Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
267
299
  },
268
- __wbg_set_3bf1de9fab0cd644: function(arg0, arg1, arg2) {
269
- arg0[arg1 >>> 0] = arg2;
270
- },
271
300
  __wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
272
301
  arg0[arg1] = arg2;
273
302
  },
303
+ __wbg_set_dc601f4a69da0bc2: function(arg0, arg1, arg2) {
304
+ arg0[arg1 >>> 0] = arg2;
305
+ },
274
306
  __wbg_stack_3b0d974bbf31e44f: function(arg0, arg1) {
275
307
  const ret = arg1.stack;
276
308
  const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -278,23 +310,23 @@ function __wbg_get_imports() {
278
310
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
279
311
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
280
312
  },
281
- __wbg_static_accessor_GLOBAL_8cfadc87a297ca02: function() {
282
- const ret = typeof global === 'undefined' ? null : global;
313
+ __wbg_static_accessor_GLOBAL_THIS_2fee5048bcca5938: function() {
314
+ const ret = typeof globalThis === 'undefined' ? null : globalThis;
283
315
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
284
316
  },
285
- __wbg_static_accessor_GLOBAL_THIS_602256ae5c8f42cf: function() {
286
- const ret = typeof globalThis === 'undefined' ? null : globalThis;
317
+ __wbg_static_accessor_GLOBAL_ce44e66a4935da8c: function() {
318
+ const ret = typeof global === 'undefined' ? null : global;
287
319
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
288
320
  },
289
- __wbg_static_accessor_SELF_e445c1c7484aecc3: function() {
321
+ __wbg_static_accessor_SELF_44f6e0cb5e67cdad: function() {
290
322
  const ret = typeof self === 'undefined' ? null : self;
291
323
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
292
324
  },
293
- __wbg_static_accessor_WINDOW_f20e8576ef1e0f17: function() {
325
+ __wbg_static_accessor_WINDOW_168f178805d978fe: function() {
294
326
  const ret = typeof window === 'undefined' ? null : window;
295
327
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
296
328
  },
297
- __wbg_value_ee3a06f4579184fa: function(arg0) {
329
+ __wbg_value_49f783bb59765962: function(arg0) {
298
330
  const ret = arg0.value;
299
331
  return ret;
300
332
  },
@@ -326,7 +358,7 @@ function __wbg_get_imports() {
326
358
 
327
359
  const EngineFinalization = (typeof FinalizationRegistry === 'undefined')
328
360
  ? { register: () => {}, unregister: () => {} }
329
- : new FinalizationRegistry(ptr => wasm.__wbg_engine_free(ptr >>> 0, 1));
361
+ : new FinalizationRegistry(ptr => wasm.__wbg_engine_free(ptr, 1));
330
362
 
331
363
  function addToExternrefTable0(obj) {
332
364
  const idx = wasm.__externref_table_alloc();
@@ -413,8 +445,7 @@ function getDataViewMemory0() {
413
445
  }
414
446
 
415
447
  function getStringFromWasm0(ptr, len) {
416
- ptr = ptr >>> 0;
417
- return decodeText(ptr, len);
448
+ return decodeText(ptr >>> 0, len);
418
449
  }
419
450
 
420
451
  let cachedUint8ArrayMemory0 = null;
@@ -651,6 +682,12 @@ class BaseFliptClient {
651
682
  this.etag = etag;
652
683
  }
653
684
  }
685
+ /**
686
+ * Returns a base64-encoded snapshot of the current flag state.
687
+ */
688
+ getSnapshot() {
689
+ return this.engine.get_snapshot();
690
+ }
654
691
  /**
655
692
  * Refresh the flags snapshot
656
693
  * @returns true if snapshot changed
@@ -831,6 +868,26 @@ class BaseFliptClient {
831
868
  }
832
869
  }
833
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
+
834
891
  class FliptClient extends BaseFliptClient {
835
892
  /**
836
893
  * Initialize the client
@@ -885,17 +942,16 @@ class FliptClient extends BaseFliptClient {
885
942
  if (!fetcher) {
886
943
  throw new Error('Failed to initialize fetcher');
887
944
  }
888
- // handle case if they pass in a custom fetcher that doesn't throw on non-2xx status codes
889
- const resp = await fetcher();
890
- if (!resp.ok) {
891
- throw new Error(`Failed to fetch data: ${resp.statusText}`);
892
- }
893
- const data = await resp.json();
894
945
  const engine = new Engine(namespace);
895
- engine.snapshot(data);
896
946
  // Create client instance
897
947
  const client = new FliptClient(engine, fetcher);
898
- 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
+ });
899
955
  client.errorStrategy = options.errorStrategy;
900
956
  client.hook = options.hook;
901
957
  return client;