Skip to content

Commit 1ded639

Browse files
Merge pull request #404 from splitio/refactor_checkIfServerSide_util
Refactor: add `checkIfServerSide` utility function
2 parents e5f6291 + 274fbd6 commit 1ded639

File tree

11 files changed

+25
-16
lines changed

11 files changed

+25
-16
lines changed

src/evaluator/fallbackTreatmentsCalculator/constants.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { Treatment, TreatmentWithConfig } from '../../../../types/splitio';
22
import { ILogger } from '../../../logger/types';
33
import { isObject, isString } from '../../../utils/lang';
4-
import { FallbackDiscardReason } from '../constants';
54

5+
enum FallbackDiscardReason {
6+
FlagName = 'Invalid flag name (max 100 chars, no spaces)',
7+
Treatment = 'Invalid treatment (max 100 chars and must match pattern)',
8+
}
69

710
export class FallbacksSanitizer {
811

src/logger/messages/info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const codesInfo: [number, string][] = codesWarn.concat([
2222
[c.POLLING_SMART_PAUSING, c.LOG_PREFIX_SYNC_POLLING + 'Turning segments data polling %s.'],
2323
[c.POLLING_START, c.LOG_PREFIX_SYNC_POLLING + 'Starting polling'],
2424
[c.POLLING_STOP, c.LOG_PREFIX_SYNC_POLLING + 'Stopping polling'],
25-
[c.SYNC_SPLITS_FETCH_RETRY, c.LOG_PREFIX_SYNC_SPLITS + 'Retrying download of feature flags #%s. Reason: %s'],
25+
[c.SYNC_SPLITS_FETCH_RETRY, c.LOG_PREFIX_SYNC_SPLITS + 'Retrying fetch of feature flags (attempt #%s). Reason: %s'],
2626
[c.SUBMITTERS_PUSH_FULL_QUEUE, c.LOG_PREFIX_SYNC_SUBMITTERS + 'Flushing full %s queue and resetting timer.'],
2727
[c.SUBMITTERS_PUSH, c.LOG_PREFIX_SYNC_SUBMITTERS + 'Pushing %s.'],
2828
[c.SUBMITTERS_PUSH_PAGE_HIDDEN, c.LOG_PREFIX_SYNC_SUBMITTERS + 'Flushing %s because page became hidden.'],

src/logger/messages/warn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const codesWarn: [number, string][] = codesError.concat([
66
[c.ENGINE_VALUE_INVALID, c.LOG_PREFIX_ENGINE_VALUE + 'Value %s doesn\'t match with expected type.'],
77
[c.ENGINE_VALUE_NO_ATTRIBUTES, c.LOG_PREFIX_ENGINE_VALUE + 'Defined attribute `%s`. No attributes received.'],
88
// synchronizer
9-
[c.SYNC_MYSEGMENTS_FETCH_RETRY, c.LOG_PREFIX_SYNC_MYSEGMENTS + 'Retrying download of segments #%s. Reason: %s'],
9+
[c.SYNC_MYSEGMENTS_FETCH_RETRY, c.LOG_PREFIX_SYNC_MYSEGMENTS + 'Retrying fetch of memberships (attempt #%s). Reason: %s'],
1010
[c.SYNC_SPLITS_FETCH_FAILS, c.LOG_PREFIX_SYNC_SPLITS + 'Error while doing fetch of feature flags. %s'],
1111
[c.STREAMING_PARSING_ERROR_FAILS, c.LOG_PREFIX_SYNC_STREAMING + 'Error parsing SSE error notification: %s'],
1212
[c.STREAMING_PARSING_MESSAGE_FAILS, c.LOG_PREFIX_SYNC_STREAMING + 'Error parsing SSE message notification: %s'],

src/storages/inMemory/TelemetryCacheInMemory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ImpressionDataType, EventDataType, LastSync, HttpErrors, HttpLatencies, StreamingEvent, Method, OperationType, MethodExceptions, MethodLatencies, TelemetryUsageStatsPayload, UpdatesFromSSEEnum, UpdatesFromSSE } from '../../sync/submitters/types';
22
import { DEDUPED, DROPPED, LOCALHOST_MODE, QUEUED } from '../../utils/constants';
3+
import { checkIfServerSide } from '../../utils/key';
34
import { findLatencyIndex } from '../findLatencyIndex';
45
import { ISegmentsCacheSync, ISplitsCacheSync, IStorageFactoryParams, ITelemetryCacheSync } from '../types';
56

@@ -20,7 +21,7 @@ const ACCEPTANCE_RANGE = 0.001;
2021
* All factory instances track telemetry on server-side, and 0.1% on client-side.
2122
*/
2223
export function shouldRecordTelemetry({ settings }: IStorageFactoryParams) {
23-
return settings.mode !== LOCALHOST_MODE && (settings.core.key === undefined || Math.random() <= ACCEPTANCE_RANGE);
24+
return settings.mode !== LOCALHOST_MODE && (checkIfServerSide(settings) || Math.random() <= ACCEPTANCE_RANGE);
2425
}
2526

2627
export class TelemetryCacheInMemory implements ITelemetryCacheSync {

src/storages/pluggable/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { UniqueKeysCacheInMemoryCS } from '../inMemory/UniqueKeysCacheInMemoryCS
2121
import { metadataBuilder } from '../utils';
2222
import { LOG_PREFIX } from '../pluggable/constants';
2323
import { RBSegmentsCachePluggable } from './RBSegmentsCachePluggable';
24+
import { checkIfServerSide } from '../../utils/key';
2425

2526
const NO_VALID_WRAPPER = 'Expecting pluggable storage `wrapper` in options, but no valid wrapper instance was provided.';
2627
const NO_VALID_WRAPPER_INTERFACE = 'The provided wrapper instance doesn’t follow the expected interface. Check our docs.';
@@ -83,7 +84,7 @@ export function PluggableStorage(options: PluggableStorageOptions): IStorageAsyn
8384
new ImpressionCountsCachePluggable(log, keys.buildImpressionsCountKey(), wrapper);
8485

8586
const uniqueKeysCache = isPartialConsumer ?
86-
settings.core.key === undefined ? new UniqueKeysCacheInMemory() : new UniqueKeysCacheInMemoryCS() :
87+
checkIfServerSide(settings) ? new UniqueKeysCacheInMemory() : new UniqueKeysCacheInMemoryCS() :
8788
new UniqueKeysCachePluggable(log, keys.buildUniqueKeysKey(), wrapper);
8889

8990
// Connects to wrapper and emits SDK_READY event on main client

src/sync/polling/fetchers/splitChangesFetcher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { FLAG_SPEC_VERSION } from '../../../utils/constants';
66
import { base } from '../../../utils/settingsValidation';
77
import { ISplitChangesFetcher } from './types';
88
import { LOG_PREFIX_SYNC_SPLITS } from '../../../logger/constants';
9+
import { checkIfServerSide } from '../../../utils/key';
910

1011
const PROXY_CHECK_INTERVAL_MILLIS_CS = 60 * 60 * 1000; // 1 hour in Client Side
1112
const PROXY_CHECK_INTERVAL_MILLIS_SS = 24 * PROXY_CHECK_INTERVAL_MILLIS_CS; // 24 hours in Server Side
@@ -22,7 +23,7 @@ function sdkEndpointOverridden(settings: ISettings) {
2223
export function splitChangesFetcherFactory(fetchSplitChanges: IFetchSplitChanges, settings: ISettings, storage: Pick<IStorageBase, 'splits' | 'rbSegments'>): ISplitChangesFetcher {
2324

2425
const log = settings.log;
25-
const PROXY_CHECK_INTERVAL_MILLIS = settings.core.key !== undefined ? PROXY_CHECK_INTERVAL_MILLIS_CS : PROXY_CHECK_INTERVAL_MILLIS_SS;
26+
const PROXY_CHECK_INTERVAL_MILLIS = checkIfServerSide(settings) ? PROXY_CHECK_INTERVAL_MILLIS_SS : PROXY_CHECK_INTERVAL_MILLIS_CS;
2627
let lastProxyCheckTimestamp: number | undefined;
2728

2829
return function splitChangesFetcher(

src/sync/streaming/SSEClient/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { IPlatform } from '../../../sdkFactory/types';
22
import { decorateHeaders } from '../../../services/decorateHeaders';
33
import { IEventSourceConstructor } from '../../../services/types';
44
import { ISettings } from '../../../types';
5+
import { checkIfServerSide } from '../../../utils/key';
56
import { isString } from '../../../utils/lang';
67
import { objectAssign } from '../../../utils/lang/objectAssign';
78
import { IAuthTokenPushEnabled } from '../AuthClient/types';
@@ -73,7 +74,7 @@ export class SSEClient implements ISSEClient {
7374
return encodeURIComponent(params + channel);
7475
}).join(',');
7576
const url = `${this.settings.urls.streaming}/sse?channels=${channelsQueryParam}&accessToken=${authToken.token}&v=${ABLY_API_VERSION}&heartbeats=true`; // same results using `&heartbeats=false`
76-
const isServerSide = !this.settings.core.key;
77+
const isServerSide = checkIfServerSide(this.settings);
7778

7879
this.connection = new this.eventSource!(
7980
// For client-side SDKs, metadata is passed as query param to avoid CORS issues and because native EventSource implementations in browsers do not support headers

src/sync/streaming/pushManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { SplitsUpdateWorker } from './UpdateWorkers/SplitsUpdateWorker';
1010
import { authenticateFactory, hashUserKey } from './AuthClient';
1111
import { forOwn } from '../../utils/lang';
1212
import { SSEClient } from './SSEClient';
13-
import { getMatching } from '../../utils/key';
13+
import { checkIfServerSide, getMatching } from '../../utils/key';
1414
import { MEMBERSHIPS_MS_UPDATE, MEMBERSHIPS_LS_UPDATE, PUSH_NONRETRYABLE_ERROR, PUSH_SUBSYSTEM_DOWN, SECONDS_BEFORE_EXPIRATION, SEGMENT_UPDATE, SPLIT_KILL, SPLIT_UPDATE, RB_SEGMENT_UPDATE, PUSH_RETRYABLE_ERROR, PUSH_SUBSYSTEM_UP, ControlType } from './constants';
1515
import { STREAMING_FALLBACK, STREAMING_REFRESH_TOKEN, STREAMING_CONNECTING, STREAMING_DISABLED, ERROR_STREAMING_AUTH, STREAMING_DISCONNECTING, STREAMING_RECONNECT, STREAMING_PARSING_MEMBERSHIPS_UPDATE } from '../../logger/constants';
1616
import { IMembershipMSUpdateData, IMembershipLSUpdateData, KeyList, UpdateStrategy } from './SSEHandler/types';
@@ -34,7 +34,7 @@ export function pushManagerFactory(
3434

3535
// `userKey` is the matching key of main client in client-side SDK.
3636
// It can be used to check if running on client-side or server-side SDK.
37-
const userKey = settings.core.key ? getMatching(settings.core.key) : undefined;
37+
const userKey = checkIfServerSide(settings) ? undefined : getMatching(settings.core.key);
3838
const log = settings.log;
3939

4040
let sseClient: ISSEClient;

src/sync/submitters/telemetrySubmitter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { timer } from '../../utils/timeTracker/timer';
1111
import { ISdkFactoryContextSync } from '../../sdkFactory/types';
1212
import { objectAssign } from '../../utils/lang/objectAssign';
1313
import { ISplitFiltersValidation } from '../../dtos/types';
14+
import { checkIfServerSide } from '../../utils/key';
1415

1516
const OPERATION_MODE_MAP = {
1617
[STANDALONE_MODE]: STANDALONE_ENUM,
@@ -72,16 +73,16 @@ export function telemetryCacheConfigAdapter(telemetry: ITelemetryCacheSync, sett
7273

7374
pop(): TelemetryConfigStatsPayload {
7475
const { urls, scheduler } = settings;
75-
const isClientSide = settings.core.key !== undefined;
76+
const isServerSide = checkIfServerSide(settings);
7677

7778
const { flagSetsTotal, flagSetsIgnored } = getTelemetryFlagSetsStats(settings.sync.__splitFiltersValidation);
7879

7980
return objectAssign(getTelemetryConfigStats(settings.mode, settings.storage.type), {
8081
sE: settings.streamingEnabled,
8182
rR: {
8283
sp: scheduler.featuresRefreshRate / 1000,
83-
se: isClientSide ? undefined : scheduler.segmentsRefreshRate / 1000,
84-
ms: isClientSide ? scheduler.segmentsRefreshRate / 1000 : undefined,
84+
se: isServerSide ? scheduler.segmentsRefreshRate / 1000 : undefined,
85+
ms: isServerSide ? undefined : scheduler.segmentsRefreshRate / 1000,
8586
im: scheduler.impressionsRefreshRate / 1000,
8687
ev: scheduler.eventsPushRate / 1000,
8788
te: scheduler.telemetryRefreshRate / 1000,

0 commit comments

Comments
 (0)