1
1
'use strict' ;
2
2
3
- try {
4
- const isValidUTF8 = require ( 'utf-8-validate' ) ;
5
-
6
- exports . isValidUTF8 =
7
- typeof isValidUTF8 === 'object'
8
- ? isValidUTF8 . Validation . isValidUTF8 // utf-8-validate@<3.0.0
9
- : isValidUTF8 ;
10
- } catch ( e ) /* istanbul ignore next */ {
11
- exports . isValidUTF8 = ( ) => true ;
12
- }
13
-
14
3
/**
15
4
* Checks if a status code is allowed in a close frame.
16
5
*
17
6
* @param {Number } code The status code
18
7
* @return {Boolean } `true` if the status code is valid, else `false`
19
8
* @public
20
9
*/
21
- exports . isValidStatusCode = ( code ) => {
10
+ function isValidStatusCode ( code ) {
22
11
return (
23
12
( code >= 1000 &&
24
13
code <= 1014 &&
@@ -27,4 +16,89 @@ exports.isValidStatusCode = (code) => {
27
16
code !== 1006 ) ||
28
17
( code >= 3000 && code <= 4999 )
29
18
) ;
30
- } ;
19
+ }
20
+
21
+ /**
22
+ * Checks if a given buffer contains only correct UTF-8.
23
+ * Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
24
+ * Markus Kuhn.
25
+ *
26
+ * @param {Buffer } buf The buffer to check
27
+ * @return {Boolean } `true` if `buf` contains only correct UTF-8, else `false`
28
+ * @public
29
+ */
30
+ function _isValidUTF8 ( buf ) {
31
+ const len = buf . length ;
32
+ let i = 0 ;
33
+
34
+ while ( i < len ) {
35
+ if ( buf [ i ] < 0x80 ) {
36
+ // 0xxxxxxx
37
+ i ++ ;
38
+ } else if ( ( buf [ i ] & 0xe0 ) === 0xc0 ) {
39
+ // 110xxxxx 10xxxxxx
40
+ if (
41
+ i + 1 === len ||
42
+ ( buf [ i + 1 ] & 0xc0 ) !== 0x80 ||
43
+ ( buf [ i ] & 0xfe ) === 0xc0 // Overlong
44
+ ) {
45
+ return false ;
46
+ } else {
47
+ i += 2 ;
48
+ }
49
+ } else if ( ( buf [ i ] & 0xf0 ) === 0xe0 ) {
50
+ // 1110xxxx 10xxxxxx 10xxxxxx
51
+ if (
52
+ i + 2 >= len ||
53
+ ( buf [ i + 1 ] & 0xc0 ) !== 0x80 ||
54
+ ( buf [ i + 2 ] & 0xc0 ) !== 0x80 ||
55
+ ( buf [ i ] === 0xe0 && ( buf [ i + 1 ] & 0xe0 ) === 0x80 ) || // Overlong
56
+ ( buf [ i ] === 0xed && ( buf [ i + 1 ] & 0xe0 ) === 0xa0 ) // Surrogate (U+D800 - U+DFFF)
57
+ ) {
58
+ return false ;
59
+ } else {
60
+ i += 3 ;
61
+ }
62
+ } else if ( ( buf [ i ] & 0xf8 ) === 0xf0 ) {
63
+ // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
64
+ if (
65
+ i + 3 >= len ||
66
+ ( buf [ i + 1 ] & 0xc0 ) !== 0x80 ||
67
+ ( buf [ i + 2 ] & 0xc0 ) !== 0x80 ||
68
+ ( buf [ i + 3 ] & 0xc0 ) !== 0x80 ||
69
+ ( buf [ i ] === 0xf0 && ( buf [ i + 1 ] & 0xf0 ) === 0x80 ) || // Overlong
70
+ ( buf [ i ] === 0xf4 && buf [ i + 1 ] > 0x8f ) ||
71
+ buf [ i ] > 0xf4 // > U+10FFFF
72
+ ) {
73
+ return false ;
74
+ } else {
75
+ i += 4 ;
76
+ }
77
+ } else {
78
+ return false ;
79
+ }
80
+ }
81
+
82
+ return true ;
83
+ }
84
+
85
+ try {
86
+ let isValidUTF8 = require ( 'utf-8-validate' ) ;
87
+
88
+ /* istanbul ignore if */
89
+ if ( typeof isValidUTF8 === 'object' ) {
90
+ isValidUTF8 = isValidUTF8 . Validation . isValidUTF8 ; // utf-8-validate@<3.0.0
91
+ }
92
+
93
+ module . exports = {
94
+ isValidStatusCode,
95
+ isValidUTF8 ( buf ) {
96
+ return buf . length < 150 ? _isValidUTF8 ( buf ) : isValidUTF8 ( buf ) ;
97
+ }
98
+ } ;
99
+ } catch ( e ) /* istanbul ignore next */ {
100
+ module . exports = {
101
+ isValidStatusCode,
102
+ isValidUTF8 : _isValidUTF8
103
+ } ;
104
+ }
0 commit comments