sidorares / sidorares/node-mysql2

RangeError on connection with charset with internal number above 255

Open
#1,507 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

I need to connect to a MySQL 8 database with the charset UTF8MB4 and collation UTF8MB4_DE_PB_0900_AI_CI, but instead of a connection I get a RangeError.

The source seems to be the charset/collation: UTF8MB4_DE_PB_0900_AI_CI has the internal number "256", and the RangeError says the value must be "<= 255" but received "256". Further tests show, collations with the internal number up to 255 work fine, 256 and above do not.

My MySQL has the version 8.0.23 and is hosted on AWS RDS with their default option- and parameter-groups, accessed through a tunnel through a bastion host. For "mysql2" I'm using version "2.3.3". The simplest example to reproduce this is:

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_DATABASE,
  // charset: 'UTF8MB4_0900_AI_CI' // Charset 255 -> works fine
  charset: 'UTF8MB4_DE_PB_0900_AI_CI', // Charset 256 -> error
  // charset: 'UTF8MB4_RO_0900_AI_CI', // Charset 259 -> error
});

// simple query
connection.query(
  'SELECT * FROM db_counter',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

Executing this will result in the error below. Changing the charset for example to "UTF8MB4_0900_AI_CI" (internal number 255) will produce a connection and output the content of the query.

$DB_USER=<user> DB_PASSWORD=<password> DB_DATABASE=<database> node src.js
internal/buffer.js:733
    throw new ERR_OUT_OF_RANGE('value', `>= ${min} and <= ${max}`, value);
    ^

RangeError [ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= 0 and <= 255. Received 256
    at writeU_Int8 (internal/buffer.js:733:11)
    at Buffer.writeUInt8 (internal/buffer.js:743:10)
    at Packet.writeInt8 (/my/local/path/mysql-test/node_modules/mysql2/lib/packets/packet.js:752:17)
    at HandshakeResponse.serializeResponse (/my/local/path/mysql-test/node_modules/mysql2/lib/packets/handshake_response.js:46:12)
    at HandshakeResponse.toPacket (/my/local/path/mysql-test/node_modules/mysql2/lib/packets/handshake_response.js:100:17)
    at ClientHandshake.sendCredentials (/my/local/path/mysql-test/node_modules/mysql2/lib/commands/client_handshake.js:69:46)
    at ClientHandshake.handshakeInit (/my/local/path/mysql-test/node_modules/mysql2/lib/commands/client_handshake.js:142:12)
    at ClientHandshake.execute (/my/local/path/mysql-test/node_modules/mysql2/lib/commands/command.js:45:22)
    at Connection.handlePacket (/my/local/path/mysql-test/node_modules/mysql2/lib/connection.js:456:32)
    at PacketParser.onPacket (/my/local/path/mysql-test/node_modules/mysql2/lib/connection.js:85:12) {
  code: 'ERR_OUT_OF_RANGE'
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the connection using the issue's charset examples, then inspect lib/packets/packet.js, lib/packets/handshake_response.js, and lib/commands/client_handshake.js named in the stack trace. Compare handling of collations at 255 and above; done means a MySQL 8 connection using the reported charset succeeds and has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, node.js, typescript
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.