Skip to content

Commit 7e839e4

Browse files
committed
more validation
1 parent 1759afe commit 7e839e4

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

lib/event_processor/event_processor_factory.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,59 @@ describe('getBatchEventProcessor', () => {
7373
})).toThrow('Invalid event dispatcher');
7474
});
7575

76+
it('should throw and error if provided event store is invalid', () => {
77+
expect(() => getBatchEventProcessor({
78+
eventDispatcher: getMockEventDispatcher(),
79+
defaultFlushInterval: 10000,
80+
defaultBatchSize: 10,
81+
eventStore: 'abc' as any,
82+
})).toThrow('Invalid event store');
83+
84+
expect(() => getBatchEventProcessor({
85+
eventDispatcher: getMockEventDispatcher(),
86+
defaultFlushInterval: 10000,
87+
defaultBatchSize: 10,
88+
eventStore: 123 as any,
89+
})).toThrow('Invalid event store');
90+
91+
expect(() => getBatchEventProcessor({
92+
eventDispatcher: getMockEventDispatcher(),
93+
defaultFlushInterval: 10000,
94+
defaultBatchSize: 10,
95+
eventStore: {} as any,
96+
})).toThrow('Invalid store method set, Invalid store method get, Invalid store method remove, Invalid store method getKeys');
97+
98+
expect(() => getBatchEventProcessor({
99+
eventDispatcher: getMockEventDispatcher(),
100+
defaultFlushInterval: 10000,
101+
defaultBatchSize: 10,
102+
eventStore: { set: 'abc', get: 'abc', remove: 'abc', getKeys: 'abc' } as any,
103+
})).toThrow('Invalid store method set, Invalid store method get, Invalid store method remove, Invalid store method getKeys');
104+
105+
const noop = () => {};
106+
107+
expect(() => getBatchEventProcessor({
108+
eventDispatcher: getMockEventDispatcher(),
109+
defaultFlushInterval: 10000,
110+
defaultBatchSize: 10,
111+
eventStore: { set: noop, get: 'abc' } as any,
112+
})).toThrow('Invalid store method get, Invalid store method remove, Invalid store method getKeys');
113+
114+
expect(() => getBatchEventProcessor({
115+
eventDispatcher: getMockEventDispatcher(),
116+
defaultFlushInterval: 10000,
117+
defaultBatchSize: 10,
118+
eventStore: { set: noop, get: noop, remove: 'abc' } as any,
119+
})).toThrow('Invalid store method remove, Invalid store method getKeys');
120+
121+
expect(() => getBatchEventProcessor({
122+
eventDispatcher: getMockEventDispatcher(),
123+
defaultFlushInterval: 10000,
124+
defaultBatchSize: 10,
125+
eventStore: { set: noop, get: noop, remove: noop, getKeys: 'abc' } as any,
126+
})).toThrow('Invalid store method getKeys');
127+
});
128+
76129
it('returns an instane of BatchEventProcessor if no subclass constructor is provided', () => {
77130
const options = {
78131
eventDispatcher: getMockEventDispatcher(),

lib/event_processor/event_processor_factory.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export const INVALID_EVENT_DISPATCHER = 'Invalid event dispatcher';
2929
export const FAILED_EVENT_RETRY_INTERVAL = 20 * 1000;
3030
export const EVENT_STORE_PREFIX = 'optly_event:';
3131

32+
export const INVALID_STORE = 'Invalid event store';
33+
export const INVALID_STORE_METHOD = 'Invalid store method %s';
34+
3235
export const getPrefixEventStore = (store: Store<string>): Store<EventWithId> => {
3336
if (store.operation === 'async') {
3437
return new AsyncPrefixStore<string, EventWithId>(
@@ -81,6 +84,23 @@ export const validateEventDispatcher = (eventDispatcher: EventDispatcher): void
8184
}
8285
}
8386

87+
const validateStore = (store: any) => {
88+
const errors = [];
89+
if (!store || typeof store !== 'object') {
90+
throw new Error(INVALID_STORE);
91+
}
92+
93+
for (const method of ['set', 'get', 'remove', 'getKeys']) {
94+
if (typeof store[method] !== 'function') {
95+
errors.push(INVALID_STORE_METHOD.replace('%s', method));
96+
}
97+
}
98+
99+
if (errors.length > 0) {
100+
throw new Error(errors.join(', '));
101+
}
102+
}
103+
84104
export const getBatchEventProcessor = (
85105
options: BatchEventProcessorFactoryOptions,
86106
EventProcessorConstructor: typeof BatchEventProcessor = BatchEventProcessor
@@ -92,6 +112,10 @@ export const getBatchEventProcessor = (
92112
validateEventDispatcher(closingEventDispatcher);
93113
}
94114

115+
if (eventStore) {
116+
validateStore(eventStore);
117+
}
118+
95119
const retryConfig: RetryConfig | undefined = retryOptions ? {
96120
maxRetries: retryOptions.maxRetries,
97121
backoffProvider: () => {

lib/odp/odp_manager_factory.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export type OdpManagerFactoryOptions = Omit<OdpManagerOptions, 'segmentsApiTime
6767
const validateCache = (cache: any) => {
6868
const errors = [];
6969
if (!cache || typeof cache !== 'object') {
70-
console.log('what ', cache, typeof cache);
7170
throw new Error(INVALID_CACHE);
7271
}
7372

0 commit comments

Comments
 (0)