next-url.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _detectDomainLocale = require("../../shared/lib/i18n/detect-domain-locale");
  6. var _formatNextPathnameInfo = require("../../shared/lib/router/utils/format-next-pathname-info");
  7. var _getHostname = require("../../shared/lib/get-hostname");
  8. var _getNextPathnameInfo = require("../../shared/lib/router/utils/get-next-pathname-info");
  9. const FLIGHT_PARAMETERS = [
  10. "__flight__",
  11. "__flight_router_state_tree__",
  12. "__flight_prefetch__",
  13. ];
  14. const REGEX_LOCALHOST_HOSTNAME = /(?!^https?:\/\/)(127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|::1|localhost)/;
  15. function parseURL(url, base) {
  16. return new URL(String(url).replace(REGEX_LOCALHOST_HOSTNAME, "localhost"), base && String(base).replace(REGEX_LOCALHOST_HOSTNAME, "localhost"));
  17. }
  18. function parseFlightParameters(searchParams) {
  19. let flightSearchParameters = {};
  20. let flightSearchParametersUpdated = false;
  21. for (const name of FLIGHT_PARAMETERS){
  22. const value = searchParams.get(name);
  23. if (value === null) {
  24. continue;
  25. }
  26. flightSearchParameters[name] = value;
  27. flightSearchParametersUpdated = true;
  28. }
  29. if (!flightSearchParametersUpdated) {
  30. return undefined;
  31. }
  32. return flightSearchParameters;
  33. }
  34. const Internal = Symbol("NextURLInternal");
  35. class NextURL {
  36. constructor(input, baseOrOpts, opts){
  37. let base;
  38. let options;
  39. if (typeof baseOrOpts === "object" && "pathname" in baseOrOpts || typeof baseOrOpts === "string") {
  40. base = baseOrOpts;
  41. options = opts || {};
  42. } else {
  43. options = opts || baseOrOpts || {};
  44. }
  45. this[Internal] = {
  46. url: parseURL(input, base != null ? base : options.base),
  47. options: options,
  48. basePath: ""
  49. };
  50. this.analyzeUrl();
  51. }
  52. analyzeUrl() {
  53. var ref, ref1, ref2, ref3, ref4;
  54. const pathnameInfo = (0, _getNextPathnameInfo).getNextPathnameInfo(this[Internal].url.pathname, {
  55. nextConfig: this[Internal].options.nextConfig,
  56. parseData: !process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
  57. });
  58. this[Internal].domainLocale = (0, _detectDomainLocale).detectDomainLocale((ref = this[Internal].options.nextConfig) == null ? void 0 : (ref1 = ref.i18n) == null ? void 0 : ref1.domains, (0, _getHostname).getHostname(this[Internal].url, this[Internal].options.headers));
  59. const defaultLocale = ((ref2 = this[Internal].domainLocale) == null ? void 0 : ref2.defaultLocale) || ((ref3 = this[Internal].options.nextConfig) == null ? void 0 : (ref4 = ref3.i18n) == null ? void 0 : ref4.defaultLocale);
  60. this[Internal].url.pathname = pathnameInfo.pathname;
  61. this[Internal].defaultLocale = defaultLocale;
  62. var _basePath;
  63. this[Internal].basePath = (_basePath = pathnameInfo.basePath) != null ? _basePath : "";
  64. this[Internal].buildId = pathnameInfo.buildId;
  65. var _locale;
  66. this[Internal].locale = (_locale = pathnameInfo.locale) != null ? _locale : defaultLocale;
  67. this[Internal].trailingSlash = pathnameInfo.trailingSlash;
  68. this[Internal].flightSearchParameters = parseFlightParameters(this[Internal].url.searchParams);
  69. }
  70. formatPathname() {
  71. return (0, _formatNextPathnameInfo).formatNextPathnameInfo({
  72. basePath: this[Internal].basePath,
  73. buildId: this[Internal].buildId,
  74. defaultLocale: !this[Internal].options.forceLocale ? this[Internal].defaultLocale : undefined,
  75. locale: this[Internal].locale,
  76. pathname: this[Internal].url.pathname,
  77. trailingSlash: this[Internal].trailingSlash
  78. });
  79. }
  80. formatSearch() {
  81. const flightSearchParameters = this[Internal].flightSearchParameters;
  82. // If no flight parameters are set, return the search string as is.
  83. // This is a fast path to ensure URLSearchParams only has to be recreated on Flight requests.
  84. if (!flightSearchParameters) {
  85. return this[Internal].url.search;
  86. }
  87. // Create separate URLSearchParams to ensure the original search string is not modified.
  88. const searchParams = new URLSearchParams(this[Internal].url.searchParams);
  89. // If any exist this loop is always limited to the amount of FLIGHT_PARAMETERS.
  90. for(const name in flightSearchParameters){
  91. searchParams.set(name, flightSearchParameters[name]);
  92. }
  93. const params = searchParams.toString();
  94. return params === "" ? "" : `?${params}`;
  95. }
  96. get buildId() {
  97. return this[Internal].buildId;
  98. }
  99. set buildId(buildId) {
  100. this[Internal].buildId = buildId;
  101. }
  102. get flightSearchParameters() {
  103. return this[Internal].flightSearchParameters;
  104. }
  105. set flightSearchParameters(flightSearchParams) {
  106. if (flightSearchParams) {
  107. for (const name of FLIGHT_PARAMETERS){
  108. // Ensure only the provided values are set
  109. if (flightSearchParams[name]) {
  110. this[Internal].url.searchParams.set(name, flightSearchParams[name]);
  111. } else {
  112. // Delete the ones that are not provided as flightData should be overridden.
  113. this[Internal].url.searchParams.delete(name);
  114. }
  115. }
  116. } else {
  117. for (const name of FLIGHT_PARAMETERS){
  118. this[Internal].url.searchParams.delete(name);
  119. }
  120. }
  121. this[Internal].flightSearchParameters = flightSearchParams;
  122. }
  123. get locale() {
  124. var _locale;
  125. return (_locale = this[Internal].locale) != null ? _locale : "";
  126. }
  127. set locale(locale) {
  128. var ref, ref5;
  129. if (!this[Internal].locale || !((ref = this[Internal].options.nextConfig) == null ? void 0 : (ref5 = ref.i18n) == null ? void 0 : ref5.locales.includes(locale))) {
  130. throw new TypeError(`The NextURL configuration includes no locale "${locale}"`);
  131. }
  132. this[Internal].locale = locale;
  133. }
  134. get defaultLocale() {
  135. return this[Internal].defaultLocale;
  136. }
  137. get domainLocale() {
  138. return this[Internal].domainLocale;
  139. }
  140. get searchParams() {
  141. return this[Internal].url.searchParams;
  142. }
  143. get host() {
  144. return this[Internal].url.host;
  145. }
  146. set host(value) {
  147. this[Internal].url.host = value;
  148. }
  149. get hostname() {
  150. return this[Internal].url.hostname;
  151. }
  152. set hostname(value) {
  153. this[Internal].url.hostname = value;
  154. }
  155. get port() {
  156. return this[Internal].url.port;
  157. }
  158. set port(value) {
  159. this[Internal].url.port = value;
  160. }
  161. get protocol() {
  162. return this[Internal].url.protocol;
  163. }
  164. set protocol(value) {
  165. this[Internal].url.protocol = value;
  166. }
  167. get href() {
  168. const pathname = this.formatPathname();
  169. const search = this.formatSearch();
  170. return `${this.protocol}//${this.host}${pathname}${search}`;
  171. }
  172. set href(url) {
  173. this[Internal].url = parseURL(url);
  174. this.analyzeUrl();
  175. }
  176. get origin() {
  177. return this[Internal].url.origin;
  178. }
  179. get pathname() {
  180. return this[Internal].url.pathname;
  181. }
  182. set pathname(value) {
  183. this[Internal].url.pathname = value;
  184. }
  185. get hash() {
  186. return this[Internal].url.hash;
  187. }
  188. set hash(value) {
  189. this[Internal].url.hash = value;
  190. }
  191. get search() {
  192. return this[Internal].url.search;
  193. }
  194. set search(value) {
  195. this[Internal].url.search = value;
  196. }
  197. get password() {
  198. return this[Internal].url.password;
  199. }
  200. set password(value) {
  201. this[Internal].url.password = value;
  202. }
  203. get username() {
  204. return this[Internal].url.username;
  205. }
  206. set username(value) {
  207. this[Internal].url.username = value;
  208. }
  209. get basePath() {
  210. return this[Internal].basePath;
  211. }
  212. set basePath(value) {
  213. this[Internal].basePath = value.startsWith("/") ? value : `/${value}`;
  214. }
  215. toString() {
  216. return this.href;
  217. }
  218. toJSON() {
  219. return this.href;
  220. }
  221. [Symbol.for("edge-runtime.inspect.custom")]() {
  222. return {
  223. href: this.href,
  224. origin: this.origin,
  225. protocol: this.protocol,
  226. username: this.username,
  227. password: this.password,
  228. host: this.host,
  229. hostname: this.hostname,
  230. port: this.port,
  231. pathname: this.pathname,
  232. search: this.search,
  233. searchParams: this.searchParams,
  234. hash: this.hash
  235. };
  236. }
  237. clone() {
  238. return new NextURL(String(this), this[Internal].options);
  239. }
  240. }
  241. exports.NextURL = NextURL;
  242. //# sourceMappingURL=next-url.js.map