posthog-node 5.18.0 → 5.18.1
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/dist/extensions/feature-flags/feature-flags.d.ts +11 -0
- package/dist/extensions/feature-flags/feature-flags.d.ts.map +1 -1
- package/dist/extensions/feature-flags/feature-flags.js +16 -10
- package/dist/extensions/feature-flags/feature-flags.mjs +16 -10
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +1 -1
- package/src/extensions/feature-flags/feature-flags.ts +35 -10
- package/src/version.ts +1 -1
|
@@ -48,6 +48,7 @@ declare class FeatureFlagsPoller {
|
|
|
48
48
|
private cacheProvider?;
|
|
49
49
|
private loadingPromise?;
|
|
50
50
|
private flagsEtag?;
|
|
51
|
+
private nextFetchAllowedAt?;
|
|
51
52
|
constructor({ pollingInterval, personalApiKey, projectApiKey, timeout, host, customHeaders, ...options }: FeatureFlagsPollerOptions);
|
|
52
53
|
debug(enabled?: boolean): void;
|
|
53
54
|
private logMsgIfDebug;
|
|
@@ -95,6 +96,16 @@ declare class FeatureFlagsPoller {
|
|
|
95
96
|
* @returns The polling interval to use for the next request.
|
|
96
97
|
*/
|
|
97
98
|
private getPollingInterval;
|
|
99
|
+
/**
|
|
100
|
+
* Enter backoff state after receiving an error response (401, 403, 429).
|
|
101
|
+
* This enables exponential backoff for the poller and blocks on-demand fetches.
|
|
102
|
+
*/
|
|
103
|
+
private beginBackoff;
|
|
104
|
+
/**
|
|
105
|
+
* Clear backoff state after a successful response (200, 304).
|
|
106
|
+
* This resets the polling interval and allows on-demand fetches immediately.
|
|
107
|
+
*/
|
|
108
|
+
private clearBackoff;
|
|
98
109
|
_loadFeatureFlags(): Promise<void>;
|
|
99
110
|
private getPersonalApiKeyRequestOptions;
|
|
100
111
|
_requestFeatureFlagDefinitions(): Promise<PostHogFetchResponse>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fe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wBAAwB,EACxB,WAAW,GACZ,CAAA"}
|
|
@@ -296,6 +296,7 @@ class FeatureFlagsPoller {
|
|
|
296
296
|
}
|
|
297
297
|
async loadFeatureFlags(forceReload = false) {
|
|
298
298
|
if (this.loadedSuccessfullyOnce && !forceReload) return;
|
|
299
|
+
if (!forceReload && this.nextFetchAllowedAt && Date.now() < this.nextFetchAllowedAt) return void this.logMsgIfDebug(()=>console.debug('[FEATURE FLAGS] Skipping fetch, in backoff period'));
|
|
299
300
|
if (!this.loadingPromise) this.loadingPromise = this._loadFeatureFlags().catch((err)=>this.logMsgIfDebug(()=>console.debug(`[FEATURE FLAGS] Failed to load feature flags: ${err}`))).finally(()=>{
|
|
300
301
|
this.loadingPromise = void 0;
|
|
301
302
|
});
|
|
@@ -308,6 +309,16 @@ class FeatureFlagsPoller {
|
|
|
308
309
|
if (!this.shouldBeginExponentialBackoff) return this.pollingInterval;
|
|
309
310
|
return Math.min(SIXTY_SECONDS, this.pollingInterval * 2 ** this.backOffCount);
|
|
310
311
|
}
|
|
312
|
+
beginBackoff() {
|
|
313
|
+
this.shouldBeginExponentialBackoff = true;
|
|
314
|
+
this.backOffCount += 1;
|
|
315
|
+
this.nextFetchAllowedAt = Date.now() + this.getPollingInterval();
|
|
316
|
+
}
|
|
317
|
+
clearBackoff() {
|
|
318
|
+
this.shouldBeginExponentialBackoff = false;
|
|
319
|
+
this.backOffCount = 0;
|
|
320
|
+
this.nextFetchAllowedAt = void 0;
|
|
321
|
+
}
|
|
311
322
|
async _loadFeatureFlags() {
|
|
312
323
|
if (this.poller) {
|
|
313
324
|
clearTimeout(this.poller);
|
|
@@ -333,12 +344,10 @@ class FeatureFlagsPoller {
|
|
|
333
344
|
this.logMsgIfDebug(()=>console.debug('[FEATURE FLAGS] Flags not modified (304), using cached data'));
|
|
334
345
|
this.flagsEtag = res.headers?.get('ETag') ?? this.flagsEtag;
|
|
335
346
|
this.loadedSuccessfullyOnce = true;
|
|
336
|
-
this.
|
|
337
|
-
this.backOffCount = 0;
|
|
347
|
+
this.clearBackoff();
|
|
338
348
|
return;
|
|
339
349
|
case 401:
|
|
340
|
-
this.
|
|
341
|
-
this.backOffCount += 1;
|
|
350
|
+
this.beginBackoff();
|
|
342
351
|
throw new ClientError(`Your project key or personal API key is invalid. Setting next polling interval to ${this.getPollingInterval()}ms. More information: https://posthog.com/docs/api#rate-limiting`);
|
|
343
352
|
case 402:
|
|
344
353
|
console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - unsetting all local flags. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
|
|
@@ -348,12 +357,10 @@ class FeatureFlagsPoller {
|
|
|
348
357
|
this.cohorts = {};
|
|
349
358
|
return;
|
|
350
359
|
case 403:
|
|
351
|
-
this.
|
|
352
|
-
this.backOffCount += 1;
|
|
360
|
+
this.beginBackoff();
|
|
353
361
|
throw new ClientError(`Your personal API key does not have permission to fetch feature flag definitions for local evaluation. Setting next polling interval to ${this.getPollingInterval()}ms. Are you sure you're using the correct personal and Project API key pair? More information: https://posthog.com/docs/api/overview`);
|
|
354
362
|
case 429:
|
|
355
|
-
this.
|
|
356
|
-
this.backOffCount += 1;
|
|
363
|
+
this.beginBackoff();
|
|
357
364
|
throw new ClientError(`You are being rate limited. Setting next polling interval to ${this.getPollingInterval()}ms. More information: https://posthog.com/docs/api#rate-limiting`);
|
|
358
365
|
case 200:
|
|
359
366
|
{
|
|
@@ -366,8 +373,7 @@ class FeatureFlagsPoller {
|
|
|
366
373
|
cohorts: responseJson.cohorts || {}
|
|
367
374
|
};
|
|
368
375
|
this.updateFlagState(flagData);
|
|
369
|
-
this.
|
|
370
|
-
this.backOffCount = 0;
|
|
376
|
+
this.clearBackoff();
|
|
371
377
|
if (this.cacheProvider && shouldFetch) try {
|
|
372
378
|
await this.cacheProvider.onFlagDefinitionsReceived(flagData);
|
|
373
379
|
} catch (err) {
|
|
@@ -263,6 +263,7 @@ class FeatureFlagsPoller {
|
|
|
263
263
|
}
|
|
264
264
|
async loadFeatureFlags(forceReload = false) {
|
|
265
265
|
if (this.loadedSuccessfullyOnce && !forceReload) return;
|
|
266
|
+
if (!forceReload && this.nextFetchAllowedAt && Date.now() < this.nextFetchAllowedAt) return void this.logMsgIfDebug(()=>console.debug('[FEATURE FLAGS] Skipping fetch, in backoff period'));
|
|
266
267
|
if (!this.loadingPromise) this.loadingPromise = this._loadFeatureFlags().catch((err)=>this.logMsgIfDebug(()=>console.debug(`[FEATURE FLAGS] Failed to load feature flags: ${err}`))).finally(()=>{
|
|
267
268
|
this.loadingPromise = void 0;
|
|
268
269
|
});
|
|
@@ -275,6 +276,16 @@ class FeatureFlagsPoller {
|
|
|
275
276
|
if (!this.shouldBeginExponentialBackoff) return this.pollingInterval;
|
|
276
277
|
return Math.min(SIXTY_SECONDS, this.pollingInterval * 2 ** this.backOffCount);
|
|
277
278
|
}
|
|
279
|
+
beginBackoff() {
|
|
280
|
+
this.shouldBeginExponentialBackoff = true;
|
|
281
|
+
this.backOffCount += 1;
|
|
282
|
+
this.nextFetchAllowedAt = Date.now() + this.getPollingInterval();
|
|
283
|
+
}
|
|
284
|
+
clearBackoff() {
|
|
285
|
+
this.shouldBeginExponentialBackoff = false;
|
|
286
|
+
this.backOffCount = 0;
|
|
287
|
+
this.nextFetchAllowedAt = void 0;
|
|
288
|
+
}
|
|
278
289
|
async _loadFeatureFlags() {
|
|
279
290
|
if (this.poller) {
|
|
280
291
|
clearTimeout(this.poller);
|
|
@@ -300,12 +311,10 @@ class FeatureFlagsPoller {
|
|
|
300
311
|
this.logMsgIfDebug(()=>console.debug('[FEATURE FLAGS] Flags not modified (304), using cached data'));
|
|
301
312
|
this.flagsEtag = res.headers?.get('ETag') ?? this.flagsEtag;
|
|
302
313
|
this.loadedSuccessfullyOnce = true;
|
|
303
|
-
this.
|
|
304
|
-
this.backOffCount = 0;
|
|
314
|
+
this.clearBackoff();
|
|
305
315
|
return;
|
|
306
316
|
case 401:
|
|
307
|
-
this.
|
|
308
|
-
this.backOffCount += 1;
|
|
317
|
+
this.beginBackoff();
|
|
309
318
|
throw new ClientError(`Your project key or personal API key is invalid. Setting next polling interval to ${this.getPollingInterval()}ms. More information: https://posthog.com/docs/api#rate-limiting`);
|
|
310
319
|
case 402:
|
|
311
320
|
console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - unsetting all local flags. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
|
|
@@ -315,12 +324,10 @@ class FeatureFlagsPoller {
|
|
|
315
324
|
this.cohorts = {};
|
|
316
325
|
return;
|
|
317
326
|
case 403:
|
|
318
|
-
this.
|
|
319
|
-
this.backOffCount += 1;
|
|
327
|
+
this.beginBackoff();
|
|
320
328
|
throw new ClientError(`Your personal API key does not have permission to fetch feature flag definitions for local evaluation. Setting next polling interval to ${this.getPollingInterval()}ms. Are you sure you're using the correct personal and Project API key pair? More information: https://posthog.com/docs/api/overview`);
|
|
321
329
|
case 429:
|
|
322
|
-
this.
|
|
323
|
-
this.backOffCount += 1;
|
|
330
|
+
this.beginBackoff();
|
|
324
331
|
throw new ClientError(`You are being rate limited. Setting next polling interval to ${this.getPollingInterval()}ms. More information: https://posthog.com/docs/api#rate-limiting`);
|
|
325
332
|
case 200:
|
|
326
333
|
{
|
|
@@ -333,8 +340,7 @@ class FeatureFlagsPoller {
|
|
|
333
340
|
cohorts: responseJson.cohorts || {}
|
|
334
341
|
};
|
|
335
342
|
this.updateFlagState(flagData);
|
|
336
|
-
this.
|
|
337
|
-
this.backOffCount = 0;
|
|
343
|
+
this.clearBackoff();
|
|
338
344
|
if (this.cacheProvider && shouldFetch) try {
|
|
339
345
|
await this.cacheProvider.onFlagDefinitionsReceived(flagData);
|
|
340
346
|
} catch (err) {
|
package/dist/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const version = "5.18.
|
|
1
|
+
export declare const version = "5.18.1";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/version.js
CHANGED
|
@@ -26,7 +26,7 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
|
27
27
|
version: ()=>version
|
|
28
28
|
});
|
|
29
|
-
const version = '5.18.
|
|
29
|
+
const version = '5.18.1';
|
|
30
30
|
exports.version = __webpack_exports__.version;
|
|
31
31
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
32
32
|
"version"
|
package/dist/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = '5.18.
|
|
1
|
+
const version = '5.18.1';
|
|
2
2
|
export { version };
|
package/package.json
CHANGED
|
@@ -79,6 +79,7 @@ class FeatureFlagsPoller {
|
|
|
79
79
|
private cacheProvider?: FlagDefinitionCacheProvider
|
|
80
80
|
private loadingPromise?: Promise<void>
|
|
81
81
|
private flagsEtag?: string
|
|
82
|
+
private nextFetchAllowedAt?: number
|
|
82
83
|
|
|
83
84
|
constructor({
|
|
84
85
|
pollingInterval,
|
|
@@ -586,6 +587,13 @@ class FeatureFlagsPoller {
|
|
|
586
587
|
return
|
|
587
588
|
}
|
|
588
589
|
|
|
590
|
+
// Respect backoff for on-demand fetches (e.g., from getFeatureFlag calls).
|
|
591
|
+
// The poller uses forceReload=true and has already waited the backoff period.
|
|
592
|
+
if (!forceReload && this.nextFetchAllowedAt && Date.now() < this.nextFetchAllowedAt) {
|
|
593
|
+
this.logMsgIfDebug(() => console.debug('[FEATURE FLAGS] Skipping fetch, in backoff period'))
|
|
594
|
+
return
|
|
595
|
+
}
|
|
596
|
+
|
|
589
597
|
if (!this.loadingPromise) {
|
|
590
598
|
this.loadingPromise = this._loadFeatureFlags()
|
|
591
599
|
.catch((err) => this.logMsgIfDebug(() => console.debug(`[FEATURE FLAGS] Failed to load feature flags: ${err}`)))
|
|
@@ -619,6 +627,28 @@ class FeatureFlagsPoller {
|
|
|
619
627
|
return Math.min(SIXTY_SECONDS, this.pollingInterval * 2 ** this.backOffCount)
|
|
620
628
|
}
|
|
621
629
|
|
|
630
|
+
/**
|
|
631
|
+
* Enter backoff state after receiving an error response (401, 403, 429).
|
|
632
|
+
* This enables exponential backoff for the poller and blocks on-demand fetches.
|
|
633
|
+
*/
|
|
634
|
+
private beginBackoff(): void {
|
|
635
|
+
this.shouldBeginExponentialBackoff = true
|
|
636
|
+
this.backOffCount += 1
|
|
637
|
+
// Use the same backoff interval as the poller to avoid overwhelming
|
|
638
|
+
// the server with on-demand requests while polling is backed off.
|
|
639
|
+
this.nextFetchAllowedAt = Date.now() + this.getPollingInterval()
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Clear backoff state after a successful response (200, 304).
|
|
644
|
+
* This resets the polling interval and allows on-demand fetches immediately.
|
|
645
|
+
*/
|
|
646
|
+
private clearBackoff(): void {
|
|
647
|
+
this.shouldBeginExponentialBackoff = false
|
|
648
|
+
this.backOffCount = 0
|
|
649
|
+
this.nextFetchAllowedAt = undefined
|
|
650
|
+
}
|
|
651
|
+
|
|
622
652
|
async _loadFeatureFlags(): Promise<void> {
|
|
623
653
|
if (this.poller) {
|
|
624
654
|
clearTimeout(this.poller)
|
|
@@ -690,14 +720,12 @@ class FeatureFlagsPoller {
|
|
|
690
720
|
// Update ETag if server sent one (304 can include updated ETag per HTTP spec)
|
|
691
721
|
this.flagsEtag = res.headers?.get('ETag') ?? this.flagsEtag
|
|
692
722
|
this.loadedSuccessfullyOnce = true
|
|
693
|
-
this.
|
|
694
|
-
this.backOffCount = 0
|
|
723
|
+
this.clearBackoff()
|
|
695
724
|
return
|
|
696
725
|
|
|
697
726
|
case 401:
|
|
698
727
|
// Invalid API key
|
|
699
|
-
this.
|
|
700
|
-
this.backOffCount += 1
|
|
728
|
+
this.beginBackoff()
|
|
701
729
|
throw new ClientError(
|
|
702
730
|
`Your project key or personal API key is invalid. Setting next polling interval to ${this.getPollingInterval()}ms. More information: https://posthog.com/docs/api#rate-limiting`
|
|
703
731
|
)
|
|
@@ -715,16 +743,14 @@ class FeatureFlagsPoller {
|
|
|
715
743
|
|
|
716
744
|
case 403:
|
|
717
745
|
// Permissions issue
|
|
718
|
-
this.
|
|
719
|
-
this.backOffCount += 1
|
|
746
|
+
this.beginBackoff()
|
|
720
747
|
throw new ClientError(
|
|
721
748
|
`Your personal API key does not have permission to fetch feature flag definitions for local evaluation. Setting next polling interval to ${this.getPollingInterval()}ms. Are you sure you're using the correct personal and Project API key pair? More information: https://posthog.com/docs/api/overview`
|
|
722
749
|
)
|
|
723
750
|
|
|
724
751
|
case 429:
|
|
725
752
|
// Rate limited
|
|
726
|
-
this.
|
|
727
|
-
this.backOffCount += 1
|
|
753
|
+
this.beginBackoff()
|
|
728
754
|
throw new ClientError(
|
|
729
755
|
`You are being rate limited. Setting next polling interval to ${this.getPollingInterval()}ms. More information: https://posthog.com/docs/api#rate-limiting`
|
|
730
756
|
)
|
|
@@ -748,8 +774,7 @@ class FeatureFlagsPoller {
|
|
|
748
774
|
}
|
|
749
775
|
|
|
750
776
|
this.updateFlagState(flagData)
|
|
751
|
-
this.
|
|
752
|
-
this.backOffCount = 0
|
|
777
|
+
this.clearBackoff()
|
|
753
778
|
|
|
754
779
|
if (this.cacheProvider && shouldFetch) {
|
|
755
780
|
// Only notify the cache if it's actually expecting new data
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '5.18.
|
|
1
|
+
export const version = '5.18.1'
|