source: trip-planner-front/node_modules/ws/lib/validation.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.4 KB
Line 
1'use strict';
2
3/**
4 * Checks if a status code is allowed in a close frame.
5 *
6 * @param {Number} code The status code
7 * @return {Boolean} `true` if the status code is valid, else `false`
8 * @public
9 */
10function 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 * 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 */
30function _isValidUTF8(buf) {
31 const len = buf.length;
32 let i = 0;
33
34 while (i < len) {
35 if ((buf[i] & 0x80) === 0) {
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 }
47
48 i += 2;
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 }
60
61 i += 3;
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 }
75
76 i += 4;
77 } else {
78 return false;
79 }
80 }
81
82 return true;
83}
84
85try {
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}
Note: See TracBrowser for help on using the repository browser.