-
-
Notifications
You must be signed in to change notification settings - Fork 671
feat: return unsafe integers inside JSON columns as exact strings with supportBigNumbers #4388
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,204 @@ | ||
| import { Buffer } from 'node:buffer'; | ||
| import { describe, it, strict } from 'poku'; | ||
| import Packet from '../../../lib/packets/packet.js'; | ||
| import getBinaryParser from '../../../lib/parsers/binary_parser.js'; | ||
| import getStaticBinaryParser from '../../../lib/parsers/static_binary_parser.js'; | ||
| import getStaticTextParser from '../../../lib/parsers/static_text_parser.js'; | ||
| import getTextParser from '../../../lib/parsers/text_parser.js'; | ||
|
|
||
| // With supportBigNumbers, integers inside JSON documents that cannot be | ||
| // accurately represented as JavaScript Numbers must arrive as exact | ||
| // String objects (mirroring BIGINT column behaviour) instead of being | ||
| // silently rounded by JSON.parse. Exactness requires JSON.parse source | ||
| // access (Node.js 22+); older runtimes keep plain JSON.parse behaviour. | ||
| const sourceAccessSupported = (() => { | ||
| let supported = false; | ||
| JSON.parse('0', (_key: unknown, value: unknown, context?: unknown) => { | ||
| supported = | ||
| context !== undefined && | ||
| typeof (context as { source?: unknown }).source === 'string'; | ||
| return value; | ||
| }); | ||
| return supported; | ||
| })(); | ||
|
|
||
| // 9007199254740993 exceeds Number.MAX_SAFE_INTEGER; JSON.parse rounds it | ||
| // to 9007199254740992. Safe integers, floats (including unsafe-magnitude | ||
| // decimals and exponent notation, which stay lossy doubles), numeric | ||
| // strings and nulls must pass through untouched. | ||
| const raw = Buffer.from( | ||
| '{"big": 9007199254740993, "neg": -9007199254740993, ' + | ||
| '"safe": 42, "float": 1.5, "dec": 9007199254740993.5, ' + | ||
| '"exp": 9e30, "str": "9007199254740993", "nil": null}', | ||
| 'utf8' | ||
| ); | ||
| strict.ok(raw.length < 251); | ||
|
|
||
| const exactValue = { | ||
| big: '9007199254740993', | ||
| neg: '-9007199254740993', | ||
| safe: 42, | ||
| float: 1.5, | ||
| dec: 9007199254740994, // 9007199254740993.5 rounds to this double in both modes | ||
| exp: 9e30, | ||
| str: '9007199254740993', | ||
| nil: null, | ||
| }; | ||
|
|
||
| const roundedValue = { | ||
| big: 9007199254740992, | ||
| neg: -9007199254740992, | ||
| safe: 42, | ||
| float: 1.5, | ||
| dec: 9007199254740994, // 9007199254740993.5 rounds to this double in both modes | ||
| exp: 9e30, | ||
| str: '9007199254740993', | ||
| nil: null, | ||
| }; | ||
|
|
||
| const expected = sourceAccessSupported ? exactValue : roundedValue; | ||
|
|
||
| // JSON columns can hold bare scalars too; the reviver must handle the | ||
| // root value (key '') like any other | ||
| const rawRootScalar = Buffer.from('9007199254740993', 'utf8'); | ||
| const expectedRootScalar = sourceAccessSupported | ||
| ? '9007199254740993' | ||
| : 9007199254740992; | ||
| const rawRootNull = Buffer.from('null', 'utf8'); | ||
|
|
||
| // MySQL JSON column (charset 63/BINARY, decoded as utf8) | ||
| const mysqlJsonField = { | ||
| name: 'j', | ||
| columnType: 0xf5, // JSON | ||
| characterSet: 63, | ||
| encoding: 'binary', | ||
| flags: 144, | ||
| decimals: 0, | ||
| columnLength: 4294967295, | ||
| schema: 'test', | ||
| table: 't', | ||
| orgName: 'j', | ||
| orgTable: 't', | ||
| }; | ||
|
|
||
| // MariaDB JSON column: LONG_BLOB identified via extended metadata | ||
| const mariadbJsonField = { | ||
| name: 'j', | ||
| columnType: 0xfb, // LONG_BLOB | ||
| characterSet: 224, | ||
| encoding: 'utf8', | ||
| flags: 144, | ||
| decimals: 39, | ||
| columnLength: 4294967295, | ||
| schema: 'test', | ||
| table: 't', | ||
| orgName: 'j', | ||
| orgTable: 't', | ||
| extendedTypeName: undefined, | ||
| extendedFormat: 'json', | ||
| }; | ||
|
|
||
| const textRowPacket = (payload = raw) => { | ||
| const buf = Buffer.concat([ | ||
| Buffer.alloc(4), | ||
| Buffer.from([payload.length]), | ||
| payload, | ||
| ]); | ||
| return new Packet(0, buf, 0, buf.length); | ||
| }; | ||
|
|
||
| const binaryRowPacket = (payload = raw) => { | ||
| const buf = Buffer.concat([ | ||
| Buffer.alloc(4), | ||
| Buffer.from([0]), // status byte | ||
| Buffer.from([0]), // null bitmap | ||
| Buffer.from([payload.length]), | ||
| payload, | ||
| ]); | ||
| return new Packet(0, buf, 0, buf.length); | ||
| }; | ||
|
|
||
| const options = {}; | ||
|
|
||
| // the compiled row classes are untyped internals | ||
| const jsonOf = (row: object): unknown => (row as { j: unknown }).j; | ||
|
|
||
| for (const [label, field] of [ | ||
| ['MySQL JSON', mysqlJsonField], | ||
| ['MariaDB extended-format JSON', mariadbJsonField], | ||
| ] as const) { | ||
| const fields = [field]; | ||
|
|
||
| describe(`supportBigNumbers inside ${label} values`, () => { | ||
| it('text parser returns unsafe integers exactly', () => { | ||
| const Parser = getTextParser(fields, options, { | ||
| supportBigNumbers: true, | ||
| }); | ||
| const row = new Parser(fields).next(textRowPacket(), fields, options); | ||
| strict.deepEqual(jsonOf(row), expected); | ||
| }); | ||
|
|
||
| it('binary parser returns unsafe integers exactly', () => { | ||
| const Parser = getBinaryParser(fields, options, { | ||
| supportBigNumbers: true, | ||
| }); | ||
| const row = new Parser().next(binaryRowPacket(), fields, options); | ||
| strict.deepEqual(jsonOf(row), expected); | ||
| }); | ||
|
|
||
| it('static text parser returns unsafe integers exactly', () => { | ||
| const parser = getStaticTextParser(fields, options, { | ||
| supportBigNumbers: true, | ||
| }); | ||
| const row = parser.next(textRowPacket(), fields, options); | ||
| strict.deepEqual(jsonOf(row), expected); | ||
| }); | ||
|
|
||
| it('static binary parser returns unsafe integers exactly', () => { | ||
| const Parser = getStaticBinaryParser(fields, options, { | ||
| supportBigNumbers: true, | ||
| }); | ||
| const row = new Parser().next(binaryRowPacket(), fields, options); | ||
| strict.deepEqual(jsonOf(row), expected); | ||
| }); | ||
|
|
||
| it('without supportBigNumbers keeps plain JSON.parse behaviour', () => { | ||
| const Parser = getTextParser(fields, options, {}); | ||
| const row = new Parser(fields).next(textRowPacket(), fields, options); | ||
| strict.deepEqual(jsonOf(row), roundedValue); | ||
| }); | ||
|
|
||
| it('jsonStrings still returns the raw (always exact) text', () => { | ||
| const Parser = getTextParser(fields, options, { | ||
| supportBigNumbers: true, | ||
| jsonStrings: true, | ||
| }); | ||
| const row = new Parser(fields).next(textRowPacket(), fields, options); | ||
| strict.equal(jsonOf(row), raw.toString('utf8')); | ||
| }); | ||
|
|
||
| it('handles a bare unsafe integer as the root value', () => { | ||
| const Parser = getTextParser(fields, options, { | ||
| supportBigNumbers: true, | ||
| }); | ||
| const row = new Parser(fields).next( | ||
| textRowPacket(rawRootScalar), | ||
| fields, | ||
| options | ||
| ); | ||
| strict.equal(jsonOf(row), expectedRootScalar); | ||
| }); | ||
|
|
||
| it('handles a JSON null root value', () => { | ||
| const Parser = getTextParser(fields, options, { | ||
| supportBigNumbers: true, | ||
| }); | ||
| const row = new Parser(fields).next( | ||
| textRowPacket(rawRootNull), | ||
| fields, | ||
| options | ||
| ); | ||
| strict.equal(jsonOf(row), null); | ||
| }); | ||
| }); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When comments are exactly what the logic already does next, I think they become unnecessary. The same goes for other comments that explain the implementation. What do you think about using comments only for things that the logic alone doesn't explain, or for the reasoning behind the decisions? 🙋🏻♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to prune the comments.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! It looks like GitHub Actions is down (unrelated to the PR). I'm going to restart the workflow (Done).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pruned in f3294cd - dropped everything that narrated the control flow, kept three short rationale notes (the TC39 proposal name/availability behind the feature detection, why the regex is 16 digits, and that parseJson reuses supportBigNumbers' existing BIGINT out-of-range contract rather than adding a new option).