|
| 1 | +import { Buffer } from 'node:buffer'; |
| 2 | +import { describe, it, strict } from 'poku'; |
| 3 | +import Packet from '../../../lib/packets/packet.js'; |
| 4 | +import getBinaryParser from '../../../lib/parsers/binary_parser.js'; |
| 5 | +import getStaticBinaryParser from '../../../lib/parsers/static_binary_parser.js'; |
| 6 | +import getStaticTextParser from '../../../lib/parsers/static_text_parser.js'; |
| 7 | +import getTextParser from '../../../lib/parsers/text_parser.js'; |
| 8 | + |
| 9 | +// With supportBigNumbers, integers inside JSON documents that cannot be |
| 10 | +// accurately represented as JavaScript Numbers must arrive as exact |
| 11 | +// String objects (mirroring BIGINT column behaviour) instead of being |
| 12 | +// silently rounded by JSON.parse. Exactness requires JSON.parse source |
| 13 | +// access (Node.js 22+); older runtimes keep plain JSON.parse behaviour. |
| 14 | +const sourceAccessSupported = (() => { |
| 15 | + let supported = false; |
| 16 | + JSON.parse('0', (_key: unknown, value: unknown, context?: unknown) => { |
| 17 | + supported = |
| 18 | + context !== undefined && |
| 19 | + typeof (context as { source?: unknown }).source === 'string'; |
| 20 | + return value; |
| 21 | + }); |
| 22 | + return supported; |
| 23 | +})(); |
| 24 | + |
| 25 | +// 9007199254740993 exceeds Number.MAX_SAFE_INTEGER; JSON.parse rounds it |
| 26 | +// to 9007199254740992. Safe integers, floats (including unsafe-magnitude |
| 27 | +// decimals and exponent notation, which stay lossy doubles), numeric |
| 28 | +// strings and nulls must pass through untouched. |
| 29 | +const raw = Buffer.from( |
| 30 | + '{"big": 9007199254740993, "neg": -9007199254740993, ' + |
| 31 | + '"safe": 42, "float": 1.5, "dec": 9007199254740993.5, ' + |
| 32 | + '"exp": 9e30, "str": "9007199254740993", "nil": null}', |
| 33 | + 'utf8' |
| 34 | +); |
| 35 | +strict.ok(raw.length < 251); |
| 36 | + |
| 37 | +const exactValue = { |
| 38 | + big: '9007199254740993', |
| 39 | + neg: '-9007199254740993', |
| 40 | + safe: 42, |
| 41 | + float: 1.5, |
| 42 | + dec: 9007199254740994, // 9007199254740993.5 rounds to this double in both modes |
| 43 | + exp: 9e30, |
| 44 | + str: '9007199254740993', |
| 45 | + nil: null, |
| 46 | +}; |
| 47 | + |
| 48 | +const roundedValue = { |
| 49 | + big: 9007199254740992, |
| 50 | + neg: -9007199254740992, |
| 51 | + safe: 42, |
| 52 | + float: 1.5, |
| 53 | + dec: 9007199254740994, // 9007199254740993.5 rounds to this double in both modes |
| 54 | + exp: 9e30, |
| 55 | + str: '9007199254740993', |
| 56 | + nil: null, |
| 57 | +}; |
| 58 | + |
| 59 | +const expected = sourceAccessSupported ? exactValue : roundedValue; |
| 60 | + |
| 61 | +// JSON columns can hold bare scalars too; the reviver must handle the |
| 62 | +// root value (key '') like any other |
| 63 | +const rawRootScalar = Buffer.from('9007199254740993', 'utf8'); |
| 64 | +const expectedRootScalar = sourceAccessSupported |
| 65 | + ? '9007199254740993' |
| 66 | + : 9007199254740992; |
| 67 | +const rawRootNull = Buffer.from('null', 'utf8'); |
| 68 | + |
| 69 | +// MySQL JSON column (charset 63/BINARY, decoded as utf8) |
| 70 | +const mysqlJsonField = { |
| 71 | + name: 'j', |
| 72 | + columnType: 0xf5, // JSON |
| 73 | + characterSet: 63, |
| 74 | + encoding: 'binary', |
| 75 | + flags: 144, |
| 76 | + decimals: 0, |
| 77 | + columnLength: 4294967295, |
| 78 | + schema: 'test', |
| 79 | + table: 't', |
| 80 | + orgName: 'j', |
| 81 | + orgTable: 't', |
| 82 | +}; |
| 83 | + |
| 84 | +// MariaDB JSON column: LONG_BLOB identified via extended metadata |
| 85 | +const mariadbJsonField = { |
| 86 | + name: 'j', |
| 87 | + columnType: 0xfb, // LONG_BLOB |
| 88 | + characterSet: 224, |
| 89 | + encoding: 'utf8', |
| 90 | + flags: 144, |
| 91 | + decimals: 39, |
| 92 | + columnLength: 4294967295, |
| 93 | + schema: 'test', |
| 94 | + table: 't', |
| 95 | + orgName: 'j', |
| 96 | + orgTable: 't', |
| 97 | + extendedTypeName: undefined, |
| 98 | + extendedFormat: 'json', |
| 99 | +}; |
| 100 | + |
| 101 | +const textRowPacket = (payload = raw) => { |
| 102 | + const buf = Buffer.concat([ |
| 103 | + Buffer.alloc(4), |
| 104 | + Buffer.from([payload.length]), |
| 105 | + payload, |
| 106 | + ]); |
| 107 | + return new Packet(0, buf, 0, buf.length); |
| 108 | +}; |
| 109 | + |
| 110 | +const binaryRowPacket = (payload = raw) => { |
| 111 | + const buf = Buffer.concat([ |
| 112 | + Buffer.alloc(4), |
| 113 | + Buffer.from([0]), // status byte |
| 114 | + Buffer.from([0]), // null bitmap |
| 115 | + Buffer.from([payload.length]), |
| 116 | + payload, |
| 117 | + ]); |
| 118 | + return new Packet(0, buf, 0, buf.length); |
| 119 | +}; |
| 120 | + |
| 121 | +const options = {}; |
| 122 | + |
| 123 | +// the compiled row classes are untyped internals |
| 124 | +const jsonOf = (row: object): unknown => (row as { j: unknown }).j; |
| 125 | + |
| 126 | +for (const [label, field] of [ |
| 127 | + ['MySQL JSON', mysqlJsonField], |
| 128 | + ['MariaDB extended-format JSON', mariadbJsonField], |
| 129 | +] as const) { |
| 130 | + const fields = [field]; |
| 131 | + |
| 132 | + describe(`supportBigNumbers inside ${label} values`, () => { |
| 133 | + it('text parser returns unsafe integers exactly', () => { |
| 134 | + const Parser = getTextParser(fields, options, { |
| 135 | + supportBigNumbers: true, |
| 136 | + }); |
| 137 | + const row = new Parser(fields).next(textRowPacket(), fields, options); |
| 138 | + strict.deepEqual(jsonOf(row), expected); |
| 139 | + }); |
| 140 | + |
| 141 | + it('binary parser returns unsafe integers exactly', () => { |
| 142 | + const Parser = getBinaryParser(fields, options, { |
| 143 | + supportBigNumbers: true, |
| 144 | + }); |
| 145 | + const row = new Parser().next(binaryRowPacket(), fields, options); |
| 146 | + strict.deepEqual(jsonOf(row), expected); |
| 147 | + }); |
| 148 | + |
| 149 | + it('static text parser returns unsafe integers exactly', () => { |
| 150 | + const parser = getStaticTextParser(fields, options, { |
| 151 | + supportBigNumbers: true, |
| 152 | + }); |
| 153 | + const row = parser.next(textRowPacket(), fields, options); |
| 154 | + strict.deepEqual(jsonOf(row), expected); |
| 155 | + }); |
| 156 | + |
| 157 | + it('static binary parser returns unsafe integers exactly', () => { |
| 158 | + const Parser = getStaticBinaryParser(fields, options, { |
| 159 | + supportBigNumbers: true, |
| 160 | + }); |
| 161 | + const row = new Parser().next(binaryRowPacket(), fields, options); |
| 162 | + strict.deepEqual(jsonOf(row), expected); |
| 163 | + }); |
| 164 | + |
| 165 | + it('without supportBigNumbers keeps plain JSON.parse behaviour', () => { |
| 166 | + const Parser = getTextParser(fields, options, {}); |
| 167 | + const row = new Parser(fields).next(textRowPacket(), fields, options); |
| 168 | + strict.deepEqual(jsonOf(row), roundedValue); |
| 169 | + }); |
| 170 | + |
| 171 | + it('jsonStrings still returns the raw (always exact) text', () => { |
| 172 | + const Parser = getTextParser(fields, options, { |
| 173 | + supportBigNumbers: true, |
| 174 | + jsonStrings: true, |
| 175 | + }); |
| 176 | + const row = new Parser(fields).next(textRowPacket(), fields, options); |
| 177 | + strict.equal(jsonOf(row), raw.toString('utf8')); |
| 178 | + }); |
| 179 | + |
| 180 | + it('handles a bare unsafe integer as the root value', () => { |
| 181 | + const Parser = getTextParser(fields, options, { |
| 182 | + supportBigNumbers: true, |
| 183 | + }); |
| 184 | + const row = new Parser(fields).next( |
| 185 | + textRowPacket(rawRootScalar), |
| 186 | + fields, |
| 187 | + options |
| 188 | + ); |
| 189 | + strict.equal(jsonOf(row), expectedRootScalar); |
| 190 | + }); |
| 191 | + |
| 192 | + it('handles a JSON null root value', () => { |
| 193 | + const Parser = getTextParser(fields, options, { |
| 194 | + supportBigNumbers: true, |
| 195 | + }); |
| 196 | + const row = new Parser(fields).next( |
| 197 | + textRowPacket(rawRootNull), |
| 198 | + fields, |
| 199 | + options |
| 200 | + ); |
| 201 | + strict.equal(jsonOf(row), null); |
| 202 | + }); |
| 203 | + }); |
| 204 | +} |
0 commit comments