Skip to content

Commit 927d505

Browse files
removed sanitizer logic for a separete PR
1 parent 5273d10 commit 927d505

File tree

3 files changed

+19
-113
lines changed

3 files changed

+19
-113
lines changed

src/evaluator/value/sanitize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function sanitizeArray(val: any): string[] | undefined {
2828
return arr.length ? arr : undefined;
2929
}
3030

31-
export function sanitizeBoolean(val: any): boolean | undefined {
31+
function sanitizeBoolean(val: any): boolean | undefined {
3232
if (val === true || val === false) return val;
3333

3434
if (typeof val === 'string') {

src/storages/pluggable/__tests__/wrapperAdapter.spec.ts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,6 @@ export const wrapperWithIssues = {
3838
itemContains: throwsException,
3939
};
4040

41-
export const wrapperWithValuesToSanitize = {
42-
get: () => Promise.resolve(10),
43-
set: () => Promise.resolve('tRue'),
44-
del: () => Promise.resolve(), // no result
45-
getKeysByPrefix: () => Promise.resolve(['1', null, false, true, '2', null]),
46-
incr: () => Promise.resolve('1'),
47-
decr: () => Promise.resolve('0'),
48-
getMany: () => Promise.resolve(['1', null, false, true, '2', null]),
49-
connect: () => Promise.resolve(1),
50-
getAndSet: () => Promise.resolve(true),
51-
getByPrefix: () => Promise.resolve(['1', null, false, true, '2', null]),
52-
popItems: () => Promise.resolve('invalid array'),
53-
getItemsCount: () => Promise.resolve('10'),
54-
itemContains: () => Promise.resolve('true'),
55-
};
56-
57-
const SANITIZED_RESULTS = {
58-
get: '10',
59-
set: true,
60-
del: false,
61-
getKeysByPrefix: ['1', '2'],
62-
incr: false,
63-
decr: false,
64-
getMany: ['1', null, '2', null],
65-
connect: false,
66-
getAndSet: 'true',
67-
getByPrefix: ['1', '2'],
68-
popItems: [],
69-
getItemsCount: 10,
70-
itemContains: true,
71-
};
72-
7341
const VALID_METHOD_CALLS = {
7442
'get': ['some_key'],
7543
'set': ['some_key', 'some_value'],
@@ -136,22 +104,4 @@ describe('Wrapper Adapter', () => {
136104
expect(loggerMock.error).toBeCalledTimes(methods.length);
137105
});
138106

139-
test('sanitize wrapper call results', async () => {
140-
const instance = wrapperAdapter(loggerMock, wrapperWithValuesToSanitize);
141-
const methods = Object.keys(SANITIZED_RESULTS);
142-
143-
for (let i = 0; i < methods.length; i++) {
144-
try {
145-
const method = methods[i];
146-
const result = await instance[method](...VALID_METHOD_CALLS[method]);
147-
148-
expect(result).toEqual(SANITIZED_RESULTS[method]); // result should be sanitized
149-
} catch (e) {
150-
expect(true).toBe(methods[i]); // promise shouldn't be rejected
151-
}
152-
}
153-
154-
expect(loggerMock.warn).toBeCalledTimes(methods.length); // one warning for each wrapper operation call which result had to be sanitized
155-
});
156-
157107
});

src/storages/pluggable/wrapperAdapter.ts

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,24 @@
1-
import { toString, isString, toNumber } from '../../utils/lang';
2-
import { sanitizeBoolean as sBoolean } from '../../evaluator/value/sanitize';
31
import { ILogger } from '../../logger/types';
42
import { SplitError } from '../../utils/lang/errors';
53
import { ICustomStorageWrapper } from '../types';
64
import { LOG_PREFIX } from './constants';
75

8-
// Sanitizers return the given value if it is of the expected type, or a new sanitized one if invalid.
9-
// @TODO review or remove sanitizers. Could be expensive and error-prone for producer methods
10-
11-
function sanitizeBoolean(val: any): boolean {
12-
return sBoolean(val) || false;
13-
}
14-
sanitizeBoolean.type = 'boolean';
15-
16-
function sanitizeNumber(val: any): number {
17-
return toNumber(val);
18-
}
19-
sanitizeNumber.type = 'number';
20-
21-
function sanitizeArrayOfStrings(val: any): string[] {
22-
if (!Array.isArray(val)) return []; // if not an array, returns a new empty one
23-
if (val.every(isString)) return val; // if all items are valid, return the given array
24-
return val.filter(isString); // otherwise, return a new array filtering the invalid items
25-
}
26-
sanitizeArrayOfStrings.type = 'Array<string>';
27-
28-
function sanitizeNullableString(val: any): string | null {
29-
if (val === null) return val;
30-
return toString(val); // if not null, sanitize value as an string
31-
}
32-
sanitizeNullableString.type = 'string | null';
33-
34-
function sanitizeArrayOfNullableStrings(val: any): (string | null)[] {
35-
const isStringOrNull = (v: any) => v === null || isString(v);
36-
if (!Array.isArray(val)) return [];
37-
if (val.every(isStringOrNull)) return val;
38-
return val.filter(isStringOrNull); // otherwise, return a new array with items sanitized
39-
}
40-
sanitizeArrayOfNullableStrings.type = 'Array<string | null>';
41-
42-
const METHODS_TO_PROMISE_WRAP: [string, undefined | { (val: any): any, type: string }][] = [
43-
['get', sanitizeNullableString],
44-
['set', sanitizeBoolean],
45-
['getAndSet', sanitizeNullableString],
46-
['del', sanitizeBoolean],
47-
['getKeysByPrefix', sanitizeArrayOfStrings],
48-
['getByPrefix', sanitizeArrayOfStrings],
49-
['incr', sanitizeBoolean],
50-
['decr', sanitizeBoolean],
51-
['getMany', sanitizeArrayOfNullableStrings],
52-
['pushItems', undefined],
53-
['popItems', sanitizeArrayOfStrings],
54-
['getItemsCount', sanitizeNumber],
55-
['itemContains', sanitizeBoolean],
56-
['connect', sanitizeBoolean],
57-
['close', undefined],
6+
const METHODS_TO_PROMISE_WRAP: string[] = [
7+
'get',
8+
'set',
9+
'getAndSet',
10+
'del',
11+
'getKeysByPrefix',
12+
'getByPrefix',
13+
'incr',
14+
'decr',
15+
'getMany',
16+
'pushItems',
17+
'popItems',
18+
'getItemsCount',
19+
'itemContains',
20+
'connect',
21+
'close'
5822
];
5923

6024
/**
@@ -69,7 +33,7 @@ export function wrapperAdapter(log: ILogger, wrapper: ICustomStorageWrapper): IC
6933

7034
const wrapperAdapter: Record<string, Function> = {};
7135

72-
METHODS_TO_PROMISE_WRAP.forEach(([method, sanitizer]) => {
36+
METHODS_TO_PROMISE_WRAP.forEach((method) => {
7337

7438
// Logs error and wraps it into an SplitError object (required to handle user callback errors in SDK readiness events)
7539
function handleError(e: any) {
@@ -80,15 +44,7 @@ export function wrapperAdapter(log: ILogger, wrapper: ICustomStorageWrapper): IC
8044
wrapperAdapter[method] = function () {
8145
try {
8246
// @ts-ignore
83-
return wrapper[method].apply(wrapper, arguments).then((value => {
84-
if (!sanitizer) return value;
85-
const sanitizedValue = sanitizer(value);
86-
87-
// if value had to be sanitized, log a warning
88-
if (sanitizedValue !== value) log.warn(`${LOG_PREFIX} Attempted to sanitize return value [${value}] of wrapper '${method}' operation which should be of type [${sanitizer.type}]. Sanitized and processed value => [${sanitizedValue}]`);
89-
90-
return sanitizedValue;
91-
})).catch(handleError);
47+
return wrapper[method].apply(wrapper, arguments).then(value => value).catch(handleError);
9248
} catch (e) {
9349
return handleError(e);
9450
}

0 commit comments

Comments
 (0)