Front end of the Slack clone application.

Extensions.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. 'use strict';
  2. //
  3. // Allowed token characters:
  4. //
  5. // '!', '#', '$', '%', '&', ''', '*', '+', '-',
  6. // '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
  7. //
  8. // tokenChars[32] === 0 // ' '
  9. // tokenChars[33] === 1 // '!'
  10. // tokenChars[34] === 0 // '"'
  11. // ...
  12. //
  13. const tokenChars = [
  14. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
  15. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
  16. 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
  17. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
  18. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
  19. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
  20. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
  21. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
  22. ];
  23. /**
  24. * Adds an offer to the map of extension offers or a parameter to the map of
  25. * parameters.
  26. *
  27. * @param {Object} dest The map of extension offers or parameters
  28. * @param {String} name The extension or parameter name
  29. * @param {(Object|Boolean|String)} elem The extension parameters or the
  30. * parameter value
  31. * @private
  32. */
  33. function push (dest, name, elem) {
  34. if (Object.prototype.hasOwnProperty.call(dest, name)) dest[name].push(elem);
  35. else dest[name] = [elem];
  36. }
  37. /**
  38. * Parses the `Sec-WebSocket-Extensions` header into an object.
  39. *
  40. * @param {String} header The field value of the header
  41. * @return {Object} The parsed object
  42. * @public
  43. */
  44. function parse (header) {
  45. const offers = {};
  46. if (header === undefined || header === '') return offers;
  47. var params = {};
  48. var mustUnescape = false;
  49. var isEscaping = false;
  50. var inQuotes = false;
  51. var extensionName;
  52. var paramName;
  53. var start = -1;
  54. var end = -1;
  55. for (var i = 0; i < header.length; i++) {
  56. const code = header.charCodeAt(i);
  57. if (extensionName === undefined) {
  58. if (end === -1 && tokenChars[code] === 1) {
  59. if (start === -1) start = i;
  60. } else if (code === 0x20/* ' ' */|| code === 0x09/* '\t' */) {
  61. if (end === -1 && start !== -1) end = i;
  62. } else if (code === 0x3b/* ';' */ || code === 0x2c/* ',' */) {
  63. if (start === -1) throw new Error(`unexpected character at index ${i}`);
  64. if (end === -1) end = i;
  65. const name = header.slice(start, end);
  66. if (code === 0x2c) {
  67. push(offers, name, params);
  68. params = {};
  69. } else {
  70. extensionName = name;
  71. }
  72. start = end = -1;
  73. } else {
  74. throw new Error(`unexpected character at index ${i}`);
  75. }
  76. } else if (paramName === undefined) {
  77. if (end === -1 && tokenChars[code] === 1) {
  78. if (start === -1) start = i;
  79. } else if (code === 0x20 || code === 0x09) {
  80. if (end === -1 && start !== -1) end = i;
  81. } else if (code === 0x3b || code === 0x2c) {
  82. if (start === -1) throw new Error(`unexpected character at index ${i}`);
  83. if (end === -1) end = i;
  84. push(params, header.slice(start, end), true);
  85. if (code === 0x2c) {
  86. push(offers, extensionName, params);
  87. params = {};
  88. extensionName = undefined;
  89. }
  90. start = end = -1;
  91. } else if (code === 0x3d/* '=' */&& start !== -1 && end === -1) {
  92. paramName = header.slice(start, i);
  93. start = end = -1;
  94. } else {
  95. throw new Error(`unexpected character at index ${i}`);
  96. }
  97. } else {
  98. //
  99. // The value of a quoted-string after unescaping must conform to the
  100. // token ABNF, so only token characters are valid.
  101. // Ref: https://tools.ietf.org/html/rfc6455#section-9.1
  102. //
  103. if (isEscaping) {
  104. if (tokenChars[code] !== 1) {
  105. throw new Error(`unexpected character at index ${i}`);
  106. }
  107. if (start === -1) start = i;
  108. else if (!mustUnescape) mustUnescape = true;
  109. isEscaping = false;
  110. } else if (inQuotes) {
  111. if (tokenChars[code] === 1) {
  112. if (start === -1) start = i;
  113. } else if (code === 0x22/* '"' */ && start !== -1) {
  114. inQuotes = false;
  115. end = i;
  116. } else if (code === 0x5c/* '\' */) {
  117. isEscaping = true;
  118. } else {
  119. throw new Error(`unexpected character at index ${i}`);
  120. }
  121. } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
  122. inQuotes = true;
  123. } else if (end === -1 && tokenChars[code] === 1) {
  124. if (start === -1) start = i;
  125. } else if (start !== -1 && (code === 0x20 || code === 0x09)) {
  126. if (end === -1) end = i;
  127. } else if (code === 0x3b || code === 0x2c) {
  128. if (start === -1) throw new Error(`unexpected character at index ${i}`);
  129. if (end === -1) end = i;
  130. var value = header.slice(start, end);
  131. if (mustUnescape) {
  132. value = value.replace(/\\/g, '');
  133. mustUnescape = false;
  134. }
  135. push(params, paramName, value);
  136. if (code === 0x2c) {
  137. push(offers, extensionName, params);
  138. params = {};
  139. extensionName = undefined;
  140. }
  141. paramName = undefined;
  142. start = end = -1;
  143. } else {
  144. throw new Error(`unexpected character at index ${i}`);
  145. }
  146. }
  147. }
  148. if (start === -1 || inQuotes) throw new Error('unexpected end of input');
  149. if (end === -1) end = i;
  150. const token = header.slice(start, end);
  151. if (extensionName === undefined) {
  152. push(offers, token, {});
  153. } else {
  154. if (paramName === undefined) {
  155. push(params, token, true);
  156. } else if (mustUnescape) {
  157. push(params, paramName, token.replace(/\\/g, ''));
  158. } else {
  159. push(params, paramName, token);
  160. }
  161. push(offers, extensionName, params);
  162. }
  163. return offers;
  164. }
  165. /**
  166. * Serializes a parsed `Sec-WebSocket-Extensions` header to a string.
  167. *
  168. * @param {Object} value The object to format
  169. * @return {String} A string representing the given value
  170. * @public
  171. */
  172. function format (value) {
  173. return Object.keys(value).map((token) => {
  174. var paramsList = value[token];
  175. if (!Array.isArray(paramsList)) paramsList = [paramsList];
  176. return paramsList.map((params) => {
  177. return [token].concat(Object.keys(params).map((k) => {
  178. var p = params[k];
  179. if (!Array.isArray(p)) p = [p];
  180. return p.map((v) => v === true ? k : `${k}=${v}`).join('; ');
  181. })).join('; ');
  182. }).join(', ');
  183. }).join(', ');
  184. }
  185. module.exports = { format, parse };