201 lines
5.8 KiB
JavaScript
201 lines
5.8 KiB
JavaScript
const internal = new URL("sveltekit-internal://");
|
|
function resolve(base, path) {
|
|
if (path[0] === "/" && path[1] === "/")
|
|
return path;
|
|
let url = new URL(base, internal);
|
|
url = new URL(path, url);
|
|
return url.protocol === internal.protocol ? url.pathname + url.search + url.hash : url.href;
|
|
}
|
|
function normalize_path(path, trailing_slash) {
|
|
if (path === "/" || trailing_slash === "ignore")
|
|
return path;
|
|
if (trailing_slash === "never") {
|
|
return path.endsWith("/") ? path.slice(0, -1) : path;
|
|
} else if (trailing_slash === "always" && !path.endsWith("/")) {
|
|
return path + "/";
|
|
}
|
|
return path;
|
|
}
|
|
function decode_pathname(pathname) {
|
|
return pathname.split("%25").map(decodeURI).join("%25");
|
|
}
|
|
function decode_params(params) {
|
|
for (const key in params) {
|
|
params[key] = decodeURIComponent(params[key]);
|
|
}
|
|
return params;
|
|
}
|
|
const tracked_url_properties = (
|
|
/** @type {const} */
|
|
[
|
|
"href",
|
|
"pathname",
|
|
"search",
|
|
"toString",
|
|
"toJSON"
|
|
]
|
|
);
|
|
function make_trackable(url, callback, search_params_callback) {
|
|
const tracked = new URL(url);
|
|
Object.defineProperty(tracked, "searchParams", {
|
|
value: new Proxy(tracked.searchParams, {
|
|
get(obj, key) {
|
|
if (key === "get" || key === "getAll" || key === "has") {
|
|
return (param) => {
|
|
search_params_callback(param);
|
|
return obj[key](param);
|
|
};
|
|
}
|
|
callback();
|
|
const value = Reflect.get(obj, key);
|
|
return typeof value === "function" ? value.bind(obj) : value;
|
|
}
|
|
}),
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
for (const property of tracked_url_properties) {
|
|
Object.defineProperty(tracked, property, {
|
|
get() {
|
|
callback();
|
|
return url[property];
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
}
|
|
{
|
|
tracked[Symbol.for("nodejs.util.inspect.custom")] = (depth, opts, inspect) => {
|
|
return inspect(url, opts);
|
|
};
|
|
}
|
|
{
|
|
disable_hash(tracked);
|
|
}
|
|
return tracked;
|
|
}
|
|
function disable_hash(url) {
|
|
allow_nodejs_console_log(url);
|
|
Object.defineProperty(url, "hash", {
|
|
get() {
|
|
throw new Error(
|
|
"Cannot access event.url.hash. Consider using `$page.url.hash` inside a component instead"
|
|
);
|
|
}
|
|
});
|
|
}
|
|
function disable_search(url) {
|
|
allow_nodejs_console_log(url);
|
|
for (const property of ["search", "searchParams"]) {
|
|
Object.defineProperty(url, property, {
|
|
get() {
|
|
throw new Error(`Cannot access url.${property} on a page with prerendering enabled`);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
function allow_nodejs_console_log(url) {
|
|
{
|
|
url[Symbol.for("nodejs.util.inspect.custom")] = (depth, opts, inspect) => {
|
|
return inspect(new URL(url), opts);
|
|
};
|
|
}
|
|
}
|
|
const DATA_SUFFIX = "/__data.json";
|
|
const HTML_DATA_SUFFIX = ".html__data.json";
|
|
function has_data_suffix(pathname) {
|
|
return pathname.endsWith(DATA_SUFFIX) || pathname.endsWith(HTML_DATA_SUFFIX);
|
|
}
|
|
function add_data_suffix(pathname) {
|
|
if (pathname.endsWith(".html"))
|
|
return pathname.replace(/\.html$/, HTML_DATA_SUFFIX);
|
|
return pathname.replace(/\/$/, "") + DATA_SUFFIX;
|
|
}
|
|
function strip_data_suffix(pathname) {
|
|
if (pathname.endsWith(HTML_DATA_SUFFIX)) {
|
|
return pathname.slice(0, -HTML_DATA_SUFFIX.length) + ".html";
|
|
}
|
|
return pathname.slice(0, -DATA_SUFFIX.length);
|
|
}
|
|
function validator(expected) {
|
|
function validate(module, file) {
|
|
if (!module)
|
|
return;
|
|
for (const key in module) {
|
|
if (key[0] === "_" || expected.has(key))
|
|
continue;
|
|
const values = [...expected.values()];
|
|
const hint = hint_for_supported_files(key, file?.slice(file.lastIndexOf("."))) ?? `valid exports are ${values.join(", ")}, or anything with a '_' prefix`;
|
|
throw new Error(`Invalid export '${key}'${file ? ` in ${file}` : ""} (${hint})`);
|
|
}
|
|
}
|
|
return validate;
|
|
}
|
|
function hint_for_supported_files(key, ext = ".js") {
|
|
const supported_files = [];
|
|
if (valid_layout_exports.has(key)) {
|
|
supported_files.push(`+layout${ext}`);
|
|
}
|
|
if (valid_page_exports.has(key)) {
|
|
supported_files.push(`+page${ext}`);
|
|
}
|
|
if (valid_layout_server_exports.has(key)) {
|
|
supported_files.push(`+layout.server${ext}`);
|
|
}
|
|
if (valid_page_server_exports.has(key)) {
|
|
supported_files.push(`+page.server${ext}`);
|
|
}
|
|
if (valid_server_exports.has(key)) {
|
|
supported_files.push(`+server${ext}`);
|
|
}
|
|
if (supported_files.length > 0) {
|
|
return `'${key}' is a valid export in ${supported_files.slice(0, -1).join(", ")}${supported_files.length > 1 ? " or " : ""}${supported_files.at(-1)}`;
|
|
}
|
|
}
|
|
const valid_layout_exports = /* @__PURE__ */ new Set([
|
|
"load",
|
|
"prerender",
|
|
"csr",
|
|
"ssr",
|
|
"trailingSlash",
|
|
"config"
|
|
]);
|
|
const valid_page_exports = /* @__PURE__ */ new Set([...valid_layout_exports, "entries"]);
|
|
const valid_layout_server_exports = /* @__PURE__ */ new Set([...valid_layout_exports]);
|
|
const valid_page_server_exports = /* @__PURE__ */ new Set([...valid_layout_server_exports, "actions", "entries"]);
|
|
const valid_server_exports = /* @__PURE__ */ new Set([
|
|
"GET",
|
|
"POST",
|
|
"PATCH",
|
|
"PUT",
|
|
"DELETE",
|
|
"OPTIONS",
|
|
"HEAD",
|
|
"fallback",
|
|
"prerender",
|
|
"trailingSlash",
|
|
"config",
|
|
"entries"
|
|
]);
|
|
const validate_layout_exports = validator(valid_layout_exports);
|
|
const validate_page_exports = validator(valid_page_exports);
|
|
const validate_layout_server_exports = validator(valid_layout_server_exports);
|
|
const validate_page_server_exports = validator(valid_page_server_exports);
|
|
const validate_server_exports = validator(valid_server_exports);
|
|
export {
|
|
add_data_suffix as a,
|
|
decode_pathname as b,
|
|
decode_params as c,
|
|
disable_search as d,
|
|
validate_layout_exports as e,
|
|
validate_page_server_exports as f,
|
|
validate_page_exports as g,
|
|
has_data_suffix as h,
|
|
validate_server_exports as i,
|
|
make_trackable as m,
|
|
normalize_path as n,
|
|
resolve as r,
|
|
strip_data_suffix as s,
|
|
validate_layout_server_exports as v
|
|
};
|