8000 fix: serialization of `\n`, `\r` and `\t` to Line Protocol by bednar · Pull Request #205 · influxdata/influxdb-client-js · GitHub
[go: up one dir, main page]

Skip to content

fix: serialization of \n, \r and \t to Line Protocol #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

1. [#207](https://github.com/influxdata/influxdb-client-js/pull/207): Explain the significance of the precision argument in write example.

### Bug Fixes

1. [#205](https://github.com/influxdata/influxdb-client-js/pull/205): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol

## 1.4.0 [2020-06-19]

### Features
Expand Down
58 changes: 52 additions & 6 deletions packages/core/src/util/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ const escapeChar = '\\'
class Escaper {
private _re: RegExp

constructor(chars: string[], private wrap: string = '') {
const patterns = chars.join('').replace(reEscape, '\\$&')
constructor(
private config: {[p: string]: EscaperConfig},
private wrap: string = ''
) {
const patterns = Object.keys(config)
.join('|')
.replace(reEscape, '\\$&')
this._re = new RegExp('[' + patterns + ']', 'g')
}

Expand All @@ -51,7 +56,11 @@ class Escaper {
let match = this._re.exec(val)

while (match) {
escapedVal += val.slice(chunkIndex, match.index) + escapeChar + match[0]
const matched = match[0]
const toEscape = this.config[matched].escapeChar
const toReplace = this.config[matched].replaceChar
escapedVal += val.slice(chunkIndex, match.index)
escapedVal += toReplace != undefined ? toReplace : toEscape + matched
chunkIndex = this._re.lastIndex
match = this._re.exec(val)
}
Expand All @@ -68,21 +77,58 @@ class Escaper {
}
}

class EscaperConfig {
escapeChar?: string
replaceChar?: string

constructor(escapeChar?: string, replaceChar?: string) {
this.escapeChar = escapeChar
this.replaceChar = replaceChar
}
}

const escaperConfig = new EscaperConfig(escapeChar)

const bindEsc = (e: Escaper): ((val: string) => string) => e.escape.bind(e)

export const escape = {
/**
* Measurement escapes measurement names.
*/
measurement: bindEsc(new Escaper([',', ' '])),
measurement: bindEsc(
new Escaper({
',': escaperConfig,
' ': escaperConfig,
'\n': new EscaperConfig(undefined, '\\n'),
'\r': new EscaperConfig(undefined, '\\r'),
'\t': new EscaperConfig(undefined, '\\t'),
})
),

/**
* Quoted escapes quoted values, such as database names.
*/
quoted: bindEsc(new Escaper(['"', '\\\\'], '"')),
quoted: bindEsc(
new Escaper(
{
'"': escaperConfig,
'\\\\': escaperConfig,
},
'"'
)
),

/**
* TagEscaper escapes tag keys, tag values, and field keys.
*/
tag: bindEsc(new Escaper([',', '=', ' '])),
tag: bindEsc(
new Escaper({
',': escaperConfig,
'=': escaperConfig,
' ': escaperConfig,
'\n': new EscaperConfig(undefined, '\\n'),
'\r': new EscaperConfig(undefined, '\\r'),
'\t': new EscaperConfig(undefined, '\\t'),
})
),
}
11 changes: 9 additions & 2 deletions packages/core/test/fixture/escapeTables.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
["a=b", "a\\=b"],
["a b", "a\\ b"],
["a b=c", "a\\ b\\=c"],
["'a' \\b=c\\,\\\\\"d\"", "'a'\\ \\b\\=c\\\\,\\\\\"d\""]
["'a' \\b=c\\,\\\\\"d\"", "'a'\\ \\b\\=c\\\\,\\\\\"d\""],
["new\nline", "new\\nline"],
["carriage\rreturn", "carriage\\rreturn"],
["t\tab", "t\\tab"]
],
"measurement": [
["ab", "ab"],
["a,", "a\\,"],
["a,b", "a\\,b"],
["a b", "a\\ b"],
["a b,c", "a\\ b\\,c"]
["a b,c", "a\\ b\\,c"],
["new\nline", "new\\nline"],
["carriage\rreturn", "carriage\\rreturn"],
["t\tab", "t\\tab"],
["equ=al", "equ=al"]
],
"quoted": [
["ab", "\"ab\""],
Expand Down
0