server-route-utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getCustomRoute = getCustomRoute;
  6. exports.createRedirectRoute = exports.stringifyQuery = exports.createHeaderRoute = void 0;
  7. var _redirectStatus = require("../lib/redirect-status");
  8. var _pathMatch = require("../shared/lib/router/utils/path-match");
  9. var _prepareDestination = require("../shared/lib/router/utils/prepare-destination");
  10. var _requestMeta = require("./request-meta");
  11. var _querystring = require("querystring");
  12. var _url = require("url");
  13. var _utils = require("../shared/lib/utils");
  14. function getCustomRoute(params) {
  15. const { rule , type , restrictedRedirectPaths } = params;
  16. const match = (0, _pathMatch).getPathMatch(rule.source, {
  17. strict: true,
  18. removeUnnamedParams: true,
  19. regexModifier: !rule.internal ? (regex)=>(0, _redirectStatus).modifyRouteRegex(regex, type === "redirect" ? restrictedRedirectPaths : undefined) : undefined
  20. });
  21. return {
  22. ...rule,
  23. type,
  24. match,
  25. name: type,
  26. fn: async (_req, _res, _params, _parsedUrl)=>({
  27. finished: false
  28. })
  29. };
  30. }
  31. const createHeaderRoute = ({ rule , restrictedRedirectPaths })=>{
  32. const headerRoute = getCustomRoute({
  33. type: "header",
  34. rule,
  35. restrictedRedirectPaths
  36. });
  37. return {
  38. match: headerRoute.match,
  39. matchesBasePath: true,
  40. matchesLocale: true,
  41. matchesLocaleAPIRoutes: true,
  42. matchesTrailingSlash: true,
  43. has: headerRoute.has,
  44. type: headerRoute.type,
  45. name: `${headerRoute.type} ${headerRoute.source} header route`,
  46. fn: async (_req, res, params, _parsedUrl)=>{
  47. const hasParams = Object.keys(params).length > 0;
  48. for (const header of headerRoute.headers){
  49. let { key , value } = header;
  50. if (hasParams) {
  51. key = (0, _prepareDestination).compileNonPath(key, params);
  52. value = (0, _prepareDestination).compileNonPath(value, params);
  53. }
  54. res.setHeader(key, value);
  55. }
  56. return {
  57. finished: false
  58. };
  59. }
  60. };
  61. };
  62. exports.createHeaderRoute = createHeaderRoute;
  63. const stringifyQuery = (req, query)=>{
  64. const initialQuery = (0, _requestMeta).getRequestMeta(req, "__NEXT_INIT_QUERY") || {};
  65. const initialQueryValues = Object.values(initialQuery);
  66. return (0, _querystring).stringify(query, undefined, undefined, {
  67. encodeURIComponent (value) {
  68. if (value in initialQuery || initialQueryValues.some((initialQueryVal)=>{
  69. // `value` always refers to a query value, even if it's nested in an array
  70. return Array.isArray(initialQueryVal) ? initialQueryVal.includes(value) : initialQueryVal === value;
  71. })) {
  72. // Encode keys and values from initial query
  73. return encodeURIComponent(value);
  74. }
  75. return value;
  76. }
  77. });
  78. };
  79. exports.stringifyQuery = stringifyQuery;
  80. const createRedirectRoute = ({ rule , restrictedRedirectPaths })=>{
  81. const redirectRoute = getCustomRoute({
  82. type: "redirect",
  83. rule,
  84. restrictedRedirectPaths
  85. });
  86. return {
  87. internal: redirectRoute.internal,
  88. type: redirectRoute.type,
  89. match: redirectRoute.match,
  90. matchesBasePath: true,
  91. matchesLocale: redirectRoute.internal ? undefined : true,
  92. matchesLocaleAPIRoutes: true,
  93. matchesTrailingSlash: true,
  94. has: redirectRoute.has,
  95. statusCode: redirectRoute.statusCode,
  96. name: `Redirect route ${redirectRoute.source}`,
  97. fn: async (req, res, params, parsedUrl)=>{
  98. const { parsedDestination } = (0, _prepareDestination).prepareDestination({
  99. appendParamsToQuery: false,
  100. destination: redirectRoute.destination,
  101. params: params,
  102. query: parsedUrl.query
  103. });
  104. const { query } = parsedDestination;
  105. delete parsedDestination.query;
  106. parsedDestination.search = stringifyQuery(req, query);
  107. let updatedDestination = (0, _url).format(parsedDestination);
  108. if (updatedDestination.startsWith("/")) {
  109. updatedDestination = (0, _utils).normalizeRepeatedSlashes(updatedDestination);
  110. }
  111. res.redirect(updatedDestination, (0, _redirectStatus).getRedirectStatus(redirectRoute)).body(updatedDestination).send();
  112. return {
  113. finished: true
  114. };
  115. }
  116. };
  117. };
  118. exports.createRedirectRoute = createRedirectRoute;
  119. //# sourceMappingURL=server-route-utils.js.map