index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getCookieParser = getCookieParser;
  6. exports.sendStatusCode = sendStatusCode;
  7. exports.redirect = redirect;
  8. exports.checkIsManualRevalidate = checkIsManualRevalidate;
  9. exports.clearPreviewData = clearPreviewData;
  10. exports.sendError = sendError;
  11. exports.setLazyProp = setLazyProp;
  12. exports.SYMBOL_CLEARED_COOKIES = exports.SYMBOL_PREVIEW_DATA = exports.RESPONSE_LIMIT_DEFAULT = exports.COOKIE_NAME_PRERENDER_DATA = exports.COOKIE_NAME_PRERENDER_BYPASS = exports.PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER = exports.PRERENDER_REVALIDATE_HEADER = void 0;
  13. function getCookieParser(headers) {
  14. return function parseCookie() {
  15. const header = headers.cookie;
  16. if (!header) {
  17. return {};
  18. }
  19. const { parse: parseCookieFn } = require("next/dist/compiled/cookie");
  20. return parseCookieFn(Array.isArray(header) ? header.join(";") : header);
  21. };
  22. }
  23. function sendStatusCode(res, statusCode) {
  24. res.statusCode = statusCode;
  25. return res;
  26. }
  27. function redirect(res, statusOrUrl, url) {
  28. if (typeof statusOrUrl === "string") {
  29. url = statusOrUrl;
  30. statusOrUrl = 307;
  31. }
  32. if (typeof statusOrUrl !== "number" || typeof url !== "string") {
  33. throw new Error(`Invalid redirect arguments. Please use a single argument URL, e.g. res.redirect('/destination') or use a status code and URL, e.g. res.redirect(307, '/destination').`);
  34. }
  35. res.writeHead(statusOrUrl, {
  36. Location: url
  37. });
  38. res.write(url);
  39. res.end();
  40. return res;
  41. }
  42. const PRERENDER_REVALIDATE_HEADER = "x-prerender-revalidate";
  43. exports.PRERENDER_REVALIDATE_HEADER = PRERENDER_REVALIDATE_HEADER;
  44. const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER = "x-prerender-revalidate-if-generated";
  45. exports.PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER = PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER;
  46. function checkIsManualRevalidate(req, previewProps) {
  47. return {
  48. isManualRevalidate: req.headers[PRERENDER_REVALIDATE_HEADER] === previewProps.previewModeId,
  49. revalidateOnlyGenerated: !!req.headers[PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER]
  50. };
  51. }
  52. const COOKIE_NAME_PRERENDER_BYPASS = `__prerender_bypass`;
  53. exports.COOKIE_NAME_PRERENDER_BYPASS = COOKIE_NAME_PRERENDER_BYPASS;
  54. const COOKIE_NAME_PRERENDER_DATA = `__next_preview_data`;
  55. exports.COOKIE_NAME_PRERENDER_DATA = COOKIE_NAME_PRERENDER_DATA;
  56. const RESPONSE_LIMIT_DEFAULT = 4 * 1024 * 1024;
  57. exports.RESPONSE_LIMIT_DEFAULT = RESPONSE_LIMIT_DEFAULT;
  58. const SYMBOL_PREVIEW_DATA = Symbol(COOKIE_NAME_PRERENDER_DATA);
  59. exports.SYMBOL_PREVIEW_DATA = SYMBOL_PREVIEW_DATA;
  60. const SYMBOL_CLEARED_COOKIES = Symbol(COOKIE_NAME_PRERENDER_BYPASS);
  61. exports.SYMBOL_CLEARED_COOKIES = SYMBOL_CLEARED_COOKIES;
  62. function clearPreviewData(res, options = {}) {
  63. if (SYMBOL_CLEARED_COOKIES in res) {
  64. return res;
  65. }
  66. const { serialize } = require("next/dist/compiled/cookie");
  67. const previous = res.getHeader("Set-Cookie");
  68. res.setHeader(`Set-Cookie`, [
  69. ...typeof previous === "string" ? [
  70. previous
  71. ] : Array.isArray(previous) ? previous : [],
  72. serialize(COOKIE_NAME_PRERENDER_BYPASS, "", {
  73. // To delete a cookie, set `expires` to a date in the past:
  74. // https://tools.ietf.org/html/rfc6265#section-4.1.1
  75. // `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
  76. expires: new Date(0),
  77. httpOnly: true,
  78. sameSite: process.env.NODE_ENV !== "development" ? "none" : "lax",
  79. secure: process.env.NODE_ENV !== "development",
  80. path: "/",
  81. ...options.path !== undefined ? {
  82. path: options.path
  83. } : undefined
  84. }),
  85. serialize(COOKIE_NAME_PRERENDER_DATA, "", {
  86. // To delete a cookie, set `expires` to a date in the past:
  87. // https://tools.ietf.org/html/rfc6265#section-4.1.1
  88. // `Max-Age: 0` is not valid, thus ignored, and the cookie is persisted.
  89. expires: new Date(0),
  90. httpOnly: true,
  91. sameSite: process.env.NODE_ENV !== "development" ? "none" : "lax",
  92. secure: process.env.NODE_ENV !== "development",
  93. path: "/",
  94. ...options.path !== undefined ? {
  95. path: options.path
  96. } : undefined
  97. }),
  98. ]);
  99. Object.defineProperty(res, SYMBOL_CLEARED_COOKIES, {
  100. value: true,
  101. enumerable: false
  102. });
  103. return res;
  104. }
  105. class ApiError extends Error {
  106. constructor(statusCode, message){
  107. super(message);
  108. this.statusCode = statusCode;
  109. }
  110. }
  111. exports.ApiError = ApiError;
  112. function sendError(res, statusCode, message) {
  113. res.statusCode = statusCode;
  114. res.statusMessage = message;
  115. res.end(message);
  116. }
  117. function setLazyProp({ req }, prop, getter) {
  118. const opts = {
  119. configurable: true,
  120. enumerable: true
  121. };
  122. const optsReset = {
  123. ...opts,
  124. writable: true
  125. };
  126. Object.defineProperty(req, prop, {
  127. ...opts,
  128. get: ()=>{
  129. const value = getter();
  130. // we set the property on the object to avoid recalculating it
  131. Object.defineProperty(req, prop, {
  132. ...optsReset,
  133. value
  134. });
  135. return value;
  136. },
  137. set: (value)=>{
  138. Object.defineProperty(req, prop, {
  139. ...optsReset,
  140. value
  141. });
  142. }
  143. });
  144. }
  145. //# sourceMappingURL=index.js.map