Skip to content

Commit

Permalink
fix: avoid parsing ridiculously long strings (#489)
Browse files Browse the repository at this point in the history
* fix: avoid parsing ridiculously long strings

* fix: debug

* update e2e

* fix: bun install after module update

* fix: install deps
  • Loading branch information
akvlad authored Apr 9, 2024
1 parent 2ebbf89 commit 3771360
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/bun-clickhouse.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ jobs:
with:
bun-version: ${{ matrix.bun-version }}
- run: rm -rf package-lock.json
- run: bun install
- run: git submodule init
- run: git submodule update
- run: bun install
- run: cd test/e2e && bun install
- name: Workflow Telemetry
uses: runforesight/[email protected]
if: github.event_name != 'pull_request'
Expand Down
32 changes: 32 additions & 0 deletions parser/bnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,36 @@ for (const [name, rule] of Object.entries(compiler.languages.logql.rules)) {
}
}

compiler._ParseScript = compiler.ParseScript
/**
* hack to avoid ridiculously long strings
* @param script {string}
* @constructor
*/
compiler.ParseScript = function (script) {
const qLiterals = []
const aqLiterals = []
const quotedStrings = script.replaceAll(/"([^"\\]|\\.)+"/g, (str) => {
qLiterals.push(str)
return `"QL_${qLiterals.length - 1}"`
})
const aQuotedStrings = quotedStrings.replaceAll(/`([^`\\]|\\.)+`/g, (str) => {
aqLiterals.push(str)
return `\`AL_${aqLiterals.length - 1}\``
})
const parsedScript = this._ParseScript(aQuotedStrings)
if (!parsedScript) {
return parsedScript
}
for (const t of parsedScript.rootToken.Children('QLITERAL')) {
t._value = qLiterals[parseInt(t.value.slice(4, t.value.length - 1))]
t.tokens = []
}
for (const t of parsedScript.rootToken.Children('AQLITERAL')) {
t._value = aqLiterals[parseInt(t.value.slice(4, t.value.length - 1))]
t.tokens = []
}
return parsedScript
}

module.exports = compiler
2 changes: 1 addition & 1 deletion test/e2e
Submodule e2e updated from 83d70c to 85e344

0 comments on commit 3771360

Please sign in to comment.