exports.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { n as noop } from "./index.js";
  2. import { s as safe_not_equal } from "./root.js";
  3. const SCHEME = /^[a-z][a-z\d+\-.]+:/i;
  4. const internal = new URL("sveltekit-internal://");
  5. function resolve(base, path) {
  6. if (path[0] === "/" && path[1] === "/") return path;
  7. let url = new URL(base, internal);
  8. url = new URL(path, url);
  9. return url.protocol === internal.protocol ? url.pathname + url.search + url.hash : url.href;
  10. }
  11. function normalize_path(path, trailing_slash) {
  12. if (path === "/" || trailing_slash === "ignore") return path;
  13. if (trailing_slash === "never") {
  14. return path.endsWith("/") ? path.slice(0, -1) : path;
  15. } else if (trailing_slash === "always" && !path.endsWith("/")) {
  16. return path + "/";
  17. }
  18. return path;
  19. }
  20. function decode_pathname(pathname) {
  21. return pathname.split("%25").map(decodeURI).join("%25");
  22. }
  23. function decode_params(params) {
  24. for (const key in params) {
  25. params[key] = decodeURIComponent(params[key]);
  26. }
  27. return params;
  28. }
  29. function make_trackable(url, callback, search_params_callback, allow_hash = false) {
  30. const tracked = new URL(url);
  31. Object.defineProperty(tracked, "searchParams", {
  32. value: new Proxy(tracked.searchParams, {
  33. get(obj, key) {
  34. if (key === "get" || key === "getAll" || key === "has") {
  35. return (param, ...rest) => {
  36. search_params_callback(param);
  37. return obj[key](param, ...rest);
  38. };
  39. }
  40. callback();
  41. const value = Reflect.get(obj, key);
  42. return typeof value === "function" ? value.bind(obj) : value;
  43. }
  44. }),
  45. enumerable: true,
  46. configurable: true
  47. });
  48. const tracked_url_properties = ["href", "pathname", "search", "toString", "toJSON"];
  49. if (allow_hash) tracked_url_properties.push("hash");
  50. for (const property of tracked_url_properties) {
  51. Object.defineProperty(tracked, property, {
  52. get() {
  53. callback();
  54. return url[property];
  55. },
  56. enumerable: true,
  57. configurable: true
  58. });
  59. }
  60. {
  61. tracked[Symbol.for("nodejs.util.inspect.custom")] = (depth, opts, inspect) => {
  62. return inspect(url, opts);
  63. };
  64. tracked.searchParams[Symbol.for("nodejs.util.inspect.custom")] = (depth, opts, inspect) => {
  65. return inspect(url.searchParams, opts);
  66. };
  67. }
  68. if (!allow_hash) {
  69. disable_hash(tracked);
  70. }
  71. return tracked;
  72. }
  73. function disable_hash(url) {
  74. allow_nodejs_console_log(url);
  75. Object.defineProperty(url, "hash", {
  76. get() {
  77. throw new Error(
  78. "Cannot access event.url.hash. Consider using `page.url.hash` inside a component instead"
  79. );
  80. }
  81. });
  82. }
  83. function disable_search(url) {
  84. allow_nodejs_console_log(url);
  85. for (const property of ["search", "searchParams"]) {
  86. Object.defineProperty(url, property, {
  87. get() {
  88. throw new Error(`Cannot access url.${property} on a page with prerendering enabled`);
  89. }
  90. });
  91. }
  92. }
  93. function allow_nodejs_console_log(url) {
  94. {
  95. url[Symbol.for("nodejs.util.inspect.custom")] = (depth, opts, inspect) => {
  96. return inspect(new URL(url), opts);
  97. };
  98. }
  99. }
  100. const subscriber_queue = [];
  101. function readable(value, start) {
  102. return {
  103. subscribe: writable(value, start).subscribe
  104. };
  105. }
  106. function writable(value, start = noop) {
  107. let stop = null;
  108. const subscribers = /* @__PURE__ */ new Set();
  109. function set(new_value) {
  110. if (safe_not_equal(value, new_value)) {
  111. value = new_value;
  112. if (stop) {
  113. const run_queue = !subscriber_queue.length;
  114. for (const subscriber of subscribers) {
  115. subscriber[1]();
  116. subscriber_queue.push(subscriber, value);
  117. }
  118. if (run_queue) {
  119. for (let i = 0; i < subscriber_queue.length; i += 2) {
  120. subscriber_queue[i][0](subscriber_queue[i + 1]);
  121. }
  122. subscriber_queue.length = 0;
  123. }
  124. }
  125. }
  126. }
  127. function update(fn) {
  128. set(fn(
  129. /** @type {T} */
  130. value
  131. ));
  132. }
  133. function subscribe(run, invalidate = noop) {
  134. const subscriber = [run, invalidate];
  135. subscribers.add(subscriber);
  136. if (subscribers.size === 1) {
  137. stop = start(set, update) || noop;
  138. }
  139. run(
  140. /** @type {T} */
  141. value
  142. );
  143. return () => {
  144. subscribers.delete(subscriber);
  145. if (subscribers.size === 0 && stop) {
  146. stop();
  147. stop = null;
  148. }
  149. };
  150. }
  151. return { set, update, subscribe };
  152. }
  153. function validator(expected) {
  154. function validate(module, file) {
  155. if (!module) return;
  156. for (const key in module) {
  157. if (key[0] === "_" || expected.has(key)) continue;
  158. const values = [...expected.values()];
  159. const hint = hint_for_supported_files(key, file?.slice(file.lastIndexOf("."))) ?? `valid exports are ${values.join(", ")}, or anything with a '_' prefix`;
  160. throw new Error(`Invalid export '${key}'${file ? ` in ${file}` : ""} (${hint})`);
  161. }
  162. }
  163. return validate;
  164. }
  165. function hint_for_supported_files(key, ext = ".js") {
  166. const supported_files = [];
  167. if (valid_layout_exports.has(key)) {
  168. supported_files.push(`+layout${ext}`);
  169. }
  170. if (valid_page_exports.has(key)) {
  171. supported_files.push(`+page${ext}`);
  172. }
  173. if (valid_layout_server_exports.has(key)) {
  174. supported_files.push(`+layout.server${ext}`);
  175. }
  176. if (valid_page_server_exports.has(key)) {
  177. supported_files.push(`+page.server${ext}`);
  178. }
  179. if (valid_server_exports.has(key)) {
  180. supported_files.push(`+server${ext}`);
  181. }
  182. if (supported_files.length > 0) {
  183. return `'${key}' is a valid export in ${supported_files.slice(0, -1).join(", ")}${supported_files.length > 1 ? " or " : ""}${supported_files.at(-1)}`;
  184. }
  185. }
  186. const valid_layout_exports = /* @__PURE__ */ new Set([
  187. "load",
  188. "prerender",
  189. "csr",
  190. "ssr",
  191. "trailingSlash",
  192. "config"
  193. ]);
  194. const valid_page_exports = /* @__PURE__ */ new Set([...valid_layout_exports, "entries"]);
  195. const valid_layout_server_exports = /* @__PURE__ */ new Set([...valid_layout_exports]);
  196. const valid_page_server_exports = /* @__PURE__ */ new Set([...valid_layout_server_exports, "actions", "entries"]);
  197. const valid_server_exports = /* @__PURE__ */ new Set([
  198. "GET",
  199. "POST",
  200. "PATCH",
  201. "PUT",
  202. "DELETE",
  203. "OPTIONS",
  204. "HEAD",
  205. "fallback",
  206. "prerender",
  207. "trailingSlash",
  208. "config",
  209. "entries"
  210. ]);
  211. const validate_layout_exports = validator(valid_layout_exports);
  212. const validate_page_exports = validator(valid_page_exports);
  213. const validate_layout_server_exports = validator(valid_layout_server_exports);
  214. const validate_page_server_exports = validator(valid_page_server_exports);
  215. const validate_server_exports = validator(valid_server_exports);
  216. export {
  217. SCHEME as S,
  218. decode_params as a,
  219. validate_layout_exports as b,
  220. validate_page_server_exports as c,
  221. disable_search as d,
  222. validate_page_exports as e,
  223. resolve as f,
  224. decode_pathname as g,
  225. validate_server_exports as h,
  226. make_trackable as m,
  227. normalize_path as n,
  228. readable as r,
  229. validate_layout_server_exports as v,
  230. writable as w
  231. };