JavaScript 65.5%
Python 17.8%
CSS 13%
HTML 3.7%
1'use strict';23const { isUtf8 } = require('buffer');45const { hasBlob } = require('./constants');67//8// Allowed token characters:9//10// '!', '#', '$', '%', '&', ''', '*', '+', '-',11// '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'12//13// tokenChars[32] === 0 // ' '14// tokenChars[33] === 1 // '!'15// tokenChars[34] === 0 // '"'16// ...17//18// prettier-ignore19const tokenChars = [20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 1521 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 3122 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 4723 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 6324 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 7925 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 9526 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 11127 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 12728];2930/**31 * Checks if a status code is allowed in a close frame.32 *33 * @param {Number} code The status code34 * @return {Boolean} `true` if the status code is valid, else `false`35 * @public36 */37function isValidStatusCode(code) {38 return (39 (code >= 1000 &&40 code <= 1014 &&41 code !== 1004 &&42 code !== 1005 &&43 code !== 1006) ||44 (code >= 3000 && code <= 4999)45 );46}4748/**49 * Checks if a given buffer contains only correct UTF-8.50 * Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by51 * Markus Kuhn.52 *53 * @param {Buffer} buf The buffer to check54 * @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`55 * @public56 */57function _isValidUTF8(buf) {58 const len = buf.length;59 let i = 0;6061 while (i < len) {62 if ((buf[i] & 0x80) === 0) {63 // 0xxxxxxx64 i++;65 } else if ((buf[i] & 0xe0) === 0xc0) {66 // 110xxxxx 10xxxxxx67 if (68 i + 1 === len ||69 (buf[i + 1] & 0xc0) !== 0x80 ||70 (buf[i] & 0xfe) === 0xc0 // Overlong71 ) {72 return false;73 }7475 i += 2;76 } else if ((buf[i] & 0xf0) === 0xe0) {77 // 1110xxxx 10xxxxxx 10xxxxxx78 if (79 i + 2 >= len ||80 (buf[i + 1] & 0xc0) !== 0x80 ||81 (buf[i + 2] & 0xc0) !== 0x80 ||82 (buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong83 (buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)84 ) {85 return false;86 }8788 i += 3;89 } else if ((buf[i] & 0xf8) === 0xf0) {90 // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx91 if (92 i + 3 >= len ||93 (buf[i + 1] & 0xc0) !== 0x80 ||94 (buf[i + 2] & 0xc0) !== 0x80 ||95 (buf[i + 3] & 0xc0) !== 0x80 ||96 (buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong97 (buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||98 buf[i] > 0xf4 // > U+10FFFF99 ) {100 return false;101 }102103 i += 4;104 } else {105 return false;106 }107 }108109 return true;110}111112/**113 * Determines whether a value is a `Blob`.114 *115 * @param {*} value The value to be tested116 * @return {Boolean} `true` if `value` is a `Blob`, else `false`117 * @private118 */119function isBlob(value) {120 return (121 hasBlob &&122 typeof value === 'object' &&123 typeof value.arrayBuffer === 'function' &&124 typeof value.type === 'string' &&125 typeof value.stream === 'function' &&126 (value[Symbol.toStringTag] === 'Blob' ||127 value[Symbol.toStringTag] === 'File')128 );129}130131module.exports = {132 isBlob,133 isValidStatusCode,134 isValidUTF8: _isValidUTF8,135 tokenChars136};137138if (isUtf8) {139 module.exports.isValidUTF8 = function (buf) {140 return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);141 };142} /* istanbul ignore else */ else if (!process.env.WS_NO_UTF_8_VALIDATE) {143 try {144 const isValidUTF8 = require('utf-8-validate');145146 module.exports.isValidUTF8 = function (buf) {147 return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);148 };149 } catch (e) {150 // Continue regardless of the error.151 }152}153