@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.
@@ -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
  */
@@ -76,11 +108,10 @@ class Engine {
76
108
  }
77
109
  }
78
110
  if (Symbol.dispose) Engine.prototype[Symbol.dispose] = Engine.prototype.free;
79
-
80
111
  function __wbg_get_imports() {
81
112
  const import0 = {
82
113
  __proto__: null,
83
- __wbg_Error_55538483de6e3abe: function(arg0, arg1) {
114
+ __wbg_Error_fdd633d4bb5dd76a: function(arg0, arg1) {
84
115
  const ret = Error(getStringFromWasm0(arg0, arg1));
85
116
  return ret;
86
117
  },
@@ -91,50 +122,50 @@ function __wbg_get_imports() {
91
122
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
92
123
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
93
124
  },
94
- __wbg___wbindgen_boolean_get_fe2a24fdfdb4064f: function(arg0) {
125
+ __wbg___wbindgen_boolean_get_edaed31a367ce1bd: function(arg0) {
95
126
  const v = arg0;
96
127
  const ret = typeof(v) === 'boolean' ? v : undefined;
97
128
  return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
98
129
  },
99
- __wbg___wbindgen_debug_string_d89627202d0155b7: function(arg0, arg1) {
130
+ __wbg___wbindgen_debug_string_8a447059637473e2: function(arg0, arg1) {
100
131
  const ret = debugString(arg1);
101
132
  const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
102
133
  const len1 = WASM_VECTOR_LEN;
103
134
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
104
135
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
105
136
  },
106
- __wbg___wbindgen_in_fe3eb6a509f75744: function(arg0, arg1) {
137
+ __wbg___wbindgen_in_4990f46af709e33c: function(arg0, arg1) {
107
138
  const ret = arg0 in arg1;
108
139
  return ret;
109
140
  },
110
- __wbg___wbindgen_is_function_2a95406423ea8626: function(arg0) {
141
+ __wbg___wbindgen_is_function_acc5528be2b923f2: function(arg0) {
111
142
  const ret = typeof(arg0) === 'function';
112
143
  return ret;
113
144
  },
114
- __wbg___wbindgen_is_object_59a002e76b059312: function(arg0) {
145
+ __wbg___wbindgen_is_object_0beba4a1980d3eea: function(arg0) {
115
146
  const val = arg0;
116
147
  const ret = typeof(val) === 'object' && val !== null;
117
148
  return ret;
118
149
  },
119
- __wbg___wbindgen_is_string_624d5244bb2bc87c: function(arg0) {
150
+ __wbg___wbindgen_is_string_1fca8072260dd261: function(arg0) {
120
151
  const ret = typeof(arg0) === 'string';
121
152
  return ret;
122
153
  },
123
- __wbg___wbindgen_is_undefined_87a3a837f331fef5: function(arg0) {
154
+ __wbg___wbindgen_is_undefined_721f8decd50c87a3: function(arg0) {
124
155
  const ret = arg0 === undefined;
125
156
  return ret;
126
157
  },
127
- __wbg___wbindgen_jsval_loose_eq_cf851f110c48f9ba: function(arg0, arg1) {
158
+ __wbg___wbindgen_jsval_loose_eq_4b9aba9e5b3c4582: function(arg0, arg1) {
128
159
  const ret = arg0 == arg1;
129
160
  return ret;
130
161
  },
131
- __wbg___wbindgen_number_get_769f3676dc20c1d7: function(arg0, arg1) {
162
+ __wbg___wbindgen_number_get_1cc01dd708740256: function(arg0, arg1) {
132
163
  const obj = arg1;
133
164
  const ret = typeof(obj) === 'number' ? obj : undefined;
134
165
  getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
135
166
  getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
136
167
  },
137
- __wbg___wbindgen_string_get_f1161390414f9b59: function(arg0, arg1) {
168
+ __wbg___wbindgen_string_get_71bb4348194e31f0: function(arg0, arg1) {
138
169
  const obj = arg1;
139
170
  const ret = typeof(obj) === 'string' ? obj : undefined;
140
171
  var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -142,18 +173,18 @@ function __wbg_get_imports() {
142
173
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
143
174
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
144
175
  },
145
- __wbg___wbindgen_throw_5549492daedad139: function(arg0, arg1) {
176
+ __wbg___wbindgen_throw_ea4887a5f8f9a9db: function(arg0, arg1) {
146
177
  throw new Error(getStringFromWasm0(arg0, arg1));
147
178
  },
148
- __wbg_call_6ae20895a60069a2: function() { return handleError(function (arg0, arg1) {
179
+ __wbg_call_8e98ed2f3c86c4b5: function() { return handleError(function (arg0, arg1) {
149
180
  const ret = arg0.call(arg1);
150
181
  return ret;
151
182
  }, arguments); },
152
- __wbg_done_19f92cb1f8738aba: function(arg0) {
183
+ __wbg_done_b62d4a7d2286852a: function(arg0) {
153
184
  const ret = arg0.done;
154
185
  return ret;
155
186
  },
156
- __wbg_entries_28ed7cb892e12eff: function(arg0) {
187
+ __wbg_entries_c261c3fa1f281256: function(arg0) {
157
188
  const ret = Object.entries(arg0);
158
189
  return ret;
159
190
  },
@@ -168,19 +199,19 @@ function __wbg_get_imports() {
168
199
  wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
169
200
  }
170
201
  },
171
- __wbg_getTime_c3af35594e283356: function(arg0) {
202
+ __wbg_getTime_7a770f8a2ec8d634: function(arg0) {
172
203
  const ret = arg0.getTime();
173
204
  return ret;
174
205
  },
175
- __wbg_get_94f5fc088edd3138: function(arg0, arg1) {
206
+ __wbg_get_197a3fe98f169e38: function(arg0, arg1) {
176
207
  const ret = arg0[arg1 >>> 0];
177
208
  return ret;
178
209
  },
179
- __wbg_get_a50328e7325d7f9b: function() { return handleError(function (arg0, arg1) {
210
+ __wbg_get_9a29be2cb383ed9a: function() { return handleError(function (arg0, arg1) {
180
211
  const ret = Reflect.get(arg0, arg1);
181
212
  return ret;
182
213
  }, arguments); },
183
- __wbg_get_unchecked_7c6bbabf5b0b1fbf: function(arg0, arg1) {
214
+ __wbg_get_unchecked_54a4374c38e08460: function(arg0, arg1) {
184
215
  const ret = arg0[arg1 >>> 0];
185
216
  return ret;
186
217
  },
@@ -188,7 +219,7 @@ function __wbg_get_imports() {
188
219
  const ret = arg0[arg1];
189
220
  return ret;
190
221
  },
191
- __wbg_instanceof_ArrayBuffer_8d855993947fc3a2: function(arg0) {
222
+ __wbg_instanceof_ArrayBuffer_2a7bb09fee70c2da: function(arg0) {
192
223
  let result;
193
224
  try {
194
225
  result = arg0 instanceof ArrayBuffer;
@@ -198,7 +229,7 @@ function __wbg_get_imports() {
198
229
  const ret = result;
199
230
  return ret;
200
231
  },
201
- __wbg_instanceof_Uint8Array_ce24d58a5f4bdcc3: function(arg0) {
232
+ __wbg_instanceof_Uint8Array_f080092dc70f5d58: function(arg0) {
202
233
  let result;
203
234
  try {
204
235
  result = arg0 instanceof Uint8Array;
@@ -208,50 +239,50 @@ function __wbg_get_imports() {
208
239
  const ret = result;
209
240
  return ret;
210
241
  },
211
- __wbg_isArray_867202cf8f195ed8: function(arg0) {
242
+ __wbg_isArray_145a34fd0a38d37b: function(arg0) {
212
243
  const ret = Array.isArray(arg0);
213
244
  return ret;
214
245
  },
215
- __wbg_iterator_54661826e186eb6a: function() {
246
+ __wbg_iterator_cc47ba25a2be735a: function() {
216
247
  const ret = Symbol.iterator;
217
248
  return ret;
218
249
  },
219
- __wbg_length_e6e1633fbea6cfa9: function(arg0) {
250
+ __wbg_length_589238bdcf171f0e: function(arg0) {
220
251
  const ret = arg0.length;
221
252
  return ret;
222
253
  },
223
- __wbg_length_fae3e439140f48a4: function(arg0) {
254
+ __wbg_length_c6054974c0a6cdb9: function(arg0) {
224
255
  const ret = arg0.length;
225
256
  return ret;
226
257
  },
227
- __wbg_new_0_e649c99e7382313f: function() {
258
+ __wbg_new_0_1b32bedde98fef4b: function() {
228
259
  const ret = new Date();
229
260
  return ret;
230
261
  },
231
- __wbg_new_1d96678aaacca32e: function(arg0) {
232
- const ret = new Uint8Array(arg0);
233
- return ret;
234
- },
235
262
  __wbg_new_227d7c05414eb861: function() {
236
263
  const ret = new Error();
237
264
  return ret;
238
265
  },
239
- __wbg_new_4370be21fa2b2f80: function() {
240
- const ret = new Array();
266
+ __wbg_new_2e117a478906f062: function() {
267
+ const ret = new Object();
241
268
  return ret;
242
269
  },
243
- __wbg_new_48e1d86cfd30c8e7: function() {
244
- const ret = new Object();
270
+ __wbg_new_36e147a8ced3c6e0: function() {
271
+ const ret = new Array();
245
272
  return ret;
246
273
  },
247
- __wbg_next_55d835fe0ab5b3e7: function(arg0) {
248
- const ret = arg0.next;
274
+ __wbg_new_81880fb5002cb255: function(arg0) {
275
+ const ret = new Uint8Array(arg0);
249
276
  return ret;
250
277
  },
251
- __wbg_next_e34cfb9df1518d7c: function() { return handleError(function (arg0) {
278
+ __wbg_next_0c4066e251d2eff9: function() { return handleError(function (arg0) {
252
279
  const ret = arg0.next();
253
280
  return ret;
254
281
  }, arguments); },
282
+ __wbg_next_402fa10b59ab20c3: function(arg0) {
283
+ const ret = arg0.next;
284
+ return ret;
285
+ },
255
286
  __wbg_now_e7c6795a7f81e10f: function(arg0) {
256
287
  const ret = arg0.now();
257
288
  return ret;
@@ -260,15 +291,15 @@ function __wbg_get_imports() {
260
291
  const ret = arg0.performance;
261
292
  return ret;
262
293
  },
263
- __wbg_prototypesetcall_3875d54d12ef2eec: function(arg0, arg1, arg2) {
294
+ __wbg_prototypesetcall_d721637c7ca66eb8: function(arg0, arg1, arg2) {
264
295
  Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
265
296
  },
266
- __wbg_set_4702dfa37c77f492: function(arg0, arg1, arg2) {
267
- arg0[arg1 >>> 0] = arg2;
268
- },
269
297
  __wbg_set_6be42768c690e380: function(arg0, arg1, arg2) {
270
298
  arg0[arg1] = arg2;
271
299
  },
300
+ __wbg_set_dc601f4a69da0bc2: function(arg0, arg1, arg2) {
301
+ arg0[arg1 >>> 0] = arg2;
302
+ },
272
303
  __wbg_stack_3b0d974bbf31e44f: function(arg0, arg1) {
273
304
  const ret = arg1.stack;
274
305
  const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -276,23 +307,23 @@ function __wbg_get_imports() {
276
307
  getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
277
308
  getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
278
309
  },
279
- __wbg_static_accessor_GLOBAL_8dfb7f5e26ebe523: function() {
280
- const ret = typeof global === 'undefined' ? null : global;
310
+ __wbg_static_accessor_GLOBAL_THIS_2fee5048bcca5938: function() {
311
+ const ret = typeof globalThis === 'undefined' ? null : globalThis;
281
312
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
282
313
  },
283
- __wbg_static_accessor_GLOBAL_THIS_941154efc8395cdd: function() {
284
- const ret = typeof globalThis === 'undefined' ? null : globalThis;
314
+ __wbg_static_accessor_GLOBAL_ce44e66a4935da8c: function() {
315
+ const ret = typeof global === 'undefined' ? null : global;
285
316
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
286
317
  },
287
- __wbg_static_accessor_SELF_58dac9af822f561f: function() {
318
+ __wbg_static_accessor_SELF_44f6e0cb5e67cdad: function() {
288
319
  const ret = typeof self === 'undefined' ? null : self;
289
320
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
290
321
  },
291
- __wbg_static_accessor_WINDOW_ee64f0b3d8354c0b: function() {
322
+ __wbg_static_accessor_WINDOW_168f178805d978fe: function() {
292
323
  const ret = typeof window === 'undefined' ? null : window;
293
324
  return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
294
325
  },
295
- __wbg_value_d5b248ce8419bd1b: function(arg0) {
326
+ __wbg_value_49f783bb59765962: function(arg0) {
296
327
  const ret = arg0.value;
297
328
  return ret;
298
329
  },
@@ -324,7 +355,7 @@ function __wbg_get_imports() {
324
355
 
325
356
  const EngineFinalization = (typeof FinalizationRegistry === 'undefined')
326
357
  ? { register: () => {}, unregister: () => {} }
327
- : new FinalizationRegistry(ptr => wasm.__wbg_engine_free(ptr >>> 0, 1));
358
+ : new FinalizationRegistry(ptr => wasm.__wbg_engine_free(ptr, 1));
328
359
 
329
360
  function addToExternrefTable0(obj) {
330
361
  const idx = wasm.__externref_table_alloc();
@@ -411,8 +442,7 @@ function getDataViewMemory0() {
411
442
  }
412
443
 
413
444
  function getStringFromWasm0(ptr, len) {
414
- ptr = ptr >>> 0;
415
- return decodeText(ptr, len);
445
+ return decodeText(ptr >>> 0, len);
416
446
  }
417
447
 
418
448
  let cachedUint8ArrayMemory0 = null;
@@ -649,6 +679,12 @@ class BaseFliptClient {
649
679
  this.etag = etag;
650
680
  }
651
681
  }
682
+ /**
683
+ * Returns a base64-encoded snapshot of the current flag state.
684
+ */
685
+ getSnapshot() {
686
+ return this.engine.get_snapshot();
687
+ }
652
688
  /**
653
689
  * Refresh the flags snapshot
654
690
  * @returns true if snapshot changed
@@ -829,6 +865,26 @@ class BaseFliptClient {
829
865
  }
830
866
  }
831
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
+
832
888
  class FliptClient extends BaseFliptClient {
833
889
  /**
834
890
  * Initialize the client
@@ -883,17 +939,16 @@ class FliptClient extends BaseFliptClient {
883
939
  if (!fetcher) {
884
940
  throw new Error('Failed to initialize fetcher');
885
941
  }
886
- // handle case if they pass in a custom fetcher that doesn't throw on non-2xx status codes
887
- const resp = await fetcher();
888
- if (!resp.ok) {
889
- throw new Error(`Failed to fetch data: ${resp.statusText}`);
890
- }
891
- const data = await resp.json();
892
942
  const engine = new Engine(namespace);
893
- engine.snapshot(data);
894
943
  // Create client instance
895
944
  const client = new FliptClient(engine, fetcher);
896
- 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
+ });
897
952
  client.errorStrategy = options.errorStrategy;
898
953
  client.hook = options.hook;
899
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.1",
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",
@@ -105,7 +105,7 @@
105
105
  "ts-jest": "^29.0.3",
106
106
  "tslib": "^2.6.2",
107
107
  "typescript": "^5.4.3",
108
- "undici": "^7.1.0"
108
+ "undici": "^8.0.2"
109
109
  },
110
110
  "engines": {
111
111
  "node": ">=14"