cookies.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _cookie = _interopRequireDefault(require("next/dist/compiled/cookie"));
  6. function _interopRequireDefault(obj) {
  7. return obj && obj.__esModule ? obj : {
  8. default: obj
  9. };
  10. }
  11. const normalizeCookieOptions = (options)=>{
  12. options = Object.assign({}, options);
  13. if (options.maxAge) {
  14. options.expires = new Date(Date.now() + options.maxAge * 1000);
  15. }
  16. if (options.path == null) {
  17. options.path = "/";
  18. }
  19. return options;
  20. };
  21. const serializeValue = (value)=>typeof value === "object" ? `j:${JSON.stringify(value)}` : String(value);
  22. const serializeExpiredCookie = (key, options = {})=>_cookie.default.serialize(key, "", {
  23. expires: new Date(0),
  24. path: "/",
  25. ...options
  26. });
  27. const deserializeCookie = (input)=>{
  28. const value = input.headers.get("set-cookie");
  29. return value !== undefined && value !== null ? value.split(", ") : [];
  30. };
  31. const serializeCookie = (input)=>input.join(", ");
  32. class Cookies extends Map {
  33. constructor(input){
  34. const parsedInput = typeof input === "string" ? _cookie.default.parse(input) : {};
  35. super(Object.entries(parsedInput));
  36. }
  37. set(key, value, options = {}) {
  38. return super.set(key, _cookie.default.serialize(key, serializeValue(value), normalizeCookieOptions(options)));
  39. }
  40. [Symbol.for("edge-runtime.inspect.custom")]() {
  41. return Object.fromEntries(this.entries());
  42. }
  43. }
  44. exports.Cookies = Cookies;
  45. class NextCookies extends Cookies {
  46. constructor(response){
  47. super(response.headers.get("cookie"));
  48. this.response = response;
  49. }
  50. get = (...args)=>{
  51. return this.getWithOptions(...args).value;
  52. };
  53. getWithOptions = (...args)=>{
  54. const raw = super.get(...args);
  55. if (typeof raw !== "string") return {
  56. value: raw,
  57. options: {}
  58. };
  59. const { [args[0]]: value , ...options } = _cookie.default.parse(raw);
  60. return {
  61. value,
  62. options
  63. };
  64. };
  65. set = (...args)=>{
  66. const isAlreadyAdded = super.has(args[0]);
  67. super.set(...args);
  68. const currentCookie = super.get(args[0]);
  69. if (typeof currentCookie !== "string") {
  70. throw new Error(`Invariant: failed to generate cookie for ${JSON.stringify(args)}`);
  71. }
  72. if (isAlreadyAdded) {
  73. const setCookie = serializeCookie(deserializeCookie(this.response).filter((value)=>!value.startsWith(`${args[0]}=`)));
  74. if (setCookie) {
  75. this.response.headers.set("set-cookie", [
  76. currentCookie,
  77. setCookie
  78. ].join(", "));
  79. } else {
  80. this.response.headers.set("set-cookie", currentCookie);
  81. }
  82. } else {
  83. this.response.headers.append("set-cookie", currentCookie);
  84. }
  85. return this;
  86. };
  87. delete = (key, options = {})=>{
  88. const isDeleted = super.delete(key);
  89. if (isDeleted) {
  90. const setCookie = serializeCookie(deserializeCookie(this.response).filter((value)=>!value.startsWith(`${key}=`)));
  91. const expiredCookie = serializeExpiredCookie(key, options);
  92. this.response.headers.set("set-cookie", [
  93. expiredCookie,
  94. setCookie
  95. ].join(", "));
  96. }
  97. return isDeleted;
  98. };
  99. clear = (options = {})=>{
  100. const expiredCookies = Array.from(super.keys()).map((key)=>serializeExpiredCookie(key, options)).join(", ");
  101. if (expiredCookies) this.response.headers.set("set-cookie", expiredCookies);
  102. return super.clear();
  103. };
  104. }
  105. exports.NextCookies = NextCookies;
  106. //# sourceMappingURL=cookies.js.map