utils.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.fromNodeHeaders = fromNodeHeaders;
  6. exports.splitCookiesString = splitCookiesString;
  7. exports.toNodeHeaders = toNodeHeaders;
  8. exports.validateURL = validateURL;
  9. function fromNodeHeaders(object) {
  10. const headers = new Headers();
  11. for (let [key, value] of Object.entries(object)){
  12. const values = Array.isArray(value) ? value : [
  13. value
  14. ];
  15. for (let v of values){
  16. if (v !== undefined) {
  17. headers.append(key, v);
  18. }
  19. }
  20. }
  21. return headers;
  22. }
  23. function splitCookiesString(cookiesString) {
  24. var cookiesStrings = [];
  25. var pos = 0;
  26. var start;
  27. var ch;
  28. var lastComma;
  29. var nextStart;
  30. var cookiesSeparatorFound;
  31. function skipWhitespace() {
  32. while(pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))){
  33. pos += 1;
  34. }
  35. return pos < cookiesString.length;
  36. }
  37. function notSpecialChar() {
  38. ch = cookiesString.charAt(pos);
  39. return ch !== "=" && ch !== ";" && ch !== ",";
  40. }
  41. while(pos < cookiesString.length){
  42. start = pos;
  43. cookiesSeparatorFound = false;
  44. while(skipWhitespace()){
  45. ch = cookiesString.charAt(pos);
  46. if (ch === ",") {
  47. // ',' is a cookie separator if we have later first '=', not ';' or ','
  48. lastComma = pos;
  49. pos += 1;
  50. skipWhitespace();
  51. nextStart = pos;
  52. while(pos < cookiesString.length && notSpecialChar()){
  53. pos += 1;
  54. }
  55. // currently special character
  56. if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
  57. // we found cookies separator
  58. cookiesSeparatorFound = true;
  59. // pos is inside the next cookie, so back up and return it.
  60. pos = nextStart;
  61. cookiesStrings.push(cookiesString.substring(start, lastComma));
  62. start = pos;
  63. } else {
  64. // in param ',' or param separator ';',
  65. // we continue from that comma
  66. pos = lastComma + 1;
  67. }
  68. } else {
  69. pos += 1;
  70. }
  71. }
  72. if (!cookiesSeparatorFound || pos >= cookiesString.length) {
  73. cookiesStrings.push(cookiesString.substring(start, cookiesString.length));
  74. }
  75. }
  76. return cookiesStrings;
  77. }
  78. function toNodeHeaders(headers) {
  79. const result = {};
  80. if (headers) {
  81. for (const [key, value] of headers.entries()){
  82. result[key] = value;
  83. if (key.toLowerCase() === "set-cookie") {
  84. result[key] = splitCookiesString(value);
  85. }
  86. }
  87. }
  88. return result;
  89. }
  90. function validateURL(url) {
  91. try {
  92. return String(new URL(String(url)));
  93. } catch (error) {
  94. throw new Error(`URLs is malformed. Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls`, {
  95. cause: error
  96. });
  97. }
  98. }
  99. //# sourceMappingURL=utils.js.map