urn.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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