nextpay-querystring
Version:
Thư viện QueryString của NextPay - Chuyển đổi QueryString thành điều kiện select cho MongoDB và MySQL với kiểm soát bảo mật
252 lines • 10.8 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable implicit-arrow-linebreak */
const exceptions_1 = require("../utils/exceptions");
const generics_1 = require("../utils/generics");
const tokens_1 = require("../models/tokens");
const LexemeToToken = __importStar(require("./lexemeToToken"));
const convertFuncPath = 'step1.Step1Converter.convert: ';
const convertArrayFuncPath = 'step1.Step1Converter.convertArray: ';
const markStrings = (queryString) => {
let quoteCounter = 0;
let foundLeftQuotes = false;
let leftQuotesAt = -1;
const marks = Array(queryString.length).fill(false);
queryString.split('').forEach((letter, i) => {
quoteCounter = letter === "'" ? quoteCounter + 1 : 0;
if (quoteCounter < 3)
return;
if (foundLeftQuotes) {
for (let iter = leftQuotesAt; iter <= i; iter += 1) {
marks[iter] = true;
}
foundLeftQuotes = false;
}
else {
foundLeftQuotes = true;
leftQuotesAt = i - 2;
}
});
if (foundLeftQuotes) {
throw (0, exceptions_1.noMatchingBracketException)(convertFuncPath, 0, queryString.length)('string', leftQuotesAt);
}
return marks;
};
const getRightParenPosition = (0, generics_1.matchingElemPosition)('(', ')')((x, y) => x === y);
const getRightBracketPosition = (0, generics_1.findFirst)(']')((x, y) => x === y);
const getRightHashPosition = (0, generics_1.findFirst)('#')((x, y) => x === y);
const getRightBracePosition = (0, generics_1.findFirst)('}')((x, y) => x === y);
const instanceOfToken = (obj) => 'type' in obj && 'lexeme' in obj;
const parsingLexemeToTokenWithOrders = (...parsingFuncs) => (lexeme) => {
if (lexeme.length === 0) {
throw (0, exceptions_1.invalidTokenException)(lexeme);
}
const parsingFunc = parsingFuncs.find(func => {
const parsingResult = func(lexeme);
return instanceOfToken(parsingResult);
});
if (parsingFunc) {
return parsingFunc(lexeme);
}
throw (0, exceptions_1.invalidTokenException)(lexeme);
};
const isAlphaNumeric = (c) => (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c === '_' ||
(c >= '0' && c <= '9') ||
c === '+' ||
c === '-' ||
c === '.' ||
c === '%';
const nextSeperatorInArray = (0, generics_1.findFirst)(',', ']')((x, y) => x === y);
const scanTokensInArray = (queryString, leftId, rightId, visited) => {
const tokens = [];
tokens.push({ type: tokens_1.TokenType.LEFT_BRACKET, lexeme: '[' });
let pos = leftId;
let numElems = 0;
while (pos < rightId - 1) {
const index = nextSeperatorInArray(queryString.split(''), pos + 1, rightId, visited);
const lexeme = queryString.slice(pos + 1, index).trim();
const isEmptyString = lexeme === '';
if (isEmptyString) {
if (numElems > 0) {
throw (0, exceptions_1.unexpectedCharacterException)(convertArrayFuncPath, leftId, rightId)('EMPTY_STRING', pos);
}
}
else {
// just let int before double, other orders is not important
const lexemeToLiteralToken = parsingLexemeToTokenWithOrders(LexemeToToken.toString, LexemeToToken.toDate, LexemeToToken.toVariable, LexemeToToken.toBoolean, LexemeToToken.toInt, LexemeToToken.toDouble);
const token = lexemeToLiteralToken(lexeme);
tokens.push(token);
}
pos = index;
numElems += 1;
}
tokens.push({ type: tokens_1.TokenType.RIGHT_BRACKET, lexeme: ']' });
return tokens;
};
const scanTokens = (queryString, leftId, rightId, visited) => {
const tokens = [];
for (let i = leftId; i < rightId; i += 1) {
// STRING
if (visited[i]) {
let j = i;
while (j < rightId && visited[j])
j += 1;
tokens.push({ type: tokens_1.TokenType.STRING, lexeme: queryString.slice(i, j) });
i = j - 1;
// eslint-disable-next-line no-continue
continue;
}
const c = queryString[i];
switch (c) {
case '(': {
// SUB GROUP
const rightParenAt = getRightParenPosition(queryString.split(''), i, rightId, visited);
if (rightParenAt < 0) {
throw (0, exceptions_1.noMatchingBracketException)(convertFuncPath, leftId, rightId)('clause', i);
}
tokens.push({ type: tokens_1.TokenType.LEFT_PAREN, lexeme: '(' });
tokens.push(...scanTokens(queryString, i + 1, rightParenAt, visited));
tokens.push({ type: tokens_1.TokenType.RIGHT_PAREN, lexeme: ')' });
i = rightParenAt;
break;
}
case '[': {
// ARRAY
const rightBracketAt = getRightBracketPosition(queryString.split(''), i + 1, rightId, visited);
if (rightBracketAt < 0) {
throw (0, exceptions_1.noMatchingBracketException)(convertFuncPath, leftId, rightId)('array', i);
}
tokens.push(...scanTokensInArray(queryString, i, rightBracketAt + 1, visited));
i = rightBracketAt;
break;
}
case '#': {
// DATE
const rightHashAt = getRightHashPosition(queryString.split(''), i + 1, rightId, visited);
if (rightHashAt < 0) {
throw (0, exceptions_1.noMatchingBracketException)(convertFuncPath, leftId, rightId)('date', i);
}
tokens.push({
type: tokens_1.TokenType.DATE,
lexeme: queryString.slice(i, rightHashAt + 1),
});
i = rightHashAt;
break;
}
case '{': {
// FIELD
const rightBraceAt = getRightBracePosition(queryString.split(''), i + 1, rightId, visited);
if (rightBraceAt < 0) {
throw (0, exceptions_1.noMatchingBracketException)(convertFuncPath, leftId, rightId)('field', i);
}
tokens.push({
type: tokens_1.TokenType.VARIABLE,
lexeme: queryString.slice(i, rightBraceAt + 1),
});
i = rightBraceAt;
break;
}
case '<':
case '>': {
// GT, GTE, LT, LTE filters
if (i + 1 < rightId && queryString[i + 1] === '=') {
const type = c === '<' ? tokens_1.TokenType.LESS_EQUAL : tokens_1.TokenType.GREATER_EQUAL;
tokens.push({ type, lexeme: `${c}=` });
i += 1;
}
else {
const type = c === '<' ? tokens_1.TokenType.LESS : tokens_1.TokenType.GREATER;
tokens.push({ type, lexeme: c });
}
break;
}
case '=': {
// EQ, TEXT_SEARCH filters
if (i + 2 < rightId && queryString.slice(i, i + 3) === '===') {
tokens.push({ type: tokens_1.TokenType.TRIPLE_EQUALS, lexeme: '===' });
i += 2;
}
else if (i + 1 < rightId && queryString.slice(i, i + 2) === '==') {
tokens.push({ type: tokens_1.TokenType.DOUBLE_EQUALS, lexeme: '==' });
i += 1;
}
else {
throw (0, exceptions_1.unexpectedCharacterException)(convertFuncPath, leftId, rightId)('=', i);
}
break;
}
case '!': {
// NE filter
if (i + 1 < rightId && queryString.slice(i, i + 2) === '!=') {
tokens.push({ type: tokens_1.TokenType.BANG_EQUAL, lexeme: '!=' });
i += 1;
}
else {
throw (0, exceptions_1.unexpectedCharacterException)(convertFuncPath, leftId, rightId)('=', i);
}
break;
}
case ' ':
case '\r':
case '\t': {
break;
}
default: {
// NUMBER (INT, DOUBLE), BOOLEAN, CLAUSE (AND, OR, NOT), FILTER (IN, NIN, RANGE)
let j = i;
while (j < rightId && isAlphaNumeric(queryString[j]))
j += 1;
if (i === j) {
throw (0, exceptions_1.unexpectedCharacterException)(convertFuncPath, leftId, rightId)(c, i);
}
const lexeme = queryString.slice(i, j).toLowerCase();
// just let int before double, other orders is not important
const token = parsingLexemeToTokenWithOrders(LexemeToToken.toInt, LexemeToToken.toDouble, LexemeToToken.toBoolean, LexemeToToken.toOperator, LexemeToToken.toClause)(lexeme);
tokens.push(token);
i = j - 1;
}
}
}
return tokens;
};
const tokenize = (queryString) => {
const visited = markStrings(queryString);
return scanTokens(queryString, 0, queryString.length, visited);
};
exports.default = tokenize;
//# sourceMappingURL=tokenizer.js.map
;