Fix some url jumping bugs
parent
dc83f2b8a8
commit
4178f77293
|
@ -1,48 +1,82 @@
|
||||||
export const urlPrefix: string = Deno.env.get("SB_URL_PREFIX") ??
|
const getUrlPrefix = () : string => {
|
||||||
(globalThis.silverBulletConfig
|
const prefix : string = Deno.env.get("SB_URL_PREFIX") ??
|
||||||
? globalThis.silverBulletConfig.urlPrefix
|
(globalThis.silverBulletConfig ?
|
||||||
: null) ??
|
globalThis.silverBulletConfig.urlPrefix
|
||||||
"";
|
: null) ?? '';
|
||||||
|
if (prefix === '') {
|
||||||
export const toRealUrl = <T extends (string | URL)>(url: T): T => {
|
return '';
|
||||||
if (typeof url === "string") {
|
|
||||||
const stringUrl = url as string;
|
|
||||||
if (stringUrl.startsWith("http://") || stringUrl.startsWith("https://")) {
|
|
||||||
const parsedUrl = new URL(stringUrl);
|
|
||||||
parsedUrl.pathname = urlPrefix + parsedUrl.pathname;
|
|
||||||
//console.log("Converted ", url, parsedUrl.href)
|
|
||||||
return String(parsedUrl.href) as T;
|
|
||||||
} else {
|
|
||||||
if (!stringUrl.startsWith("/")) {
|
|
||||||
console.log("Don't know how to deal with relative path: ", url);
|
|
||||||
}
|
}
|
||||||
//console.log("Converted ", url, urlPrefix + stringUrl)
|
|
||||||
|
let result = prefix;
|
||||||
|
if (!prefix.startsWith('/')) {
|
||||||
|
result = '/' + result;
|
||||||
|
}
|
||||||
|
if (prefix.endsWith('/')) {
|
||||||
|
result = result.replace(/\/*$/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const urlPrefix: string = getUrlPrefix();
|
||||||
|
|
||||||
|
const toRealUrlObject = (url : URL) : URL => {
|
||||||
|
const parsedUrl = new URL(url);
|
||||||
|
if (typeof location !== 'undefined' && parsedUrl.origin == location.origin) {
|
||||||
|
if (parsedUrl.pathname.startsWith(urlPrefix)) {
|
||||||
|
//console.trace("Path starts with prefix already: ", url);
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedUrl.pathname = urlPrefix + parsedUrl.pathname;
|
||||||
|
//console.trace("Converted full URL ", url, parsedUrl.href)
|
||||||
|
return parsedUrl;
|
||||||
|
} else {
|
||||||
|
//console.trace("Don't know how to deal with cross origin path: ", url);
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const toRealUrl = <T extends (string | URL)>(url : T) : T => {
|
||||||
|
if (typeof url === 'string') {
|
||||||
|
const stringUrl = url as string;
|
||||||
|
if (stringUrl.startsWith('http://') || stringUrl.startsWith('https://')) {
|
||||||
|
return toRealUrlObject(new URL(stringUrl)).href as T;
|
||||||
|
}
|
||||||
|
else if (!stringUrl.startsWith('/')) {
|
||||||
|
//console.trace("Don't know how to deal with relative path: ", url);
|
||||||
|
return url;
|
||||||
|
} else {
|
||||||
|
if (url.startsWith(urlPrefix)) {
|
||||||
|
//console.trace("Path starts with prefix already: ", url);
|
||||||
|
}
|
||||||
|
//console.trace("Converted absolute path ", url, urlPrefix + stringUrl)
|
||||||
return (urlPrefix + stringUrl) as T;
|
return (urlPrefix + stringUrl) as T;
|
||||||
}
|
}
|
||||||
} else if (url.protocol === "http:" || url.protocol === "https:") {
|
}
|
||||||
const parsedUrl = new URL(url as URL);
|
else if (url.protocol === 'http:' || url.protocol === 'https:') {
|
||||||
parsedUrl.pathname = urlPrefix + parsedUrl.pathname;
|
return toRealUrlObject(url) as T;
|
||||||
//console.log("Converted ", url, parsedUrl)
|
}
|
||||||
return parsedUrl as T;
|
else {
|
||||||
} else {
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const toInternalUrl = (url: string) => {
|
export const toInternalUrl = (url : string) => {
|
||||||
if (url.startsWith("http://") || url.startsWith("https://")) {
|
if (url.startsWith('http://') || url.startsWith('https://')) {
|
||||||
const parsedUrl = new URL(url);
|
const parsedUrl = new URL(url);
|
||||||
if (parsedUrl.pathname.startsWith(urlPrefix)) {
|
if (parsedUrl.pathname.startsWith(urlPrefix)) {
|
||||||
parsedUrl.pathname = parsedUrl.pathname.substr(urlPrefix.length);
|
parsedUrl.pathname = parsedUrl.pathname.substr(urlPrefix.length);
|
||||||
return parsedUrl.href;
|
return parsedUrl.href;
|
||||||
} else {
|
}
|
||||||
console.log("Don't know how to deal with non-prefix: ", url);
|
else {
|
||||||
|
//console.trace("Don't know how to deal with non-prefix: ", url);
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
} else if (url.startsWith(urlPrefix)) {
|
} else if (url.startsWith(urlPrefix)) {
|
||||||
return url.substr(urlPrefix.length);
|
return url.substr(urlPrefix.length);
|
||||||
} else {
|
}
|
||||||
console.log("Don't know how to deal with non-prefix: ", url);
|
else {
|
||||||
|
//console.trace("Don't know how to deal with non-prefix: ", url);
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -404,13 +404,14 @@ export class HttpServer {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
const url = new URL(toInternalUrl(req.url));
|
const url = new URL(toInternalUrl(req.url));
|
||||||
|
const path = toInternalUrl(req.path);
|
||||||
const host = url.host;
|
const host = url.host;
|
||||||
const redirectToAuth = () => {
|
const redirectToAuth = () => {
|
||||||
// Try filtering api paths
|
// Try filtering api paths
|
||||||
if (req.path.startsWith("/.") || req.path.endsWith(".md")) {
|
if (path.startsWith("/.") || path.endsWith(".md")) {
|
||||||
return c.redirect(toRealUrl("/.auth"), 401);
|
return c.redirect(toRealUrl("/.auth"), 401);
|
||||||
} else {
|
} else {
|
||||||
return c.redirect(toRealUrl(`/.auth?from=${req.path}`), 401);
|
return c.redirect(toRealUrl(`/.auth?from=${path}`), 401);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (!excludedPaths.includes(url.pathname)) {
|
if (!excludedPaths.includes(url.pathname)) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { safeRun } from "../lib/async.ts";
|
import { safeRun } from "../lib/async.ts";
|
||||||
import { Client } from "./client.ts";
|
import { Client } from "./client.ts";
|
||||||
|
import { toRealUrl } from "../lib/url_hack.ts";
|
||||||
|
|
||||||
const syncMode = globalThis.silverBulletConfig.syncOnly ||
|
const syncMode = globalThis.silverBulletConfig.syncOnly ||
|
||||||
!!localStorage.getItem("syncMode");
|
!!localStorage.getItem("syncMode");
|
||||||
|
@ -26,7 +27,7 @@ safeRun(async () => {
|
||||||
|
|
||||||
if (navigator.serviceWorker) {
|
if (navigator.serviceWorker) {
|
||||||
navigator.serviceWorker
|
navigator.serviceWorker
|
||||||
.register(new URL("/service_worker.js", location.href), {
|
.register(new URL(toRealUrl("/service_worker.js"), location.href), {
|
||||||
type: "module",
|
type: "module",
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
|
Loading…
Reference in New Issue