diff --git a/README.md b/README.md index 9fcd51af3..4539df294 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/lib/validation.js b/lib/validation.js index 44fc20290..c352e6ea7 100644 --- a/lib/validation.js +++ b/lib/validation.js @@ -1,5 +1,7 @@ 'use strict'; +const { isUtf8 } = require('buffer'); + // // Allowed token characters: // @@ -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.