-
-
Notifications
You must be signed in to change notification settings - Fork 641
feat(disableEval
): add static parsers
#3365
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
wellwelwel
merged 19 commits into
sidorares:master
from
thomasgauvin:deoptimise-large-rows
Feb 4, 2025
Merged
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
50e47fa
add static parser, currently disabled
sidorares 3998471
fix PR comments
thomasgauvin eb9d555
apply linting fixes
thomasgauvin 0962bbe
fix linting in tests
thomasgauvin fb34010
Update lib/connection_config.js
thomasgauvin 92876de
Update lib/parsers/static_text_parser.js
thomasgauvin 1c37f0f
chore: improve assertion identification
wellwelwel 87d0ef5
chore: use `verbose` test reporter
wellwelwel 09430e0
ci: increase timeout
wellwelwel fdca5d7
ci: test static parser through all tests and variations
wellwelwel 64ea29b
chore: ensure Buffer when TypeCast is false
wellwelwel 0a9a253
chore: implement `nestTables` to static parser
wellwelwel df47beb
chore: implement `jsonStrings` to static parser
wellwelwel ad642b0
ci: check static parser coverage
wellwelwel f8e9010
ci: improve test coverage identification
wellwelwel 831ec65
chore: improve dynamic properties security
wellwelwel 8e29081
refactor: use `disableEval` instead of `useStaticParser`
wellwelwel 0d5594e
feat(execute): add binary static parser
wellwelwel 61ca972
ci: adapt coverage limits
wellwelwel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
'use strict'; | ||
|
||
const Types = require('../constants/types.js'); | ||
const Charsets = require('../constants/charsets.js'); | ||
const helpers = require('../helpers'); | ||
|
||
const typeNames = []; | ||
for (const t in Types) { | ||
typeNames[Types[t]] = t; | ||
} | ||
|
||
function readField({ packet, type, charset, encoding, config, options }) { | ||
const supportBigNumbers = | ||
options.supportBigNumbers || config.supportBigNumbers; | ||
const bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings; | ||
thomasgauvin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const timezone = options.timezone || config.timezone; | ||
const dateStrings = options.dateStrings || config.dateStrings; | ||
|
||
switch (type) { | ||
case Types.TINY: | ||
case Types.SHORT: | ||
case Types.LONG: | ||
case Types.INT24: | ||
case Types.YEAR: | ||
return packet.parseLengthCodedIntNoBigCheck(); | ||
case Types.LONGLONG: | ||
if (supportBigNumbers && bigNumberStrings) { | ||
return packet.parseLengthCodedIntString(); | ||
} | ||
return packet.parseLengthCodedInt(supportBigNumbers); | ||
case Types.FLOAT: | ||
case Types.DOUBLE: | ||
return packet.parseLengthCodedFloat(); | ||
case Types.NULL: | ||
case Types.DECIMAL: | ||
case Types.NEWDECIMAL: | ||
if (config.decimalNumbers) { | ||
return packet.parseLengthCodedFloat(); | ||
} | ||
return packet.readLengthCodedString("ascii"); | ||
case Types.DATE: | ||
if (helpers.typeMatch(type, dateStrings, Types)) { | ||
return packet.readLengthCodedString("ascii"); | ||
} | ||
return packet.parseDate(timezone); | ||
case Types.DATETIME: | ||
case Types.TIMESTAMP: | ||
if (helpers.typeMatch(type, dateStrings, Types)) { | ||
return packet.readLengthCodedString('ascii'); | ||
} | ||
return packet.parseDateTime(timezone); | ||
case Types.TIME: | ||
return packet.readLengthCodedString('ascii'); | ||
case Types.GEOMETRY: | ||
return packet.parseGeometryValue(); | ||
case Types.JSON: | ||
// Since for JSON columns mysql always returns charset 63 (BINARY), | ||
// we have to handle it according to JSON specs and use "utf8", | ||
// see https://github.com/sidorares/node-mysql2/issues/409 | ||
return JSON.parse(packet.readLengthCodedString('utf8')); | ||
default: | ||
if (charset === Charsets.BINARY) { | ||
return packet.readLengthCodedBuffer(); | ||
} | ||
return packet.readLengthCodedString(encoding); | ||
} | ||
} | ||
|
||
function createTypecastField(field, packet) { | ||
return { | ||
type: typeNames[field.columnType], | ||
length: field.columnLength, | ||
db: field.schema, | ||
table: field.table, | ||
name: field.name, | ||
string: function(encoding = field.encoding) { | ||
if (field.columnType === Types.JSON && encoding === field.encoding) { | ||
// Since for JSON columns mysql always returns charset 63 (BINARY), | ||
// we have to handle it according to JSON specs and use "utf8", | ||
// see https://github.com/sidorares/node-mysql2/issues/1661 | ||
console.warn(`typeCast: JSON column "${field.name}" is interpreted as BINARY by default, recommended to manually set utf8 encoding: \`field.string("utf8")\``); | ||
} | ||
return packet.readLengthCodedString(encoding); | ||
}, | ||
buffer: function() { | ||
return packet.readLengthCodedBuffer(); | ||
}, | ||
geometry: function() { | ||
return packet.parseGeometryValue(); | ||
} | ||
}; | ||
} | ||
|
||
function getTextParser(fields, options, config) { | ||
return { | ||
next(packet, fields, options) { | ||
const result = options.rowsAsArray ? [] : {}; | ||
for (let i = 0; i < fields.length; i++) { | ||
const field = fields[i]; | ||
const typeCast = options.typeCast ? options.typeCast : config.typeCast; | ||
const next = () => readField({ | ||
packet, | ||
type: field.columnType, | ||
encoding: field.encoding, | ||
charset: field.characterSet, | ||
config, | ||
options | ||
}); | ||
let value; | ||
if (typeof typeCast === 'function') { | ||
value = typeCast(createTypecastField(field, packet), next); | ||
} else { | ||
value = next(); | ||
} | ||
if (options.rowsAsArray) { | ||
result.push(value); | ||
} else if (options.nestTables) { | ||
wellwelwel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!result[field.table]) { | ||
result[field.table] = {}; | ||
} | ||
result[field.table][field.name] = value; | ||
} else { | ||
result[field.name] = value; | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
module.exports = getTextParser; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.