Skip to content

Commit 42d79f6

Browse files
committedJan 7, 2023
[minor] Use buffer.isUtf8() if possible
Closes #2110
1 parent ff63bba commit 42d79f6

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed
 

‎README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ npm install ws
5858
### Opt-in for performance
5959

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

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

78+
The `utf-8-validate` module is not needed and is not required, even if it is
79+
already installed, regardless of the value of the `WS_NO_UTF_8_VALIDATE`
80+
environment variable, if [`buffer.isUtf8()`][] is available.
81+
7882
## API docs
7983

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

483487
[MIT](LICENSE)
484488

489+
[`buffer.isutf8()`]: https://nodejs.org/api/buffer.html#bufferisutf8input
485490
[changelog]: https://github.com/websockets/ws/releases
486491
[client-report]: http://websockets.github.io/ws/autobahn/clients/
487492
[https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent

‎lib/validation.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const { isUtf8 } = require('buffer');
4+
35
//
46
// Allowed token characters:
57
//
@@ -111,13 +113,16 @@ module.exports = {
111113
tokenChars
112114
};
113115

114-
/* istanbul ignore else */
115-
if (!process.env.WS_NO_UTF_8_VALIDATE) {
116+
if (isUtf8) {
117+
module.exports.isValidUTF8 = function (buf) {
118+
return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
119+
};
120+
} /* istanbul ignore else */ else if (!process.env.WS_NO_UTF_8_VALIDATE) {
116121
try {
117122
const isValidUTF8 = require('utf-8-validate');
118123

119124
module.exports.isValidUTF8 = function (buf) {
120-
return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
125+
return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
121126
};
122127
} catch (e) {
123128
// Continue regardless of the error.

0 commit comments

Comments
 (0)
Please sign in to comment.