You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
2.0 KiB

4 years ago
  1. import { SCHEMES } from "../uri";
  2. const NID$ = "(?:[0-9A-Za-z][0-9A-Za-z\\-]{1,31})";
  3. const PCT_ENCODED$ = "(?:\\%[0-9A-Fa-f]{2})";
  4. const TRANS$$ = "[0-9A-Za-z\\(\\)\\+\\,\\-\\.\\:\\=\\@\\;\\$\\_\\!\\*\\'\\/\\?\\#]";
  5. const NSS$ = "(?:(?:" + PCT_ENCODED$ + "|" + TRANS$$ + ")+)";
  6. const URN_SCHEME = new RegExp("^urn\\:(" + NID$ + ")$");
  7. const URN_PATH = new RegExp("^(" + NID$ + ")\\:(" + NSS$ + ")$");
  8. const URN_PARSE = /^([^\:]+)\:(.*)/;
  9. const URN_EXCLUDED = /[\x00-\x20\\\"\&\<\>\[\]\^\`\{\|\}\~\x7F-\xFF]/g;
  10. //RFC 2141
  11. const handler = {
  12. scheme: "urn",
  13. parse: function (components, options) {
  14. const matches = components.path && components.path.match(URN_PARSE);
  15. let urnComponents = components;
  16. if (matches) {
  17. const scheme = options.scheme || urnComponents.scheme || "urn";
  18. const nid = matches[1].toLowerCase();
  19. const nss = matches[2];
  20. const urnScheme = `${scheme}:${options.nid || nid}`;
  21. const schemeHandler = SCHEMES[urnScheme];
  22. urnComponents.nid = nid;
  23. urnComponents.nss = nss;
  24. urnComponents.path = undefined;
  25. if (schemeHandler) {
  26. urnComponents = schemeHandler.parse(urnComponents, options);
  27. }
  28. }
  29. else {
  30. urnComponents.error = urnComponents.error || "URN can not be parsed.";
  31. }
  32. return urnComponents;
  33. },
  34. serialize: function (urnComponents, options) {
  35. const scheme = options.scheme || urnComponents.scheme || "urn";
  36. const nid = urnComponents.nid;
  37. const urnScheme = `${scheme}:${options.nid || nid}`;
  38. const schemeHandler = SCHEMES[urnScheme];
  39. if (schemeHandler) {
  40. urnComponents = schemeHandler.serialize(urnComponents, options);
  41. }
  42. const uriComponents = urnComponents;
  43. const nss = urnComponents.nss;
  44. uriComponents.path = `${nid || options.nid}:${nss}`;
  45. return uriComponents;
  46. },
  47. };
  48. export default handler;
  49. //# sourceMappingURL=urn.js.map