8000 validate config manager cache by raju-opti · Pull Request #1064 · optimizely/javascript-sdk · GitHub
[go: up one dir, main page]

Skip to content

validate config manager cache #1064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/event_processor/event_processor_factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ describe('getBatchEventProcessor', () => {
defaultFlushInterval: 10000,
defaultBatchSize: 10,
eventStore: 'abc' as any,
})).toThrow('Invalid event store');
})).toThrow('Invalid store');

expect(() => getBatchEventProcessor({
eventDispatcher: getMockEventDispatcher(),
defaultFlushInterval: 10000,
defaultBatchSize: 10,
eventStore: 123 as any,
})).toThrow('Invalid event store');
})).toThrow('Invalid store');

expect(() => getBatchEventProcessor({
eventDispatcher: getMockEventDispatcher(),
Expand Down
21 changes: 1 addition & 20 deletions lib/event_processor/event_processor_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import { ForwardingEventProcessor } from "./forwarding_event_processor";
import { BatchEventProcessor, DEFAULT_MAX_BACKOFF, DEFAULT_MIN_BACKOFF, EventWithId, RetryConfig } from "./batch_event_processor";
import { AsyncPrefixStore, Store, SyncPrefixStore } from "../utils/cache/store";
import { Maybe } from "../utils/type";
import { validateStore } from "../utils/cache/store_validator";

export const INVALID_EVENT_DISPATCHER = 'Invalid event dispatcher';

export const FAILED_EVENT_RETRY_INTERVAL = 20 * 1000;
export const EVENT_STORE_PREFIX = 'optly_event:';

export const INVALID_STORE = 'Invalid event store';
export const INVALID_STORE_METHOD = 'Invalid store method %s';

export const getPrefixEventStore = (store: Store<string>): Store<EventWithId> => {
if (store.operation === 'async') {
return new AsyncPrefixStore<string, EventWithId>(
Expand Down Expand Up @@ -84,23 +82,6 @@ export const validateEventDispatcher = (eventDispatcher: EventDispatcher): void
}
}

const validateStore = (store: any) => {
const errors = [];
if (!store || typeof store !== 'object') {
throw new Error(INVALID_STORE);
}

for (const method of ['set', 'get', 'remove', 'getKeys']) {
if (typeof store[method] !== 'function') {
errors.push(INVALID_STORE_METHOD.replace('%s', method));
}
}

if (errors.length > 0) {
throw new Error(errors.join(', '));
}
}

export const getBatchEventProcessor = (
options: BatchEventProcessorFactoryOptions,
EventProcessorConstructor: typeof BatchEventProcessor = BatchEventProcessor
Expand Down
48 changes: 47 additions & 1 deletion lib/project_config/config_manager_factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,6 +53,52 @@ describe('getPollingConfigManager', () => {
MockExponentialBackoff.mockClear();
});

it('should throw an error if the passed cache is not valid', () => {
expect(() => getPollingConfigManager({
sdkKey: 'sdkKey',
requestHandler: { makeRequest: vi.fn() },
cache: 1 as any,
})).toThrow('Invalid store');

expect(() => getPollingConfigManager({
sdkKey: 'sdkKey',
requestHandler: { makeRequest: vi.fn() },
cache: 'abc' as any,
})).toThrow('Invalid store');

expect(() => getPollingConfigManager({
sdkKey: 'sdkKey',
requestHandler: { makeRequest: vi.fn() },
cache: {} as any,
})).toThrow('Invalid store method set, Invalid store method get, Invalid store method remove, Invalid store method getKeys');

expect(() => getPollingConfigManager({
sdkKey: 'sdkKey',
requestHandler: { makeRequest: vi.fn() },
cache: { set: 'abc', get: 'abc', remove: 'abc', getKeys: 'abc' } as any,
})).toThrow('Invalid store method set, Invalid store method get, Invalid store method remove, Invalid store method getKeys');

const noop = () => {};

expect(() => getPollingConfigManager({
sdkKey: 'sdkKey',
requestHandler: { makeRequest: vi.fn() },
cache: { set: noop, get: 'abc', remove: 'abc', getKeys: 'abc' } as any,
})).toThrow('Invalid store method get, Invalid store method remove, Invalid store method getKeys');

expect(() => getPollingConfigManager({
sdkKey: 'sdkKey',
requestHandler: { makeRequest: vi.fn() },
cache: { set: noop, get: noop, remove: 'abc', getKeys: 'abc' } as any,
})).toThrow('Invalid store method remove, Invalid store method getKeys');

expect(() => getPollingConfigManager({
sdkKey: 'sdkKey',
requestHandler: { makeRequest: vi.fn() },
cache: { set: noop, get: noop, remove: noop, getKeys: 'abc' } as any,
})).toThrow('Invalid store method getKeys');
});

it('uses a repeater with exponential backoff for the datafileManager', () => {
const config = {
sdkKey: 'sdkKey',
Expand Down
5 changes: 5 additions & 0 deletions lib/project_config/config_manager_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { StartupLog } from "../service";
import { MIN_UPDATE_INTERVAL, UPDATE_INTERVAL_BELOW_MINIMUM_MESSAGE } from './constant';
import { LogLevel } from '../logging/logger'
import { Store } from "../utils/cache/store";
import { validateStore } from "../utils/cache/store_validator";

export const INVALID_CONFIG_MANAGER = "Invalid config manager";

Expand Down Expand Up @@ -63,6 +64,10 @@ export type PollingConfigManagerFactoryOptions = PollingConfigManagerConfig & {
export const getPollingConfigManager = (
opt: PollingConfigManagerFactoryOptions
): ProjectConfigManager => {
if (opt.cache) {
validateStore(opt.cache);
}

const updateInterval = opt.updateInterval ?? DEFAULT_UPDATE_INTERVAL;

const backoff = new ExponentialBackoff(1000, updateInterval, 500);
Expand Down
36 changes: 36 additions & 0 deletions lib/utils/cache/store_validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

/**
* Copyright 2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export const INVALID_STORE = 'Invalid store';
export const INVALID_STORE_METHOD = 'Invalid store method %s';

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const validateStore = (store: any): void => {
const errors = [];
if (!store || typeof store !== 'object') {
throw new Error(INVALID_STORE);
}

for (const method of ['set', 'get', 'remove', 'getKeys']) {
if (typeof store[method] !== 'function') {
errors.push(INVALID_STORE_METHOD.replace('%s', method));
}
}

if (errors.length > 0) {
throw new Error(errors.join(', '));
}
}
2 changes: 1 addition & 1 deletion vitest.config.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024 Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading
0