Skip to content
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

Add support for buffer.isUtf8 #2110

Closed
1 task done
JMTK opened this issue Jan 6, 2023 · 1 comment
Closed
1 task done

Add support for buffer.isUtf8 #2110

JMTK opened this issue Jan 6, 2023 · 1 comment

Comments

@JMTK
Copy link

JMTK commented Jan 6, 2023

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

@lpinca
Copy link
Member

lpinca commented Jan 6, 2023

Yes, I will add it soon. See websockets/utf-8-validate#101 (comment) for benchmarks. Buffer.isUtf8() has the same performance of utf-8-validate@6.

@lpinca lpinca closed this as completed in 42d79f6 Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants