utf-8-validate
Advanced tools
Comparing version 2.0.0 to 3.0.0
111
fallback.js
@@ -9,64 +9,65 @@ /*! | ||
exports.Validation = { | ||
/** | ||
* Checks if a given buffer contains only correct UTF-8. | ||
* Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by | ||
* Markus Kuhn. | ||
* | ||
* @param {Buffer} buf The buffer to check | ||
* @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false` | ||
*/ | ||
isValidUTF8: function (buf) { | ||
if (!Buffer.isBuffer(buf)) { | ||
throw new TypeError('First argument needs to be a buffer'); | ||
} | ||
/** | ||
* Checks if a given buffer contains only correct UTF-8. | ||
* Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by | ||
* Markus Kuhn. | ||
* | ||
* @param {Buffer} buf The buffer to check | ||
* @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false` | ||
* @public | ||
*/ | ||
const isValidUTF8 = (buf) => { | ||
if (!Buffer.isBuffer(buf)) { | ||
throw new TypeError('First argument needs to be a buffer'); | ||
} | ||
var len = buf.length; | ||
var i = 0; | ||
var len = buf.length; | ||
var i = 0; | ||
while (i < len) { | ||
if (buf[i] < 0x80) { // 0xxxxxxx | ||
i++; | ||
} else if ((buf[i] & 0xe0) === 0xc0) { // 110xxxxx 10xxxxxx | ||
if ( | ||
i + 1 === len || | ||
(buf[i + 1] & 0xc0) !== 0x80 || | ||
(buf[i] & 0xfe) === 0xc0 // overlong | ||
) { | ||
return false; | ||
} else { | ||
i += 2; | ||
} | ||
} else if ((buf[i] & 0xf0) === 0xe0) { // 1110xxxx 10xxxxxx 10xxxxxx | ||
if ( | ||
i + 2 >= len || | ||
(buf[i + 1] & 0xc0) !== 0x80 || | ||
(buf[i + 2] & 0xc0) !== 0x80 || | ||
buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80 || // overlong | ||
buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0 // surrogate (U+D800 - U+DFFF) | ||
) { | ||
return false; | ||
} else { | ||
i += 3; | ||
} | ||
} else if ((buf[i] & 0xf8) === 0xf0) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx | ||
if ( | ||
i + 3 >= len || | ||
(buf[i + 1] & 0xc0) !== 0x80 || | ||
(buf[i + 2] & 0xc0) !== 0x80 || | ||
(buf[i + 3] & 0xc0) !== 0x80 || | ||
buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80 || // overlong | ||
buf[i] === 0xf4 && buf[i + 1] > 0x8f || buf[i] > 0xf4 // > U+10FFFF | ||
) { | ||
return false; | ||
} else { | ||
i += 4; | ||
} | ||
while (i < len) { | ||
if (buf[i] < 0x80) { // 0xxxxxxx | ||
i++; | ||
} else if ((buf[i] & 0xe0) === 0xc0) { // 110xxxxx 10xxxxxx | ||
if ( | ||
i + 1 === len || | ||
(buf[i + 1] & 0xc0) !== 0x80 || | ||
(buf[i] & 0xfe) === 0xc0 // overlong | ||
) { | ||
return false; | ||
} else { | ||
i += 2; | ||
} | ||
} else if ((buf[i] & 0xf0) === 0xe0) { // 1110xxxx 10xxxxxx 10xxxxxx | ||
if ( | ||
i + 2 >= len || | ||
(buf[i + 1] & 0xc0) !== 0x80 || | ||
(buf[i + 2] & 0xc0) !== 0x80 || | ||
buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80 || // overlong | ||
buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0 // surrogate (U+D800 - U+DFFF) | ||
) { | ||
return false; | ||
} else { | ||
i += 3; | ||
} | ||
} else if ((buf[i] & 0xf8) === 0xf0) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx | ||
if ( | ||
i + 3 >= len || | ||
(buf[i + 1] & 0xc0) !== 0x80 || | ||
(buf[i + 2] & 0xc0) !== 0x80 || | ||
(buf[i + 3] & 0xc0) !== 0x80 || | ||
buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80 || // overlong | ||
buf[i] === 0xf4 && buf[i + 1] > 0x8f || buf[i] > 0xf4 // > U+10FFFF | ||
) { | ||
return false; | ||
} else { | ||
i += 4; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return true; | ||
}; | ||
module.exports = isValidUTF8; |
{ | ||
"name": "utf-8-validate", | ||
"version": "2.0.0", | ||
"description": "Validate UTF-8 for Web", | ||
"version": "3.0.0", | ||
"description": "Check if a buffer contains valid UTF-8", | ||
"main": "index.js", | ||
@@ -25,3 +25,3 @@ "scripts": { | ||
"bindings": "~1.2.1", | ||
"nan": "~2.4.0" | ||
"nan": "~2.5.0" | ||
}, | ||
@@ -28,0 +28,0 @@ "devDependencies": { |
@@ -7,37 +7,45 @@ # utf-8-validate | ||
WebSocket connections require extensive UTF-8 validation in order to conform to | ||
the specification. This was unfortunately not possible in JavaScript, hence the | ||
need for a binary addon. | ||
Check if a buffer contains valid UTF-8 encoded text. | ||
As the module consists of binary components, it should be used as | ||
`optionalDependency` so when installation fails, it doesn't halt the | ||
installation of your module. There are fallback files available in this | ||
repository. See `fallback.js` for the suggest fallback implementation if | ||
installation fails. | ||
## Installation | ||
``` | ||
npm install utf-8-validate | ||
npm install utf-8-validate --save-optional | ||
``` | ||
The `--save-optional` flag tells npm to save the package in your package.json | ||
under the [`optionalDependencies`](https://docs.npmjs.com/files/package.json#optionaldependencies) | ||
key. | ||
## API | ||
In all examples we assume that you've already required the mdoule as | ||
follows: | ||
The module exports a single function which takes one argument. | ||
```js | ||
'use strict'; | ||
### `isValidUTF8(buffer)` | ||
var validation = require('utf-8-validate').Validation; | ||
``` | ||
Checks whether a buffer contains valid UTF-8. | ||
The module exposes 1 function: | ||
#### Arguments | ||
#### isValidUTF8 | ||
- `buffer` - The buffer to check. | ||
Validate if the passed in buffer contains valid UTF-8 chars. | ||
#### Return value | ||
`true` if the buffer contains only correct UTF-8, else `false`. | ||
#### Exceptions | ||
Throws a `TypeError` exception if the first argument is not a buffer. | ||
#### Example | ||
```js | ||
validation.isValidUTF8(buffer); | ||
'use strict'; | ||
const isValidUTF8 = require('utf-8-validate'); | ||
const buf = Buffer.from([0xf0, 0x90, 0x80, 0x80]); | ||
console.log(isValidUTF8(buf)); | ||
// => true | ||
``` | ||
@@ -44,0 +52,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55
7620
+ Addednan@2.5.1(transitive)
- Removednan@2.4.0(transitive)
Updatednan@~2.5.0