@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.
@@ -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_fdd633d4bb5dd76a: 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_edaed31a367ce1bd: 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_8a447059637473e2: 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_4990f46af709e33c: 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_acc5528be2b923f2: 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_0beba4a1980d3eea: 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_1fca8072260dd261: 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_721f8decd50c87a3: 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_4b9aba9e5b3c4582: 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_1cc01dd708740256: 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_71bb4348194e31f0: 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,18 +173,18 @@ 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_ea4887a5f8f9a9db: 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_8e98ed2f3c86c4b5: 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_b62d4a7d2286852a: function(arg0) {
152
184
  const ret = arg0.done;
153
185
  return ret;
154
186
  },
155
- __wbg_entries_e0b73aa8571ddb56: function(arg0) {
187
+ __wbg_entries_c261c3fa1f281256: function(arg0) {
156
188
  const ret = Object.entries(arg0);
157
189
  return ret;
158
190
  },
@@ -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_7a770f8a2ec8d634: 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_197a3fe98f169e38: 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_9a29be2cb383ed9a: function() { return handleError(function (arg0, arg1) {
211
+ const ret = Reflect.get(arg0, arg1);
212
+ return ret;
213
+ }, arguments); },
214
+ __wbg_get_unchecked_54a4374c38e08460: 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_2a7bb09fee70c2da: 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_f080092dc70f5d58: 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_145a34fd0a38d37b: function(arg0) {
211
243
  const ret = Array.isArray(arg0);
212
244
  return ret;
213
245
  },
214
- __wbg_iterator_013bc09ec998c2a7: function() {
246
+ __wbg_iterator_cc47ba25a2be735a: function() {
215
247
  const ret = Symbol.iterator;
216
248
  return ret;
217
249
  },
218
- __wbg_length_3d4ecd04bd8d22f1: function(arg0) {
250
+ __wbg_length_589238bdcf171f0e: function(arg0) {
219
251
  const ret = arg0.length;
220
252
  return ret;
221
253
  },
222
- __wbg_length_9f1775224cf1d815: function(arg0) {
254
+ __wbg_length_c6054974c0a6cdb9: function(arg0) {
223
255
  const ret = arg0.length;
224
256
  return ret;
225
257
  },
226
- __wbg_new_0_4d657201ced14de3: function() {
258
+ __wbg_new_0_1b32bedde98fef4b: 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);
232
- return ret;
233
- },
234
262
  __wbg_new_227d7c05414eb861: function() {
235
263
  const ret = new Error();
236
264
  return ret;
237
265
  },
238
- __wbg_new_682678e2f47e32bc: function() {
266
+ __wbg_new_2e117a478906f062: function() {
267
+ const ret = new Object();
268
+ return ret;
269
+ },
270
+ __wbg_new_36e147a8ced3c6e0: function() {
239
271
  const ret = new Array();
240
272
  return ret;
241
273
  },
242
- __wbg_new_aa8d0fa9762c29bd: function() {
243
- const ret = new Object();
274
+ __wbg_new_81880fb5002cb255: function(arg0) {
275
+ const ret = new Uint8Array(arg0);
244
276
  return ret;
245
277
  },
246
- __wbg_next_0340c4ae324393c3: function() { return handleError(function (arg0) {
278
+ __wbg_next_0c4066e251d2eff9: 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_402fa10b59ab20c3: 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_d721637c7ca66eb8: 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_dc601f4a69da0bc2: 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_2fee5048bcca5938: 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_ce44e66a4935da8c: 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_44f6e0cb5e67cdad: 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_168f178805d978fe: 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_49f783bb59765962: 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;
@@ -648,6 +679,12 @@ class BaseFliptClient {
648
679
  this.etag = etag;
649
680
  }
650
681
  }
682
+ /**
683
+ * Returns a base64-encoded snapshot of the current flag state.
684
+ */
685
+ getSnapshot() {
686
+ return this.engine.get_snapshot();
687
+ }
651
688
  /**
652
689
  * Refresh the flags snapshot
653
690
  * @returns true if snapshot changed
@@ -828,6 +865,26 @@ class BaseFliptClient {
828
865
  }
829
866
  }
830
867
 
868
+ async function initializeSnapshot({ engine, snapshot, errorStrategy, fetcher, storeEtag }) {
869
+ if (snapshot) {
870
+ engine.seed_snapshot(snapshot);
871
+ }
872
+ try {
873
+ const resp = await fetcher();
874
+ if (!resp.ok) {
875
+ throw new Error(`Failed to fetch data: ${resp.statusText}`);
876
+ }
877
+ const data = await resp.json();
878
+ engine.snapshot(data);
879
+ storeEtag(resp);
880
+ }
881
+ catch (error) {
882
+ if (!snapshot || errorStrategy !== ErrorStrategy.Fallback) {
883
+ throw error;
884
+ }
885
+ }
886
+ }
887
+
831
888
  class FliptClient extends BaseFliptClient {
832
889
  /**
833
890
  * Initialize the client
@@ -882,17 +939,16 @@ class FliptClient extends BaseFliptClient {
882
939
  if (!fetcher) {
883
940
  throw new Error('Failed to initialize fetcher');
884
941
  }
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
942
  const engine = new Engine(namespace);
892
- engine.snapshot(data);
893
943
  // Create client instance
894
944
  const client = new FliptClient(engine, fetcher);
895
- client.storeEtag(resp);
945
+ await initializeSnapshot({
946
+ engine,
947
+ snapshot: options.snapshot,
948
+ errorStrategy: options.errorStrategy,
949
+ fetcher,
950
+ storeEtag: (resp) => client.storeEtag(resp)
951
+ });
896
952
  client.errorStrategy = options.errorStrategy;
897
953
  client.hook = options.hook;
898
954
  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.0",
4
4
  "description": "Flipt Client Evaluation SDK for Web and Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/node/index.cjs",