index.js 2.9 KB

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