@@ -112,24 +112,26 @@ export function fluxString(value: any): FluxParameterLike {
112
112
* @throws Error if the the value cannot be sanitized
113
113
*/
114
114
export function sanitizeFloat ( value : any ) : string {
115
- if ( typeof value === 'number' ) {
116
- if ( ! isFinite ( value ) ) {
117
- throw new Error ( `not a flux float: ${ value } ` )
115
+ const val = Number ( value )
116
+ if ( ! isFinite ( val ) ) {
117
+ if ( typeof value === 'number' ) {
118
+ return `float(v: "${ val } ")`
118
119
}
119
- return value . toString ( )
120
+ throw new Error ( `not a flux float: ${ value } ` )
120
121
}
121
- const val = String ( value )
122
- let dot = false
123
- for ( const c of val ) {
122
+ // try to return a flux float literal if possible
123
+ // https://docs.influxdata.com/flux/v0.x/data-types/basic/float/#float-syntax
124
+ const strVal = val . toString ( )
125
+ let hasDot = false
126
+ for ( const c of strVal ) {
127
+ if ( ( c >= '0' && c <= '9' ) || c == '-' ) continue
124
128
if ( c === '.' ) {
125
- if ( dot ) throw new Error ( `not a flux float: ${ val } ` )
126
- dot = ! dot
129
+ hasDot = true
127
130
continue
128
131
}
129
- if ( c !== '.' && c !== '-' && ( c < '0' || c > '9' ) )
130
- throw new Error ( `not a flux float: ${ val } ` )
132
+ return `float(v: "${ strVal } ")`
131
133
}
132
- return val
134
+ return hasDot ? strVal : strVal + '.0'
133
135
}
134
136
/**
135
137
* Creates a flux float literal.
@@ -139,16 +141,41 @@ export function fluxFloat(value: any): FluxParameterLike {
139
141
}
140
142
141
143
/**
142
- * Creates a flux integer literal.
144
+ * Sanitizes integer value to avoid injections.
145
+ * @param value - InfluxDB integer literal
146
+ * @returns sanitized integer value
147
+ * @throws Error if the the value cannot be sanitized
143
148
*/
144
- export function fluxInteger ( value : any ) : FluxParameterLike {
145
- const val = sanitizeFloat ( value )
149
+ export function sanitizeInteger ( value : any ) : string {
150
+ // https://docs.influxdata.com/flux/v0.x/data-types/basic/int/
151
+ // Min value: -9223372036854775808
152
+ // Max value: 9223372036854775807
153
+ // "9223372036854775807".length === 19
154
+ const strVal = String ( value )
155
+ const negative = strVal . startsWith ( '-' )
156
+ const val = negative ? strVal . substring ( 1 ) : strVal
157
+ if ( val . length === 0 || val . length > 19 ) {
158
+ throw new Error ( `not a flux integer: ${ strVal } ` )
159
+ }
146
160
for ( const c of val ) {
147
- if ( c === '.' ) {
148
- throw new Error ( `not a flux integer: ${ val } ` )
161
+ if ( c < '0' || c > '9' ) throw new Error ( `not a flux integer: ${ strVal } ` )
162
+ }
163
+ if ( val . length === 19 ) {
164
+ if ( negative && val > '9223372036854775808' ) {
165
+ throw new Error ( `flux integer out of bounds: ${ strVal } ` )
166
+ }
167
+ if ( ! negative && val > '9223372036854775807' ) {
168
+ throw new Error ( `flux integer out of bounds: ${ strVal } ` )
149
169
}
150
170
}
151
- return new FluxParameter ( val )
171
+ return strVal
172
+ }
173
+
174
+ /**
175
+ * Creates a flux integer literal.
176
+ */
177
+ export function fluxInteger ( value : any ) : FluxParameterLike {
178
+ return new FluxParameter ( sanitizeInteger ( value ) )
152
179
}
153
180
154
181
function sanitizeDateTime ( value : any ) : string {
@@ -170,14 +197,19 @@ export function fluxDuration(value: any): FluxParameterLike {
170
197
}
171
198
172
199
function sanitizeRegExp ( value : any ) : string {
173
- return `regexp.compile(v: "${ sanitizeString ( value ) } ")`
200
+ if ( value instanceof RegExp ) {
201
+ return value . toString ( )
202
+ }
203
+ return new RegExp ( value ) . toString ( )
174
204
}
175
205
176
206
/**
177
- * Creates flux regexp literal.
207
+ * Creates flux regexp literal out of a regular expression. See
208
+ * https://docs.influxdata.com/flux/v0.x/data-types/basic/regexp/#regular-expression-syntax
209
+ * for details.
178
210
*/
179
211
export function fluxRegExp ( value : any ) : FluxParameterLike {
180
- // let the server decide if it can be parsed
212
+ // let the server decide if a regexp can be parsed
181
213
return new FluxParameter ( sanitizeRegExp ( value ) )
182
214
}
183
215
@@ -216,6 +248,9 @@ export function toFluxValue(value: any): string {
216
248
} else if ( typeof value === 'string' ) {
217
249
return `"${ sanitizeString ( value ) } "`
218
250
} else if ( typeof value === 'number' ) {
251
+ if ( Number . isSafeInteger ( value ) ) {
252
+ return sanitizeInteger ( value )
253
+ }
219
254
return sanitizeFloat ( value )
220
255
} else if ( typeof value === 'object' ) {
221
256
if ( typeof value [ FLUX_VALUE ] === 'function' ) {
0 commit comments