@openfeature/flagd-provider 0.13.3 → 0.14.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.
package/index.esm.js CHANGED
@@ -1,8 +1,9 @@
1
- import { GeneralError, FlagNotFoundError, TypeMismatchError, ParseError, StandardResolutionReasons, OpenFeatureError, OpenFeatureEventEmitter, ProviderEvents } from '@openfeature/server-sdk';
2
- import { makeGenericClientConstructor, status, credentials } from '@grpc/grpc-js';
1
+ import { ProviderFatalError, GeneralError, FlagNotFoundError, TypeMismatchError, ParseError, StandardResolutionReasons, OpenFeatureError, OpenFeatureEventEmitter, ProviderEvents } from '@openfeature/server-sdk';
2
+ import { makeGenericClientConstructor, credentials, status, Metadata } from '@grpc/grpc-js';
3
3
  import { ConnectivityState } from '@grpc/grpc-js/build/src/connectivity-state';
4
4
  import { LRUCache } from 'lru-cache';
5
5
  import { promisify } from 'node:util';
6
+ import { existsSync, readFileSync } from 'node:fs';
6
7
  import { FlagdCore } from '@openfeature/flagd-core';
7
8
  import { promises, watchFile, unwatchFile } from 'fs';
8
9
 
@@ -41,13 +42,23 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
41
42
  const EVENT_CONFIGURATION_CHANGE = 'configuration_change';
42
43
  const EVENT_PROVIDER_READY = 'provider_ready';
43
44
  const DEFAULT_MAX_CACHE_SIZE = 1000;
45
+ const DEFAULT_RETRY_GRACE_PERIOD = 5;
46
+ const DEFAULT_MAX_BACKOFF_MS = 12000;
47
+ const FLAGD_SELECTOR_HEADER = 'flagd-selector';
44
48
 
45
49
  const DEFAULT_CONFIG = {
50
+ deadlineMs: 500,
51
+ streamDeadlineMs: 600000,
46
52
  host: 'localhost',
47
53
  tls: false,
48
54
  selector: '',
49
55
  cache: 'lru',
50
56
  maxCacheSize: DEFAULT_MAX_CACHE_SIZE,
57
+ contextEnricher: (syncContext) => syncContext !== null && syncContext !== void 0 ? syncContext : {},
58
+ retryBackoffMs: 1000,
59
+ retryBackoffMaxMs: DEFAULT_MAX_BACKOFF_MS,
60
+ keepAliveTime: 0,
61
+ retryGracePeriod: DEFAULT_RETRY_GRACE_PERIOD,
51
62
  };
52
63
  const DEFAULT_RPC_CONFIG = Object.assign(Object.assign({}, DEFAULT_CONFIG), { resolverType: 'rpc', port: 8013 });
53
64
  const DEFAULT_IN_PROCESS_CONFIG = Object.assign(Object.assign({}, DEFAULT_CONFIG), { resolverType: 'in-process', port: 8015 });
@@ -55,45 +66,82 @@ var ENV_VAR;
55
66
  (function (ENV_VAR) {
56
67
  ENV_VAR["FLAGD_HOST"] = "FLAGD_HOST";
57
68
  ENV_VAR["FLAGD_PORT"] = "FLAGD_PORT";
69
+ ENV_VAR["FLAGD_SYNC_PORT"] = "FLAGD_SYNC_PORT";
70
+ ENV_VAR["FLAGD_DEADLINE_MS"] = "FLAGD_DEADLINE_MS";
71
+ ENV_VAR["FLAGD_STREAM_DEADLINE_MS"] = "FLAGD_STREAM_DEADLINE_MS";
58
72
  ENV_VAR["FLAGD_TLS"] = "FLAGD_TLS";
59
73
  ENV_VAR["FLAGD_SOCKET_PATH"] = "FLAGD_SOCKET_PATH";
74
+ ENV_VAR["FLAGD_SERVER_CERT_PATH"] = "FLAGD_SERVER_CERT_PATH";
60
75
  ENV_VAR["FLAGD_CACHE"] = "FLAGD_CACHE";
61
76
  ENV_VAR["FLAGD_MAX_CACHE_SIZE"] = "FLAGD_MAX_CACHE_SIZE";
62
77
  ENV_VAR["FLAGD_SOURCE_SELECTOR"] = "FLAGD_SOURCE_SELECTOR";
63
78
  ENV_VAR["FLAGD_RESOLVER"] = "FLAGD_RESOLVER";
64
79
  ENV_VAR["FLAGD_OFFLINE_FLAG_SOURCE_PATH"] = "FLAGD_OFFLINE_FLAG_SOURCE_PATH";
65
80
  ENV_VAR["FLAGD_DEFAULT_AUTHORITY"] = "FLAGD_DEFAULT_AUTHORITY";
81
+ ENV_VAR["FLAGD_RETRY_BACKOFF_MS"] = "FLAGD_RETRY_BACKOFF_MS";
82
+ ENV_VAR["FLAGD_RETRY_BACKOFF_MAX_MS"] = "FLAGD_RETRY_BACKOFF_MAX_MS";
83
+ ENV_VAR["FLAGD_KEEP_ALIVE_TIME_MS"] = "FLAGD_KEEP_ALIVE_TIME_MS";
84
+ ENV_VAR["FLAGD_FATAL_STATUS_CODES"] = "FLAGD_FATAL_STATUS_CODES";
85
+ ENV_VAR["FLAGD_RETRY_GRACE_PERIOD"] = "FLAGD_RETRY_GRACE_PERIOD";
66
86
  })(ENV_VAR || (ENV_VAR = {}));
67
- const getEnvVarConfig = () => {
87
+ function getEnvVarConfig() {
68
88
  var _a;
69
- return (Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (process.env[ENV_VAR.FLAGD_HOST] && {
89
+ let provider = undefined;
90
+ if (process.env[ENV_VAR.FLAGD_RESOLVER] &&
91
+ (process.env[ENV_VAR.FLAGD_RESOLVER].toLowerCase() === 'rpc' ||
92
+ process.env[ENV_VAR.FLAGD_RESOLVER].toLowerCase() === 'in-process')) {
93
+ provider = process.env[ENV_VAR.FLAGD_RESOLVER].toLowerCase();
94
+ }
95
+ return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (process.env[ENV_VAR.FLAGD_HOST] && {
70
96
  host: process.env[ENV_VAR.FLAGD_HOST],
71
97
  })), (Number(process.env[ENV_VAR.FLAGD_PORT]) && {
72
98
  port: Number(process.env[ENV_VAR.FLAGD_PORT]),
99
+ })), (Number(process.env[ENV_VAR.FLAGD_SYNC_PORT]) && {
100
+ port: Number(process.env[ENV_VAR.FLAGD_SYNC_PORT]),
101
+ })), (Number(process.env[ENV_VAR.FLAGD_DEADLINE_MS]) && {
102
+ deadlineMs: Number(process.env[ENV_VAR.FLAGD_DEADLINE_MS]),
103
+ })), (Number(process.env[ENV_VAR.FLAGD_STREAM_DEADLINE_MS]) && {
104
+ streamDeadlineMs: Number(process.env[ENV_VAR.FLAGD_STREAM_DEADLINE_MS]),
73
105
  })), (process.env[ENV_VAR.FLAGD_TLS] && {
74
- tls: ((_a = process.env[ENV_VAR.FLAGD_TLS]) === null || _a === undefined ? undefined : _a.toLowerCase()) === 'true',
106
+ tls: ((_a = process.env[ENV_VAR.FLAGD_TLS]) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'true',
75
107
  })), (process.env[ENV_VAR.FLAGD_SOCKET_PATH] && {
76
108
  socketPath: process.env[ENV_VAR.FLAGD_SOCKET_PATH],
109
+ })), (process.env[ENV_VAR.FLAGD_SERVER_CERT_PATH] && {
110
+ certPath: process.env[ENV_VAR.FLAGD_SERVER_CERT_PATH],
77
111
  })), ((process.env[ENV_VAR.FLAGD_CACHE] === 'lru' || process.env[ENV_VAR.FLAGD_CACHE] === 'disabled') && {
78
112
  cache: process.env[ENV_VAR.FLAGD_CACHE],
79
113
  })), (process.env[ENV_VAR.FLAGD_MAX_CACHE_SIZE] && {
80
114
  maxCacheSize: Number(process.env[ENV_VAR.FLAGD_MAX_CACHE_SIZE]),
81
115
  })), (process.env[ENV_VAR.FLAGD_SOURCE_SELECTOR] && {
82
116
  selector: process.env[ENV_VAR.FLAGD_SOURCE_SELECTOR],
83
- })), ((process.env[ENV_VAR.FLAGD_RESOLVER] === 'rpc' || process.env[ENV_VAR.FLAGD_RESOLVER] === 'in-process') && {
84
- resolverType: process.env[ENV_VAR.FLAGD_RESOLVER],
117
+ })), (provider && {
118
+ resolverType: provider,
85
119
  })), (process.env[ENV_VAR.FLAGD_OFFLINE_FLAG_SOURCE_PATH] && {
86
120
  offlineFlagSourcePath: process.env[ENV_VAR.FLAGD_OFFLINE_FLAG_SOURCE_PATH],
87
121
  })), (process.env[ENV_VAR.FLAGD_DEFAULT_AUTHORITY] && {
88
122
  defaultAuthority: process.env[ENV_VAR.FLAGD_DEFAULT_AUTHORITY],
89
- })));
90
- };
123
+ })), (Number(process.env[ENV_VAR.FLAGD_RETRY_BACKOFF_MS]) && {
124
+ retryBackoffMs: Number(process.env[ENV_VAR.FLAGD_RETRY_BACKOFF_MS]),
125
+ })), (Number(process.env[ENV_VAR.FLAGD_RETRY_BACKOFF_MAX_MS]) && {
126
+ retryBackoffMaxMs: Number(process.env[ENV_VAR.FLAGD_RETRY_BACKOFF_MAX_MS]),
127
+ })), (Number(process.env[ENV_VAR.FLAGD_KEEP_ALIVE_TIME_MS]) && {
128
+ keepAliveTime: Number(process.env[ENV_VAR.FLAGD_KEEP_ALIVE_TIME_MS]),
129
+ })), (process.env[ENV_VAR.FLAGD_FATAL_STATUS_CODES] && {
130
+ fatalStatusCodes: process.env[ENV_VAR.FLAGD_FATAL_STATUS_CODES]
131
+ .split(',')
132
+ .map((code) => code.trim())
133
+ .filter((code) => code.length > 0),
134
+ })), (process.env[ENV_VAR.FLAGD_RETRY_GRACE_PERIOD] && {
135
+ retryGracePeriod: Number(process.env[ENV_VAR.FLAGD_RETRY_GRACE_PERIOD]),
136
+ }));
137
+ }
91
138
  function getConfig(options = {}) {
139
+ var _a, _b;
92
140
  const envVarConfig = getEnvVarConfig();
93
141
  const defaultConfig = options.resolverType == 'in-process' || envVarConfig.resolverType == 'in-process'
94
142
  ? DEFAULT_IN_PROCESS_CONFIG
95
143
  : DEFAULT_RPC_CONFIG;
96
- return Object.assign(Object.assign(Object.assign({}, defaultConfig), envVarConfig), options);
144
+ return Object.assign(Object.assign(Object.assign(Object.assign({}, defaultConfig), envVarConfig), options), { streamDeadlineMs: (_b = (_a = options.streamDeadlineMs) !== null && _a !== void 0 ? _a : envVarConfig.streamDeadlineMs) !== null && _b !== void 0 ? _b : DEFAULT_CONFIG.streamDeadlineMs });
97
145
  }
98
146
 
99
147
  /**
@@ -119,10 +167,85 @@ function getConfig(options = {}) {
119
167
  // WebAssembly optimizations to do native i64 multiplication and divide
120
168
  var wasm = null;
121
169
  try {
122
- wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([
123
- 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11
124
- ])), {}).exports;
125
- } catch (e) {
170
+ wasm = new WebAssembly.Instance(
171
+ new WebAssembly.Module(
172
+ new Uint8Array([
173
+ // \0asm
174
+ 0, 97, 115, 109,
175
+ // version 1
176
+ 1, 0, 0, 0,
177
+
178
+ // section "type"
179
+ 1, 13, 2,
180
+ // 0, () => i32
181
+ 96, 0, 1, 127,
182
+ // 1, (i32, i32, i32, i32) => i32
183
+ 96, 4, 127, 127, 127, 127, 1, 127,
184
+
185
+ // section "function"
186
+ 3, 7, 6,
187
+ // 0, type 0
188
+ 0,
189
+ // 1, type 1
190
+ 1,
191
+ // 2, type 1
192
+ 1,
193
+ // 3, type 1
194
+ 1,
195
+ // 4, type 1
196
+ 1,
197
+ // 5, type 1
198
+ 1,
199
+
200
+ // section "global"
201
+ 6, 6, 1,
202
+ // 0, "high", mutable i32
203
+ 127, 1, 65, 0, 11,
204
+
205
+ // section "export"
206
+ 7, 50, 6,
207
+ // 0, "mul"
208
+ 3, 109, 117, 108, 0, 1,
209
+ // 1, "div_s"
210
+ 5, 100, 105, 118, 95, 115, 0, 2,
211
+ // 2, "div_u"
212
+ 5, 100, 105, 118, 95, 117, 0, 3,
213
+ // 3, "rem_s"
214
+ 5, 114, 101, 109, 95, 115, 0, 4,
215
+ // 4, "rem_u"
216
+ 5, 114, 101, 109, 95, 117, 0, 5,
217
+ // 5, "get_high"
218
+ 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0,
219
+
220
+ // section "code"
221
+ 10, 191, 1, 6,
222
+ // 0, "get_high"
223
+ 4, 0, 35, 0, 11,
224
+ // 1, "mul"
225
+ 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32,
226
+ 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4,
227
+ 167, 11,
228
+ // 2, "div_s"
229
+ 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32,
230
+ 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4,
231
+ 167, 11,
232
+ // 3, "div_u"
233
+ 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32,
234
+ 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4,
235
+ 167, 11,
236
+ // 4, "rem_s"
237
+ 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32,
238
+ 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4,
239
+ 167, 11,
240
+ // 5, "rem_u"
241
+ 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32,
242
+ 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4,
243
+ 167, 11,
244
+ ]),
245
+ ),
246
+ {},
247
+ ).exports;
248
+ } catch {
126
249
  // no wasm support :(
127
250
  }
128
251
 
@@ -137,7 +260,6 @@ try {
137
260
  * @constructor
138
261
  */
139
262
  function Long(low, high, unsigned) {
140
-
141
263
  /**
142
264
  * The low 32 bits as a signed value.
143
265
  * @type {number}
@@ -237,25 +359,21 @@ function fromInt(value, unsigned) {
237
359
  var obj, cachedObj, cache;
238
360
  if (unsigned) {
239
361
  value >>>= 0;
240
- if (cache = (0 <= value && value < 256)) {
362
+ if ((cache = 0 <= value && value < 256)) {
241
363
  cachedObj = UINT_CACHE[value];
242
- if (cachedObj)
243
- return cachedObj;
364
+ if (cachedObj) return cachedObj;
244
365
  }
245
366
  obj = fromBits(value, 0, true);
246
- if (cache)
247
- UINT_CACHE[value] = obj;
367
+ if (cache) UINT_CACHE[value] = obj;
248
368
  return obj;
249
369
  } else {
250
370
  value |= 0;
251
- if (cache = (-128 <= value && value < 128)) {
371
+ if ((cache = -128 <= value && value < 128)) {
252
372
  cachedObj = INT_CACHE[value];
253
- if (cachedObj)
254
- return cachedObj;
373
+ if (cachedObj) return cachedObj;
255
374
  }
256
375
  obj = fromBits(value, value < 0 ? -1 : 0, false);
257
- if (cache)
258
- INT_CACHE[value] = obj;
376
+ if (cache) INT_CACHE[value] = obj;
259
377
  return obj;
260
378
  }
261
379
  }
@@ -276,22 +394,20 @@ Long.fromInt = fromInt;
276
394
  * @inner
277
395
  */
278
396
  function fromNumber(value, unsigned) {
279
- if (isNaN(value))
280
- return unsigned ? UZERO : ZERO;
397
+ if (isNaN(value)) return unsigned ? UZERO : ZERO;
281
398
  if (unsigned) {
282
- if (value < 0)
283
- return UZERO;
284
- if (value >= TWO_PWR_64_DBL)
285
- return MAX_UNSIGNED_VALUE;
399
+ if (value < 0) return UZERO;
400
+ if (value >= TWO_PWR_64_DBL) return MAX_UNSIGNED_VALUE;
286
401
  } else {
287
- if (value <= -9223372036854776e3)
288
- return MIN_VALUE;
289
- if (value + 1 >= TWO_PWR_63_DBL)
290
- return MAX_VALUE;
402
+ if (value <= -9223372036854776e3) return MIN_VALUE;
403
+ if (value + 1 >= TWO_PWR_63_DBL) return MAX_VALUE;
291
404
  }
292
- if (value < 0)
293
- return fromNumber(-value, unsigned).neg();
294
- return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
405
+ if (value < 0) return fromNumber(-value, unsigned).neg();
406
+ return fromBits(
407
+ value % TWO_PWR_32_DBL | 0,
408
+ (value / TWO_PWR_32_DBL) | 0,
409
+ unsigned,
410
+ );
295
411
  }
296
412
 
297
413
  /**
@@ -342,24 +458,26 @@ var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4)
342
458
  * @inner
343
459
  */
344
460
  function fromString(str, unsigned, radix) {
345
- if (str.length === 0)
346
- throw Error('empty string');
347
- if (typeof unsigned === 'number') {
461
+ if (str.length === 0) throw Error("empty string");
462
+ if (typeof unsigned === "number") {
348
463
  // For goog.math.long compatibility
349
464
  radix = unsigned;
350
465
  unsigned = false;
351
466
  } else {
352
467
  unsigned = !!unsigned;
353
468
  }
354
- if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity")
469
+ if (
470
+ str === "NaN" ||
471
+ str === "Infinity" ||
472
+ str === "+Infinity" ||
473
+ str === "-Infinity"
474
+ )
355
475
  return unsigned ? UZERO : ZERO;
356
476
  radix = radix || 10;
357
- if (radix < 2 || 36 < radix)
358
- throw RangeError('radix');
477
+ if (radix < 2 || 36 < radix) throw RangeError("radix");
359
478
 
360
479
  var p;
361
- if ((p = str.indexOf('-')) > 0)
362
- throw Error('interior hyphen');
480
+ if ((p = str.indexOf("-")) > 0) throw Error("interior hyphen");
363
481
  else if (p === 0) {
364
482
  return fromString(str.substring(1), unsigned, radix).neg();
365
483
  }
@@ -402,18 +520,20 @@ Long.fromString = fromString;
402
520
  * @inner
403
521
  */
404
522
  function fromValue(val, unsigned) {
405
- if (typeof val === 'number')
406
- return fromNumber(val, unsigned);
407
- if (typeof val === 'string')
408
- return fromString(val, unsigned);
523
+ if (typeof val === "number") return fromNumber(val, unsigned);
524
+ if (typeof val === "string") return fromString(val, unsigned);
409
525
  // Throws for non-objects, converts non-instanceof Long:
410
- return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned);
526
+ return fromBits(
527
+ val.low,
528
+ val.high,
529
+ typeof unsigned === "boolean" ? unsigned : val.unsigned,
530
+ );
411
531
  }
412
532
 
413
533
  /**
414
534
  * Converts the specified value to a Long using the appropriate from* function for its type.
415
535
  * @function
416
- * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value
536
+ * @param {!Long|number|bigint|string|!{low: number, high: number, unsigned: boolean}} val Value
417
537
  * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
418
538
  * @returns {!Long}
419
539
  */
@@ -528,7 +648,7 @@ Long.NEG_ONE = NEG_ONE;
528
648
  * @type {!Long}
529
649
  * @inner
530
650
  */
531
- var MAX_VALUE = fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0, false);
651
+ var MAX_VALUE = fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
532
652
 
533
653
  /**
534
654
  * Maximum signed value.
@@ -540,7 +660,7 @@ Long.MAX_VALUE = MAX_VALUE;
540
660
  * @type {!Long}
541
661
  * @inner
542
662
  */
543
- var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF | 0, 0xFFFFFFFF | 0, true);
663
+ var MAX_UNSIGNED_VALUE = fromBits(0xffffffff | 0, 0xffffffff | 0, true);
544
664
 
545
665
  /**
546
666
  * Maximum unsigned value.
@@ -582,7 +702,7 @@ LongPrototype.toInt = function toInt() {
582
702
  */
583
703
  LongPrototype.toNumber = function toNumber() {
584
704
  if (this.unsigned)
585
- return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0);
705
+ return (this.high >>> 0) * TWO_PWR_32_DBL + (this.low >>> 0);
586
706
  return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
587
707
  };
588
708
 
@@ -596,11 +716,10 @@ LongPrototype.toNumber = function toNumber() {
596
716
  */
597
717
  LongPrototype.toString = function toString(radix) {
598
718
  radix = radix || 10;
599
- if (radix < 2 || 36 < radix)
600
- throw RangeError('radix');
601
- if (this.isZero())
602
- return '0';
603
- if (this.isNegative()) { // Unsigned Longs are never negative
719
+ if (radix < 2 || 36 < radix) throw RangeError("radix");
720
+ if (this.isZero()) return "0";
721
+ if (this.isNegative()) {
722
+ // Unsigned Longs are never negative
604
723
  if (this.eq(MIN_VALUE)) {
605
724
  // We need to change the Long value before it can be negated, so we remove
606
725
  // the bottom-most digit in this base and then recurse to do the rest.
@@ -608,26 +727,23 @@ LongPrototype.toString = function toString(radix) {
608
727
  div = this.div(radixLong),
609
728
  rem1 = div.mul(radixLong).sub(this);
610
729
  return div.toString(radix) + rem1.toInt().toString(radix);
611
- } else
612
- return '-' + this.neg().toString(radix);
730
+ } else return "-" + this.neg().toString(radix);
613
731
  }
614
732
 
615
733
  // Do several (6) digits each time through the loop, so as to
616
734
  // minimize the calls to the very expensive emulated div.
617
735
  var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned),
618
736
  rem = this;
619
- var result = '';
737
+ var result = "";
620
738
  while (true) {
621
739
  var remDiv = rem.div(radixToPower),
622
740
  intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0,
623
741
  digits = intval.toString(radix);
624
742
  rem = remDiv;
625
- if (rem.isZero())
626
- return digits + result;
743
+ if (rem.isZero()) return digits + result;
627
744
  else {
628
- while (digits.length < 6)
629
- digits = '0' + digits;
630
- result = '' + digits + result;
745
+ while (digits.length < 6) digits = "0" + digits;
746
+ result = "" + digits + result;
631
747
  }
632
748
  }
633
749
  };
@@ -674,15 +790,30 @@ LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() {
674
790
  * @returns {number}
675
791
  */
676
792
  LongPrototype.getNumBitsAbs = function getNumBitsAbs() {
677
- if (this.isNegative()) // Unsigned Longs are never negative
793
+ if (this.isNegative())
794
+ // Unsigned Longs are never negative
678
795
  return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
679
796
  var val = this.high != 0 ? this.high : this.low;
680
- for (var bit = 31; bit > 0; bit--)
681
- if ((val & (1 << bit)) != 0)
682
- break;
797
+ for (var bit = 31; bit > 0; bit--) if ((val & (1 << bit)) != 0) break;
683
798
  return this.high != 0 ? bit + 33 : bit + 1;
684
799
  };
685
800
 
801
+ /**
802
+ * Tests if this Long can be safely represented as a JavaScript number.
803
+ * @this {!Long}
804
+ * @returns {boolean}
805
+ */
806
+ LongPrototype.isSafeInteger = function isSafeInteger() {
807
+ // 2^53-1 is the maximum safe value
808
+ var top11Bits = this.high >> 21;
809
+ // [0, 2^53-1]
810
+ if (!top11Bits) return true;
811
+ // > 2^53-1
812
+ if (this.unsigned) return false;
813
+ // [-2^53, -1] except -2^53
814
+ return top11Bits === -1 && !(this.low === 0 && this.high === -2097152);
815
+ };
816
+
686
817
  /**
687
818
  * Tests if this Long's value equals zero.
688
819
  * @this {!Long}
@@ -737,13 +868,16 @@ LongPrototype.isEven = function isEven() {
737
868
  /**
738
869
  * Tests if this Long's value equals the specified's.
739
870
  * @this {!Long}
740
- * @param {!Long|number|string} other Other value
871
+ * @param {!Long|number|bigint|string} other Other value
741
872
  * @returns {boolean}
742
873
  */
743
874
  LongPrototype.equals = function equals(other) {
744
- if (!isLong(other))
745
- other = fromValue(other);
746
- if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1)
875
+ if (!isLong(other)) other = fromValue(other);
876
+ if (
877
+ this.unsigned !== other.unsigned &&
878
+ this.high >>> 31 === 1 &&
879
+ other.high >>> 31 === 1
880
+ )
747
881
  return false;
748
882
  return this.high === other.high && this.low === other.low;
749
883
  };
@@ -751,7 +885,7 @@ LongPrototype.equals = function equals(other) {
751
885
  /**
752
886
  * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}.
753
887
  * @function
754
- * @param {!Long|number|string} other Other value
888
+ * @param {!Long|number|bigint|string} other Other value
755
889
  * @returns {boolean}
756
890
  */
757
891
  LongPrototype.eq = LongPrototype.equals;
@@ -759,7 +893,7 @@ LongPrototype.eq = LongPrototype.equals;
759
893
  /**
760
894
  * Tests if this Long's value differs from the specified's.
761
895
  * @this {!Long}
762
- * @param {!Long|number|string} other Other value
896
+ * @param {!Long|number|bigint|string} other Other value
763
897
  * @returns {boolean}
764
898
  */
765
899
  LongPrototype.notEquals = function notEquals(other) {
@@ -769,7 +903,7 @@ LongPrototype.notEquals = function notEquals(other) {
769
903
  /**
770
904
  * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
771
905
  * @function
772
- * @param {!Long|number|string} other Other value
906
+ * @param {!Long|number|bigint|string} other Other value
773
907
  * @returns {boolean}
774
908
  */
775
909
  LongPrototype.neq = LongPrototype.notEquals;
@@ -777,7 +911,7 @@ LongPrototype.neq = LongPrototype.notEquals;
777
911
  /**
778
912
  * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
779
913
  * @function
780
- * @param {!Long|number|string} other Other value
914
+ * @param {!Long|number|bigint|string} other Other value
781
915
  * @returns {boolean}
782
916
  */
783
917
  LongPrototype.ne = LongPrototype.notEquals;
@@ -785,7 +919,7 @@ LongPrototype.ne = LongPrototype.notEquals;
785
919
  /**
786
920
  * Tests if this Long's value is less than the specified's.
787
921
  * @this {!Long}
788
- * @param {!Long|number|string} other Other value
922
+ * @param {!Long|number|bigint|string} other Other value
789
923
  * @returns {boolean}
790
924
  */
791
925
  LongPrototype.lessThan = function lessThan(other) {
@@ -795,7 +929,7 @@ LongPrototype.lessThan = function lessThan(other) {
795
929
  /**
796
930
  * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}.
797
931
  * @function
798
- * @param {!Long|number|string} other Other value
932
+ * @param {!Long|number|bigint|string} other Other value
799
933
  * @returns {boolean}
800
934
  */
801
935
  LongPrototype.lt = LongPrototype.lessThan;
@@ -803,7 +937,7 @@ LongPrototype.lt = LongPrototype.lessThan;
803
937
  /**
804
938
  * Tests if this Long's value is less than or equal the specified's.
805
939
  * @this {!Long}
806
- * @param {!Long|number|string} other Other value
940
+ * @param {!Long|number|bigint|string} other Other value
807
941
  * @returns {boolean}
808
942
  */
809
943
  LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) {
@@ -813,7 +947,7 @@ LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) {
813
947
  /**
814
948
  * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
815
949
  * @function
816
- * @param {!Long|number|string} other Other value
950
+ * @param {!Long|number|bigint|string} other Other value
817
951
  * @returns {boolean}
818
952
  */
819
953
  LongPrototype.lte = LongPrototype.lessThanOrEqual;
@@ -821,7 +955,7 @@ LongPrototype.lte = LongPrototype.lessThanOrEqual;
821
955
  /**
822
956
  * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
823
957
  * @function
824
- * @param {!Long|number|string} other Other value
958
+ * @param {!Long|number|bigint|string} other Other value
825
959
  * @returns {boolean}
826
960
  */
827
961
  LongPrototype.le = LongPrototype.lessThanOrEqual;
@@ -829,7 +963,7 @@ LongPrototype.le = LongPrototype.lessThanOrEqual;
829
963
  /**
830
964
  * Tests if this Long's value is greater than the specified's.
831
965
  * @this {!Long}
832
- * @param {!Long|number|string} other Other value
966
+ * @param {!Long|number|bigint|string} other Other value
833
967
  * @returns {boolean}
834
968
  */
835
969
  LongPrototype.greaterThan = function greaterThan(other) {
@@ -839,7 +973,7 @@ LongPrototype.greaterThan = function greaterThan(other) {
839
973
  /**
840
974
  * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}.
841
975
  * @function
842
- * @param {!Long|number|string} other Other value
976
+ * @param {!Long|number|bigint|string} other Other value
843
977
  * @returns {boolean}
844
978
  */
845
979
  LongPrototype.gt = LongPrototype.greaterThan;
@@ -847,7 +981,7 @@ LongPrototype.gt = LongPrototype.greaterThan;
847
981
  /**
848
982
  * Tests if this Long's value is greater than or equal the specified's.
849
983
  * @this {!Long}
850
- * @param {!Long|number|string} other Other value
984
+ * @param {!Long|number|bigint|string} other Other value
851
985
  * @returns {boolean}
852
986
  */
853
987
  LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) {
@@ -857,7 +991,7 @@ LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) {
857
991
  /**
858
992
  * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
859
993
  * @function
860
- * @param {!Long|number|string} other Other value
994
+ * @param {!Long|number|bigint|string} other Other value
861
995
  * @returns {boolean}
862
996
  */
863
997
  LongPrototype.gte = LongPrototype.greaterThanOrEqual;
@@ -865,7 +999,7 @@ LongPrototype.gte = LongPrototype.greaterThanOrEqual;
865
999
  /**
866
1000
  * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
867
1001
  * @function
868
- * @param {!Long|number|string} other Other value
1002
+ * @param {!Long|number|bigint|string} other Other value
869
1003
  * @returns {boolean}
870
1004
  */
871
1005
  LongPrototype.ge = LongPrototype.greaterThanOrEqual;
@@ -873,32 +1007,30 @@ LongPrototype.ge = LongPrototype.greaterThanOrEqual;
873
1007
  /**
874
1008
  * Compares this Long's value with the specified's.
875
1009
  * @this {!Long}
876
- * @param {!Long|number|string} other Other value
1010
+ * @param {!Long|number|bigint|string} other Other value
877
1011
  * @returns {number} 0 if they are the same, 1 if the this is greater and -1
878
1012
  * if the given one is greater
879
1013
  */
880
1014
  LongPrototype.compare = function compare(other) {
881
- if (!isLong(other))
882
- other = fromValue(other);
883
- if (this.eq(other))
884
- return 0;
1015
+ if (!isLong(other)) other = fromValue(other);
1016
+ if (this.eq(other)) return 0;
885
1017
  var thisNeg = this.isNegative(),
886
1018
  otherNeg = other.isNegative();
887
- if (thisNeg && !otherNeg)
888
- return -1;
889
- if (!thisNeg && otherNeg)
890
- return 1;
1019
+ if (thisNeg && !otherNeg) return -1;
1020
+ if (!thisNeg && otherNeg) return 1;
891
1021
  // At this point the sign bits are the same
892
- if (!this.unsigned)
893
- return this.sub(other).isNegative() ? -1 : 1;
1022
+ if (!this.unsigned) return this.sub(other).isNegative() ? -1 : 1;
894
1023
  // Both are positive if at least one is unsigned
895
- return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1;
1024
+ return other.high >>> 0 > this.high >>> 0 ||
1025
+ (other.high === this.high && other.low >>> 0 > this.low >>> 0)
1026
+ ? -1
1027
+ : 1;
896
1028
  };
897
1029
 
898
1030
  /**
899
1031
  * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}.
900
1032
  * @function
901
- * @param {!Long|number|string} other Other value
1033
+ * @param {!Long|number|bigint|string} other Other value
902
1034
  * @returns {number} 0 if they are the same, 1 if the this is greater and -1
903
1035
  * if the given one is greater
904
1036
  */
@@ -910,8 +1042,7 @@ LongPrototype.comp = LongPrototype.compare;
910
1042
  * @returns {!Long} Negated Long
911
1043
  */
912
1044
  LongPrototype.negate = function negate() {
913
- if (!this.unsigned && this.eq(MIN_VALUE))
914
- return MIN_VALUE;
1045
+ if (!this.unsigned && this.eq(MIN_VALUE)) return MIN_VALUE;
915
1046
  return this.not().add(ONE);
916
1047
  };
917
1048
 
@@ -925,56 +1056,57 @@ LongPrototype.neg = LongPrototype.negate;
925
1056
  /**
926
1057
  * Returns the sum of this and the specified Long.
927
1058
  * @this {!Long}
928
- * @param {!Long|number|string} addend Addend
1059
+ * @param {!Long|number|bigint|string} addend Addend
929
1060
  * @returns {!Long} Sum
930
1061
  */
931
1062
  LongPrototype.add = function add(addend) {
932
- if (!isLong(addend))
933
- addend = fromValue(addend);
1063
+ if (!isLong(addend)) addend = fromValue(addend);
934
1064
 
935
1065
  // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
936
1066
 
937
1067
  var a48 = this.high >>> 16;
938
- var a32 = this.high & 0xFFFF;
1068
+ var a32 = this.high & 0xffff;
939
1069
  var a16 = this.low >>> 16;
940
- var a00 = this.low & 0xFFFF;
1070
+ var a00 = this.low & 0xffff;
941
1071
 
942
1072
  var b48 = addend.high >>> 16;
943
- var b32 = addend.high & 0xFFFF;
1073
+ var b32 = addend.high & 0xffff;
944
1074
  var b16 = addend.low >>> 16;
945
- var b00 = addend.low & 0xFFFF;
1075
+ var b00 = addend.low & 0xffff;
946
1076
 
947
- var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
1077
+ var c48 = 0,
1078
+ c32 = 0,
1079
+ c16 = 0,
1080
+ c00 = 0;
948
1081
  c00 += a00 + b00;
949
1082
  c16 += c00 >>> 16;
950
- c00 &= 0xFFFF;
1083
+ c00 &= 0xffff;
951
1084
  c16 += a16 + b16;
952
1085
  c32 += c16 >>> 16;
953
- c16 &= 0xFFFF;
1086
+ c16 &= 0xffff;
954
1087
  c32 += a32 + b32;
955
1088
  c48 += c32 >>> 16;
956
- c32 &= 0xFFFF;
1089
+ c32 &= 0xffff;
957
1090
  c48 += a48 + b48;
958
- c48 &= 0xFFFF;
1091
+ c48 &= 0xffff;
959
1092
  return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
960
1093
  };
961
1094
 
962
1095
  /**
963
1096
  * Returns the difference of this and the specified Long.
964
1097
  * @this {!Long}
965
- * @param {!Long|number|string} subtrahend Subtrahend
1098
+ * @param {!Long|number|bigint|string} subtrahend Subtrahend
966
1099
  * @returns {!Long} Difference
967
1100
  */
968
1101
  LongPrototype.subtract = function subtract(subtrahend) {
969
- if (!isLong(subtrahend))
970
- subtrahend = fromValue(subtrahend);
1102
+ if (!isLong(subtrahend)) subtrahend = fromValue(subtrahend);
971
1103
  return this.add(subtrahend.neg());
972
1104
  };
973
1105
 
974
1106
  /**
975
1107
  * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}.
976
1108
  * @function
977
- * @param {!Long|number|string} subtrahend Subtrahend
1109
+ * @param {!Long|number|bigint|string} subtrahend Subtrahend
978
1110
  * @returns {!Long} Difference
979
1111
  */
980
1112
  LongPrototype.sub = LongPrototype.subtract;
@@ -982,38 +1114,27 @@ LongPrototype.sub = LongPrototype.subtract;
982
1114
  /**
983
1115
  * Returns the product of this and the specified Long.
984
1116
  * @this {!Long}
985
- * @param {!Long|number|string} multiplier Multiplier
1117
+ * @param {!Long|number|bigint|string} multiplier Multiplier
986
1118
  * @returns {!Long} Product
987
1119
  */
988
1120
  LongPrototype.multiply = function multiply(multiplier) {
989
- if (this.isZero())
990
- return this;
991
- if (!isLong(multiplier))
992
- multiplier = fromValue(multiplier);
1121
+ if (this.isZero()) return this;
1122
+ if (!isLong(multiplier)) multiplier = fromValue(multiplier);
993
1123
 
994
1124
  // use wasm support if present
995
1125
  if (wasm) {
996
- var low = wasm["mul"](this.low,
997
- this.high,
998
- multiplier.low,
999
- multiplier.high);
1126
+ var low = wasm["mul"](this.low, this.high, multiplier.low, multiplier.high);
1000
1127
  return fromBits(low, wasm["get_high"](), this.unsigned);
1001
1128
  }
1002
1129
 
1003
- if (multiplier.isZero())
1004
- return this.unsigned ? UZERO : ZERO;
1005
- if (this.eq(MIN_VALUE))
1006
- return multiplier.isOdd() ? MIN_VALUE : ZERO;
1007
- if (multiplier.eq(MIN_VALUE))
1008
- return this.isOdd() ? MIN_VALUE : ZERO;
1130
+ if (multiplier.isZero()) return this.unsigned ? UZERO : ZERO;
1131
+ if (this.eq(MIN_VALUE)) return multiplier.isOdd() ? MIN_VALUE : ZERO;
1132
+ if (multiplier.eq(MIN_VALUE)) return this.isOdd() ? MIN_VALUE : ZERO;
1009
1133
 
1010
1134
  if (this.isNegative()) {
1011
- if (multiplier.isNegative())
1012
- return this.neg().mul(multiplier.neg());
1013
- else
1014
- return this.neg().mul(multiplier).neg();
1015
- } else if (multiplier.isNegative())
1016
- return this.mul(multiplier.neg()).neg();
1135
+ if (multiplier.isNegative()) return this.neg().mul(multiplier.neg());
1136
+ else return this.neg().mul(multiplier).neg();
1137
+ } else if (multiplier.isNegative()) return this.mul(multiplier.neg()).neg();
1017
1138
 
1018
1139
  // If both longs are small, use float multiplication
1019
1140
  if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24))
@@ -1023,43 +1144,46 @@ LongPrototype.multiply = function multiply(multiplier) {
1023
1144
  // We can skip products that would overflow.
1024
1145
 
1025
1146
  var a48 = this.high >>> 16;
1026
- var a32 = this.high & 0xFFFF;
1147
+ var a32 = this.high & 0xffff;
1027
1148
  var a16 = this.low >>> 16;
1028
- var a00 = this.low & 0xFFFF;
1149
+ var a00 = this.low & 0xffff;
1029
1150
 
1030
1151
  var b48 = multiplier.high >>> 16;
1031
- var b32 = multiplier.high & 0xFFFF;
1152
+ var b32 = multiplier.high & 0xffff;
1032
1153
  var b16 = multiplier.low >>> 16;
1033
- var b00 = multiplier.low & 0xFFFF;
1154
+ var b00 = multiplier.low & 0xffff;
1034
1155
 
1035
- var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
1156
+ var c48 = 0,
1157
+ c32 = 0,
1158
+ c16 = 0,
1159
+ c00 = 0;
1036
1160
  c00 += a00 * b00;
1037
1161
  c16 += c00 >>> 16;
1038
- c00 &= 0xFFFF;
1162
+ c00 &= 0xffff;
1039
1163
  c16 += a16 * b00;
1040
1164
  c32 += c16 >>> 16;
1041
- c16 &= 0xFFFF;
1165
+ c16 &= 0xffff;
1042
1166
  c16 += a00 * b16;
1043
1167
  c32 += c16 >>> 16;
1044
- c16 &= 0xFFFF;
1168
+ c16 &= 0xffff;
1045
1169
  c32 += a32 * b00;
1046
1170
  c48 += c32 >>> 16;
1047
- c32 &= 0xFFFF;
1171
+ c32 &= 0xffff;
1048
1172
  c32 += a16 * b16;
1049
1173
  c48 += c32 >>> 16;
1050
- c32 &= 0xFFFF;
1174
+ c32 &= 0xffff;
1051
1175
  c32 += a00 * b32;
1052
1176
  c48 += c32 >>> 16;
1053
- c32 &= 0xFFFF;
1177
+ c32 &= 0xffff;
1054
1178
  c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
1055
- c48 &= 0xFFFF;
1179
+ c48 &= 0xffff;
1056
1180
  return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
1057
1181
  };
1058
1182
 
1059
1183
  /**
1060
1184
  * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}.
1061
1185
  * @function
1062
- * @param {!Long|number|string} multiplier Multiplier
1186
+ * @param {!Long|number|bigint|string} multiplier Multiplier
1063
1187
  * @returns {!Long} Product
1064
1188
  */
1065
1189
  LongPrototype.mul = LongPrototype.multiply;
@@ -1068,23 +1192,24 @@ LongPrototype.mul = LongPrototype.multiply;
1068
1192
  * Returns this Long divided by the specified. The result is signed if this Long is signed or
1069
1193
  * unsigned if this Long is unsigned.
1070
1194
  * @this {!Long}
1071
- * @param {!Long|number|string} divisor Divisor
1195
+ * @param {!Long|number|bigint|string} divisor Divisor
1072
1196
  * @returns {!Long} Quotient
1073
1197
  */
1074
1198
  LongPrototype.divide = function divide(divisor) {
1075
- if (!isLong(divisor))
1076
- divisor = fromValue(divisor);
1077
- if (divisor.isZero())
1078
- throw Error('division by zero');
1199
+ if (!isLong(divisor)) divisor = fromValue(divisor);
1200
+ if (divisor.isZero()) throw Error("division by zero");
1079
1201
 
1080
1202
  // use wasm support if present
1081
1203
  if (wasm) {
1082
1204
  // guard against signed division overflow: the largest
1083
1205
  // negative number / -1 would be 1 larger than the largest
1084
1206
  // positive number, due to two's complement.
1085
- if (!this.unsigned &&
1207
+ if (
1208
+ !this.unsigned &&
1086
1209
  this.high === -2147483648 &&
1087
- divisor.low === -1 && divisor.high === -1) {
1210
+ divisor.low === -1 &&
1211
+ divisor.high === -1
1212
+ ) {
1088
1213
  // be consistent with non-wasm code path
1089
1214
  return this;
1090
1215
  }
@@ -1092,22 +1217,20 @@ LongPrototype.divide = function divide(divisor) {
1092
1217
  this.low,
1093
1218
  this.high,
1094
1219
  divisor.low,
1095
- divisor.high
1220
+ divisor.high,
1096
1221
  );
1097
1222
  return fromBits(low, wasm["get_high"](), this.unsigned);
1098
1223
  }
1099
1224
 
1100
- if (this.isZero())
1101
- return this.unsigned ? UZERO : ZERO;
1225
+ if (this.isZero()) return this.unsigned ? UZERO : ZERO;
1102
1226
  var approx, rem, res;
1103
1227
  if (!this.unsigned) {
1104
1228
  // This section is only relevant for signed longs and is derived from the
1105
1229
  // closure library as a whole.
1106
1230
  if (this.eq(MIN_VALUE)) {
1107
1231
  if (divisor.eq(ONE) || divisor.eq(NEG_ONE))
1108
- return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
1109
- else if (divisor.eq(MIN_VALUE))
1110
- return ONE;
1232
+ return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
1233
+ else if (divisor.eq(MIN_VALUE)) return ONE;
1111
1234
  else {
1112
1235
  // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
1113
1236
  var halfThis = this.shr(1);
@@ -1120,23 +1243,19 @@ LongPrototype.divide = function divide(divisor) {
1120
1243
  return res;
1121
1244
  }
1122
1245
  }
1123
- } else if (divisor.eq(MIN_VALUE))
1124
- return this.unsigned ? UZERO : ZERO;
1246
+ } else if (divisor.eq(MIN_VALUE)) return this.unsigned ? UZERO : ZERO;
1125
1247
  if (this.isNegative()) {
1126
- if (divisor.isNegative())
1127
- return this.neg().div(divisor.neg());
1248
+ if (divisor.isNegative()) return this.neg().div(divisor.neg());
1128
1249
  return this.neg().div(divisor).neg();
1129
- } else if (divisor.isNegative())
1130
- return this.div(divisor.neg()).neg();
1250
+ } else if (divisor.isNegative()) return this.div(divisor.neg()).neg();
1131
1251
  res = ZERO;
1132
1252
  } else {
1133
1253
  // The algorithm below has not been made for unsigned longs. It's therefore
1134
1254
  // required to take special care of the MSB prior to running it.
1135
- if (!divisor.unsigned)
1136
- divisor = divisor.toUnsigned();
1137
- if (divisor.gt(this))
1138
- return UZERO;
1139
- if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true
1255
+ if (!divisor.unsigned) divisor = divisor.toUnsigned();
1256
+ if (divisor.gt(this)) return UZERO;
1257
+ if (divisor.gt(this.shru(1)))
1258
+ // 15 >>> 1 = 7 ; with divisor = 8 ; true
1140
1259
  return UONE;
1141
1260
  res = UZERO;
1142
1261
  }
@@ -1155,8 +1274,7 @@ LongPrototype.divide = function divide(divisor) {
1155
1274
  // We will tweak the approximate result by changing it in the 48-th digit or
1156
1275
  // the smallest non-fractional digit, whichever is larger.
1157
1276
  var log2 = Math.ceil(Math.log(approx) / Math.LN2),
1158
- delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48),
1159
-
1277
+ delta = log2 <= 48 ? 1 : pow_dbl(2, log2 - 48),
1160
1278
  // Decrease the approximation until it is smaller than the remainder. Note
1161
1279
  // that if it is too large, the product overflows and is negative.
1162
1280
  approxRes = fromNumber(approx),
@@ -1169,8 +1287,7 @@ LongPrototype.divide = function divide(divisor) {
1169
1287
 
1170
1288
  // We know the answer can't be zero... and actually, zero would cause
1171
1289
  // infinite recursion since we would make no progress.
1172
- if (approxRes.isZero())
1173
- approxRes = ONE;
1290
+ if (approxRes.isZero()) approxRes = ONE;
1174
1291
 
1175
1292
  res = res.add(approxRes);
1176
1293
  rem = rem.sub(approxRem);
@@ -1181,7 +1298,7 @@ LongPrototype.divide = function divide(divisor) {
1181
1298
  /**
1182
1299
  * Returns this Long divided by the specified. This is an alias of {@link Long#divide}.
1183
1300
  * @function
1184
- * @param {!Long|number|string} divisor Divisor
1301
+ * @param {!Long|number|bigint|string} divisor Divisor
1185
1302
  * @returns {!Long} Quotient
1186
1303
  */
1187
1304
  LongPrototype.div = LongPrototype.divide;
@@ -1189,12 +1306,11 @@ LongPrototype.div = LongPrototype.divide;
1189
1306
  /**
1190
1307
  * Returns this Long modulo the specified.
1191
1308
  * @this {!Long}
1192
- * @param {!Long|number|string} divisor Divisor
1309
+ * @param {!Long|number|bigint|string} divisor Divisor
1193
1310
  * @returns {!Long} Remainder
1194
1311
  */
1195
1312
  LongPrototype.modulo = function modulo(divisor) {
1196
- if (!isLong(divisor))
1197
- divisor = fromValue(divisor);
1313
+ if (!isLong(divisor)) divisor = fromValue(divisor);
1198
1314
 
1199
1315
  // use wasm support if present
1200
1316
  if (wasm) {
@@ -1202,7 +1318,7 @@ LongPrototype.modulo = function modulo(divisor) {
1202
1318
  this.low,
1203
1319
  this.high,
1204
1320
  divisor.low,
1205
- divisor.high
1321
+ divisor.high,
1206
1322
  );
1207
1323
  return fromBits(low, wasm["get_high"](), this.unsigned);
1208
1324
  }
@@ -1213,7 +1329,7 @@ LongPrototype.modulo = function modulo(divisor) {
1213
1329
  /**
1214
1330
  * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
1215
1331
  * @function
1216
- * @param {!Long|number|string} divisor Divisor
1332
+ * @param {!Long|number|bigint|string} divisor Divisor
1217
1333
  * @returns {!Long} Remainder
1218
1334
  */
1219
1335
  LongPrototype.mod = LongPrototype.modulo;
@@ -1221,7 +1337,7 @@ LongPrototype.mod = LongPrototype.modulo;
1221
1337
  /**
1222
1338
  * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
1223
1339
  * @function
1224
- * @param {!Long|number|string} divisor Divisor
1340
+ * @param {!Long|number|bigint|string} divisor Divisor
1225
1341
  * @returns {!Long} Remainder
1226
1342
  */
1227
1343
  LongPrototype.rem = LongPrototype.modulo;
@@ -1272,36 +1388,33 @@ LongPrototype.ctz = LongPrototype.countTrailingZeros;
1272
1388
  /**
1273
1389
  * Returns the bitwise AND of this Long and the specified.
1274
1390
  * @this {!Long}
1275
- * @param {!Long|number|string} other Other Long
1391
+ * @param {!Long|number|bigint|string} other Other Long
1276
1392
  * @returns {!Long}
1277
1393
  */
1278
1394
  LongPrototype.and = function and(other) {
1279
- if (!isLong(other))
1280
- other = fromValue(other);
1395
+ if (!isLong(other)) other = fromValue(other);
1281
1396
  return fromBits(this.low & other.low, this.high & other.high, this.unsigned);
1282
1397
  };
1283
1398
 
1284
1399
  /**
1285
1400
  * Returns the bitwise OR of this Long and the specified.
1286
1401
  * @this {!Long}
1287
- * @param {!Long|number|string} other Other Long
1402
+ * @param {!Long|number|bigint|string} other Other Long
1288
1403
  * @returns {!Long}
1289
1404
  */
1290
1405
  LongPrototype.or = function or(other) {
1291
- if (!isLong(other))
1292
- other = fromValue(other);
1406
+ if (!isLong(other)) other = fromValue(other);
1293
1407
  return fromBits(this.low | other.low, this.high | other.high, this.unsigned);
1294
1408
  };
1295
1409
 
1296
1410
  /**
1297
1411
  * Returns the bitwise XOR of this Long and the given one.
1298
1412
  * @this {!Long}
1299
- * @param {!Long|number|string} other Other Long
1413
+ * @param {!Long|number|bigint|string} other Other Long
1300
1414
  * @returns {!Long}
1301
1415
  */
1302
1416
  LongPrototype.xor = function xor(other) {
1303
- if (!isLong(other))
1304
- other = fromValue(other);
1417
+ if (!isLong(other)) other = fromValue(other);
1305
1418
  return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
1306
1419
  };
1307
1420
 
@@ -1312,14 +1425,15 @@ LongPrototype.xor = function xor(other) {
1312
1425
  * @returns {!Long} Shifted Long
1313
1426
  */
1314
1427
  LongPrototype.shiftLeft = function shiftLeft(numBits) {
1315
- if (isLong(numBits))
1316
- numBits = numBits.toInt();
1317
- if ((numBits &= 63) === 0)
1318
- return this;
1428
+ if (isLong(numBits)) numBits = numBits.toInt();
1429
+ if ((numBits &= 63) === 0) return this;
1319
1430
  else if (numBits < 32)
1320
- return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned);
1321
- else
1322
- return fromBits(0, this.low << (numBits - 32), this.unsigned);
1431
+ return fromBits(
1432
+ this.low << numBits,
1433
+ (this.high << numBits) | (this.low >>> (32 - numBits)),
1434
+ this.unsigned,
1435
+ );
1436
+ else return fromBits(0, this.low << (numBits - 32), this.unsigned);
1323
1437
  };
1324
1438
 
1325
1439
  /**
@@ -1337,14 +1451,20 @@ LongPrototype.shl = LongPrototype.shiftLeft;
1337
1451
  * @returns {!Long} Shifted Long
1338
1452
  */
1339
1453
  LongPrototype.shiftRight = function shiftRight(numBits) {
1340
- if (isLong(numBits))
1341
- numBits = numBits.toInt();
1342
- if ((numBits &= 63) === 0)
1343
- return this;
1454
+ if (isLong(numBits)) numBits = numBits.toInt();
1455
+ if ((numBits &= 63) === 0) return this;
1344
1456
  else if (numBits < 32)
1345
- return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned);
1457
+ return fromBits(
1458
+ (this.low >>> numBits) | (this.high << (32 - numBits)),
1459
+ this.high >> numBits,
1460
+ this.unsigned,
1461
+ );
1346
1462
  else
1347
- return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
1463
+ return fromBits(
1464
+ this.high >> (numBits - 32),
1465
+ this.high >= 0 ? 0 : -1,
1466
+ this.unsigned,
1467
+ );
1348
1468
  };
1349
1469
 
1350
1470
  /**
@@ -1364,7 +1484,12 @@ LongPrototype.shr = LongPrototype.shiftRight;
1364
1484
  LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) {
1365
1485
  if (isLong(numBits)) numBits = numBits.toInt();
1366
1486
  if ((numBits &= 63) === 0) return this;
1367
- if (numBits < 32) return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >>> numBits, this.unsigned);
1487
+ if (numBits < 32)
1488
+ return fromBits(
1489
+ (this.low >>> numBits) | (this.high << (32 - numBits)),
1490
+ this.high >>> numBits,
1491
+ this.unsigned,
1492
+ );
1368
1493
  if (numBits === 32) return fromBits(this.high, 0, this.unsigned);
1369
1494
  return fromBits(this.high >>> (numBits - 32), 0, this.unsigned);
1370
1495
  };
@@ -1397,12 +1522,20 @@ LongPrototype.rotateLeft = function rotateLeft(numBits) {
1397
1522
  if ((numBits &= 63) === 0) return this;
1398
1523
  if (numBits === 32) return fromBits(this.high, this.low, this.unsigned);
1399
1524
  if (numBits < 32) {
1400
- b = (32 - numBits);
1401
- return fromBits(((this.low << numBits) | (this.high >>> b)), ((this.high << numBits) | (this.low >>> b)), this.unsigned);
1525
+ b = 32 - numBits;
1526
+ return fromBits(
1527
+ (this.low << numBits) | (this.high >>> b),
1528
+ (this.high << numBits) | (this.low >>> b),
1529
+ this.unsigned,
1530
+ );
1402
1531
  }
1403
1532
  numBits -= 32;
1404
- b = (32 - numBits);
1405
- return fromBits(((this.high << numBits) | (this.low >>> b)), ((this.low << numBits) | (this.high >>> b)), this.unsigned);
1533
+ b = 32 - numBits;
1534
+ return fromBits(
1535
+ (this.high << numBits) | (this.low >>> b),
1536
+ (this.low << numBits) | (this.high >>> b),
1537
+ this.unsigned,
1538
+ );
1406
1539
  };
1407
1540
  /**
1408
1541
  * Returns this Long with bits rotated to the left by the given amount. This is an alias of {@link Long#rotateLeft}.
@@ -1424,12 +1557,20 @@ LongPrototype.rotateRight = function rotateRight(numBits) {
1424
1557
  if ((numBits &= 63) === 0) return this;
1425
1558
  if (numBits === 32) return fromBits(this.high, this.low, this.unsigned);
1426
1559
  if (numBits < 32) {
1427
- b = (32 - numBits);
1428
- return fromBits(((this.high << b) | (this.low >>> numBits)), ((this.low << b) | (this.high >>> numBits)), this.unsigned);
1560
+ b = 32 - numBits;
1561
+ return fromBits(
1562
+ (this.high << b) | (this.low >>> numBits),
1563
+ (this.low << b) | (this.high >>> numBits),
1564
+ this.unsigned,
1565
+ );
1429
1566
  }
1430
1567
  numBits -= 32;
1431
- b = (32 - numBits);
1432
- return fromBits(((this.low << b) | (this.high >>> numBits)), ((this.high << b) | (this.low >>> numBits)), this.unsigned);
1568
+ b = 32 - numBits;
1569
+ return fromBits(
1570
+ (this.low << b) | (this.high >>> numBits),
1571
+ (this.high << b) | (this.low >>> numBits),
1572
+ this.unsigned,
1573
+ );
1433
1574
  };
1434
1575
  /**
1435
1576
  * Returns this Long with bits rotated to the right by the given amount. This is an alias of {@link Long#rotateRight}.
@@ -1445,8 +1586,7 @@ LongPrototype.rotr = LongPrototype.rotateRight;
1445
1586
  * @returns {!Long} Signed long
1446
1587
  */
1447
1588
  LongPrototype.toSigned = function toSigned() {
1448
- if (!this.unsigned)
1449
- return this;
1589
+ if (!this.unsigned) return this;
1450
1590
  return fromBits(this.low, this.high, false);
1451
1591
  };
1452
1592
 
@@ -1456,8 +1596,7 @@ LongPrototype.toSigned = function toSigned() {
1456
1596
  * @returns {!Long} Unsigned long
1457
1597
  */
1458
1598
  LongPrototype.toUnsigned = function toUnsigned() {
1459
- if (this.unsigned)
1460
- return this;
1599
+ if (this.unsigned) return this;
1461
1600
  return fromBits(this.low, this.high, true);
1462
1601
  };
1463
1602
 
@@ -1481,13 +1620,13 @@ LongPrototype.toBytesLE = function toBytesLE() {
1481
1620
  lo = this.low;
1482
1621
  return [
1483
1622
  lo & 0xff,
1484
- lo >>> 8 & 0xff,
1485
- lo >>> 16 & 0xff,
1623
+ (lo >>> 8) & 0xff,
1624
+ (lo >>> 16) & 0xff,
1486
1625
  lo >>> 24,
1487
1626
  hi & 0xff,
1488
- hi >>> 8 & 0xff,
1489
- hi >>> 16 & 0xff,
1490
- hi >>> 24
1627
+ (hi >>> 8) & 0xff,
1628
+ (hi >>> 16) & 0xff,
1629
+ hi >>> 24,
1491
1630
  ];
1492
1631
  };
1493
1632
 
@@ -1501,13 +1640,13 @@ LongPrototype.toBytesBE = function toBytesBE() {
1501
1640
  lo = this.low;
1502
1641
  return [
1503
1642
  hi >>> 24,
1504
- hi >>> 16 & 0xff,
1505
- hi >>> 8 & 0xff,
1643
+ (hi >>> 16) & 0xff,
1644
+ (hi >>> 8) & 0xff,
1506
1645
  hi & 0xff,
1507
1646
  lo >>> 24,
1508
- lo >>> 16 & 0xff,
1509
- lo >>> 8 & 0xff,
1510
- lo & 0xff
1647
+ (lo >>> 16) & 0xff,
1648
+ (lo >>> 8) & 0xff,
1649
+ lo & 0xff,
1511
1650
  ];
1512
1651
  };
1513
1652
 
@@ -1519,7 +1658,9 @@ LongPrototype.toBytesBE = function toBytesBE() {
1519
1658
  * @returns {Long} The corresponding Long value
1520
1659
  */
1521
1660
  Long.fromBytes = function fromBytes(bytes, unsigned, le) {
1522
- return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned);
1661
+ return le
1662
+ ? Long.fromBytesLE(bytes, unsigned)
1663
+ : Long.fromBytesBE(bytes, unsigned);
1523
1664
  };
1524
1665
 
1525
1666
  /**
@@ -1530,15 +1671,9 @@ Long.fromBytes = function fromBytes(bytes, unsigned, le) {
1530
1671
  */
1531
1672
  Long.fromBytesLE = function fromBytesLE(bytes, unsigned) {
1532
1673
  return new Long(
1533
- bytes[0] |
1534
- bytes[1] << 8 |
1535
- bytes[2] << 16 |
1536
- bytes[3] << 24,
1537
- bytes[4] |
1538
- bytes[5] << 8 |
1539
- bytes[6] << 16 |
1540
- bytes[7] << 24,
1541
- unsigned
1674
+ bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24),
1675
+ bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24),
1676
+ unsigned,
1542
1677
  );
1543
1678
  };
1544
1679
 
@@ -1550,18 +1685,45 @@ Long.fromBytesLE = function fromBytesLE(bytes, unsigned) {
1550
1685
  */
1551
1686
  Long.fromBytesBE = function fromBytesBE(bytes, unsigned) {
1552
1687
  return new Long(
1553
- bytes[4] << 24 |
1554
- bytes[5] << 16 |
1555
- bytes[6] << 8 |
1556
- bytes[7],
1557
- bytes[0] << 24 |
1558
- bytes[1] << 16 |
1559
- bytes[2] << 8 |
1560
- bytes[3],
1561
- unsigned
1688
+ (bytes[4] << 24) | (bytes[5] << 16) | (bytes[6] << 8) | bytes[7],
1689
+ (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3],
1690
+ unsigned,
1562
1691
  );
1563
1692
  };
1564
1693
 
1694
+ // Support conversion to/from BigInt where available
1695
+ if (typeof BigInt === "function") {
1696
+ /**
1697
+ * Returns a Long representing the given big integer.
1698
+ * @function
1699
+ * @param {number} value The big integer value
1700
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
1701
+ * @returns {!Long} The corresponding Long value
1702
+ */
1703
+ Long.fromBigInt = function fromBigInt(value, unsigned) {
1704
+ var lowBits = Number(BigInt.asIntN(32, value));
1705
+ var highBits = Number(BigInt.asIntN(32, value >> BigInt(32)));
1706
+ return fromBits(lowBits, highBits, unsigned);
1707
+ };
1708
+
1709
+ // Override
1710
+ Long.fromValue = function fromValueWithBigInt(value, unsigned) {
1711
+ if (typeof value === "bigint") return fromBigInt(value, unsigned);
1712
+ return fromValue(value, unsigned);
1713
+ };
1714
+
1715
+ /**
1716
+ * Converts the Long to its big integer representation.
1717
+ * @this {!Long}
1718
+ * @returns {bigint}
1719
+ */
1720
+ LongPrototype.toBigInt = function toBigInt() {
1721
+ var lowBigInt = BigInt(this.low >>> 0);
1722
+ var highBigInt = BigInt(this.unsigned ? this.high >>> 0 : this.high);
1723
+ return (highBigInt << BigInt(32)) | lowBigInt;
1724
+ };
1725
+ }
1726
+
1565
1727
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
1566
1728
 
1567
1729
  function getDefaultExportFromCjs (x) {
@@ -4397,12 +4559,12 @@ const Struct = {
4397
4559
  return obj;
4398
4560
  },
4399
4561
  create(base) {
4400
- return Struct.fromPartial(base !== null && base !== undefined ? base : {});
4562
+ return Struct.fromPartial(base !== null && base !== void 0 ? base : {});
4401
4563
  },
4402
4564
  fromPartial(object) {
4403
4565
  var _a;
4404
4566
  const message = createBaseStruct();
4405
- message.fields = Object.entries((_a = object.fields) !== null && _a !== undefined ? _a : {}).reduce((acc, [key, value]) => {
4567
+ message.fields = Object.entries((_a = object.fields) !== null && _a !== void 0 ? _a : {}).reduce((acc, [key, value]) => {
4406
4568
  if (value !== undefined) {
4407
4569
  acc[key] = value;
4408
4570
  }
@@ -4470,7 +4632,7 @@ const Struct_FieldsEntry = {
4470
4632
  return message;
4471
4633
  },
4472
4634
  fromJSON(object) {
4473
- return { key: isSet$2(object.key) ? String(object.key) : "", value: isSet$2(object === null || object === undefined ? undefined : object.value) ? object.value : undefined };
4635
+ return { key: isSet$2(object.key) ? String(object.key) : "", value: isSet$2(object === null || object === void 0 ? void 0 : object.value) ? object.value : undefined };
4474
4636
  },
4475
4637
  toJSON(message) {
4476
4638
  const obj = {};
@@ -4483,13 +4645,13 @@ const Struct_FieldsEntry = {
4483
4645
  return obj;
4484
4646
  },
4485
4647
  create(base) {
4486
- return Struct_FieldsEntry.fromPartial(base !== null && base !== undefined ? base : {});
4648
+ return Struct_FieldsEntry.fromPartial(base !== null && base !== void 0 ? base : {});
4487
4649
  },
4488
4650
  fromPartial(object) {
4489
4651
  var _a, _b;
4490
4652
  const message = createBaseStruct_FieldsEntry();
4491
- message.key = (_a = object.key) !== null && _a !== undefined ? _a : "";
4492
- message.value = (_b = object.value) !== null && _b !== undefined ? _b : undefined;
4653
+ message.key = (_a = object.key) !== null && _a !== void 0 ? _a : "";
4654
+ message.value = (_b = object.value) !== null && _b !== void 0 ? _b : undefined;
4493
4655
  return message;
4494
4656
  },
4495
4657
  };
@@ -4609,17 +4771,17 @@ const Value = {
4609
4771
  return obj;
4610
4772
  },
4611
4773
  create(base) {
4612
- return Value.fromPartial(base !== null && base !== undefined ? base : {});
4774
+ return Value.fromPartial(base !== null && base !== void 0 ? base : {});
4613
4775
  },
4614
4776
  fromPartial(object) {
4615
4777
  var _a, _b, _c, _d, _e, _f;
4616
4778
  const message = createBaseValue();
4617
- message.nullValue = (_a = object.nullValue) !== null && _a !== undefined ? _a : undefined;
4618
- message.numberValue = (_b = object.numberValue) !== null && _b !== undefined ? _b : undefined;
4619
- message.stringValue = (_c = object.stringValue) !== null && _c !== undefined ? _c : undefined;
4620
- message.boolValue = (_d = object.boolValue) !== null && _d !== undefined ? _d : undefined;
4621
- message.structValue = (_e = object.structValue) !== null && _e !== undefined ? _e : undefined;
4622
- message.listValue = (_f = object.listValue) !== null && _f !== undefined ? _f : undefined;
4779
+ message.nullValue = (_a = object.nullValue) !== null && _a !== void 0 ? _a : undefined;
4780
+ message.numberValue = (_b = object.numberValue) !== null && _b !== void 0 ? _b : undefined;
4781
+ message.stringValue = (_c = object.stringValue) !== null && _c !== void 0 ? _c : undefined;
4782
+ message.boolValue = (_d = object.boolValue) !== null && _d !== void 0 ? _d : undefined;
4783
+ message.structValue = (_e = object.structValue) !== null && _e !== void 0 ? _e : undefined;
4784
+ message.listValue = (_f = object.listValue) !== null && _f !== void 0 ? _f : undefined;
4623
4785
  return message;
4624
4786
  },
4625
4787
  wrap(value) {
@@ -4651,19 +4813,19 @@ const Value = {
4651
4813
  if (message.stringValue !== undefined) {
4652
4814
  return message.stringValue;
4653
4815
  }
4654
- else if ((message === null || message === undefined ? undefined : message.numberValue) !== undefined) {
4816
+ else if ((message === null || message === void 0 ? void 0 : message.numberValue) !== undefined) {
4655
4817
  return message.numberValue;
4656
4818
  }
4657
- else if ((message === null || message === undefined ? undefined : message.boolValue) !== undefined) {
4819
+ else if ((message === null || message === void 0 ? void 0 : message.boolValue) !== undefined) {
4658
4820
  return message.boolValue;
4659
4821
  }
4660
- else if ((message === null || message === undefined ? undefined : message.structValue) !== undefined) {
4822
+ else if ((message === null || message === void 0 ? void 0 : message.structValue) !== undefined) {
4661
4823
  return message.structValue;
4662
4824
  }
4663
- else if ((message === null || message === undefined ? undefined : message.listValue) !== undefined) {
4825
+ else if ((message === null || message === void 0 ? void 0 : message.listValue) !== undefined) {
4664
4826
  return message.listValue;
4665
4827
  }
4666
- else if ((message === null || message === undefined ? undefined : message.nullValue) !== undefined) {
4828
+ else if ((message === null || message === void 0 ? void 0 : message.nullValue) !== undefined) {
4667
4829
  return null;
4668
4830
  }
4669
4831
  return undefined;
@@ -4701,32 +4863,32 @@ const ListValue = {
4701
4863
  return message;
4702
4864
  },
4703
4865
  fromJSON(object) {
4704
- return { values: Array.isArray(object === null || object === undefined ? undefined : object.values) ? [...object.values] : [] };
4866
+ return { values: Array.isArray(object === null || object === void 0 ? void 0 : object.values) ? [...object.values] : [] };
4705
4867
  },
4706
4868
  toJSON(message) {
4707
4869
  var _a;
4708
4870
  const obj = {};
4709
- if ((_a = message.values) === null || _a === undefined ? undefined : _a.length) {
4871
+ if ((_a = message.values) === null || _a === void 0 ? void 0 : _a.length) {
4710
4872
  obj.values = message.values;
4711
4873
  }
4712
4874
  return obj;
4713
4875
  },
4714
4876
  create(base) {
4715
- return ListValue.fromPartial(base !== null && base !== undefined ? base : {});
4877
+ return ListValue.fromPartial(base !== null && base !== void 0 ? base : {});
4716
4878
  },
4717
4879
  fromPartial(object) {
4718
4880
  var _a;
4719
4881
  const message = createBaseListValue();
4720
- message.values = ((_a = object.values) === null || _a === undefined ? undefined : _a.map((e) => e)) || [];
4882
+ message.values = ((_a = object.values) === null || _a === void 0 ? void 0 : _a.map((e) => e)) || [];
4721
4883
  return message;
4722
4884
  },
4723
4885
  wrap(array) {
4724
4886
  const result = createBaseListValue();
4725
- result.values = array !== null && array !== undefined ? array : [];
4887
+ result.values = array !== null && array !== void 0 ? array : [];
4726
4888
  return result;
4727
4889
  },
4728
4890
  unwrap(message) {
4729
- if ((message === null || message === undefined ? undefined : message.hasOwnProperty("values")) && Array.isArray(message.values)) {
4891
+ if ((message === null || message === void 0 ? void 0 : message.hasOwnProperty("values")) && Array.isArray(message.values)) {
4730
4892
  return message.values;
4731
4893
  }
4732
4894
  else {
@@ -4742,357 +4904,6 @@ function isSet$2(value) {
4742
4904
  }
4743
4905
 
4744
4906
  /* eslint-disable */
4745
- function createBaseResolveAllRequest() {
4746
- return { context: undefined };
4747
- }
4748
- const ResolveAllRequest = {
4749
- encode(message, writer = _m0.Writer.create()) {
4750
- if (message.context !== undefined) {
4751
- Struct.encode(Struct.wrap(message.context), writer.uint32(10).fork()).ldelim();
4752
- }
4753
- return writer;
4754
- },
4755
- decode(input, length) {
4756
- const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
4757
- let end = length === undefined ? reader.len : reader.pos + length;
4758
- const message = createBaseResolveAllRequest();
4759
- while (reader.pos < end) {
4760
- const tag = reader.uint32();
4761
- switch (tag >>> 3) {
4762
- case 1:
4763
- if (tag !== 10) {
4764
- break;
4765
- }
4766
- message.context = Struct.unwrap(Struct.decode(reader, reader.uint32()));
4767
- continue;
4768
- }
4769
- if ((tag & 7) === 4 || tag === 0) {
4770
- break;
4771
- }
4772
- reader.skipType(tag & 7);
4773
- }
4774
- return message;
4775
- },
4776
- fromJSON(object) {
4777
- return { context: isObject$1(object.context) ? object.context : undefined };
4778
- },
4779
- toJSON(message) {
4780
- const obj = {};
4781
- if (message.context !== undefined) {
4782
- obj.context = message.context;
4783
- }
4784
- return obj;
4785
- },
4786
- create(base) {
4787
- return ResolveAllRequest.fromPartial(base !== null && base !== undefined ? base : {});
4788
- },
4789
- fromPartial(object) {
4790
- var _a;
4791
- const message = createBaseResolveAllRequest();
4792
- message.context = (_a = object.context) !== null && _a !== undefined ? _a : undefined;
4793
- return message;
4794
- },
4795
- };
4796
- function createBaseResolveAllResponse() {
4797
- return { flags: {}, metadata: undefined };
4798
- }
4799
- const ResolveAllResponse = {
4800
- encode(message, writer = _m0.Writer.create()) {
4801
- Object.entries(message.flags).forEach(([key, value]) => {
4802
- ResolveAllResponse_FlagsEntry.encode({ key: key, value }, writer.uint32(10).fork()).ldelim();
4803
- });
4804
- if (message.metadata !== undefined) {
4805
- Struct.encode(Struct.wrap(message.metadata), writer.uint32(18).fork()).ldelim();
4806
- }
4807
- return writer;
4808
- },
4809
- decode(input, length) {
4810
- const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
4811
- let end = length === undefined ? reader.len : reader.pos + length;
4812
- const message = createBaseResolveAllResponse();
4813
- while (reader.pos < end) {
4814
- const tag = reader.uint32();
4815
- switch (tag >>> 3) {
4816
- case 1:
4817
- if (tag !== 10) {
4818
- break;
4819
- }
4820
- const entry1 = ResolveAllResponse_FlagsEntry.decode(reader, reader.uint32());
4821
- if (entry1.value !== undefined) {
4822
- message.flags[entry1.key] = entry1.value;
4823
- }
4824
- continue;
4825
- case 2:
4826
- if (tag !== 18) {
4827
- break;
4828
- }
4829
- message.metadata = Struct.unwrap(Struct.decode(reader, reader.uint32()));
4830
- continue;
4831
- }
4832
- if ((tag & 7) === 4 || tag === 0) {
4833
- break;
4834
- }
4835
- reader.skipType(tag & 7);
4836
- }
4837
- return message;
4838
- },
4839
- fromJSON(object) {
4840
- return {
4841
- flags: isObject$1(object.flags)
4842
- ? Object.entries(object.flags).reduce((acc, [key, value]) => {
4843
- acc[key] = AnyFlag.fromJSON(value);
4844
- return acc;
4845
- }, {})
4846
- : {},
4847
- metadata: isObject$1(object.metadata) ? object.metadata : undefined,
4848
- };
4849
- },
4850
- toJSON(message) {
4851
- const obj = {};
4852
- if (message.flags) {
4853
- const entries = Object.entries(message.flags);
4854
- if (entries.length > 0) {
4855
- obj.flags = {};
4856
- entries.forEach(([k, v]) => {
4857
- obj.flags[k] = AnyFlag.toJSON(v);
4858
- });
4859
- }
4860
- }
4861
- if (message.metadata !== undefined) {
4862
- obj.metadata = message.metadata;
4863
- }
4864
- return obj;
4865
- },
4866
- create(base) {
4867
- return ResolveAllResponse.fromPartial(base !== null && base !== undefined ? base : {});
4868
- },
4869
- fromPartial(object) {
4870
- var _a, _b;
4871
- const message = createBaseResolveAllResponse();
4872
- message.flags = Object.entries((_a = object.flags) !== null && _a !== undefined ? _a : {}).reduce((acc, [key, value]) => {
4873
- if (value !== undefined) {
4874
- acc[key] = AnyFlag.fromPartial(value);
4875
- }
4876
- return acc;
4877
- }, {});
4878
- message.metadata = (_b = object.metadata) !== null && _b !== undefined ? _b : undefined;
4879
- return message;
4880
- },
4881
- };
4882
- function createBaseResolveAllResponse_FlagsEntry() {
4883
- return { key: "", value: undefined };
4884
- }
4885
- const ResolveAllResponse_FlagsEntry = {
4886
- encode(message, writer = _m0.Writer.create()) {
4887
- if (message.key !== "") {
4888
- writer.uint32(10).string(message.key);
4889
- }
4890
- if (message.value !== undefined) {
4891
- AnyFlag.encode(message.value, writer.uint32(18).fork()).ldelim();
4892
- }
4893
- return writer;
4894
- },
4895
- decode(input, length) {
4896
- const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
4897
- let end = length === undefined ? reader.len : reader.pos + length;
4898
- const message = createBaseResolveAllResponse_FlagsEntry();
4899
- while (reader.pos < end) {
4900
- const tag = reader.uint32();
4901
- switch (tag >>> 3) {
4902
- case 1:
4903
- if (tag !== 10) {
4904
- break;
4905
- }
4906
- message.key = reader.string();
4907
- continue;
4908
- case 2:
4909
- if (tag !== 18) {
4910
- break;
4911
- }
4912
- message.value = AnyFlag.decode(reader, reader.uint32());
4913
- continue;
4914
- }
4915
- if ((tag & 7) === 4 || tag === 0) {
4916
- break;
4917
- }
4918
- reader.skipType(tag & 7);
4919
- }
4920
- return message;
4921
- },
4922
- fromJSON(object) {
4923
- return {
4924
- key: isSet$1(object.key) ? String(object.key) : "",
4925
- value: isSet$1(object.value) ? AnyFlag.fromJSON(object.value) : undefined,
4926
- };
4927
- },
4928
- toJSON(message) {
4929
- const obj = {};
4930
- if (message.key !== "") {
4931
- obj.key = message.key;
4932
- }
4933
- if (message.value !== undefined) {
4934
- obj.value = AnyFlag.toJSON(message.value);
4935
- }
4936
- return obj;
4937
- },
4938
- create(base) {
4939
- return ResolveAllResponse_FlagsEntry.fromPartial(base !== null && base !== undefined ? base : {});
4940
- },
4941
- fromPartial(object) {
4942
- var _a;
4943
- const message = createBaseResolveAllResponse_FlagsEntry();
4944
- message.key = (_a = object.key) !== null && _a !== undefined ? _a : "";
4945
- message.value = (object.value !== undefined && object.value !== null)
4946
- ? AnyFlag.fromPartial(object.value)
4947
- : undefined;
4948
- return message;
4949
- },
4950
- };
4951
- function createBaseAnyFlag() {
4952
- return {
4953
- reason: "",
4954
- variant: "",
4955
- boolValue: undefined,
4956
- stringValue: undefined,
4957
- doubleValue: undefined,
4958
- objectValue: undefined,
4959
- metadata: undefined,
4960
- };
4961
- }
4962
- const AnyFlag = {
4963
- encode(message, writer = _m0.Writer.create()) {
4964
- if (message.reason !== "") {
4965
- writer.uint32(10).string(message.reason);
4966
- }
4967
- if (message.variant !== "") {
4968
- writer.uint32(18).string(message.variant);
4969
- }
4970
- if (message.boolValue !== undefined) {
4971
- writer.uint32(24).bool(message.boolValue);
4972
- }
4973
- if (message.stringValue !== undefined) {
4974
- writer.uint32(34).string(message.stringValue);
4975
- }
4976
- if (message.doubleValue !== undefined) {
4977
- writer.uint32(41).double(message.doubleValue);
4978
- }
4979
- if (message.objectValue !== undefined) {
4980
- Struct.encode(Struct.wrap(message.objectValue), writer.uint32(50).fork()).ldelim();
4981
- }
4982
- if (message.metadata !== undefined) {
4983
- Struct.encode(Struct.wrap(message.metadata), writer.uint32(58).fork()).ldelim();
4984
- }
4985
- return writer;
4986
- },
4987
- decode(input, length) {
4988
- const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
4989
- let end = length === undefined ? reader.len : reader.pos + length;
4990
- const message = createBaseAnyFlag();
4991
- while (reader.pos < end) {
4992
- const tag = reader.uint32();
4993
- switch (tag >>> 3) {
4994
- case 1:
4995
- if (tag !== 10) {
4996
- break;
4997
- }
4998
- message.reason = reader.string();
4999
- continue;
5000
- case 2:
5001
- if (tag !== 18) {
5002
- break;
5003
- }
5004
- message.variant = reader.string();
5005
- continue;
5006
- case 3:
5007
- if (tag !== 24) {
5008
- break;
5009
- }
5010
- message.boolValue = reader.bool();
5011
- continue;
5012
- case 4:
5013
- if (tag !== 34) {
5014
- break;
5015
- }
5016
- message.stringValue = reader.string();
5017
- continue;
5018
- case 5:
5019
- if (tag !== 41) {
5020
- break;
5021
- }
5022
- message.doubleValue = reader.double();
5023
- continue;
5024
- case 6:
5025
- if (tag !== 50) {
5026
- break;
5027
- }
5028
- message.objectValue = Struct.unwrap(Struct.decode(reader, reader.uint32()));
5029
- continue;
5030
- case 7:
5031
- if (tag !== 58) {
5032
- break;
5033
- }
5034
- message.metadata = Struct.unwrap(Struct.decode(reader, reader.uint32()));
5035
- continue;
5036
- }
5037
- if ((tag & 7) === 4 || tag === 0) {
5038
- break;
5039
- }
5040
- reader.skipType(tag & 7);
5041
- }
5042
- return message;
5043
- },
5044
- fromJSON(object) {
5045
- return {
5046
- reason: isSet$1(object.reason) ? String(object.reason) : "",
5047
- variant: isSet$1(object.variant) ? String(object.variant) : "",
5048
- boolValue: isSet$1(object.boolValue) ? Boolean(object.boolValue) : undefined,
5049
- stringValue: isSet$1(object.stringValue) ? String(object.stringValue) : undefined,
5050
- doubleValue: isSet$1(object.doubleValue) ? Number(object.doubleValue) : undefined,
5051
- objectValue: isObject$1(object.objectValue) ? object.objectValue : undefined,
5052
- metadata: isObject$1(object.metadata) ? object.metadata : undefined,
5053
- };
5054
- },
5055
- toJSON(message) {
5056
- const obj = {};
5057
- if (message.reason !== "") {
5058
- obj.reason = message.reason;
5059
- }
5060
- if (message.variant !== "") {
5061
- obj.variant = message.variant;
5062
- }
5063
- if (message.boolValue !== undefined) {
5064
- obj.boolValue = message.boolValue;
5065
- }
5066
- if (message.stringValue !== undefined) {
5067
- obj.stringValue = message.stringValue;
5068
- }
5069
- if (message.doubleValue !== undefined) {
5070
- obj.doubleValue = message.doubleValue;
5071
- }
5072
- if (message.objectValue !== undefined) {
5073
- obj.objectValue = message.objectValue;
5074
- }
5075
- if (message.metadata !== undefined) {
5076
- obj.metadata = message.metadata;
5077
- }
5078
- return obj;
5079
- },
5080
- create(base) {
5081
- return AnyFlag.fromPartial(base !== null && base !== undefined ? base : {});
5082
- },
5083
- fromPartial(object) {
5084
- var _a, _b, _c, _d, _e, _f, _g;
5085
- const message = createBaseAnyFlag();
5086
- message.reason = (_a = object.reason) !== null && _a !== undefined ? _a : "";
5087
- message.variant = (_b = object.variant) !== null && _b !== undefined ? _b : "";
5088
- message.boolValue = (_c = object.boolValue) !== null && _c !== undefined ? _c : undefined;
5089
- message.stringValue = (_d = object.stringValue) !== null && _d !== undefined ? _d : undefined;
5090
- message.doubleValue = (_e = object.doubleValue) !== null && _e !== undefined ? _e : undefined;
5091
- message.objectValue = (_f = object.objectValue) !== null && _f !== undefined ? _f : undefined;
5092
- message.metadata = (_g = object.metadata) !== null && _g !== undefined ? _g : undefined;
5093
- return message;
5094
- },
5095
- };
5096
4907
  function createBaseResolveBooleanRequest() {
5097
4908
  return { flagKey: "", context: undefined };
5098
4909
  }
@@ -5150,28 +4961,28 @@ const ResolveBooleanRequest = {
5150
4961
  return obj;
5151
4962
  },
5152
4963
  create(base) {
5153
- return ResolveBooleanRequest.fromPartial(base !== null && base !== undefined ? base : {});
4964
+ return ResolveBooleanRequest.fromPartial(base !== null && base !== void 0 ? base : {});
5154
4965
  },
5155
4966
  fromPartial(object) {
5156
4967
  var _a, _b;
5157
4968
  const message = createBaseResolveBooleanRequest();
5158
- message.flagKey = (_a = object.flagKey) !== null && _a !== undefined ? _a : "";
5159
- message.context = (_b = object.context) !== null && _b !== undefined ? _b : undefined;
4969
+ message.flagKey = (_a = object.flagKey) !== null && _a !== void 0 ? _a : "";
4970
+ message.context = (_b = object.context) !== null && _b !== void 0 ? _b : undefined;
5160
4971
  return message;
5161
4972
  },
5162
4973
  };
5163
4974
  function createBaseResolveBooleanResponse() {
5164
- return { value: false, reason: "", variant: "", metadata: undefined };
4975
+ return { value: undefined, reason: "", variant: undefined, metadata: undefined };
5165
4976
  }
5166
4977
  const ResolveBooleanResponse = {
5167
4978
  encode(message, writer = _m0.Writer.create()) {
5168
- if (message.value === true) {
4979
+ if (message.value !== undefined) {
5169
4980
  writer.uint32(8).bool(message.value);
5170
4981
  }
5171
4982
  if (message.reason !== "") {
5172
4983
  writer.uint32(18).string(message.reason);
5173
4984
  }
5174
- if (message.variant !== "") {
4985
+ if (message.variant !== undefined) {
5175
4986
  writer.uint32(26).string(message.variant);
5176
4987
  }
5177
4988
  if (message.metadata !== undefined) {
@@ -5220,21 +5031,21 @@ const ResolveBooleanResponse = {
5220
5031
  },
5221
5032
  fromJSON(object) {
5222
5033
  return {
5223
- value: isSet$1(object.value) ? Boolean(object.value) : false,
5034
+ value: isSet$1(object.value) ? Boolean(object.value) : undefined,
5224
5035
  reason: isSet$1(object.reason) ? String(object.reason) : "",
5225
- variant: isSet$1(object.variant) ? String(object.variant) : "",
5036
+ variant: isSet$1(object.variant) ? String(object.variant) : undefined,
5226
5037
  metadata: isObject$1(object.metadata) ? object.metadata : undefined,
5227
5038
  };
5228
5039
  },
5229
5040
  toJSON(message) {
5230
5041
  const obj = {};
5231
- if (message.value === true) {
5042
+ if (message.value !== undefined) {
5232
5043
  obj.value = message.value;
5233
5044
  }
5234
5045
  if (message.reason !== "") {
5235
5046
  obj.reason = message.reason;
5236
5047
  }
5237
- if (message.variant !== "") {
5048
+ if (message.variant !== undefined) {
5238
5049
  obj.variant = message.variant;
5239
5050
  }
5240
5051
  if (message.metadata !== undefined) {
@@ -5243,15 +5054,15 @@ const ResolveBooleanResponse = {
5243
5054
  return obj;
5244
5055
  },
5245
5056
  create(base) {
5246
- return ResolveBooleanResponse.fromPartial(base !== null && base !== undefined ? base : {});
5057
+ return ResolveBooleanResponse.fromPartial(base !== null && base !== void 0 ? base : {});
5247
5058
  },
5248
5059
  fromPartial(object) {
5249
5060
  var _a, _b, _c, _d;
5250
5061
  const message = createBaseResolveBooleanResponse();
5251
- message.value = (_a = object.value) !== null && _a !== undefined ? _a : false;
5252
- message.reason = (_b = object.reason) !== null && _b !== undefined ? _b : "";
5253
- message.variant = (_c = object.variant) !== null && _c !== undefined ? _c : "";
5254
- message.metadata = (_d = object.metadata) !== null && _d !== undefined ? _d : undefined;
5062
+ message.value = (_a = object.value) !== null && _a !== void 0 ? _a : undefined;
5063
+ message.reason = (_b = object.reason) !== null && _b !== void 0 ? _b : "";
5064
+ message.variant = (_c = object.variant) !== null && _c !== void 0 ? _c : undefined;
5065
+ message.metadata = (_d = object.metadata) !== null && _d !== void 0 ? _d : undefined;
5255
5066
  return message;
5256
5067
  },
5257
5068
  };
@@ -5312,28 +5123,28 @@ const ResolveStringRequest = {
5312
5123
  return obj;
5313
5124
  },
5314
5125
  create(base) {
5315
- return ResolveStringRequest.fromPartial(base !== null && base !== undefined ? base : {});
5126
+ return ResolveStringRequest.fromPartial(base !== null && base !== void 0 ? base : {});
5316
5127
  },
5317
5128
  fromPartial(object) {
5318
5129
  var _a, _b;
5319
5130
  const message = createBaseResolveStringRequest();
5320
- message.flagKey = (_a = object.flagKey) !== null && _a !== undefined ? _a : "";
5321
- message.context = (_b = object.context) !== null && _b !== undefined ? _b : undefined;
5131
+ message.flagKey = (_a = object.flagKey) !== null && _a !== void 0 ? _a : "";
5132
+ message.context = (_b = object.context) !== null && _b !== void 0 ? _b : undefined;
5322
5133
  return message;
5323
5134
  },
5324
5135
  };
5325
5136
  function createBaseResolveStringResponse() {
5326
- return { value: "", reason: "", variant: "", metadata: undefined };
5137
+ return { value: undefined, reason: "", variant: undefined, metadata: undefined };
5327
5138
  }
5328
5139
  const ResolveStringResponse = {
5329
5140
  encode(message, writer = _m0.Writer.create()) {
5330
- if (message.value !== "") {
5141
+ if (message.value !== undefined) {
5331
5142
  writer.uint32(10).string(message.value);
5332
5143
  }
5333
5144
  if (message.reason !== "") {
5334
5145
  writer.uint32(18).string(message.reason);
5335
5146
  }
5336
- if (message.variant !== "") {
5147
+ if (message.variant !== undefined) {
5337
5148
  writer.uint32(26).string(message.variant);
5338
5149
  }
5339
5150
  if (message.metadata !== undefined) {
@@ -5382,21 +5193,21 @@ const ResolveStringResponse = {
5382
5193
  },
5383
5194
  fromJSON(object) {
5384
5195
  return {
5385
- value: isSet$1(object.value) ? String(object.value) : "",
5196
+ value: isSet$1(object.value) ? String(object.value) : undefined,
5386
5197
  reason: isSet$1(object.reason) ? String(object.reason) : "",
5387
- variant: isSet$1(object.variant) ? String(object.variant) : "",
5198
+ variant: isSet$1(object.variant) ? String(object.variant) : undefined,
5388
5199
  metadata: isObject$1(object.metadata) ? object.metadata : undefined,
5389
5200
  };
5390
5201
  },
5391
5202
  toJSON(message) {
5392
5203
  const obj = {};
5393
- if (message.value !== "") {
5204
+ if (message.value !== undefined) {
5394
5205
  obj.value = message.value;
5395
5206
  }
5396
5207
  if (message.reason !== "") {
5397
5208
  obj.reason = message.reason;
5398
5209
  }
5399
- if (message.variant !== "") {
5210
+ if (message.variant !== undefined) {
5400
5211
  obj.variant = message.variant;
5401
5212
  }
5402
5213
  if (message.metadata !== undefined) {
@@ -5405,15 +5216,15 @@ const ResolveStringResponse = {
5405
5216
  return obj;
5406
5217
  },
5407
5218
  create(base) {
5408
- return ResolveStringResponse.fromPartial(base !== null && base !== undefined ? base : {});
5219
+ return ResolveStringResponse.fromPartial(base !== null && base !== void 0 ? base : {});
5409
5220
  },
5410
5221
  fromPartial(object) {
5411
5222
  var _a, _b, _c, _d;
5412
5223
  const message = createBaseResolveStringResponse();
5413
- message.value = (_a = object.value) !== null && _a !== undefined ? _a : "";
5414
- message.reason = (_b = object.reason) !== null && _b !== undefined ? _b : "";
5415
- message.variant = (_c = object.variant) !== null && _c !== undefined ? _c : "";
5416
- message.metadata = (_d = object.metadata) !== null && _d !== undefined ? _d : undefined;
5224
+ message.value = (_a = object.value) !== null && _a !== void 0 ? _a : undefined;
5225
+ message.reason = (_b = object.reason) !== null && _b !== void 0 ? _b : "";
5226
+ message.variant = (_c = object.variant) !== null && _c !== void 0 ? _c : undefined;
5227
+ message.metadata = (_d = object.metadata) !== null && _d !== void 0 ? _d : undefined;
5417
5228
  return message;
5418
5229
  },
5419
5230
  };
@@ -5474,28 +5285,28 @@ const ResolveFloatRequest = {
5474
5285
  return obj;
5475
5286
  },
5476
5287
  create(base) {
5477
- return ResolveFloatRequest.fromPartial(base !== null && base !== undefined ? base : {});
5288
+ return ResolveFloatRequest.fromPartial(base !== null && base !== void 0 ? base : {});
5478
5289
  },
5479
5290
  fromPartial(object) {
5480
5291
  var _a, _b;
5481
5292
  const message = createBaseResolveFloatRequest();
5482
- message.flagKey = (_a = object.flagKey) !== null && _a !== undefined ? _a : "";
5483
- message.context = (_b = object.context) !== null && _b !== undefined ? _b : undefined;
5293
+ message.flagKey = (_a = object.flagKey) !== null && _a !== void 0 ? _a : "";
5294
+ message.context = (_b = object.context) !== null && _b !== void 0 ? _b : undefined;
5484
5295
  return message;
5485
5296
  },
5486
5297
  };
5487
5298
  function createBaseResolveFloatResponse() {
5488
- return { value: 0, reason: "", variant: "", metadata: undefined };
5299
+ return { value: undefined, reason: "", variant: undefined, metadata: undefined };
5489
5300
  }
5490
5301
  const ResolveFloatResponse = {
5491
5302
  encode(message, writer = _m0.Writer.create()) {
5492
- if (message.value !== 0) {
5303
+ if (message.value !== undefined) {
5493
5304
  writer.uint32(9).double(message.value);
5494
5305
  }
5495
5306
  if (message.reason !== "") {
5496
5307
  writer.uint32(18).string(message.reason);
5497
5308
  }
5498
- if (message.variant !== "") {
5309
+ if (message.variant !== undefined) {
5499
5310
  writer.uint32(26).string(message.variant);
5500
5311
  }
5501
5312
  if (message.metadata !== undefined) {
@@ -5544,21 +5355,21 @@ const ResolveFloatResponse = {
5544
5355
  },
5545
5356
  fromJSON(object) {
5546
5357
  return {
5547
- value: isSet$1(object.value) ? Number(object.value) : 0,
5358
+ value: isSet$1(object.value) ? Number(object.value) : undefined,
5548
5359
  reason: isSet$1(object.reason) ? String(object.reason) : "",
5549
- variant: isSet$1(object.variant) ? String(object.variant) : "",
5360
+ variant: isSet$1(object.variant) ? String(object.variant) : undefined,
5550
5361
  metadata: isObject$1(object.metadata) ? object.metadata : undefined,
5551
5362
  };
5552
5363
  },
5553
5364
  toJSON(message) {
5554
5365
  const obj = {};
5555
- if (message.value !== 0) {
5366
+ if (message.value !== undefined) {
5556
5367
  obj.value = message.value;
5557
5368
  }
5558
5369
  if (message.reason !== "") {
5559
5370
  obj.reason = message.reason;
5560
5371
  }
5561
- if (message.variant !== "") {
5372
+ if (message.variant !== undefined) {
5562
5373
  obj.variant = message.variant;
5563
5374
  }
5564
5375
  if (message.metadata !== undefined) {
@@ -5567,15 +5378,15 @@ const ResolveFloatResponse = {
5567
5378
  return obj;
5568
5379
  },
5569
5380
  create(base) {
5570
- return ResolveFloatResponse.fromPartial(base !== null && base !== undefined ? base : {});
5381
+ return ResolveFloatResponse.fromPartial(base !== null && base !== void 0 ? base : {});
5571
5382
  },
5572
5383
  fromPartial(object) {
5573
5384
  var _a, _b, _c, _d;
5574
5385
  const message = createBaseResolveFloatResponse();
5575
- message.value = (_a = object.value) !== null && _a !== undefined ? _a : 0;
5576
- message.reason = (_b = object.reason) !== null && _b !== undefined ? _b : "";
5577
- message.variant = (_c = object.variant) !== null && _c !== undefined ? _c : "";
5578
- message.metadata = (_d = object.metadata) !== null && _d !== undefined ? _d : undefined;
5386
+ message.value = (_a = object.value) !== null && _a !== void 0 ? _a : undefined;
5387
+ message.reason = (_b = object.reason) !== null && _b !== void 0 ? _b : "";
5388
+ message.variant = (_c = object.variant) !== null && _c !== void 0 ? _c : undefined;
5389
+ message.metadata = (_d = object.metadata) !== null && _d !== void 0 ? _d : undefined;
5579
5390
  return message;
5580
5391
  },
5581
5392
  };
@@ -5636,28 +5447,28 @@ const ResolveIntRequest = {
5636
5447
  return obj;
5637
5448
  },
5638
5449
  create(base) {
5639
- return ResolveIntRequest.fromPartial(base !== null && base !== undefined ? base : {});
5450
+ return ResolveIntRequest.fromPartial(base !== null && base !== void 0 ? base : {});
5640
5451
  },
5641
5452
  fromPartial(object) {
5642
5453
  var _a, _b;
5643
5454
  const message = createBaseResolveIntRequest();
5644
- message.flagKey = (_a = object.flagKey) !== null && _a !== undefined ? _a : "";
5645
- message.context = (_b = object.context) !== null && _b !== undefined ? _b : undefined;
5455
+ message.flagKey = (_a = object.flagKey) !== null && _a !== void 0 ? _a : "";
5456
+ message.context = (_b = object.context) !== null && _b !== void 0 ? _b : undefined;
5646
5457
  return message;
5647
5458
  },
5648
5459
  };
5649
5460
  function createBaseResolveIntResponse() {
5650
- return { value: "0", reason: "", variant: "", metadata: undefined };
5461
+ return { value: undefined, reason: "", variant: undefined, metadata: undefined };
5651
5462
  }
5652
5463
  const ResolveIntResponse = {
5653
5464
  encode(message, writer = _m0.Writer.create()) {
5654
- if (message.value !== "0") {
5465
+ if (message.value !== undefined) {
5655
5466
  writer.uint32(8).int64(message.value);
5656
5467
  }
5657
5468
  if (message.reason !== "") {
5658
5469
  writer.uint32(18).string(message.reason);
5659
5470
  }
5660
- if (message.variant !== "") {
5471
+ if (message.variant !== undefined) {
5661
5472
  writer.uint32(26).string(message.variant);
5662
5473
  }
5663
5474
  if (message.metadata !== undefined) {
@@ -5706,21 +5517,21 @@ const ResolveIntResponse = {
5706
5517
  },
5707
5518
  fromJSON(object) {
5708
5519
  return {
5709
- value: isSet$1(object.value) ? String(object.value) : "0",
5520
+ value: isSet$1(object.value) ? String(object.value) : undefined,
5710
5521
  reason: isSet$1(object.reason) ? String(object.reason) : "",
5711
- variant: isSet$1(object.variant) ? String(object.variant) : "",
5522
+ variant: isSet$1(object.variant) ? String(object.variant) : undefined,
5712
5523
  metadata: isObject$1(object.metadata) ? object.metadata : undefined,
5713
5524
  };
5714
5525
  },
5715
5526
  toJSON(message) {
5716
5527
  const obj = {};
5717
- if (message.value !== "0") {
5528
+ if (message.value !== undefined) {
5718
5529
  obj.value = message.value;
5719
5530
  }
5720
5531
  if (message.reason !== "") {
5721
5532
  obj.reason = message.reason;
5722
5533
  }
5723
- if (message.variant !== "") {
5534
+ if (message.variant !== undefined) {
5724
5535
  obj.variant = message.variant;
5725
5536
  }
5726
5537
  if (message.metadata !== undefined) {
@@ -5729,15 +5540,15 @@ const ResolveIntResponse = {
5729
5540
  return obj;
5730
5541
  },
5731
5542
  create(base) {
5732
- return ResolveIntResponse.fromPartial(base !== null && base !== undefined ? base : {});
5543
+ return ResolveIntResponse.fromPartial(base !== null && base !== void 0 ? base : {});
5733
5544
  },
5734
5545
  fromPartial(object) {
5735
5546
  var _a, _b, _c, _d;
5736
5547
  const message = createBaseResolveIntResponse();
5737
- message.value = (_a = object.value) !== null && _a !== undefined ? _a : "0";
5738
- message.reason = (_b = object.reason) !== null && _b !== undefined ? _b : "";
5739
- message.variant = (_c = object.variant) !== null && _c !== undefined ? _c : "";
5740
- message.metadata = (_d = object.metadata) !== null && _d !== undefined ? _d : undefined;
5548
+ message.value = (_a = object.value) !== null && _a !== void 0 ? _a : undefined;
5549
+ message.reason = (_b = object.reason) !== null && _b !== void 0 ? _b : "";
5550
+ message.variant = (_c = object.variant) !== null && _c !== void 0 ? _c : undefined;
5551
+ message.metadata = (_d = object.metadata) !== null && _d !== void 0 ? _d : undefined;
5741
5552
  return message;
5742
5553
  },
5743
5554
  };
@@ -5798,18 +5609,18 @@ const ResolveObjectRequest = {
5798
5609
  return obj;
5799
5610
  },
5800
5611
  create(base) {
5801
- return ResolveObjectRequest.fromPartial(base !== null && base !== undefined ? base : {});
5612
+ return ResolveObjectRequest.fromPartial(base !== null && base !== void 0 ? base : {});
5802
5613
  },
5803
5614
  fromPartial(object) {
5804
5615
  var _a, _b;
5805
5616
  const message = createBaseResolveObjectRequest();
5806
- message.flagKey = (_a = object.flagKey) !== null && _a !== undefined ? _a : "";
5807
- message.context = (_b = object.context) !== null && _b !== undefined ? _b : undefined;
5617
+ message.flagKey = (_a = object.flagKey) !== null && _a !== void 0 ? _a : "";
5618
+ message.context = (_b = object.context) !== null && _b !== void 0 ? _b : undefined;
5808
5619
  return message;
5809
5620
  },
5810
5621
  };
5811
5622
  function createBaseResolveObjectResponse() {
5812
- return { value: undefined, reason: "", variant: "", metadata: undefined };
5623
+ return { value: undefined, reason: "", variant: undefined, metadata: undefined };
5813
5624
  }
5814
5625
  const ResolveObjectResponse = {
5815
5626
  encode(message, writer = _m0.Writer.create()) {
@@ -5819,7 +5630,7 @@ const ResolveObjectResponse = {
5819
5630
  if (message.reason !== "") {
5820
5631
  writer.uint32(18).string(message.reason);
5821
5632
  }
5822
- if (message.variant !== "") {
5633
+ if (message.variant !== undefined) {
5823
5634
  writer.uint32(26).string(message.variant);
5824
5635
  }
5825
5636
  if (message.metadata !== undefined) {
@@ -5870,7 +5681,7 @@ const ResolveObjectResponse = {
5870
5681
  return {
5871
5682
  value: isObject$1(object.value) ? object.value : undefined,
5872
5683
  reason: isSet$1(object.reason) ? String(object.reason) : "",
5873
- variant: isSet$1(object.variant) ? String(object.variant) : "",
5684
+ variant: isSet$1(object.variant) ? String(object.variant) : undefined,
5874
5685
  metadata: isObject$1(object.metadata) ? object.metadata : undefined,
5875
5686
  };
5876
5687
  },
@@ -5882,7 +5693,7 @@ const ResolveObjectResponse = {
5882
5693
  if (message.reason !== "") {
5883
5694
  obj.reason = message.reason;
5884
5695
  }
5885
- if (message.variant !== "") {
5696
+ if (message.variant !== undefined) {
5886
5697
  obj.variant = message.variant;
5887
5698
  }
5888
5699
  if (message.metadata !== undefined) {
@@ -5891,15 +5702,15 @@ const ResolveObjectResponse = {
5891
5702
  return obj;
5892
5703
  },
5893
5704
  create(base) {
5894
- return ResolveObjectResponse.fromPartial(base !== null && base !== undefined ? base : {});
5705
+ return ResolveObjectResponse.fromPartial(base !== null && base !== void 0 ? base : {});
5895
5706
  },
5896
5707
  fromPartial(object) {
5897
5708
  var _a, _b, _c, _d;
5898
5709
  const message = createBaseResolveObjectResponse();
5899
- message.value = (_a = object.value) !== null && _a !== undefined ? _a : undefined;
5900
- message.reason = (_b = object.reason) !== null && _b !== undefined ? _b : "";
5901
- message.variant = (_c = object.variant) !== null && _c !== undefined ? _c : "";
5902
- message.metadata = (_d = object.metadata) !== null && _d !== undefined ? _d : undefined;
5710
+ message.value = (_a = object.value) !== null && _a !== void 0 ? _a : undefined;
5711
+ message.reason = (_b = object.reason) !== null && _b !== void 0 ? _b : "";
5712
+ message.variant = (_c = object.variant) !== null && _c !== void 0 ? _c : undefined;
5713
+ message.metadata = (_d = object.metadata) !== null && _d !== void 0 ? _d : undefined;
5903
5714
  return message;
5904
5715
  },
5905
5716
  };
@@ -5960,13 +5771,13 @@ const EventStreamResponse = {
5960
5771
  return obj;
5961
5772
  },
5962
5773
  create(base) {
5963
- return EventStreamResponse.fromPartial(base !== null && base !== undefined ? base : {});
5774
+ return EventStreamResponse.fromPartial(base !== null && base !== void 0 ? base : {});
5964
5775
  },
5965
5776
  fromPartial(object) {
5966
5777
  var _a, _b;
5967
5778
  const message = createBaseEventStreamResponse();
5968
- message.type = (_a = object.type) !== null && _a !== undefined ? _a : "";
5969
- message.data = (_b = object.data) !== null && _b !== undefined ? _b : undefined;
5779
+ message.type = (_a = object.type) !== null && _a !== void 0 ? _a : "";
5780
+ message.data = (_b = object.data) !== null && _b !== void 0 ? _b : undefined;
5970
5781
  return message;
5971
5782
  },
5972
5783
  };
@@ -5998,7 +5809,7 @@ const EventStreamRequest = {
5998
5809
  return obj;
5999
5810
  },
6000
5811
  create(base) {
6001
- return EventStreamRequest.fromPartial(base !== null && base !== undefined ? base : {});
5812
+ return EventStreamRequest.fromPartial(base !== null && base !== void 0 ? base : {});
6002
5813
  },
6003
5814
  fromPartial(_) {
6004
5815
  const message = createBaseEventStreamRequest();
@@ -6006,17 +5817,8 @@ const EventStreamRequest = {
6006
5817
  },
6007
5818
  };
6008
5819
  const ServiceService = {
6009
- resolveAll: {
6010
- path: "/flagd.evaluation.v1.Service/ResolveAll",
6011
- requestStream: false,
6012
- responseStream: false,
6013
- requestSerialize: (value) => Buffer.from(ResolveAllRequest.encode(value).finish()),
6014
- requestDeserialize: (value) => ResolveAllRequest.decode(value),
6015
- responseSerialize: (value) => Buffer.from(ResolveAllResponse.encode(value).finish()),
6016
- responseDeserialize: (value) => ResolveAllResponse.decode(value),
6017
- },
6018
5820
  resolveBoolean: {
6019
- path: "/flagd.evaluation.v1.Service/ResolveBoolean",
5821
+ path: "/flagd.evaluation.v2.Service/ResolveBoolean",
6020
5822
  requestStream: false,
6021
5823
  responseStream: false,
6022
5824
  requestSerialize: (value) => Buffer.from(ResolveBooleanRequest.encode(value).finish()),
@@ -6025,7 +5827,7 @@ const ServiceService = {
6025
5827
  responseDeserialize: (value) => ResolveBooleanResponse.decode(value),
6026
5828
  },
6027
5829
  resolveString: {
6028
- path: "/flagd.evaluation.v1.Service/ResolveString",
5830
+ path: "/flagd.evaluation.v2.Service/ResolveString",
6029
5831
  requestStream: false,
6030
5832
  responseStream: false,
6031
5833
  requestSerialize: (value) => Buffer.from(ResolveStringRequest.encode(value).finish()),
@@ -6034,7 +5836,7 @@ const ServiceService = {
6034
5836
  responseDeserialize: (value) => ResolveStringResponse.decode(value),
6035
5837
  },
6036
5838
  resolveFloat: {
6037
- path: "/flagd.evaluation.v1.Service/ResolveFloat",
5839
+ path: "/flagd.evaluation.v2.Service/ResolveFloat",
6038
5840
  requestStream: false,
6039
5841
  responseStream: false,
6040
5842
  requestSerialize: (value) => Buffer.from(ResolveFloatRequest.encode(value).finish()),
@@ -6043,7 +5845,7 @@ const ServiceService = {
6043
5845
  responseDeserialize: (value) => ResolveFloatResponse.decode(value),
6044
5846
  },
6045
5847
  resolveInt: {
6046
- path: "/flagd.evaluation.v1.Service/ResolveInt",
5848
+ path: "/flagd.evaluation.v2.Service/ResolveInt",
6047
5849
  requestStream: false,
6048
5850
  responseStream: false,
6049
5851
  requestSerialize: (value) => Buffer.from(ResolveIntRequest.encode(value).finish()),
@@ -6052,7 +5854,7 @@ const ServiceService = {
6052
5854
  responseDeserialize: (value) => ResolveIntResponse.decode(value),
6053
5855
  },
6054
5856
  resolveObject: {
6055
- path: "/flagd.evaluation.v1.Service/ResolveObject",
5857
+ path: "/flagd.evaluation.v2.Service/ResolveObject",
6056
5858
  requestStream: false,
6057
5859
  responseStream: false,
6058
5860
  requestSerialize: (value) => Buffer.from(ResolveObjectRequest.encode(value).finish()),
@@ -6061,7 +5863,7 @@ const ServiceService = {
6061
5863
  responseDeserialize: (value) => ResolveObjectResponse.decode(value),
6062
5864
  },
6063
5865
  eventStream: {
6064
- path: "/flagd.evaluation.v1.Service/EventStream",
5866
+ path: "/flagd.evaluation.v2.Service/EventStream",
6065
5867
  requestStream: false,
6066
5868
  responseStream: true,
6067
5869
  requestSerialize: (value) => Buffer.from(EventStreamRequest.encode(value).finish()),
@@ -6070,7 +5872,7 @@ const ServiceService = {
6070
5872
  responseDeserialize: (value) => EventStreamResponse.decode(value),
6071
5873
  },
6072
5874
  };
6073
- const ServiceClient = makeGenericClientConstructor(ServiceService, "flagd.evaluation.v1.Service");
5875
+ const ServiceClient = makeGenericClientConstructor(ServiceService, "flagd.evaluation.v2.Service");
6074
5876
  function longToString(long) {
6075
5877
  return long.toString();
6076
5878
  }
@@ -6085,6 +5887,10 @@ function isSet$1(value) {
6085
5887
  return value !== null && value !== undefined;
6086
5888
  }
6087
5889
 
5890
+ /**
5891
+ * Get the string name of a gRPC status code.
5892
+ */
5893
+ const statusName = (code) => status[code];
6088
5894
  const closeStreamIfDefined = (stream) => {
6089
5895
  /**
6090
5896
  * cancel() is necessary to prevent calls from hanging the process, so we need to we need to remove all the
@@ -6099,6 +5905,122 @@ const closeStreamIfDefined = (stream) => {
6099
5905
  stream.destroy();
6100
5906
  }
6101
5907
  };
5908
+ /**
5909
+ * Creates gRPC channel credentials based on TLS and certificate path configuration.
5910
+ * @returns Channel credentials for gRPC connection
5911
+ */
5912
+ const createChannelCredentials = (tls, certPath) => {
5913
+ if (!tls) {
5914
+ return credentials.createInsecure();
5915
+ }
5916
+ if (certPath && existsSync(certPath)) {
5917
+ const rootCerts = readFileSync(certPath);
5918
+ return credentials.createSsl(rootCerts);
5919
+ }
5920
+ return credentials.createSsl();
5921
+ };
5922
+ /**
5923
+ * Mapping of configuration options to gRPC client options.
5924
+ */
5925
+ const CONFIG_TO_GRPC_OPTIONS = [
5926
+ { configKey: 'defaultAuthority', grpcKey: 'grpc.default_authority' },
5927
+ { configKey: 'keepAliveTime', grpcKey: 'grpc.keepalive_time_ms', condition: (time) => Number(time) > 0 },
5928
+ ];
5929
+ /**
5930
+ * Builds gRPC client options from config.
5931
+ */
5932
+ function buildClientOptions(config) {
5933
+ const options = {
5934
+ 'grpc.service_config': buildRetryPolicy(['flagd.evaluation.v1.Service', 'flagd.evaluation.v2.Service', 'flagd.sync.v1.FlagSyncService'], config.retryBackoffMs, config.retryBackoffMaxMs),
5935
+ };
5936
+ for (const { configKey, grpcKey, condition } of CONFIG_TO_GRPC_OPTIONS) {
5937
+ const value = config[configKey];
5938
+ if (value !== undefined && (!condition || condition(value))) {
5939
+ options[grpcKey] = value;
5940
+ }
5941
+ }
5942
+ return options;
5943
+ }
5944
+ /**
5945
+ * Builds RetryPolicy for gRPC client options.
5946
+ * @param serviceNames Array of service names to configure retry policy for
5947
+ * @param retryBackoffMs Initial backoff duration in milliseconds
5948
+ * @param retryBackoffMaxMs Maximum backoff duration in milliseconds
5949
+ * @returns gRPC client options with retry policy
5950
+ */
5951
+ const buildRetryPolicy = (serviceNames, retryBackoffMs, retryBackoffMaxMs) => {
5952
+ const initialBackoff = retryBackoffMs !== null && retryBackoffMs !== void 0 ? retryBackoffMs : 1000;
5953
+ const maxBackoff = retryBackoffMaxMs !== null && retryBackoffMaxMs !== void 0 ? retryBackoffMaxMs : 120000;
5954
+ const retryPolicy = {
5955
+ maxAttempts: 3,
5956
+ initialBackoff: `${Math.round(initialBackoff / 1000).toFixed(2)}s`,
5957
+ maxBackoff: `${Math.round(maxBackoff / 1000).toFixed(2)}s`,
5958
+ backoffMultiplier: 2,
5959
+ retryableStatusCodes: [statusName(status.UNAVAILABLE), statusName(status.UNKNOWN)],
5960
+ };
5961
+ return JSON.stringify({
5962
+ loadBalancingConfig: [],
5963
+ methodConfig: [
5964
+ {
5965
+ name: serviceNames.map((serviceName) => ({ service: serviceName })),
5966
+ retryPolicy,
5967
+ },
5968
+ ],
5969
+ });
5970
+ };
5971
+ /**
5972
+ * Converts an array of gRPC status code strings to a Set of numeric codes.
5973
+ * @param fatalStatusCodes Array of status code strings.
5974
+ * @param logger Optional logger for warning about unknown codes
5975
+ * @returns Set of numeric status codes
5976
+ */
5977
+ const createFatalStatusCodesSet = (fatalStatusCodes, logger) => {
5978
+ if (!(fatalStatusCodes === null || fatalStatusCodes === void 0 ? void 0 : fatalStatusCodes.length)) {
5979
+ return new Set();
5980
+ }
5981
+ return fatalStatusCodes.reduce((codes, codeStr) => {
5982
+ const numericCode = status[codeStr];
5983
+ if (typeof numericCode === 'number') {
5984
+ codes.add(numericCode);
5985
+ }
5986
+ else {
5987
+ logger === null || logger === void 0 ? void 0 : logger.warn(`Unknown gRPC status code: "${codeStr}"`);
5988
+ }
5989
+ return codes;
5990
+ }, new Set());
5991
+ };
5992
+ /**
5993
+ * Checks if an error is a fatal gRPC status code that should not be retried.
5994
+ * This should only be checked on the first connection attempt.
5995
+ *
5996
+ * @param err The error to check
5997
+ * @param initialized Whether the connection has been successfully initialized
5998
+ * @param fatalStatusCodes Set of numeric status codes considered fatal
5999
+ * @returns True if the error is fatal and should not be retried
6000
+ */
6001
+ const isFatalStatusCodeError = (err, initialized, fatalStatusCodes) => {
6002
+ if (initialized) {
6003
+ return false;
6004
+ }
6005
+ const serviceError = err;
6006
+ return (serviceError === null || serviceError === void 0 ? void 0 : serviceError.code) !== undefined && fatalStatusCodes.has(serviceError.code);
6007
+ };
6008
+ /**
6009
+ * Handles a fatal gRPC status code error by logging it.
6010
+ * Should only be called when isFatalStatusCodeError returns true.
6011
+ *
6012
+ * @param err The error to handle
6013
+ * @param logger Optional logger for error logging
6014
+ * @param disconnectCallback Callback to invoke with the error message
6015
+ * @param rejectConnect Optional callback to reject the connection promise
6016
+ */
6017
+ const handleFatalStatusCodeError = (err, logger, disconnectCallback, rejectConnect) => {
6018
+ const serviceError = err;
6019
+ logger === null || logger === void 0 ? void 0 : logger.error(`Encountered fatal status code ${serviceError.code} (${serviceError.message}) on first connection, will not retry`);
6020
+ const errorMessage = `PROVIDER_FATAL: ${serviceError.message}`;
6021
+ disconnectCallback(errorMessage);
6022
+ rejectConnect === null || rejectConnect === void 0 ? void 0 : rejectConnect(new ProviderFatalError(serviceError.message));
6023
+ };
6102
6024
 
6103
6025
  class GRPCService {
6104
6026
  get _cacheActive() {
@@ -6109,9 +6031,11 @@ class GRPCService {
6109
6031
  this.logger = logger;
6110
6032
  this._cacheEnabled = false;
6111
6033
  this._eventStream = undefined;
6034
+ this._initialized = false;
6035
+ this._errorThrottled = false;
6112
6036
  this.onRejected = (err) => {
6113
6037
  // map the errors
6114
- switch (err === null || err === undefined ? undefined : err.code) {
6038
+ switch (err === null || err === void 0 ? void 0 : err.code) {
6115
6039
  case status.DATA_LOSS:
6116
6040
  throw new ParseError(err.details);
6117
6041
  case status.INVALID_ARGUMENT:
@@ -6121,95 +6045,128 @@ class GRPCService {
6121
6045
  case status.UNAVAILABLE:
6122
6046
  throw new FlagNotFoundError(err.details);
6123
6047
  default:
6124
- throw new GeneralError(err === null || err === undefined ? undefined : err.details);
6048
+ throw new GeneralError(err === null || err === void 0 ? void 0 : err.details);
6125
6049
  }
6126
6050
  };
6127
- const { host, port, tls, socketPath, defaultAuthority } = config;
6128
- let clientOptions;
6129
- if (defaultAuthority) {
6130
- clientOptions = {
6131
- 'grpc.default_authority': defaultAuthority,
6132
- };
6133
- }
6051
+ const { host, port, tls, socketPath, certPath } = config;
6052
+ const clientOptions = buildClientOptions(config);
6053
+ const channelCredentials = createChannelCredentials(tls, certPath);
6054
+ this._maxBackoffMs = config.retryBackoffMaxMs || DEFAULT_MAX_BACKOFF_MS;
6134
6055
  this._client = client
6135
6056
  ? client
6136
- : new ServiceClient(socketPath ? `unix://${socketPath}` : `${host}:${port}`, tls ? credentials.createSsl() : credentials.createInsecure(), clientOptions);
6057
+ : new ServiceClient(socketPath ? `unix://${socketPath}` : `${host}:${port}`, channelCredentials, clientOptions);
6058
+ this._deadline = config.deadlineMs;
6059
+ this._streamDeadline = config.streamDeadlineMs;
6137
6060
  if (config.cache === 'lru') {
6138
6061
  this._cacheEnabled = true;
6139
6062
  this._cache = new LRUCache({ maxSize: config.maxCacheSize || DEFAULT_MAX_CACHE_SIZE, sizeCalculation: () => 1 });
6140
6063
  }
6064
+ this._fatalStatusCodes = createFatalStatusCodesSet(config.fatalStatusCodes, logger);
6065
+ // create metadata with the flagd-selector header if selector is configured
6066
+ this._metadata = new Metadata();
6067
+ if (config.selector) {
6068
+ this._metadata.set(FLAGD_SELECTOR_HEADER, config.selector);
6069
+ }
6070
+ }
6071
+ clearCache() {
6072
+ var _a;
6073
+ (_a = this._cache) === null || _a === void 0 ? void 0 : _a.clear();
6141
6074
  }
6142
6075
  connect(reconnectCallback, changedCallback, disconnectCallback) {
6143
6076
  return new Promise((resolve, reject) => this.listen(reconnectCallback, changedCallback, disconnectCallback, resolve, reject));
6144
6077
  }
6145
6078
  disconnect() {
6146
- return __awaiter(this, undefined, undefined, function* () {
6079
+ return __awaiter(this, void 0, void 0, function* () {
6147
6080
  closeStreamIfDefined(this._eventStream);
6148
6081
  this._client.close();
6149
6082
  });
6150
6083
  }
6151
- resolveBoolean(flagKey, _, context, logger) {
6152
- return __awaiter(this, undefined, undefined, function* () {
6153
- return this.resolve(this._client.resolveBoolean, flagKey, context, logger);
6084
+ resolveBoolean(flagKey, defaultValue, context, logger) {
6085
+ return __awaiter(this, void 0, void 0, function* () {
6086
+ return this.resolve(this._client.resolveBoolean, flagKey, defaultValue, context, logger);
6154
6087
  });
6155
6088
  }
6156
- resolveString(flagKey, _, context, logger) {
6157
- return __awaiter(this, undefined, undefined, function* () {
6158
- return this.resolve(this._client.resolveString, flagKey, context, logger);
6089
+ resolveString(flagKey, defaultValue, context, logger) {
6090
+ return __awaiter(this, void 0, void 0, function* () {
6091
+ return this.resolve(this._client.resolveString, flagKey, defaultValue, context, logger);
6159
6092
  });
6160
6093
  }
6161
- resolveNumber(flagKey, _, context, logger) {
6162
- return __awaiter(this, undefined, undefined, function* () {
6163
- return this.resolve(this._client.resolveFloat, flagKey, context, logger);
6094
+ resolveNumber(flagKey, defaultValue, context, logger) {
6095
+ return __awaiter(this, void 0, void 0, function* () {
6096
+ return this.resolve(this._client.resolveFloat, flagKey, defaultValue, context, logger);
6164
6097
  });
6165
6098
  }
6166
- resolveObject(flagKey, _, context, logger) {
6167
- return __awaiter(this, undefined, undefined, function* () {
6168
- return this.resolve(this._client.resolveObject, flagKey, context, logger);
6099
+ resolveObject(flagKey, defaultValue, context, logger) {
6100
+ return __awaiter(this, void 0, void 0, function* () {
6101
+ return this.resolve(this._client.resolveObject, flagKey, defaultValue, context, logger);
6169
6102
  });
6170
6103
  }
6171
6104
  listen(reconnectCallback, changedCallback, disconnectCallback, resolveConnect, rejectConnect) {
6172
6105
  var _a;
6173
- (_a = this.logger) === null || _a === undefined ? undefined : _a.debug(`${FlagdProvider.name}: connecting stream...`);
6106
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`${FlagdProvider.name}: connecting stream...`);
6174
6107
  // close the previous stream if we're reconnecting
6175
6108
  closeStreamIfDefined(this._eventStream);
6176
- const stream = this._client.eventStream({}, {});
6177
- stream.on('error', (err) => {
6178
- rejectConnect === null || rejectConnect === undefined ? undefined : rejectConnect(err);
6179
- this.handleError(reconnectCallback, changedCallback, disconnectCallback);
6180
- });
6181
- stream.on('data', (message) => {
6182
- var _a;
6183
- if (message.type === EVENT_PROVIDER_READY) {
6184
- (_a = this.logger) === null || _a === undefined ? undefined : _a.debug(`${FlagdProvider.name}: streaming connection established with flagd`);
6185
- // if resolveConnect is undefined, this is a reconnection; we only want to fire the reconnect callback in that case
6186
- if (resolveConnect) {
6187
- resolveConnect();
6188
- }
6189
- else {
6190
- reconnectCallback();
6109
+ // wait for connection to be stable
6110
+ this._client.waitForReady(Date.now() + this._deadline, (err) => {
6111
+ if (err) {
6112
+ // Check if error is a fatal status code on first connection only
6113
+ if (isFatalStatusCodeError(err, this._initialized, this._fatalStatusCodes)) {
6114
+ handleFatalStatusCodeError(err, this.logger, disconnectCallback, rejectConnect);
6115
+ return;
6191
6116
  }
6117
+ rejectConnect === null || rejectConnect === void 0 ? void 0 : rejectConnect(err);
6118
+ this.handleError(reconnectCallback, changedCallback, disconnectCallback);
6192
6119
  }
6193
- else if (message.type === EVENT_CONFIGURATION_CHANGE) {
6194
- this.handleFlagsChanged(message, changedCallback);
6120
+ else {
6121
+ const streamDeadline = this._streamDeadline != 0 ? Date.now() + this._streamDeadline : undefined;
6122
+ const stream = this._client.eventStream({}, { deadline: streamDeadline });
6123
+ stream.on('error', (err) => {
6124
+ // In cases where we get an explicit error status, we add a delay.
6125
+ // This prevents tight loops when errors are returned immediately, typically by intervening proxies like Envoy.
6126
+ this._errorThrottled = true;
6127
+ // Check if error is a fatal status code on first connection only
6128
+ if (isFatalStatusCodeError(err, this._initialized, this._fatalStatusCodes)) {
6129
+ handleFatalStatusCodeError(err, this.logger, disconnectCallback, rejectConnect);
6130
+ return;
6131
+ }
6132
+ rejectConnect === null || rejectConnect === void 0 ? void 0 : rejectConnect(err);
6133
+ this.handleError(reconnectCallback, changedCallback, disconnectCallback);
6134
+ });
6135
+ stream.on('data', (message) => {
6136
+ var _a;
6137
+ if (message.type === EVENT_PROVIDER_READY) {
6138
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`${FlagdProvider.name}: streaming connection established with flagd`);
6139
+ this._initialized = true;
6140
+ // if resolveConnect is undefined, this is a reconnection; we only want to fire the reconnect callback in that case
6141
+ if (resolveConnect) {
6142
+ resolveConnect();
6143
+ }
6144
+ else {
6145
+ reconnectCallback();
6146
+ }
6147
+ }
6148
+ else if (message.type === EVENT_CONFIGURATION_CHANGE) {
6149
+ this.handleFlagsChanged(message, changedCallback);
6150
+ }
6151
+ });
6152
+ this._eventStream = stream;
6195
6153
  }
6196
6154
  });
6197
- this._eventStream = stream;
6198
6155
  }
6199
6156
  handleFlagsChanged(message, changedCallback) {
6200
6157
  var _a;
6201
6158
  if (message.data) {
6202
6159
  const data = message.data;
6203
- (_a = this.logger) === null || _a === undefined ? undefined : _a.debug(`${FlagdProvider.name}: got message: ${JSON.stringify(data, undefined, 2)}`);
6204
- if (data && typeof data === 'object' && 'flags' in data && (data === null || data === undefined ? undefined : data['flags'])) {
6160
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`${FlagdProvider.name}: got message: ${JSON.stringify(data, undefined, 2)}`);
6161
+ if (data && typeof data === 'object' && 'flags' in data && (data === null || data === void 0 ? void 0 : data['flags'])) {
6205
6162
  const flagChangeMessage = data;
6206
6163
  const flagsChanged = Object.keys(flagChangeMessage.flags || []);
6207
6164
  if (this._cacheEnabled) {
6208
6165
  // remove each changed key from cache
6209
6166
  flagsChanged.forEach((key) => {
6210
6167
  var _a, _b;
6211
- if ((_a = this._cache) === null || _a === undefined ? undefined : _a.delete(key)) {
6212
- (_b = this.logger) === null || _b === undefined ? undefined : _b.debug(`${FlagdProvider.name}: evicted key: ${key} from cache.`);
6168
+ if ((_a = this._cache) === null || _a === void 0 ? void 0 : _a.delete(key)) {
6169
+ (_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug(`${FlagdProvider.name}: evicted key: ${key} from cache.`);
6213
6170
  }
6214
6171
  });
6215
6172
  }
@@ -6218,34 +6175,38 @@ class GRPCService {
6218
6175
  }
6219
6176
  }
6220
6177
  reconnect(reconnectCallback, changedCallback, disconnectCallback) {
6221
- const channel = this._client.getChannel();
6222
- channel.watchConnectivityState(channel.getConnectivityState(true), Infinity, () => {
6223
- this.listen(reconnectCallback, changedCallback, disconnectCallback);
6224
- });
6178
+ setTimeout(() => this.listen(reconnectCallback, changedCallback, disconnectCallback), this._errorThrottled ? this._maxBackoffMs : 0);
6179
+ this._errorThrottled = false;
6225
6180
  }
6226
6181
  handleError(reconnectCallback, changedCallback, disconnectCallback) {
6227
6182
  var _a, _b;
6228
6183
  disconnectCallback('streaming connection error, will attempt reconnect...');
6229
- (_a = this.logger) === null || _a === undefined ? undefined : _a.error(`${FlagdProvider.name}: streaming connection error, will attempt reconnect...`);
6230
- (_b = this._cache) === null || _b === undefined ? undefined : _b.clear();
6184
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.error(`${FlagdProvider.name}: streaming connection error, will attempt reconnect...`);
6185
+ (_b = this._cache) === null || _b === void 0 ? void 0 : _b.clear();
6231
6186
  this.reconnect(reconnectCallback, changedCallback, disconnectCallback);
6232
6187
  }
6233
- resolve(promise, flagKey, context, logger) {
6234
- return __awaiter(this, undefined, undefined, function* () {
6188
+ resolve(promise, flagKey, defaultValue, context, logger) {
6189
+ return __awaiter(this, void 0, void 0, function* () {
6235
6190
  var _a, _b;
6236
6191
  const resolver = promisify(promise);
6237
6192
  if (this._cacheActive) {
6238
- const cached = (_a = this._cache) === null || _a === undefined ? undefined : _a.get(flagKey);
6193
+ const cached = (_a = this._cache) === null || _a === void 0 ? void 0 : _a.get(flagKey);
6239
6194
  if (cached) {
6240
6195
  return Object.assign(Object.assign({}, cached), { reason: StandardResolutionReasons.CACHED });
6241
6196
  }
6242
6197
  }
6243
- // invoke the passed resolver method
6198
+ // invoke the passed resolver method with metadata for selector support
6244
6199
  const response = yield resolver
6245
- .call(this._client, { flagKey, context })
6200
+ .call(this._client, { flagKey, context }, this._metadata)
6246
6201
  .then((resolved) => resolved, this.onRejected);
6202
+ let value = response.value;
6203
+ // When no default variant is configured, the server returns an empty/zero proto
6204
+ // value with reason=DEFAULT. In that case, return the caller's code default value.
6205
+ if (response.reason === StandardResolutionReasons.DEFAULT && !response.variant) {
6206
+ value = defaultValue;
6207
+ }
6247
6208
  const resolved = {
6248
- value: response.value,
6209
+ value,
6249
6210
  reason: response.reason,
6250
6211
  variant: response.variant,
6251
6212
  flagMetadata: response.metadata,
@@ -6253,7 +6214,7 @@ class GRPCService {
6253
6214
  logger.debug(`${FlagdProvider.name}: resolved flag with key: ${resolved.value}, variant: ${response.variant}, reason: ${response.reason}`);
6254
6215
  if (this._cacheActive && response.reason === StandardResolutionReasons.STATIC) {
6255
6216
  // cache this static value
6256
- (_b = this._cache) === null || _b === undefined ? undefined : _b.set(flagKey, resolved);
6217
+ (_b = this._cache) === null || _b === void 0 ? void 0 : _b.set(flagKey, resolved);
6257
6218
  }
6258
6219
  return resolved;
6259
6220
  });
@@ -6267,9 +6228,9 @@ class FileFetch {
6267
6228
  this._logger = logger;
6268
6229
  }
6269
6230
  connect(dataFillCallback, _, changedCallback) {
6270
- return __awaiter(this, undefined, undefined, function* () {
6231
+ return __awaiter(this, void 0, void 0, function* () {
6271
6232
  var _a, _b;
6272
- (_a = this._logger) === null || _a === undefined ? undefined : _a.debug('Starting file sync connection');
6233
+ (_a = this._logger) === null || _a === void 0 ? void 0 : _a.debug('Starting file sync connection');
6273
6234
  try {
6274
6235
  const output = yield promises.readFile(this._filename, encoding);
6275
6236
  // Don't emit the change event for the initial read
@@ -6294,13 +6255,13 @@ class FileFetch {
6294
6255
  throw err;
6295
6256
  }
6296
6257
  else {
6297
- switch (err === null || err === undefined ? undefined : err.code) {
6258
+ switch (err === null || err === void 0 ? void 0 : err.code) {
6298
6259
  case 'ENOENT':
6299
6260
  throw new GeneralError(`File not found: ${this._filename}`);
6300
6261
  case 'EACCES':
6301
6262
  throw new GeneralError(`File not accessible: ${this._filename}`);
6302
6263
  default:
6303
- (_b = this._logger) === null || _b === undefined ? undefined : _b.debug(`Error reading file: ${err}`);
6264
+ (_b = this._logger) === null || _b === void 0 ? void 0 : _b.debug(`Error reading file: ${err}`);
6304
6265
  throw new GeneralError();
6305
6266
  }
6306
6267
  }
@@ -6308,7 +6269,7 @@ class FileFetch {
6308
6269
  });
6309
6270
  }
6310
6271
  disconnect() {
6311
- return __awaiter(this, undefined, undefined, function* () {
6272
+ return __awaiter(this, void 0, void 0, function* () {
6312
6273
  unwatchFile(this._filename);
6313
6274
  });
6314
6275
  }
@@ -6372,24 +6333,27 @@ const SyncFlagsRequest = {
6372
6333
  return obj;
6373
6334
  },
6374
6335
  create(base) {
6375
- return SyncFlagsRequest.fromPartial(base !== null && base !== undefined ? base : {});
6336
+ return SyncFlagsRequest.fromPartial(base !== null && base !== void 0 ? base : {});
6376
6337
  },
6377
6338
  fromPartial(object) {
6378
6339
  var _a, _b;
6379
6340
  const message = createBaseSyncFlagsRequest();
6380
- message.providerId = (_a = object.providerId) !== null && _a !== undefined ? _a : "";
6381
- message.selector = (_b = object.selector) !== null && _b !== undefined ? _b : "";
6341
+ message.providerId = (_a = object.providerId) !== null && _a !== void 0 ? _a : "";
6342
+ message.selector = (_b = object.selector) !== null && _b !== void 0 ? _b : "";
6382
6343
  return message;
6383
6344
  },
6384
6345
  };
6385
6346
  function createBaseSyncFlagsResponse() {
6386
- return { flagConfiguration: "" };
6347
+ return { flagConfiguration: "", syncContext: undefined };
6387
6348
  }
6388
6349
  const SyncFlagsResponse = {
6389
6350
  encode(message, writer = _m0.Writer.create()) {
6390
6351
  if (message.flagConfiguration !== "") {
6391
6352
  writer.uint32(10).string(message.flagConfiguration);
6392
6353
  }
6354
+ if (message.syncContext !== undefined) {
6355
+ Struct.encode(Struct.wrap(message.syncContext), writer.uint32(18).fork()).ldelim();
6356
+ }
6393
6357
  return writer;
6394
6358
  },
6395
6359
  decode(input, length) {
@@ -6405,6 +6369,12 @@ const SyncFlagsResponse = {
6405
6369
  }
6406
6370
  message.flagConfiguration = reader.string();
6407
6371
  continue;
6372
+ case 2:
6373
+ if (tag !== 18) {
6374
+ break;
6375
+ }
6376
+ message.syncContext = Struct.unwrap(Struct.decode(reader, reader.uint32()));
6377
+ continue;
6408
6378
  }
6409
6379
  if ((tag & 7) === 4 || tag === 0) {
6410
6380
  break;
@@ -6414,22 +6384,29 @@ const SyncFlagsResponse = {
6414
6384
  return message;
6415
6385
  },
6416
6386
  fromJSON(object) {
6417
- return { flagConfiguration: isSet(object.flagConfiguration) ? String(object.flagConfiguration) : "" };
6387
+ return {
6388
+ flagConfiguration: isSet(object.flagConfiguration) ? String(object.flagConfiguration) : "",
6389
+ syncContext: isObject(object.syncContext) ? object.syncContext : undefined,
6390
+ };
6418
6391
  },
6419
6392
  toJSON(message) {
6420
6393
  const obj = {};
6421
6394
  if (message.flagConfiguration !== "") {
6422
6395
  obj.flagConfiguration = message.flagConfiguration;
6423
6396
  }
6397
+ if (message.syncContext !== undefined) {
6398
+ obj.syncContext = message.syncContext;
6399
+ }
6424
6400
  return obj;
6425
6401
  },
6426
6402
  create(base) {
6427
- return SyncFlagsResponse.fromPartial(base !== null && base !== undefined ? base : {});
6403
+ return SyncFlagsResponse.fromPartial(base !== null && base !== void 0 ? base : {});
6428
6404
  },
6429
6405
  fromPartial(object) {
6430
- var _a;
6406
+ var _a, _b;
6431
6407
  const message = createBaseSyncFlagsResponse();
6432
- message.flagConfiguration = (_a = object.flagConfiguration) !== null && _a !== undefined ? _a : "";
6408
+ message.flagConfiguration = (_a = object.flagConfiguration) !== null && _a !== void 0 ? _a : "";
6409
+ message.syncContext = (_b = object.syncContext) !== null && _b !== void 0 ? _b : undefined;
6433
6410
  return message;
6434
6411
  },
6435
6412
  };
@@ -6490,13 +6467,13 @@ const FetchAllFlagsRequest = {
6490
6467
  return obj;
6491
6468
  },
6492
6469
  create(base) {
6493
- return FetchAllFlagsRequest.fromPartial(base !== null && base !== undefined ? base : {});
6470
+ return FetchAllFlagsRequest.fromPartial(base !== null && base !== void 0 ? base : {});
6494
6471
  },
6495
6472
  fromPartial(object) {
6496
6473
  var _a, _b;
6497
6474
  const message = createBaseFetchAllFlagsRequest();
6498
- message.providerId = (_a = object.providerId) !== null && _a !== undefined ? _a : "";
6499
- message.selector = (_b = object.selector) !== null && _b !== undefined ? _b : "";
6475
+ message.providerId = (_a = object.providerId) !== null && _a !== void 0 ? _a : "";
6476
+ message.selector = (_b = object.selector) !== null && _b !== void 0 ? _b : "";
6500
6477
  return message;
6501
6478
  },
6502
6479
  };
@@ -6542,12 +6519,12 @@ const FetchAllFlagsResponse = {
6542
6519
  return obj;
6543
6520
  },
6544
6521
  create(base) {
6545
- return FetchAllFlagsResponse.fromPartial(base !== null && base !== undefined ? base : {});
6522
+ return FetchAllFlagsResponse.fromPartial(base !== null && base !== void 0 ? base : {});
6546
6523
  },
6547
6524
  fromPartial(object) {
6548
6525
  var _a;
6549
6526
  const message = createBaseFetchAllFlagsResponse();
6550
- message.flagConfiguration = (_a = object.flagConfiguration) !== null && _a !== undefined ? _a : "";
6527
+ message.flagConfiguration = (_a = object.flagConfiguration) !== null && _a !== void 0 ? _a : "";
6551
6528
  return message;
6552
6529
  },
6553
6530
  };
@@ -6579,7 +6556,7 @@ const GetMetadataRequest = {
6579
6556
  return obj;
6580
6557
  },
6581
6558
  create(base) {
6582
- return GetMetadataRequest.fromPartial(base !== null && base !== undefined ? base : {});
6559
+ return GetMetadataRequest.fromPartial(base !== null && base !== void 0 ? base : {});
6583
6560
  },
6584
6561
  fromPartial(_) {
6585
6562
  const message = createBaseGetMetadataRequest();
@@ -6628,12 +6605,12 @@ const GetMetadataResponse = {
6628
6605
  return obj;
6629
6606
  },
6630
6607
  create(base) {
6631
- return GetMetadataResponse.fromPartial(base !== null && base !== undefined ? base : {});
6608
+ return GetMetadataResponse.fromPartial(base !== null && base !== void 0 ? base : {});
6632
6609
  },
6633
6610
  fromPartial(object) {
6634
6611
  var _a;
6635
6612
  const message = createBaseGetMetadataResponse();
6636
- message.metadata = (_a = object.metadata) !== null && _a !== undefined ? _a : undefined;
6613
+ message.metadata = (_a = object.metadata) !== null && _a !== void 0 ? _a : undefined;
6637
6614
  return message;
6638
6615
  },
6639
6616
  };
@@ -6656,6 +6633,7 @@ const FlagSyncServiceService = {
6656
6633
  responseSerialize: (value) => Buffer.from(FetchAllFlagsResponse.encode(value).finish()),
6657
6634
  responseDeserialize: (value) => FetchAllFlagsResponse.decode(value),
6658
6635
  },
6636
+ /** @deprecated */
6659
6637
  getMetadata: {
6660
6638
  path: "/flagd.sync.v1.FlagSyncService/GetMetadata",
6661
6639
  requestStream: false,
@@ -6678,7 +6656,8 @@ function isSet(value) {
6678
6656
  * Implements the gRPC sync contract to fetch flag data.
6679
6657
  */
6680
6658
  class GrpcFetch {
6681
- constructor(config, syncServiceClient, logger) {
6659
+ constructor(config, setSyncContext, syncServiceClient, logger) {
6660
+ this._errorThrottled = false;
6682
6661
  /**
6683
6662
  * Initialized will be set to true once the initial connection is successful
6684
6663
  * and the first payload has been received. Subsequent reconnects will not
@@ -6691,63 +6670,88 @@ class GrpcFetch {
6691
6670
  * false if the connection is lost.
6692
6671
  */
6693
6672
  this._isConnected = false;
6694
- const { host, port, tls, socketPath, selector, defaultAuthority } = config;
6695
- let clientOptions;
6696
- if (defaultAuthority) {
6697
- clientOptions = {
6698
- 'grpc.default_authority': defaultAuthority,
6699
- };
6700
- }
6673
+ const { host, port, tls, socketPath, certPath, selector } = config;
6674
+ const clientOptions = buildClientOptions(config);
6675
+ const channelCredentials = createChannelCredentials(tls, certPath);
6701
6676
  this._syncClient = syncServiceClient
6702
6677
  ? syncServiceClient
6703
- : new FlagSyncServiceClient(socketPath ? `unix://${socketPath}` : `${host}:${port}`, tls ? credentials.createSsl() : credentials.createInsecure(), clientOptions);
6678
+ : new FlagSyncServiceClient(socketPath ? `unix://${socketPath}` : `${host}:${port}`, channelCredentials, clientOptions);
6679
+ this._deadlineMs = config.deadlineMs;
6680
+ this._maxBackoffMs = config.retryBackoffMaxMs || DEFAULT_MAX_BACKOFF_MS;
6681
+ this._streamDeadlineMs = config.streamDeadlineMs;
6682
+ this._setSyncContext = setSyncContext;
6704
6683
  this._logger = logger;
6684
+ // For backward compatibility during the deprecation period, we send the selector in both:
6685
+ // 1. The request field (deprecated, for older flagd versions)
6686
+ // 2. The gRPC metadata header 'flagd-selector' (new standard)
6705
6687
  this._request = { providerId: '', selector: selector ? selector : '' };
6688
+ this._fatalStatusCodes = createFatalStatusCodesSet(config.fatalStatusCodes, logger);
6689
+ // Create metadata with the flagd-selector header
6690
+ this._metadata = new Metadata();
6691
+ if (selector) {
6692
+ this._metadata.set(FLAGD_SELECTOR_HEADER, selector);
6693
+ }
6706
6694
  }
6707
6695
  connect(dataCallback, reconnectCallback, changedCallback, disconnectCallback) {
6708
- return __awaiter(this, undefined, undefined, function* () {
6696
+ return __awaiter(this, void 0, void 0, function* () {
6709
6697
  yield new Promise((resolve, reject) => this.listen(dataCallback, reconnectCallback, changedCallback, disconnectCallback, resolve, reject));
6710
6698
  this._initialized = true;
6711
6699
  });
6712
6700
  }
6713
6701
  disconnect() {
6714
- return __awaiter(this, undefined, undefined, function* () {
6702
+ return __awaiter(this, void 0, void 0, function* () {
6715
6703
  var _a;
6716
- (_a = this._logger) === null || _a === undefined ? undefined : _a.debug('Disconnecting gRPC sync connection');
6704
+ (_a = this._logger) === null || _a === void 0 ? void 0 : _a.debug('Disconnecting gRPC sync connection');
6717
6705
  closeStreamIfDefined(this._syncStream);
6718
6706
  this._syncClient.close();
6719
6707
  });
6720
6708
  }
6721
6709
  listen(dataCallback, reconnectCallback, changedCallback, disconnectCallback, resolveConnect, rejectConnect) {
6722
6710
  var _a;
6723
- (_a = this._logger) === null || _a === undefined ? undefined : _a.debug('Starting gRPC sync connection');
6711
+ (_a = this._logger) === null || _a === void 0 ? void 0 : _a.debug('Starting gRPC sync connection');
6724
6712
  closeStreamIfDefined(this._syncStream);
6725
6713
  try {
6726
- this._syncStream = this._syncClient.syncFlags(this._request);
6727
- this._syncStream.on('data', (data) => {
6728
- var _a, _b, _c, _d;
6729
- (_a = this._logger) === null || _a === void 0 ? void 0 : _a.debug(`Received sync payload`);
6730
- try {
6731
- const changes = dataCallback(data.flagConfiguration);
6732
- if (this._initialized && changes.length > 0) {
6733
- changedCallback(changes);
6734
- }
6735
- }
6736
- catch (err) {
6737
- (_b = this._logger) === null || _b === void 0 ? void 0 : _b.debug('Error processing sync payload: ', (_c = err === null || err === void 0 ? void 0 : err.message) !== null && _c !== void 0 ? _c : 'unknown error');
6738
- }
6739
- if (resolveConnect) {
6740
- resolveConnect();
6714
+ // wait for connection to be stable
6715
+ this._syncClient.waitForReady(Date.now() + this._deadlineMs, (err) => {
6716
+ if (err) {
6717
+ this.handleError(err, dataCallback, reconnectCallback, changedCallback, disconnectCallback, rejectConnect);
6741
6718
  }
6742
- else if (!this._isConnected) {
6743
- // Not the first connection and there's no active connection.
6744
- (_d = this._logger) === null || _d === void 0 ? void 0 : _d.debug('Reconnected to gRPC sync');
6745
- reconnectCallback();
6719
+ else {
6720
+ const streamDeadline = this._streamDeadlineMs != 0 ? Date.now() + this._streamDeadlineMs : undefined;
6721
+ const stream = this._syncClient.syncFlags(this._request, this._metadata, { deadline: streamDeadline });
6722
+ stream.on('data', (data) => {
6723
+ var _a, _b, _c, _d;
6724
+ (_a = this._logger) === null || _a === void 0 ? void 0 : _a.debug(`Received sync payload`);
6725
+ try {
6726
+ if (data.syncContext) {
6727
+ this._setSyncContext(data.syncContext);
6728
+ }
6729
+ const changes = dataCallback(data.flagConfiguration);
6730
+ if (this._initialized && changes.length > 0) {
6731
+ changedCallback(changes);
6732
+ }
6733
+ }
6734
+ catch (err) {
6735
+ (_b = this._logger) === null || _b === void 0 ? void 0 : _b.debug('Error processing sync payload: ', (_c = err === null || err === void 0 ? void 0 : err.message) !== null && _c !== void 0 ? _c : 'unknown error');
6736
+ }
6737
+ if (resolveConnect) {
6738
+ resolveConnect();
6739
+ }
6740
+ else if (!this._isConnected) {
6741
+ // Not the first connection and there's no active connection.
6742
+ (_d = this._logger) === null || _d === void 0 ? void 0 : _d.debug('Reconnected to gRPC sync');
6743
+ reconnectCallback();
6744
+ }
6745
+ this._isConnected = true;
6746
+ });
6747
+ stream.on('error', (err) => {
6748
+ // In cases where we get an explicit error status, we add a delay.
6749
+ // This prevents tight loops when errors are returned immediately, typically by intervening proxies like Envoy.
6750
+ this._errorThrottled = true;
6751
+ this.handleError(err, dataCallback, reconnectCallback, changedCallback, disconnectCallback, rejectConnect);
6752
+ });
6753
+ this._syncStream = stream;
6746
6754
  }
6747
- this._isConnected = true;
6748
- });
6749
- this._syncStream.on('error', (err) => {
6750
- this.handleError(err, dataCallback, reconnectCallback, changedCallback, disconnectCallback, rejectConnect);
6751
6755
  });
6752
6756
  }
6753
6757
  catch (err) {
@@ -6756,57 +6760,64 @@ class GrpcFetch {
6756
6760
  }
6757
6761
  handleError(err, dataCallback, reconnectCallback, changedCallback, disconnectCallback, rejectConnect) {
6758
6762
  var _a, _b, _c;
6759
- (_a = this._logger) === null || _a === undefined ? undefined : _a.error('Connection error, attempting to reconnect');
6760
- (_b = this._logger) === null || _b === undefined ? undefined : _b.debug(err);
6763
+ // Check if error is a fatal status code on first connection only
6764
+ if (isFatalStatusCodeError(err, this._initialized, this._fatalStatusCodes)) {
6765
+ this._isConnected = false;
6766
+ handleFatalStatusCodeError(err, this._logger, disconnectCallback, rejectConnect);
6767
+ return;
6768
+ }
6769
+ (_a = this._logger) === null || _a === void 0 ? void 0 : _a.error('Connection error, attempting to reconnect');
6770
+ (_b = this._logger) === null || _b === void 0 ? void 0 : _b.debug(err);
6761
6771
  this._isConnected = false;
6762
- const errorMessage = (_c = err === null || err === undefined ? undefined : err.message) !== null && _c !== undefined ? _c : 'Failed to connect to syncFlags stream';
6772
+ const errorMessage = (_c = err === null || err === void 0 ? void 0 : err.message) !== null && _c !== void 0 ? _c : 'Failed to connect to syncFlags stream';
6763
6773
  disconnectCallback(errorMessage);
6764
- rejectConnect === null || rejectConnect === undefined ? undefined : rejectConnect(new GeneralError(errorMessage));
6774
+ rejectConnect === null || rejectConnect === void 0 ? void 0 : rejectConnect(new GeneralError(errorMessage));
6765
6775
  this.reconnect(dataCallback, reconnectCallback, changedCallback, disconnectCallback);
6766
6776
  }
6767
6777
  reconnect(dataCallback, reconnectCallback, changedCallback, disconnectCallback) {
6768
- const channel = this._syncClient.getChannel();
6769
- channel.watchConnectivityState(channel.getConnectivityState(true), Infinity, () => {
6770
- this.listen(dataCallback, reconnectCallback, changedCallback, disconnectCallback);
6771
- });
6778
+ setTimeout(() => this.listen(dataCallback, reconnectCallback, changedCallback, disconnectCallback), this._errorThrottled ? this._maxBackoffMs : 0);
6779
+ this._errorThrottled = false;
6772
6780
  }
6773
6781
  }
6774
6782
 
6775
6783
  class InProcessService {
6776
- constructor(config, dataFetcher, logger) {
6784
+ constructor(config, setSyncContext, dataFetcher, logger) {
6777
6785
  this.config = config;
6778
6786
  this._flagdCore = new FlagdCore(undefined, logger);
6779
6787
  this._dataFetcher = dataFetcher
6780
6788
  ? dataFetcher
6781
6789
  : config.offlineFlagSourcePath
6782
6790
  ? new FileFetch(config.offlineFlagSourcePath, logger)
6783
- : new GrpcFetch(config, undefined, logger);
6791
+ : new GrpcFetch(config, setSyncContext, undefined, logger);
6792
+ }
6793
+ clearCache() {
6794
+ // in-process service does not cache flags
6784
6795
  }
6785
6796
  connect(reconnectCallback, changedCallback, disconnectCallback) {
6786
6797
  return this._dataFetcher.connect(this.setFlagConfiguration.bind(this), reconnectCallback, changedCallback, disconnectCallback);
6787
6798
  }
6788
6799
  disconnect() {
6789
- return __awaiter(this, undefined, undefined, function* () {
6800
+ return __awaiter(this, void 0, void 0, function* () {
6790
6801
  this._dataFetcher.disconnect();
6791
6802
  });
6792
6803
  }
6793
6804
  resolveBoolean(flagKey, defaultValue, context, logger) {
6794
- return __awaiter(this, undefined, undefined, function* () {
6805
+ return __awaiter(this, void 0, void 0, function* () {
6795
6806
  return this.evaluate('boolean', flagKey, defaultValue, context, logger);
6796
6807
  });
6797
6808
  }
6798
6809
  resolveNumber(flagKey, defaultValue, context, logger) {
6799
- return __awaiter(this, undefined, undefined, function* () {
6810
+ return __awaiter(this, void 0, void 0, function* () {
6800
6811
  return this.evaluate('number', flagKey, defaultValue, context, logger);
6801
6812
  });
6802
6813
  }
6803
6814
  resolveString(flagKey, defaultValue, context, logger) {
6804
- return __awaiter(this, undefined, undefined, function* () {
6815
+ return __awaiter(this, void 0, void 0, function* () {
6805
6816
  return this.evaluate('string', flagKey, defaultValue, context, logger);
6806
6817
  });
6807
6818
  }
6808
6819
  resolveObject(flagKey, defaultValue, context, logger) {
6809
- return __awaiter(this, undefined, undefined, function* () {
6820
+ return __awaiter(this, void 0, void 0, function* () {
6810
6821
  return this.evaluate('object', flagKey, defaultValue, context, logger);
6811
6822
  });
6812
6823
  }
@@ -6829,6 +6840,15 @@ class InProcessService {
6829
6840
  }
6830
6841
  }
6831
6842
 
6843
+ class SyncMetadataHook {
6844
+ constructor(enrichedContext) {
6845
+ this.enrichedContext = enrichedContext;
6846
+ }
6847
+ before(hookContext, hookHints) {
6848
+ return this.enrichedContext();
6849
+ }
6850
+ }
6851
+
6832
6852
  class FlagdProvider {
6833
6853
  /**
6834
6854
  * Construct a new flagd provider.
@@ -6838,36 +6858,58 @@ class FlagdProvider {
6838
6858
  * @param service optional internal service implementation, should not be needed for production
6839
6859
  */
6840
6860
  constructor(options, logger, service) {
6861
+ var _a;
6841
6862
  this.logger = logger;
6842
6863
  this.metadata = {
6843
6864
  name: 'flagd',
6844
6865
  };
6845
6866
  this.runsOn = 'server';
6846
6867
  this.events = new OpenFeatureEventEmitter();
6868
+ this.syncContext = null;
6869
+ this._isErrorState = false;
6847
6870
  const config = getConfig(options);
6848
- this._service = service
6849
- ? service
6850
- : config.resolverType === 'in-process'
6851
- ? new InProcessService(config, undefined, logger)
6852
- : new GRPCService(config, undefined, logger);
6871
+ this._retryGracePeriod = (_a = config.retryGracePeriod) !== null && _a !== void 0 ? _a : DEFAULT_RETRY_GRACE_PERIOD;
6872
+ if (service === undefined) {
6873
+ if (config.resolverType === 'in-process') {
6874
+ this._service = new InProcessService(config, this.setSyncContext.bind(this), undefined, logger);
6875
+ if ((config === null || config === void 0 ? void 0 : config.offlineFlagSourcePath) === undefined) {
6876
+ this.hooks = [new SyncMetadataHook(() => config.contextEnricher(this.getSyncContext()))];
6877
+ }
6878
+ }
6879
+ else {
6880
+ this._service = new GRPCService(config, undefined, logger);
6881
+ }
6882
+ }
6883
+ else {
6884
+ this._service = service;
6885
+ }
6886
+ }
6887
+ setSyncContext(context) {
6888
+ this.syncContext = context;
6889
+ }
6890
+ getSyncContext() {
6891
+ return this.syncContext;
6853
6892
  }
6854
6893
  initialize() {
6855
- return __awaiter(this, undefined, undefined, function* () {
6894
+ return __awaiter(this, void 0, void 0, function* () {
6856
6895
  var _a, _b, _c;
6857
6896
  try {
6858
6897
  yield this._service.connect(this.handleReconnect.bind(this), this.handleChanged.bind(this), this.handleError.bind(this));
6898
+ this.clearErrorTimer();
6899
+ this._isErrorState = false;
6859
6900
  (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`${this.metadata.name}: ready`);
6860
6901
  }
6861
6902
  catch (err) {
6862
- (_b = this.logger) === null || _b === undefined ? undefined : _b.error(`${this.metadata.name}: error during initialization: ${err === null || err === undefined ? undefined : err.message}`);
6863
- (_c = this.logger) === null || _c === undefined ? undefined : _c.debug(err);
6903
+ (_b = this.logger) === null || _b === void 0 ? void 0 : _b.error(`${this.metadata.name}: error during initialization: ${err === null || err === void 0 ? void 0 : err.message}`);
6904
+ (_c = this.logger) === null || _c === void 0 ? void 0 : _c.debug(err);
6864
6905
  throw err;
6865
6906
  }
6866
6907
  });
6867
6908
  }
6868
6909
  onClose() {
6869
6910
  var _a;
6870
- (_a = this.logger) === null || _a === undefined ? undefined : _a.debug(`${this.metadata.name}: shutting down`);
6911
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`${this.metadata.name}: shutting down`);
6912
+ this.clearErrorTimer();
6871
6913
  return this._service.disconnect();
6872
6914
  }
6873
6915
  resolveBooleanEvaluation(flagKey, defaultValue, transformedContext, logger) {
@@ -6883,14 +6925,35 @@ class FlagdProvider {
6883
6925
  return this._service.resolveObject(flagKey, defaultValue, transformedContext, logger);
6884
6926
  }
6885
6927
  handleReconnect() {
6928
+ this.clearErrorTimer();
6929
+ this._isErrorState = false;
6886
6930
  this.events.emit(ProviderEvents.Ready);
6887
6931
  }
6888
6932
  handleError(message) {
6889
- this.events.emit(ProviderEvents.Error, { message });
6933
+ if (this._isErrorState) {
6934
+ return;
6935
+ }
6936
+ this._isErrorState = true;
6937
+ this.events.emit(ProviderEvents.Stale, { message });
6938
+ this._errorTimer = setTimeout(() => {
6939
+ var _a;
6940
+ if (this._isErrorState) {
6941
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.error(`${this.metadata.name}: not reconnected within ${this._retryGracePeriod}s grace period, emitting ERROR`);
6942
+ this._service.clearCache();
6943
+ this.events.emit(ProviderEvents.Error, { message });
6944
+ }
6945
+ this._errorTimer = undefined;
6946
+ }, this._retryGracePeriod * 1000);
6890
6947
  }
6891
6948
  handleChanged(flagsChanged) {
6892
6949
  this.events.emit(ProviderEvents.ConfigurationChanged, { flagsChanged });
6893
6950
  }
6951
+ clearErrorTimer() {
6952
+ if (this._errorTimer) {
6953
+ clearTimeout(this._errorTimer);
6954
+ this._errorTimer = undefined;
6955
+ }
6956
+ }
6894
6957
  }
6895
6958
 
6896
6959
  export { FlagdProvider };