Skip to content

Commit

Permalink
feat: now it allows using scoped and scope-structured objects and abl…
Browse files Browse the repository at this point in the history
…e to decode this
  • Loading branch information
dalisoft committed Oct 18, 2021
1 parent d9a9bb6 commit 63f3f69
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/analyze.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
36 changes: 32 additions & 4 deletions src/extractors/scopes.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) {
Expand All @@ -19,19 +23,25 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => {
cases.SCOPE++;

rebuild += char;

pointer++;
break;
}
case "'":
case '"': {
cases.QUOTE++;

rebuild += char;

pointer++;
break;
}
case '{': {
cases.CURVE_SCOPE++;

rebuild += char;

pointer++;
break;
}
case ')': {
Expand All @@ -41,6 +51,8 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => {
} else {
// rebuild = '';
}

pointer++;
break;
}
case '}': {
Expand All @@ -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 '.': {
Expand All @@ -71,6 +89,8 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => {
matches.push(rebuild);
rebuild = '';
}

pointer++;
break;
}
case ',': {
Expand All @@ -80,9 +100,14 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => {
matches.push(rebuild);
rebuild = '';
}

pointer++;
break;
}
case ' ': {
cases.SPACES++;

pointer++;
break;
}
case ';': {
Expand All @@ -94,11 +119,14 @@ export default (line: string): ['request' | 'req', BlockMode, string?] => {
rebuild = '';
}
}

pointer++;
break;
}
default: {
rebuild += char;

pointer++;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/extractors/variable-key.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/finders/request/body.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBlock } from '../../../types/interfaces';
import { IBlock } from '../../types/interfaces';
import variableKeyExtractor from '../../extractors/variable-key';

export default function requestModeBodyFinder(
Expand Down
2 changes: 1 addition & 1 deletion src/finders/request/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBlock } from '../../../types/interfaces';
import { IBlock } from '../../types/interfaces';
import variableKeyExtractor from '../../extractors/variable-key';

export default function requestModeCookiesFinder(
Expand Down
2 changes: 1 addition & 1 deletion src/finders/request/headers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBlock } from '../../../types/interfaces';
import { IBlock } from '../../types/interfaces';
import variableKeyExtractor from '../../extractors/variable-key';

export default function requestModeHeadersFinder(
Expand Down
27 changes: 22 additions & 5 deletions src/finders/request/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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).(.*)?(;|\.|\()/
);
Expand All @@ -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'
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/finders/request/params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBlock } from '../../../types/interfaces';
import { IBlock } from '../../types/interfaces';
import variableKeyExtractor from '../../extractors/variable-key';

export default function requestModeParamsFinder(
Expand Down
2 changes: 1 addition & 1 deletion src/finders/request/property.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBlock } from '../../../types/interfaces';
import { IBlock } from '../../types/interfaces';
import variableKeyExtractor from '../../extractors/variable-key';

export default function requestMethodFinder(
Expand Down
2 changes: 1 addition & 1 deletion src/finders/request/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IBlock } from '../../../types/interfaces';
import { IBlock } from '../../types/interfaces';
import variableKeyExtractor from '../../extractors/variable-key';

export default function requestModeQueryFinder(
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions src/utils/is-scope-in.ts
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions tests/analyze.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
]);
});
});

Expand Down

0 comments on commit 63f3f69

Please sign in to comment.