Skip to content

Commit

Permalink
[minor] Use buffer.isUtf8() if possible
Browse files Browse the repository at this point in the history
Closes #2110
  • Loading branch information
lpinca committed Jan 7, 2023
1 parent ff63bba commit 42d79f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ npm install ws
### Opt-in for performance

There are 2 optional modules that can be installed along side with the ws
module. These modules are binary addons which improve certain operations.
Prebuilt binaries are available for the most popular platforms so you don't
necessarily need to have a C++ compiler installed on your machine.
module. These modules are binary addons that improve the performance of certain
operations. Prebuilt binaries are available for the most popular platforms so
you don't necessarily need to have a C++ compiler installed on your machine.

- `npm install --save-optional bufferutil`: Allows to efficiently perform
operations such as masking and unmasking the data payload of the WebSocket
Expand All @@ -75,6 +75,10 @@ variables. These might be useful to enhance security in systems where a user can
put a package in the package search path of an application of another user, due
to how the Node.js resolver algorithm works.

The `utf-8-validate` module is not needed and is not required, even if it is
already installed, regardless of the value of the `WS_NO_UTF_8_VALIDATE`
environment variable, if [`buffer.isUtf8()`][] is available.

## API docs

See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
Expand Down Expand Up @@ -482,6 +486,7 @@ We're using the GitHub [releases][changelog] for changelog entries.

[MIT](LICENSE)

[`buffer.isutf8()`]: https://nodejs.org/api/buffer.html#bufferisutf8input
[changelog]: https://github.com/websockets/ws/releases
[client-report]: http://websockets.github.io/ws/autobahn/clients/
[https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
Expand Down
11 changes: 8 additions & 3 deletions lib/validation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { isUtf8 } = require('buffer');

//
// Allowed token characters:
//
Expand Down Expand Up @@ -111,13 +113,16 @@ module.exports = {
tokenChars
};

/* istanbul ignore else */
if (!process.env.WS_NO_UTF_8_VALIDATE) {
if (isUtf8) {
module.exports.isValidUTF8 = function (buf) {
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
};
} /* istanbul ignore else */ 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);
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
};
} catch (e) {
// Continue regardless of the error.
Expand Down

0 comments on commit 42d79f6

Please sign in to comment.