1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function isValidStatusCode(code) {
|
11 | return (
|
12 | (code >= 1000 &&
|
13 | code <= 1014 &&
|
14 | code !== 1004 &&
|
15 | code !== 1005 &&
|
16 | code !== 1006) ||
|
17 | (code >= 3000 && code <= 4999)
|
18 | );
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function _isValidUTF8(buf) {
|
31 | const len = buf.length;
|
32 | let i = 0;
|
33 |
|
34 | while (i < len) {
|
35 | if ((buf[i] & 0x80) === 0) {
|
36 |
|
37 | i++;
|
38 | } else if ((buf[i] & 0xe0) === 0xc0) {
|
39 |
|
40 | if (
|
41 | i + 1 === len ||
|
42 | (buf[i + 1] & 0xc0) !== 0x80 ||
|
43 | (buf[i] & 0xfe) === 0xc0
|
44 | ) {
|
45 | return false;
|
46 | }
|
47 |
|
48 | i += 2;
|
49 | } else if ((buf[i] & 0xf0) === 0xe0) {
|
50 |
|
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) ||
|
56 | (buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0)
|
57 | ) {
|
58 | return false;
|
59 | }
|
60 |
|
61 | i += 3;
|
62 | } else if ((buf[i] & 0xf8) === 0xf0) {
|
63 |
|
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) ||
|
70 | (buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
|
71 | buf[i] > 0xf4
|
72 | ) {
|
73 | return false;
|
74 | }
|
75 |
|
76 | i += 4;
|
77 | } else {
|
78 | return false;
|
79 | }
|
80 | }
|
81 |
|
82 | return true;
|
83 | }
|
84 |
|
85 | try {
|
86 | let isValidUTF8 = require('utf-8-validate');
|
87 |
|
88 |
|
89 | if (typeof isValidUTF8 === 'object') {
|
90 | isValidUTF8 = isValidUTF8.Validation.isValidUTF8;
|
91 | }
|
92 |
|
93 | module.exports = {
|
94 | isValidStatusCode,
|
95 | isValidUTF8(buf) {
|
96 | return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
97 | }
|
98 | };
|
99 | } catch (e) {
|
100 | module.exports = {
|
101 | isValidStatusCode,
|
102 | isValidUTF8: _isValidUTF8
|
103 | };
|
104 | }
|