From 63f3f696b84c7aa628ad11a51da0bcefcf224a4c Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Mon, 18 Oct 2021 10:04:51 +0500 Subject: [PATCH] feat: now it allows using scoped and scope-structured objects and able to decode this --- src/analyze.ts | 2 +- src/extractors/scopes.ts | 36 ++++++++++++++++++++++++---- src/extractors/variable-key.ts | 2 +- src/finders/request/body.ts | 2 +- src/finders/request/cookies.ts | 2 +- src/finders/request/headers.ts | 2 +- src/finders/request/index.ts | 27 +++++++++++++++++---- src/finders/request/params.ts | 2 +- src/finders/request/property.ts | 2 +- src/finders/request/query.ts | 2 +- {types => src/types}/interfaces.d.ts | 0 src/utils/is-scope-in.ts | 6 +++++ tests/analyze.spec.js | 9 +++++++ 13 files changed, 77 insertions(+), 17 deletions(-) rename {types => src/types}/interfaces.d.ts (100%) create mode 100644 src/utils/is-scope-in.ts diff --git a/src/analyze.ts b/src/analyze.ts index 0ec2e32..092a371 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -1,4 +1,4 @@ -import { IBlock } from '../types/interfaces'; +import { IBlock } from './types/interfaces'; import caseRequestModeFinder from './finders/request'; import babelCompilerManipulationNormalize from './utils/babel-normalise'; import eachStringLine from './utils/each-str-line'; diff --git a/src/extractors/scopes.ts b/src/extractors/scopes.ts index 84eb0bd..e1c44cb 100644 --- a/src/extractors/scopes.ts +++ b/src/extractors/scopes.ts @@ -1,8 +1,9 @@ -import { BlockMode } from '../../types/interfaces'; +import { BlockMode } from '../types/interfaces'; export default (line: string): ['request' | 'req', BlockMode, string?] => { const matches = []; const cases = { + SPACES: 0, SCOPE: 0, CURVE_SCOPE: 0, QUOTE: 0, @@ -11,6 +12,9 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => { DOTS: 0, COMMAS: 0 }; + const caseSum = (): number => + Object.values(cases).reduce((acc, n) => acc + n, 0); + let pointer = 0; let rebuild = ''; for (const char of line) { @@ -19,6 +23,8 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => { cases.SCOPE++; rebuild += char; + + pointer++; break; } case "'": @@ -26,12 +32,16 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => { cases.QUOTE++; rebuild += char; + + pointer++; break; } case '{': { cases.CURVE_SCOPE++; rebuild += char; + + pointer++; break; } case ')': { @@ -41,6 +51,8 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => { } else { // rebuild = ''; } + + pointer++; break; } case '}': { @@ -52,16 +64,22 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => { rebuild = ''; } } + + pointer++; break; } case ':': case '=': { - cases.ENDLINES++; - - if (rebuild.length > 0) { + if (caseSum() === 0) { + rebuild = ''; + line = line.substr(pointer + 1); + } else if (rebuild.length > 0) { matches.push(rebuild); rebuild = ''; } + cases.ENDLINES++; + + pointer++; break; } case '.': { @@ -71,6 +89,8 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => { matches.push(rebuild); rebuild = ''; } + + pointer++; break; } case ',': { @@ -80,9 +100,14 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => { matches.push(rebuild); rebuild = ''; } + + pointer++; break; } case ' ': { + cases.SPACES++; + + pointer++; break; } case ';': { @@ -94,11 +119,14 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => { rebuild = ''; } } + + pointer++; break; } default: { rebuild += char; + pointer++; break; } } diff --git a/src/extractors/variable-key.ts b/src/extractors/variable-key.ts index 0f09d49..d767f21 100644 --- a/src/extractors/variable-key.ts +++ b/src/extractors/variable-key.ts @@ -1,5 +1,5 @@ export default function variableKeyExtractor(line: string): string[] | null { - const matcRegEx = /(const|let|var)(.*)=(.*)/; + const matcRegEx = /(const|let|var)(.*)=(.*)?;/; const matches = line.match(matcRegEx); if (matches) { diff --git a/src/finders/request/body.ts b/src/finders/request/body.ts index 3c7ad59..91aa147 100644 --- a/src/finders/request/body.ts +++ b/src/finders/request/body.ts @@ -1,4 +1,4 @@ -import { IBlock } from '../../../types/interfaces'; +import { IBlock } from '../../types/interfaces'; import variableKeyExtractor from '../../extractors/variable-key'; export default function requestModeBodyFinder( diff --git a/src/finders/request/cookies.ts b/src/finders/request/cookies.ts index d05dd5f..f2a5863 100644 --- a/src/finders/request/cookies.ts +++ b/src/finders/request/cookies.ts @@ -1,4 +1,4 @@ -import { IBlock } from '../../../types/interfaces'; +import { IBlock } from '../../types/interfaces'; import variableKeyExtractor from '../../extractors/variable-key'; export default function requestModeCookiesFinder( diff --git a/src/finders/request/headers.ts b/src/finders/request/headers.ts index 4d248af..ab7dc4c 100644 --- a/src/finders/request/headers.ts +++ b/src/finders/request/headers.ts @@ -1,4 +1,4 @@ -import { IBlock } from '../../../types/interfaces'; +import { IBlock } from '../../types/interfaces'; import variableKeyExtractor from '../../extractors/variable-key'; export default function requestModeHeadersFinder( diff --git a/src/finders/request/index.ts b/src/finders/request/index.ts index f758383..14c6ce8 100644 --- a/src/finders/request/index.ts +++ b/src/finders/request/index.ts @@ -1,5 +1,6 @@ -import { IBlock } from '../../../types/interfaces'; import scopeExtractor from '../../extractors/scopes'; +import { IBlock } from '../../types/interfaces'; +import isScopeIn from '../../utils/is-scope-in'; import requestModeBodyFinder from './body'; import requestModeCookiesFinder from './cookies'; import requestModeHeadersFinder from './headers'; @@ -8,9 +9,10 @@ import requestMethodFinder from './property'; import requestModeQueryFinder from './query'; export default function caseRequestModeFinder( - line: string, + raw_line: string, index: number ): IBlock | null | undefined { + const line = raw_line.trim(); const requestPropertyCaseFindMatch = line.match( /(request|req).(.*)?(;|\.|\()/ ); @@ -26,12 +28,27 @@ export default function caseRequestModeFinder( [_, _key, _skey] = scopeExtractor(input); if (_skey) { + if (isScopeIn(_key)) { + return { + link: _skey, + linked: false, + line_index: index, + key: _skey, + mode: _key + }; + } + return undefined; + } + + if (_key === undefined) { + [_, _key] = scopeExtractor(line); + return { - link: _skey, + link: _key, linked: false, line_index: index, - key: _skey, - mode: _key + key: _key, + mode: 'property' }; } diff --git a/src/finders/request/params.ts b/src/finders/request/params.ts index 961311b..096053f 100644 --- a/src/finders/request/params.ts +++ b/src/finders/request/params.ts @@ -1,4 +1,4 @@ -import { IBlock } from '../../../types/interfaces'; +import { IBlock } from '../../types/interfaces'; import variableKeyExtractor from '../../extractors/variable-key'; export default function requestModeParamsFinder( diff --git a/src/finders/request/property.ts b/src/finders/request/property.ts index 5cb01df..2c9b347 100644 --- a/src/finders/request/property.ts +++ b/src/finders/request/property.ts @@ -1,4 +1,4 @@ -import { IBlock } from '../../../types/interfaces'; +import { IBlock } from '../../types/interfaces'; import variableKeyExtractor from '../../extractors/variable-key'; export default function requestMethodFinder( diff --git a/src/finders/request/query.ts b/src/finders/request/query.ts index ed9aa6a..70e0b3f 100644 --- a/src/finders/request/query.ts +++ b/src/finders/request/query.ts @@ -1,4 +1,4 @@ -import { IBlock } from '../../../types/interfaces'; +import { IBlock } from '../../types/interfaces'; import variableKeyExtractor from '../../extractors/variable-key'; export default function requestModeQueryFinder( diff --git a/types/interfaces.d.ts b/src/types/interfaces.d.ts similarity index 100% rename from types/interfaces.d.ts rename to src/types/interfaces.d.ts diff --git a/src/utils/is-scope-in.ts b/src/utils/is-scope-in.ts new file mode 100644 index 0000000..150cb6f --- /dev/null +++ b/src/utils/is-scope-in.ts @@ -0,0 +1,6 @@ +import { BlockMode } from '../types/interfaces'; + +const scopes = ['headers', 'cookies', 'params', 'query', 'body', 'property']; +const isScopeIn = (scope: BlockMode): boolean => scopes.includes(scope); + +export default isScopeIn; diff --git a/tests/analyze.spec.js b/tests/analyze.spec.js index f031388..9d81026 100644 --- a/tests/analyze.spec.js +++ b/tests/analyze.spec.js @@ -129,6 +129,15 @@ describe('property parse', () => { mode: 'property' } ]); + expect(analyze((request, response) => ({ ip: request.ip }))).toEqual([ + { + key: 'ip', + line_index: 1, + link: 'ip', + linked: false, + mode: 'property' + } + ]); }); });