index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. const memory_code_points_1 = require("./memory-code-points");
  3. const mapping2space = memory_code_points_1.non_ASCII_space_characters;
  4. const mapping2nothing = memory_code_points_1.commonly_mapped_to_nothing;
  5. const getCodePoint = (character) => character.codePointAt(0);
  6. const first = (x) => x[0];
  7. const last = (x) => x[x.length - 1];
  8. function toCodePoints(input) {
  9. const codepoints = [];
  10. const size = input.length;
  11. for (let i = 0; i < size; i += 1) {
  12. const before = input.charCodeAt(i);
  13. if (before >= 0xd800 && before <= 0xdbff && size > i + 1) {
  14. const next = input.charCodeAt(i + 1);
  15. if (next >= 0xdc00 && next <= 0xdfff) {
  16. codepoints.push((before - 0xd800) * 0x400 + next - 0xdc00 + 0x10000);
  17. i += 1;
  18. continue;
  19. }
  20. }
  21. codepoints.push(before);
  22. }
  23. return codepoints;
  24. }
  25. function saslprep(input, opts = {}) {
  26. if (typeof input !== 'string') {
  27. throw new TypeError('Expected string.');
  28. }
  29. if (input.length === 0) {
  30. return '';
  31. }
  32. const mapped_input = toCodePoints(input)
  33. .map((character) => (mapping2space.get(character) ? 0x20 : character))
  34. .filter((character) => !mapping2nothing.get(character));
  35. const normalized_input = String.fromCodePoint
  36. .apply(null, mapped_input)
  37. .normalize('NFKC');
  38. const normalized_map = toCodePoints(normalized_input);
  39. const hasProhibited = normalized_map.some((character) => memory_code_points_1.prohibited_characters.get(character));
  40. if (hasProhibited) {
  41. throw new Error('Prohibited character, see https://tools.ietf.org/html/rfc4013#section-2.3');
  42. }
  43. if (opts.allowUnassigned !== true) {
  44. const hasUnassigned = normalized_map.some((character) => memory_code_points_1.unassigned_code_points.get(character));
  45. if (hasUnassigned) {
  46. throw new Error('Unassigned code point, see https://tools.ietf.org/html/rfc4013#section-2.5');
  47. }
  48. }
  49. const hasBidiRAL = normalized_map.some((character) => memory_code_points_1.bidirectional_r_al.get(character));
  50. const hasBidiL = normalized_map.some((character) => memory_code_points_1.bidirectional_l.get(character));
  51. if (hasBidiRAL && hasBidiL) {
  52. throw new Error('String must not contain RandALCat and LCat at the same time,' +
  53. ' see https://tools.ietf.org/html/rfc3454#section-6');
  54. }
  55. const isFirstBidiRAL = memory_code_points_1.bidirectional_r_al.get(getCodePoint(first(normalized_input)));
  56. const isLastBidiRAL = memory_code_points_1.bidirectional_r_al.get(getCodePoint(last(normalized_input)));
  57. if (hasBidiRAL && !(isFirstBidiRAL && isLastBidiRAL)) {
  58. throw new Error('Bidirectional RandALCat character must be the first and the last' +
  59. ' character of the string, see https://tools.ietf.org/html/rfc3454#section-6');
  60. }
  61. return normalized_input;
  62. }
  63. saslprep.saslprep = saslprep;
  64. saslprep.default = saslprep;
  65. module.exports = saslprep;
  66. //# sourceMappingURL=index.js.map