Skip to content

Add support for buffer.isUtf8 #2110

Closed
Closed
@JMTK

Description

@JMTK

Is there an existing issue for this?

  • I've searched for any related issues and avoided creating a duplicate issue.

Description

New feature in Node 19.4:
https://nodejs.org/dist/latest-v19.x/docs/api/buffer.html#bufferisutf8input

Tested it locally doing something like this. I haven't fully performance tested. I can open a PR if you're open to the idea

const buffer = require('node:buffer');
// ...
/* istanbul ignore else  */
if (!process.env.WS_NATIVE_UTF8_VALIDATE && typeof buffer.isUtf8 === 'function') {
  try {
    console.log("Using native Utf8!");
    module.exports.isValidUTF8 = buffer.isUtf8;
  } catch (e) {
    // Continue regardless of the error.
  }
}
else if (!process.env.WS_NO_UTF_8_VALIDATE) {
  try {
    const isValidUTF8 = require('utf-8-validate');

    module.exports.isValidUTF8 = function (buf) {
      return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
    };
  } catch (e) {
    // Continue regardless of the error.
  }
}

and it seemed to work ok

ws version

8.11.0

Node.js Version

19.4.0

System

N/A

Expected result

N/A

Actual result

N/A

Attachments

Testing on my Discord bot
image

Ran all tests:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions